VMTK
Loading...
Searching...
No Matches
vtkvmtkPolyBallModeller.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkPolyBallModeller.cxx,v $
5Language: C++
6Date: $Date: 2006/01/09 17:01:50 $
7Version: $Revision: 1.4 $
8
9 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
10 See LICENSE file for details.
11
12 Portions of this code are covered under the VTK copyright.
13 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
14 for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notices for more information.
19
20=========================================================================*/
22
23#include "vtkvmtkPolyBall.h"
24#include "vtkvmtkPolyBallLine.h"
25#include "vtkPolyData.h"
26#include "vtkDoubleArray.h"
27#include "vtkImageData.h"
28#include "vtkMath.h"
29#include "vtkvmtkConstants.h"
30#include "vtkObjectFactory.h"
31#include "vtkInformation.h"
32#include "vtkInformationVector.h"
33#include "vtkStreamingDemandDrivenPipeline.h"
34#include "vtkPointData.h"
35#include "vtkVersion.h"
36
37
39
41{
42 this->ReferenceImage = NULL;
43 this->ModelBounds[0] = 0.0;
44 this->ModelBounds[1] = 0.0;
45 this->ModelBounds[2] = 0.0;
46 this->ModelBounds[3] = 0.0;
47 this->ModelBounds[4] = 0.0;
48 this->ModelBounds[5] = 0.0;
49
50 this->SampleDimensions[0] = 50;
51 this->SampleDimensions[1] = 50;
52 this->SampleDimensions[2] = 50;
53
54 this->RadiusArrayName = NULL;
55
56 this->UsePolyBallLine = 0;
57 this->NegateFunction = 0;
58}
59
61{
62 if (this->ReferenceImage)
63 {
64 this->ReferenceImage->Delete();
65 this->ReferenceImage = NULL;
66 }
67
68 if (this->RadiusArrayName)
69 {
70 delete[] this->RadiusArrayName;
71 this->RadiusArrayName = NULL;
72 }
73}
74
76 vtkInformation * vtkNotUsed(request),
77 vtkInformationVector** vtkNotUsed( inputVector ),
78 vtkInformationVector *outputVector)
79{
80 double spacing[3], origin[3];
81
82 vtkInformation* outInfo = outputVector->GetInformationObject(0);
83
84 vtkPolyData* input = vtkPolyData::SafeDownCast(this->GetInput());
85
86 if (this->RadiusArrayName == NULL)
87 {
88 vtkErrorMacro(<<"Error: RadiusArrayName not specified!");
89 return 1;
90 }
91
92 vtkDataArray* radiusArray = input->GetPointData()->GetArray(this->RadiusArrayName);
93 if (radiusArray == NULL)
94 {
95 vtkErrorMacro(<<"Error: RadiusArray with name specified does not exist!");
96 return 1;
97 }
98
99 int extent[6];
100
101 if (this->ReferenceImage)
102 {
103 this->ReferenceImage->GetOrigin(origin);
104 this->ReferenceImage->GetSpacing(spacing);
105 this->ReferenceImage->GetDimensions(this->SampleDimensions);
106 this->ReferenceImage->GetExtent(extent);
107 }
108 else
109 {
110 double maxRadius = radiusArray->GetRange()[1];
111
112 if ((this->ModelBounds[0] >= this->ModelBounds[1]) || (this->ModelBounds[2] >= this->ModelBounds[3]) || (this->ModelBounds[4] >= this->ModelBounds[5]))
113 {
114 double* bounds = input->GetBounds();
115 double maxDist = 2.0 * maxRadius;
116
117 int i;
118 for (i=0; i<3; i++)
119 {
120 this->ModelBounds[2*i] = bounds[2*i] - maxDist;
121 this->ModelBounds[2*i+1] = bounds[2*i+1] + maxDist;
122 }
123 }
124
125 int i;
126 for (i=0; i<3; i++)
127 {
128 origin[i] = this->ModelBounds[2*i];
129 if (this->SampleDimensions[i] <= 1)
130 {
131 spacing[i] = 1.0;
132 }
133 else
134 {
135 spacing[i] = (this->ModelBounds[2*i+1] - this->ModelBounds[2*i]) / (this->SampleDimensions[i] - 1);
136 }
137 }
138
139 extent[0] = 0;
140 extent[1] = this->SampleDimensions[0]-1;
141 extent[2] = 0;
142 extent[3] = this->SampleDimensions[1]-1;
143 extent[4] = 0;
144 extent[5] = this->SampleDimensions[2]-1;
145 }
146
147 outInfo->Set(vtkDataObject::ORIGIN(),origin,3);
148 outInfo->Set(vtkDataObject::SPACING(),spacing,3);
149
150 vtkDataObject::SetPointDataActiveScalarInfo(outInfo, VTK_DOUBLE, 1);
151 //outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),0, this->SampleDimensions[0]-1, 0, this->SampleDimensions[1]-1, 0, this->SampleDimensions[2]-1);
152 outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),extent[0],extent[1],extent[2],extent[3],extent[4],extent[5]);
153
154 return 1;
155}
156
158{
159 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData");
160 return 1;
161}
162
164 vtkInformation *vtkNotUsed(request),
165 vtkInformationVector **inputVector,
166 vtkInformationVector *outputVector)
167{
168 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
169 vtkInformation *outInfo = outputVector->GetInformationObject(0);
170
171 vtkPolyData *input = vtkPolyData::SafeDownCast(
172 inInfo->Get(vtkDataObject::DATA_OBJECT()));
173 vtkImageData *output = vtkImageData::SafeDownCast(
174 outInfo->Get(vtkDataObject::DATA_OBJECT()));
175
176 if (this->RadiusArrayName == NULL)
177 {
178 vtkErrorMacro(<<"Error: RadiusArrayName not specified!");
179 return 1;
180 }
181
182 if (input->GetPointData()->GetArray(this->RadiusArrayName) == NULL)
183 {
184 vtkErrorMacro(<<"Error: RadiusArray with name specified does not exist!");
185 return 1;
186 }
187
188 output->SetExtent(outInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT()));
189 output->AllocateScalars(outInfo);
190
191 vtkDoubleArray *functionArray = vtkDoubleArray::SafeDownCast(output->GetPointData()->GetScalars());
192 functionArray->SetName(this->RadiusArrayName);
193
194 int numberOfOutputPoints = output->GetNumberOfPoints();
195
196#if 0
197 vtkImplicitFunction* function;
198 if (!this->UsePolyBallLine)
199 {
201 polyBall->SetInput(input);
203 function = polyBall;
204 }
205 else
206 {
208 polyBall->SetInput(input);
210 function = polyBall;
211 }
212
213 double point[3];
214 int i;
215 for (i=0; i<numberOfOutputPoints; i++)
216 {
217 output->GetPoint(i,point);
218 double polyBallFunction = function->EvaluateFunction(point);
219 functionArray->SetComponent(i,0,polyBallFunction);
220 }
221
222 function->Delete();
223#endif
224
225 if (!this->UsePolyBallLine)
226 {
227 functionArray->FillComponent(0,VTK_VMTK_LARGE_DOUBLE);
228 vtkDoubleArray* radiusArray = vtkDoubleArray::SafeDownCast(input->GetPointData()->GetArray(this->RadiusArrayName));
229 int numberOfInputPoints = input->GetNumberOfPoints();
230 int extent[6];
231 double x[3], p[3], r;
232 double boundingBox0[3], boundingBox1[3];
233 int ijk0[3], ijk1[3], ijk[3];
234 int ijkExtent[6];
235 double pcoords[3];
236 int inBounds0, inBounds1;
237 double radiusFactor = 2.0;
238 output->GetExtent(extent);
239 double sphereFunctionValue, currentSphereFunctionValue;
240 int n;
241 int i, j, k;
242 vtkIdType outputId;
243 for (n=0; n<numberOfInputPoints; n++)
244 {
245 input->GetPoint(n,p);
246 r = radiusArray->GetValue(n);
247 boundingBox0[0] = p[0] - radiusFactor * r;
248 boundingBox0[1] = p[1] - radiusFactor * r;
249 boundingBox0[2] = p[2] - radiusFactor * r;
250 boundingBox1[0] = p[0] + radiusFactor * r;
251 boundingBox1[1] = p[1] + radiusFactor * r;
252 boundingBox1[2] = p[2] + radiusFactor * r;
253 inBounds0 = output->ComputeStructuredCoordinates(boundingBox0,ijk0,pcoords);
254 inBounds1 = output->ComputeStructuredCoordinates(boundingBox1,ijk1,pcoords);
255 if (!inBounds0 && !inBounds1)
256 {
257 continue;
258 }
259 ijkExtent[0] = ijk0[0] < extent[0] ? extent[0] : ijk0[0];
260 ijkExtent[1] = ijk1[0] > extent[1] ? extent[1] : ijk1[0];
261 ijkExtent[2] = ijk0[1] < extent[2] ? extent[2] : ijk0[1];
262 ijkExtent[3] = ijk1[1] > extent[3] ? extent[3] : ijk1[1];
263 ijkExtent[4] = ijk0[2] < extent[4] ? extent[4] : ijk0[2];
264 ijkExtent[5] = ijk1[2] > extent[5] ? extent[5] : ijk1[2];
265 for (k=ijkExtent[4]; k<=ijkExtent[5]; k++)
266 {
267 for (j=ijkExtent[2]; j<=ijkExtent[3]; j++)
268 {
269 for (i=ijkExtent[0]; i<=ijkExtent[1]; i++)
270 {
271 ijk[0] = i;
272 ijk[1] = j;
273 ijk[2] = k;
274 outputId = output->ComputePointId(ijk);
275 output->GetPoint(outputId,x);
276 sphereFunctionValue = ((x[0] - p[0]) * (x[0] - p[0]) + (x[1] - p[1]) * (x[1] - p[1]) + (x[2] - p[2]) * (x[2] - p[2])) - r * r;
277 currentSphereFunctionValue = functionArray->GetComponent(outputId,0);
278 if (sphereFunctionValue < currentSphereFunctionValue)
279 {
280 functionArray->SetComponent(outputId,0,sphereFunctionValue);
281 }
282 }
283 }
284 }
285 }
286 }
287 else
288 {
290 polyBallLine->SetInput(input);
291 polyBallLine->SetPolyBallRadiusArrayName(this->RadiusArrayName);
292 double point[3];
293 int i;
294 for (i=0; i<numberOfOutputPoints; i++)
295 {
296 output->GetPoint(i,point);
297 double polyBallLineValue = polyBallLine->EvaluateFunction(point);
298 functionArray->SetComponent(i,0,polyBallLineValue);
299 }
300 }
301
302 if (this->NegateFunction)
303 {
304 int i;
305 for (i=0; i<numberOfOutputPoints; i++)
306 {
307 double functionValue = functionArray->GetComponent(i,0);
308 functionValue *= -1.0;
309 functionArray->SetComponent(i,0,functionValue);
310 }
311 }
312
313 return 1;
314}
315
316void vtkvmtkPolyBallModeller::PrintSelf(std::ostream& os, vtkIndent indent)
317{
318 this->Superclass::PrintSelf(os,indent);
319
320 os << indent << "Sample Dimensions: (" << this->SampleDimensions[0] << ", "
321 << this->SampleDimensions[1] << ", "
322 << this->SampleDimensions[2] << ")\n";
323
324 os << indent << "ModelBounds: \n";
325 os << indent << " Xmin,Xmax: (" << this->ModelBounds[0] << ", " << this->ModelBounds[1] << ")\n";
326 os << indent << " Ymin,Ymax: (" << this->ModelBounds[2] << ", " << this->ModelBounds[3] << ")\n";
327 os << indent << " Zmin,Zmax: (" << this->ModelBounds[4] << ", " << this->ModelBounds[5] << ")\n";
328
329}
Implements vtkImplicitFunction to evaluate the minimum sphere function for an envelope created by an ...
virtual void SetInput(vtkPolyData *)
virtual void SetPolyBallRadiusArrayName(const char *)
static vtkvmtkPolyBallLine * New()
double EvaluateFunction(double x[3]) override
Create an image where a polyball or polyball line is evaluated as a function.
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
virtual int RequestInformation(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void PrintSelf(std::ostream &os, vtkIndent indent) override
int FillInputPortInformation(int, vtkInformation *info) override
Implements vtkImplicitFunction to evaluate the minimum sphere function from a collection of points wi...
virtual void SetPolyBallRadiusArrayName(const char *)
static vtkvmtkPolyBall * New()
virtual void SetInput(vtkPolyData *)
#define VTK_VMTK_LARGE_DOUBLE
vtkStandardNewMacro(vtkvmtkPolyBallModeller)