forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkGraphHierarchicalBundleEdges.cxx
327 lines (294 loc) · 9.88 KB
/
vtkGraphHierarchicalBundleEdges.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkGraphHierarchicalBundleEdges.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.
-------------------------------------------------------------------------*/
#include "vtkGraphHierarchicalBundleEdges.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkEdgeListIterator.h"
#include "vtkFloatArray.h"
#include "vtkGraph.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPoints.h"
#include "vtkSmartPointer.h"
#include "vtkStdString.h"
#include "vtkTree.h"
#include "vtkVariantArray.h"
#include <vtksys/stl/map>
using vtksys_stl::map;
vtkStandardNewMacro(vtkGraphHierarchicalBundleEdges);
vtkGraphHierarchicalBundleEdges::vtkGraphHierarchicalBundleEdges()
{
this->BundlingStrength = 0.8;
this->SetNumberOfInputPorts(2);
this->DirectMapping = false;
}
int vtkGraphHierarchicalBundleEdges::FillInputPortInformation(int port, vtkInformation* info)
{
if (port == 0)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkGraph");
return 1;
}
else if (port == 1)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkTree");
return 1;
}
return 0;
}
int vtkGraphHierarchicalBundleEdges::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *graphInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *treeInfo = inputVector[1]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the input and output
vtkGraph *graph = vtkGraph::SafeDownCast(
graphInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkTree *tree = vtkTree::SafeDownCast(
treeInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkGraph *output = vtkGraph::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
// If graph or tree is empty, we're done.
if (graph->GetNumberOfVertices() == 0 ||
tree->GetNumberOfVertices() == 0)
{
return 1;
}
// Create a map from graph indices to tree indices
// If we are using DirectMapping this is trivial
// we just create an identity map
map<vtkIdType, vtkIdType> graphIndexToTreeIndex;
if (this->DirectMapping)
{
if (graph->GetNumberOfVertices() > tree->GetNumberOfVertices())
{
vtkErrorMacro("Cannot have more graph vertices than tree vertices using direct mapping.");
return 0;
}
// Create identity map.
for (vtkIdType gv = 0; gv < graph->GetNumberOfVertices(); gv++)
{
graphIndexToTreeIndex[gv] = gv;
}
}
// Okay if we do not have direct mapping then we need
// to do some templated madness to go from an arbitrary
// type to a nice vtkIdType to vtkIdType mapping
if (!this->DirectMapping)
{
// Check for valid pedigree id arrays.
vtkAbstractArray* graphIdArray =
graph->GetVertexData()->GetPedigreeIds();
if (!graphIdArray)
{
vtkErrorMacro("Graph pedigree id array not found.");
return 0;
}
// Check for valid domain array, if any.
vtkAbstractArray* graphDomainArray =
graph->GetVertexData()->GetAbstractArray("domain");
vtkAbstractArray* treeIdArray =
tree->GetVertexData()->GetPedigreeIds();
if (!treeIdArray)
{
vtkErrorMacro("Tree pedigree id array not found.");
return 0;
}
// Check for valid domain array, if any.
vtkAbstractArray* treeDomainArray =
tree->GetVertexData()->GetAbstractArray("domain");
map<vtkVariant,vtkIdType,vtkVariantLessThan> graphIdMap;
// Create a map from graph id to graph index
for (int i=0; i<graph->GetNumberOfVertices(); ++i)
{
graphIdMap[graphIdArray->GetVariantValue(i)] = i;
}
// Now create the map from graph index to tree index
for (int i=0; i<tree->GetNumberOfVertices(); ++i)
{
vtkVariant id = treeIdArray->GetVariantValue(i);
if (graphIdMap.count(id))
{
// Make sure that the domain for this id in the graph matches
// the one in the tree before adding to the map. This guards
// against drawing edges to group nodes in the tree.
if(treeDomainArray)
{
vtkVariant treeDomain = treeDomainArray->GetVariantValue(i);
vtkVariant graphDomain;
if(graphDomainArray)
{
graphDomain = graphDomainArray->GetVariantValue(graphIdMap[id]);
}
else
{
graphDomain = graphIdArray->GetName();
}
if(graphDomain != treeDomain)
{
continue;
}
}
graphIndexToTreeIndex[graphIdMap[id]] = i;
}
}
}
output->ShallowCopy(graph);
output->DeepCopyEdgePoints(graph);
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
double pt[3];
for (vtkIdType v = 0; v < graph->GetNumberOfVertices(); ++v)
{
vtkIdType treeVertex = 0;
if (graphIndexToTreeIndex.count(v) > 0)
{
treeVertex = graphIndexToTreeIndex[v];
tree->GetPoint(treeVertex, pt);
}
else
{
pt[0] = 0.0;
pt[1] = 0.0;
pt[2] = 0.0;
}
points->InsertNextPoint(pt);
}
output->SetPoints(points);
vtkSmartPointer<vtkIdList> sourceList =
vtkSmartPointer<vtkIdList>::New();
vtkSmartPointer<vtkIdList> targetList =
vtkSmartPointer<vtkIdList>::New();
vtkSmartPointer<vtkEdgeListIterator> edges =
vtkSmartPointer<vtkEdgeListIterator>::New();
graph->GetEdges(edges);
while (edges->HasNext())
{
vtkEdgeType e = edges->Next();
unsigned int graphSourceIndex = e.Source;
unsigned int graphTargetIndex = e.Target;
// Do not render loops
if (graphSourceIndex == graphTargetIndex)
{
continue;
}
vtkIdType source = 0;
vtkIdType target = 0;
if (graphIndexToTreeIndex.count(graphSourceIndex) > 0 &&
graphIndexToTreeIndex.count(graphTargetIndex) > 0)
{
source = graphIndexToTreeIndex[graphSourceIndex];
target = graphIndexToTreeIndex[graphTargetIndex];
}
else
{
// The endpoints of this edge are not found in the tree.
continue;
}
// Find path from source to target
sourceList->Reset();
vtkIdType curSource = source;
while (curSource != tree->GetRoot())
{
curSource = tree->GetParent(curSource);
sourceList->InsertNextId(curSource);
}
targetList->Reset();
vtkIdType curTarget = target;
while (sourceList->IsId(curTarget) == -1 && curTarget != source)
{
curTarget = tree->GetParent(curTarget);
targetList->InsertNextId(curTarget);
}
vtkIdType cellPoints;
if (curTarget == source)
{
cellPoints = targetList->GetNumberOfIds();
}
else
{
cellPoints = sourceList->IsId(curTarget) + targetList->GetNumberOfIds();
}
// We may eliminate a common ancestor if:
// 1. The source is not an ancestor of the target
// 2. The target is not an ancestor of the source
// 3. The number of points along the path is at least 4
bool eliminateCommonAncestor = false;
if (sourceList->IsId(target) == -1 && targetList->IsId(source) == -1 && cellPoints >= 4)
{
cellPoints--;
eliminateCommonAncestor = true;
}
double cellPointsD = static_cast<double>(cellPoints);
double sourcePt[3];
tree->GetPoint(source, sourcePt);
double targetPt[3];
tree->GetPoint(target, targetPt);
double interpPt[3];
int curPoint = 1;
// Insert points into the polyline going up the tree to
// the common ancestor.
output->ClearEdgePoints(e.Id);
for (vtkIdType s = 0; s < sourceList->IsId(curTarget); s++)
{
tree->GetPoint(sourceList->GetId(s), pt);
for (int c = 0; c < 3; c++)
{
interpPt[c] = (1.0 - curPoint/(cellPointsD+1))*sourcePt[c]
+ (curPoint/(cellPointsD+1))*targetPt[c];
interpPt[c] = (1.0 - this->BundlingStrength)*interpPt[c]
+ this->BundlingStrength*pt[c];
}
output->AddEdgePoint(e.Id, interpPt);
++curPoint;
}
// Insert points into the polyline going down the tree from
// the common ancestor to the target vertex, possibly excluding
// the common ancestor if it is a long path.
vtkIdType maxTargetId = targetList->GetNumberOfIds() - 1;
if (eliminateCommonAncestor)
{
maxTargetId = targetList->GetNumberOfIds() - 2;
}
for (vtkIdType t = maxTargetId; t >= 0; t--)
{
tree->GetPoint(targetList->GetId(t), pt);
for (int c = 0; c < 3; c++)
{
interpPt[c] = (1.0 - curPoint/(cellPointsD+1))*sourcePt[c]
+ (curPoint/(cellPointsD+1))*targetPt[c];
interpPt[c] = (1.0 - this->BundlingStrength)*interpPt[c]
+ this->BundlingStrength*pt[c];
}
output->AddEdgePoint(e.Id, interpPt);
++curPoint;
}
}
return 1;
}
void vtkGraphHierarchicalBundleEdges::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "BundlingStrength: " << this->BundlingStrength << endl;
os << indent << "DirectMapping: " << this->DirectMapping << endl;
}