VMTK
Loading...
Searching...
No Matches
vtkvmtkPolyDataDistanceToSpheres.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: VMTK
4 Module: $RCSfile: vtkvmtkPolyDataDistanceToSpheres.cxx,v $
5 Language: C++
6 Date: $$
7 Version: $$
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 Note: this class was contributed by
21 Tangui Morvan
22 Kalkulo AS
23 Simula Research Laboratory
24=========================================================================*/
25
27
28#include "vtkPointData.h"
29#include "vtkDoubleArray.h"
30#include "vtkMath.h"
31#include "vtkInformation.h"
32#include "vtkInformationVector.h"
33#include "vtkObjectFactory.h"
34#include "vtkIOStream.h"
35
36#include "vtkvmtkConstants.h"
37
38
40
42{
43 this->DistanceToSpheresArrayName = NULL;
44
45 this->Spheres = NULL;
46
47 this->DistanceOffset = 0.;
48 this->DistanceScale = 1.;
49 this->MinDistance = 0.;
50 this->MaxDistance = -1.;
51}
52
54{
56 {
57 delete[] this->DistanceToSpheresArrayName;
58 this->DistanceToSpheresArrayName = NULL;
59 }
60
61 if (this->Spheres)
62 {
63 this->Spheres->Delete();
64 this->Spheres = NULL;
65 }
66}
67
69 vtkInformation *vtkNotUsed(request),
70 vtkInformationVector **inputVector,
71 vtkInformationVector *outputVector)
72{
73 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
74 vtkInformation *outInfo = outputVector->GetInformationObject(0);
75
76 vtkPolyData *input = vtkPolyData::SafeDownCast(
77 inInfo->Get(vtkDataObject::DATA_OBJECT()));
78 vtkPolyData *output = vtkPolyData::SafeDownCast(
79 outInfo->Get(vtkDataObject::DATA_OBJECT()));
80
82 {
83 vtkErrorMacro(<<"DistanceToSpheresArrayName not set.");
84 return 1;
85 }
86
87 if (!this->Spheres)
88 {
89 vtkErrorMacro(<<"Spheres not set");
90 return 1;
91 }
92
93 int numberOfInputPoints = input->GetNumberOfPoints();
94
95 output->DeepCopy(input);
96
97
98 vtkDataArray* distanceToSpheresArray = output->GetPointData()->GetArray(this->DistanceToSpheresArrayName);
99
100 bool createArray = !distanceToSpheresArray;
101
102 if (createArray)
103 {
104 distanceToSpheresArray = vtkDoubleArray::New();
105 distanceToSpheresArray->SetName(this->DistanceToSpheresArrayName);
106 distanceToSpheresArray->SetNumberOfComponents(1);
107 distanceToSpheresArray->SetNumberOfTuples(numberOfInputPoints);
108 distanceToSpheresArray->FillComponent(0, VTK_VMTK_LARGE_DOUBLE);
109
110 output->GetPointData()->AddArray(distanceToSpheresArray);
111 }
112
113 int numberOfSpheres = this->Spheres->GetNumberOfPoints();
114
115 vtkDataArray* scalarArray = this->Spheres->GetPointData()->GetScalars();
116
117 double maxd = this->MaxDistance > 0 ? this->MaxDistance : VTK_VMTK_LARGE_DOUBLE;
118
119 double point[3], sphereCenter[3];
120 double sphereRadius = 0.;
121 double distanceToSpheres;
122 for (int i=0; i<numberOfInputPoints; i++)
123 {
124 distanceToSpheres=distanceToSpheresArray->GetComponent(i,0);
125 input->GetPoint(i,point);
126 for (int j=0; j<numberOfSpheres; j++)
127 {
128 this->Spheres->GetPoint(j,sphereCenter);
129 if (scalarArray) sphereRadius = scalarArray->GetComponent(j,0);
130 double newDist = sqrt(vtkMath::Distance2BetweenPoints(point,sphereCenter)) - sphereRadius;
131 if (newDist<0.) newDist = 0.;
132 //Scale and offset the distance
133 newDist = this->DistanceOffset + this->DistanceScale*newDist;
134 if (newDist<this->MinDistance) newDist = this->MinDistance;
135 if (newDist>maxd) newDist = maxd;
136 if (newDist<distanceToSpheres) distanceToSpheres = newDist;
137 }
138 distanceToSpheresArray->SetComponent(i,0,distanceToSpheres);
139 }
140
141 if (createArray) distanceToSpheresArray->Delete();
142
143 return 1;
144}
Compute, for each point of a surface, the minimum distance to the surface of a set of spheres.
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
#define VTK_VMTK_LARGE_DOUBLE
vtkStandardNewMacro(vtkvmtkPolyDataDistanceToSpheres)