File: vtkKdTreeSelector.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 125,776 kB
  • sloc: cpp: 1,539,582; ansic: 106,521; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 122; objc: 83
file content (301 lines) | stat: -rw-r--r-- 8,573 bytes parent folder | download | duplicates (3)
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkKdTreeSelector.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 (c) Sandia Corporation
 See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/

#include "vtkKdTreeSelector.h"

#include "vtkGraph.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkKdTree.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPointSet.h"
#include "vtkSelection.h"
#include "vtkSelectionNode.h"
#include "vtkSmartPointer.h"

vtkStandardNewMacro(vtkKdTreeSelector);

vtkKdTreeSelector::vtkKdTreeSelector()
{
  this->SelectionBounds[0] = 0.0;
  this->SelectionBounds[1] = -1.0;
  this->SelectionBounds[2] = 0.0;
  this->SelectionBounds[3] = -1.0;
  this->SelectionBounds[4] = VTK_DOUBLE_MIN;
  this->SelectionBounds[5] = VTK_DOUBLE_MAX;
  this->KdTree = 0;
  this->BuildKdTreeFromInput = true;
  this->SelectionFieldName = 0;
  this->SingleSelection = false;
  this->SingleSelectionThreshold = 1.0;
  this->SelectionAttribute = -1;
}

vtkKdTreeSelector::~vtkKdTreeSelector()
{
  this->SetKdTree(0);
  this->SetSelectionFieldName(0);
}

void vtkKdTreeSelector::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "KdTree: " << (this->KdTree ? "" : "(null)") << endl;
  if (this->KdTree)
  {
    this->KdTree->PrintSelf(os, indent.GetNextIndent());
  }
  os << indent << "SelectionFieldName: "
    << (this->SelectionFieldName ? this->SelectionFieldName : "(null)") << endl;
  os << indent << "BuildKdTreeFromInput: "
    << (this->BuildKdTreeFromInput ? "on" : "off") << endl;
  os << indent << "SelectionBounds: " << endl;
  os << indent << "  xmin, xmax = (" << this->SelectionBounds[0]
    << "," << this->SelectionBounds[1] << ")" << endl;
  os << indent << "  ymin, ymax = (" << this->SelectionBounds[2]
    << "," << this->SelectionBounds[3] << ")" << endl;
  os << indent << "  zmin, zmax = (" << this->SelectionBounds[4]
    << "," << this->SelectionBounds[5] << ")" << endl;
  os << indent << "SingleSelection: "
    << (this->SingleSelection ? "on" : "off") << endl;
  os << indent << "SingleSelectionThreshold: " << this->SingleSelectionThreshold << endl;
  os << indent << "SelectionAttribute: " << this->SelectionAttribute << endl;
}

void vtkKdTreeSelector::SetKdTree(vtkKdTree* arg)
{
  vtkDebugMacro(<< this->GetClassName() << " (" << this
                << "): setting KdTree to " << arg );
  if (this->KdTree != arg)
  {
    vtkKdTree* tempSGMacroVar = this->KdTree;
    this->KdTree = arg;
    if (this->KdTree != NULL)
    {
      this->BuildKdTreeFromInput = false;
      this->KdTree->Register(this);
    }
    else
    {
      this->BuildKdTreeFromInput = true;
    }
    if (tempSGMacroVar != NULL)
    {
      tempSGMacroVar->UnRegister(this);
    }
    this->Modified();
  }
}

vtkMTimeType vtkKdTreeSelector::GetMTime()
{
  vtkMTimeType mTime = this->Superclass::GetMTime();
  if (this->KdTree != NULL)
  {
    vtkMTimeType time = this->KdTree->GetMTime();
    mTime = (time > mTime ? time : mTime);
  }
  return mTime;
}

int vtkKdTreeSelector::RequestData(
  vtkInformation* vtkNotUsed(request),
  vtkInformationVector** inputVector,
  vtkInformationVector* outputVector)
{
  vtkAbstractArray* field = NULL;
  vtkGraph* graph = NULL;

  if (this->BuildKdTreeFromInput)
  {
    vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
    if (inInfo == NULL)
    {
      vtkErrorMacro("No input, but building kd-tree from input");
      return 0;
    }
    vtkDataObject *input = inInfo->Get(vtkDataObject::DATA_OBJECT());
    if (input == NULL)
    {
      vtkErrorMacro("Input is NULL");
      return 0;
    }
    graph = vtkGraph::SafeDownCast(input);
    vtkPointSet *pointSet = vtkPointSet::SafeDownCast(input);
    if (!graph && !pointSet)
    {
      vtkErrorMacro("Input must be a graph or point set");
      return 0;
    }

    vtkPoints *points = 0;
    if (graph)
    {
      points = graph->GetPoints();
    }
    else
    {
      points = pointSet->GetPoints();
    }

    // If no points, there is nothing to do
    if (points == NULL || points->GetNumberOfPoints() == 0)
    {
      return 1;
    }

    // Construct the kd-tree if we need to
    if (this->KdTree == NULL || this->KdTree->GetMTime() < input->GetMTime())
    {
      if (this->KdTree == NULL)
      {
        this->KdTree = vtkKdTree::New();
      }
      this->KdTree->Initialize();
      this->KdTree->BuildLocatorFromPoints(points);
    }

    // Look for selection field
    if (this->SelectionAttribute == vtkDataSetAttributes::GLOBALIDS ||
        this->SelectionAttribute == vtkDataSetAttributes::PEDIGREEIDS)
    {
      if (graph)
      {
        field = graph->GetVertexData()->GetAbstractAttribute(this->SelectionAttribute);
      }
      else
      {
        field = pointSet->GetPointData()->GetAbstractAttribute(this->SelectionAttribute);
      }
      if (field == NULL)
      {
        vtkErrorMacro("Could not find attribute " << this->SelectionAttribute);
        return 0;
      }
    }
    if (this->SelectionFieldName)
    {
      if (graph)
      {
        field = graph->GetVertexData()->GetAbstractArray(this->SelectionFieldName);
      }
      else
      {
        field = pointSet->GetPointData()->GetAbstractArray(this->SelectionFieldName);
      }
      if (field == NULL)
      {
        vtkErrorMacro("SelectionFieldName field not found");
        return 0;
      }
    }
  }

  // If no kd-tree, there is nothing to do
  if (this->KdTree == NULL)
  {
    return 1;
  }

  // Use the kd-tree to find the selected points
  vtkIdTypeArray* ids = vtkIdTypeArray::New();
  if (this->SingleSelection)
  {
    double center[3];
    for (int c = 0; c < 3; c++)
    {
      center[c] = (this->SelectionBounds[2*c] + this->SelectionBounds[2*c+1]) / 2.0;
    }
    double dist;
    vtkIdType closestToCenter = this->KdTree->FindClosestPoint(center, dist);
    if (dist < this->SingleSelectionThreshold)
    {
      ids->InsertNextValue(closestToCenter);
    }
  }
  else
  {
    this->KdTree->FindPointsInArea(this->SelectionBounds, ids);
  }

  // Fill the selection with the found ids
  vtkSelection* output = vtkSelection::GetData(outputVector);
  vtkSmartPointer<vtkSelectionNode> node =
    vtkSmartPointer<vtkSelectionNode>::New();
  output->AddNode(node);
  if (graph)
  {
    node->SetFieldType(vtkSelectionNode::VERTEX);
  }
  else
  {
    node->SetFieldType(vtkSelectionNode::POINT);
  }
  if (field)
  {
    vtkAbstractArray* arr = vtkAbstractArray::CreateArray(field->GetDataType());
    arr->SetName(field->GetName());
    for (vtkIdType i = 0; i < ids->GetNumberOfTuples(); i++)
    {
      arr->InsertNextTuple(ids->GetValue(i), field);
    }
    if (this->SelectionAttribute == vtkDataSetAttributes::GLOBALIDS ||
        this->SelectionAttribute == vtkDataSetAttributes::PEDIGREEIDS)
    {
      if (this->SelectionAttribute == vtkDataSetAttributes::GLOBALIDS)
      {
        node->SetContentType(vtkSelectionNode::GLOBALIDS);
      }
      else if (this->SelectionAttribute == vtkDataSetAttributes::PEDIGREEIDS)
      {
        node->SetContentType(vtkSelectionNode::PEDIGREEIDS);
      }
    }
    else
    {
      node->SetContentType(vtkSelectionNode::VALUES);
    }
    node->SetSelectionList(arr);
    arr->Delete();
  }
  else
  {
    node->SetContentType(vtkSelectionNode::INDICES);
    node->SetSelectionList(ids);
  }

  // Clean up
  ids->Delete();

  return 1;
}

int vtkKdTreeSelector::FillInputPortInformation(
  int vtkNotUsed(port),
  vtkInformation* info)
{
  // Input (if specified) may be a point set or graph.
  info->Remove(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE());
  info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPointSet");
  info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkGraph");
  info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
  return 1;
}