Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
circpos.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
12#include "config.h"
13/* TODO:
14 * If cut point is in exactly 2 blocks, expand block circles to overlap
15 * especially in the case where one block is the sole child of the other.
16 */
17
18#include <circogen/blockpath.h>
19#include <circogen/circpos.h>
20#include <circogen/nodelist.h>
21#include <math.h>
22#include <stddef.h>
23#include <util/alloc.h>
24
25/* The function determines how much the block should be rotated
26 * for best positioning with parent, assuming its center is at x and y
27 * relative to the parent.
28 * angle gives the angle of the new position, i.e., tan(angle) = y/x.
29 * If sn has 2 nodes, we arrange the line of the 2 normal to angle.
30 * If sn has 1 node, parent_pos has already been set to the
31 * correct angle assuming no rotation.
32 * Otherwise, we find the node in sn connected to the parent and rotate
33 * the block so that it is closer or at least visible to its node in the
34 * parent.
35 *
36 * For COALESCED blocks, if neighbor is in left half plane,
37 * use unCOALESCED case.
38 * Else let theta be angle, R = LEN(x,y), pho the radius of actual
39 * child block, phi be angle of neighbor in actual child block,
40 * and r the distance from center of coalesced block to center of
41 * actual block. Then, the angle to rotate the coalesced block to
42 * that the edge from the parent is tangent to the neighbor on the
43 * actual child block circle is
44 * alpha = theta + M_PI/2 - phi - arcsin((l/R)*(sin B))
45 * where l = r - rho/(cos phi) and beta = M_PI/2 + phi.
46 * Thus,
47 * alpha = theta + M_PI/2 - phi - arcsin((l/R)*(cos phi))
48 */
49static double getRotation(block_t *sn, double x, double y, double theta) {
50 double mindist2;
51 Agraph_t *subg;
52 Agnode_t *n, *closest_node, *neighbor;
53 double len2, newX, newY;
54
55 subg = sn->sub_graph;
56
57 nodelist_t *list = &sn->circle_list;
58
59 if (sn->parent_pos >= 0) {
60 theta += M_PI - sn->parent_pos;
61 if (theta < 0)
62 theta += 2 * M_PI;
63
64 return theta;
65 }
66
67 size_t count = nodelist_size(list);
68 if (count == 2) {
69 return theta - M_PI / 2.0;
70 }
71
72 /* Find node in block connected to block's parent */
73 neighbor = CHILD(sn);
74 newX = ND_pos(neighbor)[0] + x;
75 newY = ND_pos(neighbor)[1] + y;
76 mindist2 = LEN2(newX, newY); /* save sqrts by using sqr of dist to find min */
77 closest_node = neighbor;
78
79 for (n = agfstnode(subg); n; n = agnxtnode(subg, n)) {
80 if (n == neighbor)
81 continue;
82
83 newX = ND_pos(n)[0] + x;
84 newY = ND_pos(n)[1] + y;
85
86 len2 = LEN2(newX, newY);
87 if (len2 < mindist2) {
88 mindist2 = len2;
89 closest_node = n;
90 }
91 }
92
93 if (neighbor != closest_node) {
94 double rho = sn->rad0;
95 double r = sn->radius - rho;
96 double n_x = ND_pos(neighbor)[0];
97 if (COALESCED(sn) && -r < n_x) {
98 const double R = hypot(x, y);
99 double n_y = ND_pos(neighbor)[1];
100 double phi = atan2(n_y, n_x + r);
101 double l = r - rho / cos(phi);
102
103 theta += M_PI / 2.0 - phi - asin(l / R * cos(phi));
104 } else { /* Origin still at center of this block */
105 double phi = atan2(ND_pos(neighbor)[1], ND_pos(neighbor)[0]);
106 theta += M_PI - phi - PSI(neighbor);
107 if (theta > 2 * M_PI)
108 theta -= 2 * M_PI;
109 }
110 } else
111 theta = 0;
112 return theta;
113}
114
115/* Recursively apply rotation rotate followed by translation (x,y)
116 * to block sn and its children.
117 */
118static void applyDelta(block_t * sn, double x, double y, double rotate)
119{
120 block_t *child;
121 Agraph_t *subg;
122 Agnode_t *n;
123
124 subg = sn->sub_graph;
125
126 for (n = agfstnode(subg); n; n = agnxtnode(subg, n)) {
127
128 const double tmpX = ND_pos(n)[0];
129 const double tmpY = ND_pos(n)[1];
130 const double cosR = cos(rotate);
131 const double sinR = sin(rotate);
132
133 const double X = tmpX * cosR - tmpY * sinR;
134 const double Y = tmpX * sinR + tmpY * cosR;
135
136 /* translate */
137 ND_pos(n)[0] = X + x;
138 ND_pos(n)[1] = Y + y;
139 }
140
141 for (child = sn->children.first; child; child = child->next)
142 applyDelta(child, x, y, rotate);
143}
144
145/* firstangle and lastangle give the range of child angles.
146 * These are set and used only when a block has just 1 node.
147 * And are used to give the center angle between the two extremes.
148 * The parent will then be attached at PI - center angle (parent_pos).
149 * If this block has no children, this is PI. Otherwise, positionChildren will
150 * be called once with the blocks node. firstangle will be 0, with
151 * succeeding angles increasing.
152 * position can always return the center angle - PI, since the block
153 * must have children and if the block has 1 node, the limits will be
154 * correctly set. If the block has more than 1 node, the value is
155 * unused.
156 */
157typedef struct {
158 double radius; /* Basic radius of block */
159 double subtreeR; /* Max of subtree radii */
160 double nodeAngle; /* Angle allocated to each node in block */
161 double firstAngle; /* Smallest child angle when block has 1 node */
162 double lastAngle; /* Largest child angle when block has 1 node */
163 block_t *cp; /* Children of block */
164 node_t *neighbor; /* Node connected to parent block, if any */
165} posstate;
166
167typedef struct {
169 double theta; /* angle of node */
170 double minRadius; /* minimum radius for child circle */
171 double maxRadius; /* maximum radius of child blocks */
172 double diameter; /* length of arc needed for child blocks */
173 double scale; /* scale factor to increase minRadius to parents' children don't overlap */
174 int childCount; /* no. of child blocks attached at n */
175} posinfo_t;
176
178static double
179getInfo (posinfo_t* pi, posstate * stp, double min_dist)
180{
181 block_t *child;
182 double maxRadius = 0; /* Max. radius of children */
183 double diameter = 0; /* sum of child diameters */
184 int childCount = 0;
185
186 for (child = stp->cp; child; child = child->next) {
187 if (BLK_PARENT(child) == pi->n) {
188 childCount++;
189 maxRadius = fmax(maxRadius, child->radius);
190 diameter += 2 * child->radius + min_dist;
191 }
192 }
193
194 pi->diameter = diameter;
195 pi->childCount = childCount;
196 pi->minRadius = stp->radius + min_dist + maxRadius;
197 pi->maxRadius = maxRadius;
198 return maxRadius;
199}
200
201static void
203{
204 double t = p0->diameter * p1->minRadius + p1->diameter * p0->minRadius;
205
206 t /= 2*delta*p0->minRadius*p1->minRadius;
207
208 t = fmax(t, 1);
209
210 p0->scale = fmax(p0->scale, t);
211 p1->scale = fmax(p1->scale, t);
212}
213
214static void positionChildren(posinfo_t *info, posstate *stp, size_t length,
215 double min_dist) {
216 block_t *child;
217 double childAngle, childRadius, incidentAngle;
218 double mindistAngle, rotateAngle, midAngle = 0.0;
219 int midChild, cnt = 0;
220 double snRadius = stp->subtreeR; /* max subtree radius */
221 double firstAngle = stp->firstAngle;
222 double lastAngle = stp->lastAngle;
223 double d, deltaX, deltaY;
224
225 childRadius = info->scale * info->minRadius;
226 if (length == 1) {
227 childAngle = 0;
228 d = info->diameter / (2 * M_PI);
229 childRadius = fmax(childRadius, d);
230 d = 2 * M_PI * childRadius - info->diameter;
231 if (d > 0)
232 min_dist += d / info->childCount;
233 }
234 else
235 childAngle = info->theta - info->diameter / (2 * childRadius);
236
237 snRadius = fmax(snRadius, childRadius + info->maxRadius);
238
239 mindistAngle = min_dist / childRadius;
240
241 midChild = (info->childCount + 1) / 2;
242 for (child = stp->cp; child; child = child->next) {
243 if (BLK_PARENT(child) != info->n)
244 continue;
245 if (nodelist_is_empty(&child->circle_list))
246 continue;
247
248 incidentAngle = child->radius / childRadius;
249 if (length == 1) {
250 if (childAngle != 0) {
251 if (info->childCount == 2)
252 childAngle = M_PI;
253 else
254 childAngle += incidentAngle;
255 }
256
257 if (firstAngle < 0)
258 firstAngle = childAngle;
259
260 lastAngle = childAngle;
261 } else {
262 if (info->childCount == 1) {
263 childAngle = info->theta;
264 } else {
265 childAngle += incidentAngle + mindistAngle / 2;
266 }
267 }
268
269 deltaX = childRadius * cos(childAngle);
270 deltaY = childRadius * sin(childAngle);
271
272 /* first apply the delta to the immediate child and see if we need
273 * to rotate it for better edge link
274 * should return the theta value if there was a rotation else zero
275 */
276
277 rotateAngle = getRotation(child, deltaX, deltaY, childAngle);
278 applyDelta(child, deltaX, deltaY, rotateAngle);
279
280 if (length == 1) {
281 childAngle += incidentAngle + mindistAngle;
282 } else {
283 childAngle += incidentAngle + mindistAngle / 2;
284 }
285 cnt++;
286 if (cnt == midChild)
287 midAngle = childAngle;
288 }
289
290 if (length > 1 && info->n == stp->neighbor) {
291 PSI(info->n) = midAngle;
292 }
293
294 stp->subtreeR = snRadius;
295 stp->firstAngle = firstAngle;
296 stp->lastAngle = lastAngle;
297}
298
299/* Assume childCount > 0
300 * For each node in the block with children, getInfo is called, with the
301 * information stored in the parents array.
302 * This information is used by setInfo to compute the amount of space allocated
303 * to each parent and the radius at which to place its children.
304 * Finally, positionChildren is called to do the actual positioning.
305 * If length is 1, keeps track of minimum and maximum child angle.
306 */
307static double position(size_t childCount, size_t length, nodelist_t *nodepath,
308 block_t * sn, double min_dist)
309{
310 posstate state;
311 int i, counter = 0;
312 double maxRadius = 0.0;
313 double angle;
314 double theta = 0.0;
315 posinfo_t* parents = gv_calloc(childCount, sizeof(posinfo_t));
316 int num_parents = 0;
317 posinfo_t* next;
318 posinfo_t* curr;
319 double delta;
320
321 state.cp = sn->children.first;
322 state.subtreeR = sn->radius;
323 state.radius = sn->radius;
324 state.neighbor = CHILD(sn);
325 state.nodeAngle = 2 * M_PI / (double)length;
326 state.firstAngle = -1;
327 state.lastAngle = -1;
328
329 for (size_t item = 0; item < nodelist_size(nodepath); ++item) {
330 Agnode_t *n = nodelist_get(nodepath, item);
331
332 theta = counter * state.nodeAngle;
333 counter++;
334
335 if (ISPARENT(n)) {
336 parents[num_parents].n = n;
337 parents[num_parents].theta = theta;
338 maxRadius = getInfo (parents+num_parents, &state, min_dist);
339 num_parents++;
340 }
341 }
342
343 if (num_parents == 1)
344 parents->scale = 1.0;
345 else if (num_parents == 2) {
346 curr = parents;
347 next = parents+1;
348 delta = next->theta - curr->theta;
349 if (delta > M_PI)
350 delta = 2*M_PI - delta;
351 setInfo (curr, next, delta);
352 }
353 else {
354 curr = parents;
355 for (i = 0; i < num_parents; i++) {
356 if (i+1 == num_parents) {
357 next = parents;
358 delta = next->theta - curr->theta + 2*M_PI;
359 }
360 else {
361 next = curr+1;
362 delta = next->theta - curr->theta;
363 }
364 setInfo (curr, next, delta);
365 curr++;
366 }
367 }
368
369 for (i = 0; i < num_parents; i++) {
370 positionChildren(parents + i, &state, length, min_dist);
371 }
372
373 free (parents);
374
375 /* If block has only 1 child, to save space, we coalesce it with the
376 * child. Instead of having final radius sn->radius + max child radius,
377 * we have half that. However, the origin of the block is no longer in
378 * the center of the block, so we cannot do a simple rotation to get
379 * the neighbor node next to the parent block in getRotate.
380 */
381 if (childCount == 1) {
382 applyDelta(sn, -(maxRadius + min_dist / 2), 0, 0);
383 sn->radius += min_dist / 2 + maxRadius;
384 SET_COALESCED(sn);
385 } else
386 sn->radius = state.subtreeR;
387
388 angle = (state.firstAngle + state.lastAngle) / 2.0 - M_PI;
389 return angle;
390}
391
395static void doBlock(Agraph_t *g, block_t *sn, double min_dist,
396 circ_state *state) {
397 block_t *child;
398 double centerAngle = M_PI;
399
400 /* layout child subtrees */
401 size_t childCount = 0;
402 for (child = sn->children.first; child; child = child->next) {
403 doBlock(g, child, min_dist, state);
404 childCount++;
405 }
406
407 /* layout this block */
408 nodelist_t longest_path = layout_block(g, sn, min_dist, state);
409 sn->circle_list = longest_path;
410 size_t length = nodelist_size(&longest_path); // path contains everything in block
411
412 /* attach children */
413 if (childCount > 0)
414 centerAngle = position(childCount, length, &longest_path, sn, min_dist);
415
416 if (length == 1 && BLK_PARENT(sn)) {
417 sn->parent_pos = centerAngle;
418 if (sn->parent_pos < 0)
419 sn->parent_pos += 2 * M_PI;
420 }
421}
422
423void circPos(Agraph_t * g, block_t * sn, circ_state * state)
424{
425 doBlock(g, sn, state->min_dist, state);
426}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
#define M_PI
Definition arith.h:41
nodelist_t layout_block(Agraph_t *g, block_t *sn, double min_dist, circ_state *state)
Definition blockpath.c:591
#define BLK_PARENT(b)
Definition block.h:51
#define CHILD(b)
Definition block.h:50
#define COALESCED(b)
Definition block.h:55
#define SET_COALESCED(b)
Definition block.h:56
static void applyDelta(block_t *sn, double x, double y, double rotate)
Definition circpos.c:118
static void doBlock(Agraph_t *g, block_t *sn, double min_dist, circ_state *state)
Definition circpos.c:395
static void setInfo(posinfo_t *p0, posinfo_t *p1, double delta)
Definition circpos.c:202
static void positionChildren(posinfo_t *info, posstate *stp, size_t length, double min_dist)
Definition circpos.c:214
static double getRotation(block_t *sn, double x, double y, double theta)
Definition circpos.c:49
static double getInfo(posinfo_t *pi, posstate *stp, double min_dist)
get size info for blocks attached to the given node.
Definition circpos.c:179
void circPos(Agraph_t *g, block_t *sn, circ_state *state)
Definition circpos.c:423
#define ISPARENT(n)
Definition circular.h:106
#define PSI(n)
Definition circular.h:96
#define Y(i)
Definition gdefs.h:3
#define X(prefix, name, str, type, subtype,...)
Definition gdefs.h:14
#define LEN2(a, b)
Definition geom.h:59
void free(void *)
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:210
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_pos(n)
Definition types.h:520
#define neighbor(t, i, edim, elist)
Definition make_map.h:41
#define delta
Definition maze.c:133
static void rotate(int n, int dim, double *x, double angle)
graph or subgraph
Definition cgraph.h:425
Definition block.h:26
double radius
Definition block.h:30
nodelist_t circle_list
Definition block.h:32
blocklist_t children
Definition block.h:33
Agraph_t * sub_graph
Definition block.h:29
double parent_pos
Definition block.h:34
block_t * next
Definition block.h:28
double rad0
Definition block.h:31
block_t * first
Definition block.h:22
double min_dist
Definition circular.h:24
Definition utils.c:747
double maxRadius
Definition circpos.c:171
double theta
Definition circpos.c:169
Agnode_t * n
Definition circpos.c:168
int childCount
Definition circpos.c:174
double scale
Definition circpos.c:173
double diameter
Definition circpos.c:172
double minRadius
Definition circpos.c:170
node_t * neighbor
Definition circpos.c:164
double radius
Definition circpos.c:158
double lastAngle
Definition circpos.c:162
double nodeAngle
Definition circpos.c:160
double subtreeR
Definition circpos.c:159
double firstAngle
Definition circpos.c:161
block_t * cp
Definition circpos.c:163