| 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 |
#include "config.h" |
| 12 |
|
| 13 |
#include <assert.h> |
| 14 |
#include "config.h" |
| 15 |
#include <limits.h> |
| 16 |
#include <math.h> |
| 17 |
#include <neatogen/neato.h> |
| 18 |
#include <neatogen/adjust.h> |
| 19 |
#include <pathplan/pathplan.h> |
| 20 |
#include <pathplan/vispath.h> |
| 21 |
#include <neatogen/multispline.h> |
| 22 |
#include <stdbool.h> |
| 23 |
#include <stddef.h> |
| 24 |
#include <util/alloc.h> |
| 25 |
#include <util/gv_math.h> |
| 26 |
#include <util/unreachable.h> |
| 27 |
|
| 28 |
#ifdef ORTHO |
| 29 |
#include <ortho/ortho.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
|
| 33 |
static bool spline_merge(node_t * n) |
| 34 |
{ |
| 35 |
(void)n; |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
static bool swap_ends_p(edge_t * e) |
| 40 |
{ |
| 41 |
(void)e; |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
static splineInfo sinfo = {.swapEnds = swap_ends_p, |
| 46 |
.splineMerge = spline_merge}; |
| 47 |
|
| 48 |
static void make_barriers(Ppoly_t **poly, int npoly, int pp, int qp, |
| 49 |
Pedge_t **barriers, size_t *n_barriers) { |
| 50 |
int i, j, k; |
| 51 |
|
| 52 |
size_t n = 0; |
| 53 |
for (i = 0; i < npoly; i++) { |
| 54 |
if (i == pp) |
| 55 |
continue; |
| 56 |
if (i == qp) |
| 57 |
continue; |
| 58 |
n += poly[i]->pn; |
| 59 |
} |
| 60 |
Pedge_t *bar = gv_calloc(n, sizeof(Pedge_t)); |
| 61 |
size_t b = 0; |
| 62 |
for (i = 0; i < npoly; i++) { |
| 63 |
if (i == pp) |
| 64 |
continue; |
| 65 |
if (i == qp) |
| 66 |
continue; |
| 67 |
for (j = 0; j < (int)poly[i]->pn; j++) { |
| 68 |
k = j + 1; |
| 69 |
if (k >= (int)poly[i]->pn) |
| 70 |
k = 0; |
| 71 |
bar[b].a = poly[i]->ps[j]; |
| 72 |
bar[b].b = poly[i]->ps[k]; |
| 73 |
b++; |
| 74 |
} |
| 75 |
} |
| 76 |
assert(b == n); |
| 77 |
*barriers = bar; |
| 78 |
*n_barriers = n; |
| 79 |
} |
| 80 |
|
| 81 |
static Ppoint_t genPt(double x, double y, pointf c) |
| 82 |
{ |
| 83 |
Ppoint_t p; |
| 84 |
|
| 85 |
p.x = x + c.x; |
| 86 |
p.y = y + c.y; |
| 87 |
return p; |
| 88 |
} |
| 89 |
|
| 90 |
static Ppoint_t recPt(double x, double y, pointf c, expand_t* m) |
| 91 |
{ |
| 92 |
Ppoint_t p; |
| 93 |
|
| 94 |
p.x = x * m->x + c.x; |
| 95 |
p.y = y * m->y + c.y; |
| 96 |
return p; |
| 97 |
} |
| 98 |
|
| 99 |
typedef struct { |
| 100 |
node_t *n1; |
| 101 |
pointf p1; |
| 102 |
node_t *n2; |
| 103 |
pointf p2; |
| 104 |
} edgeinfo; |
| 105 |
typedef struct { |
| 106 |
Dtlink_t link; |
| 107 |
edgeinfo id; |
| 108 |
edge_t *e; |
| 109 |
} edgeitem; |
| 110 |
|
| 111 |
static void *newitem(void *p, Dtdisc_t *disc) { |
| 112 |
edgeitem *obj = p; |
| 113 |
edgeitem *newp; |
| 114 |
|
| 115 |
(void)disc; |
| 116 |
newp = gv_alloc(sizeof(edgeitem)); |
| 117 |
newp->id = obj->id; |
| 118 |
newp->e = obj->e; |
| 119 |
ED_count(newp->e) = 1; |
| 120 |
|
| 121 |
return newp; |
| 122 |
} |
| 123 |
|
| 124 |
static int cmpitems(void *k1, void *k2) { |
| 125 |
const edgeinfo *key1 = k1; |
| 126 |
const edgeinfo *key2 = k2; |
| 127 |
if (key1->n1 > key2->n1) |
| 128 |
return 1; |
| 129 |
if (key1->n1 < key2->n1) |
| 130 |
return -1; |
| 131 |
if (key1->n2 > key2->n2) |
| 132 |
return 1; |
| 133 |
if (key1->n2 < key2->n2) |
| 134 |
return -1; |
| 135 |
|
| 136 |
if (key1->p1.x > key2->p1.x) |
| 137 |
return 1; |
| 138 |
if (key1->p1.x < key2->p1.x) |
| 139 |
return -1; |
| 140 |
if (key1->p1.y > key2->p1.y) |
| 141 |
return 1; |
| 142 |
if (key1->p1.y < key2->p1.y) |
| 143 |
return -1; |
| 144 |
if (key1->p2.x > key2->p2.x) |
| 145 |
return 1; |
| 146 |
if (key1->p2.x < key2->p2.x) |
| 147 |
return -1; |
| 148 |
if (key1->p2.y > key2->p2.y) |
| 149 |
return 1; |
| 150 |
if (key1->p2.y < key2->p2.y) |
| 151 |
return -1; |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
Dtdisc_t edgeItemDisc = { |
| 156 |
offsetof(edgeitem, id), |
| 157 |
sizeof(edgeinfo), |
| 158 |
offsetof(edgeitem, link), |
| 159 |
newitem, |
| 160 |
free, |
| 161 |
cmpitems, |
| 162 |
}; |
| 163 |
|
| 164 |
/* See if we have already encountered an edge between the same |
| 165 |
* node:port pairs. If so, return the earlier edge. If not, |
| 166 |
* this edge is added to map and returned. |
| 167 |
* We first have to canonicalize the key fields using a lexicographic |
| 168 |
* ordering. |
| 169 |
*/ |
| 170 |
static edge_t *equivEdge(Dt_t * map, edge_t * e) |
| 171 |
{ |
| 172 |
edgeinfo test; |
| 173 |
edgeitem dummy; |
| 174 |
edgeitem *ip; |
| 175 |
|
| 176 |
if (agtail(e) < aghead(e)) { |
| 177 |
test.n1 = agtail(e); |
| 178 |
test.p1 = ED_tail_port(e).p; |
| 179 |
test.n2 = aghead(e); |
| 180 |
test.p2 = ED_head_port(e).p; |
| 181 |
} else if (agtail(e) > aghead(e)) { |
| 182 |
test.n2 = agtail(e); |
| 183 |
test.p2 = ED_tail_port(e).p; |
| 184 |
test.n1 = aghead(e); |
| 185 |
test.p1 = ED_head_port(e).p; |
| 186 |
} else { |
| 187 |
pointf hp = ED_head_port(e).p; |
| 188 |
pointf tp = ED_tail_port(e).p; |
| 189 |
if (tp.x < hp.x) { |
| 190 |
test.p1 = tp; |
| 191 |
test.p2 = hp; |
| 192 |
} else if (tp.x > hp.x) { |
| 193 |
test.p1 = hp; |
| 194 |
test.p2 = tp; |
| 195 |
} else if (tp.y < hp.y) { |
| 196 |
test.p1 = tp; |
| 197 |
test.p2 = hp; |
| 198 |
} else if (tp.y > hp.y) { |
| 199 |
test.p1 = hp; |
| 200 |
test.p2 = tp; |
| 201 |
} else { |
| 202 |
test.p1 = test.p2 = tp; |
| 203 |
} |
| 204 |
test.n2 = test.n1 = agtail(e); |
| 205 |
} |
| 206 |
dummy.id = test; |
| 207 |
dummy.e = e; |
| 208 |
ip = dtinsert(map, &dummy); |
| 209 |
return ip->e; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
/* Generate loops. We use the library routine makeSelfEdge |
| 214 |
* which also places the labels. |
| 215 |
* We have to handle port labels here. |
| 216 |
* as well as update the bbox from edge labels. |
| 217 |
*/ |
| 218 |
void makeSelfArcs(edge_t * e, int stepx) |
| 219 |
{ |
| 220 |
assert(ED_count(e) >= 0); |
| 221 |
const size_t cnt = (size_t)ED_count(e); |
| 222 |
|
| 223 |
if (cnt == 1 || Concentrate) { |
| 224 |
edge_t *edges1[1]; |
| 225 |
edges1[0] = e; |
| 226 |
makeSelfEdge(edges1, 1, stepx, stepx, &sinfo); |
| 227 |
if (ED_label(e)) |
| 228 |
updateBB(agraphof(agtail(e)), ED_label(e)); |
| 229 |
makePortLabels(e); |
| 230 |
} else if (cnt > 1) { |
| 231 |
edge_t **edges = gv_calloc(cnt, sizeof(edge_t*)); |
| 232 |
for (size_t i = 0; i < cnt; i++) { |
| 233 |
edges[i] = e; |
| 234 |
e = ED_to_virt(e); |
| 235 |
} |
| 236 |
makeSelfEdge(edges, cnt, stepx, stepx, &sinfo); |
| 237 |
for (size_t i = 0; i < cnt; i++) { |
| 238 |
e = edges[i]; |
| 239 |
if (ED_label(e)) |
| 240 |
updateBB(agraphof(agtail(e)), ED_label(e)); |
| 241 |
makePortLabels(e); |
| 242 |
} |
| 243 |
free(edges); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
/** calculate the slope of the tangent of an ellipse |
| 248 |
* |
| 249 |
* The equation for the slope `m` of the tangent of an ellipse as a function of |
| 250 |
* `x` * is given by: |
| 251 |
* |
| 252 |
* bx |
| 253 |
* m = ± ―――――――――― |
| 254 |
* _______ |
| 255 |
* a √ a²- x² |
| 256 |
* |
| 257 |
* or |
| 258 |
* |
| 259 |
* m = ± (b * x) / (a * sqrt(a * a - x * x)) |
| 260 |
* |
| 261 |
* We know that the slope is negative in the first and third quadrant, i.e., |
| 262 |
* when the signs of x and y are the same, so we use that to select the correct |
| 263 |
* slope. |
| 264 |
* |
| 265 |
* @param a Half the width of the ellipse, i.e., the maximum x value |
| 266 |
* @param b Half the height of the ellipse, i.e., the maximum y value |
| 267 |
* @param p A point on the ellipse periphery in which to calculate the slope of |
| 268 |
* the tangent |
| 269 |
* @return The slope of the tangent in point `p` |
| 270 |
*/ |
| 271 |
static double ellipse_tangent_slope(double a, double b, pointf p) { |
| 272 |
assert(p.x != a && |
| 273 |
"cannot handle ellipse tangent slope in horizontal extreme point"); |
| 274 |
const double sign_y = p.y >= 0 ? 1 : -1; |
| 275 |
const double m = -sign_y * (b * p.x) / (a * sqrt(a * a - p.x * p.x)); |
| 276 |
assert(isfinite(m) && "ellipse tangent slope is infinite"); |
| 277 |
return m; |
| 278 |
} |
| 279 |
|
| 280 |
/** calculate the intersection of two lines |
| 281 |
* |
| 282 |
* @param l0 First line |
| 283 |
* @param l1 Second line |
| 284 |
* @return Intersection of the two lines |
| 285 |
*/ |
| 286 |
static pointf line_intersection(linef l0, linef l1) { |
| 287 |
const double x = |
| 288 |
(l0.m * l0.p.x - l0.p.y - l1.m * l1.p.x + l1.p.y) / (l0.m - l1.m); |
| 289 |
const double y = l0.p.y + l0.m * (x - l0.p.x); |
| 290 |
return (pointf){x, y}; |
| 291 |
} |
| 292 |
|
| 293 |
/** calculate a corner of a polygon circumscribed about an ellipse |
| 294 |
* |
| 295 |
* @param a Half the width of the ellipse, i.e., the maximum x value |
| 296 |
* @param b Half the height of the ellipse, i.e., the maximum y value |
| 297 |
* @param i Index of the polygon corner |
| 298 |
* @param nsides Number of sides of the polygon |
| 299 |
* @return Polygon corner at index `i` |
| 300 |
*/ |
| 301 |
static pointf circumscribed_polygon_corner_about_ellipse(double a, double b, |
| 302 |
size_t i, |
| 303 |
size_t nsides) { |
| 304 |
const double angle0 = 2.0 * M_PI * ((double)i - 0.5) / (double)nsides; |
| 305 |
const double angle1 = 2.0 * M_PI * ((double)i + 0.5) / (double)nsides; |
| 306 |
const pointf p0 = {a * cos(angle0), b * sin(angle0)}; |
| 307 |
const pointf p1 = {a * cos(angle1), b * sin(angle1)}; |
| 308 |
const double m0 = ellipse_tangent_slope(a, b, p0); |
| 309 |
const double m1 = ellipse_tangent_slope(a, b, p1); |
| 310 |
const linef line0 = {{p0.x, p0.y}, m0}; |
| 311 |
const linef line1 = {{p1.x, p1.y}, m1}; |
| 312 |
return line_intersection(line0, line1); |
| 313 |
} |
| 314 |
|
| 315 |
/* Given a node, return an obstacle reflecting the |
| 316 |
* node's geometry. pmargin specifies how much space to allow |
| 317 |
* around the polygon. |
| 318 |
* Returns the constructed polygon on success, NULL on failure. |
| 319 |
* Failure means the node shape is not supported. |
| 320 |
* |
| 321 |
* If isOrtho is true, we have to use the bounding box of each node. |
| 322 |
* |
| 323 |
* The polygon has its vertices in CW order. |
| 324 |
* |
| 325 |
*/ |
| 326 |
Ppoly_t *makeObstacle(node_t * n, expand_t* pmargin, bool isOrtho) |
| 327 |
{ |
| 328 |
Ppoly_t *obs; |
| 329 |
polygon_t *poly; |
| 330 |
size_t sides; |
| 331 |
pointf polyp; |
| 332 |
boxf b; |
| 333 |
pointf pt; |
| 334 |
field_t *fld; |
| 335 |
bool isPoly; |
| 336 |
pointf* verts = NULL; |
| 337 |
pointf vs[4]; |
| 338 |
pointf p; |
| 339 |
pointf margin = {0}; |
| 340 |
|
| 341 |
switch (shapeOf(n)) { |
| 342 |
case SH_POLY: |
| 343 |
case SH_POINT: |
| 344 |
obs = gv_alloc(sizeof(Ppoly_t)); |
| 345 |
poly = ND_shape_info(n); |
| 346 |
if (isOrtho) { |
| 347 |
isPoly = true; |
| 348 |
sides = 4; |
| 349 |
verts = vs; |
| 350 |
/* For fixedshape, we can't use the width and height, as this includes |
| 351 |
* the label. We only want to use the actual node shape. |
| 352 |
*/ |
| 353 |
if (poly->option.fixedshape) { |
| 354 |
b = polyBB (poly); |
| 355 |
vs[0] = b.LL; |
| 356 |
vs[1].x = b.UR.x; |
| 357 |
vs[1].y = b.LL.y; |
| 358 |
vs[2] = b.UR; |
| 359 |
vs[3].x = b.LL.x; |
| 360 |
vs[3].y = b.UR.y; |
| 361 |
} else { |
| 362 |
const double width = ND_lw(n) + ND_rw(n); |
| 363 |
const double outline_width = INCH2PS(ND_outline_width(n)); |
| 364 |
// scale lw and rw proportionally to sum to outline width |
| 365 |
const double outline_lw = ND_lw(n) * outline_width / width; |
| 366 |
const double outline_ht = INCH2PS(ND_outline_height(n)); |
| 367 |
p.x = -outline_lw; |
| 368 |
p.y = -outline_ht / 2.0; |
| 369 |
vs[0] = p; |
| 370 |
p.x = outline_lw; |
| 371 |
vs[1] = p; |
| 372 |
p.y = outline_ht / 2.0; |
| 373 |
vs[2] = p; |
| 374 |
p.x = -outline_lw; |
| 375 |
vs[3] = p; |
| 376 |
} |
| 377 |
} |
| 378 |
else if (poly->sides >= 3) { |
| 379 |
isPoly = true; |
| 380 |
sides = poly->sides; |
| 381 |
const double penwidth = late_double(n, N_penwidth, 1.0, 0.0); |
| 382 |
// possibly use extra vertices representing the outline, i.e., the |
| 383 |
// outermost periphery with penwidth taken into account |
| 384 |
const size_t extra_peripheries = poly->peripheries >= 1 && penwidth > 0.0 ? 1 : 0; |
| 385 |
const size_t outline_periphery = poly->peripheries + extra_peripheries; |
| 386 |
const size_t vertices_offset = outline_periphery >= 1 ? (outline_periphery - 1) * sides : 0; |
| 387 |
verts = poly->vertices + vertices_offset; |
| 388 |
margin.x = pmargin->x; |
| 389 |
margin.y = pmargin->y; |
| 390 |
} else { /* ellipse */ |
| 391 |
isPoly = false; |
| 392 |
sides = 8; |
| 393 |
} |
| 394 |
obs->pn = sides; |
| 395 |
obs->ps = gv_calloc(sides, sizeof(Ppoint_t)); |
| 396 |
/* assuming polys are in CCW order, and pathplan needs CW */ |
| 397 |
for (size_t j = 0; j < sides; j++) { |
| 398 |
double xmargin = 0, ymargin = 0; |
| 399 |
if (isPoly) { |
| 400 |
if (pmargin->doAdd) { |
| 401 |
if (sides == 4) { |
| 402 |
switch (j) { |
| 403 |
case 0 : |
| 404 |
xmargin = margin.x; |
| 405 |
ymargin = margin.y; |
| 406 |
break; |
| 407 |
case 1 : |
| 408 |
xmargin = -margin.x; |
| 409 |
ymargin = margin.y; |
| 410 |
break; |
| 411 |
case 2 : |
| 412 |
xmargin = -margin.x; |
| 413 |
ymargin = -margin.y; |
| 414 |
break; |
| 415 |
case 3 : |
| 416 |
xmargin = margin.x; |
| 417 |
ymargin = -margin.y; |
| 418 |
break; |
| 419 |
default: |
| 420 |
UNREACHABLE(); |
| 421 |
} |
| 422 |
polyp.x = verts[j].x + xmargin; |
| 423 |
polyp.y = verts[j].y + ymargin; |
| 424 |
} |
| 425 |
else { |
| 426 |
const double h = hypot(verts[j].x, verts[j].y); |
| 427 |
polyp.x = verts[j].x * (1.0 + margin.x/h); |
| 428 |
polyp.y = verts[j].y * (1.0 + margin.y/h); |
| 429 |
} |
| 430 |
} |
| 431 |
else { |
| 432 |
polyp.x = verts[j].x * margin.x; |
| 433 |
polyp.y = verts[j].y * margin.y; |
| 434 |
} |
| 435 |
} else { |
| 436 |
const double width = INCH2PS(ND_outline_width(n)); |
| 437 |
const double height = INCH2PS(ND_outline_height(n)); |
| 438 |
margin = pmargin->doAdd ? (pointf){pmargin->x, pmargin->y} : (pointf){0}; |
| 439 |
const double ellipse_a = (width + margin.x) / 2.0; |
| 440 |
const double ellipse_b = (height + margin.y) / 2.0; |
| 441 |
polyp = circumscribed_polygon_corner_about_ellipse(ellipse_a, ellipse_b, j, sides); |
| 442 |
} |
| 443 |
obs->ps[sides - j - 1].x = polyp.x + ND_coord(n).x; |
| 444 |
obs->ps[sides - j - 1].y = polyp.y + ND_coord(n).y; |
| 445 |
} |
| 446 |
break; |
| 447 |
case SH_RECORD: |
| 448 |
fld = ND_shape_info(n); |
| 449 |
b = fld->b; |
| 450 |
obs = gv_alloc(sizeof(Ppoly_t)); |
| 451 |
obs->pn = 4; |
| 452 |
obs->ps = gv_calloc(4, sizeof(Ppoint_t)); |
| 453 |
/* CW order */ |
| 454 |
pt = ND_coord(n); |
| 455 |
if (pmargin->doAdd) { |
| 456 |
obs->ps[0] = genPt(b.LL.x-pmargin->x, b.LL.y-pmargin->y, pt); |
| 457 |
obs->ps[1] = genPt(b.LL.x-pmargin->x, b.UR.y+pmargin->y, pt); |
| 458 |
obs->ps[2] = genPt(b.UR.x+pmargin->x, b.UR.y+pmargin->y, pt); |
| 459 |
obs->ps[3] = genPt(b.UR.x+pmargin->x, b.LL.y-pmargin->y, pt); |
| 460 |
} |
| 461 |
else { |
| 462 |
obs->ps[0] = recPt(b.LL.x, b.LL.y, pt, pmargin); |
| 463 |
obs->ps[1] = recPt(b.LL.x, b.UR.y, pt, pmargin); |
| 464 |
obs->ps[2] = recPt(b.UR.x, b.UR.y, pt, pmargin); |
| 465 |
obs->ps[3] = recPt(b.UR.x, b.LL.y, pt, pmargin); |
| 466 |
} |
| 467 |
break; |
| 468 |
case SH_EPSF: |
| 469 |
obs = gv_alloc(sizeof(Ppoly_t)); |
| 470 |
obs->pn = 4; |
| 471 |
obs->ps = gv_calloc(4, sizeof(Ppoint_t)); |
| 472 |
/* CW order */ |
| 473 |
pt = ND_coord(n); |
| 474 |
if (pmargin->doAdd) { |
| 475 |
obs->ps[0] = genPt(-ND_lw(n)-pmargin->x, -ND_ht(n)-pmargin->y,pt); |
| 476 |
obs->ps[1] = genPt(-ND_lw(n)-pmargin->x, ND_ht(n)+pmargin->y,pt); |
| 477 |
obs->ps[2] = genPt(ND_rw(n)+pmargin->x, ND_ht(n)+pmargin->y,pt); |
| 478 |
obs->ps[3] = genPt(ND_rw(n)+pmargin->x, -ND_ht(n)-pmargin->y,pt); |
| 479 |
} |
| 480 |
else { |
| 481 |
obs->ps[0] = recPt(-ND_lw(n), -ND_ht(n), pt, pmargin); |
| 482 |
obs->ps[1] = recPt(-ND_lw(n), ND_ht(n), pt, pmargin); |
| 483 |
obs->ps[2] = recPt(ND_rw(n), ND_ht(n), pt, pmargin); |
| 484 |
obs->ps[3] = recPt(ND_rw(n), -ND_ht(n), pt, pmargin); |
| 485 |
} |
| 486 |
break; |
| 487 |
default: |
| 488 |
obs = NULL; |
| 489 |
break; |
| 490 |
} |
| 491 |
return obs; |
| 492 |
} |
| 493 |
|
| 494 |
/* Construct the shortest path from one endpoint of e to the other. |
| 495 |
* vconfig is a precomputed data structure to help in the computation. |
| 496 |
* If chkPts is true, the function finds the polygons, if any, containing |
| 497 |
* the endpoints and tells the shortest path computation to ignore them. |
| 498 |
* Assumes this info is set in ND_lim, usually from _spline_edges. |
| 499 |
* Returns the shortest path. |
| 500 |
*/ |
| 501 |
Ppolyline_t getPath(edge_t *e, vconfig_t *vconfig, bool chkPts) { |
| 502 |
Ppolyline_t line; |
| 503 |
int pp, qp; |
| 504 |
Ppoint_t p, q; |
| 505 |
|
| 506 |
p = add_pointf(ND_coord(agtail(e)), ED_tail_port(e).p); |
| 507 |
q = add_pointf(ND_coord(aghead(e)), ED_head_port(e).p); |
| 508 |
|
| 509 |
/* determine the polygons (if any) that contain the endpoints */ |
| 510 |
pp = qp = POLYID_NONE; |
| 511 |
if (chkPts) { |
| 512 |
pp = ND_lim(agtail(e)); |
| 513 |
qp = ND_lim(aghead(e)); |
| 514 |
} |
| 515 |
Pobspath(vconfig, p, pp, q, qp, &line); |
| 516 |
return line; |
| 517 |
} |
| 518 |
|
| 519 |
static void makePolyline(edge_t * e) { |
| 520 |
Ppolyline_t spl, line = ED_path(e); |
| 521 |
|
| 522 |
make_polyline (line, &spl); |
| 523 |
if (Verbose > 1) |
| 524 |
fprintf(stderr, "polyline %s %s\n", agnameof(agtail(e)), agnameof(aghead(e))); |
| 525 |
clip_and_install(e, aghead(e), spl.ps, spl.pn, &sinfo); |
| 526 |
addEdgeLabels(e); |
| 527 |
} |
| 528 |
|
| 529 |
/* Construct a spline connecting the endpoints of e, avoiding the npoly |
| 530 |
* obstacles obs. |
| 531 |
* The resultant spline is attached to the edge, the positions of any |
| 532 |
* edge labels are computed, and the graph's bounding box is recomputed. |
| 533 |
* |
| 534 |
* If chkPts is true, the function checks if one or both of the endpoints |
| 535 |
* is on or inside one of the obstacles and, if so, tells the shortest path |
| 536 |
* computation to ignore them. |
| 537 |
*/ |
| 538 |
void makeSpline(edge_t *e, Ppoly_t **obs, int npoly, bool chkPts) { |
| 539 |
Ppolyline_t line, spline; |
| 540 |
int i; |
| 541 |
int pp, qp; |
| 542 |
Ppoint_t p, q; |
| 543 |
Pedge_t *barriers; |
| 544 |
|
| 545 |
line = ED_path(e); |
| 546 |
p = line.ps[0]; |
| 547 |
q = line.ps[line.pn - 1]; |
| 548 |
/* determine the polygons (if any) that contain the endpoints */ |
| 549 |
pp = qp = POLYID_NONE; |
| 550 |
if (chkPts) |
| 551 |
for (i = 0; i < npoly; i++) { |
| 552 |
if (pp == POLYID_NONE && in_poly(*obs[i], p)) |
| 553 |
pp = i; |
| 554 |
if (qp == POLYID_NONE && in_poly(*obs[i], q)) |
| 555 |
qp = i; |
| 556 |
} |
| 557 |
|
| 558 |
size_t n_barriers; |
| 559 |
make_barriers(obs, npoly, pp, qp, &barriers, &n_barriers); |
| 560 |
Pvector_t slopes[2] = {0}; |
| 561 |
if (Proutespline(barriers, n_barriers, line, slopes, &spline) < 0) { |
| 562 |
agerrorf("makeSpline: failed to make spline edge (%s,%s)\n", agnameof(agtail(e)), agnameof(aghead(e))); |
| 563 |
return; |
| 564 |
} |
| 565 |
|
| 566 |
/* north why did you ever use int coords */ |
| 567 |
if (Verbose > 1) |
| 568 |
fprintf(stderr, "spline %s %s\n", agnameof(agtail(e)), agnameof(aghead(e))); |
| 569 |
clip_and_install(e, aghead(e), spline.ps, spline.pn, &sinfo); |
| 570 |
free(barriers); |
| 571 |
addEdgeLabels(e); |
| 572 |
} |
| 573 |
|
| 574 |
/* True if either head or tail has a port on its boundary */ |
| 575 |
#define BOUNDARY_PORT(e) ((ED_tail_port(e).side)||(ED_head_port(e).side)) |
| 576 |
|
| 577 |
/* Basic default routine for creating edges. |
| 578 |
* If splines are requested, we construct the obstacles. |
| 579 |
* If not, or nodes overlap, the function reverts to line segments. |
| 580 |
* NOTE: intra-cluster edges are not constrained to |
| 581 |
* remain in the cluster's bounding box and, conversely, a cluster's box |
| 582 |
* is not altered to reflect intra-cluster edges. |
| 583 |
* If Nop > 1 and the spline exists, it is just copied. |
| 584 |
* NOTE: if edgetype = EDGETYPE_NONE, we shouldn't be here. |
| 585 |
*/ |
| 586 |
static int spline_edges_(graph_t *g, expand_t *pmargin, int edgetype) { |
| 587 |
node_t *n; |
| 588 |
edge_t *e; |
| 589 |
edge_t *e0; |
| 590 |
Ppoly_t **obs = 0; |
| 591 |
Ppoly_t *obp; |
| 592 |
int cnt, i = 0, npoly; |
| 593 |
vconfig_t *vconfig = 0; |
| 594 |
int useEdges = Nop > 1; |
| 595 |
int legal = 0; |
| 596 |
|
| 597 |
#ifdef HAVE_GTS |
| 598 |
router_t* rtr = 0; |
| 599 |
#endif |
| 600 |
|
| 601 |
/* build configuration */ |
| 602 |
if (edgetype >= EDGETYPE_PLINE) { |
| 603 |
obs = gv_calloc(agnnodes(g), sizeof(Ppoly_t*)); |
| 604 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 605 |
obp = makeObstacle(n, pmargin, edgetype == EDGETYPE_ORTHO); |
| 606 |
if (obp) { |
| 607 |
ND_lim(n) = i; |
| 608 |
obs[i++] = obp; |
| 609 |
} |
| 610 |
else |
| 611 |
ND_lim(n) = POLYID_NONE; |
| 612 |
} |
| 613 |
} else { |
| 614 |
obs = 0; |
| 615 |
} |
| 616 |
npoly = i; |
| 617 |
if (obs) { |
| 618 |
if ((legal = Plegal_arrangement(obs, npoly))) { |
| 619 |
if (edgetype != EDGETYPE_ORTHO) vconfig = Pobsopen(obs, npoly); |
| 620 |
} |
| 621 |
else { |
| 622 |
if (edgetype == EDGETYPE_ORTHO) |
| 623 |
agwarningf("the bounding boxes of some nodes touch - falling back to straight line edges\n"); |
| 624 |
else |
| 625 |
agwarningf("some nodes with margin (%.02f,%.02f) touch - falling back to straight line edges\n", pmargin->x, pmargin->y); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
/* route edges */ |
| 630 |
if (Verbose) |
| 631 |
fprintf(stderr, "Creating edges using %s\n", |
| 632 |
(legal && edgetype == EDGETYPE_ORTHO) ? "orthogonal lines" : |
| 633 |
(vconfig ? (edgetype == EDGETYPE_SPLINE ? "splines" : "polylines") : |
| 634 |
"line segments")); |
| 635 |
if (vconfig) { |
| 636 |
/* path-finding pass */ |
| 637 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 638 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) { |
| 639 |
ED_path(e) = getPath(e, vconfig, true); |
| 640 |
} |
| 641 |
} |
| 642 |
} |
| 643 |
#ifdef ORTHO |
| 644 |
else if (legal && edgetype == EDGETYPE_ORTHO) { |
| 645 |
orthoEdges(g, false); |
| 646 |
useEdges = 1; |
| 647 |
} |
| 648 |
#endif |
| 649 |
|
| 650 |
/* spline-drawing pass */ |
| 651 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 652 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) { |
| 653 |
node_t *head = aghead(e); |
| 654 |
if (useEdges && ED_spl(e)) { |
| 655 |
addEdgeLabels(e); |
| 656 |
if (Nop == 3 && ED_spl(e)->size > 0) { |
| 657 |
// use first bezier, start point and end point will lost |
| 658 |
if (ED_spl(e)->size > 1) { |
| 659 |
agwarningf("edge %s -> %s : set more than one spline. First used, other dropped.\n", agnameof(n), agnameof(head)); |
| 660 |
} |
| 661 |
bezier *bez = ED_spl(e)->list; |
| 662 |
size_t sz = bez->size; |
| 663 |
bez->size = 0; |
| 664 |
pointf *pb = bez->list; |
| 665 |
bez->list = NULL; |
| 666 |
gv_free_splines(e); |
| 667 |
clip_and_install(e, head, pb, sz, &sinfo); |
| 668 |
free(pb); |
| 669 |
} |
| 670 |
} |
| 671 |
else if (ED_count(e) == 0) continue; /* only do representative */ |
| 672 |
else if (n == head) { /* self arc */ |
| 673 |
makeSelfArcs(e, GD_nodesep(g->root)); |
| 674 |
} else if (vconfig) { /* EDGETYPE_SPLINE or EDGETYPE_PLINE */ |
| 675 |
#ifdef HAVE_GTS |
| 676 |
if (ED_count(e) > 1 || BOUNDARY_PORT(e)) { |
| 677 |
int fail = 0; |
| 678 |
if (ED_path(e).pn == 2 && !BOUNDARY_PORT(e)) |
| 679 |
/* if a straight line can connect the ends */ |
| 680 |
makeStraightEdge(g, e, edgetype, &sinfo); |
| 681 |
else { |
| 682 |
if (!rtr) rtr = mkRouter (obs, npoly); |
| 683 |
fail = makeMultiSpline(e, rtr, edgetype == EDGETYPE_PLINE); |
| 684 |
} |
| 685 |
if (!fail) continue; |
| 686 |
} |
| 687 |
/* We can probably remove this branch and just use |
| 688 |
* makeMultiSpline. It can also catch the makeStraightEdge |
| 689 |
* case. We could then eliminate all of the vconfig stuff. |
| 690 |
*/ |
| 691 |
#endif |
| 692 |
cnt = ED_count(e); |
| 693 |
if (Concentrate) cnt = 1; /* only do representative */ |
| 694 |
e0 = e; |
| 695 |
for (i = 0; i < cnt; i++) { |
| 696 |
if (edgetype == EDGETYPE_SPLINE) |
| 697 |
makeSpline(e0, obs, npoly, true); |
| 698 |
else |
| 699 |
makePolyline(e0); |
| 700 |
e0 = ED_to_virt(e0); |
| 701 |
} |
| 702 |
} else { |
| 703 |
makeStraightEdge(g, e, edgetype, &sinfo); |
| 704 |
} |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
#ifdef HAVE_GTS |
| 709 |
if (rtr) |
| 710 |
freeRouter (rtr); |
| 711 |
#endif |
| 712 |
|
| 713 |
if (vconfig) |
| 714 |
Pobsclose (vconfig); |
| 715 |
if (obs) { |
| 716 |
for (i=0; i < npoly; i++) { |
| 717 |
free (obs[i]->ps); |
| 718 |
free (obs[i]); |
| 719 |
} |
| 720 |
free (obs); |
| 721 |
} |
| 722 |
return 0; |
| 723 |
} |
| 724 |
|
| 725 |
/* Main wrapper code for generating edges. |
| 726 |
* Sets desired separation. |
| 727 |
* Coalesces equivalent edges (edges * with the same endpoints). |
| 728 |
* It then calls the edge generating function, and marks the |
| 729 |
* spline phase complete. |
| 730 |
* Returns 0 on success. |
| 731 |
* |
| 732 |
* The edge function is given the graph, the separation to be added |
| 733 |
* around obstacles, and the type of edge. It must guarantee |
| 734 |
* that all bounding boxes are current; in particular, the bounding box of |
| 735 |
* g must reflect the addition of the edges. |
| 736 |
*/ |
| 737 |
int |
| 738 |
splineEdges(graph_t * g, int (*edgefn) (graph_t *, expand_t*, int), |
| 739 |
int edgetype) |
| 740 |
{ |
| 741 |
node_t *n; |
| 742 |
edge_t *e; |
| 743 |
expand_t margin; |
| 744 |
Dt_t *map; |
| 745 |
|
| 746 |
margin = esepFactor (g); |
| 747 |
|
| 748 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 749 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) { |
| 750 |
resolvePorts (e); |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
/* find equivalent edges */ |
| 755 |
map = dtopen(&edgeItemDisc, Dtoset); |
| 756 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 757 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) { |
| 758 |
if (Nop > 1 && ED_spl(e)) { |
| 759 |
/* If Nop > 1 (use given edges) and e has a spline, it |
| 760 |
* should have its own equivalence class. |
| 761 |
*/ |
| 762 |
ED_count(e)++; |
| 763 |
} else { |
| 764 |
edge_t *leader = equivEdge(map, e); |
| 765 |
if (leader != e) { |
| 766 |
ED_count(leader)++; |
| 767 |
ED_to_virt(e) = ED_to_virt(leader); |
| 768 |
ED_to_virt(leader) = e; |
| 769 |
} |
| 770 |
} |
| 771 |
} |
| 772 |
} |
| 773 |
dtclose(map); |
| 774 |
|
| 775 |
if (edgefn(g, &margin, edgetype)) |
| 776 |
return 1; |
| 777 |
|
| 778 |
State = GVSPLINES; |
| 779 |
return 0; |
| 780 |
} |
| 781 |
|
| 782 |
/* Construct edges using default algorithm and given splines value. |
| 783 |
* Return 0 on success. |
| 784 |
*/ |
| 785 |
int spline_edges1(graph_t * g, int edgetype) |
| 786 |
{ |
| 787 |
return splineEdges(g, spline_edges_, edgetype); |
| 788 |
} |
| 789 |
|
| 790 |
/* Sets the graph's aspect ratio. |
| 791 |
* Check splines attribute and construct edges using default algorithm. |
| 792 |
* If the splines attribute is defined but equal to "", skip edge routing. |
| 793 |
* |
| 794 |
* Assumes u.bb for has been computed for g and all clusters |
| 795 |
* (not just top-level clusters), and that GD_bb(g).LL is at the origin. |
| 796 |
* |
| 797 |
* This last criterion is, I believe, mainly to simplify the code |
| 798 |
* in neato_set_aspect. It would be good to remove this constraint, |
| 799 |
* as this would allow nodes pinned on input to have the same coordinates |
| 800 |
* when output in dot or plain format. |
| 801 |
* |
| 802 |
*/ |
| 803 |
void spline_edges0(graph_t *g, bool set_aspect) { |
| 804 |
int et = EDGE_TYPE (g); |
| 805 |
if (set_aspect) neato_set_aspect(g); |
| 806 |
if (et == EDGETYPE_NONE) return; |
| 807 |
#ifndef ORTHO |
| 808 |
if (et == EDGETYPE_ORTHO) { |
| 809 |
agwarningf("Orthogonal edges not yet supported\n"); |
| 810 |
et = EDGETYPE_PLINE; |
| 811 |
GD_flags(g->root) &= ~EDGETYPE_ORTHO; |
| 812 |
GD_flags(g->root) |= EDGETYPE_PLINE; |
| 813 |
} |
| 814 |
#endif |
| 815 |
spline_edges1(g, et); |
| 816 |
} |
| 817 |
|
| 818 |
static void |
| 819 |
shiftClusters (graph_t * g, pointf offset) |
| 820 |
{ |
| 821 |
int i; |
| 822 |
|
| 823 |
for (i = 1; i <= GD_n_cluster(g); i++) { |
| 824 |
shiftClusters (GD_clust(g)[i], offset); |
| 825 |
} |
| 826 |
|
| 827 |
GD_bb(g).UR.x -= offset.x; |
| 828 |
GD_bb(g).UR.y -= offset.y; |
| 829 |
GD_bb(g).LL.x -= offset.x; |
| 830 |
GD_bb(g).LL.y -= offset.y; |
| 831 |
} |
| 832 |
|
| 833 |
/* Compute bounding box, translate graph to origin, |
| 834 |
* then construct all edges. |
| 835 |
*/ |
| 836 |
void spline_edges(graph_t * g) |
| 837 |
{ |
| 838 |
node_t *n; |
| 839 |
pointf offset; |
| 840 |
|
| 841 |
compute_bb(g); |
| 842 |
offset.x = PS2INCH(GD_bb(g).LL.x); |
| 843 |
offset.y = PS2INCH(GD_bb(g).LL.y); |
| 844 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 845 |
ND_pos(n)[0] -= offset.x; |
| 846 |
ND_pos(n)[1] -= offset.y; |
| 847 |
} |
| 848 |
|
| 849 |
shiftClusters (g, GD_bb(g).LL); |
| 850 |
spline_edges0(g, true); |
| 851 |
} |
| 852 |
|
| 853 |
/* Scale edge by given factor. |
| 854 |
* Assume ED_spl != NULL. |
| 855 |
*/ |
| 856 |
static void scaleEdge(edge_t * e, double xf, double yf) |
| 857 |
{ |
| 858 |
pointf *pt; |
| 859 |
bezier *bez; |
| 860 |
pointf delh, delt; |
| 861 |
|
| 862 |
delh.x = POINTS_PER_INCH * (ND_pos(aghead(e))[0] * (xf - 1.0)); |
| 863 |
delh.y = POINTS_PER_INCH * (ND_pos(aghead(e))[1] * (yf - 1.0)); |
| 864 |
delt.x = POINTS_PER_INCH * (ND_pos(agtail(e))[0] * (xf - 1.0)); |
| 865 |
delt.y = POINTS_PER_INCH * (ND_pos(agtail(e))[1] * (yf - 1.0)); |
| 866 |
|
| 867 |
bez = ED_spl(e)->list; |
| 868 |
for (size_t i = 0; i < ED_spl(e)->size; i++) { |
| 869 |
pt = bez->list; |
| 870 |
for (size_t j = 0; j < bez->size; j++) { |
| 871 |
if (i == 0 && j == 0) { |
| 872 |
pt->x += delt.x; |
| 873 |
pt->y += delt.y; |
| 874 |
} |
| 875 |
else if (i == ED_spl(e)->size-1 && j == bez->size-1) { |
| 876 |
pt->x += delh.x; |
| 877 |
pt->y += delh.y; |
| 878 |
} |
| 879 |
else { |
| 880 |
pt->x *= xf; |
| 881 |
pt->y *= yf; |
| 882 |
} |
| 883 |
pt++; |
| 884 |
} |
| 885 |
if (bez->sflag) { |
| 886 |
bez->sp.x += delt.x; |
| 887 |
bez->sp.y += delt.y; |
| 888 |
} |
| 889 |
if (bez->eflag) { |
| 890 |
bez->ep.x += delh.x; |
| 891 |
bez->ep.y += delh.y; |
| 892 |
} |
| 893 |
bez++; |
| 894 |
} |
| 895 |
|
| 896 |
if (ED_label(e) && ED_label(e)->set) { |
| 897 |
ED_label(e)->pos.x *= xf; |
| 898 |
ED_label(e)->pos.y *= yf; |
| 899 |
} |
| 900 |
if (ED_head_label(e) && ED_head_label(e)->set) { |
| 901 |
ED_head_label(e)->pos.x += delh.x; |
| 902 |
ED_head_label(e)->pos.y += delh.y; |
| 903 |
} |
| 904 |
if (ED_tail_label(e) && ED_tail_label(e)->set) { |
| 905 |
ED_tail_label(e)->pos.x += delt.x; |
| 906 |
ED_tail_label(e)->pos.y += delt.y; |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
/// scale bounding box of clusters of g by given factors |
| 911 |
static void scaleBB(graph_t * g, double xf, double yf) |
| 912 |
{ |
| 913 |
int i; |
| 914 |
|
| 915 |
GD_bb(g).UR.x *= xf; |
| 916 |
GD_bb(g).UR.y *= yf; |
| 917 |
GD_bb(g).LL.x *= xf; |
| 918 |
GD_bb(g).LL.y *= yf; |
| 919 |
|
| 920 |
if (GD_label(g) && GD_label(g)->set) { |
| 921 |
GD_label(g)->pos.x *= xf; |
| 922 |
GD_label(g)->pos.y *= yf; |
| 923 |
} |
| 924 |
|
| 925 |
for (i = 1; i <= GD_n_cluster(g); i++) |
| 926 |
scaleBB(GD_clust(g)[i], xf, yf); |
| 927 |
} |
| 928 |
|
| 929 |
/* Translate edge by offset. |
| 930 |
* Assume ED_spl(e) != NULL |
| 931 |
*/ |
| 932 |
static void translateE(edge_t * e, pointf offset) |
| 933 |
{ |
| 934 |
pointf *pt; |
| 935 |
bezier *bez; |
| 936 |
|
| 937 |
bez = ED_spl(e)->list; |
| 938 |
for (size_t i = 0; i < ED_spl(e)->size; i++) { |
| 939 |
pt = bez->list; |
| 940 |
for (size_t j = 0; j < bez->size; j++) { |
| 941 |
pt->x -= offset.x; |
| 942 |
pt->y -= offset.y; |
| 943 |
pt++; |
| 944 |
} |
| 945 |
if (bez->sflag) { |
| 946 |
bez->sp.x -= offset.x; |
| 947 |
bez->sp.y -= offset.y; |
| 948 |
} |
| 949 |
if (bez->eflag) { |
| 950 |
bez->ep.x -= offset.x; |
| 951 |
bez->ep.y -= offset.y; |
| 952 |
} |
| 953 |
bez++; |
| 954 |
} |
| 955 |
|
| 956 |
if (ED_label(e) && ED_label(e)->set) { |
| 957 |
ED_label(e)->pos.x -= offset.x; |
| 958 |
ED_label(e)->pos.y -= offset.y; |
| 959 |
} |
| 960 |
if (ED_xlabel(e) && ED_xlabel(e)->set) { |
| 961 |
ED_xlabel(e)->pos.x -= offset.x; |
| 962 |
ED_xlabel(e)->pos.y -= offset.y; |
| 963 |
} |
| 964 |
if (ED_head_label(e) && ED_head_label(e)->set) { |
| 965 |
ED_head_label(e)->pos.x -= offset.x; |
| 966 |
ED_head_label(e)->pos.y -= offset.y; |
| 967 |
} |
| 968 |
if (ED_tail_label(e) && ED_tail_label(e)->set) { |
| 969 |
ED_tail_label(e)->pos.x -= offset.x; |
| 970 |
ED_tail_label(e)->pos.y -= offset.y; |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
static void translateG(Agraph_t * g, pointf offset) |
| 975 |
{ |
| 976 |
int i; |
| 977 |
|
| 978 |
GD_bb(g).UR.x -= offset.x; |
| 979 |
GD_bb(g).UR.y -= offset.y; |
| 980 |
GD_bb(g).LL.x -= offset.x; |
| 981 |
GD_bb(g).LL.y -= offset.y; |
| 982 |
|
| 983 |
if (GD_label(g) && GD_label(g)->set) { |
| 984 |
GD_label(g)->pos.x -= offset.x; |
| 985 |
GD_label(g)->pos.y -= offset.y; |
| 986 |
} |
| 987 |
|
| 988 |
for (i = 1; i <= GD_n_cluster(g); i++) |
| 989 |
translateG(GD_clust(g)[i], offset); |
| 990 |
} |
| 991 |
|
| 992 |
void neato_translate(Agraph_t * g) |
| 993 |
{ |
| 994 |
node_t *n; |
| 995 |
edge_t *e; |
| 996 |
pointf offset; |
| 997 |
pointf ll; |
| 998 |
|
| 999 |
ll = GD_bb(g).LL; |
| 1000 |
|
| 1001 |
offset.x = PS2INCH(ll.x); |
| 1002 |
offset.y = PS2INCH(ll.y); |
| 1003 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 1004 |
ND_pos(n)[0] -= offset.x; |
| 1005 |
ND_pos(n)[1] -= offset.y; |
| 1006 |
if (ND_xlabel(n) && ND_xlabel(n)->set) { |
| 1007 |
ND_xlabel(n)->pos.x -= ll.x; |
| 1008 |
ND_xlabel(n)->pos.y -= ll.y; |
| 1009 |
} |
| 1010 |
} |
| 1011 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 1012 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) |
| 1013 |
if (ED_spl(e)) |
| 1014 |
translateE(e, ll); |
| 1015 |
} |
| 1016 |
translateG(g, ll); |
| 1017 |
} |
| 1018 |
|
| 1019 |
/* Assume all bounding boxes are correct. |
| 1020 |
* Return false if no transform is performed. This includes |
| 1021 |
* the possibility that a translation was done. |
| 1022 |
*/ |
| 1023 |
static bool _neato_set_aspect(graph_t * g) |
| 1024 |
{ |
| 1025 |
double xf, yf, actual, desired; |
| 1026 |
node_t *n; |
| 1027 |
bool translated = false; |
| 1028 |
|
| 1029 |
if (g->root != g) |
| 1030 |
return false; |
| 1031 |
|
| 1032 |
if (GD_drawing(g)->ratio_kind) { |
| 1033 |
if (GD_bb(g).LL.x || GD_bb(g).LL.y) { |
| 1034 |
translated = true; |
| 1035 |
neato_translate (g); |
| 1036 |
} |
| 1037 |
/* normalize */ |
| 1038 |
if (GD_flip(g)) { |
| 1039 |
GD_bb(g).UR = exch_xyf(GD_bb(g).UR); |
| 1040 |
} |
| 1041 |
if (GD_drawing(g)->ratio_kind == R_FILL) { |
| 1042 |
/* fill is weird because both X and Y can stretch */ |
| 1043 |
if (GD_drawing(g)->size.x <= 0) |
| 1044 |
return translated; |
| 1045 |
xf = GD_drawing(g)->size.x / GD_bb(g).UR.x; |
| 1046 |
yf = GD_drawing(g)->size.y / GD_bb(g).UR.y; |
| 1047 |
/* handle case where one or more dimensions is too big */ |
| 1048 |
if (xf < 1.0 || yf < 1.0) { |
| 1049 |
if (xf < yf) { |
| 1050 |
yf /= xf; |
| 1051 |
xf = 1.0; |
| 1052 |
} else { |
| 1053 |
xf /= yf; |
| 1054 |
yf = 1.0; |
| 1055 |
} |
| 1056 |
} |
| 1057 |
} else if (GD_drawing(g)->ratio_kind == R_EXPAND) { |
| 1058 |
if (GD_drawing(g)->size.x <= 0) |
| 1059 |
return translated; |
| 1060 |
xf = GD_drawing(g)->size.x / GD_bb(g).UR.x; |
| 1061 |
yf = GD_drawing(g)->size.y / GD_bb(g).UR.y; |
| 1062 |
if (xf > 1.0 && yf > 1.0) { |
| 1063 |
double scale = fmin(xf, yf); |
| 1064 |
xf = yf = scale; |
| 1065 |
} else |
| 1066 |
return translated; |
| 1067 |
} else if (GD_drawing(g)->ratio_kind == R_VALUE) { |
| 1068 |
desired = GD_drawing(g)->ratio; |
| 1069 |
actual = GD_bb(g).UR.y / GD_bb(g).UR.x; |
| 1070 |
if (actual < desired) { |
| 1071 |
yf = desired / actual; |
| 1072 |
xf = 1.0; |
| 1073 |
} else { |
| 1074 |
xf = actual / desired; |
| 1075 |
yf = 1.0; |
| 1076 |
} |
| 1077 |
} else |
| 1078 |
return translated; |
| 1079 |
if (GD_flip(g)) { |
| 1080 |
SWAP(&xf, &yf); |
| 1081 |
} |
| 1082 |
|
| 1083 |
if (Nop > 1) { |
| 1084 |
edge_t *e; |
| 1085 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 1086 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) |
| 1087 |
if (ED_spl(e)) |
| 1088 |
scaleEdge(e, xf, yf); |
| 1089 |
} |
| 1090 |
} |
| 1091 |
/* Not relying on neato_nlist here allows us not to have to |
| 1092 |
* allocate it in the root graph and the connected components. |
| 1093 |
*/ |
| 1094 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 1095 |
ND_pos(n)[0] *= xf; |
| 1096 |
ND_pos(n)[1] *= yf; |
| 1097 |
} |
| 1098 |
scaleBB(g, xf, yf); |
| 1099 |
return true; |
| 1100 |
} |
| 1101 |
else |
| 1102 |
return false; |
| 1103 |
} |
| 1104 |
|
| 1105 |
/* Sets aspect ratio if necessary; real work done in _neato_set_aspect; |
| 1106 |
* This also copies the internal layout coordinates (ND_pos) to the |
| 1107 |
* external ones (ND_coord). |
| 1108 |
* |
| 1109 |
* Return true if some node moved. |
| 1110 |
*/ |
| 1111 |
bool neato_set_aspect(graph_t * g) |
| 1112 |
{ |
| 1113 |
node_t *n; |
| 1114 |
bool moved = false; |
| 1115 |
|
| 1116 |
/* setting aspect ratio only makes sense on root graph */ |
| 1117 |
moved = _neato_set_aspect(g); |
| 1118 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 1119 |
ND_coord(n).x = POINTS_PER_INCH * ND_pos(n)[0]; |
| 1120 |
ND_coord(n).y = POINTS_PER_INCH * ND_pos(n)[1]; |
| 1121 |
} |
| 1122 |
return moved; |
| 1123 |
} |
| 1124 |
|