Graphviz 13.0.0~dev.20250121.0651
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/hedges.h>
26#include <neatogen/digcola.h>
27#if ((defined(HAVE_GTS) || defined(HAVE_TRIANGLE)) && defined(SFDP))
28#include <neatogen/overlap.h>
29#endif
30#include <stdbool.h>
31#ifdef IPSEPCOLA
32#include <vpsc/csolve_VPSC.h>
34#endif
35#include <stddef.h>
36#include <util/agxbuf.h>
37#include <util/alloc.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 ELcleanup();
425 siteinit(); /* free memory */
426 edgeinit(); /* free memory */
427}
428
429static int vAdjust(state_t *st) {
430 unsigned iterCnt = 0;
431 unsigned badLevel = 0;
432 unsigned increaseCnt = 0;
433
434 unsigned overlapCnt = countOverlap(iterCnt);
435
436 if (overlapCnt == 0)
437 return 0;
438
439 rmEquality(st);
440 geomUpdate(st, 0);
441 voronoi(nextOne, st);
442 for (bool doAll = false;;) {
443 newPos(st, doAll);
444 iterCnt++;
445
446 const unsigned cnt = countOverlap(iterCnt);
447 if (cnt == 0)
448 break;
449 if (cnt >= overlapCnt)
450 badLevel++;
451 else
452 badLevel = 0;
453 overlapCnt = cnt;
454
455 switch (badLevel) {
456 case 0:
457 doAll = true;
458 break;
459 default:
460 doAll = true;
461 increaseCnt++;
463 break;
464 }
465
466 geomUpdate(st, 1);
467 voronoi(nextOne, st);
468 }
469
470 if (Verbose) {
471 fprintf(stderr, "Number of iterations = %u\n", iterCnt);
472 fprintf(stderr, "Number of increases = %u\n", increaseCnt);
473 }
474
475 cleanup();
476 return 1;
477}
478
479static void rePos(void) {
480 double f = 1.0 + incr;
481
482 for (size_t i = 0; i < nsites; i++) {
483 Info_t *ip = &nodeInfo[i];
484 ip->site.coord.x *= f;
485 ip->site.coord.y *= f;
486 }
487}
488
489static int sAdjust(state_t *st) {
490 unsigned iterCnt = 0;
491
492 const unsigned overlapCnt = countOverlap(iterCnt);
493
494 if (overlapCnt == 0)
495 return 0;
496
497 rmEquality(st);
498 while (1) {
499 rePos();
500 iterCnt++;
501
502 const unsigned cnt = countOverlap(iterCnt);
503 if (cnt == 0)
504 break;
505 }
506
507 if (Verbose) {
508 fprintf(stderr, "Number of iterations = %u\n", iterCnt);
509 }
510
511 return 1;
512}
513
515static void updateGraph(void)
516{
517 for (size_t i = 0; i < nsites; i++) {
518 Info_t *ip = &nodeInfo[i];
519 ND_pos(ip->node)[0] = ip->site.coord.x;
520 ND_pos(ip->node)[1] = ip->site.coord.y;
521 }
522}
523
524#define ELS "|edgelabel|"
525 /* Return true if node name starts with ELS */
526#define IS_LNODE(n) startswith(agnameof(n), ELS)
527
529double *getSizes(Agraph_t * g, pointf pad, int* n_elabels, int** elabels)
530{
531 double *sizes = gv_calloc(Ndim * agnnodes(g), sizeof(double));
532 int nedge_nodes = 0;
533
534 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
535 if (elabels && IS_LNODE(n)) nedge_nodes++;
536
537 const int i = ND_id(n);
538 sizes[i * Ndim] = ND_width(n) * .5 + pad.x;
539 sizes[i * Ndim + 1] = ND_height(n) * .5 + pad.y;
540 }
541
542 if (elabels && nedge_nodes) {
543 int* elabs = gv_calloc(nedge_nodes, sizeof(int));
544 nedge_nodes = 0;
545 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
546 if (IS_LNODE(n))
547 elabs[nedge_nodes++] = ND_id(n);
548 }
549 *elabels = elabs;
550 *n_elabels = nedge_nodes;
551 }
552
553 return sizes;
554}
555
556/* Assumes g is connected and simple, i.e., we can have a->b and b->a
557 * but not a->b and a->b
558 */
560 if (!g)
561 return NULL;
562 const int nnodes = agnnodes(g);
563 const int nedges = agnedges(g);
564
565 /* Assign node ids */
566 int i = 0;
567 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n))
568 ND_id(n) = i++;
569
570 int *I = gv_calloc(nedges, sizeof(int));
571 int *J = gv_calloc(nedges, sizeof(int));
572 double *val = gv_calloc(nedges, sizeof(double));
573
574 Agsym_t *sym = agfindedgeattr(g, "weight");
575
576 i = 0;
577 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
578 const int row = ND_id(n);
579 for (Agedge_t *e = agfstout(g, n); e; e = agnxtout(g, e)) {
580 I[i] = row;
581 J[i] = ND_id(aghead(e));
582 double v;
583 if (!sym || sscanf(agxget(e, sym), "%lf", &v) != 1)
584 v = 1;
585 val[i] = v;
586 /* edge length */
587 i++;
588 }
589 }
590
592 I, J, val,
594 sizeof(double));
595
596 free(I);
597 free(J);
598 free(val);
599
600 return A;
601}
602
603#if ((defined(HAVE_GTS) || defined(HAVE_TRIANGLE)) && defined(SFDP))
604static void fdpAdjust(graph_t *g, adjust_data *am) {
605 SparseMatrix A0 = makeMatrix(g);
606 SparseMatrix A = A0;
607 double *pos = gv_calloc(Ndim * agnnodes(g), sizeof(double));
608 expand_t sep = sepFactor(g);
609 pointf pad;
610
611 if (sep.doAdd) {
612 pad.x = PS2INCH(sep.x);
613 pad.y = PS2INCH(sep.y);
614 } else {
615 pad.x = PS2INCH(DFLT_MARGIN);
616 pad.y = PS2INCH(DFLT_MARGIN);
617 }
618 double *sizes = getSizes(g, pad, NULL, NULL);
619
620 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
621 double* npos = pos + Ndim * ND_id(n);
622 for (int i = 0; i < Ndim; i++) {
623 npos[i] = ND_pos(n)[i];
624 }
625 }
626
627 if (!SparseMatrix_is_symmetric(A, false) || A->type != MATRIX_TYPE_REAL) {
629 } else {
631 }
632
633 remove_overlap(Ndim, A, pos, sizes, am->value, am->scaling,
635 mapBool(agget(g, "overlap_shrink"), true));
636
637 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
638 double *npos = pos + Ndim * ND_id(n);
639 for (int i = 0; i < Ndim; i++) {
640 ND_pos(n)[i] = npos[i];
641 }
642 }
643
644 free(sizes);
645 free(pos);
646 if (A != A0)
649}
650#endif
651
652#ifdef IPSEPCOLA
653static int
654vpscAdjust(graph_t* G)
655{
656 enum { dim = 2 };
657 int nnodes = agnnodes(G);
658 ipsep_options opt;
659 pointf *nsize = gv_calloc(nnodes, sizeof(pointf));
660 float* coords[dim];
661 float *f_storage = gv_calloc(dim * nnodes, sizeof(float));
662
663 for (size_t i = 0; i < dim; i++) {
664 coords[i] = f_storage + i * nnodes;
665 }
666
667 size_t j = 0;
668 for (Agnode_t *v = agfstnode(G); v; v = agnxtnode(G, v)) {
669 for (size_t i = 0; i < dim; i++) {
670 coords[i][j] = (float)ND_pos(v)[i];
671 }
672 nsize[j].x = ND_width(v);
673 nsize[j].y = ND_height(v);
674 j++;
675 }
676
677 opt.diredges = 0;
678 opt.edge_gap = 0;
679 opt.noverlap = 2;
680 opt.clusters = (cluster_data){0};
681 expand_t exp_margin = sepFactor (G);
682 /* Multiply by 2 since opt.gap is the gap size, not the margin */
683 if (exp_margin.doAdd) {
684 opt.gap.x = 2.0*PS2INCH(exp_margin.x);
685 opt.gap.y = 2.0*PS2INCH(exp_margin.y);
686 }
687 else {
688 opt.gap.x = opt.gap.y = 2.0*PS2INCH(DFLT_MARGIN);
689 }
690 opt.nsize = nsize;
691
692 removeoverlaps(nnodes, coords, &opt);
693
694 j = 0;
695 for (Agnode_t *v = agfstnode(G); v; v = agnxtnode(G, v)) {
696 for (size_t i = 0; i < dim; i++) {
697 ND_pos(v)[i] = coords[i][j];
698 }
699 j++;
700 }
701
702 free (f_storage);
703 free (nsize);
704 return 0;
705}
706#endif
707
708/* Return true if "normalize" is defined and valid; return angle in phi.
709 * Read angle as degrees, convert to radians.
710 * Guarantee -PI < phi <= PI.
711 */
712static int
713angleSet (graph_t* g, double* phi)
714{
715 char* p;
716 char* a = agget(g, "normalize");
717
718 if (!a || *a == '\0')
719 return 0;
720 double ang = strtod (a, &p);
721 if (p == a) { /* no number */
722 if (mapbool(a))
723 ang = 0.0;
724 else
725 return 0;
726 }
727 while (ang > 180) ang -= 360;
728 while (ang <= -180) ang += 360;
729
730 *phi = RADIANS(ang);
731 return 1;
732}
733
734/* If normalize is set, move first node to origin, then
735 * rotate graph so that the angle of the first edge is given
736 * by the degrees from normalize.
737 * FIX: Generalize to allow rotation determined by graph shape.
738 */
740{
741 double phi;
742 int ret;
743
744 if (!angleSet(g, &phi))
745 return 0;
746
747 node_t *v = agfstnode(g);
748 pointf p = {.x = ND_pos(v)[0], .y = ND_pos(v)[1]};
749 for (v = agfstnode(g); v; v = agnxtnode(g, v)) {
750 ND_pos(v)[0] -= p.x;
751 ND_pos(v)[1] -= p.y;
752 }
753 if (p.x || p.y) ret = 1;
754 else ret = 0;
755
756 edge_t *e = NULL;
757 for (v = agfstnode(g); v; v = agnxtnode(g, v))
758 if ((e = agfstout(g, v)))
759 break;
760 if (e == NULL)
761 return ret;
762
763 /* rotation necessary; pos => ccw */
764 phi -= atan2(ND_pos(aghead(e))[1] - ND_pos(agtail(e))[1],
765 ND_pos(aghead(e))[0] - ND_pos(agtail(e))[0]);
766
767 if (phi) {
768 const pointf orig = {.x = ND_pos(agtail(e))[0],
769 .y = ND_pos(agtail(e))[1]};
770 const double cosv = cos(phi);
771 const double sinv = sin(phi);
772 for (v = agfstnode(g); v; v = agnxtnode(g, v)) {
773 p.x = ND_pos(v)[0] - orig.x;
774 p.y = ND_pos(v)[1] - orig.y;
775 ND_pos(v)[0] = p.x * cosv - p.y * sinv + orig.x;
776 ND_pos(v)[1] = p.x * sinv + p.y * cosv + orig.y;
777 }
778 return 1;
779 }
780 else return ret;
781}
782
783typedef struct {
785 char *attrib;
786 char *print;
787} lookup_t;
788
789/* Translation table from overlap values to algorithms.
790 * adjustMode[0] corresponds to overlap=true
791 * adjustMode[1] corresponds to overlap=false
792 */
793static const lookup_t adjustMode[] = {
794 {AM_NONE, "", "none"},
795#if ((defined(HAVE_GTS) || defined(HAVE_TRIANGLE)) && defined(SFDP))
796 {AM_PRISM, "prism", "prism"},
797#endif
798 {AM_VOR, "voronoi", "Voronoi"},
799 {AM_NSCALE, "scale", "scaling"},
800 {AM_COMPRESS, "compress", "compress"},
801 {AM_VPSC, "vpsc", "vpsc"},
802 {AM_IPSEP, "ipsep", "ipsep"},
803 {AM_SCALE, "oscale", "old scaling"},
804 {AM_SCALEXY, "scalexy", "x and y scaling"},
805 {AM_ORTHO, "ortho", "orthogonal constraints"},
806 {AM_ORTHO_YX, "ortho_yx", "orthogonal constraints"},
807 {AM_ORTHOXY, "orthoxy", "xy orthogonal constraints"},
808 {AM_ORTHOYX, "orthoyx", "yx orthogonal constraints"},
809 {AM_PORTHO, "portho", "pseudo-orthogonal constraints"},
810 {AM_PORTHO_YX, "portho_yx", "pseudo-orthogonal constraints"},
811 {AM_PORTHOXY, "porthoxy", "xy pseudo-orthogonal constraints"},
812 {AM_PORTHOYX, "porthoyx", "yx pseudo-orthogonal constraints"},
813#if !((defined(HAVE_GTS) || defined(HAVE_TRIANGLE)) && defined(SFDP))
814 {AM_PRISM, "prism", 0},
815#endif
816 {0}
817};
818
820static void setPrismValues(Agraph_t *g, const char *s, adjust_data *dp) {
821 int v;
822
823 if (sscanf (s, "%d", &v) > 0 && v >= 0)
824 dp->value = v;
825 else
826 dp->value = 1000;
827 dp->scaling = late_double(g, agfindgraphattr(g, "overlap_scaling"), -4.0, -1.e10);
828}
829
831static void getAdjustMode(Agraph_t *g, const char *s, adjust_data *dp) {
832 const lookup_t *ap = adjustMode + 1;
833 if (s == NULL || *s == '\0') {
834 dp->mode = adjustMode[0].mode;
835 dp->print = adjustMode[0].print;
836 }
837 else {
838 while (ap->attrib) {
839 bool matches = strcasecmp(s, ap->attrib) == 0;
840 // "prism" takes parameters, so needs to match "prism.*"
841 matches |= ap->mode == AM_PRISM
842 && strncasecmp(s, ap->attrib, strlen(ap->attrib)) == 0;
843 if (matches) {
844 if (ap->print == NULL) {
845 agwarningf("Overlap value \"%s\" unsupported - ignored\n", ap->attrib);
846 ap = &adjustMode[1];
847 }
848 dp->mode = ap->mode;
849 dp->print = ap->print;
850 if (ap->mode == AM_PRISM)
851 setPrismValues(g, s + strlen(ap->attrib), dp);
852 break;
853 }
854 ap++;
855 }
856 if (ap->attrib == NULL ) {
857 bool v = mapbool(s);
858 bool unmappable = v != mapBool(s, true);
859 if (unmappable) {
860 agwarningf("Unrecognized overlap value \"%s\" - using false\n", s);
861 v = false;
862 }
863 if (v) {
864 dp->mode = adjustMode[0].mode;
865 dp->print = adjustMode[0].print;
866 }
867 else {
868 dp->mode = adjustMode[1].mode;
869 dp->print = adjustMode[1].print;
870 }
871 if (dp->mode == AM_PRISM)
872 setPrismValues (g, "", dp);
873 }
874 }
875 if (Verbose) {
876 fprintf(stderr, "overlap: %s value %d scaling %.04f\n", dp->print, dp->value, dp->scaling);
877 }
878}
879
880void graphAdjustMode(graph_t *G, adjust_data *dp, char *dflt) {
881 char* am = agget(G, "overlap");
882 getAdjustMode (G, am ? am : (dflt ? dflt : ""), dp);
883}
884
885#define ISZERO(d) (fabs(d) < 0.000000001)
886
887static int simpleScale (graph_t* g)
888{
889 pointf sc;
890 int i;
891 char* p;
892
893 if ((p = agget(g, "scale"))) {
894 if ((i = sscanf(p, "%lf,%lf", &sc.x, &sc.y))) {
895 if (ISZERO(sc.x)) return 0;
896 if (i == 1) sc.y = sc.x;
897 else if (ISZERO(sc.y)) return 0;
898 if (sc.y == 1 && sc.x == 1) return 0;
899 if (Verbose)
900 fprintf (stderr, "scale = (%.03f,%.03f)\n", sc.x, sc.y);
901 for (node_t *n = agfstnode(g); n; n = agnxtnode(g,n)) {
902 ND_pos(n)[0] *= sc.x;
903 ND_pos(n)[1] *= sc.y;
904 }
905 return 1;
906 }
907 }
908 return 0;
909}
910
911/* Use adjust_data to determine if and how to remove
912 * node overlaps.
913 * Return non-zero if nodes are moved.
914 */
915int
917{
918 int ret;
919
920 if (agnnodes(G) < 2)
921 return 0;
922
923 int nret = normalize (G);
924 nret += simpleScale (G);
925
926 if (am->mode == AM_NONE)
927 return nret;
928
929 if (Verbose)
930 fprintf(stderr, "Adjusting %s using %s\n", agnameof(G), am->print);
931
932 if (am->mode > AM_SCALE) {
933 switch (am->mode) {
934 case AM_NSCALE:
935 ret = scAdjust(G, 1);
936 break;
937 case AM_SCALEXY:
938 ret = scAdjust(G, 0);
939 break;
940 case AM_PUSH:
941 ret = 0;
942 break;
943 case AM_PUSHPULL:
944 ret = 0;
945 break;
946 case AM_PORTHO_YX:
947 case AM_PORTHO:
948 case AM_PORTHOXY:
949 case AM_PORTHOYX:
950 case AM_ORTHO_YX:
951 case AM_ORTHO:
952 case AM_ORTHOXY:
953 case AM_ORTHOYX:
954 cAdjust(G, am->mode);
955 ret = 0;
956 break;
957 case AM_COMPRESS:
958 ret = scAdjust(G, -1);
959 break;
960#if ((defined(HAVE_GTS) || defined(HAVE_TRIANGLE)) && defined(SFDP))
961 case AM_PRISM:
962 fdpAdjust(G, am);
963 ret = 0;
964 break;
965#endif
966#ifdef IPSEPCOLA
967 case AM_IPSEP:
968 return nret; /* handled during layout */
969 break;
970 case AM_VPSC:
971 ret = vpscAdjust(G);
972 break;
973#endif
974 default: /* to silence warnings */
975 if (am->mode != AM_VOR && am->mode != AM_SCALE)
976 agwarningf("Unhandled adjust option %s\n", am->print);
977 ret = 0;
978 break;
979 }
980 return nret+ret;
981 }
982
983 /* create main array */
984 if (makeInfo(G)) {
985 freeNodes();
986 return nret;
987 }
988
989 /* establish and verify bounding box */
990 state_t st = {0};
991 chkBoundBox(&st, G);
992
993 if (am->mode == AM_SCALE)
994 ret = sAdjust(&st);
995 else
996 ret = vAdjust(&st);
997
998 if (ret)
999 updateGraph();
1000
1001 freeNodes();
1002 free(st.sites);
1003
1004 return ret+nret;
1005}
1006
1008int
1010{
1011 adjust_data am;
1012
1013 if (agnnodes(G) < 2)
1014 return 0;
1015 getAdjustMode(G, flag, &am);
1016 return removeOverlapWith (G, &am);
1017}
1018
1019/* Remove node overlap relying on graph's overlap attribute.
1020 * Return non-zero if graph has changed.
1021 */
1023{
1024 return removeOverlapAs(G, agget(G, "overlap"));
1025}
1026
1027/* Convert "sep" attribute into expand_t.
1028 * Input "+x,y" becomes {x,y,true}
1029 * Input "x,y" becomes {1 + x/sepfact,1 + y/sepfact,false}
1030 * Return 1 on success, 0 on failure
1031 */
1032static int parseFactor(char *s, expand_t *pp, double sepfact, double dflt) {
1033 int i;
1034
1035 while (gv_isspace(*s)) s++;
1036 if (*s == '+') {
1037 s++;
1038 pp->doAdd = true;
1039 }
1040 else pp->doAdd = false;
1041
1042 double x, y;
1043 if ((i = sscanf(s, "%lf,%lf", &x, &y))) {
1044 if (i == 1) y = x;
1045 if (pp->doAdd) {
1046 if (sepfact > 1) {
1047 pp->x = fmin(dflt, x / sepfact);
1048 pp->y = fmin(dflt, y / sepfact);
1049 }
1050 else if (sepfact < 1) {
1051 pp->x = fmax(dflt, x / sepfact);
1052 pp->y = fmax(dflt, y / sepfact);
1053 }
1054 else {
1055 pp->x = x;
1056 pp->y = y;
1057 }
1058 }
1059 else {
1060 pp->x = 1.0 + x / sepfact;
1061 pp->y = 1.0 + y / sepfact;
1062 }
1063 return 1;
1064 }
1065 else return 0;
1066}
1067
1070{
1071 expand_t pmargin;
1072 char* marg;
1073
1074 if ((marg = agget(g, "sep")) && parseFactor(marg, &pmargin, 1.0, 0)) {
1075 }
1076 else if ((marg = agget(g, "esep")) && parseFactor(marg, &pmargin, SEPFACT, DFLT_MARGIN)) {
1077 }
1078 else { /* default */
1079 pmargin.x = pmargin.y = DFLT_MARGIN;
1080 pmargin.doAdd = true;
1081 }
1082 if (Verbose)
1083 fprintf (stderr, "Node separation: add=%d (%f,%f)\n",
1084 pmargin.doAdd, pmargin.x, pmargin.y);
1085 return pmargin;
1086}
1087
1088/* This value should be smaller than the sep value used to expand
1089 * nodes during adjustment. If not, when the adjustment pass produces
1090 * a fairly tight layout, the spline code will find that some nodes
1091 * still overlap.
1092 */
1095{
1096 expand_t pmargin;
1097 char* marg;
1098
1099 if ((marg = agget(g, "esep")) && parseFactor(marg, &pmargin, 1.0, 0)) {
1100 }
1101 else if ((marg = agget(g, "sep")) &&
1102 parseFactor(marg, &pmargin, 1.0 / SEPFACT, SEPFACT * DFLT_MARGIN)) {
1103 }
1104 else {
1105 pmargin.x = pmargin.y = SEPFACT*DFLT_MARGIN;
1106 pmargin.doAdd = true;
1107 }
1108 if (Verbose)
1109 fprintf (stderr, "Edge separation: add=%d (%f,%f)\n",
1110 pmargin.doAdd, pmargin.x, pmargin.y);
1111 return pmargin;
1112}
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:489
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:515
expand_t esepFactor(graph_t *g)
Definition adjust.c:1094
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:559
static void setBoundBox(state_t *st, Point *ll, Point *ur)
Definition adjust.c:55
expand_t sepFactor(graph_t *g)
Definition adjust.c:1069
static void getAdjustMode(Agraph_t *g, const char *s, adjust_data *dp)
Convert string value to internal value of adjustment mode.
Definition adjust.c:831
static const lookup_t adjustMode[]
Definition adjust.c:793
#define IS_LNODE(n)
Definition adjust.c:526
int removeOverlapAs(graph_t *G, char *flag)
Use flag value to determine if and how to remove node overlaps.
Definition adjust.c:1009
#define ISZERO(d)
Definition adjust.c:885
double * getSizes(Agraph_t *g, pointf pad, int *n_elabels, int **elabels)
Set up array of half sizes in inches.
Definition adjust.c:529
static void cleanup(void)
Definition adjust.c:422
static int parseFactor(char *s, expand_t *pp, double sepfact, double dflt)
Definition adjust.c:1032
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:479
#define SEPFACT
Definition adjust.c:42
static int vAdjust(state_t *st)
Definition adjust.c:429
static void geomUpdate(state_t *st, int doSort)
Definition adjust.c:201
int adjustNodes(graph_t *G)
Definition adjust.c:1022
int normalize(graph_t *g)
Definition adjust.c:739
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:713
static int simpleScale(graph_t *g)
Definition adjust.c:887
static void setPrismValues(Agraph_t *g, const char *s, adjust_data *dp)
Initialize and set prism values.
Definition adjust.c:820
void graphAdjustMode(graph_t *G, adjust_data *dp, char *dflt)
Definition adjust.c:880
int removeOverlapWith(graph_t *G, adjust_data *am)
Definition adjust.c:916
int cAdjust(graph_t *, int)
Definition constraint.c:555
int scAdjust(graph_t *, int)
Definition constraint.c:787
#define DFLT_MARGIN
Definition adjust.h:21
adjust_mode
Definition adjust.h:23
@ AM_PORTHOYX
Definition adjust.h:27
@ AM_NONE
Definition adjust.h:24
@ AM_PUSH
Definition adjust.h:25
@ AM_VPSC
Definition adjust.h:28
@ AM_PORTHO
Definition adjust.h:27
@ AM_ORTHO_YX
Definition adjust.h:26
@ AM_ORTHO
Definition adjust.h:26
@ AM_PORTHO_YX
Definition adjust.h:27
@ AM_NSCALE
Definition adjust.h:25
@ AM_SCALE
Definition adjust.h:25
@ AM_SCALEXY
Definition adjust.h:25
@ AM_IPSEP
Definition adjust.h:28
@ AM_ORTHOYX
Definition adjust.h:26
@ AM_PRISM
Definition adjust.h:28
@ AM_VOR
Definition adjust.h:24
@ AM_ORTHOXY
Definition adjust.h:26
@ AM_PORTHOXY
Definition adjust.h:27
@ AM_COMPRESS
Definition adjust.h:27
@ AM_PUSHPULL
Definition adjust.h:25
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:69
bool mapbool(const char *p)
Definition utils.c:337
double late_double(void *obj, attrsym_t *attr, double defaultValue, double minimum)
Definition utils.c:50
bool mapBool(const char *p, bool defaultValue)
Definition utils.c:321
static int overlaps(nitem *p, int cnt)
Definition constraint.c:479
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:61
static bool Verbose
Definition gml2gv.c:23
void free(void *)
node NULL
Definition grammar.y:163
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:206
int agnedges(Agraph_t *g)
Definition graph.c:171
int agnnodes(Agraph_t *g)
Definition graph.c:165
char * agget(void *obj, char *name)
Definition attr.c:465
char * agxget(void *obj, Agsym_t *sym)
Definition attr.c:481
#define agfindedgeattr(g, a)
Definition types.h:617
Agedge_t * agfstout(Agraph_t *g, Agnode_t *n)
Definition edge.c:24
#define agtail(e)
Definition cgraph.h:888
#define aghead(e)
Definition cgraph.h:889
Agedge_t * agnxtout(Agraph_t *g, Agedge_t *e)
Definition edge.c:39
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:47
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:40
#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
void ELcleanup(void)
Definition hedges.c:25
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:39
static const int dim
NEATOPROCS_API void s1(graph_t *, node_t *)
Definition stuff.c:671
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:585
@ ELSCHEME_NONE
Definition overlap.h:29
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:31
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:641
info concerning site
Definition info.h:24
size_t n_verts
number of elements in verts
Definition info.h:30
Point * verts
sorted list of vertices of voronoi polygon
Definition info.h:29
bool overlaps
true if node overlaps other nodes
Definition info.h:27
Poly poly
polygon at node
Definition info.h:28
Agnode_t * node
libgraph node
Definition info.h:25
Site site
site used by voronoi code
Definition info.h:26
double x
Definition geometry.h:23
double y
Definition geometry.h:23
Definition poly.h:19
Point corner
Definition poly.h:21
Point origin
Definition poly.h:20
Definition site.h:22
Point coord
Definition site.h:23
size_t sitenbr
Definition site.h:24
unsigned refcnt
Definition site.h:25
int value
Definition adjust.h:34
double scaling
Definition adjust.h:35
char * print
Definition adjust.h:33
adjust_mode mode
Definition adjust.h:32
double x
Definition adjust.h:39
double y
Definition adjust.h:39
bool doAdd
Definition adjust.h:40
char * attrib
Definition adjust.c:785
char * print
Definition adjust.c:786
adjust_mode mode
Definition adjust.c:784
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:93
void voronoi(Site *(*nextsite)(void *context), void *context)
Definition voronoi.c:18