forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkSpanTreeLayoutStrategy.cxx
343 lines (307 loc) · 11 KB
/
vtkSpanTreeLayoutStrategy.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSpanTreeLayoutStrategy.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/
// File: vtkSpanTreeLayoutStrategy.cxx
// Graph visualization library for VTK
// (c) 2003 D.J. Duke
#include "vtkSpanTreeLayoutStrategy.h"
#include "vtkConeLayoutStrategy.h"
#include "vtkObjectFactory.h"
#include "vtkMutableDirectedGraph.h"
#include "vtkTree.h"
#include "vtkEdgeListIterator.h"
#include "vtkInEdgeIterator.h"
#include "vtkOutEdgeIterator.h"
#include "vtkGraphLayout.h"
#include "vtkGraph.h"
#include "vtkPoints.h"
#include "vtkDoubleArray.h"
#include "vtkIdTypeArray.h"
#include "vtkDataSetAttributes.h"
#include "vtkSmartPointer.h"
//--------------------------------------------------------------------------
vtkStandardNewMacro(vtkSpanTreeLayoutStrategy);
vtkSpanTreeLayoutStrategy::vtkSpanTreeLayoutStrategy()
{
this->TreeLayout = vtkConeLayoutStrategy::New();
this->DepthFirstSpanningTree = false;
}
vtkSpanTreeLayoutStrategy::~vtkSpanTreeLayoutStrategy()
{
if (this->TreeLayout)
{
this->TreeLayout->Delete();
this->TreeLayout = NULL;
}
}
// Edges that cross levels more than one level of the layout
// will have edge-points inserted to match the structure of
// the rest of the graph. However, in order to compute the
// position of these points, we first need to lay out a
// graph in which these edge points are represented by real
// vertices. This struct is used to keep trach of the
// relationship between the proxy nodes in the graph used
// to compute the layout, and edges in the original graph.
struct _vtkBridge_s
{
vtkEdgeType edge;
vtkIdType delta;
vtkIdType anchor[2];
};
void vtkSpanTreeLayoutStrategy::Layout()
{
vtkSmartPointer<vtkPoints> points
= vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkMutableDirectedGraph> spanningDAG
= vtkSmartPointer<vtkMutableDirectedGraph>::New();
vtkSmartPointer<vtkEdgeListIterator> edges
= vtkSmartPointer<vtkEdgeListIterator>::New();
vtkSmartPointer<vtkGraphLayout> layoutWorker
= vtkSmartPointer<vtkGraphLayout>::New();
vtkSmartPointer<vtkOutEdgeIterator> outEdges
= vtkSmartPointer<vtkOutEdgeIterator>::New();
vtkSmartPointer<vtkInEdgeIterator> inEdges
= vtkSmartPointer<vtkInEdgeIterator>::New();
// Auxiliary structures used for building a spanning tree
int *level;
int *marks;
vtkIdType *queue;
vtkIdType front, back;
// Handle for the layout computed for the spanning tree
vtkPoints *layout;
// Auxiliary structures for placing bends into edges.
_vtkBridge_s *editlist;
_vtkBridge_s link;
link.delta = 0;
link.anchor[1] = 0;
vtkIdType editsize;
vtkEdgeType edge;
vtkIdType i, nrNodes, nrEdges;
double pointS[3], pointT[3], pointA[3];
double edgePoints[6];
// ----------------------------------------------------------
vtkDebugMacro(<<"vtkSpanTreeLayoutStrategy executing.");
// Ensure that all required inputs are available.
nrNodes = this->Graph->GetNumberOfVertices();
nrEdges = this->Graph->GetNumberOfEdges();
if (nrNodes == 0 || nrEdges == 0 || !this->TreeLayout)
{
if (nrNodes == 0)
{
vtkErrorMacro(<< "Cannot execute - no nodes in input." );
}
if (nrEdges == 0)
{
vtkErrorMacro(<< "Cannot execute - no edges in input." );
}
if (!this->TreeLayout)
{
vtkErrorMacro(<< "Cannot execute - no tree layout strategy." );
}
return;
}
// Compute a spanning tree from the graph. This is done inline here
// rather than via a Boost class so we can offer a choice of spanning
// tree. Graph traversal is supported by a queue, and during
// traversal the (tree)level is calculated for each vertex.
level = new int [nrNodes];
marks = new int [nrNodes];
queue = new vtkIdType [nrNodes];
// Initialize spanning tree with all vertices of the graph.
for (vtkIdType v = 0; v < nrNodes; v++)
{
spanningDAG->AddVertex();
marks[v] = 0;
}
// Strategy: iterate over the vertices of the graph.
// As each unvisited vertex is found, we perform a traversal starting
// from that vertex. The result is technically a spanning forest.
for (vtkIdType v = 0; v < nrNodes; v++)
{
if (!marks[v]) // not visited
{
front = back = 0;
queue[back++] = v; // push node v
level[v] = 0;
marks[v] = 1; // mark as visited
while (back != front)
{
vtkIdType src;
if (this->DepthFirstSpanningTree)
{
src = queue[--back]; // stack discipline = depth-first traversal
}
else
{
src = queue[front++]; // queue discipline = breadth-first traversal
}
// Look at outgoing edges from this node,
// adding any unseen targets to the queue,
// and edges to the spanning tree.
this->Graph->GetOutEdges(src, outEdges);
while (outEdges->HasNext())
{
vtkIdType dst = outEdges->Next().Target;
if (marks[dst] == 0) // not seen or done
{
level[dst] = level[src]+1;
queue[back++] = dst;
spanningDAG->AddGraphEdge(src,dst);
marks[dst] = 1; //seen
}
}
// Look at incoming edges: as per outgoing edges.
this->Graph->GetInEdges(src, inEdges);
while (inEdges->HasNext())
{
vtkIdType origin = inEdges->Next().Source;
if (marks[origin] == 0) // not seen or done
{
level[origin] = level[src]+1;
queue[back++] = origin;
spanningDAG->AddGraphEdge(src,origin);
marks[origin] = 1; //seen
}
}
} // while back != front
} // if !marks[v]
} // for each vertex
// Check each edge to see if it spans more than one level of
// the tree. If it does, the edge will be drawn using edge-points,
// and before we lay out the tree, we need to insert proxy nodes
// to compute the position for those points.
editsize = 0;
editlist = new _vtkBridge_s[nrEdges];
this->Graph->GetEdges(edges);
while (edges->HasNext())
{
link.edge = edges->Next();
// Loop ...
if (link.edge.Source == link.edge.Target)
{
link.anchor[0] = spanningDAG->AddVertex();
spanningDAG->AddEdge(link.edge.Source,link.anchor[0]);
editlist[editsize++] = link;
continue;
}
// If the difference in level between the start and end nodes
// is greater than one, this edge, by definition, is not
// present in the layout tree.
link.delta = level[link.edge.Target] - level[link.edge.Source];
if (abs(static_cast<int>(link.delta)) > 1)
{
link.anchor[0] = spanningDAG->AddVertex();
spanningDAG->AddEdge(link.delta > 0 ? link.edge.Source : link.edge.Target, link.anchor[0]);
if (abs(static_cast<int>(link.delta)) > 2)
{
link.anchor[1] = spanningDAG->AddVertex();
spanningDAG->AddEdge(link.anchor[0], link.anchor[1]);
}
editlist[editsize++] = link;
}
}
// Layout the tree using the layout filter provided.
layoutWorker->SetLayoutStrategy(this->TreeLayout);
layoutWorker->SetInputData(spanningDAG);
layoutWorker->Update();
layout = layoutWorker->GetOutput()->GetPoints();
// Copy the node positions for nodes in the original
// graph from the layout tree to the output positions.
points->SetNumberOfPoints(nrNodes);
for (i = 0; i < nrNodes; i++)
{
points->SetPoint(i, layout->GetPoint(i));
}
// Now run through the edit list, computing the position for
// each of the edge points
for (i = 0; i < editsize; i++)
{
link = editlist[i];
if (link.delta == 0)
{
// Loop: Each loop is drawn as an edge with 2 edge points. The x & y
// coordinates have been fixed by the layout. The z coordinates are
// scaled to that the edge points are 1/3 of the distance between
// levels, above and below the node.
layout->GetPoint(link.edge.Source, pointS);
layout->GetPoint(link.anchor[0], pointA);
edgePoints[0] = edgePoints[3] = pointA[0];
edgePoints[1] = edgePoints[4] = pointA[1];
edgePoints[2] = pointS[2] + (pointA[2]-pointS[2])/3.0;
edgePoints[5] = pointS[2] - (pointA[2]-pointS[2])/3.0;
this->Graph->SetEdgePoints(link.edge.Id, 2, edgePoints);
}
else if (link.delta > 1)
{
layout->GetPoint(link.edge.Source, pointS);
layout->GetPoint(link.edge.Target, pointT);
layout->GetPoint(link.anchor[0], pointA);
edgePoints[0] = pointA[0];
edgePoints[1] = pointA[1];
edgePoints[2] = pointS[2] + (pointT[2] - pointS[2])/link.delta;
if (link.delta > 2)
{
layout->GetPoint(link.anchor[1], pointA);
edgePoints[3] = edgePoints[0];
edgePoints[4] = edgePoints[1];
edgePoints[5] = pointS[2] + (link.delta-1)*(pointT[2] - pointS[2])/link.delta;
this->Graph->SetEdgePoints(link.edge.Id, 2, edgePoints);
}
else
{
this->Graph->SetEdgePoints(link.edge.Id, 1, edgePoints);
}
}
else if (link.delta < -1)
{
int delta = -link.delta;
layout->GetPoint(link.edge.Source, pointS);
layout->GetPoint(link.edge.Target, pointT);
layout->GetPoint(link.anchor[0], pointA);
edgePoints[0] = pointA[0];
edgePoints[1] = pointA[1];
edgePoints[2] = pointS[2] + (pointT[2] - pointS[2])/delta;
if (link.delta < -2)
{
layout->GetPoint(link.anchor[1], pointA);
edgePoints[3] = edgePoints[0];
edgePoints[4] = edgePoints[1];
edgePoints[5] = pointS[2] + (delta-1)*(pointT[2] - pointS[2])/delta;
this->Graph->SetEdgePoints(link.edge.Id, 2, edgePoints);
}
else
{
this->Graph->SetEdgePoints(link.edge.Id, 1, edgePoints);
}
}
}
// Clean up temporary storage.
delete [] editlist;
delete [] level;
delete [] marks;
delete [] queue;
this->Graph->SetPoints(points);
vtkDebugMacro(<<"SpanTreeLayoutStrategy complete.");
}
void vtkSpanTreeLayoutStrategy::PrintSelf(ostream& os, vtkIndent indent)
{
vtkGraphLayoutStrategy::PrintSelf(os,indent);
os << indent << "TreeLayout: " << (this->TreeLayout ? "" : "(none)") << endl;
if (this->TreeLayout)
{
this->TreeLayout->PrintSelf(os, indent.GetNextIndent());
}
os << indent << "DepthFirstSpanningTree: " << (this->DepthFirstSpanningTree ? "On" : "Off") << endl;
}