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