forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkNetworkHierarchy.cxx
321 lines (266 loc) · 9.15 KB
/
vtkNetworkHierarchy.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkNetworkHierarchy.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 "vtkNetworkHierarchy.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMutableDirectedGraph.h"
#include "vtkObjectFactory.h"
#include "vtkOutEdgeIterator.h"
#include "vtkPointData.h"
#include "vtkSmartPointer.h"
#include "vtkStringArray.h"
#include "vtkTable.h"
#include "vtkGraph.h"
#include "vtkTree.h"
#include "vtkVariant.h"
#include <map>
#include <utility>
#include <vector>
#include <sstream>
#include <algorithm>
vtkStandardNewMacro(vtkNetworkHierarchy);
// This is just a macro wrapping for smart pointers
#define VTK_CREATE(type, name) \
vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
vtkNetworkHierarchy::vtkNetworkHierarchy()
{
this->IPArrayName = 0;
this->SetIPArrayName("ip");
}
vtkNetworkHierarchy::~vtkNetworkHierarchy()
{
this->SetIPArrayName(0);
}
void vtkNetworkHierarchy::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "IPArrayName: " << (this->IPArrayName ? "" : "(null)") << endl;
}
int vtkNetworkHierarchy::FillOutputPortInformation(
int vtkNotUsed(port), vtkInformation* info)
{
// now add our info
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkTree");
return 1;
}
int vtkNetworkHierarchy::FillInputPortInformation(
int vtkNotUsed(port), vtkInformation* info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkGraph");
return 1;
}
void vtkNetworkHierarchy::GetSubnets(unsigned int packedIP, int *subnets)
{
unsigned int num = packedIP;
subnets[3] = num % 256;
num = num >> 8;
subnets[2] = num % 256;
num = num >> 8;
subnets[1] = num % 256;
num = num >> 8;
subnets[0] = num;
}
unsigned int vtkNetworkHierarchy::ITON(vtkStdString ip)
{
unsigned int subnets[4];
sscanf(ip.c_str(),"%u.%u.%u.%u",
&(subnets[0]),&(subnets[1]),&(subnets[2]),&(subnets[3]));
int num = subnets[0];
num = num << 8;
num += subnets[1];
num = num << 8;
num += subnets[2];
num = num << 8;
num += subnets[3];
return num;
}
int vtkNetworkHierarchy::RequestData(
vtkInformation*,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// Storing the inputTable and outputTree handles
vtkGraph *inputGraph = vtkGraph::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkTree *outputTree = vtkTree::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
// Get the field to filter on
vtkAbstractArray* arr =
inputGraph->GetVertexData()->GetAbstractArray(this->IPArrayName);
vtkStringArray* ipArray = vtkArrayDownCast<vtkStringArray>(arr);
if (ipArray == NULL)
{
vtkErrorMacro(<< "An string based ip array must be specified");
return 0;
}
// Build subnet map
typedef std::vector<std::pair<unsigned int, vtkIdType> > subnet_map_type;
subnet_map_type SubnetMap;
for (vtkIdType i = 0; i < ipArray->GetNumberOfTuples(); ++i)
{
unsigned int packedID = this->ITON(ipArray->GetValue(i));
SubnetMap.push_back(std::make_pair(packedID,i));
}
std::sort(SubnetMap.begin(), SubnetMap.end());
// Create builder for the tree
VTK_CREATE(vtkMutableDirectedGraph,builder);
// Make a bunch of blank vertices
for(int i=0; i<inputGraph->GetNumberOfVertices(); ++i)
{
builder->AddVertex();
}
// Get the input graph and copy the vertex data
vtkDataSetAttributes *inputVertexData = inputGraph->GetVertexData();
vtkDataSetAttributes *builderVertexData = builder->GetVertexData();
builderVertexData->DeepCopy(inputVertexData);
// Get pedigree ids.
vtkAbstractArray* pedIDArr = builderVertexData->GetPedigreeIds();
// Get domain. If there isn't one, make one.
vtkStringArray* domainArr = vtkArrayDownCast<vtkStringArray>(
builderVertexData->GetAbstractArray("domain"));
if (pedIDArr && !domainArr)
{
domainArr = vtkStringArray::New();
domainArr->SetName("domain");
for (vtkIdType r = 0; r < inputGraph->GetNumberOfVertices(); ++r)
{
domainArr->InsertNextValue(pedIDArr->GetName());
}
builderVertexData->AddArray(domainArr);
}
// All new vertices will be placed in this domain.
vtkStdString newVertexDomain = "subnet";
// Make the builder's field data a table
// so we can call InsertNextBlankRow.
vtkSmartPointer<vtkTable> treeTable =
vtkSmartPointer<vtkTable>::New();
treeTable->SetRowData(builder->GetVertexData());
// Get the pedigree ID and domain columns
int pedIDColumn = -1;
int domainColumn = -1;
if (pedIDArr)
{
treeTable->GetRowData()->GetAbstractArray(pedIDArr->GetName(), pedIDColumn);
treeTable->GetRowData()->GetAbstractArray("domain", domainColumn);
}
// Add root
vtkIdType rootID = builder->AddVertex();
treeTable->InsertNextBlankRow();
// Don't label the root node...
// treeTable->SetValueByName(rootID, this->IPArrayName, vtkVariant("Internet"));
treeTable->SetValueByName(rootID, this->IPArrayName, vtkVariant(""));
if (pedIDArr)
{
treeTable->SetValue(rootID, pedIDColumn, rootID);
treeTable->SetValue(rootID, domainColumn, newVertexDomain);
}
// Iterate through the different subnets
subnet_map_type::iterator I;
int currentSubnet0 = -1;
int currentSubnet1 = -1;
int currentSubnet2 = -1;
int subnets[4];
vtkIdType currentParent0 = 0;
vtkIdType currentParent1 = 0;
vtkIdType currentParent2 = 0;
vtkIdType treeIndex;
vtkIdType leafIndex;
for (I = SubnetMap.begin(); I != SubnetMap.end(); ++I)
{
unsigned int packedID = (*I).first;
leafIndex = (*I).second;
this->GetSubnets(packedID,subnets);
// Is this a new subnet 0
if (subnets[0] != currentSubnet0)
{
// Add child
treeIndex = builder->AddChild(rootID);
// Add vertex fields for the child
treeTable->InsertNextBlankRow();
// Set the label for the child
std::ostringstream subnetStream;
subnetStream << subnets[0];
treeTable->SetValueByName(treeIndex, this->IPArrayName, vtkVariant(subnetStream.str()));
// Set pedigree ID and domain for the child
if (pedIDArr)
{
treeTable->SetValue(treeIndex, pedIDColumn, treeIndex);
treeTable->SetValue(treeIndex, domainColumn, newVertexDomain);
}
// Store new parent/subnet info
currentSubnet0 = subnets[0];
currentParent0 = treeIndex;
// Invalidate subnets
currentSubnet1 = currentSubnet2 = -1;
}
// Is this a new subnet 1
if (subnets[1] != currentSubnet1)
{
// Add child
treeIndex = builder->AddChild(currentParent0);
// Add vertex fields for the child
treeTable->InsertNextBlankRow();
// Set the label for the child
std::ostringstream subnetStream;
subnetStream << subnets[0] << "." << subnets[1];
treeTable->SetValueByName(treeIndex, this->IPArrayName, vtkVariant(subnetStream.str()));
// Set pedigree ID and domain for the child
if (pedIDArr)
{
treeTable->SetValue(treeIndex, pedIDColumn, treeIndex);
treeTable->SetValue(treeIndex, domainColumn, newVertexDomain);
}
// Store new parent/subnet info
currentSubnet1 = subnets[1];
currentParent1 = treeIndex;
// Invalidate subnets
currentSubnet2 = -1;
}
// Is this a new subnet 2
if (subnets[2] != currentSubnet2)
{
// Add child
treeIndex = builder->AddChild(currentParent1);
// Add vertex fields for the child
treeTable->InsertNextBlankRow();
// Set the label for the child
std::ostringstream subnetStream;
subnetStream << subnets[0] << "." << subnets[1] << "." << subnets[2];
treeTable->SetValueByName(treeIndex, this->IPArrayName, vtkVariant(subnetStream.str()));
// Set pedigree ID and domain for the child
if (pedIDArr)
{
treeTable->SetValue(treeIndex, pedIDColumn, treeIndex);
treeTable->SetValue(treeIndex, domainColumn, newVertexDomain);
}
// Store new parent/subnet info
currentSubnet2 = subnets[2];
currentParent2 = treeIndex;
}
builder->AddEdge(currentParent2, leafIndex);
}
// Move the structure to the output
if (!outputTree->CheckedShallowCopy(builder))
{
vtkErrorMacro(<<"Invalid tree structure!");
return 0;
}
return 1;
}