Graphviz 12.0.1~dev.20240715.2254
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#include <cgraph/alloc.h>
14/* TODO:
15 * If cut point is in exactly 2 blocks, expand block circles to overlap
16 * especially in the case where one block is the sole child of the other.
17 */
18
19#include <circogen/blockpath.h>
20#include <circogen/circpos.h>
21#include <circogen/nodelist.h>
22#include <math.h>
23#include <stddef.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 if (maxRadius < child->radius) {
190 maxRadius = child->radius;
191 }
192 diameter += 2 * child->radius + min_dist;
193 }
194 }
195
196 pi->diameter = diameter;
197 pi->childCount = childCount;
198 pi->minRadius = stp->radius + min_dist + maxRadius;
199 pi->maxRadius = maxRadius;
200 return maxRadius;
201}
202
203static void
205{
206 double t = p0->diameter * p1->minRadius + p1->diameter * p0->minRadius;
207
208 t /= 2*delta*p0->minRadius*p1->minRadius;
209
210 t = fmax(t, 1);
211
212 p0->scale = fmax(p0->scale, t);
213 p1->scale = fmax(p1->scale, t);
214}
215
216static void positionChildren(posinfo_t *info, posstate *stp, size_t length,
217 double min_dist) {
218 block_t *child;
219 double childAngle, childRadius, incidentAngle;
220 double mindistAngle, rotateAngle, midAngle = 0.0;
221 int midChild, cnt = 0;
222 double snRadius = stp->subtreeR; /* max subtree radius */
223 double firstAngle = stp->firstAngle;
224 double lastAngle = stp->lastAngle;
225 double d, deltaX, deltaY;
226
227 childRadius = info->scale * info->minRadius;
228 if (length == 1) {
229 childAngle = 0;
230 d = info->diameter / (2 * M_PI);
231 childRadius = fmax(childRadius, d);
232 d = 2 * M_PI * childRadius - info->diameter;
233 if (d > 0)
234 min_dist += d / info->childCount;
235 }
236 else
237 childAngle = info->theta - info->diameter / (2 * childRadius);
238
239 if ((childRadius + info->maxRadius) > snRadius)
240 snRadius = childRadius + info->maxRadius;
241
242 mindistAngle = min_dist / childRadius;
243
244 midChild = (info->childCount + 1) / 2;
245 for (child = stp->cp; child; child = child->next) {
246 if (BLK_PARENT(child) != info->n)
247 continue;
248 if (nodelist_is_empty(&child->circle_list))
249 continue;
250
251 incidentAngle = child->radius / childRadius;
252 if (length == 1) {
253 if (childAngle != 0) {
254 if (info->childCount == 2)
255 childAngle = M_PI;
256 else
257 childAngle += incidentAngle;
258 }
259
260 if (firstAngle < 0)
261 firstAngle = childAngle;
262
263 lastAngle = childAngle;
264 } else {
265 if (info->childCount == 1) {
266 childAngle = info->theta;
267 } else {
268 childAngle += incidentAngle + mindistAngle / 2;
269 }
270 }
271
272 deltaX = childRadius * cos(childAngle);
273 deltaY = childRadius * sin(childAngle);
274
275 /* first apply the delta to the immediate child and see if we need
276 * to rotate it for better edge link
277 * should return the theta value if there was a rotation else zero
278 */
279
280 rotateAngle = getRotation(child, deltaX, deltaY, childAngle);
281 applyDelta(child, deltaX, deltaY, rotateAngle);
282
283 if (length == 1) {
284 childAngle += incidentAngle + mindistAngle;
285 } else {
286 childAngle += incidentAngle + mindistAngle / 2;
287 }
288 cnt++;
289 if (cnt == midChild)
290 midAngle = childAngle;
291 }
292
293 if (length > 1 && info->n == stp->neighbor) {
294 PSI(info->n) = midAngle;
295 }
296
297 stp->subtreeR = snRadius;
298 stp->firstAngle = firstAngle;
299 stp->lastAngle = lastAngle;
300}
301
302/* Assume childCount > 0
303 * For each node in the block with children, getInfo is called, with the
304 * information stored in the parents array.
305 * This information is used by setInfo to compute the amount of space allocated
306 * to each parent and the radius at which to place its children.
307 * Finally, positionChildren is called to do the actual positioning.
308 * If length is 1, keeps track of minimum and maximum child angle.
309 */
310static double position(size_t childCount, size_t length, nodelist_t *nodepath,
311 block_t * sn, double min_dist)
312{
314 int i, counter = 0;
315 double maxRadius = 0.0;
316 double angle;
317 double theta = 0.0;
318 posinfo_t* parents = gv_calloc(childCount, sizeof(posinfo_t));
319 int num_parents = 0;
320 posinfo_t* next;
321 posinfo_t* curr;
322 double delta;
323
324 state.cp = sn->children.first;
325 state.subtreeR = sn->radius;
326 state.radius = sn->radius;
327 state.neighbor = CHILD(sn);
328 state.nodeAngle = 2 * M_PI / (double)length;
329 state.firstAngle = -1;
330 state.lastAngle = -1;
331
332 for (size_t item = 0; item < nodelist_size(nodepath); ++item) {
333 Agnode_t *n = nodelist_get(nodepath, item);
334
335 theta = counter * state.nodeAngle;
336 counter++;
337
338 if (ISPARENT(n)) {
339 parents[num_parents].n = n;
340 parents[num_parents].theta = theta;
341 maxRadius = getInfo (parents+num_parents, &state, min_dist);
342 num_parents++;
343 }
344 }
345
346 if (num_parents == 1)
347 parents->scale = 1.0;
348 else if (num_parents == 2) {
349 curr = parents;
350 next = parents+1;
351 delta = next->theta - curr->theta;
352 if (delta > M_PI)
353 delta = 2*M_PI - delta;
354 setInfo (curr, next, delta);
355 }
356 else {
357 curr = parents;
358 for (i = 0; i < num_parents; i++) {
359 if (i+1 == num_parents) {
360 next = parents;
361 delta = next->theta - curr->theta + 2*M_PI;
362 }
363 else {
364 next = curr+1;
365 delta = next->theta - curr->theta;
366 }
367 setInfo (curr, next, delta);
368 curr++;
369 }
370 }
371
372 for (i = 0; i < num_parents; i++) {
373 positionChildren(parents + i, &state, length, min_dist);
374 }
375
376 free (parents);
377
378 /* If block has only 1 child, to save space, we coalesce it with the
379 * child. Instead of having final radius sn->radius + max child radius,
380 * we have half that. However, the origin of the block is no longer in
381 * the center of the block, so we cannot do a simple rotation to get
382 * the neighbor node next to the parent block in getRotate.
383 */
384 if (childCount == 1) {
385 applyDelta(sn, -(maxRadius + min_dist / 2), 0, 0);
386 sn->radius += min_dist / 2 + maxRadius;
387 SET_COALESCED(sn);
388 } else
389 sn->radius = state.subtreeR;
390
391 angle = (state.firstAngle + state.lastAngle) / 2.0 - M_PI;
392 return angle;
393}
394
398static void doBlock(Agraph_t *g, block_t *sn, double min_dist,
399 circ_state *state) {
400 block_t *child;
401 double centerAngle = M_PI;
402
403 /* layout child subtrees */
404 size_t childCount = 0;
405 for (child = sn->children.first; child; child = child->next) {
406 doBlock(g, child, min_dist, state);
407 childCount++;
408 }
409
410 /* layout this block */
411 nodelist_t longest_path = layout_block(g, sn, min_dist, state);
412 sn->circle_list = longest_path;
413 size_t length = nodelist_size(&longest_path); // path contains everything in block
414
415 /* attach children */
416 if (childCount > 0)
417 centerAngle = position(childCount, length, &longest_path, sn, min_dist);
418
419 if (length == 1 && BLK_PARENT(sn)) {
420 sn->parent_pos = centerAngle;
421 if (sn->parent_pos < 0)
422 sn->parent_pos += 2 * M_PI;
423 }
424}
425
427{
428 doBlock(g, sn, state->min_dist, state);
429}
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:594
#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:398
static void setInfo(posinfo_t *p0, posinfo_t *p1, double delta)
Definition circpos.c:204
static void positionChildren(posinfo_t *info, posstate *stp, size_t length, double min_dist)
Definition circpos.c:216
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:426
#define ISPARENT(n)
Definition circular.h:105
#define PSI(n)
Definition circular.h:95
#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:199
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
static lexstate_t state
Definition htmllex.c:61
#define neighbor(t, i, edim, elist)
Definition make_map.h:37
#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
Definition utils.c:748
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