forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkVoxelGrid.cxx
299 lines (254 loc) · 9.15 KB
/
vtkVoxelGrid.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkVoxelGrid.cxx
Copyright (c) Kitware, Inc.
All rights reserved.
See LICENSE file 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 "vtkVoxelGrid.h"
#include "vtkObjectFactory.h"
#include "vtkDoubleArray.h"
#include "vtkPointSet.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkStaticPointLocator.h"
#include "vtkLinearKernel.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkSMPTools.h"
#include "vtkSMPThreadLocalObject.h"
#include "vtkArrayListTemplate.h" // For processing attribute data
#include <vector>
vtkStandardNewMacro(vtkVoxelGrid);
vtkCxxSetObjectMacro(vtkVoxelGrid,Kernel,vtkInterpolationKernel);
//----------------------------------------------------------------------------
// Helper classes to support efficient computing, and threaded execution.
namespace {
//----------------------------------------------------------------------------
// The threaded core of the algorithm (first pass)
template <typename T>
struct Subsample
{
T *InPoints;
vtkStaticPointLocator *Locator;
vtkInterpolationKernel *Kernel;
const vtkIdType *BinMap;
ArrayList Arrays;
T *OutPoints;
// Don't want to allocate working arrays on every thread invocation. Thread local
// storage prevents lots of new/delete.
vtkSMPThreadLocalObject<vtkIdList> PIds;
vtkSMPThreadLocalObject<vtkDoubleArray> Weights;
Subsample(T* inPts, vtkPointData *inPD, vtkPointData *outPD,
vtkStaticPointLocator *loc, vtkInterpolationKernel *k,
vtkIdType numOutPts, vtkIdType *binMap, T *outPts) :
InPoints(inPts), Locator(loc), Kernel(k), BinMap(binMap), OutPoints(outPts)
{
this->Arrays.AddArrays(numOutPts, inPD, outPD);
}
// Just allocate a little bit of memory to get started.
void Initialize()
{
vtkIdList*& pIds = this->PIds.Local();
pIds->Allocate(128); //allocate some memory
vtkDoubleArray*& weights = this->Weights.Local();
weights->Allocate(128);
}
void operator() (vtkIdType pointId, vtkIdType endPointId)
{
T *px;
T *py = this->OutPoints + 3*pointId;
const vtkIdType *map = this->BinMap;
vtkIdList*& pIds = this->PIds.Local();
vtkIdType numWeights;
vtkDoubleArray*& weights = this->Weights.Local();
double y[3], count;
vtkIdType numIds, id;
vtkStaticPointLocator *loc = this->Locator;
for ( ; pointId < endPointId; ++pointId )
{
vtkIdType binId = map[pointId];
y[0] = y[1] = y[2] = 0.0;
loc->GetBucketIds(binId,pIds);
numIds = pIds->GetNumberOfIds();
for (id=0; id < numIds; ++id)
{
px = this->InPoints + 3*pIds->GetId(id);
y[0] += *px++;
y[1] += *px++;
y[2] += *px;
}
count = static_cast<double>(numIds);
y[0] /= count;
y[1] /= count;
y[2] /= count;
*py++ = y[0];
*py++ = y[1];
*py++ = y[2];
// Now interpolate attributes
numWeights = this->Kernel->ComputeWeights(y, pIds, weights);
this->Arrays.Interpolate(numWeights, pIds->GetPointer(0),
weights->GetPointer(0), pointId);
}//for all output points in this batch
}
void Reduce()
{
}
static void Execute(T *inPts, vtkPointData *inPD, vtkPointData *outPD,
vtkStaticPointLocator *loc, vtkInterpolationKernel *k,
vtkIdType numOutPts, vtkIdType *binMap, T *outPts)
{
Subsample subsample(inPts, inPD, outPD, loc, k, numOutPts, binMap, outPts);
vtkSMPTools::For(0, numOutPts, subsample);
}
}; //Subsample
} //anonymous namespace
//================= Begin class proper =======================================
//----------------------------------------------------------------------------
vtkVoxelGrid::vtkVoxelGrid()
{
this->Locator = vtkStaticPointLocator::New();
this->ConfigurationStyle = vtkVoxelGrid::AUTOMATIC;
this->Divisions[0] = this->Divisions[1] = this->Divisions[2] = 50;
this->LeafSize[0] = this->LeafSize[1] = this->LeafSize[2] = 1.0;
this->NumberOfPointsPerBin = 10;
this->Kernel = vtkLinearKernel::New();
}
//----------------------------------------------------------------------------
vtkVoxelGrid::~vtkVoxelGrid()
{
this->Locator->UnRegister(this);
this->Locator = nullptr;
this->SetKernel(nullptr);
}
//----------------------------------------------------------------------------
// Produce the output data
int vtkVoxelGrid::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the input and output
vtkPointSet *input = vtkPointSet::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
// Check the input
if ( !input || !output )
{
return 1;
}
vtkIdType numPts = input->GetNumberOfPoints();
if ( numPts < 1 )
{
return 1;
}
// Make sure there is a kernel
if ( !this->Kernel )
{
vtkErrorMacro(<<"Interpolation kernel required\n");
return 1;
}
bool valid = 1;
if ( this->LeafSize[0] <= 0.0 || this->LeafSize[1] <= 0.0 || this->LeafSize[2] <= 0.0 ||
this->Divisions[0] < 1 || this->Divisions[1] < 1 || this->Divisions[2] < 1 )
{
valid = false;
}
// Configure and build the locator
if ( valid && this->ConfigurationStyle == vtkVoxelGrid::MANUAL )
{
this->Locator->AutomaticOff();
this->Locator->SetDivisions(this->Divisions);
}
else if ( valid && this->ConfigurationStyle == vtkVoxelGrid::SPECIFY_LEAF_SIZE )
{
double bounds[6];
int divs[3];
this->Locator->AutomaticOff();
input->GetBounds(bounds);
divs[0] = (bounds[1]-bounds[0]) / this->LeafSize[0];
divs[1] = (bounds[3]-bounds[2]) / this->LeafSize[1];
divs[2] = (bounds[5]-bounds[4]) / this->LeafSize[2];
this->Locator->SetDivisions(divs);
}
else // this->ConfigurationStyle == vtkVoxelGrid::AUTOMATIC
{
this->Locator->AutomaticOn();
this->Locator->SetNumberOfPointsPerBucket(this->NumberOfPointsPerBin);
}
this->Locator->SetDataSet(input);
this->Locator->BuildLocator();
this->Locator->GetDivisions(this->Divisions);
// Run through the locator and compute the number of output points,
// and build a map of the bin number to output point. This is a prefix sum.
vtkIdType numOutPts=0;
vtkIdType binNum, numBins = this->Locator->GetNumberOfBuckets();
std::vector<vtkIdType> binMap;
for ( binNum=0; binNum < numBins; ++binNum )
{
if ( this->Locator->GetNumberOfPointsInBucket(binNum) > 0 )
{
binMap.push_back(binNum);
++numOutPts;
}
}
// Grab the point data for interpolation
vtkPointData *inPD = input->GetPointData();
vtkPointData *outPD = output->GetPointData();
outPD->InterpolateAllocate(inPD,numOutPts);
// Finally run over all of the bins, and those that are not empty are
// processed. The processing consists of averaging all of the points found
// in the bin, and setting the average point position in the output points.
vtkPoints *points = input->GetPoints()->NewInstance();
points->SetDataType(input->GetPoints()->GetDataType());
points->SetNumberOfPoints(numOutPts);
output->SetPoints(points);
void *inPtr = input->GetPoints()->GetVoidPointer(0);
void *outPtr = output->GetPoints()->GetVoidPointer(0);
switch (output->GetPoints()->GetDataType())
{
vtkTemplateMacro(Subsample<VTK_TT>::Execute((VTK_TT *)inPtr, inPD, outPD,
this->Locator, this->Kernel, numOutPts, &binMap[0], (VTK_TT *)outPtr));
}
// Send attributes to output
int numPtArrays = input->GetPointData()->GetNumberOfArrays();
for (int i=0; i<numPtArrays; ++i)
{
output->GetPointData()->AddArray(input->GetPointData()->GetArray(i));
}
// Clean up. The locator needs to be reset.
this->Locator->Initialize();
points->Delete();
return 1;
}
//----------------------------------------------------------------------------
int vtkVoxelGrid::
FillInputPortInformation(int, vtkInformation *info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPointSet");
return 1;
}
//----------------------------------------------------------------------------
void vtkVoxelGrid::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Configuration Style: " << this->ConfigurationStyle << endl;
os << indent << "Divisions: ("
<< this->Divisions[0] << ","
<< this->Divisions[1] << ","
<< this->Divisions[2] << ")\n";
os << indent << "Leaf Size: ("
<< this->LeafSize[0] << ","
<< this->LeafSize[1] << ","
<< this->LeafSize[2] << ")\n";
os << indent << "Number of Points Per Bin: "
<< this->NumberOfPointsPerBin << endl;
}