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