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