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