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