Graphviz 14.1.3~dev.20260207.0611
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 edge_t *const *const ptr0 = x;
909 edge_t *const *const ptr1 = y;
910 pointf sz0, sz1;
911
912 edge_t *e0 = *ptr0;
913 edge_t *e1 = *ptr1;
914
915 if (ED_label(e0)) {
916 if (ED_label(e1)) {
917 sz0 = ED_label(e0)->dimen;
918 sz1 = ED_label(e1)->dimen;
919 if (sz0.x > sz1.x)
920 return -1;
921 if (sz0.x < sz1.x)
922 return 1;
923 if (sz0.y > sz1.y)
924 return -1;
925 if (sz0.y < sz1.y)
926 return 1;
927 return 0;
928 }
929 return -1;
930 }
931 if (ED_label(e1)) {
932 return 1;
933 }
934 return 0;
935}
936
937#define LBL_SPACE 6 /* space between labels, in points */
938
939/* This handles the second simplest case for flat edges between
940 * two adjacent nodes. We still invoke a dot on a rotated problem
941 * to handle edges with ports. This usually works, but fails for
942 * records because of their weird nature.
943 */
944static void makeSimpleFlatLabels(node_t *tn, node_t *hn, edge_t **edges,
945 unsigned cnt, int et, unsigned n_lbls) {
947 edge_t *e = *edges;
948 pointf points[10], tp, hp;
949 double leftend, rightend, ctrx, ctry, miny, maxy;
950 double uminx, umaxx;
951 double lminx = 0.0, lmaxx = 0.0;
952
953 edge_t **earray = gv_calloc(cnt, sizeof(edge_t *));
954
955 for (unsigned i = 0; i < cnt; i++) {
956 earray[i] = edges[i];
957 }
958
959 qsort(earray, cnt, sizeof(edge_t *), edgelblcmpfn);
960
961 tp = add_pointf(ND_coord(tn), ED_tail_port(e).p);
962 hp = add_pointf(ND_coord(hn), ED_head_port(e).p);
963
964 leftend = tp.x + ND_rw(tn);
965 rightend = hp.x - ND_lw(hn);
966 ctrx = (leftend + rightend) / 2.0;
967
968 /* do first edge */
969 e = earray[0];
970 size_t pointn = 0;
971 points[pointn++] = tp;
972 points[pointn++] = tp;
973 points[pointn++] = hp;
974 points[pointn++] = hp;
975 clip_and_install(e, aghead(e), points, pointn, &sinfo);
976 ED_label(e)->pos.x = ctrx;
977 ED_label(e)->pos.y = tp.y + (ED_label(e)->dimen.y + LBL_SPACE) / 2.0;
978 ED_label(e)->set = true;
979
980 miny = tp.y + LBL_SPACE / 2.0;
981 maxy = miny + ED_label(e)->dimen.y;
982 uminx = ctrx - ED_label(e)->dimen.x / 2.0;
983 umaxx = ctrx + ED_label(e)->dimen.x / 2.0;
984
985 unsigned i;
986 for (i = 1; i < n_lbls; i++) {
987 e = earray[i];
988 if (i % 2) { /* down */
989 if (i == 1) {
990 lminx = ctrx - ED_label(e)->dimen.x / 2.0;
991 lmaxx = ctrx + ED_label(e)->dimen.x / 2.0;
992 }
993 miny -= LBL_SPACE + ED_label(e)->dimen.y;
994 points[0] = tp;
995 points[1] = (pointf){.x = tp.x, .y = miny - LBL_SPACE};
996 points[2] = (pointf){.x = hp.x, .y = points[1].y};
997 points[3] = hp;
998 points[4] = (pointf){.x = lmaxx, .y = hp.y};
999 points[5] = (pointf){.x = lmaxx, .y = miny};
1000 points[6] = (pointf){.x = lminx, .y = miny};
1001 points[7] = (pointf){.x = lminx, .y = tp.y};
1002 ctry = miny + ED_label(e)->dimen.y / 2.0;
1003 } else { /* up */
1004 points[0] = tp;
1005 points[1] = (pointf){.x = uminx, .y = tp.y};
1006 points[2] = (pointf){.x = uminx, .y = maxy};
1007 points[3] = (pointf){.x = umaxx, .y = maxy};
1008 points[4] = (pointf){.x = umaxx, .y = hp.y};
1009 points[5] = hp;
1010 points[6] = (pointf){.x = hp.x, .y = maxy + LBL_SPACE};
1011 points[7] = (pointf){.x = tp.x, .y = maxy + LBL_SPACE};
1012 ctry = maxy + ED_label(e)->dimen.y / 2.0 + LBL_SPACE;
1013 maxy += ED_label(e)->dimen.y + LBL_SPACE;
1014 }
1015 poly.pn = 8;
1016 poly.ps = (Ppoint_t *)points;
1017 size_t pn;
1018 pointf *ps = simpleSplineRoute(tp, hp, poly, &pn, et == EDGETYPE_PLINE);
1019 if (ps == NULL || pn == 0) {
1020 free(ps);
1021 free(earray);
1022 return;
1023 }
1024 ED_label(e)->pos.x = ctrx;
1025 ED_label(e)->pos.y = ctry;
1026 ED_label(e)->set = true;
1027 clip_and_install(e, aghead(e), ps, pn, &sinfo);
1028 free(ps);
1029 }
1030
1031 /* edges with no labels */
1032 for (; i < cnt; i++) {
1033 e = earray[i];
1034 if (i % 2) { /* down */
1035 if (i == 1) {
1036 lminx = (2 * leftend + rightend) / 3.0;
1037 lmaxx = (leftend + 2 * rightend) / 3.0;
1038 }
1039 miny -= LBL_SPACE;
1040 points[0] = tp;
1041 points[1] = (pointf){.x = tp.x, .y = miny - LBL_SPACE};
1042 points[2] = (pointf){.x = hp.x, .y = points[1].y};
1043 points[3] = hp;
1044 points[4] = (pointf){.x = lmaxx, .y = hp.y};
1045 points[5] = (pointf){.x = lmaxx, .y = miny};
1046 points[6] = (pointf){.x = lminx, .y = miny};
1047 points[7] = (pointf){.x = lminx, .y = tp.y};
1048 } else { /* up */
1049 points[0] = tp;
1050 points[1] = (pointf){.x = uminx, .y = tp.y};
1051 points[2] = (pointf){.x = uminx, .y = maxy};
1052 points[3] = (pointf){.x = umaxx, .y = maxy};
1053 points[4] = (pointf){.x = umaxx, .y = hp.y};
1054 points[5] = hp;
1055 points[6] = (pointf){.x = hp.x, .y = maxy + LBL_SPACE};
1056 points[7] = (pointf){.x = tp.x, .y = maxy + LBL_SPACE};
1057 maxy += +LBL_SPACE;
1058 }
1059 poly.pn = 8;
1060 poly.ps = (Ppoint_t *)points;
1061 size_t pn;
1062 pointf *ps = simpleSplineRoute(tp, hp, poly, &pn, et == EDGETYPE_PLINE);
1063 if (ps == NULL || pn == 0) {
1064 free(ps);
1065 free(earray);
1066 return;
1067 }
1068 clip_and_install(e, aghead(e), ps, pn, &sinfo);
1069 free(ps);
1070 }
1071
1072 free(earray);
1073}
1074
1075static void makeSimpleFlat(node_t *tn, node_t *hn, edge_t **edges, unsigned cnt,
1076 int et) {
1077 edge_t *e = *edges;
1078 pointf points[10], tp, hp;
1079 double stepy, dy;
1080
1081 tp = add_pointf(ND_coord(tn), ED_tail_port(e).p);
1082 hp = add_pointf(ND_coord(hn), ED_head_port(e).p);
1083
1084 stepy = cnt > 1 ? ND_ht(tn) / (double)(cnt - 1) : 0.;
1085 dy = tp.y - (cnt > 1 ? ND_ht(tn) / 2. : 0.);
1086
1087 for (unsigned i = 0; i < cnt; i++) {
1088 e = edges[i];
1089 size_t pointn = 0;
1090 if (et == EDGETYPE_SPLINE || et == EDGETYPE_LINE) {
1091 points[pointn++] = tp;
1092 points[pointn++] = (pointf){(2 * tp.x + hp.x) / 3, dy};
1093 points[pointn++] = (pointf){(2 * hp.x + tp.x) / 3, dy};
1094 points[pointn++] = hp;
1095 } else { /* EDGETYPE_PLINE */
1096 points[pointn++] = tp;
1097 points[pointn++] = tp;
1098 points[pointn++] = (pointf){(2 * tp.x + hp.x) / 3, dy};
1099 points[pointn++] = (pointf){(2 * tp.x + hp.x) / 3, dy};
1100 points[pointn++] = (pointf){(2 * tp.x + hp.x) / 3, dy};
1101 points[pointn++] = (pointf){(2 * hp.x + tp.x) / 3, dy};
1102 points[pointn++] = (pointf){(2 * hp.x + tp.x) / 3, dy};
1103 points[pointn++] = (pointf){(2 * hp.x + tp.x) / 3, dy};
1104 points[pointn++] = hp;
1105 points[pointn++] = hp;
1106 }
1107 dy += stepy;
1108 clip_and_install(e, aghead(e), points, pointn, &sinfo);
1109 }
1110}
1111
1112/* In the simple case, with no labels or ports, this creates a simple
1113 * spindle of splines.
1114 * If there are only labels, cobble something together.
1115 * Otherwise, we run dot recursively on the 2 nodes and the edges,
1116 * essentially using rankdir=LR, to get the needed spline info.
1117 * This is probably to cute and fragile, and should be rewritten in a
1118 * more straightforward and laborious fashion.
1119 *
1120 * @return 0 on success
1121 */
1122static int make_flat_adj_edges(graph_t *g, edge_t **edges, unsigned cnt,
1123 edge_t *e0, int et) {
1124 node_t *n;
1125 node_t *tn, *hn;
1126 edge_t *e;
1127 graph_t *auxg;
1128 graph_t *subg;
1129 node_t *auxt, *auxh;
1130 edge_t *auxe;
1131 double midx, midy, leftx, rightx;
1132 pointf del;
1133 edge_t *hvye = NULL;
1134 static atomic_flag warned;
1135
1136 tn = agtail(e0), hn = aghead(e0);
1137 if (shapeOf(tn) == SH_RECORD || shapeOf(hn) == SH_RECORD) {
1138 if (!atomic_flag_test_and_set(&warned)) {
1139 agwarningf("flat edge between adjacent nodes one of which has a record "
1140 "shape - replace records with HTML-like labels\n");
1141 agerr(AGPREV, " Edge %s %s %s\n", agnameof(tn),
1142 agisdirected(g) ? "->" : "--", agnameof(hn));
1143 }
1144 return 0;
1145 }
1146 unsigned labels = 0;
1147 bool ports = false;
1148 for (unsigned i = 0; i < cnt; i++) {
1149 e = edges[i];
1150 if (ED_label(e))
1151 labels++;
1152 if (ED_tail_port(e).defined || ED_head_port(e).defined)
1153 ports = true;
1154 }
1155
1156 if (!ports) {
1157 /* flat edges without ports and labels can go straight left to right */
1158 if (labels == 0) {
1159 makeSimpleFlat(tn, hn, edges, cnt, et);
1160 }
1161 /* flat edges without ports but with labels take more work */
1162 else {
1163 makeSimpleFlatLabels(tn, hn, edges, cnt, et, labels);
1164 }
1165 return 0;
1166 }
1167
1168 attr_state_t attrs = {0};
1169 auxg = cloneGraph(g, &attrs);
1170 subg = agsubg(auxg, "xxx", 1);
1171 agbindrec(subg, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
1172 agset(subg, "rank", "source");
1173 rightx = ND_coord(hn).x;
1174 leftx = ND_coord(tn).x;
1175 if (GD_flip(g)) {
1176 SWAP(&tn, &hn);
1177 }
1178 auxt = cloneNode(subg, tn);
1179 auxh = cloneNode(auxg, hn);
1180 for (unsigned i = 0; i < cnt; i++) {
1181 e = edges[i];
1182 for (; ED_edge_type(e) != NORMAL; e = ED_to_orig(e))
1183 ;
1184 if (agtail(e) == tn)
1185 auxe = cloneEdge(auxg, auxt, auxh, e);
1186 else
1187 auxe = cloneEdge(auxg, auxh, auxt, e);
1188 ED_alg(e) = auxe;
1189 if (!hvye && !ED_tail_port(e).defined && !ED_head_port(e).defined) {
1190 hvye = auxe;
1191 ED_alg(hvye) = e;
1192 }
1193 }
1194 if (!hvye) {
1195 hvye = agedge(auxg, auxt, auxh, NULL, 1);
1196 }
1197 agxset(hvye, E_weight, "10000");
1198 GD_gvc(auxg) = GD_gvc(g);
1199 GD_dotroot(auxg) = auxg;
1200 setEdgeType(auxg, et);
1201 dot_init_node_edge(auxg);
1202
1203 dot_rank(auxg);
1204 const int r = dot_mincross(auxg);
1205 if (r != 0) {
1206 return r;
1207 }
1208 dot_position(auxg);
1209
1210 /* reposition */
1211 midx = (ND_coord(tn).x - ND_rw(tn) + ND_coord(hn).x + ND_lw(hn)) / 2;
1212 midy = (ND_coord(auxt).x + ND_coord(auxh).x) / 2;
1213 for (n = GD_nlist(auxg); n; n = ND_next(n)) {
1214 if (n == auxt) {
1215 ND_coord(n).y = rightx;
1216 ND_coord(n).x = midy;
1217 } else if (n == auxh) {
1218 ND_coord(n).y = leftx;
1219 ND_coord(n).x = midy;
1220 } else
1221 ND_coord(n).y = midx;
1222 }
1223 dot_sameports(auxg);
1224 const int rc = dot_splines_(auxg, 0);
1225 if (rc != 0) {
1226 return rc;
1227 }
1229
1230 /* copy splines */
1231 if (GD_flip(g)) {
1232 del.x = ND_coord(tn).x - ND_coord(auxt).y;
1233 del.y = ND_coord(tn).y + ND_coord(auxt).x;
1234 } else {
1235 del.x = ND_coord(tn).x - ND_coord(auxt).x;
1236 del.y = ND_coord(tn).y - ND_coord(auxt).y;
1237 }
1238 for (unsigned i = 0; i < cnt; i++) {
1239 bezier *auxbz;
1240 bezier *bz;
1241
1242 e = edges[i];
1243 for (; ED_edge_type(e) != NORMAL; e = ED_to_orig(e))
1244 ;
1245 auxe = ED_alg(e);
1246 if ((auxe == hvye) & !ED_alg(auxe))
1247 continue; /* pseudo-edge */
1248 auxbz = ED_spl(auxe)->list;
1249 bz = new_spline(e, auxbz->size);
1250 bz->sflag = auxbz->sflag;
1251 bz->sp = transformf(auxbz->sp, del, GD_flip(g));
1252 bz->eflag = auxbz->eflag;
1253 bz->ep = transformf(auxbz->ep, del, GD_flip(g));
1254 for (size_t j = 0; j < auxbz->size;) {
1255 pointf cp[4];
1256 cp[0] = bz->list[j] = transformf(auxbz->list[j], del, GD_flip(g));
1257 j++;
1258 if (j >= auxbz->size)
1259 break;
1260 cp[1] = bz->list[j] = transformf(auxbz->list[j], del, GD_flip(g));
1261 j++;
1262 cp[2] = bz->list[j] = transformf(auxbz->list[j], del, GD_flip(g));
1263 j++;
1264 cp[3] = transformf(auxbz->list[j], del, GD_flip(g));
1265 update_bb_bz(&GD_bb(g), cp);
1266 }
1267 if (ED_label(e)) {
1268 ED_label(e)->pos = transformf(ED_label(auxe)->pos, del, GD_flip(g));
1269 ED_label(e)->set = true;
1270 updateBB(g, ED_label(e));
1271 }
1272 }
1273
1274 cleanupCloneGraph(auxg, &attrs);
1275 return 0;
1276}
1277
1278static void makeFlatEnd(graph_t *g, const spline_info_t sp, path *P, node_t *n,
1279 edge_t *e, pathend_t *endp, bool isBegin) {
1280 boxf b = endp->nb = maximal_bbox(g, sp, n, NULL, e);
1281 endp->sidemask = TOP;
1282 if (isBegin)
1283 beginpath(P, e, FLATEDGE, endp, false);
1284 else
1285 endpath(P, e, FLATEDGE, endp, false);
1286 b.UR.y = endp->boxes[endp->boxn - 1].UR.y;
1287 b.LL.y = endp->boxes[endp->boxn - 1].LL.y;
1288 b = makeregularend(b, TOP, ND_coord(n).y + GD_rank(g)[ND_rank(n)].ht2);
1289 if (b.LL.x < b.UR.x && b.LL.y < b.UR.y)
1290 endp->boxes[endp->boxn++] = b;
1291}
1292
1293static void makeBottomFlatEnd(graph_t *g, const spline_info_t sp, path *P,
1294 node_t *n, edge_t *e, pathend_t *endp,
1295 bool isBegin) {
1296 boxf b = endp->nb = maximal_bbox(g, sp, n, NULL, e);
1297 endp->sidemask = BOTTOM;
1298 if (isBegin)
1299 beginpath(P, e, FLATEDGE, endp, false);
1300 else
1301 endpath(P, e, FLATEDGE, endp, false);
1302 b.UR.y = endp->boxes[endp->boxn - 1].UR.y;
1303 b.LL.y = endp->boxes[endp->boxn - 1].LL.y;
1304 b = makeregularend(b, BOTTOM, ND_coord(n).y - GD_rank(g)[ND_rank(n)].ht2);
1305 if (b.LL.x < b.UR.x && b.LL.y < b.UR.y)
1306 endp->boxes[endp->boxn++] = b;
1307}
1308
1310 edge_t *e, int et) {
1311 node_t *tn, *hn, *ln;
1312 pointf *ps;
1313 bool ps_needs_free = false;
1314 pathend_t tend, hend;
1315 boxf lb;
1316 int i;
1317 edge_t *f;
1318 pointf points[7];
1319
1320 tn = agtail(e);
1321 hn = aghead(e);
1322
1323 for (f = ED_to_virt(e); ED_to_virt(f); f = ED_to_virt(f))
1324 ;
1325 ln = agtail(f);
1326 ED_label(e)->pos = ND_coord(ln);
1327 ED_label(e)->set = true;
1328
1329 size_t pn;
1330 if (et == EDGETYPE_LINE) {
1331 pointf startp, endp, lp;
1332
1333 startp = add_pointf(ND_coord(tn), ED_tail_port(e).p);
1334 endp = add_pointf(ND_coord(hn), ED_head_port(e).p);
1335
1336 lp = ED_label(e)->pos;
1337 lp.y -= ED_label(e)->dimen.y / 2.0;
1338 points[1] = points[0] = startp;
1339 points[2] = points[3] = points[4] = lp;
1340 points[5] = points[6] = endp;
1341 ps = points;
1342 pn = 7;
1343 } else {
1344 lb.LL.x = ND_coord(ln).x - ND_lw(ln);
1345 lb.UR.x = ND_coord(ln).x + ND_rw(ln);
1346 lb.UR.y = ND_coord(ln).y + ND_ht(ln) / 2;
1347 double ydelta = ND_coord(ln).y - GD_rank(g)[ND_rank(tn)].ht1 -
1348 ND_coord(tn).y + GD_rank(g)[ND_rank(tn)].ht2;
1349 ydelta /= 6;
1350 lb.LL.y = lb.UR.y - MAX(5, ydelta);
1351
1352 makeFlatEnd(g, sp, P, tn, e, &tend, true);
1353 makeFlatEnd(g, sp, P, hn, e, &hend, false);
1354
1355 boxf boxes[] = {
1356 {
1357 .LL =
1358 {
1359 .x = tend.boxes[tend.boxn - 1].LL.x,
1360 .y = tend.boxes[tend.boxn - 1].UR.y,
1361 },
1362 .UR = lb.LL,
1363 },
1364 {
1365 .LL =
1366 {
1367 .x = tend.boxes[tend.boxn - 1].LL.x,
1368 .y = lb.LL.y,
1369 },
1370 .UR =
1371 {
1372 .x = hend.boxes[hend.boxn - 1].UR.x,
1373 .y = lb.UR.y,
1374 },
1375 },
1376 {
1377 .LL =
1378 {
1379 .x = lb.UR.x,
1380 .y = hend.boxes[hend.boxn - 1].UR.y,
1381 },
1382 .UR =
1383 {
1384 .x = hend.boxes[hend.boxn - 1].UR.x,
1385 .y = lb.LL.y,
1386 },
1387 },
1388 };
1389 const size_t boxn = sizeof(boxes) / sizeof(boxes[0]);
1390
1391 for (i = 0; i < tend.boxn; i++)
1392 add_box(P, tend.boxes[i]);
1393 for (size_t j = 0; j < boxn; j++)
1394 add_box(P, boxes[j]);
1395 for (i = hend.boxn - 1; i >= 0; i--)
1396 add_box(P, hend.boxes[i]);
1397
1398 ps_needs_free = true;
1399 if (et == EDGETYPE_SPLINE)
1400 ps = routesplines(P, &pn);
1401 else
1402 ps = routepolylines(P, &pn);
1403 if (pn == 0) {
1404 free(ps);
1405 return;
1406 }
1407 }
1408 clip_and_install(e, aghead(e), ps, pn, &sinfo);
1409 if (ps_needs_free)
1410 free(ps);
1411}
1412
1414 edge_t **edges, unsigned cnt, edge_t *e,
1415 bool use_splines) {
1416 node_t *tn, *hn;
1417 int j, r;
1418 double stepx, stepy, vspace;
1419 rank_t *nextr;
1420 pathend_t tend, hend;
1421
1422 tn = agtail(e);
1423 hn = aghead(e);
1424 r = ND_rank(tn);
1425 if (r < GD_maxrank(g)) {
1426 nextr = GD_rank(g) + (r + 1);
1427 vspace = ND_coord(tn).y - GD_rank(g)[r].pht1 -
1428 (ND_coord(nextr->v[0]).y + nextr->pht2);
1429 } else {
1430 vspace = GD_ranksep(g);
1431 }
1432 stepx = sp.Multisep / (cnt + 1);
1433 stepy = vspace / (cnt + 1);
1434
1435 makeBottomFlatEnd(g, sp, P, tn, e, &tend, true);
1436 makeBottomFlatEnd(g, sp, P, hn, e, &hend, false);
1437
1438 for (unsigned i = 0; i < cnt; i++) {
1439 boxf b;
1440 e = edges[i];
1441 size_t boxn = 0;
1442
1443 boxf boxes[3];
1444
1445 b = tend.boxes[tend.boxn - 1];
1446 boxes[boxn].LL.x = b.LL.x;
1447 boxes[boxn].UR.y = b.LL.y;
1448 boxes[boxn].UR.x = b.UR.x + (i + 1) * stepx;
1449 boxes[boxn].LL.y = b.LL.y - (i + 1) * stepy;
1450 boxn++;
1451 boxes[boxn].LL.x = tend.boxes[tend.boxn - 1].LL.x;
1452 boxes[boxn].UR.y = boxes[boxn - 1].LL.y;
1453 boxes[boxn].UR.x = hend.boxes[hend.boxn - 1].UR.x;
1454 boxes[boxn].LL.y = boxes[boxn].UR.y - stepy;
1455 boxn++;
1456 b = hend.boxes[hend.boxn - 1];
1457 boxes[boxn].UR.x = b.UR.x;
1458 boxes[boxn].UR.y = b.LL.y;
1459 boxes[boxn].LL.x = b.LL.x - (i + 1) * stepx;
1460 boxes[boxn].LL.y = boxes[boxn - 1].UR.y;
1461 boxn++;
1462 assert(boxn == sizeof(boxes) / sizeof(boxes[0]));
1463
1464 for (j = 0; j < tend.boxn; j++)
1465 add_box(P, tend.boxes[j]);
1466 for (size_t k = 0; k < boxn; k++)
1467 add_box(P, boxes[k]);
1468 for (j = hend.boxn - 1; j >= 0; j--)
1469 add_box(P, hend.boxes[j]);
1470
1471 pointf *ps = NULL;
1472 size_t pn = 0;
1473 if (use_splines)
1474 ps = routesplines(P, &pn);
1475 else
1476 ps = routepolylines(P, &pn);
1477 if (pn == 0) {
1478 free(ps);
1479 return;
1480 }
1481 clip_and_install(e, aghead(e), ps, pn, &sinfo);
1482 free(ps);
1483 P->nbox = 0;
1484 }
1485}
1486
1487/* Construct flat edges edges[ind...ind+cnt-1]
1488 * There are 4 main cases:
1489 * - all edges between a and b where a and b are adjacent
1490 * - one labeled edge
1491 * - all non-labeled edges with identical ports between non-adjacent a and b
1492 * = connecting bottom to bottom/left/right - route along bottom
1493 * = the rest - route along top
1494 *
1495 * @return 0 on success
1496 */
1497static int make_flat_edge(graph_t *g, const spline_info_t sp, path *P,
1498 edge_t **edges, unsigned cnt, int et) {
1499 Agedgeinfo_t fwdedgei;
1500 Agedgepair_t fwdedge;
1501 int j;
1502 double stepx, stepy, vspace;
1503 pathend_t tend, hend;
1504
1505 fwdedge.out.base.data = &fwdedgei.hdr;
1506
1507 /* Get sample edge; normalize to go from left to right */
1508 edge_t *e = *edges;
1509 bool isAdjacent = ED_adjacent(e) != 0;
1510 if (ED_tree_index(e) & BWDEDGE) {
1511 makefwdedge(&fwdedge.out, e);
1512 e = &fwdedge.out;
1513 }
1514 for (unsigned i = 1; i < cnt; i++) {
1515 if (ED_adjacent(edges[i])) {
1516 isAdjacent = true;
1517 break;
1518 }
1519 }
1520 // The lead edge edges[0] might not have been marked earlier as adjacent, so
1521 // check them all.
1522 if (isAdjacent) {
1523 return make_flat_adj_edges(g, edges, cnt, e, et);
1524 }
1525 if (ED_label(e)) { /* edges with labels aren't multi-edges */
1526 make_flat_labeled_edge(g, sp, P, e, et);
1527 return 0;
1528 }
1529
1530 if (et == EDGETYPE_LINE) {
1531 makeSimpleFlat(agtail(e), aghead(e), edges, cnt, et);
1532 return 0;
1533 }
1534
1535 const int tside = ED_tail_port(e).side;
1536 const int hside = ED_head_port(e).side;
1537 if ((tside == BOTTOM && hside != TOP) || (hside == BOTTOM && tside != TOP)) {
1538 make_flat_bottom_edges(g, sp, P, edges, cnt, e, et == EDGETYPE_SPLINE);
1539 return 0;
1540 }
1541
1542 node_t *tn = agtail(e);
1543 node_t *hn = aghead(e);
1544 const int r = ND_rank(tn);
1545 if (r > 0) {
1546 rank_t *prevr;
1547 if (GD_has_labels(g->root) & EDGE_LABEL)
1548 prevr = GD_rank(g) + (r - 2);
1549 else
1550 prevr = GD_rank(g) + (r - 1);
1551 vspace = ND_coord(prevr->v[0]).y - prevr->ht1 - ND_coord(tn).y -
1552 GD_rank(g)[r].ht2;
1553 } else {
1554 vspace = GD_ranksep(g);
1555 }
1556 stepx = sp.Multisep / (cnt + 1);
1557 stepy = vspace / (cnt + 1);
1558
1559 makeFlatEnd(g, sp, P, tn, e, &tend, true);
1560 makeFlatEnd(g, sp, P, hn, e, &hend, false);
1561
1562 for (unsigned i = 0; i < cnt; i++) {
1563 boxf b;
1564 e = edges[i];
1565 size_t boxn = 0;
1566
1567 boxf boxes[3];
1568
1569 b = tend.boxes[tend.boxn - 1];
1570 boxes[boxn].LL.x = b.LL.x;
1571 boxes[boxn].LL.y = b.UR.y;
1572 boxes[boxn].UR.x = b.UR.x + (i + 1) * stepx;
1573 boxes[boxn].UR.y = b.UR.y + (i + 1) * stepy;
1574 boxn++;
1575 boxes[boxn].LL.x = tend.boxes[tend.boxn - 1].LL.x;
1576 boxes[boxn].LL.y = boxes[boxn - 1].UR.y;
1577 boxes[boxn].UR.x = hend.boxes[hend.boxn - 1].UR.x;
1578 boxes[boxn].UR.y = boxes[boxn].LL.y + stepy;
1579 boxn++;
1580 b = hend.boxes[hend.boxn - 1];
1581 boxes[boxn].UR.x = b.UR.x;
1582 boxes[boxn].LL.y = b.UR.y;
1583 boxes[boxn].LL.x = b.LL.x - (i + 1) * stepx;
1584 boxes[boxn].UR.y = boxes[boxn - 1].LL.y;
1585 boxn++;
1586 assert(boxn == sizeof(boxes) / sizeof(boxes[0]));
1587
1588 for (j = 0; j < tend.boxn; j++)
1589 add_box(P, tend.boxes[j]);
1590 for (size_t k = 0; k < boxn; k++)
1591 add_box(P, boxes[k]);
1592 for (j = hend.boxn - 1; j >= 0; j--)
1593 add_box(P, hend.boxes[j]);
1594
1595 pointf *ps = NULL;
1596 size_t pn = 0;
1597 if (et == EDGETYPE_SPLINE)
1598 ps = routesplines(P, &pn);
1599 else
1600 ps = routepolylines(P, &pn);
1601 if (pn == 0) {
1602 free(ps);
1603 return 0;
1604 }
1605 clip_and_install(e, aghead(e), ps, pn, &sinfo);
1606 free(ps);
1607 P->nbox = 0;
1608 }
1609 return 0;
1610}
1611
1613static bool leftOf(pointf p1, pointf p2, pointf p3) {
1614 return (p1.y - p2.y) * (p3.x - p2.x) - (p3.y - p2.y) * (p1.x - p2.x) > 0;
1615}
1616
1617/* Create an edge as line segment. We guarantee that the points
1618 * are always drawn downwards. This means that for flipped edges,
1619 * we draw from the head to the tail. The routine returns the
1620 * end node of the edge in *hp. The points are stored in the
1621 * given array of points, and the number of points is returned.
1622 *
1623 * If the edge has a label, the edge is draw as two segments, with
1624 * the bend near the label.
1625 *
1626 * If the endpoints are on adjacent ranks, revert to usual code by
1627 * returning 0.
1628 * This is done because the usual code handles the interaction of
1629 * multiple edges better.
1630 */
1631static int makeLineEdge(graph_t *g, edge_t *fe, points_t *points, node_t **hp) {
1632 int delr, pn;
1633 node_t *hn;
1634 node_t *tn;
1635 edge_t *e = fe;
1636 pointf startp, endp, lp;
1637 pointf dimen;
1638 double width, height;
1639
1640 while (ED_edge_type(e) != NORMAL)
1641 e = ED_to_orig(e);
1642 hn = aghead(e);
1643 tn = agtail(e);
1644 delr = abs(ND_rank(hn) - ND_rank(tn));
1645 if (delr == 1 || (delr == 2 && (GD_has_labels(g->root) & EDGE_LABEL)))
1646 return 0;
1647 if (agtail(fe) == agtail(e)) {
1648 *hp = hn;
1649 startp = add_pointf(ND_coord(tn), ED_tail_port(e).p);
1650 endp = add_pointf(ND_coord(hn), ED_head_port(e).p);
1651 } else {
1652 *hp = tn;
1653 startp = add_pointf(ND_coord(hn), ED_head_port(e).p);
1654 endp = add_pointf(ND_coord(tn), ED_tail_port(e).p);
1655 }
1656
1657 if (ED_label(e)) {
1658 dimen = ED_label(e)->dimen;
1659 if (GD_flip(agraphof(hn))) {
1660 width = dimen.y;
1661 height = dimen.x;
1662 } else {
1663 width = dimen.x;
1664 height = dimen.y;
1665 }
1666
1667 lp = ED_label(e)->pos;
1668 if (leftOf(endp, startp, lp)) {
1669 lp.x += width / 2.0;
1670 lp.y -= height / 2.0;
1671 } else {
1672 lp.x -= width / 2.0;
1673 lp.y += height / 2.0;
1674 }
1675
1676 LIST_APPEND(points, startp);
1677 LIST_APPEND(points, startp);
1678 LIST_APPEND(points, lp);
1679 LIST_APPEND(points, lp);
1680 LIST_APPEND(points, lp);
1681 LIST_APPEND(points, endp);
1682 LIST_APPEND(points, endp);
1683 pn = 7;
1684 } else {
1685 LIST_APPEND(points, startp);
1686 LIST_APPEND(points, startp);
1687 LIST_APPEND(points, endp);
1688 LIST_APPEND(points, endp);
1689 pn = 4;
1690 }
1691
1692 return pn;
1693}
1694
1696 edge_t **edges, unsigned cnt, int et) {
1697 node_t *tn, *hn;
1698 Agedgeinfo_t fwdedgeai, fwdedgebi, fwdedgei;
1699 Agedgepair_t fwdedgea, fwdedgeb, fwdedge;
1700 edge_t *e, *fe, *le, *segfirst;
1701 pathend_t tend, hend;
1702 boxf b;
1703 int sl;
1704 points_t pointfs = {0};
1705 points_t pointfs2 = {0};
1706
1707 fwdedgea.out.base.data = &fwdedgeai.hdr;
1708 fwdedgeb.out.base.data = &fwdedgebi.hdr;
1709 fwdedge.out.base.data = &fwdedgei.hdr;
1710
1711 sl = 0;
1712 e = *edges;
1713 bool hackflag = false;
1714 if (abs(ND_rank(agtail(e)) - ND_rank(aghead(e))) > 1) {
1715 fwdedgeai = *(Agedgeinfo_t *)((uintptr_t)e->base.data -
1716 offsetof(Agedgeinfo_t, hdr));
1717 fwdedgea.out = *e;
1718 fwdedgea.in = *AGOUT2IN(e);
1719 fwdedgea.out.base.data = &fwdedgeai.hdr;
1720 if (ED_tree_index(e) & BWDEDGE) {
1721 makefwdedge(&fwdedgeb.out, e);
1722 agtail(&fwdedgea.out) = aghead(e);
1723 ED_tail_port(&fwdedgea.out) = ED_head_port(e);
1724 } else {
1725 fwdedgebi = *(Agedgeinfo_t *)((uintptr_t)e->base.data -
1726 offsetof(Agedgeinfo_t, hdr));
1727 fwdedgeb.out = *e;
1728 fwdedgeb.out.base.data = &fwdedgebi.hdr;
1729 agtail(&fwdedgea.out) = agtail(e);
1730 fwdedgeb.in = *AGOUT2IN(e);
1731 }
1732 le = getmainedge(e);
1733 while (ED_to_virt(le))
1734 le = ED_to_virt(le);
1735 aghead(&fwdedgea.out) = aghead(le);
1736 ED_head_port(&fwdedgea.out).defined = false;
1737 ED_edge_type(&fwdedgea.out) = VIRTUAL;
1738 ED_head_port(&fwdedgea.out).p.x = ED_head_port(&fwdedgea.out).p.y = 0;
1739 ED_to_orig(&fwdedgea.out) = e;
1740 e = &fwdedgea.out;
1741 hackflag = true;
1742 } else {
1743 if (ED_tree_index(e) & BWDEDGE) {
1744 makefwdedge(&fwdedgea.out, e);
1745 e = &fwdedgea.out;
1746 }
1747 }
1748 fe = e;
1749
1750 /* compute the spline points for the edge */
1751
1752 if (et == EDGETYPE_LINE && makeLineEdge(g, fe, &pointfs, &hn)) {
1753 } else {
1754 bool is_spline = et == EDGETYPE_SPLINE;
1755 boxes_t boxes = {0};
1756 segfirst = e;
1757 tn = agtail(e);
1758 hn = aghead(e);
1759 b = tend.nb = maximal_bbox(g, *sp, tn, NULL, e);
1760 beginpath(P, e, REGULAREDGE, &tend, spline_merge(tn));
1761 b.UR.y = tend.boxes[tend.boxn - 1].UR.y;
1762 b.LL.y = tend.boxes[tend.boxn - 1].LL.y;
1763 b = makeregularend(b, BOTTOM, ND_coord(tn).y - GD_rank(g)[ND_rank(tn)].ht1);
1764 if (b.LL.x < b.UR.x && b.LL.y < b.UR.y)
1765 tend.boxes[tend.boxn++] = b;
1766 bool smode = false;
1767 bool si = false;
1768 while (ND_node_type(hn) == VIRTUAL && !sinfo.splineMerge(hn)) {
1769 LIST_APPEND(&boxes, rank_box(sp, g, ND_rank(tn)));
1770 if (!smode && ((sl = straight_len(hn)) >=
1771 ((GD_has_labels(g->root) & EDGE_LABEL) ? 4 + 1 : 2 + 1))) {
1772 smode = true;
1773 si = true;
1774 sl -= 2;
1775 }
1776 if (!smode || si) {
1777 si = false;
1778 LIST_APPEND(&boxes, maximal_bbox(g, *sp, hn, e, ND_out(hn).list[0]));
1779 e = ND_out(hn).list[0];
1780 tn = agtail(e);
1781 hn = aghead(e);
1782 continue;
1783 }
1784 hend.nb = maximal_bbox(g, *sp, hn, e, ND_out(hn).list[0]);
1785 endpath(P, e, REGULAREDGE, &hend, spline_merge(aghead(e)));
1786 b = makeregularend(hend.boxes[hend.boxn - 1], TOP,
1787 ND_coord(hn).y + GD_rank(g)[ND_rank(hn)].ht2);
1788 if (b.LL.x < b.UR.x && b.LL.y < b.UR.y)
1789 hend.boxes[hend.boxn++] = b;
1790 P->end.theta = M_PI / 2, P->end.constrained = true;
1791 completeregularpath(P, segfirst, e, &tend, &hend, &boxes);
1792 pointf *ps = NULL;
1793 size_t pn = 0;
1794 if (is_spline)
1795 ps = routesplines(P, &pn);
1796 else {
1797 ps = routepolylines(P, &pn);
1798 if (et == EDGETYPE_LINE && pn > 4) {
1799 ps[1] = ps[0];
1800 ps[3] = ps[2] = ps[pn - 1];
1801 pn = 4;
1802 }
1803 }
1804 if (pn == 0) {
1805 free(ps);
1806 LIST_FREE(&boxes);
1807 LIST_FREE(&pointfs);
1808 LIST_FREE(&pointfs2);
1809 return;
1810 }
1811
1812 for (size_t i = 0; i < pn; i++) {
1813 LIST_APPEND(&pointfs, ps[i]);
1814 }
1815 free(ps);
1816 e = straight_path(ND_out(hn).list[0], sl, &pointfs);
1817 recover_slack(segfirst, P);
1818 segfirst = e;
1819 tn = agtail(e);
1820 hn = aghead(e);
1821 LIST_CLEAR(&boxes);
1822 tend.nb = maximal_bbox(g, *sp, tn, ND_in(tn).list[0], e);
1823 beginpath(P, e, REGULAREDGE, &tend, spline_merge(tn));
1824 b = makeregularend(tend.boxes[tend.boxn - 1], BOTTOM,
1825 ND_coord(tn).y - GD_rank(g)[ND_rank(tn)].ht1);
1826 if (b.LL.x < b.UR.x && b.LL.y < b.UR.y)
1827 tend.boxes[tend.boxn++] = b;
1828 P->start.theta = -M_PI / 2, P->start.constrained = true;
1829 smode = false;
1830 }
1831 LIST_APPEND(&boxes, rank_box(sp, g, ND_rank(tn)));
1832 b = hend.nb = maximal_bbox(g, *sp, hn, e, NULL);
1833 endpath(P, hackflag ? &fwdedgeb.out : e, REGULAREDGE, &hend,
1834 spline_merge(aghead(e)));
1835 b.UR.y = hend.boxes[hend.boxn - 1].UR.y;
1836 b.LL.y = hend.boxes[hend.boxn - 1].LL.y;
1837 b = makeregularend(b, TOP, ND_coord(hn).y + GD_rank(g)[ND_rank(hn)].ht2);
1838 if (b.LL.x < b.UR.x && b.LL.y < b.UR.y)
1839 hend.boxes[hend.boxn++] = b;
1840 completeregularpath(P, segfirst, e, &tend, &hend, &boxes);
1841 LIST_FREE(&boxes);
1842 pointf *ps = NULL;
1843 size_t pn = 0;
1844 if (is_spline)
1845 ps = routesplines(P, &pn);
1846 else
1847 ps = routepolylines(P, &pn);
1848 if (et == EDGETYPE_LINE && pn > 4) {
1849 /* Here we have used the polyline case to handle
1850 * an edge between two nodes on adjacent ranks. If the
1851 * results really is a polyline, straighten it.
1852 */
1853 ps[1] = ps[0];
1854 ps[3] = ps[2] = ps[pn - 1];
1855 pn = 4;
1856 }
1857 if (pn == 0) {
1858 free(ps);
1859 LIST_FREE(&pointfs);
1860 LIST_FREE(&pointfs2);
1861 return;
1862 }
1863 for (size_t i = 0; i < pn; i++) {
1864 LIST_APPEND(&pointfs, ps[i]);
1865 }
1866 free(ps);
1867 recover_slack(segfirst, P);
1868 hn = hackflag ? aghead(&fwdedgeb.out) : aghead(e);
1869 }
1870
1871 /* make copies of the spline points, one per multi-edge */
1872
1873 if (cnt == 1) {
1874 LIST_SYNC(&pointfs);
1875 clip_and_install(fe, hn, LIST_FRONT(&pointfs), LIST_SIZE(&pointfs), &sinfo);
1876 LIST_FREE(&pointfs);
1877 LIST_FREE(&pointfs2);
1878 return;
1879 }
1880 const double dx = sp->Multisep * (cnt - 1) / 2;
1881 for (size_t k = 1; k + 1 < LIST_SIZE(&pointfs); k++)
1882 LIST_AT(&pointfs, k)->x -= dx;
1883
1884 for (size_t k = 0; k < LIST_SIZE(&pointfs); k++)
1885 LIST_APPEND(&pointfs2, LIST_GET(&pointfs, k));
1886 LIST_SYNC(&pointfs2);
1887 clip_and_install(fe, hn, LIST_FRONT(&pointfs2), LIST_SIZE(&pointfs2), &sinfo);
1888 for (unsigned j = 1; j < cnt; j++) {
1889 e = edges[j];
1890 if (ED_tree_index(e) & BWDEDGE) {
1891 makefwdedge(&fwdedge.out, e);
1892 e = &fwdedge.out;
1893 }
1894 for (size_t k = 1; k + 1 < LIST_SIZE(&pointfs); k++)
1895 LIST_AT(&pointfs, k)->x += sp->Multisep;
1896 LIST_CLEAR(&pointfs2);
1897 for (size_t k = 0; k < LIST_SIZE(&pointfs); k++)
1898 LIST_APPEND(&pointfs2, LIST_GET(&pointfs, k));
1899 LIST_SYNC(&pointfs2);
1900 clip_and_install(e, aghead(e), LIST_FRONT(&pointfs2), LIST_SIZE(&pointfs2),
1901 &sinfo);
1902 }
1903 LIST_FREE(&pointfs);
1904 LIST_FREE(&pointfs2);
1905}
1906
1907/* regular edges */
1908
1909static void completeregularpath(path *P, edge_t *first, edge_t *last,
1910 pathend_t *tendp, pathend_t *hendp,
1911 const boxes_t *boxes) {
1912 edge_t *uleft = top_bound(first, -1);
1913 edge_t *uright = top_bound(first, 1);
1914 if (uleft) {
1915 if (getsplinepoints(uleft) == NULL)
1916 return;
1917 }
1918 if (uright) {
1919 if (getsplinepoints(uright) == NULL)
1920 return;
1921 }
1922 edge_t *lleft = bot_bound(last, -1);
1923 edge_t *lright = bot_bound(last, 1);
1924 if (lleft) {
1925 if (getsplinepoints(lleft) == NULL)
1926 return;
1927 }
1928 if (lright) {
1929 if (getsplinepoints(lright) == NULL)
1930 return;
1931 }
1932 for (int i = 0; i < tendp->boxn; i++)
1933 add_box(P, tendp->boxes[i]);
1934 const size_t fb = P->nbox + 1;
1935 const size_t lb = fb + LIST_SIZE(boxes) - 3;
1936 for (size_t i = 0; i < LIST_SIZE(boxes); i++)
1937 add_box(P, LIST_GET(boxes, i));
1938 for (int i = hendp->boxn - 1; i >= 0; i--)
1939 add_box(P, hendp->boxes[i]);
1940 adjustregularpath(P, fb, lb);
1941}
1942
1943/* Add box to fill between node and interrank space. Needed because
1944 * nodes in a given rank can differ in height.
1945 * for now, regular edges always go from top to bottom
1946 */
1947static boxf makeregularend(boxf b, int side, double y) {
1948 assert(side == BOTTOM || side == TOP);
1949 if (side == BOTTOM) {
1950 return (boxf){{b.LL.x, y}, {b.UR.x, b.LL.y}};
1951 }
1952 return (boxf){{b.LL.x, b.UR.y}, {b.UR.x, y}};
1953}
1954
1955/* make sure the path is wide enough.
1956 * the % 2 was so that in rank boxes would only be grown if
1957 * they were == 0 while inter-rank boxes could be stretched to a min
1958 * width.
1959 * The list of boxes has three parts: tail boxes, path boxes, and head
1960 * boxes. (Note that because of back edges, the tail boxes might actually
1961 * belong to the head node, and vice versa.) fb is the index of the
1962 * first interrank path box and lb is the last interrank path box.
1963 * If fb > lb, there are none.
1964 *
1965 * The second for loop was added by ek long ago, and apparently is intended
1966 * to guarantee an overlap between adjacent boxes of at least MINW.
1967 * It doesn't do this.
1968 */
1969static void adjustregularpath(path *P, size_t fb, size_t lb) {
1970 boxf *bp1, *bp2;
1971
1972 for (size_t i = fb - 1; i < lb + 1; i++) {
1973 bp1 = &P->boxes[i];
1974 if ((i - fb) % 2 == 0) {
1975 if (bp1->LL.x >= bp1->UR.x) {
1976 double x = (bp1->LL.x + bp1->UR.x) / 2;
1977 bp1->LL.x = x - HALFMINW;
1978 bp1->UR.x = x + HALFMINW;
1979 }
1980 } else {
1981 if (bp1->LL.x + MINW > bp1->UR.x) {
1982 double x = (bp1->LL.x + bp1->UR.x) / 2;
1983 bp1->LL.x = x - HALFMINW;
1984 bp1->UR.x = x + HALFMINW;
1985 }
1986 }
1987 }
1988 for (size_t i = 0; i + 1 < P->nbox; i++) {
1989 bp1 = &P->boxes[i], bp2 = &P->boxes[i + 1];
1990 if (i >= fb && i <= lb && (i - fb) % 2 == 0) {
1991 if (bp1->LL.x + MINW > bp2->UR.x)
1992 bp2->UR.x = bp1->LL.x + MINW;
1993 if (bp1->UR.x - MINW < bp2->LL.x)
1994 bp2->LL.x = bp1->UR.x - MINW;
1995 } else if (i + 1 >= fb && i < lb && (i + 1 - fb) % 2 == 0) {
1996 if (bp1->LL.x + MINW > bp2->UR.x)
1997 bp1->LL.x = bp2->UR.x - MINW;
1998 if (bp1->UR.x - MINW < bp2->LL.x)
1999 bp1->UR.x = bp2->LL.x + MINW;
2000 }
2001 }
2002}
2003
2004static boxf rank_box(spline_info_t *sp, graph_t *g, int r) {
2005 boxf b = sp->Rank_box[r];
2006 if (b.LL.x == b.UR.x) {
2007 node_t *const left0 = GD_rank(g)[r].v[0];
2008 node_t *const left1 = GD_rank(g)[r + 1].v[0];
2009 b.LL.x = sp->LeftBound;
2010 b.LL.y = ND_coord(left1).y + GD_rank(g)[r + 1].ht2;
2011 b.UR.x = sp->RightBound;
2012 b.UR.y = ND_coord(left0).y - GD_rank(g)[r].ht1;
2013 sp->Rank_box[r] = b;
2014 }
2015 return b;
2016}
2017
2018/* returns count of vertically aligned edges starting at n */
2019static int straight_len(node_t *n) {
2020 int cnt = 0;
2021 node_t *v;
2022
2023 v = n;
2024 while (1) {
2025 v = aghead(ND_out(v).list[0]);
2026 if (ND_node_type(v) != VIRTUAL)
2027 break;
2028 if (ND_out(v).size != 1 || ND_in(v).size != 1)
2029 break;
2030 if (ND_coord(v).x != ND_coord(n).x)
2031 break;
2032 cnt++;
2033 }
2034 return cnt;
2035}
2036
2037static edge_t *straight_path(edge_t *e, int cnt, points_t *plist) {
2038 edge_t *f = e;
2039
2040 while (cnt--)
2041 f = ND_out(aghead(f)).list[0];
2042 assert(!LIST_IS_EMPTY(plist));
2043 LIST_APPEND(plist, LIST_GET(plist, LIST_SIZE(plist) - 1));
2044 LIST_APPEND(plist, LIST_GET(plist, LIST_SIZE(plist) - 1));
2045
2046 return f;
2047}
2048
2049static void recover_slack(edge_t *e, path *p) {
2050 node_t *vn;
2051
2052 size_t b = 0; // skip first rank box
2053 for (vn = aghead(e); ND_node_type(vn) == VIRTUAL && !sinfo.splineMerge(vn);
2054 vn = aghead(ND_out(vn).list[0])) {
2055 while (b < p->nbox && p->boxes[b].LL.y > ND_coord(vn).y)
2056 b++;
2057 if (b >= p->nbox)
2058 break;
2059 if (p->boxes[b].UR.y < ND_coord(vn).y)
2060 continue;
2061 if (ND_label(vn))
2062 resize_vn(vn, p->boxes[b].LL.x, p->boxes[b].UR.x,
2063 p->boxes[b].UR.x + ND_rw(vn));
2064 else
2065 resize_vn(vn, p->boxes[b].LL.x, (p->boxes[b].LL.x + p->boxes[b].UR.x) / 2,
2066 p->boxes[b].UR.x);
2067 }
2068}
2069
2070static void resize_vn(node_t *vn, double lx, double cx, double rx) {
2071 ND_coord(vn).x = cx;
2072 ND_lw(vn) = cx - lx, ND_rw(vn) = rx - cx;
2073}
2074
2075/* side > 0 means right. side < 0 means left */
2076static edge_t *top_bound(edge_t *e, int side) {
2077 edge_t *f, *ans = NULL;
2078 int i;
2079
2080 for (i = 0; (f = ND_out(agtail(e)).list[i]); i++) {
2081 if (side * (ND_order(aghead(f)) - ND_order(aghead(e))) <= 0)
2082 continue;
2083 if (ED_spl(f) == NULL &&
2084 (ED_to_orig(f) == NULL || ED_spl(ED_to_orig(f)) == NULL))
2085 continue;
2086 if (ans == NULL || side * (ND_order(aghead(ans)) - ND_order(aghead(f))) > 0)
2087 ans = f;
2088 }
2089 return ans;
2090}
2091
2092static edge_t *bot_bound(edge_t *e, int side) {
2093 edge_t *f, *ans = NULL;
2094 int i;
2095
2096 for (i = 0; (f = ND_in(aghead(e)).list[i]); i++) {
2097 if (side * (ND_order(agtail(f)) - ND_order(agtail(e))) <= 0)
2098 continue;
2099 if (ED_spl(f) == NULL &&
2100 (ED_to_orig(f) == NULL || ED_spl(ED_to_orig(f)) == NULL))
2101 continue;
2102 if (ans == NULL || side * (ND_order(agtail(ans)) - ND_order(agtail(f))) > 0)
2103 ans = f;
2104 }
2105 return ans;
2106}
2107
2108/* common routines */
2109
2110static bool cl_vninside(graph_t *cl, node_t *n) {
2111 return BETWEEN(GD_bb(cl).LL.x, ND_coord(n).x, GD_bb(cl).UR.x) &&
2112 BETWEEN(GD_bb(cl).LL.y, ND_coord(n).y, GD_bb(cl).UR.y);
2113}
2114
2115/* All nodes belong to some cluster, which may be the root graph.
2116 * For the following, we only want a cluster if it is a real cluster
2117 * It is not clear this will handle all potential problems. It seems one
2118 * could have hcl and tcl contained in cl, which would also cause problems.
2119 */
2120#define REAL_CLUSTER(n) (ND_clust(n) == g ? NULL : ND_clust(n))
2121
2122/* returns the cluster of (adj) that interferes with n,
2123 */
2124static Agraph_t *cl_bound(graph_t *g, node_t *n, node_t *adj) {
2125 graph_t *rv, *cl, *tcl, *hcl;
2126 edge_t *orig;
2127
2128 rv = NULL;
2129 if (ND_node_type(n) == NORMAL)
2130 tcl = hcl = ND_clust(n);
2131 else {
2132 orig = ED_to_orig(ND_out(n).list[0]);
2133 tcl = ND_clust(agtail(orig));
2134 hcl = ND_clust(aghead(orig));
2135 }
2136 if (ND_node_type(adj) == NORMAL) {
2137 cl = REAL_CLUSTER(adj);
2138 if (cl && cl != tcl && cl != hcl)
2139 rv = cl;
2140 } else {
2141 orig = ED_to_orig(ND_out(adj).list[0]);
2142 cl = REAL_CLUSTER(agtail(orig));
2143 if (cl && cl != tcl && cl != hcl && cl_vninside(cl, adj))
2144 rv = cl;
2145 else {
2146 cl = REAL_CLUSTER(aghead(orig));
2147 if (cl && cl != tcl && cl != hcl && cl_vninside(cl, adj))
2148 rv = cl;
2149 }
2150 }
2151 return rv;
2152}
2153
2154/* Return an initial bounding box to be used for building the
2155 * beginning or ending of the path of boxes.
2156 * Height reflects height of tallest node on rank.
2157 * The extra space provided by FUDGE allows begin/endpath to create a box
2158 * FUDGE-2 away from the node, so the routing can avoid the node and the
2159 * box is at least 2 wide.
2160 */
2161#define FUDGE 4
2162
2164 edge_t *ie, edge_t *oe) {
2165 double b, nb;
2166 graph_t *left_cl, *right_cl;
2167 node_t *left, *right;
2168 boxf rv;
2169
2170 left_cl = right_cl = NULL;
2171
2172 /* give this node all the available space up to its neighbors */
2173 b = (double)(ND_coord(vn).x - ND_lw(vn) - FUDGE);
2174 if ((left = neighbor(g, vn, ie, oe, -1))) {
2175 if ((left_cl = cl_bound(g, vn, left)))
2176 nb = GD_bb(left_cl).UR.x + sp.Splinesep;
2177 else {
2178 nb = (double)(ND_coord(left).x + ND_mval(left));
2179 if (ND_node_type(left) == NORMAL)
2180 nb += GD_nodesep(g) / 2.;
2181 else
2182 nb += sp.Splinesep;
2183 }
2184 if (nb < b)
2185 b = nb;
2186 rv.LL.x = round(b);
2187 } else
2188 rv.LL.x = fmin(round(b), sp.LeftBound);
2189
2190 /* we have to leave room for our own label! */
2191 if (ND_node_type(vn) == VIRTUAL && ND_label(vn))
2192 b = (double)(ND_coord(vn).x + 10);
2193 else
2194 b = (double)(ND_coord(vn).x + ND_rw(vn) + FUDGE);
2195 if ((right = neighbor(g, vn, ie, oe, 1))) {
2196 if ((right_cl = cl_bound(g, vn, right)))
2197 nb = GD_bb(right_cl).LL.x - sp.Splinesep;
2198 else {
2199 nb = ND_coord(right).x - ND_lw(right);
2200 if (ND_node_type(right) == NORMAL)
2201 nb -= GD_nodesep(g) / 2.;
2202 else
2203 nb -= sp.Splinesep;
2204 }
2205 if (nb > b)
2206 b = nb;
2207 rv.UR.x = round(b);
2208 } else
2209 rv.UR.x = fmax(round(b), sp.RightBound);
2210
2211 if (ND_node_type(vn) == VIRTUAL && ND_label(vn)) {
2212 rv.UR.x -= ND_rw(vn);
2213 if (rv.UR.x < rv.LL.x)
2214 rv.UR.x = ND_coord(vn).x;
2215 }
2216
2217 rv.LL.y = ND_coord(vn).y - GD_rank(g)[ND_rank(vn)].ht1;
2218 rv.UR.y = ND_coord(vn).y + GD_rank(g)[ND_rank(vn)].ht2;
2219 return rv;
2220}
2221
2222static node_t *neighbor(graph_t *g, node_t *vn, edge_t *ie, edge_t *oe,
2223 int dir) {
2224 int i;
2225 node_t *n, *rv = NULL;
2226 rank_t *rank = &(GD_rank(g)[ND_rank(vn)]);
2227
2228 for (i = ND_order(vn) + dir; i >= 0 && i < rank->n; i += dir) {
2229 n = rank->v[i];
2230 if (ND_node_type(n) == VIRTUAL && ND_label(n)) {
2231 rv = n;
2232 break;
2233 }
2234 if (ND_node_type(n) == NORMAL) {
2235 rv = n;
2236 break;
2237 }
2238 if (!pathscross(n, vn, ie, oe)) {
2239 rv = n;
2240 break;
2241 }
2242 }
2243 return rv;
2244}
2245
2246static bool pathscross(node_t *n0, node_t *n1, edge_t *ie1, edge_t *oe1) {
2247 edge_t *e0, *e1;
2248 node_t *na, *nb;
2249 int order, cnt;
2250
2251 order = ND_order(n0) > ND_order(n1);
2252 if (ND_out(n0).size != 1 && ND_out(n1).size != 1)
2253 return false;
2254 e1 = oe1;
2255 if (ND_out(n0).size == 1 && e1) {
2256 e0 = ND_out(n0).list[0];
2257 for (cnt = 0; cnt < 2; cnt++) {
2258 if ((na = aghead(e0)) == (nb = aghead(e1)))
2259 break;
2260 if (order != (ND_order(na) > ND_order(nb)))
2261 return true;
2262 if (ND_out(na).size != 1 || ND_node_type(na) == NORMAL)
2263 break;
2264 e0 = ND_out(na).list[0];
2265 if (ND_out(nb).size != 1 || ND_node_type(nb) == NORMAL)
2266 break;
2267 e1 = ND_out(nb).list[0];
2268 }
2269 }
2270 e1 = ie1;
2271 if (ND_in(n0).size == 1 && e1) {
2272 e0 = ND_in(n0).list[0];
2273 for (cnt = 0; cnt < 2; cnt++) {
2274 if ((na = agtail(e0)) == (nb = agtail(e1)))
2275 break;
2276 if (order != (ND_order(na) > ND_order(nb)))
2277 return true;
2278 if (ND_in(na).size != 1 || ND_node_type(na) == NORMAL)
2279 break;
2280 e0 = ND_in(na).list[0];
2281 if (ND_in(nb).size != 1 || ND_node_type(nb) == NORMAL)
2282 break;
2283 e1 = ND_in(nb).list[0];
2284 }
2285 }
2286 return false;
2287}
2288
2289#ifdef DEBUG
2290void showpath(path *p) {
2291 pointf LL, UR;
2292
2293 fprintf(stderr, "%%!PS\n");
2294 for (size_t i = 0; i < p->nbox; i++) {
2295 LL = p->boxes[i].LL;
2296 UR = p->boxes[i].UR;
2297 fprintf(stderr,
2298 "newpath %.04f %.04f moveto %.04f %.04f lineto %.04f %.04f lineto "
2299 "%.04f %.04f lineto closepath stroke\n",
2300 LL.x, LL.y, UR.x, LL.y, UR.x, UR.y, LL.x, UR.y);
2301 }
2302 fprintf(stderr, "showpage\n");
2303}
2304#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:333
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:944
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:937
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
#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:1161
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