Graphviz 14.0.5~dev.20251117.1017
Loading...
Searching...
No Matches
neatoinit.c
Go to the documentation of this file.
1
8/*************************************************************************
9 * Copyright (c) 2011 AT&T Intellectual Property
10 * All rights reserved. This program and the accompanying materials
11 * are made available under the terms of the Eclipse Public License v1.0
12 * which accompanies this distribution, and is available at
13 * https://www.eclipse.org/legal/epl-v10.html
14 *
15 * Contributors: Details at https://graphviz.org
16 *************************************************************************/
17
18
19#include "config.h"
20
21#include <time.h>
22#ifndef _WIN32
23#include <unistd.h>
24#endif
25#include <neatogen/neato.h>
26#include <pack/pack.h>
27#include <neatogen/stress.h>
28#ifdef DIGCOLA
29#include <neatogen/digcola.h>
30#endif
31#include <neatogen/kkutils.h>
32#include <common/pointset.h>
33#include <common/render.h>
34#include <common/utils.h>
35#include <neatogen/sgd.h>
36#include <cgraph/cgraph.h>
37#include <float.h>
38#include <stdatomic.h>
39#include <stdbool.h>
40#include <stddef.h>
41#include <util/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] = 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,*cn;
204 int i,j,nclusters=0;
205 bitarray_t assigned = bitarray_new(agnnodes(g));
206 cluster_data cdata = {0};
207
208 cdata.ntoplevel = agnnodes(g);
209 for (subg = agfstsubg(mastergraph); subg; subg = agnxtsubg(subg)) {
210 if (is_a_cluster(subg)) {
211 nclusters++;
212 }
213 }
214 cdata.nvars=0;
215 cdata.nclusters = nclusters;
216 cs = cdata.clusters = gv_calloc(nclusters, sizeof(int*));
217 cn = cdata.clustersizes = gv_calloc(nclusters, sizeof(int));
218 for (subg = agfstsubg(mastergraph); subg; subg = agnxtsubg(subg)) {
219 /* clusters are processed by separate calls to ordered_edges */
220 if (is_a_cluster(subg)) {
221 int *c;
222
223 *cn = agnnodes(subg);
224 cdata.nvars += *cn;
225 c = *cs++ = gv_calloc(*cn++, sizeof(int));
226 for (n = agfstnode(subg); n; n = agnxtnode(subg, n)) {
227 node_t *gn;
228 int ind = 0;
229 for (gn = agfstnode(g); gn; gn = agnxtnode(g, gn)) {
230 if(AGSEQ(gn)==AGSEQ(n)) break;
231 ind++;
232 }
233 *c++=ind;
234 bitarray_set(&assigned, ind, true);
235 cdata.ntoplevel--;
236 }
237 }
238 }
239 cdata.bb = gv_calloc(cdata.nclusters, sizeof(boxf));
240 cdata.toplevel = gv_calloc(cdata.ntoplevel, sizeof(int));
241 for(i=j=0;i<agnnodes(g);i++) {
242 if(!bitarray_get(assigned, i)) {
243 cdata.toplevel[j++] = i;
244 }
245 }
246 assert(cdata.ntoplevel == agnnodes(g) - cdata.nvars);
247 bitarray_reset(&assigned);
248 return cdata;
249}
250
251static void freeClusterData(cluster_data c) {
252 if (c.nclusters > 0) {
253 free(c.clusters[0]);
254 free(c.clusters);
255 free(c.clustersizes);
256 free(c.toplevel);
257 free(c.bb);
258 }
259}
260#endif
261
262/* Attempt to use already existing pos info for spline
263 * Return 1 if successful, 0 otherwise.
264 * Assume E_pos != NULL and ED_spl(e) == NULL.
265 */
266static int user_spline(attrsym_t * E_pos, edge_t * e)
267{
268 int nc;
269 pointf *pp;
270 double x, y;
271 bool sflag = false, eflag = false;
272 pointf sp = { 0, 0 }, ep = { 0, 0};
273 bezier *newspl;
274 static atomic_flag warned;
275
276 const char *pos = agxget(e, E_pos);
277 if (*pos == '\0')
278 return 0;
279
280 uint32_t stype, etype;
281 arrow_flags(e, &stype, &etype);
282 for (bool more = true; more; ) {
283 /* check for s head */
284 if (sscanf(pos, "s,%lf,%lf%n", &x, &y, &nc) == 2) {
285 sflag = true;
286 pos += nc;
287 sp = (pointf){.x = x, .y = y};
288 }
289
290 /* check for e head */
291 if (sscanf(pos, " e,%lf,%lf%n", &x, &y, &nc) == 2) {
292 eflag = true;
293 pos += nc;
294 ep = (pointf){.x = x, .y = y};
295 }
296
297 const size_t npts = numFields(pos); // count potential points
298 if (npts < 4 || npts % 3 != 1) {
300 if (!atomic_flag_test_and_set(&warned)) {
301 agwarningf("pos attribute for edge (%s,%s) doesn't have 3n+1 points\n", agnameof(agtail(e)), agnameof(aghead(e)));
302 }
303 return 0;
304 }
305 pointf *ps = gv_calloc(npts, sizeof(pointf));
306 pp = ps;
307 for (size_t n = npts; n > 0; --n) {
308 if (sscanf(pos, "%lf,%lf%n", &x, &y, &nc) < 2) {
309 if (!atomic_flag_test_and_set(&warned)) {
310 agwarningf("syntax error in pos attribute for edge (%s,%s)\n", agnameof(agtail(e)), agnameof(aghead(e)));
311 }
312 free(ps);
314 return 0;
315 }
316 pos += nc;
317 *pp = (pointf){.x = x, .y = y};
318 pp++;
319 }
320 while (gv_isspace(*pos)) pos++;
321 if (*pos == '\0')
322 more = false;
323 else
324 pos++;
325
326 /* parsed successfully; create spline */
327 newspl = new_spline(e, npts);
328 if (sflag) {
329 newspl->sflag = stype;
330 newspl->sp = sp;
331 }
332 if (eflag) {
333 newspl->eflag = etype;
334 newspl->ep = ep;
335 }
336 for (size_t i = 0; i < npts; i++) {
337 newspl->list[i] = ps[i];
338 }
339 free(ps);
340 }
341
342 if (ED_label(e))
343 set_label(e, ED_label(e), "lp");
344 if (ED_xlabel(e))
345 set_label(e, ED_xlabel(e), "xlp");
346 if (ED_head_label(e))
347 set_label(e, ED_head_label(e), "head_lp");
348 if (ED_tail_label(e))
349 set_label(e, ED_tail_label(e), "tail_lp");
350
351 return 1;
352}
353
354/* Nop can be:
355 * 0 - do full layout
356 * 1 - assume initial node positions, do (optional) adjust and all splines
357 * 2 - assume final node and edges positions, do nothing except compute
358 * missing splines
359 */
360
361 /* Indicates the amount of edges with position information */
363
364/* Check edges for position info.
365 * If position info exists, check for edge label positions.
366 * Return number of edges with position info.
367 */
369{
370 node_t *n;
371 edge_t *e;
372 int nedges = 0;
373
374 if (agnedges(g) == 0)
375 return AllEdges;
376
377 attrsym_t *const E_pos = agfindedgeattr(g, "pos");
378 if (!E_pos || Nop < 2)
379 return NoEdges;
380
381 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
382 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
383 if (user_spline(E_pos, e)) {
384 nedges++;
385 }
386 }
387 }
388 if (nedges) {
389 if (nedges == agnedges(g))
390 return AllEdges;
391 return SomeEdges;
392 }
393 return NoEdges;
394}
395
396static void freeEdgeInfo (Agraph_t * g)
397{
398 node_t *n;
399 edge_t *e;
400
401 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
402 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
408 }
409 }
410}
411
412/* chkBB:
413 * Scans for a correct bb attribute. If available, sets it
414 * in the graph and returns 1.
415 */
416#define BS "%lf,%lf,%lf,%lf"
417
418static int chkBB(Agraph_t * g, attrsym_t * G_bb, boxf* bbp)
419{
420 char *s;
421 boxf bb;
422
423 s = agxget(g, G_bb);
424 if (sscanf(s, BS, &bb.LL.x, &bb.LL.y, &bb.UR.x, &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)
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, "")) /* if p is NULL or "" */
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 double x = mode==MODE_IPSEP?-1.0:1.0;
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.0) 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.0;
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 float *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(float));
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.0 : -1.0;
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#if defined(_WIN32)
951 seed = (unsigned) time(NULL);
952#else
953 seed = (unsigned) getpid() ^ (unsigned) time(NULL);
954#endif
955 agset(G, "start", ITOS(seed));
956 }
957 *seedp = seed;
958 }
959 return init;
960}
961
962/* Allow various weights for the scale factor in used to calculate stress.
963 * At present, only 1 or 2 are allowed, with 2 the default.
964 */
965#define exp_name "stresswt"
966
967static int checkExp (graph_t * G)
968{
969 int exp = late_int(G, agfindgraphattr(G, exp_name), 2, 0);
970 if (exp == 0 || exp > 2) {
971 agwarningf("%s attribute value must be 1 or 2 - ignoring\n", exp_name);
972 exp = 2;
973 }
974 return exp;
975}
976
977/* Analyzes start attribute, setting seed.
978 * If set,
979 * If start is regular, places nodes and returns INIT_REGULAR.
980 * If start is self, returns INIT_SELF.
981 * If start is random, returns INIT_RANDOM
982 * Set RNG seed
983 * else return default
984 *
985 */
986int checkStart(graph_t * G, int nG, int dflt)
987{
988 long seed;
989 int init;
990
991 seed = 1;
992 init = setSeed (G, dflt, &seed);
993 if (N_pos && init != INIT_RANDOM) {
994 agwarningf("node positions are ignored unless start=random\n");
995 }
996 if (init == INIT_REGULAR) initRegular(G, nG);
997 srand48(seed);
998 return init;
999}
1000
1001#ifdef DEBUG_COLA
1002void dumpData(graph_t * g, vtx_data * gp, int nv, int ne)
1003{
1004 node_t *v;
1005 int i;
1006
1007 fprintf(stderr, "#nodes %d #edges %d\n", nv, ne);
1008 for (v = agfstnode(g); v; v = agnxtnode(g, v)) {
1009 fprintf(stderr, "\"%s\" %d\n", agnameof(v), ND_id(v));
1010 }
1011 for (i = 0; i < nv; i++) {
1012 const size_t n = gp[i].nedges;
1013 fprintf(stderr, "[%d] %" PRISIZE_T "\n", i, n);
1014 for (size_t j = 0; j < n; j++) {
1015 fprintf(stderr, " %3d", gp[i].edges[j]);
1016 }
1017 fputs("\n", stderr);
1018 if (gp[i].ewgts) {
1019 fputs(" ewgts", stderr);
1020 for (size_t j = 0; j < n; j++) {
1021 fprintf(stderr, " %3f", gp[i].ewgts[j]);
1022 }
1023 fputs("\n", stderr);
1024 }
1025 if (gp[i].eweights) {
1026 fputs(" eweights", stderr);
1027 for (size_t j = 0; j < n; j++) {
1028 fprintf(stderr, " %3f", gp[i].eweights[j]);
1029 }
1030 fputs("\n", stderr);
1031 }
1032 if (gp[i].edists) {
1033 fputs(" edists", stderr);
1034 for (size_t j = 0; j < n; j++) {
1035 fprintf(stderr, " %3f", gp[i].edists[j]);
1036 }
1037 fputs("\n", stderr);
1038 }
1039 fputs("\n", stderr);
1040
1041 }
1042}
1043void dumpClusterData (cluster_data* dp)
1044{
1045 int i, j, sz;
1046
1047 fprintf (stderr, "nvars %d nclusters %d ntoplevel %d\n", dp->nvars, dp->nclusters, dp->ntoplevel);
1048 fprintf (stderr, "Clusters:\n");
1049 for (i = 0; i < dp->nclusters; i++) {
1050 sz = dp->clustersizes[i];
1051 fprintf (stderr, " [%d] %d vars\n", i, sz);
1052 for (j = 0; j < sz; j++)
1053 fprintf (stderr, " %d", dp->clusters[i][j]);
1054 fprintf (stderr, "\n");
1055 }
1056
1057
1058 fprintf (stderr, "Toplevel:\n");
1059 for (i = 0; i < dp->ntoplevel; i++)
1060 fprintf (stderr, " %d\n", dp->toplevel[i]);
1061
1062 fprintf (stderr, "Boxes:\n");
1063 for (i = 0; i < dp->nclusters; i++) {
1064 boxf bb = dp->bb[i];
1065 fprintf (stderr, " (%f,%f) (%f,%f)\n", bb.LL.x, bb.LL.y, bb.UR.x, bb.UR.y);
1066 }
1067}
1068void dumpOpts (ipsep_options* opp, int nv)
1069{
1070 int i;
1071
1072 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);
1073 for (i = 0; i < nv; i++)
1074 fprintf (stderr, " (%f,%f)\n", opp->nsize[i].x, opp->nsize[i].y);
1075 if (opp->clusters)
1076 dumpClusterData (opp->clusters);
1077}
1078#endif
1079
1080/* Solve stress using majorization.
1081 * Old neato attributes to incorporate:
1082 * weight
1083 * mode will be MODE_MAJOR, MODE_HIER or MODE_IPSEP
1084 */
1085static void
1086majorization(graph_t *mg, graph_t * g, int nv, int mode, int model, int dim, adjust_data* am)
1087{
1088#if !defined(DIGCOLA) || !defined(IPSEPCOLA)
1089 (void)mg;
1090 (void)am;
1091#endif
1092
1093 int ne;
1094 int rv = 0;
1095 node_t *v;
1096 vtx_data *gp;
1097 node_t** nodes;
1098 int init = checkStart(g, nv, mode == MODE_HIER ? INIT_SELF : INIT_RANDOM);
1099 int opts = checkExp (g);
1100
1101 if (init == INIT_SELF)
1103
1104 double **coords = gv_calloc(dim, sizeof(double *));
1105 coords[0] = gv_calloc(nv * dim, sizeof(double));
1106 for (int i = 1; i < Ndim; i++) {
1107 coords[i] = coords[0] + i * nv;
1108 }
1109 if (Verbose) {
1110 fprintf(stderr, "model %d smart_init %d stresswt %d iterations %d tol %f\n",
1112 fprintf(stderr, "convert graph: ");
1113 start_timer();
1114 fprintf(stderr, "majorization\n");
1115 }
1116 gp = makeGraphData(g, nv, &ne, mode, model, &nodes);
1117
1118 if (Verbose) {
1119 fprintf(stderr, "%d nodes %.2f sec\n", nv, elapsed_sec());
1120 }
1121
1122#ifdef DIGCOLA
1123 if (mode != MODE_MAJOR) {
1124 double lgap = late_double(g, agfindgraphattr(g, "levelsgap"), 0.0, -DBL_MAX);
1125 if (mode == MODE_HIER) {
1126 rv = stress_majorization_with_hierarchy(gp, nv, coords, nodes, Ndim,
1127 opts, model, MaxIter, lgap);
1128 }
1129#ifdef IPSEPCOLA
1130 else {
1131 char* str;
1132 ipsep_options opt;
1133 cluster_data cs = cluster_map(mg,g);
1134 pointf *nsize = gv_calloc(nv, sizeof(pointf));
1135 opt.edge_gap = lgap;
1136 opt.nsize = nsize;
1137 opt.clusters = cs;
1138 str = agget(g, "diredgeconstraints");
1139 if (mapbool(str)) {
1140 opt.diredges = 1;
1141 if(Verbose)
1142 fprintf(stderr,"Generating Edge Constraints...\n");
1143 } else if (str && !strncasecmp(str,"hier",4)) {
1144 opt.diredges = 2;
1145 if(Verbose)
1146 fprintf(stderr,"Generating DiG-CoLa Edge Constraints...\n");
1147 }
1148 else opt.diredges = 0;
1149 if (am->mode == AM_IPSEP) {
1150 opt.noverlap = 1;
1151 if(Verbose)
1152 fprintf(stderr,"Generating Non-overlap Constraints...\n");
1153 } else if (am->mode == AM_VPSC) {
1154 opt.noverlap = 2;
1155 if(Verbose)
1156 fprintf(stderr,"Removing overlaps as postprocess...\n");
1157 }
1158 else opt.noverlap = 0;
1159 const expand_t margin = sepFactor (g);
1160 /* Multiply by 2 since opt.gap is the gap size, not the margin */
1161 if (margin.doAdd) {
1162 opt.gap.x = 2.0*PS2INCH(margin.x);
1163 opt.gap.y = 2.0*PS2INCH(margin.y);
1164 }
1165 else opt.gap.x = opt.gap.y = 2.0*PS2INCH(DFLT_MARGIN);
1166 if(Verbose)
1167 fprintf(stderr,"gap=%f,%f\n",opt.gap.x,opt.gap.y);
1168 {
1169 size_t i = 0;
1170 for (v = agfstnode(g); v; v = agnxtnode(g, v),i++) {
1171 nsize[i].x = ND_width(v);
1172 nsize[i].y = ND_height(v);
1173 }
1174 }
1175
1176#ifdef DEBUG_COLA
1177 fprintf (stderr, "nv %d ne %d Ndim %d model %d MaxIter %d\n", nv, ne, Ndim, model, MaxIter);
1178 fprintf (stderr, "Nodes:\n");
1179 for (int i = 0; i < nv; i++) {
1180 fprintf (stderr, " %s (%f,%f)\n", nodes[i]->name, coords[0][i], coords[1][i]);
1181 }
1182 fprintf (stderr, "\n");
1183 dumpData(g, gp, nv, ne);
1184 fprintf (stderr, "\n");
1185 dumpOpts (&opt, nv);
1186#endif
1187 rv = stress_majorization_cola(gp, nv, coords, nodes, Ndim, model, MaxIter, &opt);
1188 freeClusterData(cs);
1189 free (nsize);
1190 }
1191#endif
1192 }
1193 else
1194#endif
1195 rv = stress_majorization_kD_mkernel(gp, nv, coords, nodes, Ndim, opts, model, MaxIter);
1196
1197 if (rv < 0) {
1198 agerr(AGPREV, "layout aborted\n");
1199 }
1200 else for (v = agfstnode(g); v; v = agnxtnode(g, v)) { /* store positions back in nodes */
1201 int idx = ND_id(v);
1202 for (int i = 0; i < Ndim; i++) {
1203 ND_pos(v)[i] = coords[i][idx];
1204 }
1205 }
1206 freeGraphData(gp);
1207 free(coords[0]);
1208 free(coords);
1209 free(nodes);
1210}
1211
1212static void subset_model(Agraph_t * G, int nG)
1213{
1214 int i, j, ne;
1215 vtx_data *gp;
1216
1217 gp = makeGraphData(G, nG, &ne, MODE_KK, MODEL_SUBSET, NULL);
1219 for (i = 0; i < nG; i++) {
1220 for (j = 0; j < nG; j++) {
1221 GD_dist(G)[i][j] = Dij[i][j];
1222 }
1223 }
1224 free(Dij[0]);
1225 free(Dij);
1226 freeGraphData(gp);
1227}
1228
1229/* Assume the matrix already contains shortest path values.
1230 * Use the actual lengths provided the input for edges.
1231 */
1232static void mds_model(graph_t * g)
1233{
1234 long i, j;
1235 node_t *v;
1236 edge_t *e;
1237
1238 for (v = agfstnode(g); v; v = agnxtnode(g, v)) {
1239 for (e = agfstout(g, v); e; e = agnxtout(g, e)) {
1240 i = AGSEQ(agtail(e));
1241 j = AGSEQ(aghead(e));
1242 if (i == j)
1243 continue;
1244 GD_dist(g)[i][j] = GD_dist(g)[j][i] = ED_dist(e);
1245 }
1246 }
1247}
1248
1250static void kkNeato(Agraph_t * g, int nG, int model)
1251{
1252 if (model == MODEL_SUBSET) {
1253 subset_model(g, nG);
1254 } else if (model == MODEL_CIRCUIT) {
1255 if (!circuit_model(g, nG)) {
1256 agwarningf(
1257 "graph %s is disconnected. Hence, the circuit model\n",
1258 agnameof(g));
1259 agerr(AGPREV,
1260 "is undefined. Reverting to the shortest path model.\n");
1261 agerr(AGPREV,
1262 "Alternatively, consider running neato using -Gpack=true or decomposing\n");
1263 agerr(AGPREV, "the graph into connected components.\n");
1264 shortest_path(g, nG);
1265 }
1266 } else if (model == MODEL_MDS) {
1267 shortest_path(g, nG);
1268 mds_model(g);
1269 } else
1270 shortest_path(g, nG);
1271 initial_positions(g, nG);
1272 diffeq_model(g, nG);
1273 if (Verbose) {
1274 fprintf(stderr, "Solving model %d iterations %d tol %f\n",
1275 model, MaxIter, Epsilon);
1276 start_timer();
1277 }
1278 solve_model(g, nG);
1279}
1280
1282static void
1283neatoLayout(Agraph_t * mg, Agraph_t * g, int layoutMode, int layoutModel,
1284 adjust_data* am)
1285{
1286 int nG;
1287 char *str;
1288
1289 if ((str = agget(g, "maxiter")))
1290 MaxIter = atoi(str);
1291 else if (layoutMode == MODE_MAJOR)
1293 else if (layoutMode == MODE_SGD)
1294 MaxIter = 30;
1295 else
1296 MaxIter = 100 * agnnodes(g);
1297
1298 nG = scan_graph_mode(g, layoutMode);
1299 if (nG < 2 || MaxIter < 0)
1300 return;
1301 if (layoutMode == MODE_KK)
1302 kkNeato(g, nG, layoutModel);
1303 else if (layoutMode == MODE_SGD)
1304 sgd(g, layoutModel);
1305 else
1306 majorization(mg, g, nG, layoutMode, layoutModel, Ndim, am);
1307}
1308
1309/* If dimension == 3 and z attribute is declared,
1310 * attach z value to nodes if not defined.
1311 */
1312static void addZ (Agraph_t* g)
1313{
1314 node_t* n;
1315 char buf[BUFSIZ];
1316
1317 if (Ndim >= 3 && N_z) {
1318 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
1319 snprintf(buf, sizeof(buf), "%lf", POINTS_PER_INCH * ND_pos(n)[2]);
1320 agxset(n, N_z, buf);
1321 }
1322 }
1323}
1324
1325#ifdef IPSEPCOLA
1326static void
1327addCluster (graph_t* g)
1328{
1329 graph_t *subg;
1330 for (subg = agfstsubg(agroot(g)); subg; subg = agnxtsubg(subg)) {
1331 if (is_a_cluster(subg)) {
1332 agbindrec(subg, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
1333 add_cluster(g, subg);
1334 compute_bb(subg);
1335 }
1336 }
1337}
1338#endif
1339
1340/* Simple wrapper to compute graph's bb, then route edges after
1341 * a possible aspect ratio adjustment.
1342 */
1343static void doEdges(Agraph_t* g)
1344{
1345 compute_bb(g);
1346 spline_edges0(g, true);
1347}
1348
1350{
1351 int layoutMode;
1352 int model;
1354 pack_info pinfo;
1355 adjust_data am;
1356 double save_scale = PSinputscale;
1357
1358 if (Nop) {
1359 int ret;
1362 addZ (g);
1363 ret = init_nop(g, 1);
1364 if (ret < 0) {
1365 agerr(AGPREV, "as required by the -n flag\n");
1366 return;
1367 }
1368 else gv_postprocess(g, 0);
1369 } else {
1370 bool noTranslate = mapbool(agget(g, "notranslate"));
1373 layoutMode = neatoMode(g);
1374 graphAdjustMode (g, &am, 0);
1375 model = neatoModel(g);
1376 mode = getPackModeInfo (g, l_undef, &pinfo);
1377 Pack = getPack(g, -1, CL_OFFSET);
1378 /* pack if just packmode defined. */
1379 if (mode == l_undef) {
1380 /* If the user has not indicated packing but we are
1381 * using the new neato, turn packing on.
1382 */
1383 if (Pack < 0 && layoutMode)
1384 Pack = CL_OFFSET;
1385 pinfo.mode = l_node;
1386 } else if (Pack < 0)
1387 Pack = CL_OFFSET;
1388 if (Pack >= 0) {
1389 graph_t *gc;
1390 graph_t **cc;
1391 size_t n_cc;
1392 bool pin;
1393
1394 cc = pccomps(g, &n_cc, cc_pfx, &pin);
1395
1396 if (n_cc > 1) {
1397 bool *bp;
1398 for (size_t i = 0; i < n_cc; i++) {
1399 gc = cc[i];
1400 (void)graphviz_node_induce(gc, NULL);
1401 neatoLayout(g, gc, layoutMode, model, &am);
1402 removeOverlapWith(gc, &am);
1404 if (noTranslate) doEdges(gc);
1405 else spline_edges(gc);
1406 }
1407 if (pin) {
1408 bp = gv_calloc(n_cc, sizeof(bool));
1409 bp[0] = true;
1410 } else
1411 bp = NULL;
1412 pinfo.margin = (unsigned)Pack;
1413 pinfo.fixed = bp;
1414 pinfo.doSplines = true;
1415 packGraphs(n_cc, cc, g, &pinfo);
1416 free(bp);
1417 }
1418 else {
1419 neatoLayout(g, g, layoutMode, model, &am);
1420 removeOverlapWith(g, &am);
1421 if (noTranslate) doEdges(g);
1422 else spline_edges(g);
1423 }
1424 compute_bb(g);
1425 addZ (g);
1426
1427 /* cleanup and remove component subgraphs */
1428 for (size_t i = 0; i < n_cc; i++) {
1429 gc = cc[i];
1430 free_scan_graph(gc);
1431 agdelrec (gc, "Agraphinfo_t");
1432 agdelete(g, gc);
1433 }
1434 free (cc);
1435#ifdef IPSEPCOLA
1436 addCluster (g);
1437#endif
1438 } else {
1439 neatoLayout(g, g, layoutMode, model, &am);
1440 removeOverlapWith(g, &am);
1441 addZ (g);
1442 if (noTranslate) doEdges(g);
1443 else spline_edges(g);
1444 }
1445 gv_postprocess(g, !noTranslate);
1446 }
1447 PSinputscale = save_scale;
1448}
1449
expand_t sepFactor(graph_t *g)
Definition adjust.c:1045
int adjustNodes(graph_t *G)
Definition adjust.c:998
void graphAdjustMode(graph_t *G, adjust_data *dp, char *dflt)
Definition adjust.c:862
int removeOverlapWith(graph_t *G, adjust_data *am)
Definition adjust.c:898
#define DFLT_MARGIN
Definition adjust.h:23
@ AM_VPSC
Definition adjust.h:30
@ AM_IPSEP
Definition adjust.h:30
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:2720
boxf xdotBB(Agraph_t *g)
Definition emit.c:2784
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:123
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:40
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:1250
pos_edge
Definition neatoinit.c:362
@ SomeEdges
Definition neatoinit.c:362
@ NoEdges
Definition neatoinit.c:362
@ AllEdges
Definition neatoinit.c:362
#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 BS
Definition neatoinit.c:416
#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:1086
static void initRegular(graph_t *G, int nG)
Definition neatoinit.c:892
static void mds_model(graph_t *g)
Definition neatoinit.c:1232
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:1283
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:368
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:396
#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:967
#define SLEN(s)
Definition neatoinit.c:909
int checkStart(graph_t *G, int nG, int dflt)
Definition neatoinit.c:986
static void set_label(void *obj, textlabel_t *l, char *name)
Definition neatoinit.c:187
void neato_layout(Agraph_t *g)
Definition neatoinit.c:1349
int setSeed(graph_t *G, int dflt, long *seedp)
Definition neatoinit.c:923
#define exp_name
Definition neatoinit.c:965
static int user_spline(attrsym_t *E_pos, edge_t *e)
Definition neatoinit.c:266
static int chkBB(Agraph_t *g, attrsym_t *G_bb, boxf *bbp)
Definition neatoinit.c:418
static int neatoMode(graph_t *g)
Definition neatoinit.c:636
static void addZ(Agraph_t *g)
Definition neatoinit.c:1312
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:1212
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: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