Graphviz 14.1.3~dev.20260207.0611
Loading...
Searching...
No Matches
circle.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#include "config.h"
12
13#include <assert.h>
14#include <twopigen/circle.h>
15#include <inttypes.h>
16#include <limits.h>
17#include <math.h>
18#include <stdbool.h>
19#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
22#include <util/alloc.h>
23#include <util/gv_ctype.h>
24#include <util/gv_math.h>
25#include <util/list.h>
26#include <util/streq.h>
27#define DEF_RANKSEP 1.00
28#define UNSET 10.00
29
30/* dfs to set distance from a particular leaf.
31 * Note that termination is implicit in the test
32 * for reduced number of steps. Proof?
33 */
35{
36 Agnode_t *next;
37
38 uint64_t nsteps = SLEAF(n) + 1;
39
40 for (Agedge_t *ep = agfstedge(g, n); ep; ep = agnxtedge(g, ep, n)) {
41 if ((next = agtail(ep)) == n)
42 next = aghead(ep);
43
44 if (prev == next)
45 continue;
46
47 if (nsteps < SLEAF(next)) { /* handles loops and multiedges */
48 SLEAF(next) = nsteps;
49 setNStepsToLeaf(g, next, n);
50 }
51 }
52}
53
55static bool isLeaf(Agraph_t * g, Agnode_t * n)
56{
57 Agnode_t *neighp = 0;
58 Agnode_t *np;
59
60 for (Agedge_t *ep = agfstedge(g, n); ep; ep = agnxtedge(g, ep, n)) {
61 if ((np = agtail(ep)) == n)
62 np = aghead(ep);
63 if (n == np)
64 continue; /* loop */
65 if (neighp) {
66 if (neighp != np)
67 return false; /* two different neighbors */
68 } else
69 neighp = np;
70 }
71 return true;
72}
73
74static void initLayout(Agraph_t * g)
75{
76 int nnodes = agnnodes(g);
77 assert(nnodes >= 0);
78 uint64_t INF = (uint64_t)nnodes * (uint64_t)nnodes;
79
80 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
81 SCENTER(n) = INF;
82 THETA(n) = UNSET; /* marks theta as unset, since 0 <= theta <= 2PI */
83 if (isLeaf(g, n))
84 SLEAF(n) = 0;
85 else
86 SLEAF(n) = INF;
87 }
88}
89
90/*
91 * Working recursively in from each leaf node (ie, each node
92 * with nStepsToLeaf == 0; see initLayout), set the
93 * minimum value of nStepsToLeaf for each node. Using
94 * that information, assign some node to be the centerNode.
95*/
97{
99 uint64_t maxNStepsToLeaf = 0;
100
101 /* dfs from each leaf node */
102 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
103 if (SLEAF(n) == 0)
104 setNStepsToLeaf(g, n, 0);
105 }
106
107 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
108 if (center == NULL || SLEAF(n) > maxNStepsToLeaf) {
109 maxNStepsToLeaf = SLEAF(n);
110 center = n;
111 }
112 }
113 return center;
114}
115
116/* bfs to create tree structure */
118{
119 Agnode_t *next;
120 Agsym_t* wt = agfindedgeattr(g,"weight");
121 LIST(Agnode_t *) q = {0};
122
123 LIST_PUSH_BACK(&q, n);
124 while (!LIST_IS_EMPTY(&q)) {
125 n = LIST_POP_FRONT(&q);
126 uint64_t nsteps = SCENTER(n) + 1;
127 for (Agedge_t *ep = agfstedge(g, n); ep; ep = agnxtedge(g, ep, n)) {
128 if (wt && streq(agxget(ep,wt), "0")) continue;
129 if ((next = agtail(ep)) == n)
130 next = aghead(ep);
131 if (nsteps < SCENTER(next)) {
132 SCENTER(next) = nsteps;
133 SPARENT(next) = n;
134 NCHILD(n)++;
135 LIST_PUSH_BACK(&q, next);
136 }
137 }
138 }
139 LIST_FREE(&q);
140}
141
142/*
143 * Work out from the center and determine the value of
144 * nStepsToCenter and parent node for each node.
145 * Return UINT64_MAX if some node was not reached.
146 */
147static uint64_t setParentNodes(Agraph_t * sg, Agnode_t * center)
148{
149 uint64_t maxn = 0;
150 uint64_t unset = SCENTER(center);
151
152 SCENTER(center) = 0;
153 SPARENT(center) = 0;
155
156 /* find the maximum number of steps from the center */
157 for (Agnode_t *n = agfstnode(sg); n; n = agnxtnode(sg, n)) {
158 if (SCENTER(n) == unset) {
159 return UINT64_MAX;
160 }
161 else if (SCENTER(n) > maxn) {
162 maxn = SCENTER(n);
163 }
164 }
165 return maxn;
166}
167
168/* Sets each node's subtreeSize, which counts the number of
169 * leaves in subtree rooted at the node.
170 * At present, this is done bottom-up.
171 */
172static void setSubtreeSize(Agraph_t * g)
173{
174 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
175 if (NCHILD(n) > 0)
176 continue;
177 STSIZE(n)++;
179 STSIZE(parent)++;
180 }
181 }
182}
183
185{
186 Agnode_t *next;
187
188 // Only integers up to 2⁵³ can be precisely stored in an IEEE 754 double. We
189 // do not expect subtree size to exceed this.
190 assert(STSIZE(n) <= UINT64_C(1) << 53);
191
192 const double ratio = SPAN(n) / (double)STSIZE(n);
193 for (Agedge_t *ep = agfstedge(g, n); ep; ep = agnxtedge(g, ep, n)) {
194 if ((next = agtail(ep)) == n)
195 next = aghead(ep);
196 if (SPARENT(next) != n)
197 continue; /* handles loops */
198
199 if (!is_exactly_zero(SPAN(next)))
200 continue; /* multiedges */
201 assert(STSIZE(next) <= UINT64_C(1) << 53);
202 SPAN(next) = ratio * (double)STSIZE(next);
203
204 if (NCHILD(next) > 0) {
205 setChildSubtreeSpans(g, next);
206 }
207 }
208}
209
211{
212 SPAN(center) = 2 * M_PI;
214}
215
217static bool is_set(double a) { return !is_exactly_equal(a, UNSET); }
218
219 /* Set the node positions for the 2nd and later rings. */
220static void setChildPositions(Agraph_t * sg, Agnode_t * n)
221{
222 Agnode_t *next;
223 double theta; /* theta is the lower boundary radius of the fan */
224
225 if (SPARENT(n) == 0) /* center */
226 theta = 0;
227 else
228 theta = THETA(n) - SPAN(n) / 2;
229
230 for (Agedge_t *ep = agfstedge(sg, n); ep; ep = agnxtedge(sg, ep, n)) {
231 if ((next = agtail(ep)) == n)
232 next = aghead(ep);
233 if (SPARENT(next) != n)
234 continue; /* handles loops */
235 if (is_set(THETA(next)))
236 continue; /* handles multiedges */
237
238 THETA(next) = theta + SPAN(next) / 2.0;
239 theta += SPAN(next);
240
241 if (NCHILD(next) > 0)
242 setChildPositions(sg, next);
243 }
244}
245
247{
248 THETA(center) = 0;
250}
251
252/* Return array of doubles of size maxrank+1 containing the radius of each
253 * rank. Position 0 always contains 0. Use the colon-separated list of
254 * doubles provided by ranksep to get the deltas for each additional rank.
255 * If not enough values are provided, the last value is repeated.
256 * If the ranksep attribute is not provided, use DEF_RANKSEP for all values.
257 */
258static double*
259getRankseps (Agraph_t* g, uint64_t maxrank)
260{
261 char *p;
262 char *endp;
263 uint64_t rk = 1;
264 double* ranks = gv_calloc(maxrank + 1, sizeof(double));
265 double xf = 0.0, delx = 0.0, d;
266
267 if ((p = late_string(g, agfindgraphattr(g->root, "ranksep"), NULL))) {
268 while (rk <= maxrank && (d = strtod (p, &endp)) > 0) {
269 delx = fmax(d, MIN_RANKSEP);
270 xf += delx;
271 ranks[rk++] = xf;
272 p = endp;
273 while (gv_isspace(*p) || *p == ':')
274 p++;
275 }
276 }
277 else {
278 delx = DEF_RANKSEP;
279 }
280
281 for (uint64_t i = rk; i <= maxrank; i++) {
282 xf += delx;
283 ranks[i] = xf;
284 }
285
286 return ranks;
287}
288
289static void setAbsolutePos(Agraph_t * g, uint64_t maxrank)
290{
291 double* ranksep = getRankseps (g, maxrank);
292 if (Verbose) {
293 fputs ("Rank separation = ", stderr);
294 for (uint64_t i = 0; i <= maxrank; i++)
295 fprintf (stderr, "%.03lf ", ranksep[i]);
296 fputs ("\n", stderr);
297 }
298
299 /* Convert circular to cartesian coordinates */
300 for (Agnode_t *n = agfstnode(g); n; n = agnxtnode(g, n)) {
301 double hyp = ranksep[SCENTER(n)];
302 ND_pos(n)[0] = hyp * cos(THETA(n));
303 ND_pos(n)[1] = hyp * sin(THETA(n));
304 }
305 free (ranksep);
306}
307
308/* We assume sg is is connected and non-empty.
309 * Also, if center != 0, we are guaranteed that center is
310 * in the graph.
311 */
313{
314 if (agnnodes(sg) == 1) {
315 Agnode_t *n = agfstnode(sg);
316 ND_pos(n)[0] = 0;
317 ND_pos(n)[1] = 0;
318 return center;
319 }
320
321 initLayout(sg);
322
323 if (!center)
325
326 uint64_t maxNStepsToCenter = setParentNodes(sg,center);
327 if (Verbose)
328 fprintf(stderr, "root = %s max steps to root = %" PRIu64 "\n",
329 agnameof(center), maxNStepsToCenter);
330 if (maxNStepsToCenter == UINT64_MAX) {
331 agerrorf("twopi: use of weight=0 creates disconnected component.\n");
332 return center;
333 }
334
335 setSubtreeSize(sg);
336
338
339 setPositions(sg, center);
340
341 setAbsolutePos(sg, maxNStepsToCenter);
342 return center;
343}
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
static void setNStepsToCenter(Agraph_t *g, Agnode_t *n)
Definition circle.c:117
static double * getRankseps(Agraph_t *g, uint64_t maxrank)
Definition circle.c:259
static void setPositions(Agraph_t *sg, Agnode_t *center)
Definition circle.c:246
static void setSubtreeSize(Agraph_t *g)
Definition circle.c:172
static void setChildPositions(Agraph_t *sg, Agnode_t *n)
Definition circle.c:220
static void setChildSubtreeSpans(Agraph_t *g, Agnode_t *n)
Definition circle.c:184
static bool is_set(double a)
has the given value been assigned?
Definition circle.c:217
static void setNStepsToLeaf(Agraph_t *g, Agnode_t *n, Agnode_t *prev)
Definition circle.c:34
static void setAbsolutePos(Agraph_t *g, uint64_t maxrank)
Definition circle.c:289
static void initLayout(Agraph_t *g)
Definition circle.c:74
static Agnode_t * findCenterNode(Agraph_t *g)
Definition circle.c:96
Agnode_t * circleLayout(Agraph_t *sg, Agnode_t *center)
Definition circle.c:312
#define UNSET
Definition circle.c:28
static void setSubtreeSpans(Agraph_t *sg, Agnode_t *center)
Definition circle.c:210
#define DEF_RANKSEP
Definition circle.c:27
static uint64_t setParentNodes(Agraph_t *sg, Agnode_t *center)
Definition circle.c:147
static bool isLeaf(Agraph_t *g, Agnode_t *n)
return true if n is a leaf node
Definition circle.c:55
#define SLEAF(n)
Definition circle.h:30
#define THETA(n)
Definition circle.h:36
#define NCHILD(n)
Definition circle.h:32
#define SCENTER(n)
Definition circle.h:33
#define SPAN(n)
Definition circle.h:35
#define STSIZE(n)
Definition circle.h:31
#define parent(i)
Definition closest.c:75
char * late_string(void *obj, attrsym_t *attr, char *defaultValue)
Definition utils.c:84
#define MIN_RANKSEP
Definition const.h:88
static bool Verbose
Definition gml2gv.c:26
void free(void *)
node NULL
Definition grammar.y:181
int agnnodes(Agraph_t *g)
Definition graph.c:157
char * agxget(void *obj, Agsym_t *sym)
Definition attr.c:460
#define agfindedgeattr(g, a)
Definition types.h:617
#define agtail(e)
Definition cgraph.h:977
Agedge_t * agnxtedge(Agraph_t *g, Agedge_t *e, Agnode_t *n)
Definition edge.c:98
#define aghead(e)
Definition cgraph.h:978
Agedge_t * agfstedge(Agraph_t *g, Agnode_t *n)
Definition edge.c:89
void agerrorf(const char *fmt,...)
Definition agerror.c:167
#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_pos(n)
Definition types.h:520
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:145
replacements for ctype.h functions
static bool gv_isspace(int c)
Definition gv_ctype.h:55
Arithmetic helper functions.
static bool is_exactly_zero(double v)
is a value precisely 0.0?
Definition gv_math.h:67
static bool is_exactly_equal(double a, double b)
are two values precisely the same?
Definition gv_math.h:48
$2 prev
Definition htmlparse.y:291
type-generic dynamically expanding list
#define LIST(type)
Definition list.h:55
#define LIST_POP_FRONT(list)
Definition list.h:394
#define LIST_FREE(list)
Definition list.h:370
#define LIST_IS_EMPTY(list)
Definition list.h:90
#define LIST_PUSH_BACK(list, item)
Definition list.h:384
#define SPARENT(n)
Definition patchwork.h:25
#define INF
Definition sccmap.c:43
static bool streq(const char *a, const char *b)
are a and b equal?
Definition streq.h:11
graph or subgraph
Definition cgraph.h:424
Agraph_t * root
subgraphs - ancestors
Definition cgraph.h:433
string attribute descriptor symbol in Agattr_s.dict
Definition cgraph.h:640
static point center(point vertex[], size_t n)