Graphviz 16.1.3~dev.20261128.0221
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 v2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "config.h"
12
13#include <assert.h>
14#include <dotgen/dot.h>
15#include <stdbool.h>
16#include <stddef.h>
17#include <util/alloc.h>
18#include <util/gv_math.h>
19
20static node_t *make_vn_slot(graph_t * g, int r, int pos)
21{
22 int i;
23 node_t *n;
24
25 assert(GD_rank(g)[r].av == GD_rank(g)[r].v);
26 GD_rank(g)[r].av = gv_recalloc(GD_rank(g)[r].av, GD_rank(g)[r].an,
27 GD_rank(g)[r].an + 1, sizeof(node_t *));
28 ++GD_rank(g)[r].an;
29 GD_rank(g)[r].v = GD_rank(g)[r].av;
30 node_t **const v = GD_rank(g)[r].v;
31 for (i = GD_rank(g)[r].n; i > pos; i--) {
32 v[i] = v[i - 1];
33 ND_order(v[i])++;
34 }
35 n = v[pos] = virtual_node(g);
36 ND_order(n) = pos;
37 ND_rank(n) = r;
38 v[++GD_rank(g)[r].n] = NULL;
39 return v[pos];
40}
41
42#define HLB 0 /* hard left bound */
43#define HRB 1 /* hard right bound */
44#define SLB 2 /* soft left bound */
45#define SRB 3 /* soft right bound */
46
47static void findlr(node_t * u, node_t * v, int *lp, int *rp)
48{
49 int l, r;
50 l = ND_order(u);
51 r = ND_order(v);
52 if (l > r) {
53 SWAP(&l, &r);
54 }
55 *lp = l;
56 *rp = r;
57}
58
59static void setbounds(node_t * v, int *bounds, int lpos, int rpos)
60{
61 int i, l, r, ord;
62 edge_t *f;
63
64 if (ND_node_type(v) == VIRTUAL) {
65 ord = ND_order(v);
66 if (ND_in(v).size == 0) { /* flat */
67 assert(ND_out(v).size == 2);
68 findlr(aghead(ND_out(v).list[0]), aghead(ND_out(v).list[1]), &l,
69 &r);
70 /* the other flat edge could be to the left or right */
71 if (r <= lpos)
72 bounds[SLB] = bounds[HLB] = ord;
73 else if (l >= rpos)
74 bounds[SRB] = bounds[HRB] = ord;
75 /* could be spanning this one */
76 else if (l < lpos && r > rpos); // ignore
77 /* must have intersecting ranges */
78 else {
79 if (l < lpos || (l == lpos && r < rpos))
80 bounds[SLB] = ord;
81 if (r > rpos || (r == rpos && l > lpos))
82 bounds[SRB] = ord;
83 }
84 } else { /* forward */
85 bool onleft, onright;
86 onleft = onright = false;
87 for (i = 0; (f = ND_out(v).list[i]); i++) {
88 if (ND_order(aghead(f)) <= lpos) {
89 onleft = true;
90 continue;
91 }
92 if (ND_order(aghead(f)) >= rpos) {
93 onright = true;
94 continue;
95 }
96 }
97 if (onleft && !onright)
98 bounds[HLB] = ord + 1;
99 if (onright && !onleft)
100 bounds[HRB] = ord - 1;
101 }
102 }
103}
104
105static int flat_limits(graph_t * g, edge_t * e)
106{
107 int lnode, rnode, bounds[4], lpos, rpos, pos;
108
109 const int r = ND_rank(agtail(e)) - 1;
110 node_t **const rank = GD_rank(g)[r].v;
111 lnode = 0;
112 rnode = GD_rank(g)[r].n - 1;
113 bounds[HLB] = bounds[SLB] = lnode - 1;
114 bounds[HRB] = bounds[SRB] = rnode + 1;
115 findlr(agtail(e), aghead(e), &lpos, &rpos);
116 while (lnode <= rnode) {
117 setbounds(rank[lnode], bounds, lpos, rpos);
118 if (lnode != rnode)
119 setbounds(rank[rnode], bounds, lpos, rpos);
120 lnode++;
121 rnode--;
122 if (bounds[HRB] - bounds[HLB] <= 1)
123 break;
124 }
125 if (bounds[HLB] <= bounds[HRB])
126 pos = (bounds[HLB] + bounds[HRB] + 1) / 2;
127 else
128 pos = (bounds[SLB] + bounds[SRB] + 1) / 2;
129 return pos;
130}
131
132/* Create virtual node representing edge label between
133 * actual ends of edge e.
134 * This node is characterized by being virtual and having a non-NULL
135 * ND_alg pointing to e.
136 */
137static void
139{
140 double ypos, h2;
141 graph_t *g;
142 node_t *n, *vn;
143 edge_t *ve;
144 pointf dimen;
145
146 if (ED_label(e) == NULL)
147 return;
148 g = dot_root(agtail(e));
149 const int r = ND_rank(agtail(e));
150
151 const int place = flat_limits(g, e);
152 /* grab ypos = LL.y of label box before make_vn_slot() */
153 if ((n = GD_rank(g)[r - 1].v[0]))
154 ypos = ND_coord(n).y - GD_rank(g)[r - 1].ht1;
155 else {
156 n = GD_rank(g)[r].v[0];
157 ypos = ND_coord(n).y + GD_rank(g)[r].ht2 + GD_ranksep(g);
158 }
159 vn = make_vn_slot(g, r - 1, place);
160 dimen = ED_label(e)->dimen;
161 if (GD_flip(g)) {
162 SWAP(&dimen.x, &dimen.y);
163 }
164 ND_ht(vn) = dimen.y;
165 h2 = ND_ht(vn) / 2;
166 ND_lw(vn) = ND_rw(vn) = dimen.x / 2;
167 ND_label(vn) = ED_label(e);
168 ND_coord(vn).y = ypos + h2;
169 ve = virtual_edge(vn, agtail(e), e); /* was NULL? */
170 ED_tail_port(ve).p.x = -ND_lw(vn);
171 ED_head_port(ve).p.x = ND_rw(agtail(e));
173 ve = virtual_edge(vn, aghead(e), e);
174 ED_tail_port(ve).p.x = ND_rw(vn);
175 ED_head_port(ve).p.x = ND_lw(aghead(e));
177 /* another assumed symmetry of ht1/ht2 of a label node */
178 if (GD_rank(g)[r - 1].ht1 < h2)
179 GD_rank(g)[r - 1].ht1 = h2;
180 if (GD_rank(g)[r - 1].ht2 < h2)
181 GD_rank(g)[r - 1].ht2 = h2;
182 ND_alg(vn) = e;
183}
184
185static void abomination(graph_t * g)
186{
187 int r;
188
189 assert(GD_minrank(g) == 0);
190 /* 3 = one for new rank, one for sentinel, one for off-by-one */
191 r = GD_maxrank(g) + 3;
192 rank_t *rptr = gv_recalloc(GD_rank(g), GD_maxrank(g) + 1, r,
193 sizeof(rank_t));
194 GD_rank(g) = rptr + 1;
195 for (r = GD_maxrank(g); r >= 0; r--)
196 GD_rank(g)[r] = GD_rank(g)[r - 1];
197 GD_rank(g)[r].n = 0;
198 GD_rank(g)[r].v = GD_rank(g)[r].av = gv_calloc(2, sizeof(node_t *));
199 GD_rank(g)[r].an = 2;
200 GD_rank(g)[r].flat = NULL;
201 GD_rank(g)[r].ht1 = GD_rank(g)[r].ht2 = 1;
202 GD_rank(g)[r].pht1 = GD_rank(g)[r].pht2 = 1;
203 GD_minrank(g)--;
204}
205
206/* Check if tn and hn are adjacent.
207 * If so, set adjacent bit on all related edges.
208 * Assume e is flat.
209 */
210static void
212{
213 node_t* tn = agtail(e);
214 node_t* hn = aghead(e);
215 int i, lo, hi;
216 node_t* n;
217 rank_t *rank;
218
219 if (ND_order(tn) < ND_order(hn)) {
220 lo = ND_order(tn);
221 hi = ND_order(hn);
222 }
223 else {
224 lo = ND_order(hn);
225 hi = ND_order(tn);
226 }
227 rank = &GD_rank(dot_root(tn))[ND_rank(tn)];
228 for (i = lo + 1; i < hi; i++) {
229 n = rank->v[i];
230 if ((ND_node_type(n) == VIRTUAL && ND_label(n)) ||
231 ND_node_type(n) == NORMAL)
232 break;
233 }
234 if (i == hi) { /* adjacent edge */
235 do {
236 ED_adjacent(e) = 1;
237 e = ED_to_virt(e);
238 } while (e);
239 }
240}
241
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 edge_t *e;
262
263 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
264 if (ND_flat_out(n).list) {
265 for (size_t j = 0; (e = ND_flat_out(n).list[j]); j++) {
267 }
268 }
269 for (size_t j = 0; j < ND_other(n).size; j++) {
270 e = ND_other(n).list[j];
271 if (ND_rank(aghead(e)) == ND_rank(agtail(e)))
273 }
274 }
275
276 if (GD_rank(g)[0].flat || GD_n_cluster(g) > 0) {
277 bool found = false;
278 node_t *n;
279 for (size_t i = 0; !found && (n = GD_rank(g)[0].v[i]); i++) {
280 for (size_t j = 0; !found && (e = ND_flat_in(n).list[j]); j++) {
281 if (ED_label(e) && !ED_adjacent(e)) {
282 abomination(g);
283 found = true;
284 }
285 }
286 for (size_t j = 0; !found && j < ND_other(n).size; j++) {
287 e = ND_other(n).list[j];
288 if (ED_label(e) && !ED_adjacent(e) && aghead(e) != agtail(e)) {
289 abomination(g);
290 found = true;
291 }
292 }
293 }
294 }
295
297 bool reset = false;
298 for (node_t *n = GD_nlist(g); n; n = ND_next(n)) {
299 /* if n is the tail of any flat edge, one will be in flat_out */
300 if (ND_flat_out(n).list) {
301 for (size_t i = 0; (e = ND_flat_out(n).list[i]); i++) {
302 if (ED_label(e)) {
303 if (ED_adjacent(e)) {
304 ED_dist(e) = GD_flip(g) ? ED_label(e)->dimen.y : ED_label(e)->dimen.x;
305 }
306 else {
307 reset = true;
308 flat_node(e);
309 }
310 }
311 }
312 /* look for other flat edges with labels */
313 for (size_t j = 0; j < ND_other(n).size; j++) {
314 e = ND_other(n).list[j];
315 if (ND_rank(agtail(e)) != ND_rank(aghead(e))) continue;
316 if (agtail(e) == aghead(e)) continue; /* skip loops */
317 edge_t *le = e;
318 while (ED_to_virt(le)) le = ED_to_virt(le);
320 if (ED_label(e)) {
321 if (ED_adjacent(e)) {
322 const double lw = GD_flip(g) ? ED_label(e)->dimen.y : 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 MAX(a, b)
Definition arith.h:33
#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:483
Agedge_t * virtual_edge(Agnode_t *, Agnode_t *, Agedge_t *)
Definition fastgr.c:156
void checkLabelOrder(graph_t *g)
Definition mincross.c:295
void rec_reset_vlists(Agraph_t *)
Definition mincross.c:919
void rec_save_vlists(Agraph_t *)
Definition mincross.c:911
Agnode_t * virtual_node(Agraph_t *)
Definition fastgr.c:183
#define le
Definition edges.h:29
static void flat_node(edge_t *e)
Definition flat.c:138
static int flat_limits(graph_t *g, edge_t *e)
Definition flat.c:105
#define HLB
Definition flat.c:42
int flat_edges(graph_t *g)
Definition flat.c:259
static void checkFlatAdjacent(edge_t *e)
Definition flat.c:211
static void abomination(graph_t *g)
Definition flat.c:185
#define HRB
Definition flat.c:43
static node_t * make_vn_slot(graph_t *g, int r, int pos)
Definition flat.c:20
#define SRB
Definition flat.c:45
#define SLB
Definition flat.c:44
static void findlr(node_t *u, node_t *v, int *lp, int *rp)
Definition flat.c:47
static void setbounds(node_t *v, int *bounds, int lpos, int rpos)
Definition flat.c:59
node NULL
Definition grammar.y:181
#define ED_dist(e)
Definition types.h:602
#define agtail(e)
Definition cgraph.h:982
#define ED_edge_type(e)
Definition types.h:582
#define ED_adjacent(e)
Definition types.h:584
#define aghead(e)
Definition cgraph.h:983
#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:133
int rank(graph_t *g, int balance, int maxiter)
Definition ns.c:1104
void reset(sgraph *G)
Definition sgraph.c:30
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