Graphviz 16.1.1~dev.20261012.0359
Loading...
Searching...
No Matches
routespl.c
Go to the documentation of this file.
1
3/*************************************************************************
4 * Copyright (c) 2011 AT&T Intellectual Property
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v2.0
7 * which accompanies this distribution, and is available at
8 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
9 *
10 * Contributors: Details at https://graphviz.org
11 *************************************************************************/
12
13#include "config.h"
14#include <assert.h>
15#include <common/geomprocs.h>
16#include <common/render.h>
17#include <float.h>
18#include <limits.h>
19#include <math.h>
20#include <pathplan/pathplan.h>
21#include <stdbool.h>
22#include <stdint.h>
23#include <stdlib.h>
24#include <string.h>
25#include <util/agxbuf.h>
26#include <util/alloc.h>
27#include <util/debug.h>
28#include <util/gv_math.h>
29#include <util/list.h>
30#include <util/overflow.h>
31#include <util/prisize_t.h>
32
33static int nedges;
34static size_t nboxes;
35
36static int routeinit;
37
38static int checkpath(size_t, boxf *, path *);
39static void printpath(path *pp);
40#ifdef DEBUG
41static void printboxes(size_t boxn, boxf *boxes) {
42 pointf ll, ur;
43
44 for (size_t bi = 0; bi < boxn; bi++) {
45 ll = boxes[bi].LL, ur = boxes[bi].UR;
46 agxbuf buf = {0};
47 agxbprint(&buf, "%.0f %.0f %.0f %.0f pathbox", ll.x, ll.y, ur.x, ur.y);
49 }
50}
51
52#if DEBUG > 1
53static void psprintpolypts(Ppoint_t *p, int sz) {
54 int i;
55
56 fprintf(stderr, "%%!\n");
57 fprintf(stderr, "%% constraint poly\n");
58 fprintf(stderr, "newpath\n");
59 for (i = 0; i < sz; i++)
60 fprintf(stderr, "%f %f %s\n", p[i].x, p[i].y, i == 0 ? "moveto" : "lineto");
61 fprintf(stderr, "closepath stroke\n");
62}
63static void psprintpoint(point p) {
64 fprintf(stderr, "gsave\n");
65 fprintf(stderr,
66 "newpath %d %d moveto %d %d 2 0 360 arc closepath fill stroke\n", p.x,
67 p.y, p.x, p.y);
68 fprintf(stderr, "/Times-Roman findfont 4 scalefont setfont\n");
69 fprintf(stderr, "%d %d moveto (\\(%d,%d\\)) show\n", p.x + 5, p.y + 5, p.x,
70 p.y);
71 fprintf(stderr, "grestore\n");
72}
73static void psprintpointf(pointf p) {
74 fprintf(stderr, "gsave\n");
75 fprintf(
76 stderr,
77 "newpath %.5g %.5g moveto %.5g %.5g 2 0 360 arc closepath fill stroke\n",
78 p.x, p.y, p.x, p.y);
79 fprintf(stderr, "/Times-Roman findfont 4 scalefont setfont\n");
80 fprintf(stderr, "%.5g %.5g moveto (\\(%.5g,%.5g\\)) show\n", p.x + 5, p.y + 5,
81 p.x, p.y);
82 fprintf(stderr, "grestore\n");
83}
84#endif
85
86static void psprintspline(Ppolyline_t spl) {
88 LIST_APPEND(&Show_boxes, gv_strdup("%% spline"));
89 LIST_APPEND(&Show_boxes, gv_strdup("gsave 1 0 0 setrgbcolor newpath"));
90 for (size_t i = 0; i < spl.pn; i++) {
91 agxbuf buf = {0};
92 agxbprint(&buf, "%f %f %s", spl.ps[i].x, spl.ps[i].y,
93 i == 0 ? "moveto" : (i % 3 == 0 ? "curveto" : ""));
95 }
96 LIST_APPEND(&Show_boxes, gv_strdup("stroke grestore"));
97}
98
99static void psprintline(Ppolyline_t pl) {
101 LIST_APPEND(&Show_boxes, gv_strdup("%% line"));
102 LIST_APPEND(&Show_boxes, gv_strdup("gsave 0 0 1 setrgbcolor newpath"));
103 for (size_t i = 0; i < pl.pn; i++) {
104 agxbuf buf = {0};
105 agxbprint(&buf, "%f %f %s", pl.ps[i].x, pl.ps[i].y,
106 i == 0 ? "moveto" : "lineto");
108 }
109 LIST_APPEND(&Show_boxes, gv_strdup("stroke grestore"));
110}
111
112static void psprintpoly(Ppoly_t p) {
113 char *pfx;
114
115 LIST_APPEND(&Show_boxes, gv_strdup("%% poly list"));
116 LIST_APPEND(&Show_boxes, gv_strdup("gsave 0 1 0 setrgbcolor"));
117 for (size_t bi = 0; bi < p.pn; bi++) {
118 const pointf tail = p.ps[bi];
119 const pointf head = p.ps[(bi + 1) % p.pn];
120 if (fabs(tail.x - head.x) < 1 && fabs(tail.y - head.y) < 1)
121 pfx = "%%";
122 else
123 pfx = "";
124 agxbuf buf = {0};
125 agxbprint(&buf, "%s%.0f %.0f %.0f %.0f makevec", pfx, tail.x, tail.y,
126 head.x, head.y);
128 }
129 LIST_APPEND(&Show_boxes, gv_strdup("grestore"));
130}
131
132static void psprintboxes(size_t boxn, boxf *boxes) {
133 pointf ll, ur;
134
135 LIST_APPEND(&Show_boxes, gv_strdup("%% box list"));
136 LIST_APPEND(&Show_boxes, gv_strdup("gsave 0 1 0 setrgbcolor"));
137 for (size_t bi = 0; bi < boxn; bi++) {
138 ll = boxes[bi].LL, ur = boxes[bi].UR;
139 agxbuf buf = {0};
140 agxbprint(&buf, "newpath\n%.0f %.0f moveto", ll.x, ll.y);
142 agxbprint(&buf, "%.0f %.0f lineto", ll.x, ur.y);
144 agxbprint(&buf, "%.0f %.0f lineto", ur.x, ur.y);
146 agxbprint(&buf, "%.0f %.0f lineto", ur.x, ll.y);
148 LIST_APPEND(&Show_boxes, gv_strdup("closepath stroke"));
149 }
150 LIST_APPEND(&Show_boxes, gv_strdup("grestore"));
151}
152
153static void psprintinit(int begin) {
154 if (begin)
155 LIST_APPEND(&Show_boxes, gv_strdup("dbgstart"));
156 else
157 LIST_APPEND(&Show_boxes, gv_strdup("grestore"));
158}
159
160static bool debugleveln(edge_t *realedge, int i) {
161 return GD_showboxes(agraphof(aghead(realedge))) == i ||
162 GD_showboxes(agraphof(agtail(realedge))) == i ||
163 ED_showboxes(realedge) == i || ND_showboxes(aghead(realedge)) == i ||
164 ND_showboxes(agtail(realedge)) == i;
165}
166#endif /* DEBUG */
167
169pointf *simpleSplineRoute(pointf tp, pointf hp, Ppoly_t poly, size_t *n_spl_pts,
170 int polyline) {
171 Ppolyline_t pl, spl;
172 Ppoint_t eps[2];
173
174 eps[0].x = tp.x;
175 eps[0].y = tp.y;
176 eps[1].x = hp.x;
177 eps[1].y = hp.y;
178 if (Pshortestpath(&poly, eps, &pl) < 0)
179 return NULL;
180
181 if (polyline)
182 make_polyline(pl, &spl);
183 else {
184 // polygon edges passed to Proutespline
185 Pedge_t *edges = gv_calloc(poly.pn, sizeof(Pedge_t));
186 for (size_t i = 0; i < poly.pn; i++) {
187 edges[i].a = poly.ps[i];
188 edges[i].b = poly.ps[(i + 1) % poly.pn];
189 }
190 if (Proutespline(edges, poly.pn, pl, (Pvector_t[2]){0}, &spl) < 0) {
191 free(edges);
192 return NULL;
193 }
194 free(edges);
195 }
196
197 pointf *ps = calloc(spl.pn, sizeof(ps[0]));
198 if (ps == NULL) {
199 agerrorf("cannot allocate ps\n");
200 return NULL;
201 }
202 for (size_t i = 0; i < spl.pn; i++) {
203 ps[i] = spl.ps[i];
204 }
205 *n_spl_pts = spl.pn;
206 return ps;
207}
208
213 if (++routeinit > 1)
214 return 0;
215#ifdef DEBUG
217#endif
218 nedges = 0;
219 nboxes = 0;
220 if (Verbose)
221 start_timer();
222 return 0;
223}
224
226 if (--routeinit > 0)
227 return;
228 GV_DEBUG("routesplines: %d edges, %" PRISIZE_T " boxes %.2f sec", nedges,
230}
231
232static void limitBoxes(boxf *boxes, size_t boxn, const pointf *pps, size_t pn,
233 double delta) {
234 double t;
235 pointf sp[4];
236 const double num_div = delta * (double)boxn;
237
238 for (size_t splinepi = 0; splinepi + 3 < pn; splinepi += 3) {
239 for (double si = 0; si <= num_div; si++) {
240 t = si / num_div;
241 sp[0] = pps[splinepi];
242 sp[1] = pps[splinepi + 1];
243 sp[2] = pps[splinepi + 2];
244 sp[3] = pps[splinepi + 3];
245 sp[0].x += t * (sp[1].x - sp[0].x);
246 sp[0].y += t * (sp[1].y - sp[0].y);
247 sp[1].x += t * (sp[2].x - sp[1].x);
248 sp[1].y += t * (sp[2].y - sp[1].y);
249 sp[2].x += t * (sp[3].x - sp[2].x);
250 sp[2].y += t * (sp[3].y - sp[2].y);
251 sp[0].x += t * (sp[1].x - sp[0].x);
252 sp[0].y += t * (sp[1].y - sp[0].y);
253 sp[1].x += t * (sp[2].x - sp[1].x);
254 sp[1].y += t * (sp[2].y - sp[1].y);
255 sp[0].x += t * (sp[1].x - sp[0].x);
256 sp[0].y += t * (sp[1].y - sp[0].y);
257 for (size_t bi = 0; bi < boxn; bi++) {
258/* this tested ok on 64bit machines, but on 32bit we need this FUDGE
259 * or graphs/directed/records.gv fails */
260#define FUDGE .0001
261 if (sp[0].y <= boxes[bi].UR.y + FUDGE &&
262 sp[0].y >= boxes[bi].LL.y - FUDGE) {
263 boxes[bi].LL.x = fmin(boxes[bi].LL.x, sp[0].x);
264 boxes[bi].UR.x = fmax(boxes[bi].UR.x, sp[0].x);
265 }
266 }
267 }
268 }
269}
270
271#define INIT_DELTA 10
272#define LOOP_TRIES \
273 15 /* number of times to try to limiting boxes to regain space, using \
274 smaller divisions */
275
291static pointf *routesplines_(path *pp, size_t *npoints, int polyline) {
293 Ppolyline_t pl, spl;
294 Ppoint_t eps[2];
295 int prev, next;
296 boxf *boxes;
297 edge_t *realedge;
298 bool flip;
299 int loopcnt;
300 bool unbounded;
301
302 *npoints = 0;
303 nedges++;
304 nboxes += pp->nbox;
305
306 for (realedge = pp->data; realedge && ED_edge_type(realedge) != NORMAL;
307 realedge = ED_to_orig(realedge))
308 ;
309 if (!realedge) {
310 agerrorf("in routesplines, cannot find NORMAL edge\n");
311 return NULL;
312 }
313
314 boxes = pp->boxes;
315 const size_t boxn = pp->nbox;
316
317 if (checkpath(boxn, boxes, pp))
318 return NULL;
319
320#ifdef DEBUG
321 if (debugleveln(realedge, 1))
322 printboxes(boxn, boxes);
323 if (debugleveln(realedge, 3)) {
324 psprintinit(1);
325 psprintboxes(boxn, boxes);
326 }
327#endif
328
329 // vertices of polygon defined by boxes
330 Ppoint_t *polypoints = gv_calloc(boxn * 8, sizeof(Ppoint_t));
331
332 if (boxn > 1 && boxes[0].LL.y > boxes[1].LL.y) {
333 flip = true;
334 for (size_t bi = 0; bi < boxn; bi++) {
335 double v = boxes[bi].UR.y;
336 boxes[bi].UR.y = -1 * boxes[bi].LL.y;
337 boxes[bi].LL.y = -v;
338 }
339 } else
340 flip = false;
341
342 size_t pi;
343 if (agtail(realedge) != aghead(realedge)) {
344 /* I assume that the path goes either down only or
345 up - right - down */
346 size_t bi;
347 for (bi = 0, pi = 0; bi < boxn; bi++) {
348 next = prev = 0;
349 if (bi > 0)
350 prev = boxes[bi].LL.y > boxes[bi - 1].LL.y ? -1 : 1;
351 if (bi + 1 < boxn)
352 next = boxes[bi + 1].LL.y > boxes[bi].LL.y ? 1 : -1;
353 if (prev != next) {
354 if (next == -1 || prev == 1) {
355 polypoints[pi].x = boxes[bi].LL.x;
356 polypoints[pi++].y = boxes[bi].UR.y;
357 polypoints[pi].x = boxes[bi].LL.x;
358 polypoints[pi++].y = boxes[bi].LL.y;
359 } else {
360 polypoints[pi].x = boxes[bi].UR.x;
361 polypoints[pi++].y = boxes[bi].LL.y;
362 polypoints[pi].x = boxes[bi].UR.x;
363 polypoints[pi++].y = boxes[bi].UR.y;
364 }
365 } else if (prev == 0) { /* single box */
366 polypoints[pi].x = boxes[bi].LL.x;
367 polypoints[pi++].y = boxes[bi].UR.y;
368 polypoints[pi].x = boxes[bi].LL.x;
369 polypoints[pi++].y = boxes[bi].LL.y;
370 } else {
371 if (!(prev == -1 && next == -1)) {
372 free(polypoints);
373 agerrorf("in routesplines, illegal values of prev %d and next %d, "
374 "line %d\n",
375 prev, next, __LINE__);
376 return NULL;
377 }
378 }
379 }
380 for (bi = boxn - 1; bi != SIZE_MAX; bi--) {
381 next = prev = 0;
382 if (bi + 1 < boxn)
383 prev = boxes[bi].LL.y > boxes[bi + 1].LL.y ? -1 : 1;
384 if (bi > 0)
385 next = boxes[bi - 1].LL.y > boxes[bi].LL.y ? 1 : -1;
386 if (prev != next) {
387 if (next == -1 || prev == 1) {
388 polypoints[pi].x = boxes[bi].LL.x;
389 polypoints[pi++].y = boxes[bi].UR.y;
390 polypoints[pi].x = boxes[bi].LL.x;
391 polypoints[pi++].y = boxes[bi].LL.y;
392 } else {
393 polypoints[pi].x = boxes[bi].UR.x;
394 polypoints[pi++].y = boxes[bi].LL.y;
395 polypoints[pi].x = boxes[bi].UR.x;
396 polypoints[pi++].y = boxes[bi].UR.y;
397 }
398 } else if (prev == 0) { /* single box */
399 polypoints[pi].x = boxes[bi].UR.x;
400 polypoints[pi++].y = boxes[bi].LL.y;
401 polypoints[pi].x = boxes[bi].UR.x;
402 polypoints[pi++].y = boxes[bi].UR.y;
403 } else {
404 if (!(prev == -1 && next == -1)) {
405 /* it went badly, e.g. degenerate box in boxlist */
406 free(polypoints);
407 agerrorf("in routesplines, illegal values of prev %d and next %d, "
408 "line %d\n",
409 prev, next, __LINE__);
410 return NULL; /* for correctness sake, it's best to just stop */
411 }
412 polypoints[pi].x = boxes[bi].UR.x;
413 polypoints[pi++].y = boxes[bi].LL.y;
414 polypoints[pi].x = boxes[bi].UR.x;
415 polypoints[pi++].y = boxes[bi].UR.y;
416 polypoints[pi].x = boxes[bi].LL.x;
417 polypoints[pi++].y = boxes[bi].UR.y;
418 polypoints[pi].x = boxes[bi].LL.x;
419 polypoints[pi++].y = boxes[bi].LL.y;
420 }
421 }
422 } else {
423 free(polypoints);
424 agerrorf("in routesplines, edge is a loop at %s\n",
425 agnameof(aghead(realedge)));
426 return NULL;
427 }
428
429 if (flip) {
430 for (size_t bi = 0; bi < boxn; bi++) {
431 double v = boxes[bi].UR.y;
432 boxes[bi].UR.y = -1 * boxes[bi].LL.y;
433 boxes[bi].LL.y = -v;
434 }
435 for (size_t i = 0; i < pi; i++)
436 polypoints[i].y *= -1;
437 }
438
439 static const double INITIAL_LLX = DBL_MAX;
440 static const double INITIAL_URX = -DBL_MAX;
441 for (size_t bi = 0; bi < boxn; bi++) {
442 boxes[bi].LL.x = INITIAL_LLX;
443 boxes[bi].UR.x = INITIAL_URX;
444 }
445 poly.ps = polypoints, poly.pn = pi;
446 eps[0].x = pp->start.p.x, eps[0].y = pp->start.p.y;
447 eps[1].x = pp->end.p.x, eps[1].y = pp->end.p.y;
448 if (Pshortestpath(&poly, eps, &pl) < 0) {
449 free(polypoints);
450 agerrorf("in routesplines, Pshortestpath failed\n");
451 return NULL;
452 }
453#ifdef DEBUG
454 if (debugleveln(realedge, 3)) {
455 psprintpoly(poly);
456 psprintline(pl);
457 }
458#endif
459
460 if (polyline) {
461 make_polyline(pl, &spl);
462 } else {
463 Pedge_t *edges = gv_calloc(poly.pn, sizeof(Pedge_t));
464 for (size_t edgei = 0; edgei < poly.pn; edgei++) {
465 edges[edgei].a = polypoints[edgei];
466 edges[edgei].b = polypoints[(edgei + 1) % poly.pn];
467 }
468 Pvector_t evs[2] = {0};
469 if (pp->start.constrained) {
470 evs[0].x = cos(pp->start.theta);
471 evs[0].y = sin(pp->start.theta);
472 }
473 if (pp->end.constrained) {
474 evs[1].x = -cos(pp->end.theta);
475 evs[1].y = -sin(pp->end.theta);
476 }
477
478 if (Proutespline(edges, poly.pn, pl, evs, &spl) < 0) {
479 free(edges);
480 free(polypoints);
481 agerrorf("in routesplines, Proutespline failed\n");
482 return NULL;
483 }
484 free(edges);
485#ifdef DEBUG
486 if (debugleveln(realedge, 3)) {
487 psprintspline(spl);
488 psprintinit(0);
489 }
490#endif
491 }
492 pointf *ps = calloc(spl.pn, sizeof(ps[0]));
493 if (ps == NULL) {
494 free(polypoints);
495 agerrorf("cannot allocate ps\n");
496 return NULL; /* Bailout if no memory left */
497 }
498
499 unbounded = true;
500 for (size_t splinepi = 0; splinepi < spl.pn; splinepi++) {
501 ps[splinepi] = spl.ps[splinepi];
502 }
503
504 {
505 // does the spline go straight left/right?
506 bool is_horizontal = spl.pn > 0;
507 for (size_t i = 0; i < spl.pn; ++i) {
508 if (fabs(ps[0].y - ps[i].y) > FUDGE) {
509 is_horizontal = false;
510 break;
511 }
512 }
513 if (is_horizontal) {
514 GV_INFO("spline [%.03f, %.03f] -- [%.03f, %.03f] is horizontal;"
515 " will be trivially bounded",
516 ps[0].x, ps[0].y, ps[spl.pn - 1].x, ps[spl.pn - 1].y);
517 }
518
519 // does the spline go straight up/down?
520 bool is_vertical = spl.pn > 0;
521 for (size_t i = 0; i < spl.pn; ++i) {
522 if (fabs(ps[0].x - ps[i].x) > FUDGE) {
523 is_vertical = false;
524 break;
525 }
526 }
527 if (is_vertical) {
528 GV_INFO("spline [%.03f, %.03f] -- [%.03f, %.03f] is vertical;"
529 " will be trivially bounded",
530 ps[0].x, ps[0].y, ps[spl.pn - 1].x, ps[spl.pn - 1].y);
531 }
532
533 // if the spline is horizontal or vertical, the `limitBoxes` loop will not
534 // converge, but we know the expected outcome trivially
535 if (is_horizontal || is_vertical) {
536 for (size_t i = 0; i < boxn; ++i) {
537 boxes[i].LL.x = ps[0].x;
538 boxes[i].UR.x = ps[0].x;
539 }
540 unbounded = false;
541 }
542 }
543
544 double delta = INIT_DELTA;
545
546 for (loopcnt = 0; unbounded && loopcnt < LOOP_TRIES; loopcnt++) {
547 limitBoxes(boxes, boxn, ps, spl.pn, delta);
548
549 /* The following check is necessary because if a box is not very
550 * high, it is possible that the sampling above might miss it.
551 * Therefore, we make the sample finer until all boxes have
552 * valid values. cf. bug 456.
553 */
554 size_t bi;
555 for (bi = 0; bi < boxn; bi++) {
556 /* these fp equality tests are used only to detect if the
557 * values have been changed since initialization - ok */
558 if (is_exactly_equal(boxes[bi].LL.x, INITIAL_LLX) ||
559 is_exactly_equal(boxes[bi].UR.x, INITIAL_URX)) {
560 delta *= 2; /* try again with a finer interval */
561 break;
562 }
563 }
564 if (bi == boxn)
565 unbounded = false;
566 }
567 if (unbounded) {
568 /* Either an extremely short, even degenerate, box, or some failure with the
569 * path planner causing the spline to miss some boxes. In any case, use the
570 * shortest path to bound the boxes. This will probably mean a bad edge, but
571 * we avoid an infinite loop and we can see the bad edge, and even use the
572 * showboxes scaffolding.
573 */
574 Ppolyline_t polyspl;
575 agwarningf("Unable to reclaim box space in spline routing for edge \"%s\" "
576 "-> \"%s\". Something is probably seriously wrong.\n",
577 agnameof(agtail(realedge)), agnameof(aghead(realedge)));
578 make_polyline(pl, &polyspl);
579 limitBoxes(boxes, boxn, polyspl.ps, polyspl.pn, INIT_DELTA);
580 }
581
582 *npoints = spl.pn;
583
584#ifdef DEBUG
585 if (GD_showboxes(agraphof(aghead(realedge))) == 2 ||
586 GD_showboxes(agraphof(agtail(realedge))) == 2 ||
587 ED_showboxes(realedge) == 2 || ND_showboxes(aghead(realedge)) == 2 ||
588 ND_showboxes(agtail(realedge)) == 2)
589 printboxes(boxn, boxes);
590#endif
591
592 free(polypoints);
593 return ps;
594}
596pointf *routesplines(path *pp, size_t *npoints) {
597 return routesplines_(pp, npoints, 0);
598}
600pointf *routepolylines(path *pp, size_t *npoints) {
601 return routesplines_(pp, npoints, 1);
602}
604static double overlap(double i0, double i1, double j0, double j1) {
605 if (i1 <= j0)
606 return 0;
607 if (i0 >= j1)
608 return 0;
609
610 // does the first interval subsume the second?
611 if (i0 <= j0 && i1 >= j1)
612 return i1 - i0;
613 // does the second interval subsume the first?
614 if (j0 <= i0 && j1 >= i1)
615 return j1 - j0;
616
617 if (j0 <= i0 && i0 <= j1)
618 return j1 - i0;
619 assert(j0 <= i1 && i1 <= j1);
620 return i1 - j0;
621}
622
623/*
624 * repairs minor errors in the boxpath, such as boxes not joining
625 * or slightly intersecting. it's sort of the equivalent of the
626 * audit process in the 5E control program - if you've given up on
627 * fixing all the bugs, at least try to engineer around them!
628 * in postmodern CS, we could call this "self-healing code."
629 *
630 * Return 1 on failure; 0 on success.
631 */
632static int checkpath(size_t boxn, boxf *boxes, path *thepath) {
633 boxf *ba, *bb;
634 int errs, l, r, d, u;
635
636 /* remove degenerate boxes. */
637 size_t i = 0;
638 for (size_t bi = 0; bi < boxn; bi++) {
639 if (fabs(boxes[bi].LL.y - boxes[bi].UR.y) < .01)
640 continue;
641 if (fabs(boxes[bi].LL.x - boxes[bi].UR.x) < .01)
642 continue;
643 boxes[i] = boxes[bi];
644 i++;
645 }
646 boxn = i;
647
648 if (boxn == 0) {
649 agerrorf("in checkpath, all bounding boxes are below threshold\n");
650 printpath(thepath);
651 return 1;
652 }
653
654 ba = &boxes[0];
655 if (ba->LL.x > ba->UR.x || ba->LL.y > ba->UR.y) {
656 agerrorf("in checkpath, box 0 has LL coord > UR coord\n");
657 printpath(thepath);
658 return 1;
659 }
660 for (size_t bi = 0; bi + 1 < boxn; bi++) {
661 ba = &boxes[bi], bb = &boxes[bi + 1];
662 if (bb->LL.x > bb->UR.x || bb->LL.y > bb->UR.y) {
663 agerrorf("in checkpath, box %" PRISIZE_T " has LL coord > UR coord\n",
664 bi + 1);
665 printpath(thepath);
666 return 1;
667 }
668 l = ba->UR.x < bb->LL.x ? 1 : 0;
669 r = ba->LL.x > bb->UR.x ? 1 : 0;
670 d = ba->UR.y < bb->LL.y ? 1 : 0;
671 u = ba->LL.y > bb->UR.y ? 1 : 0;
672 errs = l + r + d + u;
673 if (errs > 0 && Verbose) {
674 fprintf(stderr,
675 "in checkpath, boxes %" PRISIZE_T " and %" PRISIZE_T
676 " don't touch\n",
677 bi, bi + 1);
678 printpath(thepath);
679 }
680 if (errs > 0) {
681 double xy;
682
683 if (l == 1)
684 xy = ba->UR.x, ba->UR.x = bb->LL.x, bb->LL.x = xy, l = 0;
685 else if (r == 1)
686 xy = ba->LL.x, ba->LL.x = bb->UR.x, bb->UR.x = xy, r = 0;
687 else if (d == 1)
688 xy = ba->UR.y, ba->UR.y = bb->LL.y, bb->LL.y = xy, d = 0;
689 else if (u == 1)
690 xy = ba->LL.y, ba->LL.y = bb->UR.y, bb->UR.y = xy, u = 0;
691 for (int j = 0; j < errs - 1; j++) {
692 if (l == 1)
693 xy = (ba->UR.x + bb->LL.x) / 2.0 + 0.5, ba->UR.x = bb->LL.x = xy,
694 l = 0;
695 else if (r == 1)
696 xy = (ba->LL.x + bb->UR.x) / 2.0 + 0.5, ba->LL.x = bb->UR.x = xy,
697 r = 0;
698 else if (d == 1)
699 xy = (ba->UR.y + bb->LL.y) / 2.0 + 0.5, ba->UR.y = bb->LL.y = xy,
700 d = 0;
701 else if (u == 1)
702 xy = (ba->LL.y + bb->UR.y) / 2.0 + 0.5, ba->LL.y = bb->UR.y = xy,
703 u = 0;
704 }
705 }
706 /* check for overlapping boxes */
707 double xoverlap = overlap(ba->LL.x, ba->UR.x, bb->LL.x, bb->UR.x);
708 double yoverlap = overlap(ba->LL.y, ba->UR.y, bb->LL.y, bb->UR.y);
709 if (xoverlap > 0 && yoverlap > 0) {
710 if (xoverlap < yoverlap) {
711 if (ba->UR.x - ba->LL.x > bb->UR.x - bb->LL.x) {
712 /* take space from ba */
713 if (ba->UR.x < bb->UR.x)
714 ba->UR.x = bb->LL.x;
715 else
716 ba->LL.x = bb->UR.x;
717 } else {
718 /* take space from bb */
719 if (ba->UR.x < bb->UR.x)
720 bb->LL.x = ba->UR.x;
721 else
722 bb->UR.x = ba->LL.x;
723 }
724 } else { /* symmetric for y coords */
725 if (ba->UR.y - ba->LL.y > bb->UR.y - bb->LL.y) {
726 /* take space from ba */
727 if (ba->UR.y < bb->UR.y)
728 ba->UR.y = bb->LL.y;
729 else
730 ba->LL.y = bb->UR.y;
731 } else {
732 /* take space from bb */
733 if (ba->UR.y < bb->UR.y)
734 bb->LL.y = ba->UR.y;
735 else
736 bb->UR.y = ba->LL.y;
737 }
738 }
739 }
740 }
741
742 if (thepath->start.p.x < boxes[0].LL.x ||
743 thepath->start.p.x > boxes[0].UR.x ||
744 thepath->start.p.y < boxes[0].LL.y ||
745 thepath->start.p.y > boxes[0].UR.y) {
746 thepath->start.p.x = fmax(thepath->start.p.x, boxes[0].LL.x);
747 thepath->start.p.x = fmin(thepath->start.p.x, boxes[0].UR.x);
748 thepath->start.p.y = fmax(thepath->start.p.y, boxes[0].LL.y);
749 thepath->start.p.y = fmin(thepath->start.p.y, boxes[0].UR.y);
750 }
751 if (thepath->end.p.x < boxes[boxn - 1].LL.x ||
752 thepath->end.p.x > boxes[boxn - 1].UR.x ||
753 thepath->end.p.y < boxes[boxn - 1].LL.y ||
754 thepath->end.p.y > boxes[boxn - 1].UR.y) {
755 thepath->end.p.x = fmax(thepath->end.p.x, boxes[boxn - 1].LL.x);
756 thepath->end.p.x = fmin(thepath->end.p.x, boxes[boxn - 1].UR.x);
757 thepath->end.p.y = fmax(thepath->end.p.y, boxes[boxn - 1].LL.y);
758 thepath->end.p.y = fmin(thepath->end.p.y, boxes[boxn - 1].UR.y);
759 }
760 return 0;
761}
763static void printpath(path *pp) {
764 fprintf(stderr, "%" PRISIZE_T " boxes:\n", pp->nbox);
765 for (size_t bi = 0; bi < pp->nbox; bi++)
766 fprintf(stderr, "%" PRISIZE_T " (%.5g, %.5g), (%.5g, %.5g)\n", bi,
767 pp->boxes[bi].LL.x, pp->boxes[bi].LL.y, pp->boxes[bi].UR.x,
768 pp->boxes[bi].UR.y);
769 fprintf(stderr, "start port: (%.5g, %.5g), tangent angle: %.5g, %s\n",
770 pp->start.p.x, pp->start.p.y, pp->start.theta,
771 pp->start.constrained ? "constrained" : "not constrained");
772 fprintf(stderr, "end port: (%.5g, %.5g), tangent angle: %.5g, %s\n",
773 pp->end.p.x, pp->end.p.y, pp->end.theta,
774 pp->end.constrained ? "constrained" : "not constrained");
775}
777static pointf get_centroid(Agraph_t *g) {
778 pointf sum = {0.0, 0.0};
779
780 sum.x = (GD_bb(g).LL.x + GD_bb(g).UR.x) / 2.0;
781 sum.y = (GD_bb(g).LL.y + GD_bb(g).UR.y) / 2.0;
782 return sum;
783}
785typedef LIST(node_t *) nodes_t;
786
787static void nodes_delete(nodes_t *pvec) {
788 if (pvec != NULL) {
789 LIST_FREE(pvec);
790 }
791 free(pvec);
792}
794typedef LIST(nodes_t *) cycles_t;
795
796static bool cycle_contains_edge(nodes_t *cycle, edge_t *edge) {
797 node_t *start = agtail(edge);
798 node_t *end = aghead(edge);
799
800 const size_t cycle_len = LIST_SIZE(cycle);
801
802 for (size_t i = 0; i < cycle_len; ++i) {
803 const node_t *c_start = LIST_GET(cycle, i == 0 ? cycle_len - 1 : i - 1);
804 const node_t *c_end = LIST_GET(cycle, i);
805
806 if (c_start == start && c_end == end)
807 return true;
808 }
809
810 return false;
811}
813static bool is_cycle_unique(cycles_t *cycles, nodes_t *cycle) {
814 const size_t cycle_len = LIST_SIZE(cycle);
815 size_t i; // node counter
816
817 bool all_items_match;
818
819 for (size_t c = 0; c < LIST_SIZE(cycles); ++c) {
820 nodes_t *cur_cycle = LIST_GET(cycles, c);
821 const size_t cur_cycle_len = LIST_SIZE(cur_cycle);
822
823 // if all the items match in equal length cycles then we're not unique
824 if (cur_cycle_len == cycle_len) {
825 all_items_match = true;
826 for (i = 0; i < cur_cycle_len; ++i) {
827 node_t *cur_cycle_item = LIST_GET(cur_cycle, i);
828 if (!LIST_CONTAINS(cycle, cur_cycle_item)) {
829 all_items_match = false;
830 break;
831 }
832 }
833 if (all_items_match)
834 return false;
835 }
836 }
837
838 return true;
839}
841static void dfs(graph_t *g, node_t *search, nodes_t *visited, node_t *end,
842 cycles_t *cycles) {
843 edge_t *e;
844 node_t *n;
845
846 if (LIST_CONTAINS(visited, search)) {
847 if (search == end) {
848 if (is_cycle_unique(cycles, visited)) {
849 nodes_t *cycle = gv_alloc(sizeof(nodes_t));
850 *cycle = LIST_COPY(nodes_t, visited);
851 LIST_APPEND(cycles, cycle);
852 }
853 }
854 } else {
855 LIST_APPEND(visited, search);
856 for (e = agfstout(g, search); e; e = agnxtout(g, e)) {
857 n = aghead(e);
858 dfs(g, n, visited, end, cycles);
859 }
860 if (!LIST_IS_EMPTY(visited)) {
861 LIST_DROP_BACK(visited);
862 }
863 }
864}
865
866// Returns a vec of vec of nodes (aka a vector of cycles)
867static cycles_t find_all_cycles(graph_t *g) {
868 node_t *n;
869
870 // vector of vectors of nodes -- AKA cycles to delete
871 cycles_t alloced_cycles = {.dtor = nodes_delete};
872 cycles_t cycles = {
873 .dtor =
874 nodes_delete}; // vector of vectors of nodes AKA a vector of cycles
875
876 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
877 nodes_t *cycle = gv_alloc(sizeof(nodes_t));
878 // keep track of all items we allocate to clean up at the end of this
879 // function
880 LIST_APPEND(&alloced_cycles, cycle);
881
882 dfs(g, n, cycle, n, &cycles);
883 }
884
885 LIST_FREE(&alloced_cycles); // cycles contains copied vecs
886 return cycles;
887}
889static nodes_t *find_shortest_cycle_with_edge(cycles_t *cycles, edge_t *edge,
890 size_t min_size) {
891 nodes_t *shortest = NULL;
892
893 for (size_t c = 0; c < LIST_SIZE(cycles); ++c) {
894 nodes_t *cycle = LIST_GET(cycles, c);
895 size_t cycle_len = LIST_SIZE(cycle);
896
897 if (cycle_len < min_size)
898 continue;
899
900 if (shortest == NULL || LIST_SIZE(shortest) > cycle_len) {
901 if (cycle_contains_edge(cycle, edge)) {
902 shortest = cycle;
903 }
904 }
905 }
906 return shortest;
907}
910 cycles_t cycles = find_all_cycles(g);
911
912 // find the center of the shortest cycle containing this edge
913 // cycles of length 2 do their own thing, we want 3 or
914 nodes_t *cycle = find_shortest_cycle_with_edge(&cycles, edge, 3);
915 pointf sum = {0.0, 0.0};
916
917 if (cycle == NULL) {
918 LIST_FREE(&cycles);
919 return get_centroid(g);
920 }
921
922 double cnt = 0;
923 for (size_t idx = 0; idx < LIST_SIZE(cycle); ++idx) {
924 node_t *n = LIST_GET(cycle, idx);
925 sum.x += ND_coord(n).x;
926 sum.y += ND_coord(n).y;
927 cnt++;
928 }
929
930 LIST_FREE(&cycles);
931
932 sum.x /= cnt;
933 sum.y /= cnt;
934 return sum;
935}
937static void bend(pointf spl[4], pointf centroid) {
938 const pointf midpt = mid_pointf(spl[0], spl[3]);
939 const double dist = DIST(spl[3], spl[0]);
940 const double r = dist / 5.0;
941 const double vX = centroid.x - midpt.x;
942 const double vY = centroid.y - midpt.y;
943 const double magV = hypot(vX, vY);
944 // avoid out of range division results
945 if (fdiv_overflow(vX, magV, &(double){0}) ||
946 fdiv_overflow(vY, magV, &(double){0}))
947 return;
948 const pointf a = {.x = midpt.x - vX / magV * r,
949 .y = midpt.y - vY / magV * r}; // + would be closest point
950 /* this can be improved */
951 spl[1] = spl[2] = a;
952}
953
954// FIX: handle ports on boundary?
955void makeStraightEdge(graph_t *g, edge_t *e, int et, splineInfo *sinfo) {
956 edge_t *e0;
957
958 size_t e_cnt = 1;
959 e0 = e;
960 while (e0 != ED_to_virt(e0) && (e0 = ED_to_virt(e0)))
961 e_cnt++;
962
963 edge_t **edge_list = gv_calloc(e_cnt, sizeof(edge_t *));
964 e0 = e;
965 for (size_t i = 0; i < e_cnt; i++) {
966 edge_list[i] = e0;
967 e0 = ED_to_virt(e0);
968 }
969 assert(e_cnt <= INT_MAX);
970 makeStraightEdges(g, edge_list, e_cnt, et, sinfo);
971 free(edge_list);
972}
974void makeStraightEdges(graph_t *g, edge_t **edge_list, size_t e_cnt, int et,
975 splineInfo *sinfo) {
976 pointf dumb[4];
977 bool curved = et == EDGETYPE_CURVED;
978 pointf del;
979
980 edge_t *e = edge_list[0];
981 node_t *n = agtail(e);
982 node_t *head = aghead(e);
983 dumb[1] = dumb[0] = add_pointf(ND_coord(n), ED_tail_port(e).p);
984 dumb[2] = dumb[3] = add_pointf(ND_coord(head), ED_head_port(e).p);
985 if (e_cnt == 1 || Concentrate) {
986 if (curved)
987 bend(dumb, get_cycle_centroid(g, edge_list[0]));
988 clip_and_install(e, aghead(e), dumb, 4, sinfo);
989 addEdgeLabels(e);
990 return;
991 }
992
993 if (APPROXEQPT(dumb[0], dumb[3], MILLIPOINT)) {
994 /* degenerate case */
995 dumb[1] = dumb[0];
996 dumb[2] = dumb[3];
997 del.x = 0;
998 del.y = 0;
999 } else {
1000 pointf perp = {.x = dumb[0].y - dumb[3].y, .y = dumb[3].x - dumb[0].x};
1001 double l_perp = hypot(perp.x, perp.y);
1002 int xstep = GD_nodesep(g->root);
1003 assert(e_cnt - 1 <= INT_MAX);
1004 int dx = xstep * (int)(e_cnt - 1) / 2;
1005 dumb[1].x = dumb[0].x + dx * perp.x / l_perp;
1006 dumb[1].y = dumb[0].y + dx * perp.y / l_perp;
1007 dumb[2].x = dumb[3].x + dx * perp.x / l_perp;
1008 dumb[2].y = dumb[3].y + dx * perp.y / l_perp;
1009 del.x = -xstep * perp.x / l_perp;
1010 del.y = -xstep * perp.y / l_perp;
1011 }
1012
1013 for (size_t i = 0; i < e_cnt; i++) {
1014 edge_t *e0 = edge_list[i];
1015 pointf dumber[4];
1016 if (aghead(e0) == head) {
1017 for (size_t j = 0; j < 4; j++) {
1018 dumber[j] = dumb[j];
1019 }
1020 } else {
1021 for (size_t j = 0; j < 4; j++) {
1022 dumber[3 - j] = dumb[j];
1023 }
1024 }
1025 if (et == EDGETYPE_PLINE) {
1026 Ppoint_t pts[] = {dumber[0], dumber[1], dumber[2], dumber[3]};
1027 Ppolyline_t spl, line = {.pn = 4, .ps = pts};
1028 make_polyline(line, &spl);
1029 clip_and_install(e0, aghead(e0), spl.ps, spl.pn, sinfo);
1030 } else
1031 clip_and_install(e0, aghead(e0), dumber, 4, sinfo);
1032
1033 addEdgeLabels(e0);
1034 dumb[1] = add_pointf(dumb[1], del);
1035 dumb[2] = add_pointf(dumb[2], del);
1036 }
1037}
Dynamically expanding string buffers.
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
Definition agxbuf.h:252
static char * agxbdisown(agxbuf *xb)
Definition agxbuf.h:345
Memory allocation wrappers that exit on failure.
static char * gv_strdup(const char *original)
Definition alloc.h:101
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
static void * gv_alloc(size_t size)
Definition alloc.h:47
#define NORMAL
Definition const.h:24
#define EDGETYPE_CURVED
Definition const.h:236
#define EDGETYPE_PLINE
Definition const.h:237
helpers for verbose/debug printing
#define GV_DEBUG(...)
Definition debug.h:40
#define GV_INFO(...)
Definition debug.h:15
static splineInfo sinfo
Definition dotsplines.c:125
static float dx
Definition draw.c:42
#define head
Definition dthdr.h:15
static void del(Dict_t *d, Dtlink_t **set, Agedge_t *e)
Definition edge.c:158
static double dist(int dim, double *x, double *y)
#define MILLIPOINT
Definition geom.h:74
#define APPROXEQPT(p, q, tol)
Definition geom.h:71
#define DIST(p, q)
Definition geom.h:56
geometric functions (e.g. on points and boxes)
static WUR pointf perp(pointf p)
Definition geomprocs.h:140
static WUR pointf mid_pointf(pointf p, pointf q)
Definition geomprocs.h:104
static WUR pointf add_pointf(pointf p, pointf q)
Definition geomprocs.h:88
show_boxes_t Show_boxes
Definition globals.c:23
bool Concentrate
Definition globals.h:62
static bool Verbose
Definition gml2gv.c:26
void free(void *)
edge
Definition gmlparse.y:246
#define SIZE_MAX
Definition gmlscan.c:347
node NULL
Definition grammar.y:181
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:204
#define ED_to_orig(e)
Definition types.h:598
#define ED_showboxes(e)
Definition types.h:594
Agedge_t * agfstout(Agraph_t *g, Agnode_t *n)
Definition edge.c:28
#define agtail(e)
Definition cgraph.h:982
#define ED_edge_type(e)
Definition types.h:582
#define aghead(e)
Definition cgraph.h:983
Agedge_t * agnxtout(Agraph_t *g, Agedge_t *e)
Definition edge.c:43
#define ED_head_port(e)
Definition types.h:588
#define ED_tail_port(e)
Definition types.h:597
#define ED_to_virt(e)
Definition types.h:599
void agwarningf(const char *fmt,...)
Definition agerror.c:175
void agerrorf(const char *fmt,...)
Definition agerror.c:167
#define GD_bb(g)
Definition types.h:354
#define GD_showboxes(g)
Definition types.h:401
#define GD_nodesep(g)
Definition types.h:394
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:50
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:43
#define ND_showboxes(n)
Definition types.h:530
#define ND_coord(n)
Definition types.h:490
Agraph_t * agraphof(void *obj)
Definition obj.c:187
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:145
Arithmetic helper functions.
static bool is_exactly_equal(double a, double b)
are two values precisely the same?
Definition gv_math.h:49
$2 prev
Definition htmlparse.y:291
type-generic dynamically expanding list
#define LIST_APPEND(list,...)
Definition list.h:151
#define LIST(type)
Definition list.h:66
#define LIST_COPY(list_type, src)
Definition list.h:356
#define LIST_SIZE(list)
Definition list.h:92
#define LIST_DROP_BACK(list)
Definition list.h:483
#define LIST_FREE(list)
Definition list.h:413
#define LIST_CONTAINS(list, needle)
Definition list.h:340
#define LIST_IS_EMPTY(list)
Definition list.h:102
#define LIST_GET(list, index)
Definition list.h:197
#define delta
Definition maze.c:138
arithmetic overflow helpers
static bool fdiv_overflow(double n, double d, double *res)
Definition overflow.h:142
finds and smooths shortest paths
void make_polyline(Ppolyline_t line, Ppolyline_t *sline)
Definition util.c:44
int Proutespline(Pedge_t *barriers, size_t n_barriers, Ppolyline_t input_route, Pvector_t endpoint_slopes[2], Ppolyline_t *output_route)
Definition route.c:70
int Pshortestpath(Ppoly_t *boundary, Ppoint_t endpoints[2], Ppolyline_t *output_route)
Definition shortest.c:84
#define PRISIZE_T
Definition prisize_t.h:25
void clip_and_install(edge_t *fe, node_t *hn, pointf *ps, size_t pn, splineInfo *info)
Definition splines.c:234
void addEdgeLabels(edge_t *e)
Definition splines.c:1305
static bool is_cycle_unique(cycles_t *cycles, nodes_t *cycle)
Definition routespl.c:812
#define INIT_DELTA
Definition routespl.c:271
static void printpath(path *pp)
Definition routespl.c:762
static int routeinit
Definition routespl.c:36
static pointf get_centroid(Agraph_t *g)
Definition routespl.c:776
static void dfs(graph_t *g, node_t *search, nodes_t *visited, node_t *end, cycles_t *cycles)
Definition routespl.c:840
int routesplinesinit(void)
Definition routespl.c:212
static int nedges
total no. of edges used in routing
Definition routespl.c:33
pointf * simpleSplineRoute(pointf tp, pointf hp, Ppoly_t poly, size_t *n_spl_pts, int polyline)
Given a simple (ccw) polygon, route an edge from tp to hp.
Definition routespl.c:169
static nodes_t * find_shortest_cycle_with_edge(cycles_t *cycles, edge_t *edge, size_t min_size)
Definition routespl.c:888
static cycles_t find_all_cycles(graph_t *g)
Definition routespl.c:866
#define LOOP_TRIES
Definition routespl.c:272
static size_t nboxes
total no. of boxes used in routing
Definition routespl.c:34
static void limitBoxes(boxf *boxes, size_t boxn, const pointf *pps, size_t pn, double delta)
Definition routespl.c:232
static int checkpath(size_t, boxf *, path *)
Definition routespl.c:631
static pointf get_cycle_centroid(graph_t *g, edge_t *edge)
Definition routespl.c:908
static double overlap(double i0, double i1, double j0, double j1)
Definition routespl.c:603
#define FUDGE
void routesplinesterm(void)
Definition routespl.c:225
void makeStraightEdges(graph_t *g, edge_t **edge_list, size_t e_cnt, int et, splineInfo *sinfo)
Definition routespl.c:973
void makeStraightEdge(graph_t *g, edge_t *e, int et, splineInfo *sinfo)
Definition routespl.c:954
pointf * routesplines(path *pp, size_t *npoints)
Definition routespl.c:595
pointf * routepolylines(path *pp, size_t *npoints)
Definition routespl.c:599
static pointf * routesplines_(path *pp, size_t *npoints, int polyline)
Definition routespl.c:290
graph or subgraph
Definition cgraph.h:424
Agraph_t * root
subgraphs - ancestors
Definition cgraph.h:433
Ppoint_t b
Definition pathgeom.h:53
Ppoint_t a
Definition pathgeom.h:53
size_t pn
Definition pathgeom.h:47
Ppoint_t * ps
Definition pathgeom.h:46
double x
Definition pathgeom.h:38
double y
Definition pathgeom.h:38
Definition geom.h:41
pointf UR
Definition geom.h:41
pointf LL
Definition geom.h:41
Definition types.h:81
port start
Definition types.h:82
boxf * boxes
Definition types.h:85
port end
Definition types.h:83
size_t nbox
number of subdivisions
Definition types.h:84
void * data
Definition types.h:86
Definition geom.h:27
int y
Definition geom.h:27
int x
Definition geom.h:27
double x
Definition geom.h:29
double y
Definition geom.h:29
pointf p
Definition types.h:49
double theta
Definition types.h:50
bool constrained
Definition types.h:55
bend
Definition structures.h:33
struct poly_s poly
double elapsed_sec(void)
Definition timing.c:23
void start_timer(void)
Definition timing.c:21