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