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