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