forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrings.cpp
More file actions
422 lines (340 loc) · 9.63 KB
/
rings.cpp
File metadata and controls
422 lines (340 loc) · 9.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// $Id: rings.cpp,v 1.1 2005/01/07 23:01:20 rdmp1c Exp $
// Output RINGS representation of a GML tree
// See "RINGS: A Technique for Visualizing Large Hierarchies"
// http://www.cs.ucdavis.edu/~ma/papers/graph2.pdf
#include <iostream>
#include <fstream>
#include <queue>
#include <GTL/GTL.h>
#include <GTL/bfs.h>
#include <GTL/dfs.h>
#include "gport.h"
#include "mygraph.h"
#include "mytree.h"
#include <time.h>
GPostscriptPort g;
clock_t t0;
clock_t t1;
void ShowTimeUsed (clock_t &t0, clock_t &t1);
void ShowTimeUsed (clock_t &t0, clock_t &t1)
{
cout << "CPU time used = " << (float)(t1 - t0)/(float)CLOCKS_PER_SEC << " seconds" << endl;
}
node_map<int> num_children;
node_map<int> num_grandchildren;
node_map<double> R1;
node_map<GPoint> pt;
node_map<bool> outer_ring;
class CompareNodes {
public:
bool operator() (node x, node y)
{
return num_children[x] < num_children[y];
}
};
// Get numbers of children and grandchildren
class mydfs : public dfs
{
public:
mydfs () : dfs () { };
virtual void entry_handler (graph &G, node &n, node &f)
{
num_children[n] = n.outdeg();
num_grandchildren[n] = 0;
}
virtual void leave_handler (graph &G, node &n, node &f)
{
if (n.outdeg() > 0)
{
node::adj_nodes_iterator it = n.adj_nodes_begin();
while (it != n.adj_nodes_end())
{
num_grandchildren[n] += num_children[(*it)];
it++;
}
}
}
};
#ifndef M_PI
#define M_PI 3.14159265358979323846 // define pi
#endif
#define SQR(X) ((X) * (X))
double f (double &R1, double &R2, int n);
double f (double &R1, double &R2, int n)
{
double theta = M_PI/double(n);
double fn = SQR(1 - sin(theta)) / SQR (1 + sin(theta));
R2 = sqrt (fn * SQR(R1));
return fn;
}
// Compute and draw layout ussing a breadth first search
class mybfs : public bfs
{
public:
mybfs () : bfs () { };
virtual void popped_node_handler (graph &G, node &n)
{
if ((n.outdeg() > 0) && (num_grandchildren[n] > 0))
{
// Colour map by level in graph
switch (level(n))
{
case 0: g.SetFillColorRGB (125,126,190); break;
case 1: g.SetFillColorRGB (202,154,152); break;
case 2: g.SetFillColorRGB (178,219,178); break;
case 3: g.SetFillColorRGB (255,179,179); break;
case 4: g.SetFillColorRGB (225,224,179); break;
case 5: g.SetFillColorRGB (255,178,255); break;
default: g.SetFillColorRGB (192,192,192); break;
}
// Create a list of the children sorted by their number of children
double total_grandchildren = 0.0;
priority_queue <node, vector<node>, CompareNodes> p;
node::adj_nodes_iterator it = n.adj_nodes_begin();
while (it != n.adj_nodes_end())
{
p.push(*it);
outer_ring[*it] = false;
total_grandchildren += (double)num_children[*it];
it++;
}
// Find how many children to put in the two rings
double R2;
double sum_grandchildren = 0.0;
int count = 0;
int outer = num_children[n]; // number in outer ring
int inner = 0; // number in inner ring
// We look at the sorted list of children and
// put the split between inner and outer rings at the point where
// the fraction of children yet to be included is less than the
// amount of space available in an inner ring if count rings are
// in the outer ring. It is possible that there won't be an inner ring,
// in which case inner is 0.
// We use the node map outer_ring to classify children by which ring they
// are assigned to.
while (!p.empty())
{
node x = p.top();
outer_ring[x] = (inner == 0);
count++;
sum_grandchildren += (double)num_children[x];
double fraction = 1.0 - (sum_grandchildren / total_grandchildren);
double fk = f(R1[n], R2, count);
if ((count > 2) && (inner == 0))
{
if (fraction < fk)
{
inner = outer - count;
outer = count;
}
}
p.pop();
}
// Compute radius of children in outer ring
double fn = f(R1[n], R2, outer);
double r_outer = (R1[n] - R2)/2.0;
double theta_outer = M_PI/double(outer);
// Compute radius of children in inner ring (if any)
double r_inner = 0.0;
double R3 = 0.0;
double theta_inner = 0.0;
if (inner > 0)
{
fn = f(R2, R3, inner);
r_inner = (R2 - R3)/2.0;
theta_inner = M_PI/double(inner);
}
int inner_count = 0;
int outer_count = 0;
it = n.adj_nodes_begin();
while (it != n.adj_nodes_end())
{
if (outer_ring[*it])
{
R1[*it] = r_outer;
int offset_x = (int)((R1[*it] + R2) * cos(2 * theta_outer * outer_count));
int offset_y = (int)((R1[*it] + R2) * sin(2 * theta_outer * outer_count));
outer_count++;
// Draw!!!
GPoint p = pt[n];
p.Offset (offset_x, offset_y);
pt[*it] = p;
g.DrawLinePts (pt[n], pt[*it]);
//g.DrawCircle (pt[*it], R1[*it]);
// Centre
pt[*it] = p;
}
else
{
R1[*it] = r_inner;
int offset_x = (int)((R1[*it] + R3) * cos(2 * theta_inner * inner_count));
int offset_y = (int)((R1[*it] + R3) * sin(2 * theta_inner * inner_count));
inner_count++;
// Draw!!!
GPoint p = pt[n];
p.Offset (offset_x, offset_y);
pt[*it] = p;
g.DrawLinePts (pt[n], pt[*it]);
//g.DrawCircle (pt[*it], R1[*it]);
// Centre
pt[*it] = p;
}
it++;
}
}
else
{
// leaf, or node with no grandchildren (i.e., a star)
if (n.outdeg() > 0)
{
//g.DrawCircle (pt[n], R1[n]);
double theta = (2 * M_PI)/double(n.outdeg());
double radius = R1[n] * 0.9;
double gap = fabs (sin(theta) * radius);
// If the gap between two edges is too small to be easily visible
// we draw a filled circle
if ((gap < 2.0) && (n.outdeg() > 1))
{
g.FillCircle (pt[n], (int)radius);
}
else
{
int count = 0;
node::adj_nodes_iterator it = n.adj_nodes_begin();
while (it != n.adj_nodes_end())
{
int offset_x = (int)(radius * cos(theta*count));
int offset_y = (int)(radius * sin(theta*count));
GPoint p = pt[n];
p.Offset (offset_x, offset_y);
pt[*it] = p;
g.DrawLinePts (pt[n], pt[*it]);
count++;
it++;
}
}
}
}
}
virtual void finished_handler (graph &G, node &n)
{
}
};
int main (int argc, const char * argv[])
{
if (argc < 2)
{
cout << "Usage: graph <file-name>" << endl;
exit(1);
}
char filename[256];
strcpy (filename, argv[1]);
// ---------------------------------------------------------
// Read graph
MyTree G;
G.read_labels_as_weights();
t0 = clock();
GML_error err = G.load (filename);
t1 = clock();
if (err.err_num != GML_OK)
{
cerr << "Error (" << err.err_num << ") loading graph from file \"" << filename << "\"";
switch (err.err_num)
{
case GML_FILE_NOT_FOUND: cerr << "A file with that name doesn't exist."; break;
case GML_TOO_MANY_BRACKETS: cerr << "A mismatch of brackets was detected, i.e. there were too many closing brackets (])."; break;
case GML_OPEN_BRACKET: cerr << "Now, there were too many opening brackets ([)"; break;
case GML_TOO_MANY_DIGITS: cerr << "The number of digits a integer or floating point value can have is limited to 1024, this should be enough :-)"; break;
case GML_PREMATURE_EOF: cerr << "An EOF occured, where it wasn't expected, e.g. while scanning a string."; break;
case GML_SYNTAX: cerr << "The file isn't a valid GML file, e.g. a mismatch in the key-value pairs."; break;
case GML_UNEXPECTED: cerr << "A character occured, where it makes no sense, e.g. non-numerical characters"; break;
case GML_OK: break;
}
cerr << endl;
exit(1);
}
else
{
cout << "Graph read from file \"" << filename << "\" has " << G.number_of_nodes() << " nodes and " << G.number_of_edges() << " edges" << endl;
}
ShowTimeUsed (t0, t1);
// ---------------------------------------------------------
// Test that it is a tree
if (is_tree (G))
{
cout << "Is a tree" << endl;
}
else
{
cout << "Graph is not a tree" << endl;
node v;
forall_nodes(v,G)
if ( v.indeg () < 1 ) cout << G.get_node_label(v) << " has no parent" << endl;
if (!G.is_connected() ) cout << "Not connected";
exit(1);
}
node root = G.root();
cout << "Root = " << root << " " << "\"" << G.get_node_label (root) << "\"" << endl;
cout << "Computing layout..." << endl;
t0 = clock();
bfs b;
b.start_node (G.root());
b.calc_level(true);
if (b.check(G) != algorithm::GTL_OK)
{
cerr << "bfs check failed at " << __LINE__ << " in " << __FILE__ << endl;
exit(1);
}
else
{
if (b.run(G) != algorithm::GTL_OK)
{
cerr << "bfs algorithm failed at " << __LINE__ << " in " << __FILE__ << endl;
exit(1);
}
}
// dfs
mydfs d;
d.start_node (G.root());
if (d.check(G) != algorithm::GTL_OK)
{
cerr << "dfs check failed at " << __LINE__ << " in " << __FILE__ << endl;
exit(1);
}
else
{
if (d.run(G) != algorithm::GTL_OK)
{
cerr << "dfs algorithm failed at " << __LINE__ << " in " << __FILE__ << endl;
exit(1);
}
}
char picture_filename[256];
strcpy (picture_filename, filename);
strcat (picture_filename, ".ps");
g.StartPicture (picture_filename);
g.SetPenWidth(1);
R1[G.root()] = 200.0;
GPoint centre(200,200);
pt[G.root()] = centre;
mybfs layout;
layout.start_node (G.root());
layout.calc_level(true);
if (layout.check(G) != algorithm::GTL_OK)
{
cerr << "bfs check failed at " << __LINE__ << " in " << __FILE__ << endl;
exit(1);
}
else
{
if (layout.run(G) != algorithm::GTL_OK)
{
cerr << "bfs algorithm failed at " << __LINE__ << " in " << __FILE__ << endl;
exit(1);
}
}
g.EndPicture ();
t1 = clock();
ShowTimeUsed (t0, t1);
return 0;
}