VMTK
Loading...
Searching...
No Matches
vtkvmtkPolyBallLine2.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: VMTK
4 Module: $RCSfile: vtkvmtkPolyBallLine2.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#include "vtkvmtkConstants.h"
28#include "vtkPointData.h"
29#include "vtkPolyLine.h"
30#include "vtkMath.h"
31#include "vtkObjectFactory.h"
32#include "vtkVersion.h"
33
34
36
38{
39 this->Input = NULL;
40 this->InputCellIds = NULL;
41 this->InputCellId = -1;
42 this->PolyBallRadiusArrayName = NULL;
43 this->UseRadiusInformation = 1;
44 this->LocalInput = NULL;
45 this->TriangulatedInput = NULL;
46 this->Triangulator = vtkTriangleFilter::New();
47 this->Triangulator->PassLinesOn();
48 this->Locator = vtkCellLocator::New();
49 this->ShouldBuildLocator = true;
50 this->LocatorMTime = 0;
51}
52
54{
55 if (this->LocalInput && (this->LocalInput != this->Input))
56 {
57 this->LocalInput->Delete();
58 this->LocalInput = NULL;
59 }
60
61 if (this->Input)
62 {
63 this->Input->Delete();
64 this->Input = NULL;
65 }
66
67 if (this->InputCellIds)
68 {
69 this->InputCellIds->Delete();
70 this->InputCellIds = NULL;
71 }
72
74 {
75 delete[] this->PolyBallRadiusArrayName;
76 this->PolyBallRadiusArrayName = NULL;
77 }
78
79 if (this->Triangulator)
80 {
81 this->Triangulator->Delete();
82 this->Triangulator = NULL;
83 }
84
85 if (this->Locator)
86 {
87 this->Locator->Delete();
88 this->Locator = NULL;
89 }
90}
91
92void vtkvmtkPolyBallLine2::SetInput(vtkPolyData *inp)
93{
94 if (this->LocalInput == this->Input)
95 {
96 this->LocalInput = NULL;
97 }
98 //The locator needs to be rebuilt
99 this->ShouldBuildLocator = true;
100 vtkSetObjectBodyMacro(Input,vtkPolyData,inp);
101}
102
104{
105 //The locator needs to be rebuilt
106 this->ShouldBuildLocator = true;
107 vtkSetObjectBodyMacro(InputCellIds,vtkIdList,cellIds);
108}
109
111{
112 vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting InputCellId to " << cellId);
113 if (this->InputCellId != cellId)
114 {
115 //The locator needs to be rebuilt
116 this->ShouldBuildLocator = true;
117 this->InputCellId = cellId;
118 this->Modified();
119 }
120}
121double vtkvmtkPolyBallLine2::ComplexDot(double x[4], double y[4])
122{
123 return x[0]*y[0] + x[1]*y[1] + x[2]*y[2] - x[3]*y[3];
124}
125
127{
128 if (!this->Input)
129 {
130 vtkErrorMacro(<<"No Input specified!");
131 return 0.0;
132 }
133
134 if (this->Input->GetNumberOfPoints()==0)
135 {
136 vtkWarningMacro(<<"Empty Input specified!");
137 return 0.0;
138 }
139
140 if (this->UseRadiusInformation)
141 {
142 if (!this->PolyBallRadiusArrayName)
143 {
144 vtkErrorMacro(<<"No PolyBallRadiusArrayName specified!");
145 return 0.0;
146 }
147 }
148
149 this->ShouldBuildLocator = this->ShouldBuildLocator || (this->LocatorMTime != this->Input->GetMTime());
150
151 //Rebuild the locator if needed
152 if (this->ShouldBuildLocator)
153 {
154 this->BuildLocator();
155 }
156
157 //Find the cell closest to x
158 double dist2;
159 double closPt[3];
160 vtkIdType closCellId;
161 int subId;
162 this->Locator->FindClosestPoint(x,closPt,closCellId,subId,dist2);
163
164 double value;
165
166 if (this->UseRadiusInformation)
167 {
168 vtkDataArray *polyballRadiusArray = this->TriangulatedInput->GetPointData()->GetArray(this->PolyBallRadiusArrayName);
169
170 if (polyballRadiusArray==NULL)
171 {
172 vtkErrorMacro(<<"PolyBallRadiusArray with name specified does not exist!");
173 return 0.0;
174 }
175
176 vtkIdType cellType = this->TriangulatedInput->GetCellType(closCellId);
177
178 if (cellType != VTK_LINE)
179 {
180 vtkErrorMacro(<<"Error wrong cell type in polyline!");
181 return 0.0;
182 }
183
184 vtkIdList *linePointsIds = vtkIdList::New();
185 this->TriangulatedInput->GetCellPoints(closCellId, linePointsIds);
186 double *point0;
187 double *point1;
188 double vector0[3], vector1[3];
189 double radius0, radius1;
190 point0 = this->TriangulatedInput->GetPoint(linePointsIds->GetId(0));
191 point1 = this->TriangulatedInput->GetPoint(linePointsIds->GetId(1));
192
193 for (int i=0;i<3;i++)
194 {
195 vector0[i] = point1[i] - point0[i];
196 vector1[i] = closPt[i] - point0[i];
197 }
198
199 double num = vtkMath::Dot(vector0,vector1);
200 double den = vtkMath::Dot(vector0,vector0);
201
202 double t=0;
203
204 //t should be between 0 and 1 since we took the closest point
205 if (den!=0.)
206 {
207 t = num/den;
208 }
209
210 radius0 = polyballRadiusArray->GetComponent(linePointsIds->GetId(0),0);
211 radius1 = polyballRadiusArray->GetComponent(linePointsIds->GetId(1),0);
212
213 double radius = radius0 + t*(radius1 - radius0);
214
215 value = sqrt(dist2) - radius;
216
217 linePointsIds->Delete();
218 }
219 else
220 {
221 value = sqrt(dist2);
222 }
223
224 return value;
225}
226
227void vtkvmtkPolyBallLine2::EvaluateGradient(double x[3], double n[3])
228{
229 vtkWarningMacro("Poly ball gradient computation not yet implemented!");
230 // TODO
231}
232
233void vtkvmtkPolyBallLine2::PrintSelf(std::ostream& os, vtkIndent indent)
234{
235 this->Superclass::PrintSelf(os,indent);
236
237}
238
240{
241 if (!this->Input)
242 {
243 vtkErrorMacro(<<"No Input specified!");
244 return;
245 }
246
247 if (this->Input->GetNumberOfPoints()==0)
248 {
249 vtkWarningMacro(<<"Empty Input specified!");
250 return;
251 }
252
253 this->LocatorMTime = this->Input->GetMTime();
254
255 if (this->LocalInput && (this->LocalInput != this->Input))
256 {
257 this->LocalInput->Delete();
258 }
259
260 //If only part of the cells are used, copy these cells to LocalInput
261 if (this->InputCellIds || this->InputCellId)
262 {
263 this->LocalInput = vtkPolyData::New();
264 this->LocalInput->DeepCopy(this->Input);
265
266 if (this->InputCellIds)
267 {
268 //Remove the cells whose id is not the list
269 for (int i=0; i<this->LocalInput->GetNumberOfCells(); i++)
270 {
271 if (!this->InputCellIds->IsId(i))
272 {
273 this->LocalInput->DeleteCell(i);
274 }
275 }
276 }
277
278 else if (this->InputCellId != -1)
279 {
280 //Remove the cells whose is not InputCellId
281 for (int i=0; i<this->LocalInput->GetNumberOfCells(); i++)
282 {
283 if (i!=this->InputCellId)
284 {
285 this->LocalInput->DeleteCell(i);
286 }
287 }
288 }
289 }
290 else
291 {
292 this->LocalInput = this->Input;
293 }
294
295 //Triangulate the input for improved performance
296 this->Triangulator->SetInputData(this->LocalInput);
297 this->Triangulator->Update();
298 this->TriangulatedInput = this->Triangulator->GetOutput();
299
300 //Build the locator
301 this->Locator->SetDataSet(this->TriangulatedInput);
302 this->Locator->BuildLocator();
303
304 this->ShouldBuildLocator = false;
305}
Second version of polyline where distance to the line is first computed and then the radius at the cl...
vtkTriangleFilter * Triangulator
static double ComplexDot(double x[4], double y[4])
void PrintSelf(std::ostream &os, vtkIndent indent) override
void EvaluateGradient(double x[3], double n[3]) override
virtual void SetInput(vtkPolyData *inp)
virtual void SetInputCellId(vtkIdType cellId)
double EvaluateFunction(double x[3]) override
virtual void SetInputCellIds(vtkIdList *cellIds)
vtkStandardNewMacro(vtkvmtkPolyBallLine2)