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