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