Graphviz 13.0.0~dev.20241220.2304
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/unreachable.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(void *p, Dtdisc_t *disc) {
109 edgeitem *obj = p;
110 edgeitem *newp;
111
112 (void)disc;
113 newp = gv_alloc(sizeof(edgeitem));
114 newp->id = obj->id;
115 newp->e = obj->e;
116 ED_count(newp->e) = 1;
117
118 return newp;
119}
120
121static int cmpitems(void *k1, void *k2) {
122 const edgeinfo *key1 = k1;
123 const edgeinfo *key2 = k2;
124 if (key1->n1 > key2->n1)
125 return 1;
126 if (key1->n1 < key2->n1)
127 return -1;
128 if (key1->n2 > key2->n2)
129 return 1;
130 if (key1->n2 < key2->n2)
131 return -1;
132
133 if (key1->p1.x > key2->p1.x)
134 return 1;
135 if (key1->p1.x < key2->p1.x)
136 return -1;
137 if (key1->p1.y > key2->p1.y)
138 return 1;
139 if (key1->p1.y < key2->p1.y)
140 return -1;
141 if (key1->p2.x > key2->p2.x)
142 return 1;
143 if (key1->p2.x < key2->p2.x)
144 return -1;
145 if (key1->p2.y > key2->p2.y)
146 return 1;
147 if (key1->p2.y < key2->p2.y)
148 return -1;
149 return 0;
150}
151
153 offsetof(edgeitem, id),
154 sizeof(edgeinfo),
155 offsetof(edgeitem, link),
156 newitem,
157 free,
158 cmpitems,
159};
160
161/* equivEdge:
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/* makeSelfArcs:
212 * Generate loops. We use the library routine makeSelfEdge
213 * which also places the labels.
214 * We have to handle port labels here.
215 * as well as update the bbox from edge labels.
216 */
217void makeSelfArcs(edge_t * e, int stepx)
218{
219 assert(ED_count(e) >= 0);
220 const size_t cnt = (size_t)ED_count(e);
221
222 if (cnt == 1 || Concentrate) {
223 edge_t *edges1[1];
224 edges1[0] = e;
225 makeSelfEdge(edges1, 0, 1, stepx, stepx, &sinfo);
226 if (ED_label(e))
229 } else if (cnt > 1) {
230 edge_t **edges = gv_calloc(cnt, sizeof(edge_t*));
231 for (size_t i = 0; i < cnt; i++) {
232 edges[i] = e;
233 e = ED_to_virt(e);
234 }
235 makeSelfEdge(edges, 0, cnt, stepx, stepx, &sinfo);
236 for (size_t i = 0; i < cnt; i++) {
237 e = edges[i];
238 if (ED_label(e))
241 }
242 free(edges);
243 }
244}
245
270static double ellipse_tangent_slope(double a, double b, pointf p) {
271 assert(p.x != a &&
272 "cannot handle ellipse tangent slope in horizontal extreme point");
273 const double sign_y = p.y >= 0 ? 1 : -1;
274 const double m = -sign_y * (b * p.x) / (a * sqrt(a * a - p.x * p.x));
275 assert(isfinite(m) && "ellipse tangent slope is infinite");
276 return m;
277}
278
286 const double x =
287 (l0.m * l0.p.x - l0.p.y - l1.m * l1.p.x + l1.p.y) / (l0.m - l1.m);
288 const double y = l0.p.y + l0.m * (x - l0.p.x);
289 return (pointf){x, y};
290}
291
301 size_t i,
302 size_t nsides) {
303 const double angle0 = 2.0 * M_PI * ((double)i - 0.5) / (double)nsides;
304 const double angle1 = 2.0 * M_PI * ((double)i + 0.5) / (double)nsides;
305 const pointf p0 = {a * cos(angle0), b * sin(angle0)};
306 const pointf p1 = {a * cos(angle1), b * sin(angle1)};
307 const double m0 = ellipse_tangent_slope(a, b, p0);
308 const double m1 = ellipse_tangent_slope(a, b, p1);
309 const linef line0 = {{p0.x, p0.y}, m0};
310 const linef line1 = {{p1.x, p1.y}, m1};
311 return line_intersection(line0, line1);
312}
313
314/* makeObstacle:
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.0, ymargin = 0.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.0, 0.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/* getPath
495 * Construct the shortest path from one endpoint of e to the other.
496 * vconfig is a precomputed data structure to help in the computation.
497 * If chkPts is true, the function finds the polygons, if any, containing
498 * the endpoints and tells the shortest path computation to ignore them.
499 * Assumes this info is set in ND_lim, usually from _spline_edges.
500 * Returns the shortest path.
501 */
502Ppolyline_t getPath(edge_t *e, vconfig_t *vconfig, bool chkPts) {
503 Ppolyline_t line;
504 int pp, qp;
505 Ppoint_t p, q;
506
507 p = add_pointf(ND_coord(agtail(e)), ED_tail_port(e).p);
508 q = add_pointf(ND_coord(aghead(e)), ED_head_port(e).p);
509
510 /* determine the polygons (if any) that contain the endpoints */
511 pp = qp = POLYID_NONE;
512 if (chkPts) {
513 pp = ND_lim(agtail(e));
514 qp = ND_lim(aghead(e));
515 }
516 Pobspath(vconfig, p, pp, q, qp, &line);
517 return line;
518}
519
520static void makePolyline(edge_t * e) {
521 Ppolyline_t spl, line = ED_path(e);
522
523 make_polyline (line, &spl);
524 if (Verbose > 1)
525 fprintf(stderr, "polyline %s %s\n", agnameof(agtail(e)), agnameof(aghead(e)));
526 clip_and_install(e, aghead(e), spl.ps, spl.pn, &sinfo);
527 addEdgeLabels(e);
528}
529
530/* makeSpline:
531 * Construct a spline connecting the endpoints of e, avoiding the npoly
532 * obstacles obs.
533 * The resultant spline is attached to the edge, the positions of any
534 * edge labels are computed, and the graph's bounding box is recomputed.
535 *
536 * If chkPts is true, the function checks if one or both of the endpoints
537 * is on or inside one of the obstacles and, if so, tells the shortest path
538 * computation to ignore them.
539 */
540void makeSpline(edge_t *e, Ppoly_t **obs, int npoly, bool chkPts) {
541 Ppolyline_t line, spline;
542 Pvector_t slopes[2];
543 int i;
544 int pp, qp;
545 Ppoint_t p, q;
546 Pedge_t *barriers;
547
548 line = ED_path(e);
549 p = line.ps[0];
550 q = line.ps[line.pn - 1];
551 /* determine the polygons (if any) that contain the endpoints */
552 pp = qp = POLYID_NONE;
553 if (chkPts)
554 for (i = 0; i < npoly; i++) {
555 if (pp == POLYID_NONE && in_poly(*obs[i], p))
556 pp = i;
557 if (qp == POLYID_NONE && in_poly(*obs[i], q))
558 qp = i;
559 }
560
561 size_t n_barriers;
562 make_barriers(obs, npoly, pp, qp, &barriers, &n_barriers);
563 slopes[0].x = slopes[0].y = 0.0;
564 slopes[1].x = slopes[1].y = 0.0;
565 if (Proutespline(barriers, n_barriers, line, slopes, &spline) < 0) {
566 agerrorf("makeSpline: failed to make spline edge (%s,%s)\n", agnameof(agtail(e)), agnameof(aghead(e)));
567 return;
568 }
569
570 /* north why did you ever use int coords */
571 if (Verbose > 1)
572 fprintf(stderr, "spline %s %s\n", agnameof(agtail(e)), agnameof(aghead(e)));
573 clip_and_install(e, aghead(e), spline.ps, spline.pn, &sinfo);
574 free(barriers);
575 addEdgeLabels(e);
576}
577
578 /* True if either head or tail has a port on its boundary */
579#define BOUNDARY_PORT(e) ((ED_tail_port(e).side)||(ED_head_port(e).side))
580
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 node_t *n;
592 edge_t *e;
593 edge_t *e0;
594 Ppoly_t **obs = 0;
595 Ppoly_t *obp;
596 int cnt, i = 0, npoly;
597 vconfig_t *vconfig = 0;
598 int useEdges = Nop > 1;
599 int legal = 0;
600
601#ifdef HAVE_GTS
602 router_t* rtr = 0;
603#endif
604
605 /* build configuration */
606 if (edgetype >= EDGETYPE_PLINE) {
607 obs = gv_calloc(agnnodes(g), sizeof(Ppoly_t*));
608 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
609 obp = makeObstacle(n, pmargin, edgetype == EDGETYPE_ORTHO);
610 if (obp) {
611 ND_lim(n) = i;
612 obs[i++] = obp;
613 }
614 else
615 ND_lim(n) = POLYID_NONE;
616 }
617 } else {
618 obs = 0;
619 }
620 npoly = i;
621 if (obs) {
622 if ((legal = Plegal_arrangement(obs, npoly))) {
623 if (edgetype != EDGETYPE_ORTHO) vconfig = Pobsopen(obs, npoly);
624 }
625 else {
626 if (edgetype == EDGETYPE_ORTHO)
627 agwarningf("the bounding boxes of some nodes touch - falling back to straight line edges\n");
628 else
629 agwarningf("some nodes with margin (%.02f,%.02f) touch - falling back to straight line edges\n", pmargin->x, pmargin->y);
630 }
631 }
632
633 /* route edges */
634 if (Verbose)
635 fprintf(stderr, "Creating edges using %s\n",
636 (legal && edgetype == EDGETYPE_ORTHO) ? "orthogonal lines" :
637 (vconfig ? (edgetype == EDGETYPE_SPLINE ? "splines" : "polylines") :
638 "line segments"));
639 if (vconfig) {
640 /* path-finding pass */
641 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
642 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
643 ED_path(e) = getPath(e, vconfig, true);
644 }
645 }
646 }
647#ifdef ORTHO
648 else if (legal && edgetype == EDGETYPE_ORTHO) {
649 orthoEdges(g, false);
650 useEdges = 1;
651 }
652#endif
653
654 /* spline-drawing pass */
655 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
656 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
657/* fprintf (stderr, "%s -- %s %d\n", agnameof(agtail(e)), agnameof(aghead(e)), ED_count(e)); */
658 node_t *head = aghead(e);
659 if (useEdges && ED_spl(e)) {
662 addEdgeLabels(e);
663 }
664 else if (ED_count(e) == 0) continue; /* only do representative */
665 else if (n == head) { /* self arc */
667 } else if (vconfig) { /* EDGETYPE_SPLINE or EDGETYPE_PLINE */
668#ifdef HAVE_GTS
669 if (ED_count(e) > 1 || BOUNDARY_PORT(e)) {
670 int fail = 0;
671 if (ED_path(e).pn == 2 && !BOUNDARY_PORT(e))
672 /* if a straight line can connect the ends */
673 makeStraightEdge(g, e, edgetype, &sinfo);
674 else {
675 if (!rtr) rtr = mkRouter (obs, npoly);
676 fail = makeMultiSpline(e, rtr, edgetype == EDGETYPE_PLINE);
677 }
678 if (!fail) continue;
679 }
680 /* We can probably remove this branch and just use
681 * makeMultiSpline. It can also catch the makeStraightEdge
682 * case. We could then eliminate all of the vconfig stuff.
683 */
684#endif
685 cnt = ED_count(e);
686 if (Concentrate) cnt = 1; /* only do representative */
687 e0 = e;
688 for (i = 0; i < cnt; i++) {
689 if (edgetype == EDGETYPE_SPLINE)
690 makeSpline(e0, obs, npoly, true);
691 else
692 makePolyline(e0);
693 e0 = ED_to_virt(e0);
694 }
695 } else {
696 makeStraightEdge(g, e, edgetype, &sinfo);
697 }
698 }
699 }
700
701#ifdef HAVE_GTS
702 if (rtr)
703 freeRouter (rtr);
704#endif
705
706 if (vconfig)
707 Pobsclose (vconfig);
708 if (obs) {
709 for (i=0; i < npoly; i++) {
710 free (obs[i]->ps);
711 free (obs[i]);
712 }
713 free (obs);
714 }
715 return 0;
716}
717
718/* splineEdges:
719 * Main wrapper code for generating edges.
720 * Sets desired separation.
721 * Coalesces equivalent edges (edges * with the same endpoints).
722 * It then calls the edge generating function, and marks the
723 * spline phase complete.
724 * Returns 0 on success.
725 *
726 * The edge function is given the graph, the separation to be added
727 * around obstacles, and the type of edge. It must guarantee
728 * that all bounding boxes are current; in particular, the bounding box of
729 * g must reflect the addition of the edges.
730 */
731int
733 int edgetype)
734{
735 node_t *n;
736 edge_t *e;
737 expand_t margin;
738 Dt_t *map;
739
740 margin = esepFactor (g);
741
742 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
743 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
744 resolvePorts (e);
745 }
746 }
747
748 /* find equivalent edges */
749 map = dtopen(&edgeItemDisc, Dtoset);
750 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
751 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
752 if (Nop > 1 && ED_spl(e)) {
753 /* If Nop > 1 (use given edges) and e has a spline, it
754 * should have its own equivalence class.
755 */
756 ED_count(e)++;
757 } else {
758 edge_t *leader = equivEdge(map, e);
759 if (leader != e) {
760 ED_count(leader)++;
761 ED_to_virt(e) = ED_to_virt(leader);
762 ED_to_virt(leader) = e;
763 }
764 }
765 }
766 }
767 dtclose(map);
768
769 if (edgefn(g, &margin, edgetype))
770 return 1;
771
773 return 0;
774}
775
776/* spline_edges1:
777 * Construct edges using default algorithm and given splines value.
778 * Return 0 on success.
779 */
780int spline_edges1(graph_t * g, int edgetype)
781{
782 return splineEdges(g, spline_edges_, edgetype);
783}
784
785/* spline_edges0:
786 * Sets the graph's aspect ratio.
787 * Check splines attribute and construct edges using default algorithm.
788 * If the splines attribute is defined but equal to "", skip edge routing.
789 *
790 * Assumes u.bb for has been computed for g and all clusters
791 * (not just top-level clusters), and that GD_bb(g).LL is at the origin.
792 *
793 * This last criterion is, I believe, mainly to simplify the code
794 * in neato_set_aspect. It would be good to remove this constraint,
795 * as this would allow nodes pinned on input to have the same coordinates
796 * when output in dot or plain format.
797 *
798 */
800 int et = EDGE_TYPE (g);
802 if (et == EDGETYPE_NONE) return;
803#ifndef ORTHO
804 if (et == EDGETYPE_ORTHO) {
805 agwarningf("Orthogonal edges not yet supported\n");
806 et = EDGETYPE_PLINE;
807 GD_flags(g->root) &= ~EDGETYPE_ORTHO;
809 }
810#endif
811 spline_edges1(g, et);
812}
813
814static void
816{
817 int i;
818
819 for (i = 1; i <= GD_n_cluster(g); i++) {
821 }
822
823 GD_bb(g).UR.x -= offset.x;
824 GD_bb(g).UR.y -= offset.y;
825 GD_bb(g).LL.x -= offset.x;
826 GD_bb(g).LL.y -= offset.y;
827}
828
829/* spline_edges:
830 * Compute bounding box, translate graph to origin,
831 * then construct all edges.
832 */
834{
835 node_t *n;
837
838 compute_bb(g);
839 offset.x = PS2INCH(GD_bb(g).LL.x);
840 offset.y = PS2INCH(GD_bb(g).LL.y);
841 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
842 ND_pos(n)[0] -= offset.x;
843 ND_pos(n)[1] -= offset.y;
844 }
845
846 shiftClusters (g, GD_bb(g).LL);
847 spline_edges0(g, true);
848}
849
850/* scaleEdge:
851 * Scale edge by given factor.
852 * Assume ED_spl != NULL.
853 */
854static void scaleEdge(edge_t * e, double xf, double yf)
855{
856 pointf *pt;
857 bezier *bez;
858 pointf delh, delt;
859
860 delh.x = POINTS_PER_INCH * (ND_pos(aghead(e))[0] * (xf - 1.0));
861 delh.y = POINTS_PER_INCH * (ND_pos(aghead(e))[1] * (yf - 1.0));
862 delt.x = POINTS_PER_INCH * (ND_pos(agtail(e))[0] * (xf - 1.0));
863 delt.y = POINTS_PER_INCH * (ND_pos(agtail(e))[1] * (yf - 1.0));
864
865 bez = ED_spl(e)->list;
866 for (size_t i = 0; i < ED_spl(e)->size; i++) {
867 pt = bez->list;
868 for (size_t j = 0; j < bez->size; j++) {
869 if (i == 0 && j == 0) {
870 pt->x += delt.x;
871 pt->y += delt.y;
872 }
873 else if (i == ED_spl(e)->size-1 && j == bez->size-1) {
874 pt->x += delh.x;
875 pt->y += delh.y;
876 }
877 else {
878 pt->x *= xf;
879 pt->y *= yf;
880 }
881 pt++;
882 }
883 if (bez->sflag) {
884 bez->sp.x += delt.x;
885 bez->sp.y += delt.y;
886 }
887 if (bez->eflag) {
888 bez->ep.x += delh.x;
889 bez->ep.y += delh.y;
890 }
891 bez++;
892 }
893
894 if (ED_label(e) && ED_label(e)->set) {
895 ED_label(e)->pos.x *= xf;
896 ED_label(e)->pos.y *= yf;
897 }
898 if (ED_head_label(e) && ED_head_label(e)->set) {
899 ED_head_label(e)->pos.x += delh.x;
900 ED_head_label(e)->pos.y += delh.y;
901 }
902 if (ED_tail_label(e) && ED_tail_label(e)->set) {
903 ED_tail_label(e)->pos.x += delt.x;
904 ED_tail_label(e)->pos.y += delt.y;
905 }
906}
907
908/* scaleBB:
909 * Scale bounding box of clusters of g by given factors.
910 */
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/* translateE:
930 * Translate edge by offset.
931 * Assume ED_spl(e) != NULL
932 */
934{
935 pointf *pt;
936 bezier *bez;
937
938 bez = ED_spl(e)->list;
939 for (size_t i = 0; i < ED_spl(e)->size; i++) {
940 pt = bez->list;
941 for (size_t j = 0; j < bez->size; j++) {
942 pt->x -= offset.x;
943 pt->y -= offset.y;
944 pt++;
945 }
946 if (bez->sflag) {
947 bez->sp.x -= offset.x;
948 bez->sp.y -= offset.y;
949 }
950 if (bez->eflag) {
951 bez->ep.x -= offset.x;
952 bez->ep.y -= offset.y;
953 }
954 bez++;
955 }
956
957 if (ED_label(e) && ED_label(e)->set) {
958 ED_label(e)->pos.x -= offset.x;
959 ED_label(e)->pos.y -= offset.y;
960 }
961 if (ED_xlabel(e) && ED_xlabel(e)->set) {
962 ED_xlabel(e)->pos.x -= offset.x;
963 ED_xlabel(e)->pos.y -= offset.y;
964 }
965 if (ED_head_label(e) && ED_head_label(e)->set) {
966 ED_head_label(e)->pos.x -= offset.x;
967 ED_head_label(e)->pos.y -= offset.y;
968 }
969 if (ED_tail_label(e) && ED_tail_label(e)->set) {
970 ED_tail_label(e)->pos.x -= offset.x;
971 ED_tail_label(e)->pos.y -= offset.y;
972 }
973}
974
976{
977 int i;
978
979 GD_bb(g).UR.x -= offset.x;
980 GD_bb(g).UR.y -= offset.y;
981 GD_bb(g).LL.x -= offset.x;
982 GD_bb(g).LL.y -= offset.y;
983
984 if (GD_label(g) && GD_label(g)->set) {
985 GD_label(g)->pos.x -= offset.x;
986 GD_label(g)->pos.y -= offset.y;
987 }
988
989 for (i = 1; i <= GD_n_cluster(g); i++)
990 translateG(GD_clust(g)[i], offset);
991}
992
994{
995 node_t *n;
996 edge_t *e;
998 pointf ll;
999
1000 ll = GD_bb(g).LL;
1001
1002 offset.x = PS2INCH(ll.x);
1003 offset.y = PS2INCH(ll.y);
1004 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
1005 ND_pos(n)[0] -= offset.x;
1006 ND_pos(n)[1] -= offset.y;
1007 if (ND_xlabel(n) && ND_xlabel(n)->set) {
1008 ND_xlabel(n)->pos.x -= ll.x;
1009 ND_xlabel(n)->pos.y -= ll.y;
1010 }
1011 }
1012 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
1013 for (e = agfstout(g, n); e; e = agnxtout(g, e))
1014 if (ED_spl(e))
1015 translateE(e, ll);
1016 }
1017 translateG(g, ll);
1018}
1019
1020/* _neato_set_aspect;
1021 * Assume all bounding boxes are correct.
1022 * Return false if no transform is performed. This includes
1023 * the possibility that a translation was done.
1024 */
1026{
1027 double xf, yf, actual, desired;
1028 node_t *n;
1029 bool translated = false;
1030
1031 if (g->root != g)
1032 return false;
1033
1034 if (GD_drawing(g)->ratio_kind) {
1035 if (GD_bb(g).LL.x || GD_bb(g).LL.y) {
1036 translated = true;
1037 neato_translate (g);
1038 }
1039 /* normalize */
1040 if (GD_flip(g)) {
1041 GD_bb(g).UR = exch_xyf(GD_bb(g).UR);
1042 }
1043 if (GD_drawing(g)->ratio_kind == R_FILL) {
1044 /* fill is weird because both X and Y can stretch */
1045 if (GD_drawing(g)->size.x <= 0)
1046 return translated;
1047 xf = GD_drawing(g)->size.x / GD_bb(g).UR.x;
1048 yf = GD_drawing(g)->size.y / GD_bb(g).UR.y;
1049 /* handle case where one or more dimensions is too big */
1050 if (xf < 1.0 || yf < 1.0) {
1051 if (xf < yf) {
1052 yf /= xf;
1053 xf = 1.0;
1054 } else {
1055 xf /= yf;
1056 yf = 1.0;
1057 }
1058 }
1059 } else if (GD_drawing(g)->ratio_kind == R_EXPAND) {
1060 if (GD_drawing(g)->size.x <= 0)
1061 return translated;
1062 xf = GD_drawing(g)->size.x / GD_bb(g).UR.x;
1063 yf = GD_drawing(g)->size.y / GD_bb(g).UR.y;
1064 if (xf > 1.0 && yf > 1.0) {
1065 double scale = fmin(xf, yf);
1066 xf = yf = scale;
1067 } else
1068 return translated;
1069 } else if (GD_drawing(g)->ratio_kind == R_VALUE) {
1070 desired = GD_drawing(g)->ratio;
1071 actual = GD_bb(g).UR.y / GD_bb(g).UR.x;
1072 if (actual < desired) {
1073 yf = desired / actual;
1074 xf = 1.0;
1075 } else {
1076 xf = actual / desired;
1077 yf = 1.0;
1078 }
1079 } else
1080 return translated;
1081 if (GD_flip(g)) {
1082 double t = xf;
1083 xf = yf;
1084 yf = t;
1085 }
1086
1087 if (Nop > 1) {
1088 edge_t *e;
1089 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
1090 for (e = agfstout(g, n); e; e = agnxtout(g, e))
1091 if (ED_spl(e))
1092 scaleEdge(e, xf, yf);
1093 }
1094 }
1095 /* Not relying on neato_nlist here allows us not to have to
1096 * allocate it in the root graph and the connected components.
1097 */
1098 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
1099 ND_pos(n)[0] *= xf;
1100 ND_pos(n)[1] *= yf;
1101 }
1102 scaleBB(g, xf, yf);
1103 return true;
1104 }
1105 else
1106 return false;
1107}
1108
1109/* neato_set_aspect:
1110 * Sets aspect ratio if necessary; real work done in _neato_set_aspect;
1111 * This also copies the internal layout coordinates (ND_pos) to the
1112 * external ones (ND_coord).
1113 *
1114 * Return true if some node moved.
1115 */
1117{
1118 node_t *n;
1119 bool moved = false;
1120
1121 /* setting aspect ratio only makes sense on root graph */
1122 moved = _neato_set_aspect(g);
1123 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
1124 ND_coord(n).x = POINTS_PER_INCH * ND_pos(n)[0];
1125 ND_coord(n).y = POINTS_PER_INCH * ND_pos(n)[1];
1126 }
1127 return moved;
1128}
1129
expand_t esepFactor(graph_t *g)
Definition adjust.c:1094
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:185
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:599
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:619
void compute_bb(graph_t *g)
Definition utils.c:628
#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
static Dtdisc_t disc
Definition exparse.y:209
#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:62
bool Concentrate
Definition globals.h:58
int Nop
Definition globals.h:54
Agsym_t * N_penwidth
Definition globals.h:79
static bool Verbose
Definition gml2gv.c:23
void free(void *)
node NULL
Definition grammar.y:163
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:210
int agnnodes(Agraph_t *g)
Definition graph.c:169
#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:24
#define ED_spl(e)
Definition types.h:595
#define ED_count(e)
Definition types.h:580
#define agtail(e)
Definition cgraph.h:880
#define ED_path(e)
Definition types.h:593
#define ED_tail_label(e)
Definition types.h:596
#define aghead(e)
Definition cgraph.h:881
Agedge_t * agnxtout(Agraph_t *g, Agedge_t *e)
Definition edge.c:39
#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:185
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:158
swig_ptr_object_handlers offset
Definition gv_php.cpp:5907
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: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 * 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)
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:1231
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:909
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:4358
shape_kind shapeOf(node_t *)
Definition shapes.c:1906
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 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:100
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