forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkImageToPoints.cxx
255 lines (216 loc) · 7.41 KB
/
vtkImageToPoints.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkImageToPoints.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 "vtkImageToPoints.h"
#include <vtkObjectFactory.h>
#include <vtkMath.h>
#include <vtkPoints.h>
#include <vtkImageData.h>
#include <vtkPolyData.h>
#include <vtkPointData.h>
#include <vtkImageStencilData.h>
#include <vtkImagePointIterator.h>
#include <vtkInformation.h>
#include <vtkInformationVector.h>
#include <vtkStreamingDemandDrivenPipeline.h>
#include <vtkSmartPointer.h>
vtkStandardNewMacro(vtkImageToPoints);
//----------------------------------------------------------------------------
// Constructor sets default values
vtkImageToPoints::vtkImageToPoints()
{
this->OutputPointsPrecision = vtkAlgorithm::DEFAULT_PRECISION;
this->SetNumberOfInputPorts(2);
this->SetNumberOfOutputPorts(1);
}
//----------------------------------------------------------------------------
vtkImageToPoints::~vtkImageToPoints()
{
}
//----------------------------------------------------------------------------
void vtkImageToPoints::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "OutputPointsPrecision: "
<< this->OutputPointsPrecision << "\n";
}
//----------------------------------------------------------------------------
void vtkImageToPoints::SetStencilConnection(
vtkAlgorithmOutput *stencil)
{
this->SetInputConnection(1, stencil);
}
//----------------------------------------------------------------------------
vtkAlgorithmOutput *vtkImageToPoints::GetStencilConnection()
{
return this->GetInputConnection(1, 0);
}
//----------------------------------------------------------------------------
void vtkImageToPoints::SetStencilData(vtkImageStencilData *stencil)
{
this->SetInputData(1, stencil);
}
//----------------------------------------------------------------------------
int vtkImageToPoints::FillInputPortInformation(
int port, vtkInformation *info)
{
if (port == 0)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkImageData");
}
else if (port == 1)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkImageStencilData");
info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
}
return 1;
}
//----------------------------------------------------------------------------
int vtkImageToPoints::FillOutputPortInformation(
int port, vtkInformation* info)
{
if (port == 0)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkPolyData");
}
return 1;
}
//----------------------------------------------------------------------------
int vtkImageToPoints::RequestInformation(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *vtkNotUsed(outputVector))
{
return 1;
}
//----------------------------------------------------------------------------
int vtkImageToPoints::RequestUpdateExtent(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *vtkNotUsed(outputVector))
{
int inExt[6];
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), inExt);
inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), inExt, 6);
// need to set the stencil update extent to the input extent
if (this->GetNumberOfInputConnections(1) > 0)
{
vtkInformation *stencilInfo = inputVector[1]->GetInformationObject(0);
stencilInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(),
inExt, 6);
}
return 1;
}
namespace {
//----------------------------------------------------------------------------
vtkIdType vtkImageToPointsCount(
vtkImageData *inData, vtkImageStencilData *stencil, const int extent[6])
{
// count the number of points so that we can pre-allocate the space
vtkIdType count = 0;
// iterate over all spans for the stencil
vtkImagePointDataIterator inIter(inData, extent, stencil);
for (; !inIter.IsAtEnd(); inIter.NextSpan())
{
if (inIter.IsInStencil())
{
count += inIter.SpanEndId() - inIter.GetId();
}
}
return count;
}
//----------------------------------------------------------------------------
// The execute method is templated over the point type (float or double)
template<class T>
void vtkImageToPointsExecute(
vtkImageToPoints *self, vtkImageData *inData, const int extent[6],
vtkImageStencilData *stencil, T *outPoints, vtkPointData *outPD)
{
vtkPointData *inPD = inData->GetPointData();
vtkImagePointIterator inIter(inData, extent, stencil, self, 0);
vtkIdType outId = 0;
// iterate over all spans for the stencil
while (!inIter.IsAtEnd())
{
if (inIter.IsInStencil())
{
// if span is inside stencil, generate points
vtkIdType n = inIter.SpanEndId() - inIter.GetId();
outPD->CopyData(inPD, outId, n, inIter.GetId());
outId += n;
for (vtkIdType i = 0; i < n; i++)
{
inIter.GetPosition(outPoints);
outPoints += 3;
inIter.Next();
}
}
else
{
// if span is outside stencil, skip to next span
inIter.NextSpan();
}
}
}
}
//----------------------------------------------------------------------------
int vtkImageToPoints::RequestData(
vtkInformation*,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
// get the input
vtkInformation* info = inputVector[0]->GetInformationObject(0);
vtkInformation *stencilInfo = inputVector[1]->GetInformationObject(0);
vtkImageData *inData = vtkImageData::SafeDownCast(
info->Get(vtkDataObject::DATA_OBJECT()));
// use a stencil, if a stencil is connected
vtkImageStencilData* stencil = 0;
if (stencilInfo)
{
stencil = static_cast<vtkImageStencilData *>(
stencilInfo->Get(vtkDataObject::DATA_OBJECT()));
}
// get the requested precision
int pointsType = VTK_DOUBLE;
if (this->OutputPointsPrecision == vtkAlgorithm::SINGLE_PRECISION)
{
pointsType = VTK_FLOAT;
}
// get the output data object
vtkInformation *outInfo = outputVector->GetInformationObject(0);
vtkPolyData *outData = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
// count the total number of output points
const int *extent = inData->GetExtent();
vtkIdType numPoints = vtkImageToPointsCount(inData, stencil, extent);
// create the points
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
points->SetDataType(pointsType);
points->SetNumberOfPoints(numPoints);
outData->SetPoints(points);
// pre-allocate output arrays
vtkPointData *outPD = outData->GetPointData();
outPD->CopyAllocate(inData->GetPointData(), numPoints);
// iterate over the input and create the point data
void *ptr = points->GetVoidPointer(0);
if (pointsType == VTK_FLOAT)
{
vtkImageToPointsExecute(
this, inData, extent, stencil, static_cast<float *>(ptr), outPD);
}
else
{
vtkImageToPointsExecute(
this, inData, extent, stencil, static_cast<double *>(ptr), outPD);
}
return 1;
}