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