Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
flat.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 <dotgen/dot.h>
12#include <stdbool.h>
13#include <stddef.h>
14#include <util/alloc.h>
15
16static node_t *make_vn_slot(graph_t * g, int r, int pos)
17{
18 int i;
19 node_t **v, *n;
20
21 v = GD_rank(g)[r].v = gv_recalloc(GD_rank(g)[r].v, GD_rank(g)[r].n + 1,
22 GD_rank(g)[r].n + 2, sizeof(node_t *));
23 for (i = GD_rank(g)[r].n; i > pos; i--) {
24 v[i] = v[i - 1];
25 ND_order(v[i])++;
26 }
27 n = v[pos] = virtual_node(g);
28 ND_order(n) = pos;
29 ND_rank(n) = r;
30 v[++(GD_rank(g)[r].n)] = NULL;
31 return v[pos];
32}
33
34#define HLB 0 /* hard left bound */
35#define HRB 1 /* hard right bound */
36#define SLB 2 /* soft left bound */
37#define SRB 3 /* soft right bound */
38
39static void findlr(node_t * u, node_t * v, int *lp, int *rp)
40{
41 int l, r;
42 l = ND_order(u);
43 r = ND_order(v);
44 if (l > r) {
45 int t = l;
46 l = r;
47 r = t;
48 }
49 *lp = l;
50 *rp = r;
51}
52
53static void setbounds(node_t * v, int *bounds, int lpos, int rpos)
54{
55 int i, l, r, ord;
56 edge_t *f;
57
58 if (ND_node_type(v) == VIRTUAL) {
59 ord = ND_order(v);
60 if (ND_in(v).size == 0) { /* flat */
61 assert(ND_out(v).size == 2);
62 findlr(aghead(ND_out(v).list[0]), aghead(ND_out(v).list[1]), &l,
63 &r);
64 /* the other flat edge could be to the left or right */
65 if (r <= lpos)
66 bounds[SLB] = bounds[HLB] = ord;
67 else if (l >= rpos)
68 bounds[SRB] = bounds[HRB] = ord;
69 /* could be spanning this one */
70 else if ((l < lpos) && (r > rpos)); /* ignore */
71 /* must have intersecting ranges */
72 else {
73 if ((l < lpos) || ((l == lpos) && (r < rpos)))
74 bounds[SLB] = ord;
75 if ((r > rpos) || ((r == rpos) && (l > lpos)))
76 bounds[SRB] = ord;
77 }
78 } else { /* forward */
79 bool onleft, onright;
80 onleft = onright = false;
81 for (i = 0; (f = ND_out(v).list[i]); i++) {
82 if (ND_order(aghead(f)) <= lpos) {
83 onleft = true;
84 continue;
85 }
86 if (ND_order(aghead(f)) >= rpos) {
87 onright = true;
88 continue;
89 }
90 }
91 if (onleft && !onright)
92 bounds[HLB] = ord + 1;
93 if (onright && !onleft)
94 bounds[HRB] = ord - 1;
95 }
96 }
97}
98
99static int flat_limits(graph_t * g, edge_t * e)
100{
101 int lnode, rnode, r, bounds[4], lpos, rpos, pos;
102 node_t **rank;
103
104 r = ND_rank(agtail(e)) - 1;
105 rank = GD_rank(g)[r].v;
106 lnode = 0;
107 rnode = GD_rank(g)[r].n - 1;
108 bounds[HLB] = bounds[SLB] = lnode - 1;
109 bounds[HRB] = bounds[SRB] = rnode + 1;
110 findlr(agtail(e), aghead(e), &lpos, &rpos);
111 while (lnode <= rnode) {
112 setbounds(rank[lnode], bounds, lpos, rpos);
113 if (lnode != rnode)
114 setbounds(rank[rnode], bounds, lpos, rpos);
115 lnode++;
116 rnode--;
117 if (bounds[HRB] - bounds[HLB] <= 1)
118 break;
119 }
120 if (bounds[HLB] <= bounds[HRB])
121 pos = (bounds[HLB] + bounds[HRB] + 1) / 2;
122 else
123 pos = (bounds[SLB] + bounds[SRB] + 1) / 2;
124 return pos;
125}
126
127/* flat_node:
128 * Create virtual node representing edge label between
129 * actual ends of edge e.
130 * This node is characterized by being virtual and having a non-NULL
131 * ND_alg pointing to e.
132 */
133static void
135{
136 int r, place;
137 double ypos, h2;
138 graph_t *g;
139 node_t *n, *vn;
140 edge_t *ve;
141 pointf dimen;
142
143 if (ED_label(e) == NULL)
144 return;
145 g = dot_root(agtail(e));
146 r = ND_rank(agtail(e));
147
148 place = flat_limits(g, e);
149 /* grab ypos = LL.y of label box before make_vn_slot() */
150 if ((n = GD_rank(g)[r - 1].v[0]))
151 ypos = ND_coord(n).y - GD_rank(g)[r - 1].ht1;
152 else {
153 n = GD_rank(g)[r].v[0];
154 ypos = ND_coord(n).y + GD_rank(g)[r].ht2 + GD_ranksep(g);
155 }
156 vn = make_vn_slot(g, r - 1, place);
157 dimen = ED_label(e)->dimen;
158 if (GD_flip(g)) {
159 double f = dimen.x;
160 dimen.x = dimen.y;
161 dimen.y = f;
162 }
163 ND_ht(vn) = dimen.y;
164 h2 = ND_ht(vn) / 2;
165 ND_lw(vn) = ND_rw(vn) = dimen.x / 2;
166 ND_label(vn) = ED_label(e);
167 ND_coord(vn).y = ypos + h2;
168 ve = virtual_edge(vn, agtail(e), e); /* was NULL? */
169 ED_tail_port(ve).p.x = -ND_lw(vn);
170 ED_head_port(ve).p.x = ND_rw(agtail(e));
172 ve = virtual_edge(vn, aghead(e), e);
173 ED_tail_port(ve).p.x = ND_rw(vn);
174 ED_head_port(ve).p.x = ND_lw(aghead(e));
176 /* another assumed symmetry of ht1/ht2 of a label node */
177 if (GD_rank(g)[r - 1].ht1 < h2)
178 GD_rank(g)[r - 1].ht1 = h2;
179 if (GD_rank(g)[r - 1].ht2 < h2)
180 GD_rank(g)[r - 1].ht2 = h2;
181 ND_alg(vn) = e;
182}
183
184static void abomination(graph_t * g)
185{
186 int r;
187
188 assert(GD_minrank(g) == 0);
189 /* 3 = one for new rank, one for sentinel, one for off-by-one */
190 r = GD_maxrank(g) + 3;
191 rank_t *rptr = gv_recalloc(GD_rank(g), GD_maxrank(g) + 1, r,
192 sizeof(rank_t));
193 GD_rank(g) = rptr + 1;
194 for (r = GD_maxrank(g); r >= 0; r--)
195 GD_rank(g)[r] = GD_rank(g)[r - 1];
196 GD_rank(g)[r].n = GD_rank(g)[r].an = 0;
197 GD_rank(g)[r].v = GD_rank(g)[r].av = gv_calloc(2, sizeof(node_t *));
198 GD_rank(g)[r].flat = NULL;
199 GD_rank(g)[r].ht1 = GD_rank(g)[r].ht2 = 1;
200 GD_rank(g)[r].pht1 = GD_rank(g)[r].pht2 = 1;
201 GD_minrank(g)--;
202}
203
204/* checkFlatAdjacent:
205 * Check if tn and hn are adjacent.
206 * If so, set adjacent bit on all related edges.
207 * Assume e is flat.
208 */
209static void
211{
212 node_t* tn = agtail(e);
213 node_t* hn = aghead(e);
214 int i, lo, hi;
215 node_t* n;
216 rank_t *rank;
217
218 if (ND_order(tn) < ND_order(hn)) {
219 lo = ND_order(tn);
220 hi = ND_order(hn);
221 }
222 else {
223 lo = ND_order(hn);
224 hi = ND_order(tn);
225 }
226 rank = &(GD_rank(dot_root(tn))[ND_rank(tn)]);
227 for (i = lo + 1; i < hi; i++) {
228 n = rank->v[i];
229 if ((ND_node_type(n) == VIRTUAL && ND_label(n)) ||
230 ND_node_type(n) == NORMAL)
231 break;
232 }
233 if (i == hi) { /* adjacent edge */
234 do {
235 ED_adjacent(e) = 1;
236 e = ED_to_virt(e);
237 } while (e);
238 }
239}
240
241/* flat_edges:
242 * Process flat edges.
243 * First, mark flat edges as having adjacent endpoints or not.
244 *
245 * Second, if there are edge labels, nodes are placed on ranks 0,2,4,...
246 * If we have a labeled flat edge on rank 0, add a rank -1.
247 *
248 * Finally, create label information. Add a virtual label node in the
249 * previous rank for each labeled, non-adjacent flat edge. If this is
250 * done for any edge, return true, so that main code will reset y coords.
251 * For labeled adjacent flat edges, store label width in representative edge.
252 * FIX: We should take into account any extra height needed for the latter
253 * labels.
254 *
255 * We leave equivalent flat edges in ND_other. Their ED_virt field should
256 * still point to the class representative.
257 */
258int
260{
261 int i;
262 bool reset = false;
263 node_t *n;
264 edge_t *e;
265
266 for (n = GD_nlist(g); n; n = ND_next(n)) {
267 if (ND_flat_out(n).list) {
268 for (size_t j = 0; (e = ND_flat_out(n).list[j]); j++) {
270 }
271 }
272 for (size_t j = 0; j < ND_other(n).size; j++) {
273 e = ND_other(n).list[j];
274 if (ND_rank(aghead(e)) == ND_rank(agtail(e)))
276 }
277 }
278
279 if ((GD_rank(g)[0].flat) || (GD_n_cluster(g) > 0)) {
280 bool found = false;
281 for (i = 0; (n = GD_rank(g)[0].v[i]); i++) {
282 for (size_t j = 0; (e = ND_flat_in(n).list[j]); j++) {
283 if ((ED_label(e)) && !ED_adjacent(e)) {
284 abomination(g);
285 found = true;
286 break;
287 }
288 }
289 if (found)
290 break;
291 }
292 }
293
295 for (n = GD_nlist(g); n; n = ND_next(n)) {
296 /* if n is the tail of any flat edge, one will be in flat_out */
297 if (ND_flat_out(n).list) {
298 for (i = 0; (e = ND_flat_out(n).list[i]); i++) {
299 if (ED_label(e)) {
300 if (ED_adjacent(e)) {
301 if (GD_flip(g)) ED_dist(e) = ED_label(e)->dimen.y;
302 else ED_dist(e) = ED_label(e)->dimen.x;
303 }
304 else {
305 reset = true;
306 flat_node(e);
307 }
308 }
309 }
310 /* look for other flat edges with labels */
311 for (size_t j = 0; j < ND_other(n).size; j++) {
312 edge_t* le;
313 e = ND_other(n).list[j];
314 if (ND_rank(agtail(e)) != ND_rank(aghead(e))) continue;
315 if (agtail(e) == aghead(e)) continue; /* skip loops */
316 le = e;
317 while (ED_to_virt(le)) le = ED_to_virt(le);
319 if (ED_label(e)) {
320 if (ED_adjacent(e)) {
321 double lw;
322 if (GD_flip(g)) lw = ED_label(e)->dimen.y;
323 else lw = ED_label(e)->dimen.x;
324 ED_dist(le) = MAX(lw,ED_dist(le));
325 }
326 else {
327 reset = true;
328 flat_node(e);
329 }
330 }
331 }
332 }
333 }
334 if (reset) {
337 }
338 return reset;
339}
Memory allocation wrappers that exit on failure.
static void * gv_recalloc(void *ptr, size_t old_nmemb, size_t new_nmemb, size_t size)
Definition alloc.h:73
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
#define NORMAL
Definition const.h:24
#define FLATORDER
Definition const.h:28
#define VIRTUAL
Definition const.h:25
Agraph_t * dot_root(void *p)
Definition dotinit.c:496
Agedge_t * virtual_edge(Agnode_t *, Agnode_t *, Agedge_t *)
Definition fastgr.c:172
void checkLabelOrder(graph_t *g)
Definition mincross.c:305
void rec_reset_vlists(Agraph_t *)
Definition mincross.c:948
void rec_save_vlists(Agraph_t *)
Definition mincross.c:938
Agnode_t * virtual_node(Agraph_t *)
Definition fastgr.c:202
#define le
Definition edges.h:25
static void flat_node(edge_t *e)
Definition flat.c:134
static int flat_limits(graph_t *g, edge_t *e)
Definition flat.c:99
#define HLB
Definition flat.c:34
int flat_edges(graph_t *g)
Definition flat.c:259
static void checkFlatAdjacent(edge_t *e)
Definition flat.c:210
static void abomination(graph_t *g)
Definition flat.c:184
#define HRB
Definition flat.c:35
static node_t * make_vn_slot(graph_t *g, int r, int pos)
Definition flat.c:16
#define SRB
Definition flat.c:37
#define SLB
Definition flat.c:36
static void findlr(node_t *u, node_t *v, int *lp, int *rp)
Definition flat.c:39
static void setbounds(node_t *v, int *bounds, int lpos, int rpos)
Definition flat.c:53
node NULL
Definition grammar.y:163
#define ED_dist(e)
Definition types.h:602
#define agtail(e)
Definition cgraph.h:880
#define ED_edge_type(e)
Definition types.h:582
#define ED_adjacent(e)
Definition types.h:584
#define aghead(e)
Definition cgraph.h:881
#define ED_head_port(e)
Definition types.h:588
#define ED_label(e)
Definition types.h:589
#define ED_tail_port(e)
Definition types.h:597
#define ED_to_virt(e)
Definition types.h:599
#define GD_minrank(g)
Definition types.h:384
#define GD_maxrank(g)
Definition types.h:382
#define GD_rank(g)
Definition types.h:395
#define GD_nlist(g)
Definition types.h:393
#define GD_n_cluster(g)
Definition types.h:389
#define GD_flip(g)
Definition types.h:378
#define GD_ranksep(g)
Definition types.h:397
#define ND_rank(n)
Definition types.h:523
#define ND_ht(n)
Definition types.h:500
#define ND_next(n)
Definition types.h:510
#define ND_other(n)
Definition types.h:514
#define ND_label(n)
Definition types.h:502
#define ND_alg(n)
Definition types.h:484
#define ND_flat_out(n)
Definition types.h:493
#define ND_rw(n)
Definition types.h:525
#define ND_node_type(n)
Definition types.h:511
#define ND_lw(n)
Definition types.h:506
#define ND_order(n)
Definition types.h:513
#define ND_flat_in(n)
Definition types.h:492
#define ND_coord(n)
Definition types.h:490
#define ND_in(n)
Definition types.h:501
#define ND_out(n)
Definition types.h:515
int rank(graph_t *g, int balance, int maxiter)
Definition ns.c:1001
void reset(sgraph *G)
Definition sgraph.c:29
graph or subgraph
Definition cgraph.h:425
double x
Definition geom.h:29
double y
Definition geom.h:29
int n
Definition types.h:201
#define MAX(a, b)
Definition write.c:31