Graphviz 14.1.2~dev.20260118.1035
Loading...
Searching...
No Matches
neatosplines.c
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 "config.h"
12
13#include <assert.h>
14#include "config.h"
15#include <limits.h>
16#include <math.h>
17#include <neatogen/neato.h>
18#include <neatogen/adjust.h>
19#include <pathplan/pathplan.h>
20#include <pathplan/vispath.h>
22#include <stdbool.h>
23#include <stddef.h>
24#include <util/alloc.h>
25#include <util/gv_math.h>
26#include <util/unreachable.h>
27
28#ifdef ORTHO
29#include <ortho/ortho.h>
30#endif
31
32
33static bool spline_merge(node_t * n)
34{
35 (void)n;
36 return false;
37}
38
39static bool swap_ends_p(edge_t * e)
40{
41 (void)e;
42 return false;
43}
44
46 .splineMerge = spline_merge};
47
48static void make_barriers(Ppoly_t **poly, int npoly, int pp, int qp,
49 Pedge_t **barriers, size_t *n_barriers) {
50 int i, j, k;
51
52 size_t n = 0;
53 for (i = 0; i < npoly; i++) {
54 if (i == pp)
55 continue;
56 if (i == qp)
57 continue;
58 n += poly[i]->pn;
59 }
60 Pedge_t *bar = gv_calloc(n, sizeof(Pedge_t));
61 size_t b = 0;
62 for (i = 0; i < npoly; i++) {
63 if (i == pp)
64 continue;
65 if (i == qp)
66 continue;
67 for (j = 0; j < (int)poly[i]->pn; j++) {
68 k = j + 1;
69 if (k >= (int)poly[i]->pn)
70 k = 0;
71 bar[b].a = poly[i]->ps[j];
72 bar[b].b = poly[i]->ps[k];
73 b++;
74 }
75 }
76 assert(b == n);
77 *barriers = bar;
78 *n_barriers = n;
79}
80
81static Ppoint_t genPt(double x, double y, pointf c)
82{
83 Ppoint_t p;
84
85 p.x = x + c.x;
86 p.y = y + c.y;
87 return p;
88}
89
90static Ppoint_t recPt(double x, double y, pointf c, expand_t* m)
91{
92 Ppoint_t p;
93
94 p.x = x * m->x + c.x;
95 p.y = y * m->y + c.y;
96 return p;
97}
98
110
111static void *newitem(void *p, Dtdisc_t *disc) {
112 edgeitem *obj = p;
113 edgeitem *newp;
114
115 (void)disc;
116 newp = gv_alloc(sizeof(edgeitem));
117 newp->id = obj->id;
118 newp->e = obj->e;
119 ED_count(newp->e) = 1;
120
121 return newp;
122}
123
124static int cmpitems(void *k1, void *k2) {
125 const edgeinfo *key1 = k1;
126 const edgeinfo *key2 = k2;
127 if (key1->n1 > key2->n1)
128 return 1;
129 if (key1->n1 < key2->n1)
130 return -1;
131 if (key1->n2 > key2->n2)
132 return 1;
133 if (key1->n2 < key2->n2)
134 return -1;
135
136 if (key1->p1.x > key2->p1.x)
137 return 1;
138 if (key1->p1.x < key2->p1.x)
139 return -1;
140 if (key1->p1.y > key2->p1.y)
141 return 1;
142 if (key1->p1.y < key2->p1.y)
143 return -1;
144 if (key1->p2.x > key2->p2.x)
145 return 1;
146 if (key1->p2.x < key2->p2.x)
147 return -1;
148 if (key1->p2.y > key2->p2.y)
149 return 1;
150 if (key1->p2.y < key2->p2.y)
151 return -1;
152 return 0;
153}
154
156 offsetof(edgeitem, id),
157 sizeof(edgeinfo),
158 offsetof(edgeitem, link),
159 newitem,
160 free,
161 cmpitems,
162};
163
164/* See if we have already encountered an edge between the same
165 * node:port pairs. If so, return the earlier edge. If not,
166 * this edge is added to map and returned.
167 * We first have to canonicalize the key fields using a lexicographic
168 * ordering.
169 */
170static edge_t *equivEdge(Dt_t * map, edge_t * e)
171{
172 edgeinfo test;
173 edgeitem dummy;
174 edgeitem *ip;
175
176 if (agtail(e) < aghead(e)) {
177 test.n1 = agtail(e);
178 test.p1 = ED_tail_port(e).p;
179 test.n2 = aghead(e);
180 test.p2 = ED_head_port(e).p;
181 } else if (agtail(e) > aghead(e)) {
182 test.n2 = agtail(e);
183 test.p2 = ED_tail_port(e).p;
184 test.n1 = aghead(e);
185 test.p1 = ED_head_port(e).p;
186 } else {
187 pointf hp = ED_head_port(e).p;
188 pointf tp = ED_tail_port(e).p;
189 if (tp.x < hp.x) {
190 test.p1 = tp;
191 test.p2 = hp;
192 } else if (tp.x > hp.x) {
193 test.p1 = hp;
194 test.p2 = tp;
195 } else if (tp.y < hp.y) {
196 test.p1 = tp;
197 test.p2 = hp;
198 } else if (tp.y > hp.y) {
199 test.p1 = hp;
200 test.p2 = tp;
201 } else {
202 test.p1 = test.p2 = tp;
203 }
204 test.n2 = test.n1 = agtail(e);
205 }
206 dummy.id = test;
207 dummy.e = e;
208 ip = dtinsert(map, &dummy);
209 return ip->e;
210}
211
212
213/* Generate loops. We use the library routine makeSelfEdge
214 * which also places the labels.
215 * We have to handle port labels here.
216 * as well as update the bbox from edge labels.
217 */
218void makeSelfArcs(edge_t * e, int stepx)
219{
220 assert(ED_count(e) >= 0);
221 const size_t cnt = (size_t)ED_count(e);
222
223 if (cnt == 1 || Concentrate) {
224 edge_t *edges1[1];
225 edges1[0] = e;
226 makeSelfEdge(edges1, 1, stepx, stepx, &sinfo);
227 if (ED_label(e))
230 } else if (cnt > 1) {
231 edge_t **edges = gv_calloc(cnt, sizeof(edge_t*));
232 for (size_t i = 0; i < cnt; i++) {
233 edges[i] = e;
234 e = ED_to_virt(e);
235 }
236 makeSelfEdge(edges, cnt, stepx, stepx, &sinfo);
237 for (size_t i = 0; i < cnt; i++) {
238 e = edges[i];
239 if (ED_label(e))
242 }
243 free(edges);
244 }
245}
246
271static double ellipse_tangent_slope(double a, double b, pointf p) {
272 assert(p.x != a &&
273 "cannot handle ellipse tangent slope in horizontal extreme point");
274 const double sign_y = p.y >= 0 ? 1 : -1;
275 const double m = -sign_y * (b * p.x) / (a * sqrt(a * a - p.x * p.x));
276 assert(isfinite(m) && "ellipse tangent slope is infinite");
277 return m;
278}
279
287 const double x =
288 (l0.m * l0.p.x - l0.p.y - l1.m * l1.p.x + l1.p.y) / (l0.m - l1.m);
289 const double y = l0.p.y + l0.m * (x - l0.p.x);
290 return (pointf){x, y};
291}
292
302 size_t i,
303 size_t nsides) {
304 const double angle0 = 2.0 * M_PI * ((double)i - 0.5) / (double)nsides;
305 const double angle1 = 2.0 * M_PI * ((double)i + 0.5) / (double)nsides;
306 const pointf p0 = {a * cos(angle0), b * sin(angle0)};
307 const pointf p1 = {a * cos(angle1), b * sin(angle1)};
308 const double m0 = ellipse_tangent_slope(a, b, p0);
309 const double m1 = ellipse_tangent_slope(a, b, p1);
310 const linef line0 = {{p0.x, p0.y}, m0};
311 const linef line1 = {{p1.x, p1.y}, m1};
312 return line_intersection(line0, line1);
313}
314
315/* Given a node, return an obstacle reflecting the
316 * node's geometry. pmargin specifies how much space to allow
317 * around the polygon.
318 * Returns the constructed polygon on success, NULL on failure.
319 * Failure means the node shape is not supported.
320 *
321 * If isOrtho is true, we have to use the bounding box of each node.
322 *
323 * The polygon has its vertices in CW order.
324 *
325 */
326Ppoly_t *makeObstacle(node_t * n, expand_t* pmargin, bool isOrtho)
327{
328 Ppoly_t *obs;
330 size_t sides;
331 pointf polyp;
332 boxf b;
333 pointf pt;
334 field_t *fld;
335 bool isPoly;
336 pointf* verts = NULL;
337 pointf vs[4];
338 pointf p;
339 pointf margin = {0};
340
341 switch (shapeOf(n)) {
342 case SH_POLY:
343 case SH_POINT:
344 obs = gv_alloc(sizeof(Ppoly_t));
345 poly = ND_shape_info(n);
346 if (isOrtho) {
347 isPoly = true;
348 sides = 4;
349 verts = vs;
350 /* For fixedshape, we can't use the width and height, as this includes
351 * the label. We only want to use the actual node shape.
352 */
353 if (poly->option.fixedshape) {
354 b = polyBB (poly);
355 vs[0] = b.LL;
356 vs[1].x = b.UR.x;
357 vs[1].y = b.LL.y;
358 vs[2] = b.UR;
359 vs[3].x = b.LL.x;
360 vs[3].y = b.UR.y;
361 } else {
362 const double width = ND_lw(n) + ND_rw(n);
363 const double outline_width = INCH2PS(ND_outline_width(n));
364 // scale lw and rw proportionally to sum to outline width
365 const double outline_lw = ND_lw(n) * outline_width / width;
366 const double outline_ht = INCH2PS(ND_outline_height(n));
367 p.x = -outline_lw;
368 p.y = -outline_ht / 2.0;
369 vs[0] = p;
370 p.x = outline_lw;
371 vs[1] = p;
372 p.y = outline_ht / 2.0;
373 vs[2] = p;
374 p.x = -outline_lw;
375 vs[3] = p;
376 }
377 }
378 else if (poly->sides >= 3) {
379 isPoly = true;
380 sides = poly->sides;
381 const double penwidth = late_double(n, N_penwidth, 1.0, 0.0);
382 // possibly use extra vertices representing the outline, i.e., the
383 // outermost periphery with penwidth taken into account
384 const size_t extra_peripheries = poly->peripheries >= 1 && penwidth > 0.0 ? 1 : 0;
385 const size_t outline_periphery = poly->peripheries + extra_peripheries;
386 const size_t vertices_offset = outline_periphery >= 1 ? (outline_periphery - 1) * sides : 0;
387 verts = poly->vertices + vertices_offset;
388 margin.x = pmargin->x;
389 margin.y = pmargin->y;
390 } else { /* ellipse */
391 isPoly = false;
392 sides = 8;
393 }
394 obs->pn = sides;
395 obs->ps = gv_calloc(sides, sizeof(Ppoint_t));
396 /* assuming polys are in CCW order, and pathplan needs CW */
397 for (size_t j = 0; j < sides; j++) {
398 double xmargin = 0, ymargin = 0;
399 if (isPoly) {
400 if (pmargin->doAdd) {
401 if (sides == 4) {
402 switch (j) {
403 case 0 :
404 xmargin = margin.x;
405 ymargin = margin.y;
406 break;
407 case 1 :
408 xmargin = -margin.x;
409 ymargin = margin.y;
410 break;
411 case 2 :
412 xmargin = -margin.x;
413 ymargin = -margin.y;
414 break;
415 case 3 :
416 xmargin = margin.x;
417 ymargin = -margin.y;
418 break;
419 default:
420 UNREACHABLE();
421 }
422 polyp.x = verts[j].x + xmargin;
423 polyp.y = verts[j].y + ymargin;
424 }
425 else {
426 const double h = hypot(verts[j].x, verts[j].y);
427 polyp.x = verts[j].x * (1.0 + margin.x/h);
428 polyp.y = verts[j].y * (1.0 + margin.y/h);
429 }
430 }
431 else {
432 polyp.x = verts[j].x * margin.x;
433 polyp.y = verts[j].y * margin.y;
434 }
435 } else {
436 const double width = INCH2PS(ND_outline_width(n));
437 const double height = INCH2PS(ND_outline_height(n));
438 margin = pmargin->doAdd ? (pointf){pmargin->x, pmargin->y} : (pointf){0};
439 const double ellipse_a = (width + margin.x) / 2.0;
440 const double ellipse_b = (height + margin.y) / 2.0;
441 polyp = circumscribed_polygon_corner_about_ellipse(ellipse_a, ellipse_b, j, sides);
442 }
443 obs->ps[sides - j - 1].x = polyp.x + ND_coord(n).x;
444 obs->ps[sides - j - 1].y = polyp.y + ND_coord(n).y;
445 }
446 break;
447 case SH_RECORD:
448 fld = ND_shape_info(n);
449 b = fld->b;
450 obs = gv_alloc(sizeof(Ppoly_t));
451 obs->pn = 4;
452 obs->ps = gv_calloc(4, sizeof(Ppoint_t));
453 /* CW order */
454 pt = ND_coord(n);
455 if (pmargin->doAdd) {
456 obs->ps[0] = genPt(b.LL.x-pmargin->x, b.LL.y-pmargin->y, pt);
457 obs->ps[1] = genPt(b.LL.x-pmargin->x, b.UR.y+pmargin->y, pt);
458 obs->ps[2] = genPt(b.UR.x+pmargin->x, b.UR.y+pmargin->y, pt);
459 obs->ps[3] = genPt(b.UR.x+pmargin->x, b.LL.y-pmargin->y, pt);
460 }
461 else {
462 obs->ps[0] = recPt(b.LL.x, b.LL.y, pt, pmargin);
463 obs->ps[1] = recPt(b.LL.x, b.UR.y, pt, pmargin);
464 obs->ps[2] = recPt(b.UR.x, b.UR.y, pt, pmargin);
465 obs->ps[3] = recPt(b.UR.x, b.LL.y, pt, pmargin);
466 }
467 break;
468 case SH_EPSF:
469 obs = gv_alloc(sizeof(Ppoly_t));
470 obs->pn = 4;
471 obs->ps = gv_calloc(4, sizeof(Ppoint_t));
472 /* CW order */
473 pt = ND_coord(n);
474 if (pmargin->doAdd) {
475 obs->ps[0] = genPt(-ND_lw(n)-pmargin->x, -ND_ht(n)-pmargin->y,pt);
476 obs->ps[1] = genPt(-ND_lw(n)-pmargin->x, ND_ht(n)+pmargin->y,pt);
477 obs->ps[2] = genPt(ND_rw(n)+pmargin->x, ND_ht(n)+pmargin->y,pt);
478 obs->ps[3] = genPt(ND_rw(n)+pmargin->x, -ND_ht(n)-pmargin->y,pt);
479 }
480 else {
481 obs->ps[0] = recPt(-ND_lw(n), -ND_ht(n), pt, pmargin);
482 obs->ps[1] = recPt(-ND_lw(n), ND_ht(n), pt, pmargin);
483 obs->ps[2] = recPt(ND_rw(n), ND_ht(n), pt, pmargin);
484 obs->ps[3] = recPt(ND_rw(n), -ND_ht(n), pt, pmargin);
485 }
486 break;
487 default:
488 obs = NULL;
489 break;
490 }
491 return obs;
492}
493
494/* Construct the shortest path from one endpoint of e to the other.
495 * vconfig is a precomputed data structure to help in the computation.
496 * If chkPts is true, the function finds the polygons, if any, containing
497 * the endpoints and tells the shortest path computation to ignore them.
498 * Assumes this info is set in ND_lim, usually from _spline_edges.
499 * Returns the shortest path.
500 */
501Ppolyline_t getPath(edge_t *e, vconfig_t *vconfig, bool chkPts) {
502 Ppolyline_t line;
503 int pp, qp;
504 Ppoint_t p, q;
505
506 p = add_pointf(ND_coord(agtail(e)), ED_tail_port(e).p);
507 q = add_pointf(ND_coord(aghead(e)), ED_head_port(e).p);
508
509 /* determine the polygons (if any) that contain the endpoints */
510 pp = qp = POLYID_NONE;
511 if (chkPts) {
512 pp = ND_lim(agtail(e));
513 qp = ND_lim(aghead(e));
514 }
515 Pobspath(vconfig, p, pp, q, qp, &line);
516 return line;
517}
518
519static void makePolyline(edge_t * e) {
520 Ppolyline_t spl, line = ED_path(e);
521
522 make_polyline (line, &spl);
523 if (Verbose > 1)
524 fprintf(stderr, "polyline %s %s\n", agnameof(agtail(e)), agnameof(aghead(e)));
525 clip_and_install(e, aghead(e), spl.ps, spl.pn, &sinfo);
526 addEdgeLabels(e);
527}
528
529/* Construct a spline connecting the endpoints of e, avoiding the npoly
530 * obstacles obs.
531 * The resultant spline is attached to the edge, the positions of any
532 * edge labels are computed, and the graph's bounding box is recomputed.
533 *
534 * If chkPts is true, the function checks if one or both of the endpoints
535 * is on or inside one of the obstacles and, if so, tells the shortest path
536 * computation to ignore them.
537 */
538void makeSpline(edge_t *e, Ppoly_t **obs, int npoly, bool chkPts) {
539 Ppolyline_t line, spline;
540 int i;
541 int pp, qp;
542 Ppoint_t p, q;
543 Pedge_t *barriers;
544
545 line = ED_path(e);
546 p = line.ps[0];
547 q = line.ps[line.pn - 1];
548 /* determine the polygons (if any) that contain the endpoints */
549 pp = qp = POLYID_NONE;
550 if (chkPts)
551 for (i = 0; i < npoly; i++) {
552 if (pp == POLYID_NONE && in_poly(*obs[i], p))
553 pp = i;
554 if (qp == POLYID_NONE && in_poly(*obs[i], q))
555 qp = i;
556 }
557
558 size_t n_barriers;
559 make_barriers(obs, npoly, pp, qp, &barriers, &n_barriers);
560 Pvector_t slopes[2] = {0};
561 if (Proutespline(barriers, n_barriers, line, slopes, &spline) < 0) {
562 agerrorf("makeSpline: failed to make spline edge (%s,%s)\n", agnameof(agtail(e)), agnameof(aghead(e)));
563 return;
564 }
565
566 /* north why did you ever use int coords */
567 if (Verbose > 1)
568 fprintf(stderr, "spline %s %s\n", agnameof(agtail(e)), agnameof(aghead(e)));
569 clip_and_install(e, aghead(e), spline.ps, spline.pn, &sinfo);
570 free(barriers);
571 addEdgeLabels(e);
572}
573
574 /* True if either head or tail has a port on its boundary */
575#define BOUNDARY_PORT(e) ((ED_tail_port(e).side)||(ED_head_port(e).side))
576
577/* Basic default routine for creating edges.
578 * If splines are requested, we construct the obstacles.
579 * If not, or nodes overlap, the function reverts to line segments.
580 * NOTE: intra-cluster edges are not constrained to
581 * remain in the cluster's bounding box and, conversely, a cluster's box
582 * is not altered to reflect intra-cluster edges.
583 * If Nop > 1 and the spline exists, it is just copied.
584 * NOTE: if edgetype = EDGETYPE_NONE, we shouldn't be here.
585 */
586static int spline_edges_(graph_t *g, expand_t *pmargin, int edgetype) {
587 node_t *n;
588 edge_t *e;
589 edge_t *e0;
590 Ppoly_t **obs = 0;
591 Ppoly_t *obp;
592 int cnt, i = 0, npoly;
593 vconfig_t *vconfig = 0;
594 int useEdges = Nop > 1;
595 int legal = 0;
596
597#ifdef HAVE_GTS
598 router_t* rtr = 0;
599#endif
600
601 /* build configuration */
602 if (edgetype >= EDGETYPE_PLINE) {
603 obs = gv_calloc(agnnodes(g), sizeof(Ppoly_t*));
604 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
605 obp = makeObstacle(n, pmargin, edgetype == EDGETYPE_ORTHO);
606 if (obp) {
607 ND_lim(n) = i;
608 obs[i++] = obp;
609 }
610 else
611 ND_lim(n) = POLYID_NONE;
612 }
613 } else {
614 obs = 0;
615 }
616 npoly = i;
617 if (obs) {
618 if ((legal = Plegal_arrangement(obs, npoly))) {
619 if (edgetype != EDGETYPE_ORTHO) vconfig = Pobsopen(obs, npoly);
620 }
621 else {
622 if (edgetype == EDGETYPE_ORTHO)
623 agwarningf("the bounding boxes of some nodes touch - falling back to straight line edges\n");
624 else
625 agwarningf("some nodes with margin (%.02f,%.02f) touch - falling back to straight line edges\n", pmargin->x, pmargin->y);
626 }
627 }
628
629 /* route edges */
630 if (Verbose)
631 fprintf(stderr, "Creating edges using %s\n",
632 (legal && edgetype == EDGETYPE_ORTHO) ? "orthogonal lines" :
633 (vconfig ? (edgetype == EDGETYPE_SPLINE ? "splines" : "polylines") :
634 "line segments"));
635 if (vconfig) {
636 /* path-finding pass */
637 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
638 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
639 ED_path(e) = getPath(e, vconfig, true);
640 }
641 }
642 }
643#ifdef ORTHO
644 else if (legal && edgetype == EDGETYPE_ORTHO) {
645 orthoEdges(g, false);
646 useEdges = 1;
647 }
648#endif
649
650 /* spline-drawing pass */
651 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
652 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
653 node_t *head = aghead(e);
654 if (useEdges && ED_spl(e)) {
655 addEdgeLabels(e);
656 if (Nop == 3 && ED_spl(e)->size > 0) {
657 // use first bezier, start point and end point will lost
658 if (ED_spl(e)->size > 1) {
659 agwarningf("edge %s -> %s : set more than one spline. First used, other dropped.\n", agnameof(n), agnameof(head));
660 }
661 bezier *bez = ED_spl(e)->list;
662 size_t sz = bez->size;
663 bez->size = 0;
664 pointf *pb = bez->list;
665 bez->list = NULL;
667 clip_and_install(e, head, pb, sz, &sinfo);
668 free(pb);
669 }
670 }
671 else if (ED_count(e) == 0) continue; /* only do representative */
672 else if (n == head) { /* self arc */
674 } else if (vconfig) { /* EDGETYPE_SPLINE or EDGETYPE_PLINE */
675#ifdef HAVE_GTS
676 if (ED_count(e) > 1 || BOUNDARY_PORT(e)) {
677 int fail = 0;
678 if (ED_path(e).pn == 2 && !BOUNDARY_PORT(e))
679 /* if a straight line can connect the ends */
680 makeStraightEdge(g, e, edgetype, &sinfo);
681 else {
682 if (!rtr) rtr = mkRouter (obs, npoly);
683 fail = makeMultiSpline(e, rtr, edgetype == EDGETYPE_PLINE);
684 }
685 if (!fail) continue;
686 }
687 /* We can probably remove this branch and just use
688 * makeMultiSpline. It can also catch the makeStraightEdge
689 * case. We could then eliminate all of the vconfig stuff.
690 */
691#endif
692 cnt = ED_count(e);
693 if (Concentrate) cnt = 1; /* only do representative */
694 e0 = e;
695 for (i = 0; i < cnt; i++) {
696 if (edgetype == EDGETYPE_SPLINE)
697 makeSpline(e0, obs, npoly, true);
698 else
699 makePolyline(e0);
700 e0 = ED_to_virt(e0);
701 }
702 } else {
703 makeStraightEdge(g, e, edgetype, &sinfo);
704 }
705 }
706 }
707
708#ifdef HAVE_GTS
709 if (rtr)
710 freeRouter (rtr);
711#endif
712
713 if (vconfig)
714 Pobsclose (vconfig);
715 if (obs) {
716 for (i=0; i < npoly; i++) {
717 free (obs[i]->ps);
718 free (obs[i]);
719 }
720 free (obs);
721 }
722 return 0;
723}
724
725/* Main wrapper code for generating edges.
726 * Sets desired separation.
727 * Coalesces equivalent edges (edges * with the same endpoints).
728 * It then calls the edge generating function, and marks the
729 * spline phase complete.
730 * Returns 0 on success.
731 *
732 * The edge function is given the graph, the separation to be added
733 * around obstacles, and the type of edge. It must guarantee
734 * that all bounding boxes are current; in particular, the bounding box of
735 * g must reflect the addition of the edges.
736 */
737int
739 int edgetype)
740{
741 node_t *n;
742 edge_t *e;
743 expand_t margin;
744 Dt_t *map;
745
746 margin = esepFactor (g);
747
748 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
749 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
750 resolvePorts (e);
751 }
752 }
753
754 /* find equivalent edges */
755 map = dtopen(&edgeItemDisc, Dtoset);
756 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
757 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
758 if (Nop > 1 && ED_spl(e)) {
759 /* If Nop > 1 (use given edges) and e has a spline, it
760 * should have its own equivalence class.
761 */
762 ED_count(e)++;
763 } else {
764 edge_t *leader = equivEdge(map, e);
765 if (leader != e) {
766 ED_count(leader)++;
767 ED_to_virt(e) = ED_to_virt(leader);
768 ED_to_virt(leader) = e;
769 }
770 }
771 }
772 }
773 dtclose(map);
774
775 if (edgefn(g, &margin, edgetype))
776 return 1;
777
779 return 0;
780}
781
782/* Construct edges using default algorithm and given splines value.
783 * Return 0 on success.
784 */
785int spline_edges1(graph_t * g, int edgetype)
786{
787 return splineEdges(g, spline_edges_, edgetype);
788}
789
790/* Sets the graph's aspect ratio.
791 * Check splines attribute and construct edges using default algorithm.
792 * If the splines attribute is defined but equal to "", skip edge routing.
793 *
794 * Assumes u.bb for has been computed for g and all clusters
795 * (not just top-level clusters), and that GD_bb(g).LL is at the origin.
796 *
797 * This last criterion is, I believe, mainly to simplify the code
798 * in neato_set_aspect. It would be good to remove this constraint,
799 * as this would allow nodes pinned on input to have the same coordinates
800 * when output in dot or plain format.
801 *
802 */
804 int et = EDGE_TYPE (g);
806 if (et == EDGETYPE_NONE) return;
807#ifndef ORTHO
808 if (et == EDGETYPE_ORTHO) {
809 agwarningf("Orthogonal edges not yet supported\n");
810 et = EDGETYPE_PLINE;
811 GD_flags(g->root) &= ~EDGETYPE_ORTHO;
813 }
814#endif
815 spline_edges1(g, et);
816}
817
818static void
820{
821 int i;
822
823 for (i = 1; i <= GD_n_cluster(g); i++) {
824 shiftClusters (GD_clust(g)[i], offset);
825 }
826
827 GD_bb(g).UR.x -= offset.x;
828 GD_bb(g).UR.y -= offset.y;
829 GD_bb(g).LL.x -= offset.x;
830 GD_bb(g).LL.y -= offset.y;
831}
832
833/* Compute bounding box, translate graph to origin,
834 * then construct all edges.
835 */
837{
838 node_t *n;
839 pointf offset;
840
841 compute_bb(g);
842 offset.x = PS2INCH(GD_bb(g).LL.x);
843 offset.y = PS2INCH(GD_bb(g).LL.y);
844 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
845 ND_pos(n)[0] -= offset.x;
846 ND_pos(n)[1] -= offset.y;
847 }
848
849 shiftClusters (g, GD_bb(g).LL);
850 spline_edges0(g, true);
851}
852
853/* Scale edge by given factor.
854 * Assume ED_spl != NULL.
855 */
856static void scaleEdge(edge_t * e, double xf, double yf)
857{
858 pointf *pt;
859 bezier *bez;
860 pointf delh, delt;
861
862 delh.x = POINTS_PER_INCH * (ND_pos(aghead(e))[0] * (xf - 1.0));
863 delh.y = POINTS_PER_INCH * (ND_pos(aghead(e))[1] * (yf - 1.0));
864 delt.x = POINTS_PER_INCH * (ND_pos(agtail(e))[0] * (xf - 1.0));
865 delt.y = POINTS_PER_INCH * (ND_pos(agtail(e))[1] * (yf - 1.0));
866
867 bez = ED_spl(e)->list;
868 for (size_t i = 0; i < ED_spl(e)->size; i++) {
869 pt = bez->list;
870 for (size_t j = 0; j < bez->size; j++) {
871 if (i == 0 && j == 0) {
872 pt->x += delt.x;
873 pt->y += delt.y;
874 }
875 else if (i == ED_spl(e)->size-1 && j == bez->size-1) {
876 pt->x += delh.x;
877 pt->y += delh.y;
878 }
879 else {
880 pt->x *= xf;
881 pt->y *= yf;
882 }
883 pt++;
884 }
885 if (bez->sflag) {
886 bez->sp.x += delt.x;
887 bez->sp.y += delt.y;
888 }
889 if (bez->eflag) {
890 bez->ep.x += delh.x;
891 bez->ep.y += delh.y;
892 }
893 bez++;
894 }
895
896 if (ED_label(e) && ED_label(e)->set) {
897 ED_label(e)->pos.x *= xf;
898 ED_label(e)->pos.y *= yf;
899 }
900 if (ED_head_label(e) && ED_head_label(e)->set) {
901 ED_head_label(e)->pos.x += delh.x;
902 ED_head_label(e)->pos.y += delh.y;
903 }
904 if (ED_tail_label(e) && ED_tail_label(e)->set) {
905 ED_tail_label(e)->pos.x += delt.x;
906 ED_tail_label(e)->pos.y += delt.y;
907 }
908}
909
911static void scaleBB(graph_t * g, double xf, double yf)
912{
913 int i;
914
915 GD_bb(g).UR.x *= xf;
916 GD_bb(g).UR.y *= yf;
917 GD_bb(g).LL.x *= xf;
918 GD_bb(g).LL.y *= yf;
919
920 if (GD_label(g) && GD_label(g)->set) {
921 GD_label(g)->pos.x *= xf;
922 GD_label(g)->pos.y *= yf;
923 }
924
925 for (i = 1; i <= GD_n_cluster(g); i++)
926 scaleBB(GD_clust(g)[i], xf, yf);
927}
928
929/* Translate edge by offset.
930 * Assume ED_spl(e) != NULL
931 */
932static void translateE(edge_t * e, pointf offset)
933{
934 pointf *pt;
935 bezier *bez;
936
937 bez = ED_spl(e)->list;
938 for (size_t i = 0; i < ED_spl(e)->size; i++) {
939 pt = bez->list;
940 for (size_t j = 0; j < bez->size; j++) {
941 pt->x -= offset.x;
942 pt->y -= offset.y;
943 pt++;
944 }
945 if (bez->sflag) {
946 bez->sp.x -= offset.x;
947 bez->sp.y -= offset.y;
948 }
949 if (bez->eflag) {
950 bez->ep.x -= offset.x;
951 bez->ep.y -= offset.y;
952 }
953 bez++;
954 }
955
956 if (ED_label(e) && ED_label(e)->set) {
957 ED_label(e)->pos.x -= offset.x;
958 ED_label(e)->pos.y -= offset.y;
959 }
960 if (ED_xlabel(e) && ED_xlabel(e)->set) {
961 ED_xlabel(e)->pos.x -= offset.x;
962 ED_xlabel(e)->pos.y -= offset.y;
963 }
964 if (ED_head_label(e) && ED_head_label(e)->set) {
965 ED_head_label(e)->pos.x -= offset.x;
966 ED_head_label(e)->pos.y -= offset.y;
967 }
968 if (ED_tail_label(e) && ED_tail_label(e)->set) {
969 ED_tail_label(e)->pos.x -= offset.x;
970 ED_tail_label(e)->pos.y -= offset.y;
971 }
972}
973
974static void translateG(Agraph_t * g, pointf offset)
975{
976 int i;
977
978 GD_bb(g).UR.x -= offset.x;
979 GD_bb(g).UR.y -= offset.y;
980 GD_bb(g).LL.x -= offset.x;
981 GD_bb(g).LL.y -= offset.y;
982
983 if (GD_label(g) && GD_label(g)->set) {
984 GD_label(g)->pos.x -= offset.x;
985 GD_label(g)->pos.y -= offset.y;
986 }
987
988 for (i = 1; i <= GD_n_cluster(g); i++)
989 translateG(GD_clust(g)[i], offset);
990}
991
993{
994 node_t *n;
995 edge_t *e;
996 pointf offset;
997 pointf ll;
998
999 ll = GD_bb(g).LL;
1000
1001 offset.x = PS2INCH(ll.x);
1002 offset.y = PS2INCH(ll.y);
1003 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
1004 ND_pos(n)[0] -= offset.x;
1005 ND_pos(n)[1] -= offset.y;
1006 if (ND_xlabel(n) && ND_xlabel(n)->set) {
1007 ND_xlabel(n)->pos.x -= ll.x;
1008 ND_xlabel(n)->pos.y -= ll.y;
1009 }
1010 }
1011 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
1012 for (e = agfstout(g, n); e; e = agnxtout(g, e))
1013 if (ED_spl(e))
1014 translateE(e, ll);
1015 }
1016 translateG(g, ll);
1017}
1018
1019/* Assume all bounding boxes are correct.
1020 * Return false if no transform is performed. This includes
1021 * the possibility that a translation was done.
1022 */
1024{
1025 double xf, yf, actual, desired;
1026 node_t *n;
1027 bool translated = false;
1028
1029 if (g->root != g)
1030 return false;
1031
1032 if (GD_drawing(g)->ratio_kind) {
1033 if (GD_bb(g).LL.x || GD_bb(g).LL.y) {
1034 translated = true;
1035 neato_translate (g);
1036 }
1037 /* normalize */
1038 if (GD_flip(g)) {
1039 GD_bb(g).UR = exch_xyf(GD_bb(g).UR);
1040 }
1041 if (GD_drawing(g)->ratio_kind == R_FILL) {
1042 /* fill is weird because both X and Y can stretch */
1043 if (GD_drawing(g)->size.x <= 0)
1044 return translated;
1045 xf = GD_drawing(g)->size.x / GD_bb(g).UR.x;
1046 yf = GD_drawing(g)->size.y / GD_bb(g).UR.y;
1047 /* handle case where one or more dimensions is too big */
1048 if (xf < 1.0 || yf < 1.0) {
1049 if (xf < yf) {
1050 yf /= xf;
1051 xf = 1.0;
1052 } else {
1053 xf /= yf;
1054 yf = 1.0;
1055 }
1056 }
1057 } else if (GD_drawing(g)->ratio_kind == R_EXPAND) {
1058 if (GD_drawing(g)->size.x <= 0)
1059 return translated;
1060 xf = GD_drawing(g)->size.x / GD_bb(g).UR.x;
1061 yf = GD_drawing(g)->size.y / GD_bb(g).UR.y;
1062 if (xf > 1.0 && yf > 1.0) {
1063 double scale = fmin(xf, yf);
1064 xf = yf = scale;
1065 } else
1066 return translated;
1067 } else if (GD_drawing(g)->ratio_kind == R_VALUE) {
1068 desired = GD_drawing(g)->ratio;
1069 actual = GD_bb(g).UR.y / GD_bb(g).UR.x;
1070 if (actual < desired) {
1071 yf = desired / actual;
1072 xf = 1.0;
1073 } else {
1074 xf = actual / desired;
1075 yf = 1.0;
1076 }
1077 } else
1078 return translated;
1079 if (GD_flip(g)) {
1080 SWAP(&xf, &yf);
1081 }
1082
1083 if (Nop > 1) {
1084 edge_t *e;
1085 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
1086 for (e = agfstout(g, n); e; e = agnxtout(g, e))
1087 if (ED_spl(e))
1088 scaleEdge(e, xf, yf);
1089 }
1090 }
1091 /* Not relying on neato_nlist here allows us not to have to
1092 * allocate it in the root graph and the connected components.
1093 */
1094 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
1095 ND_pos(n)[0] *= xf;
1096 ND_pos(n)[1] *= yf;
1097 }
1098 scaleBB(g, xf, yf);
1099 return true;
1100 }
1101 else
1102 return false;
1103}
1104
1105/* Sets aspect ratio if necessary; real work done in _neato_set_aspect;
1106 * This also copies the internal layout coordinates (ND_pos) to the
1107 * external ones (ND_coord).
1108 *
1109 * Return true if some node moved.
1110 */
1112{
1113 node_t *n;
1114 bool moved = false;
1115
1116 /* setting aspect ratio only makes sense on root graph */
1117 moved = _neato_set_aspect(g);
1118 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
1119 ND_coord(n).x = POINTS_PER_INCH * ND_pos(n)[0];
1120 ND_coord(n).y = POINTS_PER_INCH * ND_pos(n)[1];
1121 }
1122 return moved;
1123}
1124
expand_t esepFactor(graph_t *g)
Definition adjust.c:1071
Memory allocation wrappers that exit on failure.
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 M_PI
Definition arith.h:41
#define dtinsert(d, o)
Definition cdt.h:186
CDT_API int dtclose(Dt_t *)
Definition dtclose.c:10
CDT_API Dtmethod_t * Dtoset
ordered set (self-adjusting tree)
Definition dttree.c:306
CDT_API Dt_t * dtopen(Dtdisc_t *, Dtmethod_t *)
Definition dtopen.c:11
boxf polyBB(polygon_t *poly)
Definition utils.c:604
double late_double(void *obj, attrsym_t *attr, double defaultValue, double minimum)
Definition utils.c:54
void updateBB(graph_t *g, textlabel_t *lp)
Definition utils.c:624
void compute_bb(graph_t *g)
Definition utils.c:633
#define EDGETYPE_SPLINE
Definition const.h:239
#define EDGETYPE_ORTHO
Definition const.h:238
#define EDGETYPE_PLINE
Definition const.h:237
#define EDGETYPE_NONE
Definition const.h:234
#define GVSPLINES
Definition const.h:164
vconfig_t * Pobsopen(Ppoly_t **obs, int n_obs)
Definition cvt.c:28
void Pobsclose(vconfig_t *config)
Definition cvt.c:89
void Pobspath(vconfig_t *config, Ppoint_t p0, int poly0, Ppoint_t p1, int poly1, Ppolyline_t *output_route)
Definition cvt.c:102
#define head
Definition dthdr.h:15
static Dtdisc_t disc
Definition exparse.y:209
#define PS2INCH(a_points)
Definition geom.h:64
struct pointf_s pointf
#define POINTS_PER_INCH
Definition geom.h:58
#define INCH2PS(a_inches)
Definition geom.h:63
static WUR pointf add_pointf(pointf p, pointf q)
Definition geomprocs.h:88
static WUR pointf exch_xyf(pointf p)
Definition geomprocs.h:128
static WUR pointf scale(double c, pointf p)
Definition geomprocs.h:148
int State
Definition globals.h:63
bool Concentrate
Definition globals.h:59
int Nop
Definition globals.h:55
Agsym_t * N_penwidth
Definition globals.h:80
static bool Verbose
Definition gml2gv.c:26
void free(void *)
node NULL
Definition grammar.y:181
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:198
int agnnodes(Agraph_t *g)
Definition graph.c:157
#define ED_xlabel(e)
Definition types.h:590
#define ED_head_label(e)
Definition types.h:587
Agedge_t * agfstout(Agraph_t *g, Agnode_t *n)
Definition edge.c:28
#define ED_spl(e)
Definition types.h:595
#define ED_count(e)
Definition types.h:580
#define agtail(e)
Definition cgraph.h:977
#define ED_path(e)
Definition types.h:593
#define ED_tail_label(e)
Definition types.h:596
#define aghead(e)
Definition cgraph.h:978
Agedge_t * agnxtout(Agraph_t *g, Agedge_t *e)
Definition edge.c:43
#define ED_head_port(e)
Definition types.h:588
#define ED_label(e)
Definition types.h:589
#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_drawing(g)
Definition types.h:353
#define GD_clust(g)
Definition types.h:360
#define GD_flags(g)
Definition types.h:365
#define GD_bb(g)
Definition types.h:354
#define GD_n_cluster(g)
Definition types.h:389
#define GD_label(g)
Definition types.h:374
#define GD_nodesep(g)
Definition types.h:394
#define GD_flip(g)
Definition types.h:378
#define ND_outline_width(n)
Definition types.h:516
#define ND_outline_height(n)
Definition types.h:517
#define ND_ht(n)
Definition types.h:500
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_lim(n)
Definition types.h:504
#define ND_rw(n)
Definition types.h:525
#define ND_lw(n)
Definition types.h:506
#define ND_xlabel(n)
Definition types.h:503
#define ND_shape_info(n)
Definition types.h:529
#define ND_pos(n)
Definition types.h:520
#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.
#define SWAP(a, b)
Definition gv_math.h:134
static double penwidth[]
bool in_poly(const Ppoly_t poly, Ppoint_t q)
Definition inpoly.c:26
int Plegal_arrangement(Ppoly_t **polys, int n_polys)
Definition legal.c:357
static int * ps
Definition lu.c:53
#define EDGE_TYPE(g)
Definition macros.h:25
router_t * mkRouter(Ppoly_t **obsp, int npoly)
void freeRouter(router_t *rtr)
int makeMultiSpline(edge_t *e, router_t *rtr, int doPolyline)
static bool _neato_set_aspect(graph_t *g)
static int cmpitems(void *k1, void *k2)
static pointf line_intersection(linef l0, linef l1)
static void * newitem(void *p, Dtdisc_t *disc)
static void make_barriers(Ppoly_t **poly, int npoly, int pp, int qp, Pedge_t **barriers, size_t *n_barriers)
static int spline_edges_(graph_t *g, expand_t *pmargin, int edgetype)
static void translateE(edge_t *e, pointf offset)
static Ppoint_t recPt(double x, double y, pointf c, expand_t *m)
static bool swap_ends_p(edge_t *e)
int spline_edges1(graph_t *g, int edgetype)
static bool spline_merge(node_t *n)
Ppoly_t * makeObstacle(node_t *n, expand_t *pmargin, bool isOrtho)
Ppolyline_t getPath(edge_t *e, vconfig_t *vconfig, bool chkPts)
Dtdisc_t edgeItemDisc
void makeSpline(edge_t *e, Ppoly_t **obs, int npoly, bool chkPts)
void spline_edges0(graph_t *g, bool set_aspect)
static pointf circumscribed_polygon_corner_about_ellipse(double a, double b, size_t i, size_t nsides)
static double ellipse_tangent_slope(double a, double b, pointf p)
static void shiftClusters(graph_t *g, pointf offset)
static void scaleEdge(edge_t *e, double xf, double yf)
static void translateG(Agraph_t *g, pointf offset)
static void makePolyline(edge_t *e)
static void scaleBB(graph_t *g, double xf, double yf)
scale bounding box of clusters of g by given factors
static splineInfo sinfo
int splineEdges(graph_t *g, int(*edgefn)(graph_t *, expand_t *, int), int edgetype)
void makeSelfArcs(edge_t *e, int stepx)
void neato_translate(Agraph_t *g)
static edge_t * equivEdge(Dt_t *map, edge_t *e)
static Ppoint_t genPt(double x, double y, pointf c)
void spline_edges(graph_t *g)
#define BOUNDARY_PORT(e)
bool neato_set_aspect(graph_t *g)
void orthoEdges(Agraph_t *g, bool useLbls)
Definition ortho.c:1163
finds and smooths shortest paths
void make_polyline(Ppolyline_t line, Ppolyline_t *sline)
Definition util.c:60
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
static void set_aspect(graph_t *g)
Definition position.c:899
void makePortLabels(edge_t *e)
add head and tail labels if necessary and update bounding box
Definition splines.c:1205
void clip_and_install(edge_t *fe, node_t *hn, pointf *ps, size_t pn, splineInfo *info)
Definition splines.c:236
void gv_free_splines(edge_t *e)
Definition utils.c:1502
void resolvePorts(edge_t *e)
Definition shapes.c:4358
shape_kind shapeOf(node_t *)
Definition shapes.c:1908
void makeStraightEdge(graph_t *g, edge_t *e, int edgetype, splineInfo *info)
Definition routespl.c:956
void addEdgeLabels(edge_t *e)
Definition splines.c:1307
void makeSelfEdge(edge_t *edges[], size_t cnt, double sizex, double sizey, splineInfo *sinfo)
Definition splines.c:1164
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 types.h:89
size_t size
Definition types.h:91
pointf sp
Definition types.h:94
pointf * list
Definition types.h:90
uint32_t eflag
Definition types.h:93
pointf ep
Definition types.h:95
uint32_t sflag
Definition types.h:92
Definition geom.h:41
pointf UR
Definition geom.h:41
pointf LL
Definition geom.h:41
Definition cdt.h:98
node_t * n2
pointf p1
pointf p2
node_t * n1
edge_t * e
edgeinfo id
Dtlink_t link
double x
Definition adjust.h:41
double y
Definition adjust.h:41
bool doAdd
Definition adjust.h:42
boxf b
Definition types.h:237
Definition geom.h:31
pointf p
Definition geom.h:32
double m
Definition geom.h:33
double x
Definition geom.h:29
double y
Definition geom.h:29
bool(* swapEnds)(edge_t *e)
Definition types.h:67
struct poly_s poly
void(* edgefn)(Agraph_t *, Agedge_t *, glCompColor)
@ SH_EPSF
Definition types.h:187
@ SH_RECORD
Definition types.h:187
@ SH_POINT
Definition types.h:187
@ SH_POLY
Definition types.h:187
@ R_VALUE
Definition types.h:216
@ R_FILL
Definition types.h:216
@ R_EXPAND
Definition types.h:216
#define UNREACHABLE()
Definition unreachable.h:30
#define POLYID_NONE
Definition vispath.h:50