Graphviz 16.1.3~dev.20261128.0221
Loading...
Searching...
No Matches
dotsplines.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 v2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11/*
12 * set edge splines.
13 */
14
15#include "config.h"
16
17#include <assert.h>
18#include <common/boxes.h>
19#include <dotgen/dot.h>
20#include <math.h>
21#include <stdatomic.h>
22#include <stdbool.h>
23#include <stddef.h>
24#include <stdint.h>
25#include <stdlib.h>
26#include <string.h>
27#include <util/agxbuf.h>
28#include <util/alloc.h>
29#include <util/gv_math.h>
30#include <util/list.h>
31
32#ifdef ORTHO
33#include <ortho/ortho.h>
34#endif
35
36#define NSUB 9 /* number of subdivisions, re-aiming splines */
37
38#define MINW 16 /* minimum width of a box in the edge path */
39#define HALFMINW 8
40
41#define FWDEDGE 16
42#define BWDEDGE 32
43
44#define MAINGRAPH 64
45#define AUXGRAPH 128
46#define GRAPHTYPEMASK 192 /* the OR of the above */
47
48static void makefwdedge(edge_t *new, edge_t *old) {
49 Agedgeinfo_t *const info =
50 (Agedgeinfo_t *)((uintptr_t)new->base.data - offsetof(Agedgeinfo_t, hdr));
51 const Agedgeinfo_t *const old_info =
52 (Agedgeinfo_t *)((uintptr_t)old->base.data - offsetof(Agedgeinfo_t, hdr));
53 *info = *old_info;
54 *new = *old;
55 new->base.data = &info->hdr;
56 AGTAIL(new) = AGHEAD(old);
57 AGHEAD(new) = AGTAIL(old);
58 ED_tail_port(new) = ED_head_port(old);
59 ED_head_port(new) = ED_tail_port(old);
60 ED_edge_type(new) = VIRTUAL;
61 ED_to_orig(new) = old;
62}
63
64typedef struct {
65 double LeftBound;
66 double RightBound;
67 double Splinesep;
68 double Multisep;
71
72typedef LIST(pointf) points_t;
73
74static void adjustregularpath(path *, size_t, size_t);
75static Agedge_t *bot_bound(Agedge_t *, int);
76static bool pathscross(Agnode_t *, Agnode_t *, Agedge_t *, Agedge_t *);
78static bool cl_vninside(Agraph_t *, Agnode_t *);
80 pathend_t *, const boxes_t *);
81static int edgecmp(const void *, const void *);
82static int make_flat_edge(graph_t *, const spline_info_t, path *, Agedge_t **,
83 unsigned, int);
84static void make_regular_edge(graph_t *g, spline_info_t *, path *, Agedge_t **,
85 unsigned, int);
86static boxf makeregularend(boxf, int, double);
88 Agedge_t *, Agedge_t *);
89static Agnode_t *neighbor(graph_t *, Agnode_t *, Agedge_t *, Agedge_t *, int);
90static void place_vnlabel(Agnode_t *);
91static boxf rank_box(spline_info_t *sp, Agraph_t *, int);
92static void recover_slack(Agedge_t *, path *);
93static void resize_vn(Agnode_t *, double, double, double);
94static void setflags(Agedge_t *, int, int, int);
95static int straight_len(Agnode_t *);
96static Agedge_t *straight_path(Agedge_t *, int, points_t *);
97static Agedge_t *top_bound(Agedge_t *, int);
98
99static edge_t *getmainedge(edge_t *e) {
100 edge_t *le = e;
101 while (ED_to_virt(le))
102 le = ED_to_virt(le);
103 while (ED_to_orig(le))
104 le = ED_to_orig(le);
105 return le;
106}
107
108static bool spline_merge(node_t *n) {
109 return ND_node_type(n) == VIRTUAL &&
110 (ND_in(n).size > 1 || ND_out(n).size > 1);
111}
112
113static bool swap_ends_p(edge_t *e) {
114 while (ED_to_orig(e))
115 e = ED_to_orig(e);
116 if (ND_rank(aghead(e)) > ND_rank(agtail(e)))
117 return false;
118 if (ND_rank(aghead(e)) < ND_rank(agtail(e)))
119 return true;
120 if (ND_order(aghead(e)) >= ND_order(agtail(e)))
121 return false;
122 return true;
123}
124
126 .splineMerge = spline_merge};
127
128int portcmp(port p0, port p1) {
129 if (!p1.defined)
130 return p0.defined ? 1 : 0;
131 if (!p0.defined)
132 return -1;
133 if (p0.p.x < p1.p.x)
134 return -1;
135 if (p0.p.x > p1.p.x)
136 return 1;
137 if (p0.p.y < p1.p.y)
138 return -1;
139 if (p0.p.y > p1.p.y)
140 return 1;
141 return 0;
142}
143
144static void swap_bezier(bezier *b) {
145 const size_t sz = b->size;
146 for (size_t i = 0; i < sz / 2; ++i) { // reverse list of points
147 SWAP(&b->list[i], &b->list[sz - 1 - i]);
148 }
149
150 SWAP(&b->sflag, &b->eflag);
151 SWAP(&b->sp, &b->ep);
152}
153
154static void swap_spline(splines *s) {
155 const size_t sz = s->size;
156
157 // reverse list
158 for (size_t i = 0; i < sz / 2; ++i) {
159 SWAP(&s->list[i], &s->list[sz - 1 - i]);
160 }
161
162 // swap Béziers
163 for (size_t i = 0; i < sz; ++i) {
164 swap_bezier(&s->list[i]);
165 }
166}
167
168/* Some back edges are reversed during layout and the reversed edge
169 * is used to compute the spline. We would like to guarantee that
170 * the order of control points always goes from tail to head, so
171 * we reverse them if necessary.
172 */
173static void edge_normalize(graph_t *g) {
174 for (node_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
175 for (edge_t *e = agfstout(g, n); e; e = agnxtout(g, e)) {
176 if (sinfo.swapEnds(e) && ED_spl(e))
178 }
179 }
180}
181
182/* In position, each node has its rw stored in mval and,
183 * if a node is part of a loop, rw may be increased to
184 * reflect the loops and associated labels. We restore
185 * the original value here.
186 */
187static void resetRW(graph_t *g) {
188 for (node_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
189 if (ND_other(n).list) {
190 SWAP(&ND_rw(n), &ND_mval(n));
191 }
192 }
193}
194
195/* Set edge label position information for regular and non-adjacent flat edges.
196 * Dot has allocated space and position for these labels. This info will be
197 * used when routing orthogonal edges.
198 */
199static void setEdgeLabelPos(graph_t *g) {
200 textlabel_t *l;
201
202 /* place regular edge labels */
203 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
204 if (ND_node_type(n) == VIRTUAL) {
205 if (ND_alg(n)) { // label of non-adjacent flat edge
206 edge_t *fe = ND_alg(n);
207 l = ED_label(fe);
208 assert(l);
209 l->pos = ND_coord(n);
210 l->set = true;
211 } else if ((l = ND_label(n))) { // label of regular edge
212 place_vnlabel(n);
213 }
214 if (l)
215 updateBB(g, l);
216 }
217 }
218}
219
228static int dot_splines_(graph_t *g, int normalize) {
229 int i, n_nodes;
230 node_t *n;
231 Agedgeinfo_t fwdedgeai, fwdedgebi;
232 Agedgepair_t fwdedgea, fwdedgeb;
233 edge_t *e, *e0, *e1, *ea, *eb, *le1;
234 path P = {0};
235 int et = EDGE_TYPE(g);
236 fwdedgea.out.base.data = &fwdedgeai.hdr;
237 fwdedgeb.out.base.data = &fwdedgebi.hdr;
238
239 if (et == EDGETYPE_NONE)
240 return 0;
241 if (et == EDGETYPE_CURVED) {
242 resetRW(g);
243 if (GD_has_labels(g->root) & EDGE_LABEL) {
244 agwarningf("edge labels with splines=curved not supported in dot - use "
245 "xlabels\n");
246 }
247 }
248 spline_info_t sd = {0};
249 LIST(edge_t *) edges = {0};
250#ifdef ORTHO
251 if (et == EDGETYPE_ORTHO) {
252 resetRW(g);
253 if (GD_has_labels(g->root) & EDGE_LABEL) {
255 const int rc = orthoEdges(g, true);
256 if (rc != 0) {
257 return rc;
258 }
259 } else {
260 const int rc = orthoEdges(g, false);
261 if (rc != 0) {
262 return rc;
263 }
264 }
265 goto finish;
266 }
267#else
268 (void)setEdgeLabelPos;
269#endif
270
273 sd = (spline_info_t){.Splinesep = GD_nodesep(g) / 4,
274 .Multisep = GD_nodesep(g)};
275
276 /* compute boundaries and list of splines */
277 n_nodes = 0;
278 for (i = GD_minrank(g); i <= GD_maxrank(g); i++) {
279 n_nodes += GD_rank(g)[i].n;
280 if ((n = GD_rank(g)[i].v[0]))
281 sd.LeftBound = MIN(sd.LeftBound, ND_coord(n).x - ND_lw(n));
282 if (GD_rank(g)[i].n && (n = GD_rank(g)[i].v[GD_rank(g)[i].n - 1]))
283 sd.RightBound = MAX(sd.RightBound, ND_coord(n).x + ND_rw(n));
284 sd.LeftBound -= MINW;
285 sd.RightBound += MINW;
286
287 for (int j = 0; j < GD_rank(g)[i].n; j++) {
288 n = GD_rank(g)[i].v[j];
289 /* if n is the label of a flat edge, copy its position to
290 * the label.
291 */
292 if (ND_alg(n)) {
293 edge_t *fe = ND_alg(n);
294 assert(ED_label(fe));
295 ED_label(fe)->pos = ND_coord(n);
296 ED_label(fe)->set = true;
297 }
298 if (ND_node_type(n) != NORMAL && !sinfo.splineMerge(n))
299 continue;
300 for (int k = 0; (e = ND_out(n).list[k]); k++) {
301 if (ED_edge_type(e) == FLATORDER || ED_edge_type(e) == IGNORED)
302 continue;
304 LIST_APPEND(&edges, e);
305 }
306 if (ND_flat_out(n).list)
307 for (int k = 0; (e = ND_flat_out(n).list[k]); k++) {
308 setflags(e, FLATEDGE, 0, AUXGRAPH);
309 LIST_APPEND(&edges, e);
310 }
311 if (ND_other(n).list) {
312 /* In position, each node has its rw stored in mval and,
313 * if a node is part of a loop, rw may be increased to
314 * reflect the loops and associated labels. We restore
315 * the original value here.
316 */
317 if (ND_node_type(n) == NORMAL) {
318 SWAP(&ND_rw(n), &ND_mval(n));
319 }
320 for (int k = 0; (e = ND_other(n).list[k]); k++) {
321 setflags(e, 0, 0, AUXGRAPH);
322 LIST_APPEND(&edges, e);
323 }
324 }
325 }
326 }
327
328 /* Sort so that equivalent edges are contiguous.
329 * Equivalence should basically mean that 2 edges have the
330 * same set {(tailnode,tailport),(headnode,headport)}, or
331 * alternatively, the edges would be routed identically if
332 * routed separately.
333 */
334 LIST_SORT(&edges, edgecmp);
335
336 /* FIXME: just how many boxes can there be? */
337 P.boxes = gv_calloc(n_nodes + 20 * 2 * NSUB, sizeof(boxf));
338 sd.Rank_box = gv_calloc(i, sizeof(boxf));
339
340 if (et == EDGETYPE_LINE) {
341 /* place regular edge labels */
342 for (n = GD_nlist(g); n; n = ND_next(n)) {
343 if (ND_node_type(n) == VIRTUAL && ND_label(n)) {
344 place_vnlabel(n);
345 }
346 }
347 }
348
349 for (unsigned l = 0; l < LIST_SIZE(&edges);) {
350 const unsigned ind = l;
351 edge_t *le0 = getmainedge((e0 = LIST_GET(&edges, l++)));
352 if (ED_tail_port(e0).defined || ED_head_port(e0).defined) {
353 ea = e0;
354 } else {
355 ea = le0;
356 }
357 if (ED_tree_index(ea) & BWDEDGE) {
358 makefwdedge(&fwdedgea.out, ea);
359 ea = &fwdedgea.out;
360 }
361 unsigned cnt;
362 for (cnt = 1; l < LIST_SIZE(&edges); cnt++, l++) {
363 if (le0 != (le1 = getmainedge((e1 = LIST_GET(&edges, l)))))
364 break;
365 if (ED_adjacent(e0))
366 continue; /* all flat adjacent edges at once */
367 if (ED_tail_port(e1).defined || ED_head_port(e1).defined) {
368 eb = e1;
369 } else {
370 eb = le1;
371 }
372 if (ED_tree_index(eb) & BWDEDGE) {
373 makefwdedge(&fwdedgeb.out, eb);
374 eb = &fwdedgeb.out;
375 }
376 if (portcmp(ED_tail_port(ea), ED_tail_port(eb)))
377 break;
378 if (portcmp(ED_head_port(ea), ED_head_port(eb)))
379 break;
380 if ((ED_tree_index(e0) & EDGETYPEMASK) == FLATEDGE &&
381 ED_label(e0) != ED_label(e1))
382 break;
383 if (ED_tree_index(LIST_GET(&edges, l)) & MAINGRAPH) /* Aha! -C is on */
384 break;
385 }
386
387 if (et == EDGETYPE_CURVED) {
388 edge_t **edgelist = gv_calloc(cnt, sizeof(edge_t *));
389 edgelist[0] = getmainedge(LIST_GET(&edges, ind));
390 for (unsigned ii = 1; ii < cnt; ii++)
391 edgelist[ii] = LIST_GET(&edges, ind + ii);
393 free(edgelist);
394 } else if (agtail(e0) == aghead(e0)) {
395 double sizey;
396 n = agtail(e0);
397 const int r = ND_rank(n);
398 if (r == GD_maxrank(g) || GD_rank(g)[r + 1].v[0] == NULL) {
399 if (r > 0 && GD_rank(g)[r - 1].v[0] != NULL) {
400 sizey = ND_coord(GD_rank(g)[r - 1].v[0]).y - ND_coord(n).y;
401 } else {
402 sizey = ND_ht(n);
403 }
404 } else if (r == GD_minrank(g) || GD_rank(g)[r - 1].v[0] == NULL) {
405 sizey = ND_coord(n).y - ND_coord(GD_rank(g)[r + 1].v[0]).y;
406 } else {
407 double upy = ND_coord(GD_rank(g)[r - 1].v[0]).y - ND_coord(n).y;
408 double dwny = ND_coord(n).y - ND_coord(GD_rank(g)[r + 1].v[0]).y;
409 sizey = fmin(upy, dwny);
410 }
411 makeSelfEdge(LIST_AT(&edges, ind), cnt, sd.Multisep, sizey / 2, &sinfo);
412 for (unsigned b = 0; b < cnt; b++) {
413 e = LIST_GET(&edges, ind + b);
414 if (ED_label(e))
415 updateBB(g, ED_label(e));
416 }
417 } else if (ND_rank(agtail(e0)) == ND_rank(aghead(e0))) {
418 const int rc = make_flat_edge(g, sd, &P, LIST_AT(&edges, ind), cnt, et);
419 if (rc != 0) {
420 free(sd.Rank_box);
421 LIST_FREE(&edges);
422 free(P.boxes);
423 return rc;
424 }
425 } else
426 make_regular_edge(g, &sd, &P, LIST_AT(&edges, ind), cnt, et);
427 }
428
429 /* place regular edge labels */
430 for (n = GD_nlist(g); n; n = ND_next(n)) {
431 if (ND_node_type(n) == VIRTUAL && ND_label(n)) {
432 place_vnlabel(n);
433 updateBB(g, ND_label(n));
434 }
435 }
436
437 /* normalize splines so they always go from tail to head */
438 /* place_portlabel relies on this being done first */
439 if (normalize)
441
442#ifdef ORTHO
443finish:
444#endif
445 /* place port labels */
446 /* FIX: head and tail labels are not part of cluster bbox */
448 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
449 if (E_headlabel) {
450 for (e = agfstin(g, n); e; e = agnxtin(g, e))
451 if (ED_head_label(AGMKOUT(e))) {
452 place_portlabel(AGMKOUT(e), true);
454 }
455 }
456 if (E_taillabel) {
457 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
458 if (ED_tail_label(e)) {
459 if (place_portlabel(e, false))
460 updateBB(g, ED_tail_label(e));
461 }
462 }
463 }
464 }
465 }
466
467#ifdef ORTHO
468 if (et != EDGETYPE_ORTHO && et != EDGETYPE_CURVED) {
469#else
470 if (et != EDGETYPE_CURVED) {
471#endif
473 }
474 free(sd.Rank_box);
475 LIST_FREE(&edges);
476 free(P.boxes);
478 EdgeLabelsDone = 1;
479 return 0;
480}
481
482/* If the splines attribute is defined but equal to "", skip edge routing.
483 *
484 * @return 0 on success
485 */
486int dot_splines(graph_t *g) { return dot_splines_(g, 1); }
487
488/* assign position of an edge label from its virtual node
489 * This is for regular edges only.
490 */
491static void place_vnlabel(node_t *n) {
492 edge_t *e;
493 if (ND_in(n).size == 0)
494 return; /* skip flat edge labels here */
495 for (e = ND_out(n).list[0]; ED_edge_type(e) != NORMAL; e = ED_to_orig(e))
496 ;
497 const pointf dimen = ED_label(e)->dimen;
498 const double width = GD_flip(agraphof(n)) ? dimen.y : dimen.x;
499 ED_label(e)->pos.x = ND_coord(n).x + width / 2.0;
500 ED_label(e)->pos.y = ND_coord(n).y;
501 ED_label(e)->set = true;
502}
503
504static void setflags(edge_t *e, int hint1, int hint2, int f3) {
505 int f1, f2;
506 if (hint1 != 0)
507 f1 = hint1;
508 else {
509 if (agtail(e) == aghead(e))
510 if (ED_tail_port(e).defined || ED_head_port(e).defined)
511 f1 = SELFWPEDGE;
512 else
513 f1 = SELFNPEDGE;
514 else if (ND_rank(agtail(e)) == ND_rank(aghead(e)))
515 f1 = FLATEDGE;
516 else
517 f1 = REGULAREDGE;
518 }
519 if (hint2 != 0)
520 f2 = hint2;
521 else {
522 if (f1 == REGULAREDGE)
523 f2 = ND_rank(agtail(e)) < ND_rank(aghead(e)) ? FWDEDGE : BWDEDGE;
524 else if (f1 == FLATEDGE)
525 f2 = ND_order(agtail(e)) < ND_order(aghead(e)) ? FWDEDGE : BWDEDGE;
526 else /* f1 == SELF*EDGE */
527 f2 = FWDEDGE;
528 }
529 ED_tree_index(e) = f1 | f2 | f3;
530}
531
532/* lexicographically order edges by
533 * - edge type
534 * - |rank difference of nodes|
535 * - |x difference of nodes|
536 * - id of witness edge for equivalence class
537 * - port comparison
538 * - graph type
539 * - labels if flat edges
540 * - edge id
541 */
542static int edgecmp(const void *p0, const void *p1) {
543 edge_t *const *ptr0 = p0;
544 edge_t *const *ptr1 = p1;
545 Agedgeinfo_t fwdedgeai, fwdedgebi;
546 Agedgepair_t fwdedgea, fwdedgeb;
547 int rv;
548
549 fwdedgea.out.base.data = &fwdedgeai.hdr;
550 fwdedgeb.out.base.data = &fwdedgebi.hdr;
551 edge_t *const e0 = *ptr0;
552 edge_t *const e1 = *ptr1;
553 int et0 = ED_tree_index(e0) & EDGETYPEMASK;
554 int et1 = ED_tree_index(e1) & EDGETYPEMASK;
555 if (et0 < et1) {
556 return 1;
557 }
558 if (et0 > et1) {
559 return -1;
560 }
561
562 edge_t *le0 = getmainedge(e0);
563 edge_t *le1 = getmainedge(e1);
564
565 {
566 const int rank_diff0 = ND_rank(agtail(le0)) - ND_rank(aghead(le0));
567 const int rank_diff1 = ND_rank(agtail(le1)) - ND_rank(aghead(le1));
568 const int v0 = abs(rank_diff0);
569 const int v1 = abs(rank_diff1);
570 if (v0 < v1) {
571 return -1;
572 }
573 if (v0 > v1) {
574 return 1;
575 }
576 }
577
578 {
579 const double t0 = ND_coord(agtail(le0)).x - ND_coord(aghead(le0)).x;
580 const double t1 = ND_coord(agtail(le1)).x - ND_coord(aghead(le1)).x;
581 const double v0 = fabs(t0);
582 const double v1 = fabs(t1);
583 if (v0 < v1) {
584 return -1;
585 }
586 if (v0 > v1) {
587 return 1;
588 }
589 }
590
591 /* This provides a cheap test for edges having the same set of endpoints. */
592 if (AGSEQ(le0) < AGSEQ(le1)) {
593 return -1;
594 }
595 if (AGSEQ(le0) > AGSEQ(le1)) {
596 return 1;
597 }
598
599 edge_t *ea =
600 (ED_tail_port(e0).defined || ED_head_port(e0).defined) ? e0 : le0;
601 if (ED_tree_index(ea) & BWDEDGE) {
602 makefwdedge(&fwdedgea.out, ea);
603 ea = &fwdedgea.out;
604 }
605 edge_t *eb =
606 (ED_tail_port(e1).defined || ED_head_port(e1).defined) ? e1 : le1;
607 if (ED_tree_index(eb) & BWDEDGE) {
608 makefwdedge(&fwdedgeb.out, eb);
609 eb = &fwdedgeb.out;
610 }
611 if ((rv = portcmp(ED_tail_port(ea), ED_tail_port(eb))))
612 return rv;
613 if ((rv = portcmp(ED_head_port(ea), ED_head_port(eb))))
614 return rv;
615
616 et0 = ED_tree_index(e0) & GRAPHTYPEMASK;
617 et1 = ED_tree_index(e1) & GRAPHTYPEMASK;
618 if (et0 < et1) {
619 return -1;
620 }
621 if (et0 > et1) {
622 return 1;
623 }
624
625 if (et0 == FLATEDGE) {
626 if (ED_label(e0) < ED_label(e1)) {
627 return -1;
628 }
629 if (ED_label(e0) > ED_label(e1)) {
630 return 1;
631 }
632 }
633
634 if (AGSEQ(e0) < AGSEQ(e1)) {
635 return -1;
636 }
637 if (AGSEQ(e0) > AGSEQ(e1)) {
638 return 1;
639 }
640 return 0;
641}
642
687
688static void setState(graph_t *auxg, attr_state_t *attr_state) {
689 /* save state */
690 attr_state->E_constr = E_constr;
691 attr_state->E_dir = E_dir;
692 attr_state->E_samehead = E_samehead;
693 attr_state->E_sametail = E_sametail;
694 attr_state->E_weight = E_weight;
695 attr_state->E_minlen = E_minlen;
696 attr_state->E_fontcolor = E_fontcolor;
697 attr_state->E_fontname = E_fontname;
698 attr_state->E_fontsize = E_fontsize;
699 attr_state->E_headclip = E_headclip;
700 attr_state->E_headlabel = E_headlabel;
701 attr_state->E_label = E_label;
702 attr_state->E_label_float = E_label_float;
704 attr_state->E_labelfontname = E_labelfontname;
705 attr_state->E_labelfontsize = E_labelfontsize;
706 attr_state->E_tailclip = E_tailclip;
707 attr_state->E_taillabel = E_taillabel;
708 attr_state->E_xlabel = E_xlabel;
709 attr_state->N_height = N_height;
710 attr_state->N_width = N_width;
711 attr_state->N_shape = N_shape;
712 attr_state->N_style = N_style;
713 attr_state->N_fontsize = N_fontsize;
714 attr_state->N_fontname = N_fontname;
715 attr_state->N_fontcolor = N_fontcolor;
716 attr_state->N_label = N_label;
717 attr_state->N_xlabel = N_xlabel;
718 attr_state->N_showboxes = N_showboxes;
719 attr_state->N_ordering = N_ordering;
720 attr_state->N_sides = N_sides;
721 attr_state->N_peripheries = N_peripheries;
722 attr_state->N_skew = N_skew;
723 attr_state->N_orientation = N_orientation;
724 attr_state->N_distortion = N_distortion;
725 attr_state->N_fixed = N_fixed;
726 attr_state->N_nojustify = N_nojustify;
727 attr_state->N_group = N_group;
728 attr_state->State = State;
729 attr_state->G_ordering = G_ordering;
730
731 E_constr = NULL;
732 E_dir = agattr_text(auxg, AGEDGE, "dir", NULL);
733 E_samehead = agattr_text(auxg, AGEDGE, "samehead", NULL);
734 E_sametail = agattr_text(auxg, AGEDGE, "sametail", NULL);
735 E_weight = agattr_text(auxg, AGEDGE, "weight", NULL);
736 if (!E_weight)
737 E_weight = agattr_text(auxg, AGEDGE, "weight", "");
738 E_minlen = NULL;
740 E_fontname = agfindedgeattr(auxg, "fontname");
741 E_fontsize = agfindedgeattr(auxg, "fontsize");
742 E_headclip = agfindedgeattr(auxg, "headclip");
744 E_label = agfindedgeattr(auxg, "label");
745 E_label_float = agfindedgeattr(auxg, "label_float");
747 E_labelfontname = agfindedgeattr(auxg, "labelfontname");
748 E_labelfontsize = agfindedgeattr(auxg, "labelfontsize");
749 E_tailclip = agfindedgeattr(auxg, "tailclip");
751 E_xlabel = NULL;
752 N_height = agfindnodeattr(auxg, "height");
753 N_width = agfindnodeattr(auxg, "width");
754 N_shape = agfindnodeattr(auxg, "shape");
755 N_style = NULL;
756 N_fontsize = agfindnodeattr(auxg, "fontsize");
757 N_fontname = agfindnodeattr(auxg, "fontname");
759 N_label = agfindnodeattr(auxg, "label");
760 N_xlabel = NULL;
762 N_ordering = agfindnodeattr(auxg, "ordering");
763 N_sides = agfindnodeattr(auxg, "sides");
764 N_peripheries = agfindnodeattr(auxg, "peripheries");
765 N_skew = agfindnodeattr(auxg, "skew");
766 N_orientation = agfindnodeattr(auxg, "orientation");
767 N_distortion = agfindnodeattr(auxg, "distortion");
768 N_fixed = agfindnodeattr(auxg, "fixed");
770 N_group = NULL;
771 G_ordering = agfindgraphattr(auxg, "ordering");
772}
773
774/* Create clone graph. It stores the global Agsyms, to be
775 * restored in cleanupCloneGraph. The graph uses the main
776 * graph's settings for certain geometry parameters, and
777 * declares all node and edge attributes used in the original
778 * graph.
779 */
780static graph_t *cloneGraph(graph_t *g, attr_state_t *attr_state) {
781 Agsym_t *sym;
782 graph_t *auxg;
783 if (agisdirected(g))
784 auxg = agopen("auxg", Agdirected, NULL);
785 else
786 auxg = agopen("auxg", Agundirected, NULL);
787 agbindrec(auxg, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
788 agattr_text(auxg, AGRAPH, "rank", "");
789 GD_drawing(auxg) = gv_alloc(sizeof(layout_t));
790 GD_drawing(auxg)->quantum = GD_drawing(g)->quantum;
791 GD_drawing(auxg)->dpi = GD_drawing(g)->dpi;
792
793 GD_charset(auxg) = GD_charset(g);
794 if (GD_flip(g))
795 SET_RANKDIR(auxg, RANKDIR_TB);
796 else
797 SET_RANKDIR(auxg, RANKDIR_LR);
798 GD_nodesep(auxg) = GD_nodesep(g);
799 GD_ranksep(auxg) = GD_ranksep(g);
800
801 // copy node attrs to auxg
802 sym = agnxtattr(agroot(g), AGNODE, NULL); // get the first attr.
803 for (; sym; sym = agnxtattr(agroot(g), AGNODE, sym)) {
804 const bool is_html = aghtmlstr(sym->defval);
805 is_html ? agattr_html(auxg, AGNODE, sym->name, sym->defval)
806 : agattr_text(auxg, AGNODE, sym->name, sym->defval);
807 }
808
809 // copy edge attributes
810 sym = agnxtattr(agroot(g), AGEDGE, NULL); // get the first attr.
811 for (; sym; sym = agnxtattr(agroot(g), AGEDGE, sym)) {
812 const bool is_html = aghtmlstr(sym->defval);
813 is_html ? agattr_html(auxg, AGEDGE, sym->name, sym->defval)
814 : agattr_text(auxg, AGEDGE, sym->name, sym->defval);
815 }
816
817 if (!agattr_text(auxg, AGEDGE, "headport", NULL))
818 agattr_text(auxg, AGEDGE, "headport", "");
819 if (!agattr_text(auxg, AGEDGE, "tailport", NULL))
820 agattr_text(auxg, AGEDGE, "tailport", "");
821
822 setState(auxg, attr_state);
823
824 return auxg;
825}
826
827static void cleanupCloneGraph(graph_t *g, attr_state_t *attr_state) {
828 /* restore main graph syms */
829 E_constr = attr_state->E_constr;
830 E_dir = attr_state->E_dir;
831 E_samehead = attr_state->E_samehead;
832 E_sametail = attr_state->E_sametail;
833 E_weight = attr_state->E_weight;
834 E_minlen = attr_state->E_minlen;
835 E_fontcolor = attr_state->E_fontcolor;
836 E_fontname = attr_state->E_fontname;
837 E_fontsize = attr_state->E_fontsize;
838 E_headclip = attr_state->E_headclip;
839 E_headlabel = attr_state->E_headlabel;
840 E_label = attr_state->E_label;
841 E_label_float = attr_state->E_label_float;
843 E_labelfontname = attr_state->E_labelfontname;
844 E_labelfontsize = attr_state->E_labelfontsize;
845 E_tailclip = attr_state->E_tailclip;
846 E_taillabel = attr_state->E_taillabel;
847 E_xlabel = attr_state->E_xlabel;
848 N_height = attr_state->N_height;
849 N_width = attr_state->N_width;
850 N_shape = attr_state->N_shape;
851 N_style = attr_state->N_style;
852 N_fontsize = attr_state->N_fontsize;
853 N_fontname = attr_state->N_fontname;
854 N_fontcolor = attr_state->N_fontcolor;
855 N_label = attr_state->N_label;
856 N_xlabel = attr_state->N_xlabel;
857 N_showboxes = attr_state->N_showboxes;
858 N_ordering = attr_state->N_ordering;
859 N_sides = attr_state->N_sides;
860 N_peripheries = attr_state->N_peripheries;
861 N_skew = attr_state->N_skew;
862 N_orientation = attr_state->N_orientation;
863 N_distortion = attr_state->N_distortion;
864 N_fixed = attr_state->N_fixed;
865 N_nojustify = attr_state->N_nojustify;
866 N_group = attr_state->N_group;
867 G_ordering = attr_state->G_ordering;
868 State = attr_state->State;
869
870 dot_cleanup(g);
871 agclose(g);
872}
873
874/* If original graph has rankdir=LR or RL, records change shape,
875 * so we wrap a record node's label in "{...}" to prevent this.
876 */
877static node_t *cloneNode(graph_t *g, node_t *orign) {
878 node_t *n = agnode(g, agnameof(orign), 1);
879 agbindrec(n, "Agnodeinfo_t", sizeof(Agnodeinfo_t), true);
880 agcopyattr(orign, n);
881 if (shapeOf(orign) == SH_RECORD) {
882 agxbuf buf = {0};
883 agxbprint(&buf, "{%s}", ND_label(orign)->text);
884 agset(n, "label", agxbuse(&buf));
885 agxbfree(&buf);
886 }
887
888 return n;
889}
890
891static edge_t *cloneEdge(graph_t *g, node_t *tn, node_t *hn, edge_t *orig) {
892 edge_t *e = agedge(g, tn, hn, NULL, 1);
893 agbindrec(e, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true);
894 agcopyattr(orig, e);
895
896 return e;
897}
898
900static pointf transformf(pointf p, pointf del, int flip) {
901 if (flip) {
902 double i = p.x;
903 p.x = p.y;
904 p.y = -i;
905 }
906 return add_pointf(p, del);
907}
908
909/* lexicographically order edges by
910 * - has label
911 * - label is wider
912 * - label is higher
913 */
914static int edgelblcmpfn(const void *x, const void *y) {
915 edge_t *const *const ptr0 = x;
916 edge_t *const *const ptr1 = y;
917 pointf sz0, sz1;
918
919 edge_t *e0 = *ptr0;
920 edge_t *e1 = *ptr1;
921
922 if (ED_label(e0)) {
923 if (ED_label(e1)) {
924 sz0 = ED_label(e0)->dimen;
925 sz1 = ED_label(e1)->dimen;
926 if (sz0.x > sz1.x)
927 return -1;
928 if (sz0.x < sz1.x)
929 return 1;
930 if (sz0.y > sz1.y)
931 return -1;
932 if (sz0.y < sz1.y)
933 return 1;
934 return 0;
935 }
936 return -1;
937 }
938 if (ED_label(e1)) {
939 return 1;
940 }
941 return 0;
942}
943
944#define LBL_SPACE 6 /* space between labels, in points */
945
946/* This handles the second simplest case for flat edges between
947 * two adjacent nodes. We still invoke a dot on a rotated problem
948 * to handle edges with ports. This usually works, but fails for
949 * records because of their weird nature.
950 */
951static void makeSimpleFlatLabels(node_t *tn, node_t *hn, edge_t **edges,
952 unsigned cnt, int et, unsigned n_lbls) {
954 edge_t *e = *edges;
955 pointf points[10], tp, hp;
956 double leftend, rightend, ctrx, ctry, miny, maxy;
957 double uminx, umaxx;
958 double lminx = 0.0, lmaxx = 0.0;
959
960 edge_t **earray = gv_calloc(cnt, sizeof(edge_t *));
961
962 for (unsigned i = 0; i < cnt; i++) {
963 earray[i] = edges[i];
964 }
965
966 qsort(earray, cnt, sizeof(edge_t *), edgelblcmpfn);
967
968 tp = add_pointf(ND_coord(tn), ED_tail_port(e).p);
969 hp = add_pointf(ND_coord(hn), ED_head_port(e).p);
970
971 leftend = tp.x + ND_rw(tn);
972 rightend = hp.x - ND_lw(hn);
973 ctrx = (leftend + rightend) / 2.0;
974
975 /* do first edge */
976 e = earray[0];
977 size_t pointn = 0;
978 points[pointn++] = tp;
979 points[pointn++] = tp;
980 points[pointn++] = hp;
981 points[pointn++] = hp;
982 clip_and_install(e, aghead(e), points, pointn, &sinfo);
983 ED_label(e)->pos.x = ctrx;
984 ED_label(e)->pos.y = tp.y + (ED_label(e)->dimen.y + LBL_SPACE) / 2.0;
985 ED_label(e)->set = true;
986
987 miny = tp.y + LBL_SPACE / 2.0;
988 maxy = miny + ED_label(e)->dimen.y;
989 uminx = ctrx - ED_label(e)->dimen.x / 2.0;
990 umaxx = ctrx + ED_label(e)->dimen.x / 2.0;
991
992 unsigned i;
993 for (i = 1; i < n_lbls; i++) {
994 e = earray[i];
995 if (i % 2) { /* down */
996 if (i == 1) {
997 lminx = ctrx - ED_label(e)->dimen.x / 2.0;
998 lmaxx = ctrx + ED_label(e)->dimen.x / 2.0;
999 }
1000 miny -= LBL_SPACE + ED_label(e)->dimen.y;
1001 points[0] = tp;
1002 points[1] = (pointf){.x = tp.x, .y = miny - LBL_SPACE};
1003 points[2] = (pointf){.x = hp.x, .y = points[1].y};
1004 points[3] = hp;
1005 points[4] = (pointf){.x = lmaxx, .y = hp.y};
1006 points[5] = (pointf){.x = lmaxx, .y = miny};
1007 points[6] = (pointf){.x = lminx, .y = miny};
1008 points[7] = (pointf){.x = lminx, .y = tp.y};
1009 ctry = miny + ED_label(e)->dimen.y / 2.0;
1010 } else { /* up */
1011 points[0] = tp;
1012 points[1] = (pointf){.x = uminx, .y = tp.y};
1013 points[2] = (pointf){.x = uminx, .y = maxy};
1014 points[3] = (pointf){.x = umaxx, .y = maxy};
1015 points[4] = (pointf){.x = umaxx, .y = hp.y};
1016 points[5] = hp;
1017 points[6] = (pointf){.x = hp.x, .y = maxy + LBL_SPACE};
1018 points[7] = (pointf){.x = tp.x, .y = maxy + LBL_SPACE};
1019 ctry = maxy + ED_label(e)->dimen.y / 2.0 + LBL_SPACE;
1020 maxy += ED_label(e)->dimen.y + LBL_SPACE;
1021 }
1022 poly.pn = 8;
1023 poly.ps = (Ppoint_t *)points;
1024 size_t pn;
1025 pointf *ps = simpleSplineRoute(tp, hp, poly, &pn, et == EDGETYPE_PLINE);
1026 if (ps == NULL || pn == 0) {
1027 free(ps);
1028 free(earray);
1029 return;
1030 }
1031 ED_label(e)->pos.x = ctrx;
1032 ED_label(e)->pos.y = ctry;
1033 ED_label(e)->set = true;
1034 clip_and_install(e, aghead(e), ps, pn, &sinfo);
1035 free(ps);
1036 }
1037
1038 /* edges with no labels */
1039 for (; i < cnt; i++) {
1040 e = earray[i];
1041 if (i % 2) { /* down */
1042 if (i == 1) {
1043 lminx = (2 * leftend + rightend) / 3.0;
1044 lmaxx = (leftend + 2 * rightend) / 3.0;
1045 }
1046 miny -= LBL_SPACE;
1047 points[0] = tp;
1048 points[1] = (pointf){.x = tp.x, .y = miny - LBL_SPACE};
1049 points[2] = (pointf){.x = hp.x, .y = points[1].y};
1050 points[3] = hp;
1051 points[4] = (pointf){.x = lmaxx, .y = hp.y};
1052 points[5] = (pointf){.x = lmaxx, .y = miny};
1053 points[6] = (pointf){.x = lminx, .y = miny};
1054 points[7] = (pointf){.x = lminx, .y = tp.y};
1055 } else { /* up */
1056 points[0] = tp;
1057 points[1] = (pointf){.x = uminx, .y = tp.y};
1058 points[2] = (pointf){.x = uminx, .y = maxy};
1059 points[3] = (pointf){.x = umaxx, .y = maxy};
1060 points[4] = (pointf){.x = umaxx, .y = hp.y};
1061 points[5] = hp;
1062 points[6] = (pointf){.x = hp.x, .y = maxy + LBL_SPACE};
1063 points[7] = (pointf){.x = tp.x, .y = maxy + LBL_SPACE};
1064 maxy += +LBL_SPACE;
1065 }
1066 poly.pn = 8;
1067 poly.ps = (Ppoint_t *)points;
1068 size_t pn;
1069 pointf *ps = simpleSplineRoute(tp, hp, poly, &pn, et == EDGETYPE_PLINE);
1070 if (ps == NULL || pn == 0) {
1071 free(ps);
1072 free(earray);
1073 return;
1074 }
1075 clip_and_install(e, aghead(e), ps, pn, &sinfo);
1076 free(ps);
1077 }
1078
1079 free(earray);
1080}
1081
1082static void makeSimpleFlat(node_t *tn, node_t *hn, edge_t **edges, unsigned cnt,
1083 int et) {
1084 edge_t *e = *edges;
1085 pointf points[10], tp, hp;
1086 double stepy, dy;
1087
1088 tp = add_pointf(ND_coord(tn), ED_tail_port(e).p);
1089 hp = add_pointf(ND_coord(hn), ED_head_port(e).p);
1090
1091 stepy = cnt > 1 ? ND_ht(tn) / (double)(cnt - 1) : 0.;
1092 dy = tp.y - (cnt > 1 ? ND_ht(tn) / 2. : 0.);
1093
1094 for (unsigned i = 0; i < cnt; i++) {
1095 e = edges[i];
1096 size_t pointn = 0;
1097 if (et == EDGETYPE_SPLINE || et == EDGETYPE_LINE) {
1098 points[pointn++] = tp;
1099 points[pointn++] = (pointf){(2 * tp.x + hp.x) / 3, dy};
1100 points[pointn++] = (pointf){(2 * hp.x + tp.x) / 3, dy};
1101 points[pointn++] = hp;
1102 } else { /* EDGETYPE_PLINE */
1103 points[pointn++] = tp;
1104 points[pointn++] = tp;
1105 points[pointn++] = (pointf){(2 * tp.x + hp.x) / 3, dy};
1106 points[pointn++] = (pointf){(2 * tp.x + hp.x) / 3, dy};
1107 points[pointn++] = (pointf){(2 * tp.x + hp.x) / 3, dy};
1108 points[pointn++] = (pointf){(2 * hp.x + tp.x) / 3, dy};
1109 points[pointn++] = (pointf){(2 * hp.x + tp.x) / 3, dy};
1110 points[pointn++] = (pointf){(2 * hp.x + tp.x) / 3, dy};
1111 points[pointn++] = hp;
1112 points[pointn++] = hp;
1113 }
1114 dy += stepy;
1115 clip_and_install(e, aghead(e), points, pointn, &sinfo);
1116 }
1117}
1118
1119/* In the simple case, with no labels or ports, this creates a simple
1120 * spindle of splines.
1121 * If there are only labels, cobble something together.
1122 * Otherwise, we run dot recursively on the 2 nodes and the edges,
1123 * essentially using rankdir=LR, to get the needed spline info.
1124 * This is probably to cute and fragile, and should be rewritten in a
1125 * more straightforward and laborious fashion.
1126 *
1127 * @return 0 on success
1128 */
1129static int make_flat_adj_edges(graph_t *g, edge_t **edges, unsigned cnt,
1130 edge_t *e0, int et) {
1131 node_t *n;
1132 node_t *tn, *hn;
1133 edge_t *e;
1134 graph_t *auxg;
1135 graph_t *subg;
1136 node_t *auxt, *auxh;
1137 edge_t *auxe;
1138 double midx, midy, leftx, rightx;
1139 pointf del;
1140 edge_t *hvye = NULL;
1141 static atomic_flag warned;
1142
1143 tn = agtail(e0), hn = aghead(e0);
1144 if (shapeOf(tn) == SH_RECORD || shapeOf(hn) == SH_RECORD) {
1145 if (!atomic_flag_test_and_set(&warned)) {
1146 agwarningf("flat edge between adjacent nodes one of which has a record "
1147 "shape - replace records with HTML-like labels\n");
1148 agerr(AGPREV, " Edge %s %s %s\n", agnameof(tn),
1149 agisdirected(g) ? "->" : "--", agnameof(hn));
1150 }
1151 return 0;
1152 }
1153 unsigned labels = 0;
1154 bool ports = false;
1155 for (unsigned i = 0; i < cnt; i++) {
1156 e = edges[i];
1157 if (ED_label(e))
1158 labels++;
1159 if (ED_tail_port(e).defined || ED_head_port(e).defined)
1160 ports = true;
1161 }
1162
1163 if (!ports) {
1164 /* flat edges without ports and labels can go straight left to right */
1165 if (labels == 0) {
1166 makeSimpleFlat(tn, hn, edges, cnt, et);
1167 }
1168 /* flat edges without ports but with labels take more work */
1169 else {
1170 makeSimpleFlatLabels(tn, hn, edges, cnt, et, labels);
1171 }
1172 return 0;
1173 }
1174
1175 attr_state_t attrs = {0};
1176 auxg = cloneGraph(g, &attrs);
1177 subg = agsubg(auxg, "xxx", 1);
1178 agbindrec(subg, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
1179 agset(subg, "rank", "source");
1180 rightx = ND_coord(hn).x;
1181 leftx = ND_coord(tn).x;
1182 if (GD_flip(g)) {
1183 SWAP(&tn, &hn);
1184 }
1185 auxt = cloneNode(subg, tn);
1186 auxh = cloneNode(auxg, hn);
1187 for (unsigned i = 0; i < cnt; i++) {
1188 e = edges[i];
1189 for (; ED_edge_type(e) != NORMAL; e = ED_to_orig(e))
1190 ;
1191 if (agtail(e) == tn)
1192 auxe = cloneEdge(auxg, auxt, auxh, e);
1193 else
1194 auxe = cloneEdge(auxg, auxh, auxt, e);
1195 ED_alg(e) = auxe;
1196 if (!hvye && !ED_tail_port(e).defined && !ED_head_port(e).defined) {
1197 hvye = auxe;
1198 ED_alg(hvye) = e;
1199 }
1200 }
1201 if (!hvye) {
1202 hvye = agedge(auxg, auxt, auxh, NULL, 1);
1203 }
1204 agxset(hvye, E_weight, "10000");
1205 GD_gvc(auxg) = GD_gvc(g);
1206 GD_dotroot(auxg) = auxg;
1207 setEdgeType(auxg, et);
1208 dot_init_node_edge(auxg);
1209
1210 dot_rank(auxg);
1211 const int r = dot_mincross(auxg);
1212 if (r != 0) {
1213 return r;
1214 }
1215 {
1216 const int rc = dot_position(auxg);
1217 if (rc != 0) {
1218 return rc;
1219 }
1220 }
1221
1222 /* reposition */
1223 midx = (ND_coord(tn).x - ND_rw(tn) + ND_coord(hn).x + ND_lw(hn)) / 2;
1224 midy = (ND_coord(auxt).x + ND_coord(auxh).x) / 2;
1225 for (n = GD_nlist(auxg); n; n = ND_next(n)) {
1226 if (n == auxt) {
1227 ND_coord(n).y = rightx;
1228 ND_coord(n).x = midy;
1229 } else if (n == auxh) {
1230 ND_coord(n).y = leftx;
1231 ND_coord(n).x = midy;
1232 } else
1233 ND_coord(n).y = midx;
1234 }
1235 dot_sameports(auxg);
1236 const int rc = dot_splines_(auxg, 0);
1237 if (rc != 0) {
1238 return rc;
1239 }
1241
1242 /* copy splines */
1243 if (GD_flip(g)) {
1244 del.x = ND_coord(tn).x - ND_coord(auxt).y;
1245 del.y = ND_coord(tn).y + ND_coord(auxt).x;
1246 } else {
1247 del.x = ND_coord(tn).x - ND_coord(auxt).x;
1248 del.y = ND_coord(tn).y - ND_coord(auxt).y;
1249 }
1250 for (unsigned i = 0; i < cnt; i++) {
1251 bezier *auxbz;
1252 bezier *bz;
1253
1254 e = edges[i];
1255 for (; ED_edge_type(e) != NORMAL; e = ED_to_orig(e))
1256 ;
1257 auxe = ED_alg(e);
1258 if ((auxe == hvye) & !ED_alg(auxe))
1259 continue; /* pseudo-edge */
1260 auxbz = ED_spl(auxe)->list;
1261 bz = new_spline(e, auxbz->size);
1262 bz->sflag = auxbz->sflag;
1263 bz->sp = transformf(auxbz->sp, del, GD_flip(g));
1264 bz->eflag = auxbz->eflag;
1265 bz->ep = transformf(auxbz->ep, del, GD_flip(g));
1266 for (size_t j = 0; j < auxbz->size;) {
1267 pointf cp[4];
1268 cp[0] = bz->list[j] = transformf(auxbz->list[j], del, GD_flip(g));
1269 j++;
1270 if (j >= auxbz->size)
1271 break;
1272 cp[1] = bz->list[j] = transformf(auxbz->list[j], del, GD_flip(g));
1273 j++;
1274 cp[2] = bz->list[j] = transformf(auxbz->list[j], del, GD_flip(g));
1275 j++;
1276 cp[3] = transformf(auxbz->list[j], del, GD_flip(g));
1277 update_bb_bz(&GD_bb(g), cp);
1278 }
1279 if (ED_label(e)) {
1280 ED_label(e)->pos = transformf(ED_label(auxe)->pos, del, GD_flip(g));
1281 ED_label(e)->set = true;
1282 updateBB(g, ED_label(e));
1283 }
1284 }
1285
1286 cleanupCloneGraph(auxg, &attrs);
1287 return 0;
1288}
1289
1290static void makeFlatEnd(graph_t *g, const spline_info_t sp, path *P, node_t *n,
1291 edge_t *e, pathend_t *endp, bool isBegin) {
1292 boxf b = endp->nb = maximal_bbox(g, sp, n, NULL, e);
1293 endp->sidemask = TOP;
1294 if (isBegin)
1295 beginpath(P, e, /* is_flat_edge = */ true, endp, false);
1296 else
1297 endpath(P, e, /* is_flat_edge = */ true, endp, false);
1298 b.UR.y = endp->boxes[endp->boxn - 1].UR.y;
1299 b.LL.y = endp->boxes[endp->boxn - 1].LL.y;
1300 b = makeregularend(b, TOP, ND_coord(n).y + GD_rank(g)[ND_rank(n)].ht2);
1301 if (b.LL.x < b.UR.x && b.LL.y < b.UR.y)
1302 endp->boxes[endp->boxn++] = b;
1303}
1304
1305static void makeBottomFlatEnd(graph_t *g, const spline_info_t sp, path *P,
1306 node_t *n, edge_t *e, pathend_t *endp,
1307 bool isBegin) {
1308 boxf b = endp->nb = maximal_bbox(g, sp, n, NULL, e);
1309 endp->sidemask = BOTTOM;
1310 if (isBegin)
1311 beginpath(P, e, /* is_flat_edge = */ true, endp, false);
1312 else
1313 endpath(P, e, /* is_flat_edge = */ true, endp, false);
1314 b.UR.y = endp->boxes[endp->boxn - 1].UR.y;
1315 b.LL.y = endp->boxes[endp->boxn - 1].LL.y;
1316 b = makeregularend(b, BOTTOM, ND_coord(n).y - GD_rank(g)[ND_rank(n)].ht2);
1317 if (b.LL.x < b.UR.x && b.LL.y < b.UR.y)
1318 endp->boxes[endp->boxn++] = b;
1319}
1320
1322 edge_t *e, int et) {
1323 node_t *tn, *hn, *ln;
1324 pointf *ps;
1325 bool ps_needs_free = false;
1326 pathend_t tend, hend;
1327 boxf lb;
1328 int i;
1329 edge_t *f;
1330 pointf points[7];
1331
1332 tn = agtail(e);
1333 hn = aghead(e);
1334
1335 for (f = ED_to_virt(e); ED_to_virt(f); f = ED_to_virt(f))
1336 ;
1337 ln = agtail(f);
1338 ED_label(e)->pos = ND_coord(ln);
1339 ED_label(e)->set = true;
1340
1341 size_t pn;
1342 if (et == EDGETYPE_LINE) {
1343 pointf startp, endp, lp;
1344
1345 startp = add_pointf(ND_coord(tn), ED_tail_port(e).p);
1346 endp = add_pointf(ND_coord(hn), ED_head_port(e).p);
1347
1348 lp = ED_label(e)->pos;
1349 lp.y -= ED_label(e)->dimen.y / 2.0;
1350 points[1] = points[0] = startp;
1351 points[2] = points[3] = points[4] = lp;
1352 points[5] = points[6] = endp;
1353 ps = points;
1354 pn = 7;
1355 } else {
1356 lb.LL.x = ND_coord(ln).x - ND_lw(ln);
1357 lb.UR.x = ND_coord(ln).x + ND_rw(ln);
1358 lb.UR.y = ND_coord(ln).y + ND_ht(ln) / 2;
1359 double ydelta = ND_coord(ln).y - GD_rank(g)[ND_rank(tn)].ht1 -
1360 ND_coord(tn).y + GD_rank(g)[ND_rank(tn)].ht2;
1361 ydelta /= 6;
1362 lb.LL.y = lb.UR.y - MAX(5, ydelta);
1363
1364 makeFlatEnd(g, sp, P, tn, e, &tend, true);
1365 makeFlatEnd(g, sp, P, hn, e, &hend, false);
1366
1367 boxf boxes[] = {
1368 {
1369 .LL =
1370 {
1371 .x = tend.boxes[tend.boxn - 1].LL.x,
1372 .y = tend.boxes[tend.boxn - 1].UR.y,
1373 },
1374 .UR = lb.LL,
1375 },
1376 {
1377 .LL =
1378 {
1379 .x = tend.boxes[tend.boxn - 1].LL.x,
1380 .y = lb.LL.y,
1381 },
1382 .UR =
1383 {
1384 .x = hend.boxes[hend.boxn - 1].UR.x,
1385 .y = lb.UR.y,
1386 },
1387 },
1388 {
1389 .LL =
1390 {
1391 .x = lb.UR.x,
1392 .y = hend.boxes[hend.boxn - 1].UR.y,
1393 },
1394 .UR =
1395 {
1396 .x = hend.boxes[hend.boxn - 1].UR.x,
1397 .y = lb.LL.y,
1398 },
1399 },
1400 };
1401 const size_t boxn = sizeof(boxes) / sizeof(boxes[0]);
1402
1403 for (i = 0; i < tend.boxn; i++)
1404 add_box(P, tend.boxes[i]);
1405 for (size_t j = 0; j < boxn; j++)
1406 add_box(P, boxes[j]);
1407 for (i = hend.boxn - 1; i >= 0; i--)
1408 add_box(P, hend.boxes[i]);
1409
1410 ps_needs_free = true;
1411 if (et == EDGETYPE_SPLINE)
1412 ps = routesplines(P, &pn);
1413 else
1414 ps = routepolylines(P, &pn);
1415 if (pn == 0) {
1416 free(ps);
1417 return;
1418 }
1419 }
1420 clip_and_install(e, aghead(e), ps, pn, &sinfo);
1421 if (ps_needs_free)
1422 free(ps);
1423}
1424
1426 edge_t **edges, unsigned cnt, edge_t *e,
1427 bool use_splines) {
1428 node_t *tn, *hn;
1429 int j, r;
1430 double stepx, stepy, vspace;
1431 rank_t *nextr;
1432 pathend_t tend, hend;
1433
1434 tn = agtail(e);
1435 hn = aghead(e);
1436 r = ND_rank(tn);
1437 if (r < GD_maxrank(g)) {
1438 nextr = GD_rank(g) + (r + 1);
1439 vspace = ND_coord(tn).y - GD_rank(g)[r].pht1 -
1440 (ND_coord(nextr->v[0]).y + nextr->pht2);
1441 } else {
1442 vspace = GD_ranksep(g);
1443 }
1444 stepx = sp.Multisep / (cnt + 1);
1445 stepy = vspace / (cnt + 1);
1446
1447 makeBottomFlatEnd(g, sp, P, tn, e, &tend, true);
1448 makeBottomFlatEnd(g, sp, P, hn, e, &hend, false);
1449
1450 for (unsigned i = 0; i < cnt; i++) {
1451 boxf b;
1452 e = edges[i];
1453 size_t boxn = 0;
1454
1455 boxf boxes[3];
1456
1457 b = tend.boxes[tend.boxn - 1];
1458 boxes[boxn].LL.x = b.LL.x;
1459 boxes[boxn].UR.y = b.LL.y;
1460 boxes[boxn].UR.x = b.UR.x + (i + 1) * stepx;
1461 boxes[boxn].LL.y = b.LL.y - (i + 1) * stepy;
1462 boxn++;
1463 boxes[boxn].LL.x = tend.boxes[tend.boxn - 1].LL.x;
1464 boxes[boxn].UR.y = boxes[boxn - 1].LL.y;
1465 boxes[boxn].UR.x = hend.boxes[hend.boxn - 1].UR.x;
1466 boxes[boxn].LL.y = boxes[boxn].UR.y - stepy;
1467 boxn++;
1468 b = hend.boxes[hend.boxn - 1];
1469 boxes[boxn].UR.x = b.UR.x;
1470 boxes[boxn].UR.y = b.LL.y;
1471 boxes[boxn].LL.x = b.LL.x - (i + 1) * stepx;
1472 boxes[boxn].LL.y = boxes[boxn - 1].UR.y;
1473 boxn++;
1474 assert(boxn == sizeof(boxes) / sizeof(boxes[0]));
1475
1476 for (j = 0; j < tend.boxn; j++)
1477 add_box(P, tend.boxes[j]);
1478 for (size_t k = 0; k < boxn; k++)
1479 add_box(P, boxes[k]);
1480 for (j = hend.boxn - 1; j >= 0; j--)
1481 add_box(P, hend.boxes[j]);
1482
1483 pointf *ps = NULL;
1484 size_t pn = 0;
1485 if (use_splines)
1486 ps = routesplines(P, &pn);
1487 else
1488 ps = routepolylines(P, &pn);
1489 if (pn == 0) {
1490 free(ps);
1491 return;
1492 }
1493 clip_and_install(e, aghead(e), ps, pn, &sinfo);
1494 free(ps);
1495 P->nbox = 0;
1496 }
1497}
1498
1499/* Construct flat edges edges[ind...ind+cnt-1]
1500 * There are 4 main cases:
1501 * - all edges between a and b where a and b are adjacent
1502 * - one labeled edge
1503 * - all non-labeled edges with identical ports between non-adjacent a and b
1504 * = connecting bottom to bottom/left/right - route along bottom
1505 * = the rest - route along top
1506 *
1507 * @return 0 on success
1508 */
1509static int make_flat_edge(graph_t *g, const spline_info_t sp, path *P,
1510 edge_t **edges, unsigned cnt, int et) {
1511 Agedgeinfo_t fwdedgei;
1512 Agedgepair_t fwdedge;
1513 int j;
1514 double stepx, stepy, vspace;
1515 pathend_t tend, hend;
1516
1517 fwdedge.out.base.data = &fwdedgei.hdr;
1518
1519 /* Get sample edge; normalize to go from left to right */
1520 edge_t *e = *edges;
1521 bool isAdjacent = ED_adjacent(e) != 0;
1522 if (ED_tree_index(e) & BWDEDGE) {
1523 makefwdedge(&fwdedge.out, e);
1524 e = &fwdedge.out;
1525 }
1526 for (unsigned i = 1; i < cnt; i++) {
1527 if (ED_adjacent(edges[i])) {
1528 isAdjacent = true;
1529 break;
1530 }
1531 }
1532 // The lead edge edges[0] might not have been marked earlier as adjacent, so
1533 // check them all.
1534 if (isAdjacent) {
1535 return make_flat_adj_edges(g, edges, cnt, e, et);
1536 }
1537 if (ED_label(e)) { /* edges with labels aren't multi-edges */
1538 make_flat_labeled_edge(g, sp, P, e, et);
1539 return 0;
1540 }
1541
1542 if (et == EDGETYPE_LINE) {
1543 makeSimpleFlat(agtail(e), aghead(e), edges, cnt, et);
1544 return 0;
1545 }
1546
1547 const int tside = ED_tail_port(e).side;
1548 const int hside = ED_head_port(e).side;
1549 if ((tside == BOTTOM && hside != TOP) || (hside == BOTTOM && tside != TOP)) {
1550 make_flat_bottom_edges(g, sp, P, edges, cnt, e, et == EDGETYPE_SPLINE);
1551 return 0;
1552 }
1553
1554 node_t *tn = agtail(e);
1555 node_t *hn = aghead(e);
1556 const int r = ND_rank(tn);
1557 if (r > 0) {
1558 rank_t *prevr;
1559 if (GD_has_labels(g->root) & EDGE_LABEL)
1560 prevr = GD_rank(g) + (r - 2);
1561 else
1562 prevr = GD_rank(g) + (r - 1);
1563 vspace = ND_coord(prevr->v[0]).y - prevr->ht1 - ND_coord(tn).y -
1564 GD_rank(g)[r].ht2;
1565 } else {
1566 vspace = GD_ranksep(g);
1567 }
1568 stepx = sp.Multisep / (cnt + 1);
1569 stepy = vspace / (cnt + 1);
1570
1571 makeFlatEnd(g, sp, P, tn, e, &tend, true);
1572 makeFlatEnd(g, sp, P, hn, e, &hend, false);
1573
1574 for (unsigned i = 0; i < cnt; i++) {
1575 boxf b;
1576 e = edges[i];
1577 size_t boxn = 0;
1578
1579 boxf boxes[3];
1580
1581 b = tend.boxes[tend.boxn - 1];
1582 boxes[boxn].LL.x = b.LL.x;
1583 boxes[boxn].LL.y = b.UR.y;
1584 boxes[boxn].UR.x = b.UR.x + (i + 1) * stepx;
1585 boxes[boxn].UR.y = b.UR.y + (i + 1) * stepy;
1586 boxn++;
1587 boxes[boxn].LL.x = tend.boxes[tend.boxn - 1].LL.x;
1588 boxes[boxn].LL.y = boxes[boxn - 1].UR.y;
1589 boxes[boxn].UR.x = hend.boxes[hend.boxn - 1].UR.x;
1590 boxes[boxn].UR.y = boxes[boxn].LL.y + stepy;
1591 boxn++;
1592 b = hend.boxes[hend.boxn - 1];
1593 boxes[boxn].UR.x = b.UR.x;
1594 boxes[boxn].LL.y = b.UR.y;
1595 boxes[boxn].LL.x = b.LL.x - (i + 1) * stepx;
1596 boxes[boxn].UR.y = boxes[boxn - 1].LL.y;
1597 boxn++;
1598 assert(boxn == sizeof(boxes) / sizeof(boxes[0]));
1599
1600 for (j = 0; j < tend.boxn; j++)
1601 add_box(P, tend.boxes[j]);
1602 for (size_t k = 0; k < boxn; k++)
1603 add_box(P, boxes[k]);
1604 for (j = hend.boxn - 1; j >= 0; j--)
1605 add_box(P, hend.boxes[j]);
1606
1607 pointf *ps = NULL;
1608 size_t pn = 0;
1609 if (et == EDGETYPE_SPLINE)
1610 ps = routesplines(P, &pn);
1611 else
1612 ps = routepolylines(P, &pn);
1613 if (pn == 0) {
1614 free(ps);
1615 return 0;
1616 }
1617 clip_and_install(e, aghead(e), ps, pn, &sinfo);
1618 free(ps);
1619 P->nbox = 0;
1620 }
1621 return 0;
1622}
1623
1625static bool leftOf(pointf p1, pointf p2, pointf p3) {
1626 return (p1.y - p2.y) * (p3.x - p2.x) - (p3.y - p2.y) * (p1.x - p2.x) > 0;
1627}
1628
1629/* Create an edge as line segment. We guarantee that the points
1630 * are always drawn downwards. This means that for flipped edges,
1631 * we draw from the head to the tail. The routine returns the
1632 * end node of the edge in *hp. The points are stored in the
1633 * given array of points, and the number of points is returned.
1634 *
1635 * If the edge has a label, the edge is draw as two segments, with
1636 * the bend near the label.
1637 *
1638 * If the endpoints are on adjacent ranks, revert to usual code by
1639 * returning 0.
1640 * This is done because the usual code handles the interaction of
1641 * multiple edges better.
1642 */
1643static int makeLineEdge(graph_t *g, edge_t *fe, points_t *points, node_t **hp) {
1644 int delr, pn;
1645 node_t *hn;
1646 node_t *tn;
1647 edge_t *e = fe;
1648 pointf startp, endp, lp;
1649 pointf dimen;
1650 double width, height;
1651
1652 while (ED_edge_type(e) != NORMAL)
1653 e = ED_to_orig(e);
1654 hn = aghead(e);
1655 tn = agtail(e);
1656 delr = abs(ND_rank(hn) - ND_rank(tn));
1657 if (delr == 1 || (delr == 2 && (GD_has_labels(g->root) & EDGE_LABEL)))
1658 return 0;
1659 if (agtail(fe) == agtail(e)) {
1660 *hp = hn;
1661 startp = add_pointf(ND_coord(tn), ED_tail_port(e).p);
1662 endp = add_pointf(ND_coord(hn), ED_head_port(e).p);
1663 } else {
1664 *hp = tn;
1665 startp = add_pointf(ND_coord(hn), ED_head_port(e).p);
1666 endp = add_pointf(ND_coord(tn), ED_tail_port(e).p);
1667 }
1668
1669 if (ED_label(e)) {
1670 dimen = ED_label(e)->dimen;
1671 if (GD_flip(agraphof(hn))) {
1672 width = dimen.y;
1673 height = dimen.x;
1674 } else {
1675 width = dimen.x;
1676 height = dimen.y;
1677 }
1678
1679 lp = ED_label(e)->pos;
1680 if (leftOf(endp, startp, lp)) {
1681 lp.x += width / 2.0;
1682 lp.y -= height / 2.0;
1683 } else {
1684 lp.x -= width / 2.0;
1685 lp.y += height / 2.0;
1686 }
1687
1688 LIST_APPEND(points, startp);
1689 LIST_APPEND(points, startp);
1690 LIST_APPEND(points, lp);
1691 LIST_APPEND(points, lp);
1692 LIST_APPEND(points, lp);
1693 LIST_APPEND(points, endp);
1694 LIST_APPEND(points, endp);
1695 pn = 7;
1696 } else {
1697 LIST_APPEND(points, startp);
1698 LIST_APPEND(points, startp);
1699 LIST_APPEND(points, endp);
1700 LIST_APPEND(points, endp);
1701 pn = 4;
1702 }
1703
1704 return pn;
1705}
1706
1708 edge_t **edges, unsigned cnt, int et) {
1709 node_t *tn, *hn;
1710 Agedgeinfo_t fwdedgeai, fwdedgebi, fwdedgei;
1711 Agedgepair_t fwdedgea, fwdedgeb, fwdedge;
1712 edge_t *e, *fe, *le, *segfirst;
1713 pathend_t tend, hend;
1714 boxf b;
1715 int sl;
1716 points_t pointfs = {0};
1717 points_t pointfs2 = {0};
1718
1719 fwdedgea.out.base.data = &fwdedgeai.hdr;
1720 fwdedgeb.out.base.data = &fwdedgebi.hdr;
1721 fwdedge.out.base.data = &fwdedgei.hdr;
1722
1723 sl = 0;
1724 e = *edges;
1725 bool hackflag = false;
1726 if (abs(ND_rank(agtail(e)) - ND_rank(aghead(e))) > 1) {
1727 fwdedgeai = *(Agedgeinfo_t *)((uintptr_t)e->base.data -
1728 offsetof(Agedgeinfo_t, hdr));
1729 fwdedgea.out = *e;
1730 fwdedgea.in = *AGOUT2IN(e);
1731 fwdedgea.out.base.data = &fwdedgeai.hdr;
1732 if (ED_tree_index(e) & BWDEDGE) {
1733 makefwdedge(&fwdedgeb.out, e);
1734 agtail(&fwdedgea.out) = aghead(e);
1735 ED_tail_port(&fwdedgea.out) = ED_head_port(e);
1736 } else {
1737 fwdedgebi = *(Agedgeinfo_t *)((uintptr_t)e->base.data -
1738 offsetof(Agedgeinfo_t, hdr));
1739 fwdedgeb.out = *e;
1740 fwdedgeb.out.base.data = &fwdedgebi.hdr;
1741 agtail(&fwdedgea.out) = agtail(e);
1742 fwdedgeb.in = *AGOUT2IN(e);
1743 }
1744 le = getmainedge(e);
1745 while (ED_to_virt(le))
1746 le = ED_to_virt(le);
1747 aghead(&fwdedgea.out) = aghead(le);
1748 ED_head_port(&fwdedgea.out).defined = false;
1749 ED_edge_type(&fwdedgea.out) = VIRTUAL;
1750 ED_head_port(&fwdedgea.out).p.x = ED_head_port(&fwdedgea.out).p.y = 0;
1751 ED_to_orig(&fwdedgea.out) = e;
1752 e = &fwdedgea.out;
1753 hackflag = true;
1754 } else {
1755 if (ED_tree_index(e) & BWDEDGE) {
1756 makefwdedge(&fwdedgea.out, e);
1757 e = &fwdedgea.out;
1758 }
1759 }
1760 fe = e;
1761
1762 /* compute the spline points for the edge */
1763
1764 if (et == EDGETYPE_LINE && makeLineEdge(g, fe, &pointfs, &hn)) {
1765 } else {
1766 bool is_spline = et == EDGETYPE_SPLINE;
1767 boxes_t boxes = {0};
1768 segfirst = e;
1769 tn = agtail(e);
1770 hn = aghead(e);
1771 b = tend.nb = maximal_bbox(g, *sp, tn, NULL, e);
1772 beginpath(P, e, /* is_flat_edge = */ false, &tend, spline_merge(tn));
1773 b.UR.y = tend.boxes[tend.boxn - 1].UR.y;
1774 b.LL.y = tend.boxes[tend.boxn - 1].LL.y;
1775 b = makeregularend(b, BOTTOM, ND_coord(tn).y - GD_rank(g)[ND_rank(tn)].ht1);
1776 if (b.LL.x < b.UR.x && b.LL.y < b.UR.y)
1777 tend.boxes[tend.boxn++] = b;
1778 bool smode = false;
1779 bool si = false;
1780 while (ND_node_type(hn) == VIRTUAL && !sinfo.splineMerge(hn)) {
1781 LIST_APPEND(&boxes, rank_box(sp, g, ND_rank(tn)));
1782 if (!smode && ((sl = straight_len(hn)) >=
1783 ((GD_has_labels(g->root) & EDGE_LABEL) ? 4 + 1 : 2 + 1))) {
1784 smode = true;
1785 si = true;
1786 sl -= 2;
1787 }
1788 if ((!smode || si) && ND_out(hn).size > 0) {
1789 si = false;
1790 LIST_APPEND(&boxes, maximal_bbox(g, *sp, hn, e, ND_out(hn).list[0]));
1791 e = ND_out(hn).list[0];
1792 tn = agtail(e);
1793 hn = aghead(e);
1794 continue;
1795 }
1796 hend.nb = maximal_bbox(g, *sp, hn, e, ND_out(hn).list[0]);
1797 endpath(P, e, /* is_flat_edge = */ false, &hend, spline_merge(aghead(e)));
1798 b = makeregularend(hend.boxes[hend.boxn - 1], TOP,
1799 ND_coord(hn).y + GD_rank(g)[ND_rank(hn)].ht2);
1800 if (b.LL.x < b.UR.x && b.LL.y < b.UR.y)
1801 hend.boxes[hend.boxn++] = b;
1802 P->end.theta = M_PI / 2, P->end.constrained = true;
1803 completeregularpath(P, segfirst, e, &tend, &hend, &boxes);
1804 pointf *ps = NULL;
1805 size_t pn = 0;
1806 if (is_spline)
1807 ps = routesplines(P, &pn);
1808 else {
1809 ps = routepolylines(P, &pn);
1810 if (et == EDGETYPE_LINE && pn > 4) {
1811 ps[1] = ps[0];
1812 ps[3] = ps[2] = ps[pn - 1];
1813 pn = 4;
1814 }
1815 }
1816 if (pn == 0) {
1817 free(ps);
1818 LIST_FREE(&boxes);
1819 LIST_FREE(&pointfs);
1820 LIST_FREE(&pointfs2);
1821 return;
1822 }
1823
1824 for (size_t i = 0; i < pn; i++) {
1825 LIST_APPEND(&pointfs, ps[i]);
1826 }
1827 free(ps);
1828 e = straight_path(ND_out(hn).list[0], sl, &pointfs);
1829 recover_slack(segfirst, P);
1830 if (e == NULL) {
1831 LIST_FREE(&boxes);
1832 LIST_FREE(&pointfs);
1833 LIST_FREE(&pointfs2);
1834 return;
1835 }
1836 segfirst = e;
1837 tn = agtail(e);
1838 hn = aghead(e);
1839 LIST_CLEAR(&boxes);
1840 tend.nb = maximal_bbox(g, *sp, tn, ND_in(tn).list[0], e);
1841 beginpath(P, e, /* is_flat_edge = */ false, &tend, spline_merge(tn));
1842 b = makeregularend(tend.boxes[tend.boxn - 1], BOTTOM,
1843 ND_coord(tn).y - GD_rank(g)[ND_rank(tn)].ht1);
1844 if (b.LL.x < b.UR.x && b.LL.y < b.UR.y)
1845 tend.boxes[tend.boxn++] = b;
1846 P->start.theta = -M_PI / 2, P->start.constrained = true;
1847 smode = false;
1848 }
1849 LIST_APPEND(&boxes, rank_box(sp, g, ND_rank(tn)));
1850 b = hend.nb = maximal_bbox(g, *sp, hn, e, NULL);
1851 endpath(P, hackflag ? &fwdedgeb.out : e, /* is_flat_edge = */ false, &hend,
1852 spline_merge(aghead(e)));
1853 b.UR.y = hend.boxes[hend.boxn - 1].UR.y;
1854 b.LL.y = hend.boxes[hend.boxn - 1].LL.y;
1855 b = makeregularend(b, TOP, ND_coord(hn).y + GD_rank(g)[ND_rank(hn)].ht2);
1856 if (b.LL.x < b.UR.x && b.LL.y < b.UR.y)
1857 hend.boxes[hend.boxn++] = b;
1858 completeregularpath(P, segfirst, e, &tend, &hend, &boxes);
1859 LIST_FREE(&boxes);
1860 pointf *ps = NULL;
1861 size_t pn = 0;
1862 if (is_spline)
1863 ps = routesplines(P, &pn);
1864 else
1865 ps = routepolylines(P, &pn);
1866 if (et == EDGETYPE_LINE && pn > 4) {
1867 /* Here we have used the polyline case to handle
1868 * an edge between two nodes on adjacent ranks. If the
1869 * results really is a polyline, straighten it.
1870 */
1871 ps[1] = ps[0];
1872 ps[3] = ps[2] = ps[pn - 1];
1873 pn = 4;
1874 }
1875 if (pn == 0) {
1876 free(ps);
1877 LIST_FREE(&pointfs);
1878 LIST_FREE(&pointfs2);
1879 return;
1880 }
1881 for (size_t i = 0; i < pn; i++) {
1882 LIST_APPEND(&pointfs, ps[i]);
1883 }
1884 free(ps);
1885 recover_slack(segfirst, P);
1886 hn = hackflag ? aghead(&fwdedgeb.out) : aghead(e);
1887 }
1888
1889 /* make copies of the spline points, one per multi-edge */
1890
1891 if (cnt == 1) {
1892 LIST_SYNC(&pointfs);
1893 clip_and_install(fe, hn, LIST_FRONT(&pointfs), LIST_SIZE(&pointfs), &sinfo);
1894 LIST_FREE(&pointfs);
1895 LIST_FREE(&pointfs2);
1896 return;
1897 }
1898 const double dx = sp->Multisep * (cnt - 1) / 2;
1899 for (size_t k = 1; k + 1 < LIST_SIZE(&pointfs); k++)
1900 LIST_AT(&pointfs, k)->x -= dx;
1901
1902 for (size_t k = 0; k < LIST_SIZE(&pointfs); k++)
1903 LIST_APPEND(&pointfs2, LIST_GET(&pointfs, k));
1904 LIST_SYNC(&pointfs2);
1905 clip_and_install(fe, hn, LIST_FRONT(&pointfs2), LIST_SIZE(&pointfs2), &sinfo);
1906 for (unsigned j = 1; j < cnt; j++) {
1907 e = edges[j];
1908 if (ED_tree_index(e) & BWDEDGE) {
1909 makefwdedge(&fwdedge.out, e);
1910 e = &fwdedge.out;
1911 }
1912 for (size_t k = 1; k + 1 < LIST_SIZE(&pointfs); k++)
1913 LIST_AT(&pointfs, k)->x += sp->Multisep;
1914 LIST_CLEAR(&pointfs2);
1915 for (size_t k = 0; k < LIST_SIZE(&pointfs); k++)
1916 LIST_APPEND(&pointfs2, LIST_GET(&pointfs, k));
1917 LIST_SYNC(&pointfs2);
1918 clip_and_install(e, aghead(e), LIST_FRONT(&pointfs2), LIST_SIZE(&pointfs2),
1919 &sinfo);
1920 }
1921 LIST_FREE(&pointfs);
1922 LIST_FREE(&pointfs2);
1923}
1924
1925/* regular edges */
1926
1927static void completeregularpath(path *P, edge_t *first, edge_t *last,
1928 pathend_t *tendp, pathend_t *hendp,
1929 const boxes_t *boxes) {
1930 edge_t *uleft = top_bound(first, -1);
1931 edge_t *uright = top_bound(first, 1);
1932 if (uleft) {
1933 if (getsplinepoints(uleft) == NULL)
1934 return;
1935 }
1936 if (uright) {
1937 if (getsplinepoints(uright) == NULL)
1938 return;
1939 }
1940 edge_t *lleft = bot_bound(last, -1);
1941 edge_t *lright = bot_bound(last, 1);
1942 if (lleft) {
1943 if (getsplinepoints(lleft) == NULL)
1944 return;
1945 }
1946 if (lright) {
1947 if (getsplinepoints(lright) == NULL)
1948 return;
1949 }
1950 for (int i = 0; i < tendp->boxn; i++)
1951 add_box(P, tendp->boxes[i]);
1952 const size_t fb = P->nbox + 1;
1953 const size_t lb = fb + LIST_SIZE(boxes) - 3;
1954 for (size_t i = 0; i < LIST_SIZE(boxes); i++)
1955 add_box(P, LIST_GET(boxes, i));
1956 for (int i = hendp->boxn - 1; i >= 0; i--)
1957 add_box(P, hendp->boxes[i]);
1958 adjustregularpath(P, fb, lb);
1959}
1960
1961/* Add box to fill between node and interrank space. Needed because
1962 * nodes in a given rank can differ in height.
1963 * for now, regular edges always go from top to bottom
1964 */
1965static boxf makeregularend(boxf b, int side, double y) {
1966 assert(side == BOTTOM || side == TOP);
1967 if (side == BOTTOM) {
1968 return (boxf){{b.LL.x, y}, {b.UR.x, b.LL.y}};
1969 }
1970 return (boxf){{b.LL.x, b.UR.y}, {b.UR.x, y}};
1971}
1972
1973/* make sure the path is wide enough.
1974 * the % 2 was so that in rank boxes would only be grown if
1975 * they were == 0 while inter-rank boxes could be stretched to a min
1976 * width.
1977 * The list of boxes has three parts: tail boxes, path boxes, and head
1978 * boxes. (Note that because of back edges, the tail boxes might actually
1979 * belong to the head node, and vice versa.) fb is the index of the
1980 * first interrank path box and lb is the last interrank path box.
1981 * If fb > lb, there are none.
1982 *
1983 * The second for loop was added by ek long ago, and apparently is intended
1984 * to guarantee an overlap between adjacent boxes of at least MINW.
1985 * It doesn't do this.
1986 */
1987static void adjustregularpath(path *P, size_t fb, size_t lb) {
1988 boxf *bp1, *bp2;
1989
1990 for (size_t i = fb - 1; i < lb + 1; i++) {
1991 bp1 = &P->boxes[i];
1992 if ((i - fb) % 2 == 0) {
1993 if (bp1->LL.x >= bp1->UR.x) {
1994 double x = (bp1->LL.x + bp1->UR.x) / 2;
1995 bp1->LL.x = x - HALFMINW;
1996 bp1->UR.x = x + HALFMINW;
1997 }
1998 } else {
1999 if (bp1->LL.x + MINW > bp1->UR.x) {
2000 double x = (bp1->LL.x + bp1->UR.x) / 2;
2001 bp1->LL.x = x - HALFMINW;
2002 bp1->UR.x = x + HALFMINW;
2003 }
2004 }
2005 }
2006 for (size_t i = 0; i + 1 < P->nbox; i++) {
2007 bp1 = &P->boxes[i], bp2 = &P->boxes[i + 1];
2008 if (i >= fb && i <= lb && (i - fb) % 2 == 0) {
2009 if (bp1->LL.x + MINW > bp2->UR.x)
2010 bp2->UR.x = bp1->LL.x + MINW;
2011 if (bp1->UR.x - MINW < bp2->LL.x)
2012 bp2->LL.x = bp1->UR.x - MINW;
2013 } else if (i + 1 >= fb && i < lb && (i + 1 - fb) % 2 == 0) {
2014 if (bp1->LL.x + MINW > bp2->UR.x)
2015 bp1->LL.x = bp2->UR.x - MINW;
2016 if (bp1->UR.x - MINW < bp2->LL.x)
2017 bp1->UR.x = bp2->LL.x + MINW;
2018 }
2019 }
2020}
2021
2022static boxf rank_box(spline_info_t *sp, graph_t *g, int r) {
2023 boxf b = sp->Rank_box[r];
2024 if (b.LL.x == b.UR.x) {
2025 node_t *const left0 = GD_rank(g)[r].v[0];
2026 node_t *const left1 = GD_rank(g)[r + 1].v[0];
2027 b.LL.x = sp->LeftBound;
2028 b.LL.y = ND_coord(left1).y + GD_rank(g)[r + 1].ht2;
2029 b.UR.x = sp->RightBound;
2030 b.UR.y = ND_coord(left0).y - GD_rank(g)[r].ht1;
2031 sp->Rank_box[r] = b;
2032 }
2033 return b;
2034}
2035
2036/* returns count of vertically aligned edges starting at n */
2037static int straight_len(node_t *n) {
2038 int cnt = 0;
2039 node_t *v;
2040
2041 v = n;
2042 while (ND_out(v).size > 0) {
2043 v = aghead(ND_out(v).list[0]);
2044 if (ND_node_type(v) != VIRTUAL)
2045 break;
2046 if (ND_out(v).size != 1 || ND_in(v).size != 1)
2047 break;
2048 if (ND_coord(v).x != ND_coord(n).x)
2049 break;
2050 cnt++;
2051 }
2052 return cnt;
2053}
2054
2055static edge_t *straight_path(edge_t *e, int cnt, points_t *plist) {
2056 edge_t *f = e;
2057
2058 while (cnt--)
2059 f = ND_out(aghead(f)).list[0];
2060 assert(!LIST_IS_EMPTY(plist));
2061 LIST_APPEND(plist, LIST_GET(plist, LIST_SIZE(plist) - 1));
2062 LIST_APPEND(plist, LIST_GET(plist, LIST_SIZE(plist) - 1));
2063
2064 return f;
2065}
2066
2067static void recover_slack(edge_t *e, path *p) {
2068 node_t *vn;
2069
2070 size_t b = 0; // skip first rank box
2071 for (vn = aghead(e); ND_node_type(vn) == VIRTUAL && !sinfo.splineMerge(vn);
2072 vn = aghead(ND_out(vn).list[0])) {
2073 while (b < p->nbox && p->boxes[b].LL.y > ND_coord(vn).y)
2074 b++;
2075 if (b >= p->nbox)
2076 break;
2077 if (p->boxes[b].UR.y < ND_coord(vn).y) {
2078 if (ND_out(vn).size == 0) {
2079 break;
2080 }
2081 continue;
2082 }
2083 if (ND_label(vn))
2084 resize_vn(vn, p->boxes[b].LL.x, p->boxes[b].UR.x,
2085 p->boxes[b].UR.x + ND_rw(vn));
2086 else
2087 resize_vn(vn, p->boxes[b].LL.x, (p->boxes[b].LL.x + p->boxes[b].UR.x) / 2,
2088 p->boxes[b].UR.x);
2089 if (ND_out(vn).size == 0) {
2090 break;
2091 }
2092 }
2093}
2094
2095static void resize_vn(node_t *vn, double lx, double cx, double rx) {
2096 ND_coord(vn).x = cx;
2097 ND_lw(vn) = cx - lx, ND_rw(vn) = rx - cx;
2098}
2099
2100/* side > 0 means right. side < 0 means left */
2101static edge_t *top_bound(edge_t *e, int side) {
2102 edge_t *f, *ans = NULL;
2103 int i;
2104
2105 for (i = 0; (f = ND_out(agtail(e)).list[i]); i++) {
2106 if (side * (ND_order(aghead(f)) - ND_order(aghead(e))) <= 0)
2107 continue;
2108 if (ED_spl(f) == NULL &&
2109 (ED_to_orig(f) == NULL || ED_spl(ED_to_orig(f)) == NULL))
2110 continue;
2111 if (ans == NULL || side * (ND_order(aghead(ans)) - ND_order(aghead(f))) > 0)
2112 ans = f;
2113 }
2114 return ans;
2115}
2116
2117static edge_t *bot_bound(edge_t *e, int side) {
2118 edge_t *f, *ans = NULL;
2119 int i;
2120
2121 for (i = 0; (f = ND_in(aghead(e)).list[i]); i++) {
2122 if (side * (ND_order(agtail(f)) - ND_order(agtail(e))) <= 0)
2123 continue;
2124 if (ED_spl(f) == NULL &&
2125 (ED_to_orig(f) == NULL || ED_spl(ED_to_orig(f)) == NULL))
2126 continue;
2127 if (ans == NULL || side * (ND_order(agtail(ans)) - ND_order(agtail(f))) > 0)
2128 ans = f;
2129 }
2130 return ans;
2131}
2132
2133/* common routines */
2134
2135static bool cl_vninside(graph_t *cl, node_t *n) {
2136 return BETWEEN(GD_bb(cl).LL.x, ND_coord(n).x, GD_bb(cl).UR.x) &&
2137 BETWEEN(GD_bb(cl).LL.y, ND_coord(n).y, GD_bb(cl).UR.y);
2138}
2139
2140/* All nodes belong to some cluster, which may be the root graph.
2141 * For the following, we only want a cluster if it is a real cluster
2142 * It is not clear this will handle all potential problems. It seems one
2143 * could have hcl and tcl contained in cl, which would also cause problems.
2144 */
2145#define REAL_CLUSTER(n) (ND_clust(n) == g ? NULL : ND_clust(n))
2146
2147/* returns the cluster of (adj) that interferes with n,
2148 */
2149static Agraph_t *cl_bound(graph_t *g, node_t *n, node_t *adj) {
2150 graph_t *rv, *cl, *tcl, *hcl;
2151 edge_t *orig;
2152
2153 rv = NULL;
2154 if (ND_node_type(n) == NORMAL || ND_out(n).size == 0)
2155 tcl = hcl = ND_clust(n);
2156 else {
2157 orig = ED_to_orig(ND_out(n).list[0]);
2158 tcl = ND_clust(agtail(orig));
2159 hcl = ND_clust(aghead(orig));
2160 }
2161 if (ND_node_type(adj) == NORMAL) {
2162 cl = REAL_CLUSTER(adj);
2163 if (cl && cl != tcl && cl != hcl)
2164 rv = cl;
2165 } else {
2166 orig = ED_to_orig(ND_out(adj).list[0]);
2167 cl = REAL_CLUSTER(agtail(orig));
2168 if (cl && cl != tcl && cl != hcl && cl_vninside(cl, adj))
2169 rv = cl;
2170 else {
2171 cl = REAL_CLUSTER(aghead(orig));
2172 if (cl && cl != tcl && cl != hcl && cl_vninside(cl, adj))
2173 rv = cl;
2174 }
2175 }
2176 return rv;
2177}
2178
2179/* Return an initial bounding box to be used for building the
2180 * beginning or ending of the path of boxes.
2181 * Height reflects height of tallest node on rank.
2182 * The extra space provided by FUDGE allows begin/endpath to create a box
2183 * FUDGE-2 away from the node, so the routing can avoid the node and the
2184 * box is at least 2 wide.
2185 */
2186#define FUDGE 4
2187
2189 edge_t *ie, edge_t *oe) {
2190 double b, nb;
2191 graph_t *left_cl, *right_cl;
2192 node_t *left, *right;
2193 boxf rv;
2194
2195 left_cl = right_cl = NULL;
2196
2197 /* give this node all the available space up to its neighbors */
2198 b = (double)(ND_coord(vn).x - ND_lw(vn) - FUDGE);
2199 if ((left = neighbor(g, vn, ie, oe, -1))) {
2200 if ((left_cl = cl_bound(g, vn, left)))
2201 nb = GD_bb(left_cl).UR.x + sp.Splinesep;
2202 else {
2203 nb = (double)(ND_coord(left).x + ND_mval(left));
2204 if (ND_node_type(left) == NORMAL)
2205 nb += GD_nodesep(g) / 2.;
2206 else
2207 nb += sp.Splinesep;
2208 }
2209 if (nb < b)
2210 b = nb;
2211 rv.LL.x = round(b);
2212 } else
2213 rv.LL.x = fmin(round(b), sp.LeftBound);
2214
2215 /* we have to leave room for our own label! */
2216 if (ND_node_type(vn) == VIRTUAL && ND_label(vn))
2217 b = (double)(ND_coord(vn).x + 10);
2218 else
2219 b = (double)(ND_coord(vn).x + ND_rw(vn) + FUDGE);
2220 if ((right = neighbor(g, vn, ie, oe, 1))) {
2221 if ((right_cl = cl_bound(g, vn, right)))
2222 nb = GD_bb(right_cl).LL.x - sp.Splinesep;
2223 else {
2224 nb = ND_coord(right).x - ND_lw(right);
2225 if (ND_node_type(right) == NORMAL)
2226 nb -= GD_nodesep(g) / 2.;
2227 else
2228 nb -= sp.Splinesep;
2229 }
2230 if (nb > b)
2231 b = nb;
2232 rv.UR.x = round(b);
2233 } else
2234 rv.UR.x = fmax(round(b), sp.RightBound);
2235
2236 if (ND_node_type(vn) == VIRTUAL && ND_label(vn)) {
2237 rv.UR.x -= ND_rw(vn);
2238 if (rv.UR.x < rv.LL.x)
2239 rv.UR.x = ND_coord(vn).x;
2240 }
2241
2242 rv.LL.y = ND_coord(vn).y - GD_rank(g)[ND_rank(vn)].ht1;
2243 rv.UR.y = ND_coord(vn).y + GD_rank(g)[ND_rank(vn)].ht2;
2244 return rv;
2245}
2246
2247static node_t *neighbor(graph_t *g, node_t *vn, edge_t *ie, edge_t *oe,
2248 int dir) {
2249 int i;
2250 node_t *n, *rv = NULL;
2251 rank_t *rank = &GD_rank(g)[ND_rank(vn)];
2252
2253 for (i = ND_order(vn) + dir; i >= 0 && i < rank->n; i += dir) {
2254 n = rank->v[i];
2255 if (ND_node_type(n) == VIRTUAL && ND_label(n)) {
2256 rv = n;
2257 break;
2258 }
2259 if (ND_node_type(n) == NORMAL) {
2260 rv = n;
2261 break;
2262 }
2263 if (!pathscross(n, vn, ie, oe)) {
2264 rv = n;
2265 break;
2266 }
2267 }
2268 return rv;
2269}
2270
2271static bool pathscross(node_t *n0, node_t *n1, edge_t *ie1, edge_t *oe1) {
2272 edge_t *e0, *e1;
2273 node_t *na, *nb;
2274 int order, cnt;
2275
2276 order = ND_order(n0) > ND_order(n1);
2277 if (ND_out(n0).size != 1 && ND_out(n1).size != 1)
2278 return false;
2279 e1 = oe1;
2280 if (ND_out(n0).size == 1 && e1) {
2281 e0 = ND_out(n0).list[0];
2282 for (cnt = 0; cnt < 2; cnt++) {
2283 if ((na = aghead(e0)) == (nb = aghead(e1)))
2284 break;
2285 if (order != (ND_order(na) > ND_order(nb)))
2286 return true;
2287 if (ND_out(na).size != 1 || ND_node_type(na) == NORMAL)
2288 break;
2289 e0 = ND_out(na).list[0];
2290 if (ND_out(nb).size != 1 || ND_node_type(nb) == NORMAL)
2291 break;
2292 e1 = ND_out(nb).list[0];
2293 }
2294 }
2295 e1 = ie1;
2296 if (ND_in(n0).size == 1 && e1) {
2297 e0 = ND_in(n0).list[0];
2298 for (cnt = 0; cnt < 2; cnt++) {
2299 if ((na = agtail(e0)) == (nb = agtail(e1)))
2300 break;
2301 if (order != (ND_order(na) > ND_order(nb)))
2302 return true;
2303 if (ND_in(na).size != 1 || ND_node_type(na) == NORMAL)
2304 break;
2305 e0 = ND_in(na).list[0];
2306 if (ND_in(nb).size != 1 || ND_node_type(nb) == NORMAL)
2307 break;
2308 e1 = ND_in(nb).list[0];
2309 }
2310 }
2311 return false;
2312}
2313
2314#ifdef DEBUG
2315void showpath(path *p) {
2316 pointf LL, UR;
2317
2318 fprintf(stderr, "%%!PS\n");
2319 for (size_t i = 0; i < p->nbox; i++) {
2320 LL = p->boxes[i].LL;
2321 UR = p->boxes[i].UR;
2322 fprintf(stderr,
2323 "newpath %.04f %.04f moveto %.04f %.04f lineto %.04f %.04f lineto "
2324 "%.04f %.04f lineto closepath stroke\n",
2325 LL.x, LL.y, UR.x, LL.y, UR.x, UR.y, LL.x, UR.y);
2326 }
2327 fprintf(stderr, "showpage\n");
2328}
2329#endif
int normalize(graph_t *g)
Definition adjust.c:722
static agxbuf last
last message
Definition agerror.c:31
Dynamically expanding string buffers.
static void agxbfree(agxbuf *xb)
free any malloced resources
Definition agxbuf.h:97
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
Definition agxbuf.h:252
static WUR char * agxbuse(agxbuf *xb)
Definition agxbuf.h:325
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 BETWEEN(a, b, c)
Definition arith.h:38
#define MIN(a, b)
Definition arith.h:28
#define M_PI
Definition arith.h:41
#define MAX(a, b)
Definition arith.h:33
#define right(i)
Definition closest.c:74
void setEdgeType(graph_t *g, int defaultValue)
Definition utils.c:1358
void updateBB(graph_t *g, textlabel_t *lp)
Definition utils.c:575
#define SELFWPEDGE
Definition const.h:152
#define NORMAL
Definition const.h:24
#define EDGETYPE_SPLINE
Definition const.h:239
#define FLATORDER
Definition const.h:28
#define REGULAREDGE
Definition const.h:150
#define EDGE_LABEL
Definition const.h:167
#define IGNORED
Definition const.h:30
#define RANKDIR_TB
Definition const.h:181
#define EDGETYPE_CURVED
Definition const.h:236
#define VIRTUAL
Definition const.h:25
#define RANKDIR_LR
Definition const.h:182
#define EDGETYPE_ORTHO
Definition const.h:238
#define EDGETYPE_PLINE
Definition const.h:237
#define EDGETYPE_LINE
Definition const.h:235
#define EDGETYPEMASK
Definition const.h:155
#define EDGETYPE_NONE
Definition const.h:234
#define GVSPLINES
Definition const.h:164
#define BOTTOM
Definition const.h:117
#define FLATEDGE
Definition const.h:151
#define TOP
Definition const.h:119
#define SELFNPEDGE
Definition const.h:153
void dot_init_node_edge(graph_t *g)
Definition dotinit.c:89
void dot_cleanup(graph_t *g)
Definition dotinit.c:181
int dot_mincross(Agraph_t *)
Definition mincross.c:330
void dot_sameports(Agraph_t *)
void dot_rank(Agraph_t *)
Definition rank.c:493
WUR int dot_position(Agraph_t *)
Definition position.c:121
static bool leftOf(pointf p1, pointf p2, pointf p3)
Return true if p3 is to left of ray p1->p2.
static edge_t * top_bound(edge_t *e, int side)
static void makeBottomFlatEnd(graph_t *g, const spline_info_t sp, path *P, node_t *n, edge_t *e, pathend_t *endp, bool isBegin)
static void make_flat_labeled_edge(graph_t *g, const spline_info_t sp, path *P, edge_t *e, int et)
#define HALFMINW
Definition dotsplines.c:39
static void recover_slack(edge_t *e, path *p)
static void swap_bezier(bezier *b)
Definition dotsplines.c:144
static void make_regular_edge(graph_t *g, spline_info_t *sp, path *P, edge_t **edges, unsigned cnt, int et)
static bool cl_vninside(graph_t *cl, node_t *n)
static Agraph_t * cl_bound(graph_t *g, node_t *n, node_t *adj)
#define MINW
Definition dotsplines.c:38
static edge_t * bot_bound(edge_t *e, int side)
int dot_splines(graph_t *g)
Definition dotsplines.c:486
#define FWDEDGE
Definition dotsplines.c:41
static void makeSimpleFlatLabels(node_t *tn, node_t *hn, edge_t **edges, unsigned cnt, int et, unsigned n_lbls)
Definition dotsplines.c:951
static int make_flat_edge(graph_t *g, const spline_info_t sp, path *P, edge_t **edges, unsigned cnt, int et)
static void makeSimpleFlat(node_t *tn, node_t *hn, edge_t **edges, unsigned cnt, int et)
#define NSUB
Definition dotsplines.c:36
static edge_t * cloneEdge(graph_t *g, node_t *tn, node_t *hn, edge_t *orig)
Definition dotsplines.c:891
static void setState(graph_t *auxg, attr_state_t *attr_state)
Definition dotsplines.c:688
static void edge_normalize(graph_t *g)
Definition dotsplines.c:173
static int makeLineEdge(graph_t *g, edge_t *fe, points_t *points, node_t **hp)
#define BWDEDGE
Definition dotsplines.c:42
static bool swap_ends_p(edge_t *e)
Definition dotsplines.c:113
int portcmp(port p0, port p1)
Definition dotsplines.c:128
static bool spline_merge(node_t *n)
Definition dotsplines.c:108
static void makeFlatEnd(graph_t *g, const spline_info_t sp, path *P, node_t *n, edge_t *e, pathend_t *endp, bool isBegin)
#define AUXGRAPH
Definition dotsplines.c:45
static void place_vnlabel(node_t *n)
Definition dotsplines.c:491
static void setEdgeLabelPos(graph_t *g)
Definition dotsplines.c:199
#define MAINGRAPH
Definition dotsplines.c:44
static void completeregularpath(path *P, edge_t *first, edge_t *last, pathend_t *tendp, pathend_t *hendp, const boxes_t *boxes)
static int edgecmp(const void *p0, const void *p1)
Definition dotsplines.c:542
static graph_t * cloneGraph(graph_t *g, attr_state_t *attr_state)
Definition dotsplines.c:780
static void adjustregularpath(path *P, size_t fb, size_t lb)
static void makefwdedge(edge_t *new, edge_t *old)
Definition dotsplines.c:48
static int make_flat_adj_edges(graph_t *g, edge_t **edges, unsigned cnt, edge_t *e0, int et)
#define LBL_SPACE
Definition dotsplines.c:944
static bool pathscross(node_t *n0, node_t *n1, edge_t *ie1, edge_t *oe1)
static void cleanupCloneGraph(graph_t *g, attr_state_t *attr_state)
Definition dotsplines.c:827
static int straight_len(node_t *n)
#define FUDGE
#define GRAPHTYPEMASK
Definition dotsplines.c:46
#define REAL_CLUSTER(n)
static splineInfo sinfo
Definition dotsplines.c:125
static int edgelblcmpfn(const void *x, const void *y)
Definition dotsplines.c:914
static boxf rank_box(spline_info_t *sp, graph_t *g, int r)
static void resetRW(graph_t *g)
Definition dotsplines.c:187
static pointf transformf(pointf p, pointf del, int flip)
rotate, if necessary, then translate points
Definition dotsplines.c:900
static int dot_splines_(graph_t *g, int normalize)
Definition dotsplines.c:228
static boxf makeregularend(boxf b, int side, double y)
static void swap_spline(splines *s)
Definition dotsplines.c:154
static void setflags(edge_t *e, int hint1, int hint2, int f3)
Definition dotsplines.c:504
static boxf maximal_bbox(graph_t *g, const spline_info_t sp, node_t *vn, edge_t *ie, edge_t *oe)
static void resize_vn(node_t *vn, double lx, double cx, double rx)
static void make_flat_bottom_edges(graph_t *g, const spline_info_t sp, path *P, edge_t **edges, unsigned cnt, edge_t *e, bool use_splines)
static edge_t * straight_path(edge_t *e, int cnt, points_t *plist)
static node_t * cloneNode(graph_t *g, node_t *orign)
Definition dotsplines.c:877
static float dy
Definition draw.c:43
static float dx
Definition draw.c:42
#define left
Definition dthdr.h:12
static void del(Dict_t *d, Dtlink_t **set, Agedge_t *e)
Definition edge.c:149
#define le
Definition edges.h:29
void update_bb_bz(boxf *bb, const pointf *cp)
Definition emit.c:770
struct pointf_s pointf
static WUR pointf add_pointf(pointf p, pointf q)
Definition geomprocs.h:88
Agsym_t * E_sametail
Definition globals.h:86
Agsym_t * N_fontsize
Definition globals.h:78
Agsym_t * E_labelfontsize
Definition globals.h:87
Agsym_t * E_fontcolor
Definition globals.h:84
Agsym_t * N_group
Definition globals.h:81
Agsym_t * N_width
Definition globals.h:77
Agsym_t * E_weight
Definition globals.h:83
int State
Definition globals.h:66
Agsym_t * N_orientation
Definition globals.h:80
Agsym_t * N_sides
Definition globals.h:79
Agsym_t * E_headclip
Definition globals.h:88
Agsym_t * G_ordering
Definition globals.h:75
Agsym_t * E_headlabel
Definition globals.h:86
Agsym_t * N_showboxes
Definition globals.h:79
Agsym_t * N_fontname
Definition globals.h:78
Agsym_t * E_fontname
Definition globals.h:84
Agsym_t * N_style
Definition globals.h:79
int EdgeLabelsDone
Definition globals.h:68
Agsym_t * E_dir
Definition globals.h:84
Agsym_t * E_label
Definition globals.h:84
Agsym_t * N_skew
Definition globals.h:80
Agsym_t * E_minlen
Definition globals.h:83
Agsym_t * N_shape
Definition globals.h:77
Agsym_t * N_xlabel
Definition globals.h:78
Agsym_t * E_label_float
Definition globals.h:86
Agsym_t * E_samehead
Definition globals.h:86
Agsym_t * N_nojustify
Definition globals.h:79
Agsym_t * E_taillabel
Definition globals.h:87
Agsym_t * N_label
Definition globals.h:78
Agsym_t * E_labelangle
Definition globals.h:88
Agsym_t * E_fontsize
Definition globals.h:84
Agsym_t * N_fixed
Definition globals.h:80
Agsym_t * N_peripheries
Definition globals.h:79
Agsym_t * E_labelfontname
Definition globals.h:87
Agsym_t * N_distortion
Definition globals.h:80
Agsym_t * E_xlabel
Definition globals.h:84
Agsym_t * E_constr
Definition globals.h:85
Agsym_t * N_fontcolor
Definition globals.h:78
Agsym_t * E_labelfontcolor
Definition globals.h:87
Agsym_t * E_tailclip
Definition globals.h:88
Agsym_t * E_labeldistance
Definition globals.h:88
Agsym_t * N_ordering
Definition globals.h:79
Agsym_t * N_height
Definition globals.h:77
void free(void *)
node NULL
Definition grammar.y:181
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:204
Agsym_t * agattr_text(Agraph_t *g, int kind, char *name, const char *value)
creates or looks up text attributes of a graph
Definition attr.c:333
int agset(void *obj, char *name, const char *value)
Definition attr.c:474
Agsym_t * agnxtattr(Agraph_t *g, int kind, Agsym_t *attr)
permits traversing the list of attributes of a given type
Definition attr.c:362
int agxset(void *obj, Agsym_t *sym, const char *value)
Definition attr.c:521
int agcopyattr(void *oldobj, void *newobj)
copies all of the attributes from one object to another
Definition attr.c:632
Agsym_t * agattr_html(Agraph_t *g, int kind, char *name, const char *value)
agattr_text, but creates HTML-like values
Definition attr.c:337
#define ED_to_orig(e)
Definition types.h:598
#define ED_tree_index(e)
Definition types.h:600
Agedge_t * agedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, char *name, int createflag)
Definition edge.c:234
#define AGMKOUT(e)
Definition cgraph.h:976
Agedge_t * agnxtin(Agraph_t *g, Agedge_t *e)
Definition edge.c:70
#define ED_head_label(e)
Definition types.h:587
#define agfindedgeattr(g, a)
Definition types.h:617
Agedge_t * agfstout(Agraph_t *g, Agnode_t *n)
Definition edge.c:28
#define ED_spl(e)
Definition types.h:595
#define agtail(e)
Definition cgraph.h:982
#define ED_edge_type(e)
Definition types.h:582
#define ED_alg(e)
Definition types.h:578
#define ED_tail_label(e)
Definition types.h:596
#define ED_adjacent(e)
Definition types.h:584
#define aghead(e)
Definition cgraph.h:983
Agedge_t * agnxtout(Agraph_t *g, Agedge_t *e)
Definition edge.c:42
#define AGTAIL(e)
Definition cgraph.h:978
#define ED_head_port(e)
Definition types.h:588
Agedge_t * agfstin(Agraph_t *g, Agnode_t *n)
Definition edge.c:57
#define ED_label(e)
Definition types.h:589
#define ED_tail_port(e)
Definition types.h:597
#define AGHEAD(e)
Definition cgraph.h:979
#define ED_to_virt(e)
Definition types.h:599
#define AGOUT2IN(outedge)
Agedgepair_s.out -> Agedgepair_s.in/*#end#*‍/.
Definition cgraph.h:972
void agwarningf(const char *fmt,...)
Definition agerror.c:175
int agerr(agerrlevel_t level, const char *fmt,...)
Definition agerror.c:157
@ AGPREV
Definition cgraph.h:951
#define GD_minrank(g)
Definition types.h:384
#define agfindgraphattr(g, a)
Definition types.h:613
#define GD_maxrank(g)
Definition types.h:382
#define GD_drawing(g)
Definition types.h:353
Agdesc_t Agundirected
undirected
Definition graph.c:280
#define GD_has_labels(g)
Definition types.h:368
int agisdirected(Agraph_t *g)
Definition graph.c:184
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
Definition graph.c:97
#define GD_rank(g)
Definition types.h:395
#define GD_bb(g)
Definition types.h:354
#define GD_nlist(g)
Definition types.h:393
Agraph_t * agopen(char *name, Agdesc_t desc, Agdisc_t *disc)
creates a new graph with the given name and kind
Definition graph.c:44
#define GD_dotroot(g)
Definition types.h:361
#define GD_nodesep(g)
Definition types.h:394
#define GD_charset(g)
Definition types.h:367
#define GD_gvc(g)
Definition types.h:355
#define GD_flip(g)
Definition types.h:378
#define GD_ranksep(g)
Definition types.h:397
Agdesc_t Agdirected
directed
Definition graph.c:278
Agnode_t * agnode(Agraph_t *g, char *name, int createflag)
Definition node.c:143
#define ND_rank(n)
Definition types.h:523
#define ND_ht(n)
Definition types.h:500
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:50
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:43
#define ND_next(n)
Definition types.h:510
#define ND_clust(n)
Definition types.h:489
#define ND_other(n)
Definition types.h:514
#define agfindnodeattr(g, a)
Definition types.h:615
#define ND_label(n)
Definition types.h:502
#define ND_alg(n)
Definition types.h:484
#define ND_flat_out(n)
Definition types.h:493
#define ND_rw(n)
Definition types.h:525
#define ND_node_type(n)
Definition types.h:511
#define ND_lw(n)
Definition types.h:506
#define ND_mval(n)
Definition types.h:508
#define ND_order(n)
Definition types.h:513
#define ND_coord(n)
Definition types.h:490
#define ND_in(n)
Definition types.h:501
#define ND_out(n)
Definition types.h:515
Agraph_t * agraphof(void *obj)
Definition obj.c:187
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:145
Agraph_t * agroot(void *obj)
Definition obj.c:170
#define AGSEQ(obj)
Definition cgraph.h:225
@ AGEDGE
Definition cgraph.h:207
@ AGNODE
Definition cgraph.h:207
@ AGRAPH
Definition cgraph.h:207
void * agbindrec(void *obj, const char *name, unsigned int recsize, int move_to_front)
attaches a new record of the given size to the object
Definition rec.c:91
int aghtmlstr(const char *)
Definition refstr.c:440
Agraph_t * agsubg(Agraph_t *g, char *name, int cflag)
Definition subg.c:52
Arithmetic helper functions.
#define SWAP(a, b)
Definition gv_math.h:133
static gdPoint * points
void mark_lowclusters(Agraph_t *root)
Definition cluster.c:385
type-generic dynamically expanding list
#define LIST_AT(list, index)
Definition list.h:210
#define LIST_APPEND(list,...)
Definition list.h:151
#define LIST(type)
Definition list.h:66
#define LIST_SIZE(list)
Definition list.h:92
#define LIST_CLEAR(list)
Definition list.h:307
#define LIST_FREE(list)
Definition list.h:413
#define LIST_SORT(list, cmp)
Definition list.h:381
#define LIST_FRONT(list)
Definition list.h:222
#define LIST_IS_EMPTY(list)
Definition list.h:102
#define LIST_SYNC(list)
Definition list.h:370
#define LIST_GET(list, index)
Definition list.h:197
#define EDGE_TYPE(g)
Definition macros.h:25
#define neighbor(t, i, edim, elist)
Definition make_map.h:41
int rank(graph_t *g, int balance, int maxiter)
Definition ns.c:1104
int orthoEdges(Agraph_t *g, bool useLbls)
Definition ortho.c:1161
void dotneato_postprocess(Agraph_t *g)
Definition postproc.c:657
bezier * new_spline(edge_t *e, size_t sz)
create and attach a new Bézier of size sz to the edge d
Definition splines.c:200
splines * getsplinepoints(edge_t *e)
Definition splines.c:1304
void clip_and_install(edge_t *fe, node_t *hn, pointf *ps, size_t pn, splineInfo *info)
Definition splines.c:221
pointf * simpleSplineRoute(pointf, pointf, Ppoly_t, size_t *, int)
Given a simple (ccw) polygon, route an edge from tp to hp.
Definition routespl.c:135
void beginpath(path *, Agedge_t *, bool is_flat_edge, pathend_t *, bool)
Definition splines.c:360
shape_kind shapeOf(node_t *)
Definition shapes.c:1909
void add_box(path *, boxf)
Definition splines.c:322
void endpath(path *, Agedge_t *, bool is_flat_edge, pathend_t *, bool)
Definition splines.c:541
pointf * routesplines(path *, size_t *)
Definition routespl.c:560
void makeSelfEdge(edge_t *edges[], size_t cnt, double sizex, double sizey, splineInfo *sinfo)
Definition splines.c:1117
void routesplinesterm(void)
Definition routespl.c:190
void routesplinesinit(void)
Definition routespl.c:178
pointf * routepolylines(path *pp, size_t *npoints)
Definition routespl.c:564
int place_portlabel(edge_t *e, bool head_p)
Definition splines.c:1255
void makeStraightEdges(graph_t *g, edge_t **edges, size_t e_cnt, int et, splineInfo *sinfo)
Definition routespl.c:938
Agobj_t base
Definition cgraph.h:269
Agrec_t hdr
Definition types.h:544
Agedge_t in
Definition cgraph.h:276
Agedge_t out
Definition cgraph.h:276
Agrec_t * data
stores programmer-defined data, access with AGDATA
Definition cgraph.h:212
graph or subgraph
Definition cgraph.h:424
Agraph_t * root
subgraphs - ancestors
Definition cgraph.h:433
string attribute descriptor symbol in Agattr_s.dict
Definition cgraph.h:641
char * name
Definition cgraph.h:643
char * defval
Definition cgraph.h:644
attrsym_t * N_style
Definition dotsplines.c:667
attrsym_t * E_sametail
Definition dotsplines.c:647
attrsym_t * E_fontsize
Definition dotsplines.c:652
attrsym_t * N_shape
Definition dotsplines.c:666
attrsym_t * E_minlen
Definition dotsplines.c:649
attrsym_t * E_tailclip
Definition dotsplines.c:660
attrsym_t * E_taillabel
Definition dotsplines.c:661
attrsym_t * E_label
Definition dotsplines.c:655
attrsym_t * E_weight
Definition dotsplines.c:648
attrsym_t * N_nojustify
Definition dotsplines.c:681
attrsym_t * N_fontcolor
Definition dotsplines.c:670
attrsym_t * N_peripheries
Definition dotsplines.c:676
attrsym_t * E_xlabel
Definition dotsplines.c:662
attrsym_t * E_samehead
Definition dotsplines.c:646
attrsym_t * N_xlabel
Definition dotsplines.c:672
attrsym_t * E_label_float
Definition dotsplines.c:656
attrsym_t * N_fontname
Definition dotsplines.c:669
attrsym_t * E_headclip
Definition dotsplines.c:653
attrsym_t * N_sides
Definition dotsplines.c:675
attrsym_t * N_group
Definition dotsplines.c:682
attrsym_t * N_ordering
Definition dotsplines.c:674
attrsym_t * N_width
Definition dotsplines.c:665
attrsym_t * N_fixed
Definition dotsplines.c:680
attrsym_t * E_labelfontname
Definition dotsplines.c:658
attrsym_t * E_dir
Definition dotsplines.c:645
attrsym_t * E_labelfontcolor
Definition dotsplines.c:657
attrsym_t * N_height
Definition dotsplines.c:664
attrsym_t * G_ordering
Definition dotsplines.c:684
attrsym_t * E_constr
Definition dotsplines.c:644
attrsym_t * N_label
Definition dotsplines.c:671
attrsym_t * E_fontname
Definition dotsplines.c:651
attrsym_t * N_skew
Definition dotsplines.c:677
attrsym_t * N_distortion
Definition dotsplines.c:679
attrsym_t * E_fontcolor
Definition dotsplines.c:650
attrsym_t * E_headlabel
Definition dotsplines.c:654
attrsym_t * N_showboxes
Definition dotsplines.c:673
attrsym_t * N_orientation
Definition dotsplines.c:678
attrsym_t * N_fontsize
Definition dotsplines.c:668
attrsym_t * E_labelfontsize
Definition dotsplines.c:659
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
Definition types.h:81
port start
Definition types.h:82
boxf * boxes
Definition types.h:85
port end
Definition types.h:83
size_t nbox
number of subdivisions
Definition types.h:84
boxf nb
Definition types.h:74
int boxn
Definition types.h:77
int sidemask
Definition types.h:76
boxf boxes[20]
Definition types.h:78
double x
Definition geom.h:29
double y
Definition geom.h:29
Definition types.h:48
pointf p
Definition types.h:49
double theta
Definition types.h:50
bool constrained
Definition types.h:55
bool defined
Definition types.h:54
double pht1
Definition types.h:207
node_t ** v
Definition types.h:202
double ht1
Definition types.h:205
double pht2
Definition types.h:208
bool(* swapEnds)(edge_t *e)
Definition types.h:67
bool(* splineMerge)(node_t *n)
Definition types.h:68
boxf * Rank_box
Definition dotsplines.c:69
double RightBound
Definition dotsplines.c:66
double LeftBound
Definition dotsplines.c:65
double Multisep
Definition dotsplines.c:68
double Splinesep
Definition dotsplines.c:67
pointf pos
Definition types.h:114
bool set
Definition types.h:123
struct poly_s poly
@ SH_RECORD
Definition types.h:187
#define SET_RANKDIR(g, rd)
Definition types.h:607
Definition grammar.c:90
struct item_s * list
Definition grammar.c:96