VMTK
Loading...
Searching...
No Matches
vtkvmtkSurfaceDistance.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkSurfaceDistance.cxx,v $
5Language: C++
6Date: $Date: 2005/03/31 15:49:05 $
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 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=========================================================================*/
21
23
24#include "vtkCellLocator.h"
25#include "vtkDoubleArray.h"
26#include "vtkGenericCell.h"
27#include "vtkPolyData.h"
28#include "vtkPointData.h"
29#include "vtkMath.h"
30#include "vtkInformation.h"
31#include "vtkInformationVector.h"
32#include "vtkObjectFactory.h"
33
34
36
44
46{
47 if (this->ReferenceSurface)
48 {
49 this->ReferenceSurface->Delete();
50 this->ReferenceSurface = NULL;
51 }
52
53 if (this->DistanceArrayName)
54 {
55 delete[] this->DistanceArrayName;
56 this->DistanceArrayName = NULL;
57 }
58
60 {
61 delete[] this->DistanceVectorsArrayName;
62 this->DistanceVectorsArrayName = NULL;
63 }
64
66 {
67 delete[] this->SignedDistanceArrayName;
68 this->SignedDistanceArrayName = NULL;
69 }
70}
71
73 vtkInformation *vtkNotUsed(request),
74 vtkInformationVector **inputVector,
75 vtkInformationVector *outputVector)
76{
77 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
78 vtkInformation *outInfo = outputVector->GetInformationObject(0);
79
80 vtkPolyData *input = vtkPolyData::SafeDownCast(
81 inInfo->Get(vtkDataObject::DATA_OBJECT()));
82 vtkPolyData *output = vtkPolyData::SafeDownCast(
83 outInfo->Get(vtkDataObject::DATA_OBJECT()));
84
85 vtkIdType i, j;
86 vtkIdType numberOfPoints, numberOfCellPoints;
87 vtkIdType cellId, pointId;
88 int subId;
89 double point[3], closestPoint[3];
90 double distance2, distance, signedDistance;
91 double pcoords[3];
92 double pointNormal[3], *normal;
93 double distanceVector[3];
94 double *weights;
95 double dot;
96 bool computeDistance, computeDistanceVectors, computeSignedDistance;
97 vtkDoubleArray *distanceArray, *distanceVectorsArray, *signedDistanceArray;
98 vtkDataArray *normals;
99 vtkCellLocator *locator;
100 vtkGenericCell *genericCell;
101 vtkCell *cell;
102
103 if (!this->ReferenceSurface)
104 {
105 vtkErrorMacro(<<"No reference surface!");
106 return 1;
107 }
108
110 {
111 vtkErrorMacro(<<"No array names set!");
112 return 1;
113 }
114
115 distanceArray = vtkDoubleArray::New();
116 distanceVectorsArray = vtkDoubleArray::New();
117 signedDistanceArray = vtkDoubleArray::New();
118 normals = NULL;
119
120 locator = vtkCellLocator::New();
121 genericCell = vtkGenericCell::New();
122
123 numberOfPoints = input->GetNumberOfPoints();
124
125 computeDistance = false;
126 computeDistanceVectors = false;
127 computeSignedDistance = false;
128
129 if (this->DistanceArrayName)
130 {
131 computeDistance = true;
132 distanceArray->SetName(this->DistanceArrayName);
133 distanceArray->SetNumberOfComponents(1);
134 distanceArray->SetNumberOfTuples(numberOfPoints);
135 }
136
137 if (this->DistanceVectorsArrayName)
138 {
139 computeDistanceVectors = true;
140 distanceVectorsArray->SetName(this->DistanceVectorsArrayName);
141 distanceVectorsArray->SetNumberOfComponents(3);
142 distanceVectorsArray->SetNumberOfTuples(numberOfPoints);
143 }
144
145 if (this->SignedDistanceArrayName)
146 {
147 computeSignedDistance = true;
148 signedDistanceArray->SetName(this->SignedDistanceArrayName);
149 signedDistanceArray->SetNumberOfComponents(1);
150 signedDistanceArray->SetNumberOfTuples(numberOfPoints);
151 normals = this->ReferenceSurface->GetPointData()->GetNormals();
152 if (!normals)
153 {
154 vtkErrorMacro(<<"Signed distance requires point normals to be defined over ReferenceSurface!");
155 return 1;
156 }
157 }
158
159 locator->SetDataSet(this->ReferenceSurface);
160 locator->BuildLocator();
161
162 for (i=0; i<numberOfPoints; i++)
163 {
164 input->GetPoint(i,point);
165 locator->FindClosestPoint(point,closestPoint,genericCell,cellId,subId,distance2);
166 distanceVector[0] = point[0] - closestPoint[0];
167 distanceVector[1] = point[1] - closestPoint[1];
168 distanceVector[2] = point[2] - closestPoint[2];
169 distance = sqrt(distance2);
170
171 if (computeDistance)
172 {
173 distanceArray->SetTuple1(i,distance);
174 }
175
176 if (computeDistanceVectors)
177 {
178 distanceVectorsArray->SetTuple3(i,-distanceVector[0],-distanceVector[1],-distanceVector[2]);
179 }
180
181 if (computeSignedDistance)
182 {
183 cell = this->ReferenceSurface->GetCell(cellId);
184 numberOfCellPoints = cell->GetNumberOfPoints();
185 weights = new double[numberOfCellPoints];
186 cell->EvaluatePosition(point,NULL,subId,pcoords,distance2,weights);
187 pointNormal[0] = 0.0;
188 pointNormal[1] = 0.0;
189 pointNormal[2] = 0.0;
190 for (j=0; j<numberOfCellPoints; j++)
191 {
192 pointId = cell->GetPointId(j);
193 normal = normals->GetTuple3(pointId);
194 pointNormal[0] += weights[j] * normal[0];
195 pointNormal[1] += weights[j] * normal[1];
196 pointNormal[2] += weights[j] * normal[2];
197 }
198 dot = vtkMath::Dot(distanceVector,pointNormal);
199 signedDistance = distance;
200 // distance is positive if distanceVector and normal have negative dot
201 if (dot>0.0)
202 {
203 signedDistance *= -1.0;
204 }
205 signedDistanceArray->SetTuple1(i,signedDistance);
206 delete[] weights;
207 }
208 }
209
210 output->DeepCopy(input);
211
212 if (computeDistance)
213 {
214 output->GetPointData()->AddArray(distanceArray);
215 }
216
217 if (computeDistanceVectors)
218 {
219 output->GetPointData()->AddArray(distanceVectorsArray);
220 }
221
222 if (computeSignedDistance)
223 {
224 output->GetPointData()->AddArray(signedDistanceArray);
225 }
226
227 distanceArray->Delete();
228 distanceVectorsArray->Delete();
229 signedDistanceArray->Delete();
230 locator->Delete();
231 genericCell->Delete();
232
233 return 1;
234}
235
236void vtkvmtkSurfaceDistance::PrintSelf(std::ostream& os, vtkIndent indent)
237{
238 this->Superclass::PrintSelf(os,indent);
239}
Calculate the Euclidean distance of one surface from another.
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void PrintSelf(std::ostream &os, vtkIndent indent) override
vtkStandardNewMacro(vtkvmtkSurfaceDistance)