| 1 |
/** |
| 2 |
* @file |
| 3 |
* @brief API neatogen/neatoprocs.h: |
| 4 |
* @ref neato_init_node, @ref user_pos, @ref neato_cleanup, |
| 5 |
* @ref init_nop, @ref setSeed, @ref checkStart, @ref neato_layout |
| 6 |
*/ |
| 7 |
|
| 8 |
/************************************************************************* |
| 9 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 10 |
* All rights reserved. This program and the accompanying materials |
| 11 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 12 |
* which accompanies this distribution, and is available at |
| 13 |
* https://www.eclipse.org/legal/epl-v10.html |
| 14 |
* |
| 15 |
* Contributors: Details at https://graphviz.org |
| 16 |
*************************************************************************/ |
| 17 |
|
| 18 |
|
| 19 |
#include "config.h" |
| 20 |
|
| 21 |
#include <time.h> |
| 22 |
#include <neatogen/neato.h> |
| 23 |
#include <pack/pack.h> |
| 24 |
#include <neatogen/stress.h> |
| 25 |
#ifdef DIGCOLA |
| 26 |
#include <neatogen/digcola.h> |
| 27 |
#endif |
| 28 |
#include <neatogen/kkutils.h> |
| 29 |
#include <common/pointset.h> |
| 30 |
#include <common/render.h> |
| 31 |
#include <common/utils.h> |
| 32 |
#include <neatogen/sgd.h> |
| 33 |
#include <cgraph/cgraph.h> |
| 34 |
#include <float.h> |
| 35 |
#include <stdatomic.h> |
| 36 |
#include <stdbool.h> |
| 37 |
#include <stddef.h> |
| 38 |
#include <util/agxbuf.h> |
| 39 |
#include <util/alloc.h> |
| 40 |
#include <util/bitarray.h> |
| 41 |
#include <util/gv_ctype.h> |
| 42 |
#include <util/gv_math.h> |
| 43 |
#include <util/itos.h> |
| 44 |
#include <util/prisize_t.h> |
| 45 |
#include <util/startswith.h> |
| 46 |
#include <util/strcasecmp.h> |
| 47 |
#include <util/streq.h> |
| 48 |
|
| 49 |
#ifndef HAVE_SRAND48 |
| 50 |
#define srand48 srand |
| 51 |
#endif |
| 52 |
|
| 53 |
static attrsym_t *N_pos; |
| 54 |
static int Pack; /* If >= 0, layout components separately and pack together |
| 55 |
* The value of Pack gives margins around graphs. |
| 56 |
*/ |
| 57 |
static char *cc_pfx = "_neato_cc"; |
| 58 |
|
| 59 |
void neato_init_node(node_t * n) |
| 60 |
{ |
| 61 |
agbindrec(n, "Agnodeinfo_t", sizeof(Agnodeinfo_t), true); //node custom data |
| 62 |
common_init_node(n); |
| 63 |
ND_pos(n) = gv_calloc(GD_ndim(agraphof(n)), sizeof(double)); |
| 64 |
gv_nodesize(n, GD_flip(agraphof(n))); |
| 65 |
} |
| 66 |
|
| 67 |
static void neato_init_edge(edge_t * e) |
| 68 |
{ |
| 69 |
agbindrec(e, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); //node custom data |
| 70 |
common_init_edge(e); |
| 71 |
ED_factor(e) = late_double(e, E_weight, 1.0, 1.0); |
| 72 |
} |
| 73 |
|
| 74 |
bool user_pos(attrsym_t *posptr, attrsym_t *pinptr, node_t *np, int nG) { |
| 75 |
double *pvec; |
| 76 |
char *p, c; |
| 77 |
double z; |
| 78 |
|
| 79 |
if (posptr == NULL) |
| 80 |
return false; |
| 81 |
pvec = ND_pos(np); |
| 82 |
p = agxget(np, posptr); |
| 83 |
if (p[0]) { |
| 84 |
c = '\0'; |
| 85 |
if (Ndim >= 3 && sscanf(p, "%lf,%lf,%lf%c", pvec, pvec+1, pvec+2, &c) >= 3){ |
| 86 |
ND_pinned(np) = P_SET; |
| 87 |
if (PSinputscale > 0.0) { |
| 88 |
int i; |
| 89 |
for (i = 0; i < Ndim; i++) |
| 90 |
pvec[i] /= PSinputscale; |
| 91 |
} |
| 92 |
if (Ndim > 3) |
| 93 |
jitter_d(np, nG, 3); |
| 94 |
if (c == '!' || (pinptr && mapbool(agxget(np, pinptr)))) |
| 95 |
ND_pinned(np) = P_PIN; |
| 96 |
return true; |
| 97 |
} |
| 98 |
else if (sscanf(p, "%lf,%lf%c", pvec, pvec + 1, &c) >= 2) { |
| 99 |
ND_pinned(np) = P_SET; |
| 100 |
if (PSinputscale > 0.0) { |
| 101 |
int i; |
| 102 |
for (i = 0; i < Ndim; i++) |
| 103 |
pvec[i] /= PSinputscale; |
| 104 |
} |
| 105 |
if (Ndim > 2) { |
| 106 |
if (N_z && (p = agxget(np, N_z)) && sscanf(p,"%lf",&z) == 1) { |
| 107 |
if (PSinputscale > 0.0) { |
| 108 |
pvec[2] = z / PSinputscale; |
| 109 |
} |
| 110 |
else |
| 111 |
pvec[2] = z; |
| 112 |
jitter_d(np, nG, 3); |
| 113 |
} |
| 114 |
else |
| 115 |
jitter3d(np, nG); |
| 116 |
} |
| 117 |
if (c == '!' || (pinptr && mapbool(agxget(np, pinptr)))) |
| 118 |
ND_pinned(np) = P_PIN; |
| 119 |
return true; |
| 120 |
} else |
| 121 |
agerrorf("node %s, position %s, expected two doubles\n", |
| 122 |
agnameof(np), p); |
| 123 |
} |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
static void neato_init_node_edge(graph_t * g) |
| 128 |
{ |
| 129 |
node_t *n; |
| 130 |
edge_t *e; |
| 131 |
int nG = agnnodes(g); |
| 132 |
attrsym_t *N_pin; |
| 133 |
|
| 134 |
N_pos = agfindnodeattr(g, "pos"); |
| 135 |
N_pin = agfindnodeattr(g, "pin"); |
| 136 |
|
| 137 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 138 |
neato_init_node(n); |
| 139 |
user_pos(N_pos, N_pin, n, nG); /* set user position if given */ |
| 140 |
} |
| 141 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 142 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) |
| 143 |
neato_init_edge(e); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
static void neato_cleanup_graph(graph_t * g) |
| 148 |
{ |
| 149 |
if (Nop || Pack < 0) { |
| 150 |
free_scan_graph(g); |
| 151 |
} |
| 152 |
free(GD_clust(g)); |
| 153 |
} |
| 154 |
|
| 155 |
void neato_cleanup(graph_t * g) |
| 156 |
{ |
| 157 |
node_t *n; |
| 158 |
edge_t *e; |
| 159 |
|
| 160 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 161 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) { |
| 162 |
gv_cleanup_edge(e); |
| 163 |
} |
| 164 |
gv_cleanup_node(n); |
| 165 |
} |
| 166 |
neato_cleanup_graph(g); |
| 167 |
} |
| 168 |
|
| 169 |
static size_t numFields(const char *pos) { |
| 170 |
size_t cnt = 0; |
| 171 |
char c; |
| 172 |
|
| 173 |
do { |
| 174 |
while (gv_isspace(*pos)) |
| 175 |
pos++; /* skip white space */ |
| 176 |
if ((c = *pos)) { /* skip token */ |
| 177 |
cnt++; |
| 178 |
while ((c = *pos) && !gv_isspace(c) && c != ';') |
| 179 |
pos++; |
| 180 |
} |
| 181 |
} while (gv_isspace(c)); |
| 182 |
return cnt; |
| 183 |
} |
| 184 |
|
| 185 |
static void set_label(void* obj, textlabel_t * l, char *name) |
| 186 |
{ |
| 187 |
double x, y; |
| 188 |
char *lp; |
| 189 |
lp = agget(obj, name); |
| 190 |
if (lp && sscanf(lp, "%lf,%lf", &x, &y) == 2) { |
| 191 |
l->pos = (pointf){x, y}; |
| 192 |
l->set = true; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
#ifdef IPSEPCOLA |
| 197 |
static cluster_data cluster_map(graph_t *mastergraph, graph_t *g) { |
| 198 |
graph_t *subg; |
| 199 |
node_t *n; |
| 200 |
/* array of arrays of node indices in each cluster */ |
| 201 |
int **cs,*cn; |
| 202 |
int i,j,nclusters=0; |
| 203 |
bitarray_t assigned = bitarray_new(agnnodes(g)); |
| 204 |
cluster_data cdata = {0}; |
| 205 |
|
| 206 |
cdata.ntoplevel = agnnodes(g); |
| 207 |
for (subg = agfstsubg(mastergraph); subg; subg = agnxtsubg(subg)) { |
| 208 |
if (is_a_cluster(subg)) { |
| 209 |
nclusters++; |
| 210 |
} |
| 211 |
} |
| 212 |
cdata.nvars=0; |
| 213 |
cdata.nclusters = nclusters; |
| 214 |
cs = cdata.clusters = gv_calloc(nclusters, sizeof(int*)); |
| 215 |
cn = cdata.clustersizes = gv_calloc(nclusters, sizeof(int)); |
| 216 |
for (subg = agfstsubg(mastergraph); subg; subg = agnxtsubg(subg)) { |
| 217 |
/* clusters are processed by separate calls to ordered_edges */ |
| 218 |
if (is_a_cluster(subg)) { |
| 219 |
int *c; |
| 220 |
|
| 221 |
*cn = agnnodes(subg); |
| 222 |
cdata.nvars += *cn; |
| 223 |
c = *cs++ = gv_calloc(*cn++, sizeof(int)); |
| 224 |
for (n = agfstnode(subg); n; n = agnxtnode(subg, n)) { |
| 225 |
node_t *gn; |
| 226 |
int ind = 0; |
| 227 |
for (gn = agfstnode(g); gn; gn = agnxtnode(g, gn)) { |
| 228 |
if(AGSEQ(gn)==AGSEQ(n)) break; |
| 229 |
ind++; |
| 230 |
} |
| 231 |
*c++=ind; |
| 232 |
bitarray_set(&assigned, ind, true); |
| 233 |
cdata.ntoplevel--; |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
cdata.bb = gv_calloc(cdata.nclusters, sizeof(boxf)); |
| 238 |
cdata.toplevel = gv_calloc(cdata.ntoplevel, sizeof(int)); |
| 239 |
for(i=j=0;i<agnnodes(g);i++) { |
| 240 |
if(!bitarray_get(assigned, i)) { |
| 241 |
cdata.toplevel[j++] = i; |
| 242 |
} |
| 243 |
} |
| 244 |
assert(cdata.ntoplevel == agnnodes(g) - cdata.nvars); |
| 245 |
bitarray_reset(&assigned); |
| 246 |
return cdata; |
| 247 |
} |
| 248 |
|
| 249 |
static void freeClusterData(cluster_data c) { |
| 250 |
if (c.nclusters > 0) { |
| 251 |
free(c.clusters[0]); |
| 252 |
free(c.clusters); |
| 253 |
free(c.clustersizes); |
| 254 |
free(c.toplevel); |
| 255 |
free(c.bb); |
| 256 |
} |
| 257 |
} |
| 258 |
#endif |
| 259 |
|
| 260 |
/* Attempt to use already existing pos info for spline |
| 261 |
* Return 1 if successful, 0 otherwise. |
| 262 |
* Assume E_pos != NULL and ED_spl(e) == NULL. |
| 263 |
*/ |
| 264 |
static int user_spline(attrsym_t * E_pos, edge_t * e) |
| 265 |
{ |
| 266 |
int nc; |
| 267 |
pointf *pp; |
| 268 |
double x, y; |
| 269 |
bool sflag = false, eflag = false; |
| 270 |
pointf sp = { 0, 0 }, ep = { 0, 0}; |
| 271 |
bezier *newspl; |
| 272 |
static atomic_flag warned; |
| 273 |
|
| 274 |
const char *pos = agxget(e, E_pos); |
| 275 |
if (*pos == '\0') |
| 276 |
return 0; |
| 277 |
|
| 278 |
uint32_t stype, etype; |
| 279 |
arrow_flags(e, &stype, &etype); |
| 280 |
for (bool more = true; more; ) { |
| 281 |
/* check for s head */ |
| 282 |
if (sscanf(pos, "s,%lf,%lf%n", &x, &y, &nc) == 2) { |
| 283 |
sflag = true; |
| 284 |
pos += nc; |
| 285 |
sp = (pointf){.x = x, .y = y}; |
| 286 |
} |
| 287 |
|
| 288 |
/* check for e head */ |
| 289 |
if (sscanf(pos, " e,%lf,%lf%n", &x, &y, &nc) == 2) { |
| 290 |
eflag = true; |
| 291 |
pos += nc; |
| 292 |
ep = (pointf){.x = x, .y = y}; |
| 293 |
} |
| 294 |
|
| 295 |
const size_t npts = numFields(pos); // count potential points |
| 296 |
if (npts < 4 || npts % 3 != 1) { |
| 297 |
gv_free_splines(e); |
| 298 |
if (!atomic_flag_test_and_set(&warned)) { |
| 299 |
agwarningf("pos attribute for edge (%s,%s) doesn't have 3n+1 points\n", agnameof(agtail(e)), agnameof(aghead(e))); |
| 300 |
} |
| 301 |
return 0; |
| 302 |
} |
| 303 |
pointf *ps = gv_calloc(npts, sizeof(pointf)); |
| 304 |
pp = ps; |
| 305 |
for (size_t n = npts; n > 0; --n) { |
| 306 |
if (sscanf(pos, "%lf,%lf%n", &x, &y, &nc) < 2) { |
| 307 |
if (!atomic_flag_test_and_set(&warned)) { |
| 308 |
agwarningf("syntax error in pos attribute for edge (%s,%s)\n", agnameof(agtail(e)), agnameof(aghead(e))); |
| 309 |
} |
| 310 |
free(ps); |
| 311 |
gv_free_splines(e); |
| 312 |
return 0; |
| 313 |
} |
| 314 |
pos += nc; |
| 315 |
*pp = (pointf){.x = x, .y = y}; |
| 316 |
pp++; |
| 317 |
} |
| 318 |
while (gv_isspace(*pos)) pos++; |
| 319 |
if (*pos == '\0') |
| 320 |
more = false; |
| 321 |
else |
| 322 |
pos++; |
| 323 |
|
| 324 |
/* parsed successfully; create spline */ |
| 325 |
newspl = new_spline(e, npts); |
| 326 |
if (sflag) { |
| 327 |
newspl->sflag = stype; |
| 328 |
newspl->sp = sp; |
| 329 |
} |
| 330 |
if (eflag) { |
| 331 |
newspl->eflag = etype; |
| 332 |
newspl->ep = ep; |
| 333 |
} |
| 334 |
for (size_t i = 0; i < npts; i++) { |
| 335 |
newspl->list[i] = ps[i]; |
| 336 |
} |
| 337 |
free(ps); |
| 338 |
} |
| 339 |
|
| 340 |
if (ED_label(e)) |
| 341 |
set_label(e, ED_label(e), "lp"); |
| 342 |
if (ED_xlabel(e)) |
| 343 |
set_label(e, ED_xlabel(e), "xlp"); |
| 344 |
if (ED_head_label(e)) |
| 345 |
set_label(e, ED_head_label(e), "head_lp"); |
| 346 |
if (ED_tail_label(e)) |
| 347 |
set_label(e, ED_tail_label(e), "tail_lp"); |
| 348 |
|
| 349 |
return 1; |
| 350 |
} |
| 351 |
|
| 352 |
/* Nop can be: |
| 353 |
* 0 - do full layout |
| 354 |
* 1 - assume initial node positions, do (optional) adjust and all splines |
| 355 |
* 2 - assume final node and edges positions, do nothing except compute |
| 356 |
* missing splines |
| 357 |
*/ |
| 358 |
|
| 359 |
/* Indicates the amount of edges with position information */ |
| 360 |
typedef enum { NoEdges, SomeEdges, AllEdges } pos_edge; |
| 361 |
|
| 362 |
/* Check edges for position info. |
| 363 |
* If position info exists, check for edge label positions. |
| 364 |
* Return number of edges with position info. |
| 365 |
*/ |
| 366 |
static pos_edge nop_init_edges(Agraph_t * g) |
| 367 |
{ |
| 368 |
node_t *n; |
| 369 |
edge_t *e; |
| 370 |
int nedges = 0; |
| 371 |
|
| 372 |
if (agnedges(g) == 0) |
| 373 |
return AllEdges; |
| 374 |
|
| 375 |
attrsym_t *const E_pos = agfindedgeattr(g, "pos"); |
| 376 |
if (!E_pos || Nop < 2) |
| 377 |
return NoEdges; |
| 378 |
|
| 379 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 380 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) { |
| 381 |
if (user_spline(E_pos, e)) { |
| 382 |
nedges++; |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
if (nedges) { |
| 387 |
if (nedges == agnedges(g)) |
| 388 |
return AllEdges; |
| 389 |
return SomeEdges; |
| 390 |
} |
| 391 |
return NoEdges; |
| 392 |
} |
| 393 |
|
| 394 |
static void freeEdgeInfo (Agraph_t * g) |
| 395 |
{ |
| 396 |
node_t *n; |
| 397 |
edge_t *e; |
| 398 |
|
| 399 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 400 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) { |
| 401 |
gv_free_splines(e); |
| 402 |
free_label(ED_label(e)); |
| 403 |
free_label(ED_xlabel(e)); |
| 404 |
free_label(ED_head_label(e)); |
| 405 |
free_label(ED_tail_label(e)); |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
/* chkBB: |
| 411 |
* Scans for a correct bb attribute. If available, sets it |
| 412 |
* in the graph and returns 1. |
| 413 |
*/ |
| 414 |
static int chkBB(Agraph_t * g, attrsym_t * G_bb, boxf* bbp) |
| 415 |
{ |
| 416 |
char *s; |
| 417 |
boxf bb; |
| 418 |
|
| 419 |
s = agxget(g, G_bb); |
| 420 |
if (sscanf(s, "%lf,%lf,%lf,%lf", &bb.LL.x, &bb.LL.y, &bb.UR.x, |
| 421 |
&bb.UR.y) == 4) { |
| 422 |
if (bb.LL.y > bb.UR.y) { |
| 423 |
/* If the LL.y coordinate is bigger than the UR.y coordinate, |
| 424 |
* we assume the input was produced using -y, so we normalize |
| 425 |
* the bb. |
| 426 |
*/ |
| 427 |
SWAP(&bb.LL.y, &bb.UR.y); |
| 428 |
} |
| 429 |
*bbp = bb; |
| 430 |
return 1; |
| 431 |
} |
| 432 |
return 0; |
| 433 |
} |
| 434 |
|
| 435 |
static void add_cluster(Agraph_t * g, Agraph_t * subg) |
| 436 |
{ |
| 437 |
int cno; |
| 438 |
cno = ++(GD_n_cluster(g)); |
| 439 |
GD_clust(g) = gv_recalloc(GD_clust(g), GD_n_cluster(g), cno + 1, |
| 440 |
sizeof(graph_t*)); |
| 441 |
GD_clust(g)[cno] = subg; |
| 442 |
do_graph_label(subg); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
static void nop_init_graphs(Agraph_t *, attrsym_t *, attrsym_t *); |
| 447 |
|
| 448 |
/* Process subgraph subg of parent graph g |
| 449 |
* If subg is a cluster, add its bounding box, if any; attach to |
| 450 |
* cluster array of parent, and recursively initialize subg. |
| 451 |
* If not a cluster, recursively call this function on the subgraphs |
| 452 |
* of subg, using parentg as the parent graph. |
| 453 |
*/ |
| 454 |
static void |
| 455 |
dfs(Agraph_t * subg, Agraph_t * parentg, attrsym_t * G_lp, attrsym_t * G_bb) |
| 456 |
{ |
| 457 |
boxf bb; |
| 458 |
|
| 459 |
if (is_a_cluster(subg) && chkBB(subg, G_bb, &bb)) { |
| 460 |
agbindrec(subg, "Agraphinfo_t", sizeof(Agraphinfo_t), true); |
| 461 |
GD_bb(subg) = bb; |
| 462 |
add_cluster(parentg, subg); |
| 463 |
nop_init_graphs(subg, G_lp, G_bb); |
| 464 |
} else { |
| 465 |
graph_t *sg; |
| 466 |
for (sg = agfstsubg(subg); sg; sg = agnxtsubg(sg)) { |
| 467 |
dfs(sg, parentg, G_lp, G_bb); |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
/* Read in clusters and graph label info. |
| 473 |
* A subgraph is a cluster if its name starts with "cluster" and |
| 474 |
* it has a valid bb. |
| 475 |
*/ |
| 476 |
static void |
| 477 |
nop_init_graphs(Agraph_t * g, attrsym_t * G_lp, attrsym_t * G_bb) |
| 478 |
{ |
| 479 |
graph_t *subg; |
| 480 |
char *s; |
| 481 |
double x, y; |
| 482 |
|
| 483 |
if (GD_label(g) && G_lp) { |
| 484 |
s = agxget(g, G_lp); |
| 485 |
if (sscanf(s, "%lf,%lf", &x, &y) == 2) { |
| 486 |
GD_label(g)->pos = (pointf){x, y}; |
| 487 |
GD_label(g)->set = true; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
if (!G_bb) |
| 492 |
return; |
| 493 |
for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg)) { |
| 494 |
dfs(subg, g, G_lp, G_bb); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
/* This assumes all nodes have been positioned. |
| 499 |
* It also assumes none of the relevant fields in A*info_t have been set. |
| 500 |
* The input may provide additional position information for |
| 501 |
* clusters, edges and labels. If certain position information |
| 502 |
* is missing, init_nop will use a standard neato technique to |
| 503 |
* supply it. |
| 504 |
* |
| 505 |
* If adjust is false, init_nop does nothing but initialize all |
| 506 |
* of the basic graph information. No tweaking of positions or |
| 507 |
* filling in edge splines is done. |
| 508 |
* |
| 509 |
* Returns 0 on normal success, 1 if layout has a background, and -1 |
| 510 |
* on failure. |
| 511 |
*/ |
| 512 |
int init_nop(Agraph_t * g, int adjust) |
| 513 |
{ |
| 514 |
int i; |
| 515 |
node_t *np; |
| 516 |
pos_edge posEdges; /* How many edges have spline info */ |
| 517 |
attrsym_t *G_lp = agfindgraphattr(g, "lp"); |
| 518 |
attrsym_t *G_bb = agfindgraphattr(g, "bb"); |
| 519 |
int didAdjust = 0; /* Have nodes been moved? */ |
| 520 |
int haveBackground; |
| 521 |
bool translate = !mapbool(agget(g, "notranslate")); |
| 522 |
|
| 523 |
/* If G_bb not defined, define it */ |
| 524 |
if (!G_bb) |
| 525 |
G_bb = agattr_text(g, AGRAPH, "bb", ""); |
| 526 |
|
| 527 |
scan_graph(g); /* mainly to set up GD_neato_nlist */ |
| 528 |
for (i = 0; (np = GD_neato_nlist(g)[i]); i++) { |
| 529 |
if (!hasPos(np) && !startswith(agnameof(np), "cluster")) { |
| 530 |
agerrorf("node %s in graph %s has no position\n", |
| 531 |
agnameof(np), agnameof(g)); |
| 532 |
return -1; |
| 533 |
} |
| 534 |
if (ND_xlabel(np)) |
| 535 |
set_label(np, ND_xlabel(np), "xlp"); |
| 536 |
} |
| 537 |
nop_init_graphs(g, G_lp, G_bb); |
| 538 |
posEdges = nop_init_edges(g); |
| 539 |
|
| 540 |
if (GD_drawing(g)->xdots) { |
| 541 |
haveBackground = 1; |
| 542 |
GD_drawing(g)->ratio_kind = R_NONE; /* Turn off any aspect change if background present */ |
| 543 |
} |
| 544 |
else |
| 545 |
haveBackground = 0; |
| 546 |
|
| 547 |
if (adjust && Nop == 1 && !haveBackground) |
| 548 |
didAdjust = adjustNodes(g); |
| 549 |
|
| 550 |
if (didAdjust) { |
| 551 |
if (GD_label(g)) GD_label(g)->set = false; |
| 552 |
/* FIX: |
| 553 |
* - if nodes are moved, clusters are no longer valid. |
| 554 |
*/ |
| 555 |
} |
| 556 |
|
| 557 |
compute_bb(g); |
| 558 |
|
| 559 |
/* Adjust bounding box for any background */ |
| 560 |
if (haveBackground) |
| 561 |
GD_bb(g) = xdotBB (g); |
| 562 |
|
| 563 |
/* At this point, all bounding boxes should be correctly defined. |
| 564 |
*/ |
| 565 |
|
| 566 |
if (!adjust) { |
| 567 |
node_t *n; |
| 568 |
State = GVSPLINES; |
| 569 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 570 |
ND_coord(n).x = POINTS_PER_INCH * ND_pos(n)[0]; |
| 571 |
ND_coord(n).y = POINTS_PER_INCH * ND_pos(n)[1]; |
| 572 |
} |
| 573 |
} |
| 574 |
else { |
| 575 |
bool didShift; |
| 576 |
if (translate && !haveBackground && (GD_bb(g).LL.x != 0||GD_bb(g).LL.y != 0)) |
| 577 |
neato_translate (g); |
| 578 |
didShift = neato_set_aspect(g); |
| 579 |
/* if we have some edge positions and we either shifted or adjusted, free edge positions */ |
| 580 |
if (posEdges != NoEdges && (didShift || didAdjust)) { |
| 581 |
freeEdgeInfo (g); |
| 582 |
posEdges = NoEdges; |
| 583 |
} |
| 584 |
if (posEdges != AllEdges || Nop == 3) |
| 585 |
spline_edges0(g, false); /* add edges */ |
| 586 |
else |
| 587 |
State = GVSPLINES; |
| 588 |
} |
| 589 |
|
| 590 |
return haveBackground; |
| 591 |
} |
| 592 |
|
| 593 |
static void neato_init_graph (Agraph_t * g) |
| 594 |
{ |
| 595 |
int outdim; |
| 596 |
|
| 597 |
setEdgeType (g, EDGETYPE_LINE); |
| 598 |
outdim = late_int(g, agfindgraphattr(g, "dimen"), 2, 2); |
| 599 |
GD_ndim(agroot(g)) = late_int(g, agfindgraphattr(g, "dim"), outdim, 2); |
| 600 |
Ndim = GD_ndim(g->root) = MIN(GD_ndim(g->root), MAXDIM); |
| 601 |
GD_odim(g->root) = MIN(outdim, Ndim); |
| 602 |
neato_init_node_edge(g); |
| 603 |
} |
| 604 |
|
| 605 |
static int neatoModel(graph_t * g) |
| 606 |
{ |
| 607 |
char *p = agget(g, "model"); |
| 608 |
|
| 609 |
if (!p || streq(p, "")) |
| 610 |
return MODEL_SHORTPATH; |
| 611 |
if (streq(p, "circuit")) |
| 612 |
return MODEL_CIRCUIT; |
| 613 |
if (streq(p, "subset")) |
| 614 |
return MODEL_SUBSET; |
| 615 |
if (streq(p, "shortpath")) |
| 616 |
return MODEL_SHORTPATH; |
| 617 |
if (streq(p, "mds")) { |
| 618 |
if (agattr_text(g, AGEDGE, "len", 0)) |
| 619 |
return MODEL_MDS; |
| 620 |
else { |
| 621 |
agwarningf( |
| 622 |
"edges in graph %s have no len attribute. Hence, the mds model\n", agnameof(g)); |
| 623 |
agerr(AGPREV, "is inappropriate. Reverting to the shortest path model.\n"); |
| 624 |
return MODEL_SHORTPATH; |
| 625 |
} |
| 626 |
} |
| 627 |
agwarningf( |
| 628 |
"Unknown value %s for attribute \"model\" in graph %s - ignored\n", |
| 629 |
p, agnameof(g)); |
| 630 |
return MODEL_SHORTPATH; |
| 631 |
} |
| 632 |
|
| 633 |
static int neatoMode(graph_t * g) |
| 634 |
{ |
| 635 |
char *str; |
| 636 |
int mode = MODE_MAJOR; /* default mode */ |
| 637 |
|
| 638 |
str = agget(g, "mode"); |
| 639 |
if (str && !streq(str, "")) { |
| 640 |
if (streq(str, "KK")) |
| 641 |
mode = MODE_KK; |
| 642 |
else if (streq(str, "major")) |
| 643 |
mode = MODE_MAJOR; |
| 644 |
else if (streq(str, "sgd")) |
| 645 |
mode = MODE_SGD; |
| 646 |
#ifdef DIGCOLA |
| 647 |
else if (streq(str, "hier")) |
| 648 |
mode = MODE_HIER; |
| 649 |
#ifdef IPSEPCOLA |
| 650 |
else if (streq(str, "ipsep")) |
| 651 |
mode = MODE_IPSEP; |
| 652 |
#endif |
| 653 |
#endif |
| 654 |
else |
| 655 |
agwarningf( |
| 656 |
"Illegal value %s for attribute \"mode\" in graph %s - ignored\n", |
| 657 |
str, agnameof(g)); |
| 658 |
} |
| 659 |
|
| 660 |
return mode; |
| 661 |
} |
| 662 |
|
| 663 |
/* checkEdge: |
| 664 |
* |
| 665 |
*/ |
| 666 |
static int checkEdge(PointMap * pm, edge_t * ep, int idx) |
| 667 |
{ |
| 668 |
int i = ND_id(agtail(ep)); |
| 669 |
int j = ND_id(aghead(ep)); |
| 670 |
|
| 671 |
if (i > j) { |
| 672 |
SWAP(&i, &j); |
| 673 |
} |
| 674 |
return insertPM(pm, i, j, idx); |
| 675 |
} |
| 676 |
|
| 677 |
#ifdef DIGCOLA |
| 678 |
/* dfsCycle: |
| 679 |
* dfs for breaking cycles in vtxdata |
| 680 |
*/ |
| 681 |
static void |
| 682 |
dfsCycle (vtx_data* graph, int i,int mode, node_t* nodes[]) |
| 683 |
{ |
| 684 |
node_t *np, *hp; |
| 685 |
int j; |
| 686 |
/* if mode is IPSEP make it an in-edge |
| 687 |
* at both ends, so that an edge constraint won't be generated! |
| 688 |
*/ |
| 689 |
double x = mode==MODE_IPSEP?-1.0:1.0; |
| 690 |
|
| 691 |
np = nodes[i]; |
| 692 |
ND_mark(np) = true; |
| 693 |
ND_onstack(np) = true; |
| 694 |
for (size_t e = 1; e < graph[i].nedges; e++) { |
| 695 |
if (graph[i].edists[e] == 1.0) continue; /* in edge */ |
| 696 |
j = graph[i].edges[e]; |
| 697 |
hp = nodes[j]; |
| 698 |
if (ND_onstack(hp)) { /* back edge: reverse it */ |
| 699 |
graph[i].edists[e] = x; |
| 700 |
size_t f; |
| 701 |
for (f = 1; f < graph[j].nedges && graph[j].edges[f] != i; f++) ; |
| 702 |
assert (f < graph[j].nedges); |
| 703 |
graph[j].edists[f] = -1.0; |
| 704 |
} |
| 705 |
else if (!ND_mark(hp)) dfsCycle(graph, j, mode, nodes); |
| 706 |
|
| 707 |
} |
| 708 |
ND_onstack(np) = false; |
| 709 |
} |
| 710 |
|
| 711 |
/// do a dfs of the vtx_data, looking for cycles, reversing edges |
| 712 |
static void |
| 713 |
acyclic (vtx_data* graph, int nv, int mode, node_t* nodes[]) |
| 714 |
{ |
| 715 |
int i; |
| 716 |
node_t* np; |
| 717 |
|
| 718 |
for (i = 0; i < nv; i++) { |
| 719 |
np = nodes[i]; |
| 720 |
ND_mark(np) = false; |
| 721 |
ND_onstack(np) = false; |
| 722 |
} |
| 723 |
for (i = 0; i < nv; i++) { |
| 724 |
if (ND_mark(nodes[i])) continue; |
| 725 |
dfsCycle (graph, i, mode, nodes); |
| 726 |
} |
| 727 |
|
| 728 |
} |
| 729 |
#endif |
| 730 |
|
| 731 |
/* Create sparse graph representation via arrays. |
| 732 |
* Each node is represented by a vtx_data. |
| 733 |
* The index of each neighbor is stored in the edges array; |
| 734 |
* the corresponding edge lengths and weights go on ewgts and eweights. |
| 735 |
* We do not allocate the latter 2 if the graph does not use them. |
| 736 |
* By convention, graph[i].edges[0] == i. |
| 737 |
* The values graph[i].ewgts[0] and graph[i].eweights[0] are left undefined. |
| 738 |
* |
| 739 |
* In constructing graph from g, we neglect loops. We track multiedges (ignoring |
| 740 |
* direction). Edge weights are additive; the final edge length is the max. |
| 741 |
* |
| 742 |
* If direction is used, we set the edists field, -1 for tail, +1 for head. |
| 743 |
* graph[i].edists[0] is left undefined. If multiedges exist, the direction |
| 744 |
* of the first one encountered is used. Finally, a pass is made to guarantee |
| 745 |
* the graph is acyclic. |
| 746 |
* |
| 747 |
*/ |
| 748 |
static vtx_data *makeGraphData(graph_t * g, int nv, int *nedges, int mode, int model, node_t*** nodedata) |
| 749 |
{ |
| 750 |
int ne = agnedges(g); /* upper bound */ |
| 751 |
float *ewgts = NULL; |
| 752 |
node_t *np; |
| 753 |
edge_t *ep; |
| 754 |
float *eweights = NULL; |
| 755 |
#ifdef DIGCOLA |
| 756 |
float *edists = NULL; |
| 757 |
#endif |
| 758 |
PointMap *ps = newPM(); |
| 759 |
int i, idx; |
| 760 |
|
| 761 |
/* lengths and weights unused in reweight model */ |
| 762 |
bool haveLen = false; |
| 763 |
bool haveWt = false; |
| 764 |
if (model != MODEL_SUBSET) { |
| 765 |
haveLen = agattr_text(g, AGEDGE, "len", 0) != NULL; |
| 766 |
haveWt = E_weight != 0; |
| 767 |
} |
| 768 |
bool haveDir = mode == MODE_HIER || mode == MODE_IPSEP; |
| 769 |
|
| 770 |
vtx_data *graph = gv_calloc(nv, sizeof(vtx_data)); |
| 771 |
node_t** nodes = gv_calloc(nv, sizeof(node_t*)); |
| 772 |
const size_t edges_size = (size_t)(2 * ne + nv); |
| 773 |
int *edges = gv_calloc(edges_size, sizeof(int)); // reserve space for self loops |
| 774 |
if (haveLen || haveDir) |
| 775 |
ewgts = gv_calloc(edges_size, sizeof(float)); |
| 776 |
if (haveWt) |
| 777 |
eweights = gv_calloc(edges_size, sizeof(float)); |
| 778 |
#ifdef DIGCOLA |
| 779 |
if (haveDir) |
| 780 |
edists = gv_calloc(edges_size, sizeof(float)); |
| 781 |
#endif |
| 782 |
|
| 783 |
i = 0; |
| 784 |
ne = 0; |
| 785 |
for (np = agfstnode(g); np; np = agnxtnode(g, np)) { |
| 786 |
int j = 1; /* index of neighbors */ |
| 787 |
clearPM(ps); |
| 788 |
assert(ND_id(np) == i); |
| 789 |
nodes[i] = np; |
| 790 |
graph[i].edges = edges++; /* reserve space for the self loop */ |
| 791 |
if (haveLen || haveDir) |
| 792 |
graph[i].ewgts = ewgts++; |
| 793 |
else |
| 794 |
graph[i].ewgts = NULL; |
| 795 |
if (haveWt) |
| 796 |
graph[i].eweights = eweights++; |
| 797 |
else |
| 798 |
graph[i].eweights = NULL; |
| 799 |
#ifdef DIGCOLA |
| 800 |
if (haveDir) { |
| 801 |
graph[i].edists = edists++; |
| 802 |
} |
| 803 |
else |
| 804 |
graph[i].edists = NULL; |
| 805 |
#endif |
| 806 |
size_t i_nedges = 1; // one for the self |
| 807 |
|
| 808 |
for (ep = agfstedge(g, np); ep; ep = agnxtedge(g, ep, np)) { |
| 809 |
if (aghead(ep) == agtail(ep)) |
| 810 |
continue; /* ignore loops */ |
| 811 |
idx = checkEdge(ps, ep, j); |
| 812 |
if (idx != j) { /* seen before */ |
| 813 |
if (haveWt) |
| 814 |
graph[i].eweights[idx] += ED_factor(ep); |
| 815 |
if (haveLen) { |
| 816 |
graph[i].ewgts[idx] = fmax(graph[i].ewgts[idx], ED_dist(ep)); |
| 817 |
} |
| 818 |
} else { |
| 819 |
node_t *vp = agtail(ep) == np ? aghead(ep) : agtail(ep); |
| 820 |
ne++; |
| 821 |
j++; |
| 822 |
|
| 823 |
*edges++ = ND_id(vp); |
| 824 |
if (haveWt) |
| 825 |
*eweights++ = ED_factor(ep); |
| 826 |
if (haveLen) |
| 827 |
*ewgts++ = ED_dist(ep); |
| 828 |
else if (haveDir) |
| 829 |
*ewgts++ = 1.0; |
| 830 |
#ifdef DIGCOLA |
| 831 |
if (haveDir) { |
| 832 |
char *s = agget(ep,"dir"); |
| 833 |
if(s && startswith(s, "none")) { |
| 834 |
*edists++ = 0; |
| 835 |
} else { |
| 836 |
*edists++ = np == aghead(ep) ? 1.0 : -1.0; |
| 837 |
} |
| 838 |
} |
| 839 |
#endif |
| 840 |
i_nedges++; |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
graph[i].nedges = i_nedges; |
| 845 |
graph[i].edges[0] = i; |
| 846 |
i++; |
| 847 |
} |
| 848 |
#ifdef DIGCOLA |
| 849 |
if (haveDir) { |
| 850 |
/* Make graph acyclic */ |
| 851 |
acyclic (graph, nv, mode, nodes); |
| 852 |
} |
| 853 |
#endif |
| 854 |
|
| 855 |
ne /= 2; /* every edge is counted twice */ |
| 856 |
|
| 857 |
/* If necessary, release extra memory. */ |
| 858 |
if (ne != agnedges(g)) { |
| 859 |
edges = gv_recalloc(graph[0].edges, edges_size, 2 * ne + nv, sizeof(int)); |
| 860 |
if (haveLen) |
| 861 |
ewgts = gv_recalloc(graph[0].ewgts, edges_size, 2 * ne + nv, sizeof(float)); |
| 862 |
if (haveWt) |
| 863 |
eweights = gv_recalloc(graph[0].eweights, edges_size, 2 * ne + nv, sizeof(float)); |
| 864 |
|
| 865 |
for (i = 0; i < nv; i++) { |
| 866 |
const size_t sz = graph[i].nedges; |
| 867 |
graph[i].edges = edges; |
| 868 |
edges += sz; |
| 869 |
if (haveLen) { |
| 870 |
graph[i].ewgts = ewgts; |
| 871 |
ewgts += sz; |
| 872 |
} |
| 873 |
if (haveWt) { |
| 874 |
graph[i].eweights = eweights; |
| 875 |
eweights += sz; |
| 876 |
} |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
*nedges = ne; |
| 881 |
if (nodedata) |
| 882 |
*nodedata = nodes; |
| 883 |
else |
| 884 |
free (nodes); |
| 885 |
freePM(ps); |
| 886 |
return graph; |
| 887 |
} |
| 888 |
|
| 889 |
static void initRegular(graph_t * G, int nG) |
| 890 |
{ |
| 891 |
double a, da; |
| 892 |
node_t *np; |
| 893 |
|
| 894 |
a = 0.0; |
| 895 |
da = 2 * M_PI / nG; |
| 896 |
for (np = agfstnode(G); np; np = agnxtnode(G, np)) { |
| 897 |
ND_pos(np)[0] = nG * Spring_coeff * cos(a); |
| 898 |
ND_pos(np)[1] = nG * Spring_coeff * sin(a); |
| 899 |
ND_pinned(np) = P_SET; |
| 900 |
a = a + da; |
| 901 |
if (Ndim > 2) |
| 902 |
jitter3d(np, nG); |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
#define SLEN(s) (sizeof(s)-1) |
| 907 |
#define SMART "self" |
| 908 |
#define REGULAR "regular" |
| 909 |
#define RANDOM "random" |
| 910 |
|
| 911 |
/* Analyze "start" attribute. If unset, return dflt. |
| 912 |
* If it begins with self, regular, or random, return set init to same, |
| 913 |
* else set init to dflt. |
| 914 |
* If init is random, look for value integer suffix to use a seed; if not |
| 915 |
* found, use time to set seed and store seed in graph. |
| 916 |
* Return seed in seedp. |
| 917 |
* Return init. |
| 918 |
*/ |
| 919 |
int |
| 920 |
setSeed (graph_t * G, int dflt, long* seedp) |
| 921 |
{ |
| 922 |
char *p = agget(G, "start"); |
| 923 |
int init = dflt; |
| 924 |
|
| 925 |
if (!p || *p == '\0') return dflt; |
| 926 |
if (gv_isalpha(*p)) { |
| 927 |
if (startswith(p, SMART)) { |
| 928 |
init = INIT_SELF; |
| 929 |
p += SLEN(SMART); |
| 930 |
} else if (startswith(p, REGULAR)) { |
| 931 |
init = INIT_REGULAR; |
| 932 |
p += SLEN(REGULAR); |
| 933 |
} else if (startswith(p, RANDOM)) { |
| 934 |
init = INIT_RANDOM; |
| 935 |
p += SLEN(RANDOM); |
| 936 |
} |
| 937 |
else init = dflt; |
| 938 |
} |
| 939 |
else if (gv_isdigit(*p)) { |
| 940 |
init = INIT_RANDOM; |
| 941 |
} |
| 942 |
|
| 943 |
if (init == INIT_RANDOM) { |
| 944 |
long seed; |
| 945 |
/* Check for seed value */ |
| 946 |
if (!gv_isdigit(*p) || sscanf(p, "%ld", &seed) < 1) { |
| 947 |
seed = (unsigned) time(NULL); |
| 948 |
agset(G, "start", ITOS(seed)); |
| 949 |
} |
| 950 |
*seedp = seed; |
| 951 |
} |
| 952 |
return init; |
| 953 |
} |
| 954 |
|
| 955 |
/* Allow various weights for the scale factor in used to calculate stress. |
| 956 |
* At present, only 1 or 2 are allowed, with 2 the default. |
| 957 |
*/ |
| 958 |
#define exp_name "stresswt" |
| 959 |
|
| 960 |
static int checkExp (graph_t * G) |
| 961 |
{ |
| 962 |
int exp = late_int(G, agfindgraphattr(G, exp_name), 2, 0); |
| 963 |
if (exp == 0 || exp > 2) { |
| 964 |
agwarningf("%s attribute value must be 1 or 2 - ignoring\n", exp_name); |
| 965 |
exp = 2; |
| 966 |
} |
| 967 |
return exp; |
| 968 |
} |
| 969 |
|
| 970 |
/* Analyzes start attribute, setting seed. |
| 971 |
* If set, |
| 972 |
* If start is regular, places nodes and returns INIT_REGULAR. |
| 973 |
* If start is self, returns INIT_SELF. |
| 974 |
* If start is random, returns INIT_RANDOM |
| 975 |
* Set RNG seed |
| 976 |
* else return default |
| 977 |
* |
| 978 |
*/ |
| 979 |
int checkStart(graph_t * G, int nG, int dflt) |
| 980 |
{ |
| 981 |
long seed; |
| 982 |
int init; |
| 983 |
|
| 984 |
seed = 1; |
| 985 |
init = setSeed (G, dflt, &seed); |
| 986 |
if (N_pos && init != INIT_RANDOM) { |
| 987 |
agwarningf("node positions are ignored unless start=random\n"); |
| 988 |
} |
| 989 |
if (init == INIT_REGULAR) initRegular(G, nG); |
| 990 |
srand48(seed); |
| 991 |
return init; |
| 992 |
} |
| 993 |
|
| 994 |
#ifdef DEBUG_COLA |
| 995 |
void dumpData(graph_t * g, vtx_data * gp, int nv, int ne) |
| 996 |
{ |
| 997 |
node_t *v; |
| 998 |
int i; |
| 999 |
|
| 1000 |
fprintf(stderr, "#nodes %d #edges %d\n", nv, ne); |
| 1001 |
for (v = agfstnode(g); v; v = agnxtnode(g, v)) { |
| 1002 |
fprintf(stderr, "\"%s\" %d\n", agnameof(v), ND_id(v)); |
| 1003 |
} |
| 1004 |
for (i = 0; i < nv; i++) { |
| 1005 |
const size_t n = gp[i].nedges; |
| 1006 |
fprintf(stderr, "[%d] %" PRISIZE_T "\n", i, n); |
| 1007 |
for (size_t j = 0; j < n; j++) { |
| 1008 |
fprintf(stderr, " %3d", gp[i].edges[j]); |
| 1009 |
} |
| 1010 |
fputs("\n", stderr); |
| 1011 |
if (gp[i].ewgts) { |
| 1012 |
fputs(" ewgts", stderr); |
| 1013 |
for (size_t j = 0; j < n; j++) { |
| 1014 |
fprintf(stderr, " %3f", gp[i].ewgts[j]); |
| 1015 |
} |
| 1016 |
fputs("\n", stderr); |
| 1017 |
} |
| 1018 |
if (gp[i].eweights) { |
| 1019 |
fputs(" eweights", stderr); |
| 1020 |
for (size_t j = 0; j < n; j++) { |
| 1021 |
fprintf(stderr, " %3f", gp[i].eweights[j]); |
| 1022 |
} |
| 1023 |
fputs("\n", stderr); |
| 1024 |
} |
| 1025 |
if (gp[i].edists) { |
| 1026 |
fputs(" edists", stderr); |
| 1027 |
for (size_t j = 0; j < n; j++) { |
| 1028 |
fprintf(stderr, " %3f", gp[i].edists[j]); |
| 1029 |
} |
| 1030 |
fputs("\n", stderr); |
| 1031 |
} |
| 1032 |
fputs("\n", stderr); |
| 1033 |
|
| 1034 |
} |
| 1035 |
} |
| 1036 |
void dumpClusterData (cluster_data* dp) |
| 1037 |
{ |
| 1038 |
int i, j, sz; |
| 1039 |
|
| 1040 |
fprintf (stderr, "nvars %d nclusters %d ntoplevel %d\n", dp->nvars, dp->nclusters, dp->ntoplevel); |
| 1041 |
fprintf (stderr, "Clusters:\n"); |
| 1042 |
for (i = 0; i < dp->nclusters; i++) { |
| 1043 |
sz = dp->clustersizes[i]; |
| 1044 |
fprintf (stderr, " [%d] %d vars\n", i, sz); |
| 1045 |
for (j = 0; j < sz; j++) |
| 1046 |
fprintf (stderr, " %d", dp->clusters[i][j]); |
| 1047 |
fprintf (stderr, "\n"); |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
fprintf (stderr, "Toplevel:\n"); |
| 1052 |
for (i = 0; i < dp->ntoplevel; i++) |
| 1053 |
fprintf (stderr, " %d\n", dp->toplevel[i]); |
| 1054 |
|
| 1055 |
fprintf (stderr, "Boxes:\n"); |
| 1056 |
for (i = 0; i < dp->nclusters; i++) { |
| 1057 |
boxf bb = dp->bb[i]; |
| 1058 |
fprintf (stderr, " (%f,%f) (%f,%f)\n", bb.LL.x, bb.LL.y, bb.UR.x, bb.UR.y); |
| 1059 |
} |
| 1060 |
} |
| 1061 |
void dumpOpts (ipsep_options* opp, int nv) |
| 1062 |
{ |
| 1063 |
int i; |
| 1064 |
|
| 1065 |
fprintf (stderr, "diredges %d edge_gap %f noverlap %d gap (%f,%f)\n", opp->diredges, opp->edge_gap, opp->noverlap, opp->gap.x, opp->gap.y); |
| 1066 |
for (i = 0; i < nv; i++) |
| 1067 |
fprintf (stderr, " (%f,%f)\n", opp->nsize[i].x, opp->nsize[i].y); |
| 1068 |
if (opp->clusters) |
| 1069 |
dumpClusterData (opp->clusters); |
| 1070 |
} |
| 1071 |
#endif |
| 1072 |
|
| 1073 |
/* Solve stress using majorization. |
| 1074 |
* Old neato attributes to incorporate: |
| 1075 |
* weight |
| 1076 |
* mode will be MODE_MAJOR, MODE_HIER or MODE_IPSEP |
| 1077 |
*/ |
| 1078 |
static void |
| 1079 |
majorization(graph_t *mg, graph_t * g, int nv, int mode, int model, int dim, adjust_data* am) |
| 1080 |
{ |
| 1081 |
#if !defined(DIGCOLA) || !defined(IPSEPCOLA) |
| 1082 |
(void)mg; |
| 1083 |
(void)am; |
| 1084 |
#endif |
| 1085 |
|
| 1086 |
int ne; |
| 1087 |
int rv = 0; |
| 1088 |
node_t *v; |
| 1089 |
vtx_data *gp; |
| 1090 |
node_t** nodes; |
| 1091 |
int init = checkStart(g, nv, mode == MODE_HIER ? INIT_SELF : INIT_RANDOM); |
| 1092 |
int opts = checkExp (g); |
| 1093 |
|
| 1094 |
if (init == INIT_SELF) |
| 1095 |
opts |= opt_smart_init; |
| 1096 |
|
| 1097 |
double **coords = gv_calloc(dim, sizeof(double *)); |
| 1098 |
coords[0] = gv_calloc(nv * dim, sizeof(double)); |
| 1099 |
for (int i = 1; i < Ndim; i++) { |
| 1100 |
coords[i] = coords[0] + i * nv; |
| 1101 |
} |
| 1102 |
if (Verbose) { |
| 1103 |
fprintf(stderr, "model %d smart_init %d stresswt %d iterations %d tol %f\n", |
| 1104 |
model, init == INIT_SELF, opts & opt_exp_flag, MaxIter, Epsilon); |
| 1105 |
fprintf(stderr, "convert graph: "); |
| 1106 |
start_timer(); |
| 1107 |
fprintf(stderr, "majorization\n"); |
| 1108 |
} |
| 1109 |
gp = makeGraphData(g, nv, &ne, mode, model, &nodes); |
| 1110 |
|
| 1111 |
if (Verbose) { |
| 1112 |
fprintf(stderr, "%d nodes %.2f sec\n", nv, elapsed_sec()); |
| 1113 |
} |
| 1114 |
|
| 1115 |
#ifdef DIGCOLA |
| 1116 |
if (mode != MODE_MAJOR) { |
| 1117 |
double lgap = late_double(g, agfindgraphattr(g, "levelsgap"), 0.0, -DBL_MAX); |
| 1118 |
if (mode == MODE_HIER) { |
| 1119 |
rv = stress_majorization_with_hierarchy(gp, nv, coords, nodes, Ndim, |
| 1120 |
opts, model, MaxIter, lgap); |
| 1121 |
} |
| 1122 |
#ifdef IPSEPCOLA |
| 1123 |
else { |
| 1124 |
char* str; |
| 1125 |
ipsep_options opt; |
| 1126 |
cluster_data cs = cluster_map(mg,g); |
| 1127 |
pointf *nsize = gv_calloc(nv, sizeof(pointf)); |
| 1128 |
opt.edge_gap = lgap; |
| 1129 |
opt.nsize = nsize; |
| 1130 |
opt.clusters = cs; |
| 1131 |
str = agget(g, "diredgeconstraints"); |
| 1132 |
if (mapbool(str)) { |
| 1133 |
opt.diredges = 1; |
| 1134 |
if(Verbose) |
| 1135 |
fprintf(stderr,"Generating Edge Constraints...\n"); |
| 1136 |
} else if (str && !strncasecmp(str,"hier",4)) { |
| 1137 |
opt.diredges = 2; |
| 1138 |
if(Verbose) |
| 1139 |
fprintf(stderr,"Generating DiG-CoLa Edge Constraints...\n"); |
| 1140 |
} |
| 1141 |
else opt.diredges = 0; |
| 1142 |
if (am->mode == AM_IPSEP) { |
| 1143 |
opt.noverlap = 1; |
| 1144 |
if(Verbose) |
| 1145 |
fprintf(stderr,"Generating Non-overlap Constraints...\n"); |
| 1146 |
} else if (am->mode == AM_VPSC) { |
| 1147 |
opt.noverlap = 2; |
| 1148 |
if(Verbose) |
| 1149 |
fprintf(stderr,"Removing overlaps as postprocess...\n"); |
| 1150 |
} |
| 1151 |
else opt.noverlap = 0; |
| 1152 |
const expand_t margin = sepFactor (g); |
| 1153 |
/* Multiply by 2 since opt.gap is the gap size, not the margin */ |
| 1154 |
if (margin.doAdd) { |
| 1155 |
opt.gap.x = 2.0*PS2INCH(margin.x); |
| 1156 |
opt.gap.y = 2.0*PS2INCH(margin.y); |
| 1157 |
} |
| 1158 |
else opt.gap.x = opt.gap.y = 2.0*PS2INCH(DFLT_MARGIN); |
| 1159 |
if(Verbose) |
| 1160 |
fprintf(stderr,"gap=%f,%f\n",opt.gap.x,opt.gap.y); |
| 1161 |
{ |
| 1162 |
size_t i = 0; |
| 1163 |
for (v = agfstnode(g); v; v = agnxtnode(g, v),i++) { |
| 1164 |
nsize[i].x = ND_width(v); |
| 1165 |
nsize[i].y = ND_height(v); |
| 1166 |
} |
| 1167 |
} |
| 1168 |
|
| 1169 |
#ifdef DEBUG_COLA |
| 1170 |
fprintf (stderr, "nv %d ne %d Ndim %d model %d MaxIter %d\n", nv, ne, Ndim, model, MaxIter); |
| 1171 |
fprintf (stderr, "Nodes:\n"); |
| 1172 |
for (int i = 0; i < nv; i++) { |
| 1173 |
fprintf (stderr, " %s (%f,%f)\n", nodes[i]->name, coords[0][i], coords[1][i]); |
| 1174 |
} |
| 1175 |
fprintf (stderr, "\n"); |
| 1176 |
dumpData(g, gp, nv, ne); |
| 1177 |
fprintf (stderr, "\n"); |
| 1178 |
dumpOpts (&opt, nv); |
| 1179 |
#endif |
| 1180 |
rv = stress_majorization_cola(gp, nv, coords, nodes, Ndim, model, MaxIter, &opt); |
| 1181 |
freeClusterData(cs); |
| 1182 |
free (nsize); |
| 1183 |
} |
| 1184 |
#endif |
| 1185 |
} |
| 1186 |
else |
| 1187 |
#endif |
| 1188 |
rv = stress_majorization_kD_mkernel(gp, nv, coords, nodes, Ndim, opts, model, MaxIter); |
| 1189 |
|
| 1190 |
if (rv < 0) { |
| 1191 |
agerr(AGPREV, "layout aborted\n"); |
| 1192 |
} |
| 1193 |
else for (v = agfstnode(g); v; v = agnxtnode(g, v)) { /* store positions back in nodes */ |
| 1194 |
int idx = ND_id(v); |
| 1195 |
for (int i = 0; i < Ndim; i++) { |
| 1196 |
ND_pos(v)[i] = coords[i][idx]; |
| 1197 |
} |
| 1198 |
} |
| 1199 |
freeGraphData(gp); |
| 1200 |
free(coords[0]); |
| 1201 |
free(coords); |
| 1202 |
free(nodes); |
| 1203 |
} |
| 1204 |
|
| 1205 |
static void subset_model(Agraph_t * G, int nG) |
| 1206 |
{ |
| 1207 |
int i, j, ne; |
| 1208 |
vtx_data *gp; |
| 1209 |
|
| 1210 |
gp = makeGraphData(G, nG, &ne, MODE_KK, MODEL_SUBSET, NULL); |
| 1211 |
DistType **Dij = compute_apsp_artificial_weights(gp, nG); |
| 1212 |
for (i = 0; i < nG; i++) { |
| 1213 |
for (j = 0; j < nG; j++) { |
| 1214 |
GD_dist(G)[i][j] = Dij[i][j]; |
| 1215 |
} |
| 1216 |
} |
| 1217 |
free(Dij[0]); |
| 1218 |
free(Dij); |
| 1219 |
freeGraphData(gp); |
| 1220 |
} |
| 1221 |
|
| 1222 |
/* Assume the matrix already contains shortest path values. |
| 1223 |
* Use the actual lengths provided the input for edges. |
| 1224 |
*/ |
| 1225 |
static void mds_model(graph_t * g) |
| 1226 |
{ |
| 1227 |
long i, j; |
| 1228 |
node_t *v; |
| 1229 |
edge_t *e; |
| 1230 |
|
| 1231 |
for (v = agfstnode(g); v; v = agnxtnode(g, v)) { |
| 1232 |
for (e = agfstout(g, v); e; e = agnxtout(g, e)) { |
| 1233 |
i = AGSEQ(agtail(e)); |
| 1234 |
j = AGSEQ(aghead(e)); |
| 1235 |
if (i == j) |
| 1236 |
continue; |
| 1237 |
GD_dist(g)[i][j] = GD_dist(g)[j][i] = ED_dist(e); |
| 1238 |
} |
| 1239 |
} |
| 1240 |
} |
| 1241 |
|
| 1242 |
/// solve using gradient descent a la Kamada-Kawai |
| 1243 |
static void kkNeato(Agraph_t * g, int nG, int model) |
| 1244 |
{ |
| 1245 |
if (model == MODEL_SUBSET) { |
| 1246 |
subset_model(g, nG); |
| 1247 |
} else if (model == MODEL_CIRCUIT) { |
| 1248 |
if (!circuit_model(g, nG)) { |
| 1249 |
agwarningf( |
| 1250 |
"graph %s is disconnected. Hence, the circuit model\n", |
| 1251 |
agnameof(g)); |
| 1252 |
agerr(AGPREV, |
| 1253 |
"is undefined. Reverting to the shortest path model.\n"); |
| 1254 |
agerr(AGPREV, |
| 1255 |
"Alternatively, consider running neato using -Gpack=true or decomposing\n"); |
| 1256 |
agerr(AGPREV, "the graph into connected components.\n"); |
| 1257 |
shortest_path(g, nG); |
| 1258 |
} |
| 1259 |
} else if (model == MODEL_MDS) { |
| 1260 |
shortest_path(g, nG); |
| 1261 |
mds_model(g); |
| 1262 |
} else |
| 1263 |
shortest_path(g, nG); |
| 1264 |
initial_positions(g, nG); |
| 1265 |
diffeq_model(g, nG); |
| 1266 |
if (Verbose) { |
| 1267 |
fprintf(stderr, "Solving model %d iterations %d tol %f\n", |
| 1268 |
model, MaxIter, Epsilon); |
| 1269 |
start_timer(); |
| 1270 |
} |
| 1271 |
solve_model(g, nG); |
| 1272 |
} |
| 1273 |
|
| 1274 |
/// use stress optimization to layout a single component |
| 1275 |
static void |
| 1276 |
neatoLayout(Agraph_t * mg, Agraph_t * g, int layoutMode, int layoutModel, |
| 1277 |
adjust_data* am) |
| 1278 |
{ |
| 1279 |
int nG; |
| 1280 |
char *str; |
| 1281 |
|
| 1282 |
if ((str = agget(g, "maxiter"))) |
| 1283 |
MaxIter = atoi(str); |
| 1284 |
else if (layoutMode == MODE_MAJOR) |
| 1285 |
MaxIter = DFLT_ITERATIONS; |
| 1286 |
else if (layoutMode == MODE_SGD) |
| 1287 |
MaxIter = 30; |
| 1288 |
else |
| 1289 |
MaxIter = 100 * agnnodes(g); |
| 1290 |
|
| 1291 |
nG = scan_graph_mode(g, layoutMode); |
| 1292 |
if (nG < 2 || MaxIter < 0) |
| 1293 |
return; |
| 1294 |
if (layoutMode == MODE_KK) |
| 1295 |
kkNeato(g, nG, layoutModel); |
| 1296 |
else if (layoutMode == MODE_SGD) |
| 1297 |
sgd(g, layoutModel); |
| 1298 |
else |
| 1299 |
majorization(mg, g, nG, layoutMode, layoutModel, Ndim, am); |
| 1300 |
} |
| 1301 |
|
| 1302 |
/* If dimension == 3 and z attribute is declared, |
| 1303 |
* attach z value to nodes if not defined. |
| 1304 |
*/ |
| 1305 |
static void addZ (Agraph_t* g) |
| 1306 |
{ |
| 1307 |
node_t* n; |
| 1308 |
agxbuf buf = {0}; |
| 1309 |
|
| 1310 |
if (Ndim >= 3 && N_z) { |
| 1311 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 1312 |
agxbprint(&buf, "%lf", POINTS_PER_INCH * ND_pos(n)[2]); |
| 1313 |
agxset(n, N_z, agxbuse(&buf)); |
| 1314 |
} |
| 1315 |
} |
| 1316 |
agxbfree(&buf); |
| 1317 |
} |
| 1318 |
|
| 1319 |
#ifdef IPSEPCOLA |
| 1320 |
static void |
| 1321 |
addCluster (graph_t* g) |
| 1322 |
{ |
| 1323 |
graph_t *subg; |
| 1324 |
for (subg = agfstsubg(agroot(g)); subg; subg = agnxtsubg(subg)) { |
| 1325 |
if (is_a_cluster(subg)) { |
| 1326 |
agbindrec(subg, "Agraphinfo_t", sizeof(Agraphinfo_t), true); |
| 1327 |
add_cluster(g, subg); |
| 1328 |
compute_bb(subg); |
| 1329 |
} |
| 1330 |
} |
| 1331 |
} |
| 1332 |
#endif |
| 1333 |
|
| 1334 |
/* Simple wrapper to compute graph's bb, then route edges after |
| 1335 |
* a possible aspect ratio adjustment. |
| 1336 |
*/ |
| 1337 |
static void doEdges(Agraph_t* g) |
| 1338 |
{ |
| 1339 |
compute_bb(g); |
| 1340 |
spline_edges0(g, true); |
| 1341 |
} |
| 1342 |
|
| 1343 |
void neato_layout(Agraph_t * g) |
| 1344 |
{ |
| 1345 |
int layoutMode; |
| 1346 |
int model; |
| 1347 |
pack_mode mode; |
| 1348 |
pack_info pinfo; |
| 1349 |
adjust_data am; |
| 1350 |
double save_scale = PSinputscale; |
| 1351 |
|
| 1352 |
if (Nop) { |
| 1353 |
int ret; |
| 1354 |
PSinputscale = POINTS_PER_INCH; |
| 1355 |
neato_init_graph(g); |
| 1356 |
addZ (g); |
| 1357 |
ret = init_nop(g, 1); |
| 1358 |
if (ret < 0) { |
| 1359 |
agerr(AGPREV, "as required by the -n flag\n"); |
| 1360 |
return; |
| 1361 |
} |
| 1362 |
else gv_postprocess(g, 0); |
| 1363 |
} else { |
| 1364 |
bool noTranslate = mapbool(agget(g, "notranslate")); |
| 1365 |
PSinputscale = get_inputscale (g); |
| 1366 |
neato_init_graph(g); |
| 1367 |
layoutMode = neatoMode(g); |
| 1368 |
graphAdjustMode (g, &am, 0); |
| 1369 |
model = neatoModel(g); |
| 1370 |
mode = getPackModeInfo (g, l_undef, &pinfo); |
| 1371 |
Pack = getPack(g, -1, CL_OFFSET); |
| 1372 |
/* pack if just packmode defined. */ |
| 1373 |
if (mode == l_undef) { |
| 1374 |
/* If the user has not indicated packing but we are |
| 1375 |
* using the new neato, turn packing on. |
| 1376 |
*/ |
| 1377 |
if (Pack < 0 && layoutMode) |
| 1378 |
Pack = CL_OFFSET; |
| 1379 |
pinfo.mode = l_node; |
| 1380 |
} else if (Pack < 0) |
| 1381 |
Pack = CL_OFFSET; |
| 1382 |
if (Pack >= 0) { |
| 1383 |
graph_t *gc; |
| 1384 |
graph_t **cc; |
| 1385 |
size_t n_cc; |
| 1386 |
bool pin; |
| 1387 |
|
| 1388 |
cc = pccomps(g, &n_cc, cc_pfx, &pin); |
| 1389 |
|
| 1390 |
if (n_cc > 1) { |
| 1391 |
bool *bp; |
| 1392 |
for (size_t i = 0; i < n_cc; i++) { |
| 1393 |
gc = cc[i]; |
| 1394 |
(void)graphviz_node_induce(gc, NULL); |
| 1395 |
neatoLayout(g, gc, layoutMode, model, &am); |
| 1396 |
removeOverlapWith(gc, &am); |
| 1397 |
setEdgeType (gc, EDGETYPE_LINE); |
| 1398 |
if (noTranslate) doEdges(gc); |
| 1399 |
else spline_edges(gc); |
| 1400 |
} |
| 1401 |
if (pin) { |
| 1402 |
bp = gv_calloc(n_cc, sizeof(bool)); |
| 1403 |
bp[0] = true; |
| 1404 |
} else |
| 1405 |
bp = NULL; |
| 1406 |
pinfo.margin = (unsigned)Pack; |
| 1407 |
pinfo.fixed = bp; |
| 1408 |
pinfo.doSplines = true; |
| 1409 |
packGraphs(n_cc, cc, g, &pinfo); |
| 1410 |
free(bp); |
| 1411 |
} |
| 1412 |
else { |
| 1413 |
neatoLayout(g, g, layoutMode, model, &am); |
| 1414 |
removeOverlapWith(g, &am); |
| 1415 |
if (noTranslate) doEdges(g); |
| 1416 |
else spline_edges(g); |
| 1417 |
} |
| 1418 |
compute_bb(g); |
| 1419 |
addZ (g); |
| 1420 |
|
| 1421 |
/* cleanup and remove component subgraphs */ |
| 1422 |
for (size_t i = 0; i < n_cc; i++) { |
| 1423 |
gc = cc[i]; |
| 1424 |
free_scan_graph(gc); |
| 1425 |
agdelrec (gc, "Agraphinfo_t"); |
| 1426 |
agdelete(g, gc); |
| 1427 |
} |
| 1428 |
free (cc); |
| 1429 |
#ifdef IPSEPCOLA |
| 1430 |
addCluster (g); |
| 1431 |
#endif |
| 1432 |
} else { |
| 1433 |
neatoLayout(g, g, layoutMode, model, &am); |
| 1434 |
removeOverlapWith(g, &am); |
| 1435 |
addZ (g); |
| 1436 |
if (noTranslate) doEdges(g); |
| 1437 |
else spline_edges(g); |
| 1438 |
} |
| 1439 |
gv_postprocess(g, !noTranslate); |
| 1440 |
} |
| 1441 |
PSinputscale = save_scale; |
| 1442 |
} |
| 1443 |
|
| 1444 |
/** |
| 1445 |
* @dir lib/neatogen |
| 1446 |
* @brief "spring model" layout engine, API neatogen/neatoprocs.h |
| 1447 |
* @ingroup engines |
| 1448 |
* |
| 1449 |
* [Neato layout user manual](https://graphviz.org/docs/layouts/neato/) |
| 1450 |
* |
| 1451 |
* Other @ref engines |
| 1452 |
*/ |