VMTK
Loading...
Searching...
No Matches
vtkvmtkPolyDataPotentialFit.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkPolyDataPotentialFit.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:48:25 $
7Version: $Revision: 1.3 $
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 Portions of this code are covered under the ITK copyright.
17 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm
18 for details.
19
20 This software is distributed WITHOUT ANY WARRANTY; without even
21 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
22 PURPOSE. See the above copyright notices for more information.
23
24=========================================================================*/
25
26
28
30#include "vtkvmtkConstants.h"
31#include "vtkMath.h"
32#include "vtkPolyData.h"
33#include "vtkImageData.h"
34#include "vtkImageGradient.h"
35#include "vtkDoubleArray.h"
36#include "vtkVoxel.h"
37#include "vtkPixel.h"
38#include "vtkPointData.h"
39#include "vtkCellData.h"
40#include "vtkPolyDataNormals.h"
41#include "vtkInformation.h"
42#include "vtkInformationVector.h"
43#include "vtkObjectFactory.h"
44#include "vtkVersion.h"
45
46
48
50{
51 this->PotentialImage = NULL;
52 this->InflationImage = NULL;
53 this->PotentialGradientImage = NULL;
55 this->TimeStep = 0.0;
56 this->MaxDisplacementNorm = 0.0;
57 this->MinEdgeLength = 0.0;
58 this->Relaxation = 1.0;
59 this->Displacements = NULL;
60 this->MinPotentialSpacing = 0.0;
61 this->Convergence = 1E-1;
62 this->PotentialWeight = 0.0;
63 this->StiffnessWeight = 0.0;
64 this->InflationWeight = 0.0;
65 this->PotentialMaxNorm = 0.0;
67 this->InflationThreshold = 0.0;
68
69 this->MaxTimeStep = 1.0; // pixels per time step
70 this->AdaptiveTimeStep = 1;
71
72 this->FlipNormals = 0;
73 this->Dimensionality = 3;
74
77
78 this->Neighborhoods = NULL;
79}
80
82{
83 if (this->PotentialImage)
84 {
85 this->PotentialImage->Delete();
86 this->PotentialImage = NULL;
87 }
88
89 if (this->InflationImage)
90 {
91 this->InflationImage->Delete();
92 this->InflationImage = NULL;
93 }
94
95 if (this->PotentialGradientImage)
96 {
97 this->PotentialGradientImage->Delete();
98 this->PotentialGradientImage = NULL;
99 }
100
101 if (this->Displacements)
102 {
103 this->Displacements->Delete();
104 this->Displacements = NULL;
105 }
106
107 if (this->Neighborhoods)
108 {
109 this->Neighborhoods->Delete();
110 this->Neighborhoods = NULL;
111 }
112}
113
116
117void vtkvmtkPolyDataPotentialFit::EvaluateForce(double point[3], double force[3], bool normalize)
118{
119 int ijk[3];
120 double pcoords[3];
121 double weights[8];
122 int inBounds;
123 int i;
124
125 force[0] = force[1] = force[2] = 0.0;
126
127 inBounds = this->PotentialGradientImage->ComputeStructuredCoordinates(point,ijk,pcoords);
128
129 if (!inBounds)
130// if (!inBounds || !this->IsCellInExtent(this->PotentialGradientImage->GetExtent(),ijk,0))
131 {
132 vtkWarningMacro("Point out of extent.");
133 return;
134 }
135
136 vtkCell* cell = this->PotentialGradientImage->GetCell(this->PotentialGradientImage->ComputeCellId(ijk));
137
138 if (cell->GetCellType() == VTK_VOXEL)
139 {
140 vtkVoxel::InterpolationFunctions(pcoords,weights);
141 }
142 else if (cell->GetCellType() == VTK_PIXEL)
143 {
144 vtkPixel::InterpolationFunctions(pcoords,weights);
145 }
146 else
147 {
148 vtkErrorMacro("Non voxel or pixel cell found in PotentialImage");
149 }
150
151 int numberOfCellPoints = cell->GetNumberOfPoints();
152
153 vtkDataArray* gradientVectorsArray = this->PotentialGradientImage->GetPointData()->GetScalars();
154
155 for (i=0; i<numberOfCellPoints; i++)
156 {
157 double vectorValue[3];
158 gradientVectorsArray->GetTuple(cell->GetPointId(i),vectorValue);
159 force[0] += weights[i] * vectorValue[0];
160 force[1] += weights[i] * vectorValue[1];
161 force[2] += weights[i] * vectorValue[2];
162 }
163
164 if (normalize && this->PotentialMaxNorm > VTK_VMTK_DOUBLE_TOL)
165 {
166 force[0] /= this->PotentialMaxNorm;
167 force[1] /= this->PotentialMaxNorm;
168 force[2] /= this->PotentialMaxNorm;
169 }
170
171 force[0] *= -1.0;
172 force[1] *= -1.0;
173 force[2] *= -1.0;
174}
175
177{
178 int ijk[3];
179 double pcoords[3];
180 double weights[8];
181 int inBounds;
182 int i;
183
184 double potential = 0.0;
185
186 inBounds = this->PotentialImage->ComputeStructuredCoordinates(point,ijk,pcoords);
187
188 if (!inBounds)
189// if (!inBounds || !this->IsCellInExtent(this->PotentialImage->GetExtent(),ijk,0))
190 {
191 vtkWarningMacro("Point out of extent.");
192 return 0.0;
193 }
194
195 vtkCell* cell = this->PotentialImage->GetCell(this->PotentialImage->ComputeCellId(ijk));
196
197 if (cell->GetCellType() == VTK_VOXEL)
198 {
199 vtkVoxel::InterpolationFunctions(pcoords,weights);
200 }
201 else if (cell->GetCellType() == VTK_PIXEL)
202 {
203 vtkPixel::InterpolationFunctions(pcoords,weights);
204 }
205 else
206 {
207 vtkErrorMacro("Non voxel or pixel cell found in PotentialImage");
208 return 0.0;
209 }
210
211 int numberOfCellPoints = cell->GetNumberOfPoints();
212
213 vtkDataArray* potentialArray = this->PotentialImage->GetPointData()->GetScalars();
214
215 for (i=0; i<numberOfCellPoints; i++)
216 {
217 double value = potentialArray->GetTuple1(cell->GetPointId(i));
218 potential += weights[i] * value;
219 }
220
221 return potential;
222}
223
225{
226 if (this->InflationImage == NULL) {
227 return 1.0;
228 }
229
230 int ijk[3];
231 double pcoords[3];
232 double weights[8];
233 int inBounds;
234 int i;
235
236 double inflation = 0.0;
237
238 inBounds = this->InflationImage->ComputeStructuredCoordinates(point,ijk,pcoords);
239
240 if (!inBounds)
241// if (!inBounds || !this->IsCellInExtent(this->PotentialImage->GetExtent(),ijk,0))
242 {
243 vtkWarningMacro("Point out of extent.");
244 return 0.0;
245 }
246
247 vtkCell* cell = this->InflationImage->GetCell(this->InflationImage->ComputeCellId(ijk));
248
249 if (cell->GetCellType() == VTK_VOXEL)
250 {
251 vtkVoxel::InterpolationFunctions(pcoords,weights);
252 }
253 else if (cell->GetCellType() == VTK_PIXEL)
254 {
255 vtkPixel::InterpolationFunctions(pcoords,weights);
256 }
257 else
258 {
259 vtkErrorMacro("Non voxel or pixel cell found in PotentialImage");
260 return 0.0;
261 }
262
263 int numberOfCellPoints = cell->GetNumberOfPoints();
264
265 vtkDataArray* inflationArray = this->InflationImage->GetPointData()->GetScalars();
266
267 for (i=0; i<numberOfCellPoints; i++)
268 {
269 double value = inflationArray->GetTuple1(cell->GetPointId(i));
270 inflation += weights[i] * value;
271 }
272
273 return inflation - this->InflationThreshold;
274}
275
276void vtkvmtkPolyDataPotentialFit::ComputePotentialDisplacement(vtkIdType pointId, double potentialDisplacement[3])
277{
278 double point[3];
279 double force[3];
280 this->GetOutput()->GetPoint(pointId, point);
281 this->EvaluateForce(point, force);
282 potentialDisplacement[0] = force[0];
283 potentialDisplacement[1] = force[1];
284 potentialDisplacement[2] = force[2];
285}
286
287void vtkvmtkPolyDataPotentialFit::ComputeStiffnessDisplacement(vtkIdType pointId, double stiffnessDisplacement[3])
288{
289 vtkIdType j;
290 double point[3];
291 double laplacianPoint[3], neighborhoodPoint[3];
292 vtkIdType numberOfNeighborhoodPoints;
293 vtkvmtkNeighborhood *neighborhood;
294 vtkPolyData *output;
295
296 output = this->GetOutput();
297 neighborhood = this->Neighborhoods->GetNeighborhood(pointId);
298
299 output->GetPoint(pointId,point);
300
301 stiffnessDisplacement[0] = stiffnessDisplacement[1] = stiffnessDisplacement[2] = 0.0;
302
303// double weight;
304// double weightSum;
305// weightSum = 0.0;
306
307 laplacianPoint[0] = laplacianPoint[1] = laplacianPoint[2] = 0.0;
308 numberOfNeighborhoodPoints = neighborhood->GetNumberOfPoints();
309 for (j=0; j<numberOfNeighborhoodPoints; j++)
310 {
311 output->GetPoint(neighborhood->GetPointId(j),neighborhoodPoint);
312
313 laplacianPoint[0] += neighborhoodPoint[0];
314 laplacianPoint[1] += neighborhoodPoint[1];
315 laplacianPoint[2] += neighborhoodPoint[2];
316
317// weight = fabs(neighborhoodPoint[2] - point[2]);
318// laplacianPoint[0] += weight * neighborhoodPoint[0];
319// laplacianPoint[1] += weight * neighborhoodPoint[1];
320// laplacianPoint[2] += weight * neighborhoodPoint[2];
321// weightSum += weight;
322 }
323 laplacianPoint[0] /= (double)numberOfNeighborhoodPoints;
324 laplacianPoint[1] /= (double)numberOfNeighborhoodPoints;
325 laplacianPoint[2] /= (double)numberOfNeighborhoodPoints;
326// laplacianPoint[0] /= weightSum;
327// laplacianPoint[1] /= weightSum;
328// laplacianPoint[2] /= weightSum;
329
330 stiffnessDisplacement[0] = laplacianPoint[0] - point[0];
331 stiffnessDisplacement[1] = laplacianPoint[1] - point[1];
332 stiffnessDisplacement[2] = laplacianPoint[2] - point[2];
333}
334
335void vtkvmtkPolyDataPotentialFit::ComputeInflationDisplacement(vtkIdType pointId, double inflationDisplacement[3])
336{
337 vtkIdType j;
338 double point[3], inputOutwardNormal[3];
339 double neighborhoodVector[3], firstNeighborhoodVector[3];
340 double cross[3], neighborhoodNormal[3];
341 double neighborhoodPoint[3];
342 double dot;
343 vtkIdType numberOfNeighborhoodPoints;
344 vtkvmtkNeighborhood *neighborhood;
345 vtkPolyData *output;
346
347 output = this->GetOutput();
348 neighborhood = this->Neighborhoods->GetNeighborhood(pointId);
349
350 if (neighborhood->GetNumberOfPoints() == 0)
351 {
352 inflationDisplacement[0] = 0.0;
353 inflationDisplacement[1] = 0.0;
354 inflationDisplacement[2] = 0.0;
355 return;
356 }
357
358 output->GetPoint(pointId,point);
359 this->Normals->GetTuple(pointId,inputOutwardNormal);
360
361 double inflation = this->EvaluateInflation(point);
362 double potential = 1.0;
363 if (this->UsePotentialInInflation) {
364 potential = this->EvaluatePotential(point);
365 }
366
367 neighborhoodNormal[0] = 0.0;
368 neighborhoodNormal[1] = 0.0;
369 neighborhoodNormal[2] = 0.0;
370
371 output->GetPoint(neighborhood->GetPointId(0),neighborhoodPoint);
372 firstNeighborhoodVector[0] = neighborhoodPoint[0] - point[0];
373 firstNeighborhoodVector[1] = neighborhoodPoint[1] - point[1];
374 firstNeighborhoodVector[2] = neighborhoodPoint[2] - point[2];
375
376 numberOfNeighborhoodPoints = neighborhood->GetNumberOfPoints();
377 for (j=1; j<numberOfNeighborhoodPoints; j++)
378 {
379 output->GetPoint(neighborhood->GetPointId(j),neighborhoodPoint);
380 neighborhoodVector[0] = neighborhoodPoint[0] - point[0];
381 neighborhoodVector[1] = neighborhoodPoint[1] - point[1];
382 neighborhoodVector[2] = neighborhoodPoint[2] - point[2];
383 vtkMath::Cross(firstNeighborhoodVector,neighborhoodVector,cross);
384 neighborhoodNormal[0] += cross[0];
385 neighborhoodNormal[1] += cross[1];
386 neighborhoodNormal[2] += cross[2];
387 }
388 vtkMath::Normalize(neighborhoodNormal);
389
390 dot = vtkMath::Dot(inputOutwardNormal,neighborhoodNormal);
391
392 if (dot < 0.0)
393 {
394 neighborhoodNormal[0] *= -1.0;
395 neighborhoodNormal[1] *= -1.0;
396 neighborhoodNormal[2] *= -1.0;
397 }
398
399 inflationDisplacement[0] = inflation * potential * neighborhoodNormal[0];
400 inflationDisplacement[1] = inflation * potential * neighborhoodNormal[1];
401 inflationDisplacement[2] = inflation * potential * neighborhoodNormal[2];
402}
403
404void vtkvmtkPolyDataPotentialFit::ComputeDisplacements(bool potential, bool stiffness, bool inflation)
405{
406 vtkIdType i;
407 vtkIdType numberOfPoints;
408 double displacement[3];
409 double potentialDisplacement[3];
410 double stiffnessDisplacement[3];
411 double inflationDisplacement[3];
412 double displacementNorm;
413
414 numberOfPoints = this->GetOutput()->GetNumberOfPoints();
415
416 this->MaxDisplacementNorm = 0.0;
417
418 for (i=0; i<numberOfPoints; i++)
419 {
420 displacement[0] = displacement[1] = displacement[2] = 0.0;
421
422 if (potential)
423 {
424 this->ComputePotentialDisplacement(i, potentialDisplacement);
425 displacement[0] += this->PotentialWeight * potentialDisplacement[0];
426 displacement[1] += this->PotentialWeight * potentialDisplacement[1];
427 displacement[2] += this->PotentialWeight * potentialDisplacement[2];
428 }
429
430 if (stiffness)
431 {
432 this->ComputeStiffnessDisplacement(i, stiffnessDisplacement);
433 displacement[0] += this->StiffnessWeight * stiffnessDisplacement[0];
434 displacement[1] += this->StiffnessWeight * stiffnessDisplacement[1];
435 displacement[2] += this->StiffnessWeight * stiffnessDisplacement[2];
436 }
437
438 if (inflation)
439 {
440 this->ComputeInflationDisplacement(i, inflationDisplacement);
441 displacement[0] += this->InflationWeight * inflationDisplacement[0];
442 displacement[1] += this->InflationWeight * inflationDisplacement[1];
443 displacement[2] += this->InflationWeight * inflationDisplacement[2];
444 }
445
446 this->Displacements->SetTuple(i, displacement);
447 displacementNorm = vtkMath::Norm(displacement);
448
449 if (displacementNorm > this->MaxDisplacementNorm)
450 {
451 this->MaxDisplacementNorm = displacementNorm;
452 }
453 }
454}
455
457{
458 if (!this->AdaptiveTimeStep)
459 {
460 return;
461 }
462
464 {
466 if (this->TimeStep > this->MaxTimeStep)
467 {
468 this->TimeStep = this->MaxTimeStep;
469 }
470 }
471 else
472 {
473 this->TimeStep = this->MaxTimeStep;
474 }
475}
476
478{
479 vtkIdType i;
480 vtkIdType numberOfPoints;
481 double point[3], displacement[3], newPoint[3];
482 vtkPoints *points;
483
484 points = this->GetOutput()->GetPoints();
485 numberOfPoints = points->GetNumberOfPoints();
486
487 for (i=0; i<numberOfPoints; i++)
488 {
489 points->GetPoint(i,point);
490 this->Displacements->GetTuple(i,displacement);
491 newPoint[0] = point[0] + this->Relaxation * this->TimeStep * displacement[0];
492 newPoint[1] = point[1] + this->Relaxation * this->TimeStep * displacement[1];
493 newPoint[2] = point[2] + this->Relaxation * this->TimeStep * displacement[2];
494 points->SetPoint(i,newPoint);
495 }
496}
497
499{
500 double minSpacing;
501 minSpacing = (spacing[0] < spacing[1]) && (spacing[0] < spacing[2]) ? spacing[0] : (spacing[1] < spacing[2]) ? spacing[1] : spacing[2];
502 return minSpacing;
503}
504
506{
507 return this->TimeStep * this->MaxDisplacementNorm < this->Convergence ? 1 : 0;
508}
509
511 vtkInformation *vtkNotUsed(request),
512 vtkInformationVector **inputVector,
513 vtkInformationVector *outputVector)
514{
515 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
516 vtkInformation *outInfo = outputVector->GetInformationObject(0);
517
518 vtkPolyData *input = vtkPolyData::SafeDownCast(
519 inInfo->Get(vtkDataObject::DATA_OBJECT()));
520 vtkPolyData *output = vtkPolyData::SafeDownCast(
521 outInfo->Get(vtkDataObject::DATA_OBJECT()));
522
523 bool potential, stiffness, inflation;
524 vtkIdType n, m;
525 vtkIdType numberOfPoints;
526 vtkPoints *newPoints;
527
528 numberOfPoints = input->GetNumberOfPoints();
529
530 newPoints = vtkPoints::New();
531 newPoints->DeepCopy(input->GetPoints());
532
533 if (this->Displacements)
534 {
535 this->Displacements->Delete();
536 this->Displacements = NULL;
537 }
538
539 this->Displacements = vtkDoubleArray::New();
540 this->Displacements->SetNumberOfComponents(3);
541 this->Displacements->SetNumberOfTuples(numberOfPoints);
542
543 vtkImageGradient* gradientFilter = vtkImageGradient::New();
544 gradientFilter->SetInputData(this->PotentialImage);
545 gradientFilter->SetDimensionality(this->Dimensionality);
546 gradientFilter->Update();
547
548 if (this->PotentialGradientImage)
549 {
550 this->PotentialGradientImage->Delete();
551 this->PotentialGradientImage = NULL;
552 }
553
554 this->PotentialGradientImage = vtkImageData::New();
555 this->PotentialGradientImage->DeepCopy(gradientFilter->GetOutput());
556
557 gradientFilter->Delete();
558
559 output->SetPoints(newPoints);
560 output->SetVerts(input->GetVerts());
561 output->SetLines(input->GetLines());
562 output->SetPolys(input->GetPolys());
563 output->SetStrips(input->GetStrips());
564 output->GetPointData()->PassData(input->GetPointData());
565 output->GetCellData()->PassData(input->GetCellData());
566
567 newPoints->Delete();
568
569 if (this->Neighborhoods)
570 {
571 this->Neighborhoods->Delete();
572 this->Neighborhoods = NULL;
573 }
574
576 this->Neighborhoods->SetNeighborhoodTypeToPolyDataManifoldNeighborhood();
577 this->Neighborhoods->SetDataSet(input);
578 this->Neighborhoods->Build();
579
580 vtkPolyDataNormals* surfaceNormals = vtkPolyDataNormals::New();
581 surfaceNormals->SetInputData(input);
582 surfaceNormals->SplittingOff();
583 surfaceNormals->AutoOrientNormalsOn();
584 surfaceNormals->SetFlipNormals(this->FlipNormals);
585 surfaceNormals->ComputePointNormalsOn();
586 surfaceNormals->ConsistencyOn();
587 surfaceNormals->Update();
588
589 this->Normals = surfaceNormals->GetOutput()->GetPointData()->GetNormals();
590
591 this->MinPotentialSpacing = this->ComputeMinSpacing(this->PotentialImage->GetSpacing());
592
593 this->PotentialMaxNorm = this->PotentialGradientImage->GetPointData()->GetScalars()->GetMaxNorm();
594
595 for (n=0; n<this->NumberOfIterations; n++)
596 {
597 for (m=0; m<this->NumberOfStiffnessSubIterations; m++)
598 {
599 potential = false;
600 stiffness = true;
601 inflation = false;
602 this->ComputeDisplacements(potential, stiffness, inflation);
603 this->ComputeTimeStep();
604 this->ApplyDisplacements();
605 }
606
607 for (m=0; m<this->NumberOfInflationSubIterations; m++)
608 {
609 potential = false;
610 stiffness = false;
611 inflation = true;
612 this->ComputeDisplacements(potential, stiffness, inflation);
613 this->ComputeTimeStep();
614 this->ApplyDisplacements();
615 }
616
617 potential = true;
618 stiffness = true;
619 inflation = true;
620 this->ComputeDisplacements(potential, stiffness, inflation);
621 this->ComputeTimeStep();
622 this->ApplyDisplacements();
623
624 if (this->TestConvergence())
625 {
626 break;
627 }
628 }
629
630 surfaceNormals->Delete();
631 this->Normals = NULL;
632
633 return 1;
634}
635
636void vtkvmtkPolyDataPotentialFit::PrintSelf(std::ostream& os, vtkIndent indent)
637{
638 this->Superclass::PrintSelf(os,indent);
639}
640
Query the neighborhood surrounding the set point id in an data set item.
vtkIdType GetPointId(vtkIdType i)
static vtkvmtkNeighborhoods * New()
Create an explicitly deformable model which evolves a surface to gradient magnitudes of an input imag...
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
static double ComputeMinSpacing(double spacing[3])
void ComputeInflationDisplacement(vtkIdType pointId, double inflationDisplacement[3])
void ComputeDisplacements(bool potential, bool stiffness, bool inflation)
void ComputePotentialDisplacement(vtkIdType pointId, double potentialDisplacement[3])
void ComputeStiffnessDisplacement(vtkIdType pointId, double stiffnessDisplacement[3])
void PrintSelf(std::ostream &os, vtkIndent indent) override
void EvaluateForce(double point[3], double force[3], bool normalize=true)
#define VTK_VMTK_LARGE_INTEGER
#define VTK_VMTK_DOUBLE_TOL
vtkCxxSetObjectMacro(vtkvmtkPolyDataPotentialFit, PotentialImage, vtkImageData)
vtkStandardNewMacro(vtkvmtkPolyDataPotentialFit)