Debian Codegraph @

lib/ortho/maze.c

raw
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
14 #define DEBUG
15
16 #include <assert.h>
17 #include <float.h>
18 #include <limits.h>
19 #include <math.h>
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include <ortho/maze.h>
23 #include <ortho/partition.h>
24 #include <ortho/trap.h>
25 #include <common/arith.h>
26 #include <util/alloc.h>
27 #include <util/list.h>
28
29 #define MARGIN 36
30
31 #ifdef DEBUG
32 char* pre = "%!PS-Adobe-2.0\n\
33 /node {\n\
34 /Y exch def\n\
35 /X exch def\n\
36 /y exch def\n\
37 /x exch def\n\
38 newpath\n\
39 x y moveto\n\
40 x Y lineto\n\
41 X Y lineto\n\
42 X y lineto\n\
43 closepath fill\n\
44 } def\n\
45 /cell {\n\
46 /Y exch def\n\
47 /X exch def\n\
48 /y exch def\n\
49 /x exch def\n\
50 newpath\n\
51 x y moveto\n\
52 x Y lineto\n\
53 X Y lineto\n\
54 X y lineto\n\
55 closepath stroke\n\
56 } def\n";
57
58 char* post = "showpage\n";
59
60 /// @brief dumps @ref maze::gcells and @ref maze::cells via rects to PostScript
61
62 static void psdump(cell *gcells, size_t n_gcells, boxf BB, boxf *rects,
63 size_t nrect) {
64 boxf bb;
65 boxf absbb = {.LL = {.y = 10.0, .x = 10.0}};
66
67 absbb.UR.x = absbb.LL.x + BB.UR.x - BB.LL.x;
68 absbb.UR.y = absbb.LL.y + BB.UR.y - BB.LL.y;
69 fputs (pre, stderr);
70 fprintf(stderr, "%%%%Page: 1 1\n%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
71 absbb.LL.x, absbb.LL.y, absbb.UR.x, absbb.UR.y);
72
73
74 fprintf (stderr, "%f %f translate\n", 10-BB.LL.x, 10-BB.LL.y);
75 fputs ("0 0 1 setrgbcolor\n", stderr);
76 for (size_t i = 0; i < n_gcells; i++) {
77 bb = gcells[i].bb;
78 fprintf (stderr, "%f %f %f %f node\n", bb.LL.x, bb.LL.y, bb.UR.x, bb.UR.y);
79 }
80 fputs ("0 0 0 setrgbcolor\n", stderr);
81 for (size_t i = 0; i < nrect; i++) {
82 bb = rects[i];
83 fprintf (stderr, "%f %f %f %f cell\n", bb.LL.x, bb.LL.y, bb.UR.x, bb.UR.y);
84 }
85 fputs ("1 0 0 setrgbcolor\n", stderr);
86 fprintf (stderr, "%f %f %f %f cell\n", BB.LL.x, BB.LL.y, BB.UR.x, BB.UR.y);
87 fputs (post, stderr);
88 }
89 #endif
90
91 /// compares points by X and then by Y
92
93 static int vcmpid(void *k1, void *k2) {
94 const pointf *key1 = k1;
95 const pointf *key2 = k2;
96 int dx = dfp_cmp(key1->x, key2->x);
97 if (dx != 0)
98 return dx;
99 return dfp_cmp(key1->y, key2->y);
100 }
101
102 /// compares points by Y and then by X
103
104 static int hcmpid(void *k1, void *k2) {
105 const pointf *key1 = k1;
106 const pointf *key2 = k2;
107 int dy = dfp_cmp(key1->y, key2->y);
108 if (dy != 0)
109 return dy;
110 return dfp_cmp(key1->x, key2->x);
111 }
112
113 typedef struct {
114 snode* np;
115 pointf p;
116 Dtlink_t link;
117 } snodeitem;
118
119 static Dtdisc_t vdictDisc = {
120 offsetof(snodeitem,p),
121 sizeof(pointf),
122 offsetof(snodeitem,link),
123 0,
124 0,
125 vcmpid,
126 };
127 static Dtdisc_t hdictDisc = {
128 offsetof(snodeitem,p),
129 sizeof(pointf),
130 offsetof(snodeitem,link),
131 0,
132 0,
133 hcmpid,
134 };
135
136 #define delta 1 /* weight of length */
137 #define mu 500 /* weight of bends */
138
139 #define BEND(g,e) ((g->nodes + e->v1)->isVert != (g->nodes + e->v2)->isVert)
140 #define HORZ(g,e) ((g->nodes + e->v1)->isVert)
141 #define BIG 16384
142 #define CHANSZ(w) (((w)-3)/2)
143 #define IS_SMALL(v) (CHANSZ(v) < 2)
144
145 /**
146 * @brief updates single @ref sedge::weight
147 *
148 * At present, we use a step function. When a bound is reached, the weight
149 * becomes huge. We might consider bumping up the weight more gradually, the
150 * thinner the channel, the faster the weight rises.
151 */
152
153 static void updateWt(sedge *ep, double sz) {
154 ep->cnt++;
155 if (ep->cnt > sz) {
156 ep->cnt = 0;
157 ep->weight += BIG;
158 }
159 }
160
161 /**
162 * @brief updates @ref sedge::weight of cell edges
163 *
164 * Iterate over edges in a cell, adjust weights as necessary.
165 * It always updates the bent edges belonging to a cell.
166 * A horizontal/vertical edge is updated only if the edge traversed
167 * is bent, or if it is the traversed edge.
168 */
169
170 void
171 updateWts (sgraph* g, cell* cp, sedge* ep)
172 {
173 int i;
174 sedge* e;
175 int isBend = BEND(g,ep);
176 const double hsz = CHANSZ(cp->bb.UR.y - cp->bb.LL.y);
177 const double vsz = CHANSZ(cp->bb.UR.x - cp->bb.LL.x);
178 const double minsz = fmin(hsz, vsz);
179
180 /* Bend edges are added first */
181 for (i = 0; i < cp->nedges; i++) {
182 e = cp->edges[i];
183 if (!BEND(g,e)) break;
184 updateWt (e, minsz);
185 }
186
187 for (; i < cp->nedges; i++) {
188 e = cp->edges[i];
189 if (isBend || e == ep) updateWt (e, HORZ(g,e)?hsz:vsz);
190 }
191 }
192
193 /**
194 * cp corresponds to a real node. If it is small, its associated cells should
195 * be marked as usable.
196 */
197
198 static void
199 markSmall (cell* cp)
200 {
201 snode* onp;
202 cell* ocp;
203
204 if (IS_SMALL(cp->bb.UR.y-cp->bb.LL.y)) {
205 for (size_t i = 0; i < cp->nsides; i++) {
206 onp = cp->sides[i];
207 if (!onp->isVert) continue;
208 if (onp->cells[0] == cp) { /* onp on the right of cp */
209 ocp = onp->cells[1];
210 ocp->flags |= MZ_SMALLV;
211 while ((onp = ocp->sides[M_RIGHT]) && !IsNode(onp->cells[1])) {
212 ocp = onp->cells[1];
213 ocp->flags |= MZ_SMALLV;
214 }
215 }
216 else { /* onp on the left of cp */
217 ocp = onp->cells[0];
218 ocp->flags |= MZ_SMALLV;
219 while ((onp = ocp->sides[M_LEFT]) && !IsNode(onp->cells[0])) {
220 ocp = onp->cells[0];
221 ocp->flags |= MZ_SMALLV;
222 }
223 }
224 }
225 }
226
227 if (IS_SMALL(cp->bb.UR.x-cp->bb.LL.x)) {
228 for (size_t i = 0; i < cp->nsides; i++) {
229 onp = cp->sides[i];
230 if (onp->isVert) continue;
231 if (onp->cells[0] == cp) { /* onp on the top of cp */
232 ocp = onp->cells[1];
233 ocp->flags |= MZ_SMALLH;
234 while ((onp = ocp->sides[M_TOP]) && !IsNode(onp->cells[1])) {
235 ocp = onp->cells[1];
236 ocp->flags |= MZ_SMALLH;
237 }
238 }
239 else { /* onp on the bottom of cp */
240 ocp = onp->cells[0];
241 ocp->flags |= MZ_SMALLH;
242 while ((onp = ocp->sides[M_BOTTOM]) && !IsNode(onp->cells[0])) {
243 ocp = onp->cells[0];
244 ocp->flags |= MZ_SMALLH;
245 }
246 }
247 }
248 }
249 }
250
251 /// fills @ref cell::sides and @ref sgraph::edges
252
253 static void
254 createSEdges (cell* cp, sgraph* g)
255 {
256 boxf bb = cp->bb;
257 double hwt = delta*(bb.UR.x-bb.LL.x);
258 double vwt = delta*(bb.UR.y-bb.LL.y);
259 double wt = (hwt + vwt)/2.0 + mu;
260
261 /* We automatically make small channels have high cost to guide routes to
262 * more spacious channels.
263 */
264 if (IS_SMALL(bb.UR.y-bb.LL.y) && !IsSmallV(cp)) {
265 hwt = BIG;
266 wt = BIG;
267 }
268 if (IS_SMALL(bb.UR.x-bb.LL.x) && !IsSmallH(cp)) {
269 vwt = BIG;
270 wt = BIG;
271 }
272
273 if (cp->sides[M_LEFT] && cp->sides[M_TOP])
274 cp->edges[cp->nedges++] = createSEdge (g, cp->sides[M_LEFT], cp->sides[M_TOP], wt);
275 if (cp->sides[M_TOP] && cp->sides[M_RIGHT])
276 cp->edges[cp->nedges++] = createSEdge (g, cp->sides[M_TOP], cp->sides[M_RIGHT], wt);
277 if (cp->sides[M_LEFT] && cp->sides[M_BOTTOM])
278 cp->edges[cp->nedges++] = createSEdge (g, cp->sides[M_LEFT], cp->sides[M_BOTTOM], wt);
279 if (cp->sides[M_BOTTOM] && cp->sides[M_RIGHT])
280 cp->edges[cp->nedges++] = createSEdge (g, cp->sides[M_BOTTOM], cp->sides[M_RIGHT], wt);
281 if (cp->sides[M_TOP] && cp->sides[M_BOTTOM])
282 cp->edges[cp->nedges++] = createSEdge (g, cp->sides[M_TOP], cp->sides[M_BOTTOM], vwt);
283 if (cp->sides[M_LEFT] && cp->sides[M_RIGHT])
284 cp->edges[cp->nedges++] = createSEdge (g, cp->sides[M_LEFT], cp->sides[M_RIGHT], hwt);
285 }
286
287 /// finds a @ref snode by point or creates it
288
289 static snode*
290 findSVert (sgraph* g, Dt_t* cdt, pointf p, snodeitem* ditems, bool isVert)
291 {
292 snodeitem* n = dtmatch (cdt, &p);
293
294 if (!n) {
295 snode* np = createSNode (g);
296 assert(ditems);
297 n = ditems + np->index;
298 n->p = p;
299 n->np = np;
300 np->isVert = isVert;
301 dtinsert (cdt, n);
302 }
303
304 return n->np;
305 }
306
307 static void
308 chkSgraph (sgraph* g)
309 {
310 int i;
311 snode* np;
312
313 for (i = 0; i < g->nnodes; i++) {
314 np = g->nodes+i;
315 if (!np->cells[0]) fprintf (stderr, "failed at node %d[0]\n", i);
316 assert (np->cells[0]);
317 if (!np->cells[1]) fprintf (stderr, "failed at node %d[1]\n", i);
318 assert (np->cells[1]);
319 }
320
321 }
322
323 /**
324 * @brief creates and fills @ref sgraph for @ref maze
325 *
326 * Subroutines: @ref createSGraph, @ref findSVert with @ref createSNode,
327 * @ref initSEdges and @ref chkSgraph
328 */
329
330 static sgraph*
331 mkMazeGraph (maze* mp, boxf bb)
332 {
333 const size_t bound = 4 * mp->ncells;
334 sgraph* g = createSGraph (bound + 2);
335 Dt_t* vdict = dtopen(&vdictDisc,Dtoset);
336 Dt_t* hdict = dtopen(&hdictDisc,Dtoset);
337 snodeitem* ditems = gv_calloc(bound, sizeof(snodeitem));
338 snode** sides;
339
340 /* For each cell, create if necessary and attach a node in search
341 * corresponding to each internal face. The node also gets
342 * a pointer to the cell.
343 */
344 sides = gv_calloc(4 * mp->ncells, sizeof(snode*));
345 for (size_t i = 0; i < mp->ncells; i++) {
346 cell* cp = mp->cells+i;
347 snode* np;
348 pointf pt;
349
350 cp->nsides = 4;
351 cp->sides = sides + 4*i;
352 if (cp->bb.UR.x < bb.UR.x) {
353 pt.x = cp->bb.UR.x;
354 pt.y = cp->bb.LL.y;
355 np = findSVert(g, vdict, pt, ditems, true);
356 np->cells[0] = cp;
357 cp->sides[M_RIGHT] = np;
358 }
359 if (cp->bb.UR.y < bb.UR.y) {
360 pt.x = cp->bb.LL.x;
361 pt.y = cp->bb.UR.y;
362 np = findSVert(g, hdict, pt, ditems, false);
363 np->cells[0] = cp;
364 cp->sides[M_TOP] = np;
365 }
366 if (cp->bb.LL.x > bb.LL.x) {
367 np = findSVert(g, vdict, cp->bb.LL, ditems, true);
368 np->cells[1] = cp;
369 cp->sides[M_LEFT] = np;
370 }
371 if (cp->bb.LL.y > bb.LL.y) {
372 np = findSVert(g, hdict, cp->bb.LL, ditems, false);
373 np->cells[1] = cp;
374 cp->sides[M_BOTTOM] = np;
375 }
376 }
377
378 /* For each gcell, corresponding to a node in the input graph,
379 * connect it to its corresponding search nodes.
380 */
381 size_t maxdeg = 0;
382 for (size_t i = 0; i < mp->ngcells; i++) {
383 cell *cp = &mp->gcells[i];
384 pointf pt;
385 snodeitem* np;
386
387 LIST(snode *) cp_sides = {0};
388 pt = cp->bb.LL;
389 np = dtmatch (hdict, &pt);
390 for (; np && np->p.x < cp->bb.UR.x; np = dtnext (hdict, np)) {
391 LIST_APPEND(&cp_sides, np->np);
392 np->np->cells[1] = cp;
393 }
394 np = dtmatch (vdict, &pt);
395 for (; np && np->p.y < cp->bb.UR.y; np = dtnext (vdict, np)) {
396 LIST_APPEND(&cp_sides, np->np);
397 np->np->cells[1] = cp;
398 }
399 pt.y = cp->bb.UR.y;
400 np = dtmatch (hdict, &pt);
401 for (; np && np->p.x < cp->bb.UR.x; np = dtnext (hdict, np)) {
402 LIST_APPEND(&cp_sides, np->np);
403 np->np->cells[0] = cp;
404 }
405 pt.x = cp->bb.UR.x;
406 pt.y = cp->bb.LL.y;
407 np = dtmatch (vdict, &pt);
408 for (; np && np->p.y < cp->bb.UR.y; np = dtnext (vdict, np)) {
409 LIST_APPEND(&cp_sides, np->np);
410 np->np->cells[0] = cp;
411 }
412 LIST_DETACH(&cp_sides, &cp->sides, &cp->nsides);
413 if (cp->nsides > maxdeg) maxdeg = cp->nsides;
414 }
415
416 /* Mark cells that are small because of a small node, not because of the close
417 * alignment of two rectangles.
418 */
419 for (size_t i = 0; i < mp->ngcells; i++) {
420 cell *cp = &mp->gcells[i];
421 markSmall (cp);
422 }
423
424 /* Set index of two dummy nodes used for real nodes */
425 g->nodes[g->nnodes].index = g->nnodes;
426 g->nodes[g->nnodes+1].index = g->nnodes+1;
427
428 /* create edges
429 * For each ordinary cell, there can be at most 6 edges.
430 * At most 2 gcells will be used at a time, and each of these
431 * can have at most degree maxdeg.
432 */
433 initSEdges (g, maxdeg);
434 for (size_t i = 0; i < mp->ncells; i++) {
435 cell* cp = mp->cells+i;
436 createSEdges (cp, g);
437 }
438
439 /* tidy up memory */
440 dtclose (vdict);
441 dtclose (hdict);
442 free (ditems);
443
444 chkSgraph (g);
445 /* save core graph state */
446 gsave(g);
447 return g;
448 }
449
450 /// creates @ref maze and fills @ref maze::gcells and @ref maze::cells. A subroutine of @ref orthoEdges.
451
452 maze *mkMaze(graph_t *g) {
453 node_t* n;
454 maze* mp = gv_alloc(sizeof(maze));
455 boxf* rects;
456 cell* cp;
457 double w2, h2;
458 boxf bb;
459
460 assert(agnnodes(g) >= 0);
461 mp->ngcells = (size_t)agnnodes(g);
462 cp = mp->gcells = gv_calloc(mp->ngcells, sizeof(cell));
463
464 boxf BB = {.LL = {.x = DBL_MAX, .y = DBL_MAX},
465 .UR = {.x = -DBL_MAX, .y = -DBL_MAX}};
466 for (n = agfstnode (g); n; n = agnxtnode(g,n)) {
467 w2 = fmax(1, ND_xsize(n) / 2.0);
468 h2 = fmax(1, ND_ysize(n) / 2.0);
469 bb.LL.x = ND_coord(n).x - w2;
470 bb.UR.x = ND_coord(n).x + w2;
471 bb.LL.y = ND_coord(n).y - h2;
472 bb.UR.y = ND_coord(n).y + h2;
473 BB.LL.x = fmin(BB.LL.x, bb.LL.x);
474 BB.LL.y = fmin(BB.LL.y, bb.LL.y);
475 BB.UR.x = fmax(BB.UR.x, bb.UR.x);
476 BB.UR.y = fmax(BB.UR.y, bb.UR.y);
477 cp->bb = bb;
478 cp->flags |= MZ_ISNODE;
479 ND_alg(n) = cp;
480 cp++;
481 }
482
483 BB.LL.x -= MARGIN;
484 BB.LL.y -= MARGIN;
485 BB.UR.x += MARGIN;
486 BB.UR.y += MARGIN;
487 size_t nrect;
488 rects = partition(mp->gcells, mp->ngcells, &nrect, BB);
489
490 #ifdef DEBUG
491 if (odb_flags & ODB_MAZE) psdump (mp->gcells, mp->ngcells, BB, rects, nrect);
492 #endif
493 mp->cells = gv_calloc(nrect, sizeof(cell));
494 mp->ncells = nrect;
495 for (size_t i = 0; i < nrect; i++) {
496 mp->cells[i].bb = rects[i];
497 }
498 free (rects);
499
500 mp->sg = mkMazeGraph (mp, BB);
501 return mp;
502 }
503
504 void freeMaze (maze* mp)
505 {
506 free (mp->cells[0].sides);
507 free (mp->cells);
508 for (size_t i = 0; i < mp->ngcells; ++i) {
509 free(mp->gcells[i].sides);
510 }
511 free (mp->gcells);
512 freeSGraph (mp->sg);
513 dtclose (mp->hchans);
514 dtclose (mp->vchans);
515 free (mp);
516 }
517