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