VMTK
Loading...
Searching...
No Matches
vtkvmtkPolyDataNormalPlaneEstimator.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkPolyDataNormalPlaneEstimator.cxx,v $
5Language: C++
6Date: $Date: 2015/12/01 12:26:27 $
7Version: $Revision: 1.1 $
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 "vtkPolyDataConnectivityFilter.h"
25#include "vtkFloatArray.h"
26#include "vtkIdTypeArray.h"
27#include "vtkIdList.h"
28#include "vtkMath.h"
29#include "vtkCell.h"
30#include "vtkPointData.h"
31#include "vtkPointLocator.h"
32#include "vtkPolyData.h"
33#include "vtkPolyDataNormals.h"
34
35#include "vtkInformation.h"
36#include "vtkInformationVector.h"
37#include "vtkObjectFactory.h"
38#include "vtkVersion.h"
39
40
42
44{
45 this->UseConnectivity = 1;
46 this->MinimumNeighborhoodSize = 10;
47 this->OriginPointId = -1;
48 this->Origin[0] = this->Origin[1] = this->Origin[2] = 0.0;
49 this->Normal[0] = this->Normal[1] = this->Normal[2] = 0.0;
50}
51
55
57 vtkInformation *vtkNotUsed(request),
58 vtkInformationVector **inputVector,
59 vtkInformationVector *outputVector)
60{
61 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
62 vtkInformation *outInfo = outputVector->GetInformationObject(0);
63
64 vtkPolyData *input = vtkPolyData::SafeDownCast(
65 inInfo->Get(vtkDataObject::DATA_OBJECT()));
66 vtkPolyData *output = vtkPolyData::SafeDownCast(
67 outInfo->Get(vtkDataObject::DATA_OBJECT()));
68
69 if (this->OriginPointId == -1)
70 {
71 vtkErrorMacro("Origin point id not specified");
72 return 1;
73 }
74
75 input->BuildCells();
76 input->BuildLinks();
77
78 vtkFloatArray* normals = vtkFloatArray::New();
79
80 if (input->GetPointData()->GetNormals())
81 {
82 normals->DeepCopy(input->GetPointData()->GetNormals());
83 }
84 else
85 {
86 vtkPolyDataNormals* normalsFilter = vtkPolyDataNormals::New();
87 normalsFilter->SetInputData(input);
88 normalsFilter->AutoOrientNormalsOn();
89 normalsFilter->FlipNormalsOn();
90 normalsFilter->ConsistencyOn();
91 normalsFilter->ComputeCellNormalsOff();
92 normalsFilter->SplittingOff();
93 normalsFilter->Update();
94
95 normals->DeepCopy(normalsFilter->GetOutput()->GetPointData()->GetNormals());
96
97 normalsFilter->Delete();
98 }
99
100 vtkPointLocator* locator = vtkPointLocator::New();
101 locator->SetDataSet(input);
102 locator->BuildLocator();
103
104 vtkIdTypeArray* connectivityArray = vtkIdTypeArray::New();
105 if (this->UseConnectivity)
106 {
107 vtkPolyDataConnectivityFilter* connectivityFilter = vtkPolyDataConnectivityFilter::New();
108 connectivityFilter->SetInputData(input);
109 connectivityFilter->SetExtractionModeToAllRegions();
110 connectivityFilter->ColorRegionsOn();
111 connectivityFilter->Update();
112
113 connectivityArray->DeepCopy(connectivityFilter->GetOutput()->GetPointData()->GetScalars());
114 connectivityFilter->Delete();
115 }
116
117 vtkIdType npts, *cells;
118 vtkIdType ncells;
119
120 input->GetPointCells(this->OriginPointId,ncells,cells);
121
122 double averageLength = 0.0;
123
124 for (vtkIdType i=0; i<ncells; i++)
125 {
126 averageLength += sqrt(input->GetCell(cells[i])->GetLength2());
127 }
128 averageLength /= ncells;
129
130 double radiusStep = averageLength;
131
132 double direction[3];
133 double normal0[3], normal1[3];
134
135 input->GetPoint(this->OriginPointId,this->Origin);
136 normals->GetTuple(this->OriginPointId,normal1);
137
138 vtkIdType regionId = -1;
139 if (this->UseConnectivity)
140 {
141 regionId = connectivityArray->GetValue(this->OriginPointId);
142 }
143
144 vtkIdList* pointIdsWithinRadius = vtkIdList::New();
145
146 bool doneOnce = false;
147
148 // Future improvement: only consider topologically connected vertices
149
150 for (int k=0; k<100; k++)
151 {
152 pointIdsWithinRadius->Initialize();
153
154 double radius = k * radiusStep;
155 locator->FindPointsWithinRadius(radius,this->Origin,pointIdsWithinRadius);
156
157 vtkIdType numberOfIds = pointIdsWithinRadius->GetNumberOfIds();
158
159 vtkIdType neighborhoodSize = numberOfIds;
160
161 if (this->UseConnectivity)
162 {
163 for (vtkIdType i=0; i<numberOfIds; i++)
164 {
165 vtkIdType id = pointIdsWithinRadius->GetId(i);
166 if (connectivityArray->GetValue(id) == regionId)
167 {
168 neighborhoodSize += 1;
169 }
170 }
171 }
172
173 if (neighborhoodSize < this->MinimumNeighborhoodSize)
174 {
175 continue;
176 }
177
178 double kdirection[3];
179 kdirection[0] = kdirection[1] = kdirection[2] = 0.0;
180
181 bool done = false;
182
183 for (vtkIdType i=0; i<numberOfIds; i++)
184 {
185 vtkIdType id = pointIdsWithinRadius->GetId(i);
186
187 if (this->UseConnectivity && connectivityArray->GetValue(id) != regionId)
188 {
189 continue;
190 }
191
192 normals->GetTuple(id,normal0);
193
194 double dot = vtkMath::Dot(normal0,normal1);
195
196 if (fabs(dot) > 0.6) {
197 continue;
198 }
199
200 if (dot < -0.5 && doneOnce) {
201 done = true;
202 break;
203 }
204
205 double cross[3];
206 vtkMath::Cross(normal0,normal1,cross);
207
208 if (vtkMath::Dot(kdirection,cross) < 0.0) {
209 cross[0] *= -1.0;
210 cross[1] *= -1.0;
211 cross[2] *= -1.0;
212 }
213
214 kdirection[0] += cross[0];
215 kdirection[1] += cross[1];
216 kdirection[2] += cross[2];
217 doneOnce = true;
218 }
219
220 if (!doneOnce)
221 {
222 continue;
223 }
224
225 vtkMath::Normalize(kdirection);
226
227 if (done)
228 {
229 break;
230 }
231
232 this->Normal[0] = kdirection[0];
233 this->Normal[1] = kdirection[1];
234 this->Normal[2] = kdirection[2];
235 }
236
237 vtkMath::Normalize(this->Normal);
238
239 normals->Delete();
240 locator->Delete();
241 pointIdsWithinRadius->Delete();
242 connectivityArray->Delete();
243
244 return 1;
245}
246
247void vtkvmtkPolyDataNormalPlaneEstimator::PrintSelf(std::ostream& os, vtkIndent indent)
248{
249 this->Superclass::PrintSelf(os,indent);
250}
Estimate the plane that locally cuts a tubular surface transversally at a given seed point.
void PrintSelf(std::ostream &os, vtkIndent indent) override
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
vtkStandardNewMacro(vtkvmtkPolyDataNormalPlaneEstimator)