Debian Codegraph @

lib/fdpgen/dbg.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 #include "config.h"
12
13 /* dbg.c:
14 * Written by Emden R. Gansner
15 *
16 * Simple debugging infrastructure
17 */
18 #ifdef DEBUG
19
20 #define FDP_PRIVATE
21
22 #include <fdpgen/dbg.h>
23 #include <neatogen/neatoprocs.h>
24 #include <fdpgen/fdp.h>
25 #include <math.h>
26 #include <util/exit.h>
27
28 static int indent = -1;
29
30 void incInd()
31 {
32 indent++;
33 }
34
35 void decInd()
36 {
37 if (indent >= 0)
38 indent--;
39 }
40
41 void prIndent(void)
42 {
43 int i;
44 for (i = 0; i < indent; i++)
45 fputs(" ", stderr);
46 }
47
48 void prEdge(edge_t *e,char *s)
49 {
50 fprintf(stderr,"%s --", agnameof(agtail(e)));
51 fprintf(stderr,"%s%s", agnameof(aghead(e)),s);
52 }
53
54 static void dumpBB(graph_t * g)
55 {
56 boxf bb;
57 boxf b;
58
59 bb = BB(g);
60 b = GD_bb(g);
61 prIndent();
62 fprintf(stderr, " LL (%f,%f) UR (%f,%f)\n", bb.LL.x, bb.LL.y,
63 bb.UR.x, bb.UR.y);
64 prIndent();
65 fprintf(stderr, " LL (%f,%f) UR (%f,%f)\n", b.LL.x, b.LL.y,
66 b.UR.x, b.UR.y);
67 }
68
69 static void dumpSG(graph_t * g)
70 {
71 graph_t *subg;
72 int i;
73
74 if (GD_n_cluster(g) == 0)
75 return;
76 prIndent();
77 fprintf(stderr, " {\n");
78 for (i = 1; i <= GD_n_cluster(g); i++) {
79 subg = (GD_clust(g))[i];
80 prIndent();
81 fprintf(stderr, " subgraph %s : %d nodes\n", agnameof(subg),
82 agnnodes(subg));
83 dumpBB(subg);
84 incInd ();
85 dumpSG(subg);
86 decInd ();
87 }
88 prIndent();
89 fprintf(stderr, " }\n");
90 }
91
92 /* dump:
93 */
94 void dump(graph_t *g, int level) {
95 node_t *n;
96 double w, h;
97 pointf pos;
98
99 if (Verbose < level)
100 return;
101 prIndent();
102 fprintf(stderr, "Graph %s : %d nodes\n", agnameof(g), agnnodes(g));
103 dumpBB(g);
104 if (Verbose > level) {
105 incInd();
106 dumpSG(g);
107 decInd();
108 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
109 pos.x = ND_pos(n)[0];
110 pos.y = ND_pos(n)[1];
111 prIndent();
112 w = ND_width(n);
113 h = ND_height(n);
114 fprintf(stderr, "%s: (%f,%f) (%f,%f) \n",
115 agnameof(n), pos.x, pos.y, w, h);
116 }
117 }
118 }
119
120 void dumpG(graph_t * g, char *fname, int expMode)
121 {
122 FILE *fp;
123
124 fp = fopen(fname, "w");
125 if (!fp) {
126 fprintf(stderr, "Couldn not open %s \n", fname);
127 graphviz_exit(1);
128 }
129 outputGraph(g, fp, expMode);
130 fclose(fp);
131 }
132
133 static const double ArrowScale = 1.0;
134
135 #define ARROW_LENGTH 10
136 #define ARROW_WIDTH 5
137
138 static char *plog = "%!PS-Adobe-2.0\n\n\
139 /Times-Roman findfont 14 scalefont setfont\n\
140 /lLabel {\n\
141 \tmoveto\n\
142 \tgsave\n\
143 \tshow\n\
144 \tgrestore\n\
145 } def\n\
146 /inch {\n\
147 \t72 mul\n\
148 } def\n\
149 /doBox {\n\
150 \tnewpath\n\
151 \tmoveto\n\
152 \t/ht exch def\n\
153 \t/wd exch def\n\
154 \t0 ht rlineto\n\
155 \twd 0 rlineto\n\
156 \t0 0 ht sub rlineto\n\
157 \tclosepath\n\
158 \tgsave\n\
159 \t\t.9 setgray\n\
160 \t\tfill\n\
161 \tgrestore\n\
162 \tstroke\n\
163 } def\n\
164 /drawCircle {\n\
165 \t/r exch def\n\
166 \t/y exch def\n\
167 \t/x exch def\n\
168 \tnewpath\n\
169 \tx y r 0 360 arc\n\
170 \tstroke\n\
171 } def\n\
172 /fillCircle {\n\
173 \t/r exch def\n\
174 \t/y exch def\n\
175 \t/x exch def\n\
176 \tnewpath\n\
177 \tx y r 0 360 arc\n\
178 \tfill\n\
179 } def\n";
180
181 static char *elog = "showpage\n";
182
183 static double PSWidth = 550.0;
184 static double PSHeight = 756.0;
185
186 static void pswrite(Agraph_t * g, FILE * fp, int expMode)
187 {
188 Agnode_t *n;
189 Agnode_t *h;
190 Agedge_t *e;
191 double minx, miny, maxx, maxy;
192 double scale, width, height;
193 int do_arrow;
194 int angle;
195 char *p;
196 double theta;
197 double arrow_w, arrow_l;
198 int portColor = 0;
199
200 fprintf(fp, "%s", plog);
201
202 do_arrow = 0;
203
204 n = agfstnode(g);
205 minx = ND_pos(n)[0];
206 miny = ND_pos(n)[1];
207 maxx = ND_pos(n)[0];
208 maxy = ND_pos(n)[1];
209 n = agnxtnode(g, n);
210 for (; n; n = agnxtnode(g, n)) {
211 minx = fmin(minx, ND_pos(n)[0]);
212 miny = fmin(miny, ND_pos(n)[1]);
213 maxx = fmax(maxx, ND_pos(n)[0]);
214 maxy = fmax(maxy, ND_pos(n)[1]);
215 }
216
217 /* Convert to points
218 */
219 minx *= POINTS_PER_INCH;
220 miny *= POINTS_PER_INCH;
221 maxx *= POINTS_PER_INCH;
222 maxy *= POINTS_PER_INCH;
223
224 /* Check for rotation
225 */
226 if ((p = agget(g, "rotate")) && *p != '\0' && (angle = atoi(p)) != 0) {
227 fprintf(fp, "306 396 translate\n");
228 fprintf(fp, "%d rotate\n", angle);
229 fprintf(fp, "-306 -396 translate\n");
230 }
231
232 /* If user gives scale factor, use it.
233 * Else if figure too large for standard PS page, scale it to fit.
234 */
235 width = maxx - minx + 20;
236 height = maxy - miny + 20;
237 if (width > PSWidth) {
238 if (height > PSHeight) {
239 scale = fmin(PSWidth / width, PSHeight / height);
240 } else
241 scale = PSWidth / width;
242 } else if (height > PSHeight) {
243 scale = PSHeight / height;
244 } else
245 scale = 1.0;
246
247 fprintf(fp, "%f %f translate\n",
248 (PSWidth - scale * (minx + maxx)) / 2.0,
249 (PSHeight - scale * (miny + maxy)) / 2.0);
250 fprintf(fp, "%f %f scale\n", scale, scale);
251
252 /*
253 if (Verbose)
254 fprintf (stderr, "Region (%f,%f) (%f,%f), scale %f\n",
255 minx, miny, maxx, maxy, scale);
256 */
257
258 if (do_arrow) {
259 arrow_w = ArrowScale * ARROW_WIDTH / scale;
260 arrow_l = ArrowScale * ARROW_LENGTH / scale;
261 }
262
263 fprintf(fp, "0.0 setlinewidth\n");
264 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
265 if (IS_PORT(n)) {
266 double r;
267 r = sqrt(ND_pos(n)[0] * ND_pos(n)[0] +
268 ND_pos(n)[1] * ND_pos(n)[1]);
269 fprintf(fp, "0 0 %f inch drawCircle\n", r);
270 break;
271 }
272 }
273
274 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
275 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
276 h = aghead(e);
277 fprintf(fp, "%f inch %f inch moveto %f inch %f inch lineto\n",
278 ND_pos(n)[0], ND_pos(n)[1], ND_pos(h)[0],
279 ND_pos(h)[1]);
280 fprintf(fp, "stroke\n");
281 if (do_arrow) {
282 theta =
283 atan2(ND_pos(n)[1] - ND_pos(h)[1],
284 ND_pos(n)[0] - ND_pos(h)[0]);
285 fprintf(fp, "%f %f %.2f %.2f %.2f doArrow\n",
286 ND_pos(h)[0], ND_pos(h)[1], DEGREES(theta),
287 arrow_l, arrow_w);
288 }
289
290 }
291 }
292
293 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
294 fprintf(fp, "%% %s\n", agnameof(n));
295 if (expMode) {
296 double wd, ht;
297 double r;
298 wd = ND_width(n);
299 ht = ND_height(n);
300 r = sqrt(wd * wd / 4 + ht * ht / 4);
301 fprintf(fp, "%f inch %f inch %f inch %f inch doBox\n", wd, ht,
302 ND_pos(n)[0] - wd / 2, ND_pos(n)[1] - ht / 2);
303 fprintf(fp, "%f inch %f inch %f inch drawCircle\n",
304 ND_pos(n)[0], ND_pos(n)[1], r);
305 } else {
306 if (IS_PORT(n)) {
307 if (!portColor) {
308 fprintf(fp, "0.667 1.000 1.000 sethsbcolor\n");
309 portColor = 1;
310 }
311 } else {
312 if (portColor) {
313 fprintf(fp, "0.0 0.000 0.000 sethsbcolor\n");
314 portColor = 0;
315 }
316 }
317 }
318 fprintf(fp, "%f inch %f inch %f fillCircle\n", ND_pos(n)[0],
319 ND_pos(n)[1], 3 / scale);
320 }
321
322 fprintf(fp, "0.667 1.000 1.000 sethsbcolor\n");
323 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
324 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
325 h = aghead(e);
326 fprintf(fp, "%f inch %f inch moveto %f inch %f inch lineto\n",
327 ND_pos(n)[0], ND_pos(n)[1], ND_pos(h)[0],
328 ND_pos(h)[1]);
329 fprintf(fp, "stroke\n");
330 if (do_arrow) {
331 theta =
332 atan2(ND_pos(n)[1] - ND_pos(h)[1],
333 ND_pos(n)[0] - ND_pos(h)[0]);
334 fprintf(fp, "%f %f %.2f %.2f %.2f doArrow\n",
335 ND_pos(h)[0], ND_pos(h)[1], DEGREES(theta),
336 arrow_l, arrow_w);
337 }
338
339 }
340 }
341
342 fprintf(fp, "%s", elog);
343 }
344
345 void outputGraph(Agraph_t * g, FILE * fp, int expMode)
346 {
347 pswrite(g, fp, expMode);
348 }
349
350 #endif /* DEBUG */