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