Debian Codegraph @

cmd/smyrna/topfisheyeview.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 #include "topfisheyeview.h"
14 #include <math.h>
15 #include "viewport.h"
16 #include "viewportcamera.h"
17 #include "draw.h"
18 #include "smyrna_utils.h"
19
20 #include <assert.h>
21 #include "hier.h"
22 #include "topfisheyeview.h"
23 #include <string.h>
24 #include <common/color.h>
25 #include <common/colorprocs.h>
26 #include <util/alloc.h>
27
28 static int get_temp_coords(topview *t, int level, int v, float *coord_x,
29 float *coord_y);
30
31 static int color_interpolation(glCompColor srcColor, glCompColor tarColor,
32 glCompColor * color, int levelcount,
33 int level)
34 {
35 if (levelcount <= 0)
36 return -1;
37
38 color->R = (level * tarColor.R - level * srcColor.R +
39 levelcount * srcColor.R) / levelcount;
40 color->G = (level * tarColor.G - level * srcColor.G +
41 levelcount * srcColor.G) / levelcount;
42 color->B = (level * tarColor.B - level * srcColor.B +
43 levelcount * srcColor.B) / levelcount;
44 return 0;
45 }
46
47 static v_data *makeGraph(Agraph_t* gg, int *nedges)
48 {
49 int ne = agnedges(gg);
50 int nv = agnnodes(gg);
51 v_data *graph = gv_calloc(nv, sizeof(v_data));
52 int *edges = gv_calloc(2 * ne + nv, sizeof(int)); /* reserve space for self loops */
53 float *ewgts = gv_calloc(2 * ne + nv, sizeof(float));
54 Agraph_t *g = NULL;
55 ne = 0;
56 int i = 0;
57 for (Agnode_t *np = agfstnode(gg); np; np = agnxtnode(gg, np)) {
58 graph[i].edges = edges++; /* reserve space for the self loop */
59 graph[i].ewgts = ewgts++;
60 int i_nedges = 1; // one for the self
61
62 if (!g)
63 g = agraphof(np);
64 for (Agedge_t *ep = agfstedge(g, np); ep; ep = agnxtedge(g, ep, np)) {
65 Agnode_t *tp = agtail(ep);
66 Agnode_t *hp = aghead(ep);
67 assert(hp != tp);
68 /* FIX: handle multiedges */
69 Agnode_t *const vp = tp == np ? hp : tp;
70 ne++;
71 i_nedges++;
72 *edges++ = ND_TVref(vp);
73 *ewgts++ = 1;
74
75 }
76
77 graph[i].nedges = i_nedges;
78 graph[i].edges[0] = i;
79 graph[i].ewgts[0] = 1 - i_nedges;
80 i++;
81 }
82 ne /= 2; /* each edge counted twice */
83 *nedges = ne;
84 return graph;
85 }
86
87 static void refresh_old_values(topview * t)
88 {
89 int level, v;
90 Hierarchy *hp = t->fisheyeParams.h;
91 for (level = 0; level < hp->nlevels; level++) {
92 for (v = 0; v < hp->nvtxs[level]; v++) {
93 ex_vtx_data *gg = hp->geom_graphs[level];
94 gg[v].old_physical_x_coord = gg[v].physical_x_coord;
95 gg[v].old_physical_y_coord = gg[v].physical_y_coord;
96 gg[v].old_active_level = gg[v].active_level;
97 }
98 }
99
100 }
101
102 /* To use:
103 * double* x_coords; // initial x coordinates
104 * double* y_coords; // initial y coordinates
105 * focus_t* fs;
106 * int ne;
107 * v_data* graph = makeGraph (topview*, &ne);
108 * hierarchy = makeHier(topview->NodeCount, ne, graph, x_coords, y_coords);
109 * freeGraph (graph);
110 * fs = initFocus (topview->Nodecount); // create focus set
111 */
112 void prepare_topological_fisheye(Agraph_t* g,topview * t)
113 {
114 double *x_coords = gv_calloc(t->Nodecount, sizeof(double)); // initial x coordinates
115 double *y_coords = gv_calloc(t->Nodecount, sizeof(double)); // initial y coordinates
116 focus_t *fs;
117 int ne;
118 int i;
119 int closest_fine_node;
120 int cur_level = 0;
121 Hierarchy *hp;
122 gvcolor_t cl;
123 Agnode_t *np;
124
125 v_data *graph = makeGraph(g, &ne);
126
127 i=0;
128 for (np = agfstnode(g); np; np = agnxtnode(g, np))
129 {
130 x_coords[i]=ND_A(np).x;
131 y_coords[i]=ND_A(np).y;
132 i++;
133 }
134 hp = t->fisheyeParams.h =
135 makeHier(agnnodes(g), ne, graph, x_coords, y_coords,
136 t->fisheyeParams.dist2_limit);
137 freeGraph(graph);
138 free(x_coords);
139 free(y_coords);
140
141 fs = t->fisheyeParams.fs = initFocus(agnnodes(g)); // create focus set
142
143 closest_fine_node = 0; /* first node */
144 fs->num_foci = 1;
145 fs->foci_nodes[0] = closest_fine_node;
146 fs->x_foci[0] = hp->geom_graphs[cur_level][closest_fine_node].x_coord;
147 fs->y_foci[0] = hp->geom_graphs[cur_level][closest_fine_node].y_coord;
148
149 view->Topview->fisheyeParams.repos.width =
150 round(view->bdxRight - view->bdxLeft);
151 view->Topview->fisheyeParams.repos.height =
152 round(view->bdyTop - view->bdyBottom);
153
154 //topological fisheye
155
156 colorxlate(get_attribute_value
157 ("topologicalfisheyefinestcolor", view,
158 view->g[view->activeGraph]), &cl, RGBA_DOUBLE);
159 view->Topview->fisheyeParams.srcColor.R = cl.u.RGBA[0];
160 view->Topview->fisheyeParams.srcColor.G = cl.u.RGBA[1];
161 view->Topview->fisheyeParams.srcColor.B = cl.u.RGBA[2];
162 colorxlate(get_attribute_value
163 ("topologicalfisheyecoarsestcolor", view,
164 view->g[view->activeGraph]), &cl, RGBA_DOUBLE);
165 view->Topview->fisheyeParams.tarColor.R = cl.u.RGBA[0];
166 view->Topview->fisheyeParams.tarColor.G = cl.u.RGBA[1];
167 view->Topview->fisheyeParams.tarColor.B = cl.u.RGBA[2];
168
169 sscanf(agget
170 (view->g[view->activeGraph],
171 "topologicalfisheyedistortionfactor"), "%lf",
172 &view->Topview->fisheyeParams.repos.distortion);
173 sscanf(agget
174 (view->g[view->activeGraph], "topologicalfisheyefinenodes"),
175 "%d", &view->Topview->fisheyeParams.level.num_fine_nodes);
176 sscanf(agget
177 (view->g[view->activeGraph],
178 "topologicalfisheyecoarseningfactor"), "%lf",
179 &view->Topview->fisheyeParams.level.coarsening_rate);
180 int dist2_limit = 0;
181 sscanf(agget
182 (view->g[view->activeGraph], "topologicalfisheyedist2limit"),
183 "%d", &dist2_limit);
184 view->Topview->fisheyeParams.dist2_limit = dist2_limit != 0;
185 sscanf(agget(view->g[view->activeGraph], "topologicalfisheyeanimate"),
186 "%d", &view->Topview->fisheyeParams.animate);
187
188 set_active_levels(hp, fs->foci_nodes, fs->num_foci, &t->fisheyeParams.level);
189 positionAllItems(hp, fs, &t->fisheyeParams.repos);
190 refresh_old_values(t);
191 }
192
193 static void drawtopfishnodes(topview * t)
194 {
195 glCompColor color;
196 int level, v;
197 Hierarchy *hp = t->fisheyeParams.h;
198 static int max_visible_level = 0;
199 const glCompColor srcColor = view->Topview->fisheyeParams.srcColor;
200 const glCompColor tarColor = view->Topview->fisheyeParams.tarColor;
201
202 //drawing nodes
203 glPointSize(7);
204 glBegin(GL_POINTS);
205 for (level = 0; level < hp->nlevels; level++) {
206 for (v = 0; v < hp->nvtxs[level]; v++) {
207 float x0, y0;
208 if (get_temp_coords(t, level, v, &x0, &y0)) {
209
210 if (!(-x0 / view->zoom > view->clipX1 && -x0 / view->zoom < view->clipX2 &&
211 -y0 / view->zoom > view->clipY1 && -y0 / view->zoom < view->clipY2))
212 continue;
213
214 if (max_visible_level < level)
215 max_visible_level = level;
216 if (color_interpolation(srcColor, tarColor, &color, max_visible_level, level)
217 != 0) {
218 continue;
219 }
220 glColor4d(color.R, color.G, color.B, view->defaultnodealpha);
221
222 glVertex3f(x0, y0, 0.0f);
223 }
224 }
225 }
226 glEnd();
227
228 }
229
230 static void drawtopfishedges(topview * t)
231 {
232 glCompColor color;
233
234 int level, v, i, n;
235 Hierarchy *hp = t->fisheyeParams.h;
236 static int max_visible_level = 0;
237 const glCompColor srcColor = view->Topview->fisheyeParams.srcColor;
238 const glCompColor tarColor = view->Topview->fisheyeParams.tarColor;
239
240 //and edges
241 glBegin(GL_LINES);
242 for (level = 0; level < hp->nlevels; level++) {
243 for (v = 0; v < hp->nvtxs[level]; v++) {
244 v_data *g = hp->graphs[level];
245 float x0, y0;
246 if (get_temp_coords(t, level, v, &x0, &y0)) {
247 for (i = 1; i < g[v].nedges; i++) {
248 float x, y;
249 n = g[v].edges[i];
250
251
252 if (max_visible_level < level)
253 max_visible_level = level;
254 if (color_interpolation(srcColor, tarColor, &color, max_visible_level,
255 level) != 0) {
256 continue;
257 }
258 glColor4d(color.R, color.G, color.B, view->defaultnodealpha);
259
260 if (get_temp_coords(t, level, n, &x, &y)) {
261 glVertex3f(x0, y0, 0.0f);
262 glVertex3f(x, y, 0.0f);
263 } else
264 {
265 int levell, nodee;
266 find_active_ancestor_info(hp, level, n, &levell,
267 &nodee);
268 if (get_temp_coords(t, levell, nodee, &x, &y)) {
269
270 if (!(-x0 / view->zoom > view->clipX1
271 && -x0 / view->zoom < view->clipX2
272 && -y0 / view->zoom > view->clipY1
273 && -y0 / view->zoom < view->clipY2)
274 && !(-x / view->zoom > view->clipX1
275 && -x / view->zoom < view->clipX2
276 && -y / view->zoom > view->clipY1
277 && -y / view->zoom < view->clipY2))
278
279 continue;
280
281 glVertex3f(x0, y0, 0.0f);
282 glVertex3f(x, y, 0.0f);
283 }
284 }
285 }
286 }
287 }
288 }
289 glEnd();
290
291 }
292
293 static void get_active_frame(void) {
294 gdouble seconds = g_timer_elapsed(view->timer, NULL);
295 const int fr = (int)(seconds * 1000.0);
296 if (fr < view->total_frames) {
297 view->active_frame = fr;
298 return;
299 }
300 g_timer_stop(view->timer);
301 view->Topview->fisheyeParams.animate = 0;
302 }
303
304 void drawtopologicalfisheye(topview * t)
305 {
306 get_active_frame();
307 drawtopfishnodes(t);
308 drawtopfishedges(t);
309 }
310
311 static void get_interpolated_coords(float x0, float y0, float x1, float y1,
312 int fr, int total_fr, float *x, float *y) {
313 *x = x0 + (x1 - x0) / (float)total_fr * (float)(fr + 1);
314 *y = y0 + (y1 - y0) / (float)total_fr * (float)(fr + 1);
315 }
316
317 static int get_temp_coords(topview *t, int level, int v, float *coord_x,
318 float *coord_y) {
319 Hierarchy *hp = t->fisheyeParams.h;
320 ex_vtx_data *gg = hp->geom_graphs[level];
321
322 if (!t->fisheyeParams.animate) {
323 if (gg[v].active_level != level)
324 return 0;
325
326 *coord_x = gg[v].physical_x_coord;
327 *coord_y = gg[v].physical_y_coord;
328 } else {
329
330
331 int OAL, AL;
332
333 float x0 = 0;
334 float y0 = 0;
335 float x1 = 0;
336 float y1 = 0;
337 AL = gg[v].active_level;
338 OAL = gg[v].old_active_level;
339
340 if (OAL < level || AL < level) //no draw
341 return 0;
342 if (OAL >= level || AL >= level) //draw the node
343 {
344 if (OAL == level && AL == level) //draw as is from old coords to new)
345 {
346 x0 = gg[v].old_physical_x_coord;
347 y0 = gg[v].old_physical_y_coord;
348 x1 = gg[v].physical_x_coord;
349 y1 = gg[v].physical_y_coord;
350 }
351 if (OAL > level && AL == level) //draw as from ancs to new)
352 {
353 find_old_physical_coords(t->fisheyeParams.h, level, v, &x0, &y0);
354 x1 = gg[v].physical_x_coord;
355 y1 = gg[v].physical_y_coord;
356 }
357 if (OAL == level && AL > level) //draw as from ancs to new)
358 {
359 find_physical_coords(t->fisheyeParams.h, level, v, &x1, &y1);
360 x0 = gg[v].old_physical_x_coord;
361 y0 = gg[v].old_physical_y_coord;
362 }
363 get_interpolated_coords(x0, y0, x1, y1, view->active_frame,
364 view->total_frames, coord_x, coord_y);
365 if (x0 == 0 || x1 == 0)
366 return 0;
367
368 }
369 }
370 return 1;
371 }
372
373 void changetopfishfocus(topview * t, float *x, float *y, int num_foci)
374 {
375
376 gvcolor_t cl;
377 focus_t *fs = t->fisheyeParams.fs;
378 int i;
379 int closest_fine_node;
380 int cur_level = 0;
381
382 Hierarchy *hp = t->fisheyeParams.h;
383 refresh_old_values(t);
384 fs->num_foci = num_foci;
385 for (i = 0; i < num_foci; i++) {
386 find_closest_active_node(hp, x[i], y[i], &closest_fine_node);
387 fs->foci_nodes[i] = closest_fine_node;
388 fs->x_foci[i] =
389 hp->geom_graphs[cur_level][closest_fine_node].x_coord;
390 fs->y_foci[i] =
391 hp->geom_graphs[cur_level][closest_fine_node].y_coord;
392 }
393
394
395 view->Topview->fisheyeParams.repos.width =
396 (int) (view->bdxRight - view->bdxLeft);
397 view->Topview->fisheyeParams.repos.height =
398 (int) (view->bdyTop - view->bdyBottom);
399
400 colorxlate(get_attribute_value
401 ("topologicalfisheyefinestcolor", view,
402 view->g[view->activeGraph]), &cl, RGBA_DOUBLE);
403 view->Topview->fisheyeParams.srcColor.R = cl.u.RGBA[0];
404 view->Topview->fisheyeParams.srcColor.G = cl.u.RGBA[1];
405 view->Topview->fisheyeParams.srcColor.B = cl.u.RGBA[2];
406 colorxlate(get_attribute_value
407 ("topologicalfisheyecoarsestcolor", view,
408 view->g[view->activeGraph]), &cl, RGBA_DOUBLE);
409 view->Topview->fisheyeParams.tarColor.R = cl.u.RGBA[0];
410 view->Topview->fisheyeParams.tarColor.G = cl.u.RGBA[1];
411 view->Topview->fisheyeParams.tarColor.B = cl.u.RGBA[2];
412
413 sscanf(agget
414 (view->g[view->activeGraph],
415 "topologicalfisheyedistortionfactor"), "%lf",
416 &view->Topview->fisheyeParams.repos.distortion);
417 sscanf(agget
418 (view->g[view->activeGraph], "topologicalfisheyefinenodes"),
419 "%d", &view->Topview->fisheyeParams.level.num_fine_nodes);
420 sscanf(agget
421 (view->g[view->activeGraph],
422 "topologicalfisheyecoarseningfactor"), "%lf",
423 &view->Topview->fisheyeParams.level.coarsening_rate);
424 int dist2_limit = 0;
425 sscanf(agget
426 (view->g[view->activeGraph], "topologicalfisheyedist2limit"),
427 "%d", &dist2_limit);
428 view->Topview->fisheyeParams.dist2_limit = dist2_limit != 0;
429 sscanf(agget(view->g[view->activeGraph], "topologicalfisheyeanimate"),
430 "%d", &view->Topview->fisheyeParams.animate);
431
432 set_active_levels(hp, fs->foci_nodes, fs->num_foci, &t->fisheyeParams.level);
433
434 positionAllItems(hp, fs, &t->fisheyeParams.repos);
435
436 view->Topview->fisheyeParams.animate = 1;
437
438 if (t->fisheyeParams.animate) {
439 view->active_frame = 0;
440 g_timer_start(view->timer);
441 }
442
443 }