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