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