Graphviz 13.1.3~dev.20250813.1130
Loading...
Searching...
No Matches
adjust.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11/* adjust.c
12 * Routines for repositioning nodes after initial layout in
13 * order to reduce/remove node overlaps.
14 */
15
16#include <assert.h>
17#include <neatogen/neato.h>
18#include <common/utils.h>
19#include <float.h>
20#include <math.h>
21#include <neatogen/voronoi.h>
22#include <neatogen/info.h>
23#include <neatogen/edges.h>
24#include <neatogen/site.h>
25#include <neatogen/digcola.h>
26#if defined(HAVE_GTS) && defined(SFDP)
27#include <neatogen/overlap.h>
28#endif
29#include <stdbool.h>
30#ifdef IPSEPCOLA
31#include <vpsc/csolve_VPSC.h>
33#endif
34#include <stddef.h>
35#include <util/agxbuf.h>
36#include <util/alloc.h>
37#include <util/debug.h>
38#include <util/gv_ctype.h>
39#include <util/startswith.h>
40#include <util/strcasecmp.h>
41
42#define SEPFACT 0.8 // default esep/sep
43
44static const double incr = 0.05; /* Increase bounding box by adding
45 * incr * dimension around box.
46 */
47
48typedef struct {
51 Point nw, ne, sw, se;
53} state_t;
54
55static void setBoundBox(state_t *st, Point *ll, Point *ur) {
56 pxmin = ll->x;
57 pxmax = ur->x;
58 pymin = ll->y;
59 pymax = ur->y;
60 st->nw.x = st->sw.x = pxmin;
61 st->ne.x = st->se.x = pxmax;
62 st->nw.y = st->ne.y = pymax;
63 st->sw.y = st->se.y = pymin;
64}
65
67static void freeNodes(void)
68{
69 for (size_t i = 0; i < nsites; i++) {
71 }
72 polyFree();
73 if (nodeInfo != NULL) {
74 free(nodeInfo->verts); // Free vertices
75 }
77}
78
79/* Compute extremes of graph, then set up bounding box.
80 * If user supplied a bounding box, use that;
81 * else if "window" is a graph attribute, use that;
82 * otherwise, define bounding box as a percentage expansion of
83 * graph extremes.
84 * In the first two cases, check that graph fits in bounding box.
85 */
86static void chkBoundBox(state_t *st, Agraph_t *graph) {
87 Point ll, ur;
88
89 double x_min = DBL_MAX;
90 double y_min = DBL_MAX;
91 double x_max = -DBL_MAX;
92 double y_max = -DBL_MAX;
93 assert(nsites > 0);
94 for (size_t i = 0; i < nsites; ++i) {
95 Info_t *ip = &nodeInfo[i];
96 Poly *pp = &ip->poly;
97 double x = ip->site.coord.x;
98 double y = ip->site.coord.y;
99 x_min = fmin(x_min, pp->origin.x + x);
100 y_min = fmin(y_min, pp->origin.y + y);
101 x_max = fmax(x_max, pp->corner.x + x);
102 y_max = fmax(y_max, pp->corner.y + y);
103 }
104
105 // create initial bounding box by adding margin × dimension around box
106 // enclosing nodes
107 char *marg = agget(graph, "voro_margin");
108 const double margin = (marg && *marg != '\0') ? atof(marg) : 0.05;
109 double ydelta = margin * (y_max - y_min);
110 double xdelta = margin * (x_max - x_min);
111 ll.x = x_min - xdelta;
112 ll.y = y_min - ydelta;
113 ur.x = x_max + xdelta;
114 ur.y = y_max + ydelta;
115
116 setBoundBox(st, &ll, &ur);
117}
118
121{
122 int (*polyf)(Poly *, Agnode_t *, double, double);
123
124 assert(agnnodes(graph) >= 0);
125 nsites = (size_t)agnnodes(graph);
126 geominit();
127
128 nodeInfo = gv_calloc(nsites, sizeof(Info_t));
129
131
132 expand_t pmargin = sepFactor (graph);
133
134 if (pmargin.doAdd) {
135 polyf = makeAddPoly;
136 /* we need inches for makeAddPoly */
137 pmargin.x = PS2INCH(pmargin.x);
138 pmargin.y = PS2INCH(pmargin.y);
139 }
140
141 else polyf = makePoly;
142 for (size_t i = 0; i < nsites; i++) {
143 Info_t *ip = &nodeInfo[i];
144 ip->site.coord.x = ND_pos(node)[0];
145 ip->site.coord.y = ND_pos(node)[1];
146
147 if (polyf(&ip->poly, node, pmargin.x, pmargin.y)) {
148 free (nodeInfo);
149 nodeInfo = NULL;
150 return 1;
151 }
152
153 ip->site.sitenbr = i;
154 ip->site.refcnt = 1;
155 ip->node = node;
156 ip->verts = NULL;
157 ip->n_verts = 0;
159 }
160 return 0;
161}
162
163/* sort sites on y, then x, coord */
164static int scomp(const void *S1, const void *S2)
165{
166 const Site *s1 = *(Site *const *)S1;
167 const Site *s2 = *(Site *const *)S2;
168 if (s1->coord.y < s2->coord.y)
169 return -1;
170 if (s1->coord.y > s2->coord.y)
171 return 1;
172 if (s1->coord.x < s2->coord.x)
173 return -1;
174 if (s1->coord.x > s2->coord.x)
175 return 1;
176 return 0;
177}
178
180static void sortSites(state_t *st) {
181 if (st->sites == NULL) {
182 st->sites = gv_calloc(nsites, sizeof(Site*));
183 st->endSite = st->sites + nsites;
184 }
185
186 for (size_t i = 0; i < nsites; i++) {
187 Info_t *ip = &nodeInfo[i];
188 st->sites[i] = &ip->site;
189 ip->verts = NULL;
190 ip->n_verts = 0;
191 ip->site.refcnt = 1;
192 }
193
194 qsort(st->sites, nsites, sizeof(Site *), scomp);
195
196 /* Reset site index for nextOne */
197 st->nextSite = st->sites;
198
199}
200
201static void geomUpdate(state_t *st, int doSort) {
202 if (doSort)
203 sortSites(st);
204
205 /* compute ranges */
206 xmin = DBL_MAX;
207 xmax = -DBL_MAX;
208 assert(nsites > 0);
209 for (size_t i = 0; i < nsites; ++i) {
210 xmin = fmin(xmin, st->sites[i]->coord.x);
211 xmax = fmax(xmax, st->sites[i]->coord.x);
212 }
213 ymin = st->sites[0]->coord.y;
214 ymax = st->sites[nsites - 1]->coord.y;
215
216 deltax = xmax - xmin;
217}
218
219static Site *nextOne(void *state) {
220 state_t *st = state;
221 if (st->nextSite < st->endSite) {
222 return *st->nextSite++;
223 } else
224 return NULL;
225}
226
228static void rmEquality(state_t *st) {
229 sortSites(st);
230
231 for (Site **ip = st->sites; ip < st->endSite; ) {
232 Site **jp = ip + 1;
233 if (jp >= st->endSite ||
234 (*jp)->coord.x != (*ip)->coord.x ||
235 (*jp)->coord.y != (*ip)->coord.y) {
236 ip = jp;
237 continue;
238 }
239
240 /* Find first node kp with position different from ip */
241 int cnt = 2;
242 Site **kp = jp + 1;
243 while (kp < st->endSite &&
244 (*kp)->coord.x == (*ip)->coord.x &&
245 (*kp)->coord.y == (*ip)->coord.y) {
246 cnt++;
247 jp = kp;
248 kp = jp + 1;
249 }
250
251 /* If next node exists and is on the same line */
252 if (kp < st->endSite && (*kp)->coord.y == (*ip)->coord.y) {
253 const double xdel = ((*kp)->coord.x - (*ip)->coord.x) / cnt;
254 int i = 1;
255 for (jp = ip + 1; jp < kp; jp++) {
256 (*jp)->coord.x += i * xdel;
257 i++;
258 }
259 } else { /* nothing is to the right */
260 Info_t *info;
261 for (jp = ip + 1; jp < kp; ip++, jp++) {
262 info = nodeInfo + (*ip)->sitenbr;
263 double xdel = info->poly.corner.x - info->poly.origin.x;
264 info = nodeInfo + (*jp)->sitenbr;
265 xdel += info->poly.corner.x - info->poly.origin.x;
266 (*jp)->coord.x = (*ip)->coord.x + xdel / 2;
267 }
268 }
269 ip = kp;
270 }
271}
272
274static unsigned countOverlap(unsigned iter) {
275 unsigned count = 0;
276
277 for (size_t i = 0; i < nsites; i++)
278 nodeInfo[i].overlaps = false;
279
280 for (size_t i = 0; i < nsites - 1; i++) {
281 Info_t *ip = &nodeInfo[i];
282 for (size_t j = i + 1; j < nsites; j++) {
283 Info_t *jp = &nodeInfo[j];
284 if (polyOverlap(ip->site.coord, &ip->poly, jp->site.coord, &jp->poly)) {
285 count++;
286 ip->overlaps = true;
287 jp->overlaps = true;
288 }
289 }
290 }
291
292 if (Verbose > 1)
293 fprintf(stderr, "overlap [%u] : %u\n", iter, count);
294 return count;
295}
296
297static void increaseBoundBox(state_t *st) {
298 Point ur = {.x = pxmax, .y = pymax};
299 Point ll = {.x = pxmin, .y = pymin};
300
301 const double ydelta = incr * (ur.y - ll.y);
302 const double xdelta = incr * (ur.x - ll.x);
303
304 ur.x += xdelta;
305 ur.y += ydelta;
306 ll.x -= xdelta;
307 ll.y -= ydelta;
308
309 setBoundBox(st, &ll, &ur);
310}
311
313static double areaOf(Point a, Point b, Point c)
314{
315 return fabs(a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)) / 2;
316}
317
318/* Compute centroid of triangle with vertices a, b, c.
319 * Return coordinates in x and y.
320 */
321static void centroidOf(Point a, Point b, Point c, double *x, double *y)
322{
323 *x = (a.x + b.x + c.x) / 3;
324 *y = (a.y + b.y + c.y) / 3;
325}
326
327/* The new position is the centroid of the voronoi polygon. This is the weighted
328 * sum of the centroids of a triangulation, normalized to the total area.
329 */
330static void newpos(Info_t * ip)
331{
332 const Point anchor = ip->verts[0];
333 double totalArea = 0.0;
334 double cx = 0.0;
335 double cy = 0.0;
336 double x;
337 double y;
338
339 for (size_t i = 1; i + 1 < ip->n_verts; ++i) {
340 const Point p = ip->verts[i];
341 const Point q = ip->verts[i + 1];
342 const double area = areaOf(anchor, p, q);
343 centroidOf(anchor, p, q, &x, &y);
344 cx += area * x;
345 cy += area * y;
346 totalArea += area;
347 }
348
349 ip->site.coord.x = cx / totalArea;
350 ip->site.coord.y = cy / totalArea;
351}
352
353 /* Add corners of clipping window to appropriate sites.
354 * A site gets a corner if it is the closest site to that corner.
355 */
356static void addCorners(const state_t *st) {
357 Info_t *ip = nodeInfo;
358 Info_t *sws = ip;
359 Info_t *nws = ip;
360 Info_t *ses = ip;
361 Info_t *nes = ip;
362 double swd = dist_2(ip->site.coord, st->sw);
363 double nwd = dist_2(ip->site.coord, st->nw);
364 double sed = dist_2(ip->site.coord, st->se);
365 double ned = dist_2(ip->site.coord, st->ne);
366
367 for (size_t i = 1; i < nsites; i++) {
368 ip = &nodeInfo[i];
369 double d = dist_2(ip->site.coord, st->sw);
370 if (d < swd) {
371 swd = d;
372 sws = ip;
373 }
374 d = dist_2(ip->site.coord, st->se);
375 if (d < sed) {
376 sed = d;
377 ses = ip;
378 }
379 d = dist_2(ip->site.coord, st->nw);
380 if (d < nwd) {
381 nwd = d;
382 nws = ip;
383 }
384 d = dist_2(ip->site.coord, st->ne);
385 if (d < ned) {
386 ned = d;
387 nes = ip;
388 }
389 }
390
391 addVertex(&sws->site, st->sw.x, st->sw.y);
392 addVertex(&ses->site, st->se.x, st->se.y);
393 addVertex(&nws->site, st->nw.x, st->nw.y);
394 addVertex(&nes->site, st->ne.x, st->ne.y);
395}
396
397 /* Calculate the new position of a site as the centroid
398 * of its voronoi polygon, if it overlaps other nodes.
399 * The polygons are finite by being clipped to the clipping
400 * window.
401 * We first add the corner of the clipping windows to the
402 * vertex lists of the appropriate sites.
403 *
404 * @param st Algorithm state
405 * @param doAll Move all nodes, regardless of overlap
406 */
407static void newPos(const state_t *st, bool doAll) {
408 addCorners(st);
409 for (size_t i = 0; i < nsites; i++) {
410 Info_t *ip = &nodeInfo[i];
411 if (doAll || ip->overlaps)
412 newpos(ip);
413 }
414}
415
416/* Cleanup voronoi memory.
417 * Note that ELcleanup relies on the number
418 * of sites, so should at least be reset every time we use vAdjust.
419 * This could be optimized, over multiple components or
420 * even multiple graphs, but probably not worth it.
421 */
422static void cleanup(void)
423{
424 siteinit(); /* free memory */
425 edgeinit(); /* free memory */
426}
427
428static int vAdjust(state_t *st) {
429 unsigned iterCnt = 0;
430 unsigned badLevel = 0;
431 unsigned increaseCnt = 0;
432
433 unsigned overlapCnt = countOverlap(iterCnt);
434
435 if (overlapCnt == 0)
436 return 0;
437
438 rmEquality(st);
439 geomUpdate(st, 0);
440 voronoi(nextOne, st);
441 for (bool doAll = false;;) {
442 newPos(st, doAll);
443 iterCnt++;
444
445 const unsigned cnt = countOverlap(iterCnt);
446 if (cnt == 0)
447 break;
448 if (cnt >= overlapCnt)
449 badLevel++;
450 else
451 badLevel = 0;
452 overlapCnt = cnt;
453
454 switch (badLevel) {
455 case 0:
456 doAll = true;
457 break;
458 default:
459 doAll = true;
460 increaseCnt++;
462 break;
463 }
464
465 geomUpdate(st, 1);
466 voronoi(nextOne, st);
467 }
468
469 GV_DEBUG("Number of iterations = %u", iterCnt);
470 GV_DEBUG("Number of increases = %u", increaseCnt);
471
472 cleanup();
473 return 1;
474}
475
476static void rePos(void) {
477 double f = 1.0 + incr;
478
479 for (size_t i = 0; i < nsites; i++) {
480 Info_t *ip = &nodeInfo[i];
481 ip->site.coord.x *= f;
482 ip->site.coord.y *= f;
483 }
484}
485
486static int sAdjust(state_t *st) {
487 unsigned iterCnt = 0;
488
489 const unsigned overlapCnt = countOverlap(iterCnt);
490
491 if (overlapCnt == 0)
492 return 0;
493
494 rmEquality(st);
495 while (1) {
496 rePos();
497 iterCnt++;
498
499 const unsigned cnt = countOverlap(iterCnt);
500 if (cnt == 0)
501 break;
502 }
503
504 if (Verbose) {
505 fprintf(stderr, "Number of iterations = %u\n", iterCnt);
506 }
507
508 return 1;
509}
510
512static void updateGraph(void)
513{
514 for (size_t i = 0; i < nsites; i++) {
515 Info_t *ip = &nodeInfo[i];
516 ND_pos(ip->node)[0] = ip->site.coord.x;
517 ND_pos(ip->node)[1] = ip->site.coord.y;
518 }
519}
520
521#define ELS "|edgelabel|"
522 /* Return true if node name starts with ELS */
523#define IS_LNODE(n) startswith(agnameof(n), ELS)
524
526double *getSizes(Agraph_t * g, pointf pad, int* n_elabels, int** elabels)
527{
528 double *sizes = gv_calloc(Ndim * agnnodes(g), sizeof(double));
529 int nedge_nodes = 0;
530
531 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
532 if (elabels && IS_LNODE(n)) nedge_nodes++;
533
534 const int i = ND_id(n);
535 sizes[i * Ndim] = ND_width(n) * .5 + pad.x;
536 sizes[i * Ndim + 1] = ND_height(n) * .5 + pad.y;
537 }
538
539 if (elabels && nedge_nodes) {
540 int* elabs = gv_calloc(nedge_nodes, sizeof(int));
541 nedge_nodes = 0;
542 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
543 if (IS_LNODE(n))
544 elabs[nedge_nodes++] = ND_id(n);
545 }
546 *elabels = elabs;
547 *n_elabels = nedge_nodes;
548 }
549
550 return sizes;
551}
552
553/* Assumes g is connected and simple, i.e., we can have a->b and b->a
554 * but not a->b and a->b
555 */
557 if (!g)
558 return NULL;
559 const int nnodes = agnnodes(g);
560 const int nedges = agnedges(g);
561
562 /* Assign node ids */
563 int i = 0;
564 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n))
565 ND_id(n) = i++;
566
567 int *I = gv_calloc(nedges, sizeof(int));
568 int *J = gv_calloc(nedges, sizeof(int));
569 double *val = gv_calloc(nedges, sizeof(double));
570
571 Agsym_t *sym = agfindedgeattr(g, "weight");
572
573 i = 0;
574 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
575 const int row = ND_id(n);
576 for (Agedge_t *e = agfstout(g, n); e; e = agnxtout(g, e)) {
577 I[i] = row;
578 J[i] = ND_id(aghead(e));
579 double v;
580 if (!sym || sscanf(agxget(e, sym), "%lf", &v) != 1)
581 v = 1;
582 val[i] = v;
583 /* edge length */
584 i++;
585 }
586 }
587
589 I, J, val,
591 sizeof(double));
592
593 free(I);
594 free(J);
595 free(val);
596
597 return A;
598}
599
600#if defined(HAVE_GTS) && defined(SFDP)
601static void fdpAdjust(graph_t *g, adjust_data *am) {
602 SparseMatrix A0 = makeMatrix(g);
603 SparseMatrix A = A0;
604 double *pos = gv_calloc(Ndim * agnnodes(g), sizeof(double));
605 expand_t sep = sepFactor(g);
606 pointf pad;
607
608 if (sep.doAdd) {
609 pad.x = PS2INCH(sep.x);
610 pad.y = PS2INCH(sep.y);
611 } else {
612 pad.x = PS2INCH(DFLT_MARGIN);
613 pad.y = PS2INCH(DFLT_MARGIN);
614 }
615 double *sizes = getSizes(g, pad, NULL, NULL);
616
617 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
618 double* npos = pos + Ndim * ND_id(n);
619 for (int i = 0; i < Ndim; i++) {
620 npos[i] = ND_pos(n)[i];
621 }
622 }
623
624 if (!SparseMatrix_is_symmetric(A, false) || A->type != MATRIX_TYPE_REAL) {
626 } else {
628 }
629
630 remove_overlap(Ndim, A, pos, sizes, am->value, am->scaling,
632 mapBool(agget(g, "overlap_shrink"), true));
633
634 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
635 double *npos = pos + Ndim * ND_id(n);
636 for (int i = 0; i < Ndim; i++) {
637 ND_pos(n)[i] = npos[i];
638 }
639 }
640
641 free(sizes);
642 free(pos);
643 if (A != A0)
646}
647#endif
648
649#ifdef IPSEPCOLA
650static int
651vpscAdjust(graph_t* G)
652{
653 enum { dim = 2 };
654 int nnodes = agnnodes(G);
655 ipsep_options opt;
656 pointf *nsize = gv_calloc(nnodes, sizeof(pointf));
657 float* coords[dim];
658 float *f_storage = gv_calloc(dim * nnodes, sizeof(float));
659
660 for (size_t i = 0; i < dim; i++) {
661 coords[i] = f_storage + i * nnodes;
662 }
663
664 size_t j = 0;
665 for (Agnode_t *v = agfstnode(G); v; v = agnxtnode(G, v)) {
666 for (size_t i = 0; i < dim; i++) {
667 coords[i][j] = (float)ND_pos(v)[i];
668 }
669 nsize[j].x = ND_width(v);
670 nsize[j].y = ND_height(v);
671 j++;
672 }
673
674 opt.diredges = 0;
675 opt.edge_gap = 0;
676 opt.noverlap = 2;
677 opt.clusters = (cluster_data){0};
678 expand_t exp_margin = sepFactor (G);
679 /* Multiply by 2 since opt.gap is the gap size, not the margin */
680 if (exp_margin.doAdd) {
681 opt.gap.x = 2.0*PS2INCH(exp_margin.x);
682 opt.gap.y = 2.0*PS2INCH(exp_margin.y);
683 }
684 else {
685 opt.gap.x = opt.gap.y = 2.0*PS2INCH(DFLT_MARGIN);
686 }
687 opt.nsize = nsize;
688
689 removeoverlaps(nnodes, coords, &opt);
690
691 j = 0;
692 for (Agnode_t *v = agfstnode(G); v; v = agnxtnode(G, v)) {
693 for (size_t i = 0; i < dim; i++) {
694 ND_pos(v)[i] = coords[i][j];
695 }
696 j++;
697 }
698
699 free (f_storage);
700 free (nsize);
701 return 0;
702}
703#endif
704
705/* Return true if "normalize" is defined and valid; return angle in phi.
706 * Read angle as degrees, convert to radians.
707 * Guarantee -PI < phi <= PI.
708 */
709static int
710angleSet (graph_t* g, double* phi)
711{
712 char* p;
713 char* a = agget(g, "normalize");
714
715 if (!a || *a == '\0')
716 return 0;
717 double ang = strtod (a, &p);
718 if (p == a) { /* no number */
719 if (mapbool(a))
720 ang = 0.0;
721 else
722 return 0;
723 }
724 while (ang > 180) ang -= 360;
725 while (ang <= -180) ang += 360;
726
727 *phi = RADIANS(ang);
728 return 1;
729}
730
731/* If normalize is set, move first node to origin, then
732 * rotate graph so that the angle of the first edge is given
733 * by the degrees from normalize.
734 * FIX: Generalize to allow rotation determined by graph shape.
735 */
737{
738 double phi;
739 int ret;
740
741 if (!angleSet(g, &phi))
742 return 0;
743
744 node_t *v = agfstnode(g);
745 pointf p = {.x = ND_pos(v)[0], .y = ND_pos(v)[1]};
746 for (v = agfstnode(g); v; v = agnxtnode(g, v)) {
747 ND_pos(v)[0] -= p.x;
748 ND_pos(v)[1] -= p.y;
749 }
750 if (p.x || p.y) ret = 1;
751 else ret = 0;
752
753 edge_t *e = NULL;
754 for (v = agfstnode(g); v; v = agnxtnode(g, v))
755 if ((e = agfstout(g, v)))
756 break;
757 if (e == NULL)
758 return ret;
759
760 /* rotation necessary; pos => ccw */
761 phi -= atan2(ND_pos(aghead(e))[1] - ND_pos(agtail(e))[1],
762 ND_pos(aghead(e))[0] - ND_pos(agtail(e))[0]);
763
764 if (phi) {
765 const pointf orig = {.x = ND_pos(agtail(e))[0],
766 .y = ND_pos(agtail(e))[1]};
767 const double cosv = cos(phi);
768 const double sinv = sin(phi);
769 for (v = agfstnode(g); v; v = agnxtnode(g, v)) {
770 p.x = ND_pos(v)[0] - orig.x;
771 p.y = ND_pos(v)[1] - orig.y;
772 ND_pos(v)[0] = p.x * cosv - p.y * sinv + orig.x;
773 ND_pos(v)[1] = p.x * sinv + p.y * cosv + orig.y;
774 }
775 return 1;
776 }
777 else return ret;
778}
779
780typedef struct {
782 char *attrib;
783 char *print;
784} lookup_t;
785
786/* Translation table from overlap values to algorithms.
787 * adjustMode[0] corresponds to overlap=true
788 * adjustMode[1] corresponds to overlap=false
789 */
790static const lookup_t adjustMode[] = {
791 {AM_NONE, "", "none"},
792#if defined(HAVE_GTS) && defined(SFDP)
793 {AM_PRISM, "prism", "prism"},
794#endif
795 {AM_VOR, "voronoi", "Voronoi"},
796 {AM_NSCALE, "scale", "scaling"},
797 {AM_COMPRESS, "compress", "compress"},
798 {AM_VPSC, "vpsc", "vpsc"},
799 {AM_IPSEP, "ipsep", "ipsep"},
800 {AM_SCALE, "oscale", "old scaling"},
801 {AM_SCALEXY, "scalexy", "x and y scaling"},
802 {AM_ORTHO, "ortho", "orthogonal constraints"},
803 {AM_ORTHO_YX, "ortho_yx", "orthogonal constraints"},
804 {AM_ORTHOXY, "orthoxy", "xy orthogonal constraints"},
805 {AM_ORTHOYX, "orthoyx", "yx orthogonal constraints"},
806 {AM_PORTHO, "portho", "pseudo-orthogonal constraints"},
807 {AM_PORTHO_YX, "portho_yx", "pseudo-orthogonal constraints"},
808 {AM_PORTHOXY, "porthoxy", "xy pseudo-orthogonal constraints"},
809 {AM_PORTHOYX, "porthoyx", "yx pseudo-orthogonal constraints"},
810#if !(defined(HAVE_GTS) && defined(SFDP))
811 {AM_PRISM, "prism", 0},
812#endif
813 {0}
814};
815
817static void setPrismValues(Agraph_t *g, const char *s, adjust_data *dp) {
818 int v;
819
820 if (sscanf (s, "%d", &v) > 0 && v >= 0)
821 dp->value = v;
822 else
823 dp->value = 1000;
824 dp->scaling = late_double(g, agfindgraphattr(g, "overlap_scaling"), -4.0, -1.e10);
825}
826
828static void getAdjustMode(Agraph_t *g, const char *s, adjust_data *dp) {
829 const lookup_t *ap = adjustMode + 1;
830 if (s == NULL || *s == '\0') {
831 dp->mode = adjustMode[0].mode;
832 dp->print = adjustMode[0].print;
833 }
834 else {
835 while (ap->attrib) {
836 bool matches = strcasecmp(s, ap->attrib) == 0;
837 // "prism" takes parameters, so needs to match "prism.*"
838 matches |= ap->mode == AM_PRISM
839 && strncasecmp(s, ap->attrib, strlen(ap->attrib)) == 0;
840 if (matches) {
841 if (ap->print == NULL) {
842 agwarningf("Overlap value \"%s\" unsupported - ignored\n", ap->attrib);
843 ap = &adjustMode[1];
844 }
845 dp->mode = ap->mode;
846 dp->print = ap->print;
847 if (ap->mode == AM_PRISM)
848 setPrismValues(g, s + strlen(ap->attrib), dp);
849 break;
850 }
851 ap++;
852 }
853 if (ap->attrib == NULL ) {
854 bool v = mapbool(s);
855 bool unmappable = v != mapBool(s, true);
856 if (unmappable) {
857 agwarningf("Unrecognized overlap value \"%s\" - using false\n", s);
858 v = false;
859 }
860 if (v) {
861 dp->mode = adjustMode[0].mode;
862 dp->print = adjustMode[0].print;
863 }
864 else {
865 dp->mode = adjustMode[1].mode;
866 dp->print = adjustMode[1].print;
867 }
868 if (dp->mode == AM_PRISM)
869 setPrismValues (g, "", dp);
870 }
871 }
872 if (Verbose) {
873 fprintf(stderr, "overlap: %s value %d scaling %.04f\n", dp->print, dp->value, dp->scaling);
874 }
875}
876
877void graphAdjustMode(graph_t *G, adjust_data *dp, char *dflt) {
878 char* am = agget(G, "overlap");
879 getAdjustMode (G, am ? am : (dflt ? dflt : ""), dp);
880}
881
882#define ISZERO(d) (fabs(d) < 0.000000001)
883
884static int simpleScale (graph_t* g)
885{
886 pointf sc;
887 int i;
888 char* p;
889
890 if ((p = agget(g, "scale"))) {
891 if ((i = sscanf(p, "%lf,%lf", &sc.x, &sc.y))) {
892 if (ISZERO(sc.x)) return 0;
893 if (i == 1) sc.y = sc.x;
894 else if (ISZERO(sc.y)) return 0;
895 if (sc.y == 1 && sc.x == 1) return 0;
896 if (Verbose)
897 fprintf (stderr, "scale = (%.03f,%.03f)\n", sc.x, sc.y);
898 for (node_t *n = agfstnode(g); n; n = agnxtnode(g,n)) {
899 ND_pos(n)[0] *= sc.x;
900 ND_pos(n)[1] *= sc.y;
901 }
902 return 1;
903 }
904 }
905 return 0;
906}
907
908/* Use adjust_data to determine if and how to remove
909 * node overlaps.
910 * Return non-zero if nodes are moved.
911 */
912int
914{
915 int ret;
916
917 if (agnnodes(G) < 2)
918 return 0;
919
920 int nret = normalize (G);
921 nret += simpleScale (G);
922
923 if (am->mode == AM_NONE)
924 return nret;
925
926 if (Verbose)
927 fprintf(stderr, "Adjusting %s using %s\n", agnameof(G), am->print);
928
929 if (am->mode > AM_SCALE) {
930 switch (am->mode) {
931 case AM_NSCALE:
932 ret = scAdjust(G, 1);
933 break;
934 case AM_SCALEXY:
935 ret = scAdjust(G, 0);
936 break;
937 case AM_PUSH:
938 ret = 0;
939 break;
940 case AM_PUSHPULL:
941 ret = 0;
942 break;
943 case AM_PORTHO_YX:
944 case AM_PORTHO:
945 case AM_PORTHOXY:
946 case AM_PORTHOYX:
947 case AM_ORTHO_YX:
948 case AM_ORTHO:
949 case AM_ORTHOXY:
950 case AM_ORTHOYX:
951 cAdjust(G, am->mode);
952 ret = 0;
953 break;
954 case AM_COMPRESS:
955 ret = scAdjust(G, -1);
956 break;
957#if defined(HAVE_GTS) && defined(SFDP)
958 case AM_PRISM:
959 fdpAdjust(G, am);
960 ret = 0;
961 break;
962#endif
963#ifdef IPSEPCOLA
964 case AM_IPSEP:
965 return nret; /* handled during layout */
966 break;
967 case AM_VPSC:
968 ret = vpscAdjust(G);
969 break;
970#endif
971 default: /* to silence warnings */
972 if (am->mode != AM_VOR && am->mode != AM_SCALE)
973 agwarningf("Unhandled adjust option %s\n", am->print);
974 ret = 0;
975 break;
976 }
977 return nret+ret;
978 }
979
980 /* create main array */
981 if (makeInfo(G)) {
982 freeNodes();
983 return nret;
984 }
985
986 /* establish and verify bounding box */
987 state_t st = {0};
988 chkBoundBox(&st, G);
989
990 if (am->mode == AM_SCALE)
991 ret = sAdjust(&st);
992 else
993 ret = vAdjust(&st);
994
995 if (ret)
996 updateGraph();
997
998 freeNodes();
999 free(st.sites);
1000
1001 return ret+nret;
1002}
1003
1005int
1007{
1008 adjust_data am;
1009
1010 if (agnnodes(G) < 2)
1011 return 0;
1012 getAdjustMode(G, flag, &am);
1013 return removeOverlapWith (G, &am);
1014}
1015
1016/* Remove node overlap relying on graph's overlap attribute.
1017 * Return non-zero if graph has changed.
1018 */
1020{
1021 return removeOverlapAs(G, agget(G, "overlap"));
1022}
1023
1024/* Convert "sep" attribute into expand_t.
1025 * Input "+x,y" becomes {x,y,true}
1026 * Input "x,y" becomes {1 + x/sepfact,1 + y/sepfact,false}
1027 * Return 1 on success, 0 on failure
1028 */
1029static int parseFactor(char *s, expand_t *pp, double sepfact, double dflt) {
1030 int i;
1031
1032 while (gv_isspace(*s)) s++;
1033 if (*s == '+') {
1034 s++;
1035 pp->doAdd = true;
1036 }
1037 else pp->doAdd = false;
1038
1039 double x, y;
1040 if ((i = sscanf(s, "%lf,%lf", &x, &y))) {
1041 if (i == 1) y = x;
1042 if (pp->doAdd) {
1043 if (sepfact > 1) {
1044 pp->x = fmin(dflt, x / sepfact);
1045 pp->y = fmin(dflt, y / sepfact);
1046 }
1047 else if (sepfact < 1) {
1048 pp->x = fmax(dflt, x / sepfact);
1049 pp->y = fmax(dflt, y / sepfact);
1050 }
1051 else {
1052 pp->x = x;
1053 pp->y = y;
1054 }
1055 }
1056 else {
1057 pp->x = 1.0 + x / sepfact;
1058 pp->y = 1.0 + y / sepfact;
1059 }
1060 return 1;
1061 }
1062 else return 0;
1063}
1064
1067{
1068 expand_t pmargin;
1069 char* marg;
1070
1071 if ((marg = agget(g, "sep")) && parseFactor(marg, &pmargin, 1.0, 0)) {
1072 }
1073 else if ((marg = agget(g, "esep")) && parseFactor(marg, &pmargin, SEPFACT, DFLT_MARGIN)) {
1074 }
1075 else { /* default */
1076 pmargin.x = pmargin.y = DFLT_MARGIN;
1077 pmargin.doAdd = true;
1078 }
1079 if (Verbose)
1080 fprintf (stderr, "Node separation: add=%d (%f,%f)\n",
1081 pmargin.doAdd, pmargin.x, pmargin.y);
1082 return pmargin;
1083}
1084
1085/* This value should be smaller than the sep value used to expand
1086 * nodes during adjustment. If not, when the adjustment pass produces
1087 * a fairly tight layout, the spline code will find that some nodes
1088 * still overlap.
1089 */
1092{
1093 expand_t pmargin;
1094 char* marg;
1095
1096 if ((marg = agget(g, "esep")) && parseFactor(marg, &pmargin, 1.0, 0)) {
1097 }
1098 else if ((marg = agget(g, "sep")) &&
1099 parseFactor(marg, &pmargin, 1.0 / SEPFACT, SEPFACT * DFLT_MARGIN)) {
1100 }
1101 else {
1102 pmargin.x = pmargin.y = SEPFACT*DFLT_MARGIN;
1103 pmargin.doAdd = true;
1104 }
1105 if (Verbose)
1106 fprintf (stderr, "Edge separation: add=%d (%f,%f)\n",
1107 pmargin.doAdd, pmargin.x, pmargin.y);
1108 return pmargin;
1109}
bool SparseMatrix_is_symmetric(SparseMatrix A, bool test_pattern_symmetry_only)
SparseMatrix SparseMatrix_from_coordinate_arrays(int nz, int m, int n, int *irn, int *jcn, void *val0, int type, size_t sz)
void SparseMatrix_delete(SparseMatrix A)
SparseMatrix SparseMatrix_get_real_adjacency_matrix_symmetrized(SparseMatrix A)
SparseMatrix SparseMatrix_remove_diagonal(SparseMatrix A)
@ MATRIX_TYPE_REAL
static unsigned countOverlap(unsigned iter)
Count number of node-node overlaps at iteration iter.
Definition adjust.c:274
static void newpos(Info_t *ip)
Definition adjust.c:330
static int sAdjust(state_t *st)
Definition adjust.c:486
static void sortSites(state_t *st)
Fill array of pointer to sites and sort the sites using scomp.
Definition adjust.c:180
static void newPos(const state_t *st, bool doAll)
Definition adjust.c:407
static void updateGraph(void)
Enter new node positions into the graph.
Definition adjust.c:512
expand_t esepFactor(graph_t *g)
Definition adjust.c:1091
static void centroidOf(Point a, Point b, Point c, double *x, double *y)
Definition adjust.c:321
static void chkBoundBox(state_t *st, Agraph_t *graph)
Definition adjust.c:86
static void freeNodes(void)
Free node resources.
Definition adjust.c:67
SparseMatrix makeMatrix(Agraph_t *g)
Definition adjust.c:556
static void setBoundBox(state_t *st, Point *ll, Point *ur)
Definition adjust.c:55
expand_t sepFactor(graph_t *g)
Definition adjust.c:1066
static void getAdjustMode(Agraph_t *g, const char *s, adjust_data *dp)
Convert string value to internal value of adjustment mode.
Definition adjust.c:828
static const lookup_t adjustMode[]
Definition adjust.c:790
#define IS_LNODE(n)
Definition adjust.c:523
int removeOverlapAs(graph_t *G, char *flag)
Use flag value to determine if and how to remove node overlaps.
Definition adjust.c:1006
#define ISZERO(d)
Definition adjust.c:882
double * getSizes(Agraph_t *g, pointf pad, int *n_elabels, int **elabels)
Set up array of half sizes in inches.
Definition adjust.c:526
static void cleanup(void)
Definition adjust.c:422
static int parseFactor(char *s, expand_t *pp, double sepfact, double dflt)
Definition adjust.c:1029
static int scomp(const void *S1, const void *S2)
Definition adjust.c:164
static double areaOf(Point a, Point b, Point c)
Area of triangle whose vertices are a,b,c.
Definition adjust.c:313
static Site * nextOne(void *state)
Definition adjust.c:219
static void rmEquality(state_t *st)
Check for nodes with identical positions and tweak the positions.
Definition adjust.c:228
static int makeInfo(Agraph_t *graph)
For each node in the graph, create a Info data structure.
Definition adjust.c:120
static void rePos(void)
Definition adjust.c:476
#define SEPFACT
Definition adjust.c:42
static int vAdjust(state_t *st)
Definition adjust.c:428
static void geomUpdate(state_t *st, int doSort)
Definition adjust.c:201
int adjustNodes(graph_t *G)
Definition adjust.c:1019
int normalize(graph_t *g)
Definition adjust.c:736
static void increaseBoundBox(state_t *st)
Definition adjust.c:297
static const double incr
Definition adjust.c:44
static void addCorners(const state_t *st)
Definition adjust.c:356
static int angleSet(graph_t *g, double *phi)
Definition adjust.c:710
static int simpleScale(graph_t *g)
Definition adjust.c:884
static void setPrismValues(Agraph_t *g, const char *s, adjust_data *dp)
Initialize and set prism values.
Definition adjust.c:817
void graphAdjustMode(graph_t *G, adjust_data *dp, char *dflt)
Definition adjust.c:877
int removeOverlapWith(graph_t *G, adjust_data *am)
Definition adjust.c:913
INTERNAL int cAdjust(graph_t *, int)
Definition constraint.c:552
INTERNAL int scAdjust(graph_t *, int)
Definition constraint.c:784
#define DFLT_MARGIN
Definition adjust.h:27
adjust_mode
Definition adjust.h:29
@ AM_PORTHOYX
Definition adjust.h:33
@ AM_NONE
Definition adjust.h:30
@ AM_PUSH
Definition adjust.h:31
@ AM_VPSC
Definition adjust.h:34
@ AM_PORTHO
Definition adjust.h:33
@ AM_ORTHO_YX
Definition adjust.h:32
@ AM_ORTHO
Definition adjust.h:32
@ AM_PORTHO_YX
Definition adjust.h:33
@ AM_NSCALE
Definition adjust.h:31
@ AM_SCALE
Definition adjust.h:31
@ AM_SCALEXY
Definition adjust.h:31
@ AM_IPSEP
Definition adjust.h:34
@ AM_ORTHOYX
Definition adjust.h:32
@ AM_PRISM
Definition adjust.h:34
@ AM_VOR
Definition adjust.h:30
@ AM_ORTHOXY
Definition adjust.h:32
@ AM_PORTHOXY
Definition adjust.h:33
@ AM_COMPRESS
Definition adjust.h:33
@ AM_PUSHPULL
Definition adjust.h:31
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
#define RADIANS(deg)
Definition arith.h:49
static bool doAll
induce subgraphs
Definition ccomps.c:67
bool mapbool(const char *p)
Definition utils.c:339
double late_double(void *obj, attrsym_t *attr, double defaultValue, double minimum)
Definition utils.c:51
bool mapBool(const char *p, bool defaultValue)
Definition utils.c:323
static int overlaps(nitem *p, int cnt)
Definition constraint.c:476
helpers for verbose/debug printing
#define GV_DEBUG(...)
Definition debug.h:39
double pymin
Definition edges.c:18
double pymax
Definition edges.c:18
double pxmin
Definition edges.c:18
void edgeinit(void)
Definition edges.c:22
double pxmax
Definition edges.c:18
#define A(n, t)
Definition expr.h:76
#define I
Definition expr.h:71
#define G
Definition gdefs.h:7
#define PS2INCH(a_points)
Definition geom.h:64
size_t nsites
Definition geometry.c:18
double deltax
Definition geometry.c:16
double xmax
Definition geometry.c:15
void geominit(void)
Definition geometry.c:21
double ymin
Definition geometry.c:15
double xmin
Definition geometry.c:15
double dist_2(Point pp, Point qp)
distance squared between two points
Definition geometry.c:29
double ymax
Definition geometry.c:15
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
char * agget(void *obj, char *name)
Definition attr.c:448
char * agxget(void *obj, Agsym_t *sym)
Definition attr.c:458
#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:988
#define aghead(e)
Definition cgraph.h:989
Agedge_t * agnxtout(Agraph_t *g, Agedge_t *e)
Definition edge.c:41
void agwarningf(const char *fmt,...)
Definition agerror.c:173
#define agfindgraphattr(g, a)
Definition types.h:613
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_height(n)
Definition types.h:498
#define ND_width(n)
Definition types.h:536
#define ND_pos(n)
Definition types.h:520
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:143
Agraph_t * graph(char *name)
Definition gv.cpp:30
replacements for ctype.h functions
static bool gv_isspace(int c)
Definition gv_ctype.h:55
rows row
Definition htmlparse.y:326
Info_t * nodeInfo
array of node info
Definition info.c:17
void addVertex(Site *s, double x, double y)
insert vertex into sorted list
Definition info.c:101
#define ND_id(n)
Definition mm2gv.c:40
static const int dim
NEATOPROCS_API void s1(graph_t *, node_t *)
Definition stuff.c:663
void remove_overlap(int dim, SparseMatrix A, double *x, double *label_sizes, int ntry, double initial_scaling, int edge_labeling_scheme, int n_constr_nodes, int *constr_nodes, SparseMatrix A_constr, bool do_shrinking)
Definition overlap.c:586
@ ELSCHEME_NONE
Definition overlap.h:35
int polyOverlap(Point p, Poly *pp, Point q, Poly *qp)
Definition poly.c:438
void breakPoly(Poly *pp)
Definition poly.c:44
void polyFree(void)
Definition poly.c:33
int makePoly(Poly *pp, Agnode_t *n, double xmargin, double ymargin)
Definition poly.c:218
int makeAddPoly(Poly *pp, Agnode_t *n, double xmargin, double ymargin)
Definition poly.c:128
static int nedges
total no. of edges used in routing
Definition routespl.c:32
int fdpAdjust(graph_t *g)
void siteinit(void)
Definition site.c:22
platform abstraction for case-insensitive string functions
graph or subgraph
Definition cgraph.h:424
string attribute descriptor symbol in Agattr_s.dict
Definition cgraph.h:651
info concerning site
Definition info.h:30
size_t n_verts
number of elements in verts
Definition info.h:36
Point * verts
sorted list of vertices of voronoi polygon
Definition info.h:35
bool overlaps
true if node overlaps other nodes
Definition info.h:33
Poly poly
polygon at node
Definition info.h:34
Agnode_t * node
libgraph node
Definition info.h:31
Site site
site used by voronoi code
Definition info.h:32
double x
Definition geometry.h:29
double y
Definition geometry.h:29
Definition poly.h:25
Point corner
Definition poly.h:27
Point origin
Definition poly.h:26
Definition site.h:28
Point coord
Definition site.h:29
size_t sitenbr
Definition site.h:30
unsigned refcnt
Definition site.h:31
int value
Definition adjust.h:40
double scaling
Definition adjust.h:41
char * print
Definition adjust.h:39
adjust_mode mode
Definition adjust.h:38
double x
Definition adjust.h:45
double y
Definition adjust.h:45
bool doAdd
Definition adjust.h:46
char * attrib
Definition adjust.c:782
char * print
Definition adjust.c:783
adjust_mode mode
Definition adjust.c:781
double x
Definition geom.h:29
double y
Definition geom.h:29
information the ID allocator needs to do its job
Definition id.c:27
Site ** sites
array of pointers to sites; used in qsort
Definition adjust.c:49
Point sw
Definition adjust.c:51
Site ** nextSite
Definition adjust.c:52
Point nw
Definition adjust.c:51
Site ** endSite
sentinel on sites array
Definition adjust.c:50
Point ne
Definition adjust.c:51
Point se
corners of clipping window
Definition adjust.c:51
Definition grammar.c:90
void voronoi(Site *(*nextsite)(void *context), void *context)
Definition voronoi.c:19