Graphviz 13.0.0~dev.20241220.2304
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 }
219 return ccord + sqrt(std::max(0.0, dnorm)) / tan(alpha);
220}
221
222/* ink:
223 * Compute minimal ink used the input edges are bundled.
224 * Assumes tails all occur on one side and heads on the other.
225 */
226double ink(const std::vector<pedge> &edges, int numEdges, int *pick,
227 double *ink0, point_t *meet1, point_t *meet2, double angle_param,
228 double angle) {
229 int i;
230 point_t begin, end, mid, diff;
231 double inkUsed;
232 double eps = 1.0e-2;
233 double cend = 0, cbegin = 0;
234 double wgt = 0;
235
236 ink_count += numEdges;
237
238 *ink0 = 0;
239
240 /* canonicalize so that edges 1,2,3 and 3,2,1 gives the same optimal ink */
241 if (pick) vector_sort_int(numEdges, pick);
242
243 begin = end = Origin;
244 for (i = 0; i < numEdges; i++) {
245 const pedge &e = pick ? edges[pick[i]] : edges[i];
246 const std::vector<double> &x = e.x;
247 point_t source = {x[0], x[1]};
248 point_t target = {x[e.dim * e.npoints - e.dim],
249 x[e.dim * e.npoints - e.dim + 1]};
250 (*ink0) += hypot(source.x - target.x, source.y - target.y);
251 begin = addPoint(begin, scalePoint(source, e.wgt));
252 end = addPoint(end, scalePoint(target, e.wgt));
253 wgt += e.wgt;
254 }
255
256 begin = scalePoint (begin, 1.0/wgt);
257 end = scalePoint (end, 1.0/wgt);
258
259
260 if (numEdges == 1){
261 *meet1 = begin;
262 *meet2 = end;
263 return *ink0;
264 }
265
266 /* shift the begin and end point to avoid sharp turns */
267 std::vector<point_t> sources;
268 std::vector<point_t> targets;
269 for (i = 0; i < numEdges; i++) {
270 const pedge &e = pick ? edges[pick[i]] : edges[i];
271 const std::vector<double> &x = e.x;
272 sources.push_back(point_t{x[0], x[1]});
273 targets.push_back(point_t{x[e.dim * e.npoints - e.dim],
274 x[e.dim * e.npoints - e.dim + 1]});
275 /* begin(1) ----------- mid(0) */
276 if (i == 0){
277 cbegin = project_to_line(sources[i], begin, end, angle);
278 cend = project_to_line(targets[i], end, begin, angle);
279 } else {
280 cbegin = std::max(cbegin, project_to_line(sources[i], begin, end, angle));
281 cend = std::max(cend, project_to_line(targets[i], end, begin, angle));
282 }
283 }
284
285 if (angle > 0 && angle < M_PI){
286 if (cbegin + cend > 1 || cbegin > 1 || cend > 1){
287 /* no point can be found that satisfies the angular constraints, so we give up and set ink to a large value */
288 inkUsed = 1000*(*ink0);
289 return inkUsed;
290 }
291 /* make sure the turning angle is no more than alpha degree */
292 cbegin = std::max(0.0, cbegin);/* make sure the new adjusted point is with in [begin,end] internal */
293 diff = subPoint(end, begin);
294 begin = addPoint(begin, scalePoint(diff, cbegin));
295
296 cend = std::max(0.0, cend);/* make sure the new adjusted point is with in [end,begin] internal */
297 end = subPoint(end, scalePoint(diff, cend));
298 }
299 mid = scalePoint (addPoint(begin,end),0.5);
300
301 inkUsed = bestInk(sources, begin, mid, eps, meet1, angle_param)
302 + bestInk(targets, end, mid, eps, meet2, angle_param);
303
304 return inkUsed;
305}
306
307double ink1(const pedge &e) {
308 double ink0 = 0;
309
310 const std::vector<double> &x = e.x;
311 const double xx = x[0] - x[e.dim * e.npoints - e.dim];
312 const double yy = x[1] - x[e.dim * e.npoints - e.dim + 1];
313 ink0 += hypot(xx, yy);
314 return ink0;
315}
#define M_PI
Definition arith.h:41
#define right(i)
Definition closest.c:79
#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:226
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:307
#define alpha
Definition shapes.c:4058
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