Graphviz 16.0.1~dev.20260815.2250
Loading...
Searching...
No Matches
ink.cpp
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "config.h"
12
13#include <algorithm>
14#include <cmath>
15#include <cstdlib>
16#include <common/types.h>
17#include <common/globals.h>
18#include <mingle/ink.h>
19#include <vector>
20
21double ink_count;
22
24{
25 a.x += b.x;
26 a.y += b.y;
27 return a;
28}
29
31{
32 a.x -= b.x;
33 a.y -= b.y;
34 return a;
35}
36
37static point_t scalePoint (point_t a, double d)
38{
39 a.x *= d;
40 a.y *= d;
41 return a;
42}
43
44static double dotPoint(point_t a, point_t b){
45 return a.x*b.x + a.y*b.y;
46}
47
48static const point_t Origin = {0, 0};
49
50/* sumLengths:
51 */
52static double sumLengths_avoid_bad_angle(const std::vector<point_t> &points,
53 point_t end, point_t meeting,
54 double angle_param) {
55 /* avoid sharp turns, we want cos_theta to be as close to -1 as possible */
56 double len0, len, sum = 0;
57 double diff_x, diff_y, diff_x0, diff_y0;
58 double cos_theta, cos_max = -10;
59
60 diff_x0 = end.x-meeting.x;
61 diff_y0 = end.y-meeting.y;
62 len0 = sum = hypot(diff_x0, diff_y0);
63
64 // distance form each of 'points' till 'meeting'
65 for (const point_t &p : points) {
66 diff_x = p.x - meeting.x;
67 diff_y = p.y - meeting.y;
68 len = hypot(diff_x, diff_y);
69 sum += len;
70 cos_theta = (diff_x0 * diff_x + diff_y0 * diff_y)
71 / std::max(len * len0, 0.00001);
72 cos_max = std::max(cos_max, cos_theta);
73 }
74
75 // distance of single line from 'meeting' to 'end'
76 return sum*(cos_max + angle_param);/* straight line gives angle_param - 1, turning angle of 180 degree gives angle_param + 1 */
77}
78static double sumLengths(const std::vector<point_t> &points, point_t end,
79 point_t meeting) {
80 double sum = 0;
81 double diff_x, diff_y;
82
83 // distance form each of 'points' till 'meeting'
84 for (const point_t &p : points) {
85 diff_x = p.x - meeting.x;
86 diff_y = p.y - meeting.y;
87 sum += hypot(diff_x, diff_y);
88 }
89 // distance of single line from 'meeting' to 'end'
90 diff_x = end.x-meeting.x;
91 diff_y = end.y-meeting.y;
92 sum += hypot(diff_x, diff_y);
93 return sum;
94}
95
96/* bestInk:
97 */
98static double bestInk(const std::vector<point_t> &points, point_t begin,
99 point_t end, double prec, point_t *meet,
100 double angle_param) {
101 point_t first, second, third, fourth, diff, meeting;
102 double value1, value2, value3, value4;
103
104 first = begin;
105 fourth = end;
106
107 do {
108 diff = subPoint(fourth,first);
109 second = addPoint(first,scalePoint(diff,1.0/3.0));
110 third = addPoint(first,scalePoint(diff,2.0/3.0));
111
112 if (angle_param < 1){
113 value1 = sumLengths(points, end, first);
114 value2 = sumLengths(points, end, second);
115 value3 = sumLengths(points, end, third);
116 value4 = sumLengths(points, end, fourth);
117 } else {
118 value1 = sumLengths_avoid_bad_angle(points, end, first, angle_param);
119 value2 = sumLengths_avoid_bad_angle(points, end, second, angle_param);
120 value3 = sumLengths_avoid_bad_angle(points, end, third, angle_param);
121 value4 = sumLengths_avoid_bad_angle(points, end, fourth, angle_param);
122 }
123
124 if (value1<value2) {
125 if (value1<value3) {
126 if (value1<value4) {
127 // first is smallest
128 fourth = second;
129 }
130 else {
131 // fourth is smallest
132 first = third;
133 }
134 }
135 else {
136 if (value3<value4) {
137 // third is smallest
138 first = second;
139 }
140 else {
141 // fourth is smallest
142 first = third;
143 }
144 }
145 }
146 else {
147 if (value2<value3) {
148 if (value2<value4) {
149 // second is smallest
150 fourth = third;
151 }
152 else {
153 // fourth is smallest
154 first = third;
155 }
156 }
157 else {
158 if (value3<value4) {
159 // third is smallest
160 first = second;
161 }
162 else {
163 // fourth is smallest
164 first = third;
165 }
166 }
167 }
168 } while (fabs(value1 - value4) / (std::min(value1, value4) + 1e-10) > prec
169 && dotPoint(diff, diff) > 1.e-20);
170
171 meeting = scalePoint(addPoint(first,fourth),0.5);
172 *meet = meeting;
173
174 return sumLengths(points, end, meeting);
175
176}
177
178static double project_to_line(point_t pt, point_t left, point_t right, double angle){
179 /* pt
180 ^ ^
181 . \ \
182 . \ \
183 d . a\ \
184 . \ \
185 . \ \
186 . c \ alpha \ b
187 .<------left:0 ----------------------------> right:1. Find the projection of pt on the left--right line. If the turning angle is small,
188 | |
189 |<-------f---------
190 we should get a negative number. Let a := left->pt, b := left->right, then we are calculating:
191 c = |a| cos(a,b)/|b| b
192 d = a - c
193 f = -ctan(alpha)*|d|/|b| b
194 and the project of alpha degree on the left->right line is
195 c-f = |a| cos(a,b)/|b| b - -ctan(alpha)*|d|/|b| b
196 = (|a| a.b/(|a||b|) + ctan(alpha)|a-c|)/|b| b
197 = (a.b/|b| + ctan(alpha)|a-c|)/|b| b
198 the dimentionless projection is:
199 a.b/|b|^2 + ctan(alpha)|a-c|/|b|
200 = a.b/|b|^2 + ctan(alpha)|d|/|b|
201 */
202
203
204 point_t b, a;
205 double bnorm, dnorm;
206 double alpha, ccord;
207
208 if (angle <=0 || angle >= M_PI) return 2;/* return outside of the interval which should be handled as a sign of infeasible turning angle */
209 alpha = angle;
210
211 assert(alpha > 0 && alpha < M_PI);
212 b = subPoint(right, left);
213 a = subPoint(pt, left);
214 bnorm = std::max(1.e-10, dotPoint(b, b));
215 ccord = dotPoint(b, a)/bnorm;
216 dnorm = dotPoint(a,a)/bnorm - ccord*ccord;
217 if (alpha == M_PI/2){
218 return ccord;
219 }
220 return ccord + sqrt(std::max(0.0, dnorm)) / tan(alpha);
221}
222
223/* ink:
224 * Compute minimal ink used the input edges are bundled.
225 * Assumes tails all occur on one side and heads on the other.
226 */
227double ink(const std::vector<pedge> &edges, int numEdges, int *pick,
228 double *ink0, point_t *meet1, point_t *meet2, double angle_param,
229 double angle) {
230 int i;
231 point_t begin, end, mid, diff;
232 double inkUsed;
233 double eps = 1.0e-2;
234 double cend = 0, cbegin = 0;
235 double wgt = 0;
236
237 ink_count += numEdges;
238
239 *ink0 = 0;
240
241 /* canonicalize so that edges 1,2,3 and 3,2,1 gives the same optimal ink */
242 if (pick) std::sort(pick, pick + numEdges);
243
244 begin = end = Origin;
245 for (i = 0; i < numEdges; i++) {
246 const pedge &e = pick ? edges[pick[i]] : edges[i];
247 const std::vector<double> &x = e.x;
248 point_t source = {x[0], x[1]};
249 point_t target = {x[e.dim * e.npoints - e.dim],
250 x[e.dim * e.npoints - e.dim + 1]};
251 (*ink0) += hypot(source.x - target.x, source.y - target.y);
252 begin = addPoint(begin, scalePoint(source, e.wgt));
253 end = addPoint(end, scalePoint(target, e.wgt));
254 wgt += e.wgt;
255 }
256
257 begin = scalePoint (begin, 1.0/wgt);
258 end = scalePoint (end, 1.0/wgt);
259
260
261 if (numEdges == 1){
262 *meet1 = begin;
263 *meet2 = end;
264 return *ink0;
265 }
266
267 /* shift the begin and end point to avoid sharp turns */
268 std::vector<point_t> sources;
269 std::vector<point_t> targets;
270 for (i = 0; i < numEdges; i++) {
271 const pedge &e = pick ? edges[pick[i]] : edges[i];
272 const std::vector<double> &x = e.x;
273 sources.push_back(point_t{x[0], x[1]});
274 targets.push_back(point_t{x[e.dim * e.npoints - e.dim],
275 x[e.dim * e.npoints - e.dim + 1]});
276 /* begin(1) ----------- mid(0) */
277 if (i == 0){
278 cbegin = project_to_line(sources[i], begin, end, angle);
279 cend = project_to_line(targets[i], end, begin, angle);
280 } else {
281 cbegin = std::max(cbegin, project_to_line(sources[i], begin, end, angle));
282 cend = std::max(cend, project_to_line(targets[i], end, begin, angle));
283 }
284 }
285
286 if (angle > 0 && angle < M_PI){
287 if (cbegin + cend > 1 || cbegin > 1 || cend > 1){
288 /* no point can be found that satisfies the angular constraints, so we give up and set ink to a large value */
289 inkUsed = 1000*(*ink0);
290 return inkUsed;
291 }
292 /* make sure the turning angle is no more than alpha degree */
293 cbegin = std::max(0.0, cbegin);/* make sure the new adjusted point is with in [begin,end] internal */
294 diff = subPoint(end, begin);
295 begin = addPoint(begin, scalePoint(diff, cbegin));
296
297 cend = std::max(0.0, cend);/* make sure the new adjusted point is with in [end,begin] internal */
298 end = subPoint(end, scalePoint(diff, cend));
299 }
300 mid = scalePoint (addPoint(begin,end),0.5);
301
302 inkUsed = bestInk(sources, begin, mid, eps, meet1, angle_param)
303 + bestInk(targets, end, mid, eps, meet2, angle_param);
304
305 return inkUsed;
306}
307
308double ink1(const pedge &e) {
309 double ink0 = 0;
310
311 const std::vector<double> &x = e.x;
312 const double xx = x[0] - x[e.dim * e.npoints - e.dim];
313 const double yy = x[1] - x[e.dim * e.npoints - e.dim + 1];
314 ink0 += hypot(xx, yy);
315 return ink0;
316}
#define M_PI
Definition arith.h:41
#define right(i)
Definition closest.c:74
#define left
Definition dthdr.h:12
static double len(glCompPoint p)
Definition glutils.c:138
static gdPoint * points
static point_t addPoint(point_t a, point_t b)
Definition ink.cpp:23
static double project_to_line(point_t pt, point_t left, point_t right, double angle)
Definition ink.cpp:178
static double sumLengths_avoid_bad_angle(const std::vector< point_t > &points, point_t end, point_t meeting, double angle_param)
Definition ink.cpp:52
double ink(const std::vector< pedge > &edges, int numEdges, int *pick, double *ink0, point_t *meet1, point_t *meet2, double angle_param, double angle)
Definition ink.cpp:227
static double dotPoint(point_t a, point_t b)
Definition ink.cpp:44
static point_t scalePoint(point_t a, double d)
Definition ink.cpp:37
static point_t subPoint(point_t a, point_t b)
Definition ink.cpp:30
static double sumLengths(const std::vector< point_t > &points, point_t end, point_t meeting)
Definition ink.cpp:78
double ink_count
Definition ink.cpp:21
static const point_t Origin
Definition ink.cpp:48
static double bestInk(const std::vector< point_t > &points, point_t begin, point_t end, double prec, point_t *meet, double angle_param)
Definition ink.cpp:98
double ink1(const pedge &e)
Definition ink.cpp:308
#define alpha
Definition shapes.c:4034
std::vector< double > x
coordinates of the npoints poly points. Dimension dim*npoints
double wgt
int npoints
Definition ink.h:16
double y
Definition ink.h:17
double x
Definition ink.h:17
graphs, nodes and edges info: Agraphinfo_t, Agnodeinfo_t and Agedgeinfo_t