Graphviz 12.0.1~dev.20240716.0800
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 v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include <algorithm>
12#include <cmath>
13#include <cstdlib>
14#include <common/types.h>
15#include <common/globals.h>
16#include <sparse/general.h>
17#include <mingle/ink.h>
18#include <vector>
19
20double ink_count;
21
23{
24 a.x += b.x;
25 a.y += b.y;
26 return a;
27}
28
30{
31 a.x -= b.x;
32 a.y -= b.y;
33 return a;
34}
35
36static point_t scalePoint (point_t a, double d)
37{
38 a.x *= d;
39 a.y *= d;
40 return a;
41}
42
43static double dotPoint(point_t a, point_t b){
44 return a.x*b.x + a.y*b.y;
45}
46
47static const point_t Origin = {0, 0};
48
49/* sumLengths:
50 */
51static double sumLengths_avoid_bad_angle(const std::vector<point_t> &points,
52 point_t end, point_t meeting,
53 double angle_param) {
54 /* avoid sharp turns, we want cos_theta to be as close to -1 as possible */
55 double len0, len, sum = 0;
56 double diff_x, diff_y, diff_x0, diff_y0;
57 double cos_theta, cos_max = -10;
58
59 diff_x0 = end.x-meeting.x;
60 diff_y0 = end.y-meeting.y;
61 len0 = sum = hypot(diff_x0, diff_y0);
62
63 // distance form each of 'points' till 'meeting'
64 for (const point_t &p : points) {
65 diff_x = p.x - meeting.x;
66 diff_y = p.y - meeting.y;
67 len = hypot(diff_x, diff_y);
68 sum += len;
69 cos_theta = (diff_x0 * diff_x + diff_y0 * diff_y)
70 / std::max(len * len0, 0.00001);
71 cos_max = std::max(cos_max, cos_theta);
72 }
73
74 // distance of single line from 'meeting' to 'end'
75 return sum*(cos_max + angle_param);/* straight line gives angle_param - 1, turning angle of 180 degree gives angle_param + 1 */
76}
77static double sumLengths(const std::vector<point_t> &points, point_t end,
78 point_t meeting) {
79 double sum = 0;
80 double diff_x, diff_y;
81
82 // distance form each of 'points' till 'meeting'
83 for (const point_t &p : points) {
84 diff_x = p.x - meeting.x;
85 diff_y = p.y - meeting.y;
86 sum += hypot(diff_x, diff_y);
87 }
88 // distance of single line from 'meeting' to 'end'
89 diff_x = end.x-meeting.x;
90 diff_y = end.y-meeting.y;
91 sum += hypot(diff_x, diff_y);
92 return sum;
93}
94
95/* bestInk:
96 */
97static double bestInk(const std::vector<point_t> &points, point_t begin,
98 point_t end, double prec, point_t *meet,
99 double angle_param) {
100 point_t first, second, third, fourth, diff, meeting;
101 double value1, value2, value3, value4;
102
103 first = begin;
104 fourth = end;
105
106 do {
107 diff = subPoint(fourth,first);
108 second = addPoint(first,scalePoint(diff,1.0/3.0));
109 third = addPoint(first,scalePoint(diff,2.0/3.0));
110
111 if (angle_param < 1){
112 value1 = sumLengths(points, end, first);
113 value2 = sumLengths(points, end, second);
114 value3 = sumLengths(points, end, third);
115 value4 = sumLengths(points, end, fourth);
116 } else {
117 value1 = sumLengths_avoid_bad_angle(points, end, first, angle_param);
118 value2 = sumLengths_avoid_bad_angle(points, end, second, angle_param);
119 value3 = sumLengths_avoid_bad_angle(points, end, third, angle_param);
120 value4 = sumLengths_avoid_bad_angle(points, end, fourth, angle_param);
121 }
122
123 if (value1<value2) {
124 if (value1<value3) {
125 if (value1<value4) {
126 // first is smallest
127 fourth = second;
128 }
129 else {
130 // fourth is smallest
131 first = third;
132 }
133 }
134 else {
135 if (value3<value4) {
136 // third is smallest
137 first = second;
138 }
139 else {
140 // fourth is smallest
141 first = third;
142 }
143 }
144 }
145 else {
146 if (value2<value3) {
147 if (value2<value4) {
148 // second is smallest
149 fourth = third;
150 }
151 else {
152 // fourth is smallest
153 first = third;
154 }
155 }
156 else {
157 if (value3<value4) {
158 // third is smallest
159 first = second;
160 }
161 else {
162 // fourth is smallest
163 first = third;
164 }
165 }
166 }
167 } while (fabs(value1 - value4) / (std::min(value1, value4) + 1e-10) > prec
168 && dotPoint(diff, diff) > 1.e-20);
169
170 meeting = scalePoint(addPoint(first,fourth),0.5);
171 *meet = meeting;
172
173 return sumLengths(points, end, meeting);
174
175}
176
177static double project_to_line(point_t pt, point_t left, point_t right, double angle){
178 /* pt
179 ^ ^
180 . \ \
181 . \ \
182 d . a\ \
183 . \ \
184 . \ \
185 . c \ alpha \ b
186 .<------left:0 ----------------------------> right:1. Find the projection of pt on the left--right line. If the turning angle is small,
187 | |
188 |<-------f---------
189 we should get a negative number. Let a := left->pt, b := left->right, then we are calculating:
190 c = |a| cos(a,b)/|b| b
191 d = a - c
192 f = -ctan(alpha)*|d|/|b| b
193 and the project of alpha degree on the left->right line is
194 c-f = |a| cos(a,b)/|b| b - -ctan(alpha)*|d|/|b| b
195 = (|a| a.b/(|a||b|) + ctan(alpha)|a-c|)/|b| b
196 = (a.b/|b| + ctan(alpha)|a-c|)/|b| b
197 the dimentionless projection is:
198 a.b/|b|^2 + ctan(alpha)|a-c|/|b|
199 = a.b/|b|^2 + ctan(alpha)|d|/|b|
200 */
201
202
203 point_t b, a;
204 double bnorm, dnorm;
205 double alpha, ccord;
206
207 if (angle <=0 || angle >= M_PI) return 2;/* return outside of the interval which should be handled as a sign of infeasible turning angle */
208 alpha = angle;
209
210 assert(alpha > 0 && alpha < M_PI);
211 b = subPoint(right, left);
212 a = subPoint(pt, left);
213 bnorm = std::max(1.e-10, dotPoint(b, b));
214 ccord = dotPoint(b, a)/bnorm;
215 dnorm = dotPoint(a,a)/bnorm - ccord*ccord;
216 if (alpha == M_PI/2){
217 return ccord;
218 } else {
219 return ccord + sqrt(std::max(0.0, dnorm)) / tan(alpha);
220 }
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) vector_sort_int(numEdges, pick);
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:77
#define left
Definition dthdr.h:12
void vector_sort_int(int n, int *v)
Definition general.c:136
static double len(glCompPoint p)
Definition glutils.c:150
static gdPoint * points
static point_t addPoint(point_t a, point_t b)
Definition ink.cpp:22
static double project_to_line(point_t pt, point_t left, point_t right, double angle)
Definition ink.cpp:177
static double sumLengths_avoid_bad_angle(const std::vector< point_t > &points, point_t end, point_t meeting, double angle_param)
Definition ink.cpp:51
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:43
static point_t scalePoint(point_t a, double d)
Definition ink.cpp:36
static point_t subPoint(point_t a, point_t b)
Definition ink.cpp:29
static double sumLengths(const std::vector< point_t > &points, point_t end, point_t meeting)
Definition ink.cpp:77
double ink_count
Definition ink.cpp:20
static const point_t Origin
Definition ink.cpp:47
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:97
double ink1(const pedge &e)
Definition ink.cpp:308
#define alpha
Definition shapes.c:4068
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