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