forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkExtractSelectedTree.cxx
243 lines (209 loc) · 7.8 KB
/
vtkExtractSelectedTree.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkExtractSelectedTree.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.
=========================================================================*/
#include "vtkExtractSelectedTree.h"
#include "vtkCellData.h"
#include "vtkInformation.h"
#include "vtkMutableDirectedGraph.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkSmartPointer.h"
#include "vtkStringArray.h"
#include "vtkTree.h"
#include "vtkIdTypeArray.h"
#include "vtkSelection.h"
#include "vtkSelectionNode.h"
#include "vtkConvertSelection.h"
#include "vtkEdgeListIterator.h"
#include "vtkNew.h"
#include <map>
vtkStandardNewMacro(vtkExtractSelectedTree);
vtkExtractSelectedTree::vtkExtractSelectedTree()
{
this->SetNumberOfInputPorts(2);
}
vtkExtractSelectedTree::~vtkExtractSelectedTree()
{
}
//----------------------------------------------------------------------------
void vtkExtractSelectedTree::SetSelectionConnection(vtkAlgorithmOutput* in)
{
this->SetInputConnection(1, in);
}
//----------------------------------------------------------------------------
int vtkExtractSelectedTree::FillInputPortInformation(int port, vtkInformation *info)
{
if(port == 0)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkTree");
return 1;
}
else if(port == 1)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkSelection");
return 1;
}
return 0;
}
//----------------------------------------------------------------------------
void vtkExtractSelectedTree::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
int vtkExtractSelectedTree::BuildTree( vtkTree * inputTree, vtkIdTypeArray * selectedVerticesList,
vtkMutableDirectedGraph * builder )
{
// Get the input and builder vertex and edge data.
vtkDataSetAttributes * inputVertexData = inputTree->GetVertexData();
vtkDataSetAttributes * inputEdgeData = inputTree->GetEdgeData();
vtkDataSetAttributes * builderVertexData = builder->GetVertexData();
vtkDataSetAttributes * builderEdgeData = builder->GetEdgeData();
builderVertexData->CopyAllocate(inputVertexData);
builderEdgeData->CopyAllocate(inputEdgeData);
//Add selected vertices and set up a map between the input tree vertex id
//and the output tree vertex id
std::map<vtkIdType, vtkIdType> vertexMap;
for (vtkIdType j = 0; j < selectedVerticesList->GetNumberOfTuples();j++)
{
vtkIdType inVert = selectedVerticesList->GetValue(j);
vtkIdType outVert = builder->AddVertex();
builderVertexData->CopyData(inputVertexData, inVert, outVert);
vertexMap[inVert] = outVert;
}
// Add edges connecting selected vertices
vtkSmartPointer<vtkEdgeListIterator> edges = vtkSmartPointer<vtkEdgeListIterator>::New();
inputTree->GetEdges(edges);
while (edges->HasNext())
{
vtkEdgeType e = edges->Next();
if (vertexMap.find(e.Source) != vertexMap.end() &&
vertexMap.find(e.Target) != vertexMap.end())
{
vtkIdType source = vertexMap[e.Source];
vtkIdType target = vertexMap[e.Target];
vtkEdgeType f = builder->AddEdge(source, target);
builderEdgeData->CopyData(inputEdgeData, e.Id, f.Id);
vtkIdType npts;
double* pts;
inputTree->GetEdgePoints(e.Id, npts, pts);
builder->SetEdgePoints(f.Id, npts, pts);
}
}
return 1;
}
int vtkExtractSelectedTree::RequestData(
vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
vtkTree * inputTree = vtkTree::GetData(inputVector[0]);
vtkSelection * selection = vtkSelection::GetData(inputVector[1]);
vtkTree * outputTree = vtkTree::GetData(outputVector);
if(!selection)
{
vtkErrorMacro("No vtkSelection provided as input.");
return 0;
}
//obtain a vertex selection list from the input vtkSelection
// Convert the selection to an INDICES selection
vtkSmartPointer<vtkSelection> converted;
converted.TakeReference(vtkConvertSelection::ToIndexSelection(selection, inputTree));
if (!converted.GetPointer())
{
vtkErrorMacro("Selection conversion to INDICES failed.");
return 0;
}
vtkNew<vtkIdTypeArray> selectedVerticesList;
for (unsigned int i = 0; i < converted->GetNumberOfNodes(); ++i)
{
vtkSelectionNode * node = converted->GetNode(i);
// Append the selectedVerticesList
vtkIdTypeArray * curList = vtkIdTypeArray::SafeDownCast(node->GetSelectionList());
if (curList)
{
int inverse = node->GetProperties()->Get(vtkSelectionNode::INVERSE());
if (inverse)
{//selection is to be removed
if (node->GetFieldType() == vtkSelectionNode::VERTEX)
{//keep all the other vertices
vtkIdType num = inputTree->GetNumberOfVertices();
for (vtkIdType j = 0; j < num; ++j)
{
if (curList->LookupValue(j) < 0 && selectedVerticesList->LookupValue(j) < 0)
{
selectedVerticesList->InsertNextValue(j);
}
}
}
else if (node->GetFieldType() == vtkSelectionNode ::EDGE)
{// keep all the other edges
vtkIdType num = inputTree->GetNumberOfEdges();
for (vtkIdType j = 0; j < num; ++j)
{
if (curList->LookupValue(j) < 0 )
{
vtkIdType s = inputTree->GetSourceVertex(j);
vtkIdType t = inputTree->GetTargetVertex(j);
if (selectedVerticesList->LookupValue(s) < 0)
{
selectedVerticesList->InsertNextValue(s);
}
if (selectedVerticesList->LookupValue(t) < 0)
{
selectedVerticesList->InsertNextValue(t);
}
}
}
}
}// end of if(!inverse)
else
{//selection is to be extracted
vtkIdType numTuples = curList->GetNumberOfTuples();
for (vtkIdType j = 0; j < numTuples; ++j)
{
if (node->GetFieldType() == vtkSelectionNode::VERTEX )
{
vtkIdType curVertexId = curList->GetValue(j);
if (selectedVerticesList->LookupValue(curVertexId) < 0)
{
selectedVerticesList->InsertNextValue(curVertexId);
}
}
else if (node->GetFieldType() == vtkSelectionNode::EDGE)
{//if an edge is selected to be extracted,
//keep both source and target vertices
vtkIdType curEdgeId = curList->GetValue(j);
vtkIdType t = inputTree->GetTargetVertex(curEdgeId);
vtkIdType s = inputTree->GetSourceVertex(curEdgeId);
if (selectedVerticesList->LookupValue(s) < 0)
{
selectedVerticesList->InsertNextValue(s);
}
if (selectedVerticesList->LookupValue(t) < 0)
{
selectedVerticesList->InsertNextValue(t);
}
}
}
}
} // end if (curList)
} // end for each selection node
vtkNew<vtkMutableDirectedGraph> builder;
// build the tree recursively
this->BuildTree(inputTree, selectedVerticesList.GetPointer(), builder.GetPointer());
// Copy the structure into the output.
if (!outputTree->CheckedShallowCopy(builder.GetPointer()))
{
vtkErrorMacro( <<"Invalid tree structure." << outputTree->GetNumberOfVertices());
return 0;
}
return 1;
}