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