VMTK
Loading...
Searching...
No Matches
vtkvmtkSteepestDescentShooter.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkSteepestDescentShooter.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:46:43 $
7Version: $Revision: 1.4 $
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#include "vtkPolyLine.h"
24#include "vtkPointData.h"
25#include "vtkCellData.h"
26#include "vtkLine.h"
27#include "vtkMath.h"
28#include "vtkObjectFactory.h"
29#include "vtkInformation.h"
30#include "vtkInformationVector.h"
31#include "vtkvmtkConstants.h"
32
33
35
37{
38 this->Seeds = NULL;
39 this->Target = NULL;
40
41 this->EdgeArrayName = NULL;
42
43 this->TargetVectorsArrayName = NULL;
44 this->TargetCellIdsArrayName = NULL;
45 this->TargetPCoordsArrayName = NULL;
46
47 this->TargetVectors = vtkDoubleArray::New();
48 this->TargetCellIds = vtkIntArray::New();
49 this->TargetPCoords = vtkDoubleArray::New();
50
51 this->TargetArray = vtkIntArray::New();
52
53}
54
56{
57 this->TargetVectors->Delete();
58 this->TargetCellIds->Delete();
59 this->TargetPCoords->Delete();
60
61 this->TargetArray->Delete();
62
63 if (this->Seeds)
64 {
65 this->Seeds->Delete();
66 this->Seeds = NULL;
67 }
68
69 if (this->Target)
70 {
71 this->Target->Delete();
72 this->Target = NULL;
73 }
74}
75
76void vtkvmtkSteepestDescentShooter::FindNearestPolyLinePoint(double* currentPoint, double* lineClosestPoint, int &lineCellId, int &lineCellSubId, double &linePCoord)
77{
78 vtkIdType i;
79 int subId;
80 double closestPoint[3];
81 double parCoords[3];
82 double dist2, minDist2;
83 double* weights;
84 vtkPolyLine* polyLine;
85
86 minDist2 = VTK_VMTK_LARGE_DOUBLE;
87 for (i=0; i<this->Target->GetNumberOfCells(); i++)
88 {
89 polyLine = vtkPolyLine::SafeDownCast(this->Target->GetCell(i));
90 if (!polyLine)
91 {
92 continue;
93 }
94 weights = new double[polyLine->GetNumberOfPoints()];
95 polyLine->EvaluatePosition(currentPoint,closestPoint,subId,parCoords,dist2,weights);
96
97 if (dist2 - minDist2 < -VTK_VMTK_DOUBLE_TOL)
98 {
99 minDist2 = dist2;
100 lineCellId = i;
101 lineCellSubId = subId;
102 linePCoord = parCoords[0];
103 lineClosestPoint[0] = closestPoint[0];
104 lineClosestPoint[1] = closestPoint[1];
105 lineClosestPoint[2] = closestPoint[2];
106 }
107
108 delete weights;
109 }
110}
111
112void vtkvmtkSteepestDescentShooter::Backtrace(vtkPolyData* input, vtkIdType seedId)
113{
114 bool done;
115 double seedPoint[3];
116 double currentPoint[3], currentScalar;
117 double currentS;
118 vtkIdType currentEdge[2], steepestDescentEdge[2];
119 double steepestDescentS;
120 double previousPoint[3], lineClosestPoint[3];
121 double vector[3];
122 int lineCellId, lineCellSubId;
123 double linePCoord;
124
125 done = false;
126
127 input->GetPoint(seedId,seedPoint);
128 currentPoint[0] = seedPoint[0];
129 currentPoint[1] = seedPoint[1];
130 currentPoint[2] = seedPoint[2];
131
132 previousPoint[0] = seedPoint[0];
133 previousPoint[1] = seedPoint[1];
134 previousPoint[2] = seedPoint[2];
135
136 currentScalar = this->DescentArray->GetTuple1(seedId);
137
138 currentEdge[0] = seedId;
139 currentEdge[1] = seedId;
140 currentS = 0.0;
141
142 while (!done)
143 {
144 if (((this->TargetArray->GetValue(currentEdge[0])!=-1) && (this->TargetArray->GetValue(currentEdge[1])!=-1)))
145 {
146 this->FindNearestPolyLinePoint(currentPoint, lineClosestPoint, lineCellId, lineCellSubId, linePCoord);
147 vector[0] = lineClosestPoint[0] - seedPoint[0];
148 vector[1] = lineClosestPoint[1] - seedPoint[1];
149 vector[2] = lineClosestPoint[2] - seedPoint[2];
150 this->TargetVectors->SetTuple(seedId,vector);
151 this->TargetCellIds->SetComponent(seedId,0,lineCellId);
152 this->TargetCellIds->SetComponent(seedId,1,lineCellSubId);
153 this->TargetPCoords->SetTuple1(seedId,linePCoord);
154
155 done = true;
156 break;
157 }
158
159 if (currentScalar==0.0)
160 {
161 // THIS MEANS THERE'S SOMETHING WRONG SOMEWHERE!
162
163 done = true;
164 break;
165 }
166
167 this->GetSteepestDescent(input,currentEdge,currentS,steepestDescentEdge,steepestDescentS);
168
169 currentEdge[0] = steepestDescentEdge[0];
170 currentEdge[1] = steepestDescentEdge[1];
171 currentS = steepestDescentS;
172
173 previousPoint[0] = currentPoint[0];
174 previousPoint[1] = currentPoint[1];
175 previousPoint[2] = currentPoint[2];
176
177 // Safe GetPoint: avoid shared-temp aliasing when float points (VTK 9).
178 {
179 double edgePoint0[3], edgePoint1[3];
180 input->GetPoint(currentEdge[0], edgePoint0);
181 input->GetPoint(currentEdge[1], edgePoint1);
182 currentPoint[0] = edgePoint0[0] * (1.0 - currentS) + edgePoint1[0] * currentS;
183 currentPoint[1] = edgePoint0[1] * (1.0 - currentS) + edgePoint1[1] * currentS;
184 currentPoint[2] = edgePoint0[2] * (1.0 - currentS) + edgePoint1[2] * currentS;
185 }
186
187 currentScalar = this->DescentArray->GetTuple1(currentEdge[0]) * (1.0 - currentS) + this->DescentArray->GetTuple1(currentEdge[1]) * currentS;
188 }
189
190}
191
193 vtkInformation *vtkNotUsed(request),
194 vtkInformationVector **inputVector,
195 vtkInformationVector *outputVector)
196{
197 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
198 vtkInformation *outInfo = outputVector->GetInformationObject(0);
199
200 vtkPolyData *input = vtkPolyData::SafeDownCast(
201 inInfo->Get(vtkDataObject::DATA_OBJECT()));
202 vtkPolyData *output = vtkPolyData::SafeDownCast(
203 outInfo->Get(vtkDataObject::DATA_OBJECT()));
204
205 vtkIdType i, j, k, h, l, m;
206 vtkIdType poleId;
207 vtkIdType npts, targetNpts, *cells;
208 const vtkIdType *pts;
209 const vtkIdType *targetPts;
210 vtkIdType ncells;
211
212 if (!this->DescentArrayName)
213 {
214 vtkErrorMacro("Descent array name not specified.");
215 return 1;
216 }
217
218 if (!input->GetPointData()->GetArray(this->DescentArrayName))
219 {
220 vtkErrorMacro(<< "Descent array with name specified does not exist!");
221 return 1;
222 }
223
224 if (!this->Seeds)
225 {
226 vtkErrorMacro(<< "No seeds specified!");
227 return 1;
228 }
229
230 if (!this->Target)
231 {
232 vtkErrorMacro(<< "No target specified!");
233 return 1;
234 }
235
236 if (!this->EdgeArrayName)
237 {
238 vtkErrorMacro(<< "No edge array name specified!");
239 return 1;
240 }
241
242 if (!this->Target->GetPointData()->GetArray(this->EdgeArrayName))
243 {
244 vtkErrorMacro(<< "Edge array with name specified does not exist in target!");
245 return 1;
246 }
247
248 if (this->TargetVectorsArrayName)
249 {
251 }
252 else
253 {
254 this->TargetVectors->SetName("TargetVectors");
255 }
256
257 if (this->TargetCellIdsArrayName)
258 {
260 }
261 else
262 {
263 this->TargetCellIds->SetName("TargetCellIds");
264 }
265
266 if (this->TargetPCoordsArrayName)
267 {
269 }
270 else
271 {
272 this->TargetPCoords->SetName("TargetPCoords");
273 }
274
275 this->DescentArray = input->GetPointData()->GetArray(this->DescentArrayName);
276 this->EdgeArray = this->Target->GetPointData()->GetArray(this->EdgeArrayName);
277
278 this->TargetVectors->SetNumberOfComponents(3);
279 this->TargetCellIds->SetNumberOfComponents(2);
280
281 this->TargetVectors->SetNumberOfTuples(input->GetNumberOfPoints());
282 this->TargetCellIds->SetNumberOfTuples(input->GetNumberOfPoints());
283 this->TargetPCoords->SetNumberOfTuples(input->GetNumberOfPoints());
284
285 this->TargetVectors->FillComponent(0,0.0);
286 this->TargetVectors->FillComponent(1,0.0);
287 this->TargetVectors->FillComponent(2,0.0);
288 this->TargetCellIds->FillComponent(0,0.0);
289 this->TargetCellIds->FillComponent(1,0.0);
290 this->TargetPCoords->FillComponent(0,0.0);
291
292 input->BuildCells();
293 input->BuildLinks();
294
295 this->TargetArray->SetNumberOfTuples(input->GetNumberOfPoints());
296 this->TargetArray->FillComponent(0,-1);
297
298 this->Target->BuildCells();
299
300 for (h=this->Target->GetNumberOfCells()-1; h>=0; h--)
301 {
302 this->Target->GetCellPoints(h,targetNpts,targetPts);
303 for (l=0; l<targetNpts; l++)
304 {
305 for (m=0; m<2; m++)
306 {
307 input->GetPointCells(static_cast<vtkIdType>(this->EdgeArray->GetComponent(targetPts[l],m)),ncells,cells);
308 for (j=0; j<ncells; j++)
309 {
310 input->GetCellPoints(cells[j],npts,pts);
311 for (k=0; k<npts; k++)
312 {
313 this->TargetArray->SetValue(pts[k],h);
314 }
315 }
316 }
317 }
318 }
319
320 for (i=0; i<this->Seeds->GetNumberOfIds(); i++)
321 {
322 poleId = this->Seeds->GetId(i);
323 this->Backtrace(input,poleId);
324 }
325
326 output->CopyStructure(input);
327 output->GetPointData()->PassData(input->GetPointData());
328 output->GetCellData()->PassData(input->GetCellData());
329
330 output->GetPointData()->AddArray(this->TargetVectors);
331 output->GetPointData()->AddArray(this->TargetCellIds);
332 output->GetPointData()->AddArray(this->TargetPCoords);
333
334 return 1;
335}
336
337void vtkvmtkSteepestDescentShooter::PrintSelf(std::ostream& os, vtkIndent indent)
338{
339 this->Superclass::PrintSelf(os,indent);
340}
double GetSteepestDescent(vtkPolyData *input, vtkIdType *edge, double s, vtkIdType *steepestDescentEdge, double &steepestDescentS)
void PrintSelf(std::ostream &os, vtkIndent indent) override
Experimental: for each seed point (e.g. a Voronoi pole), backtrace a steepest descent path over a non...
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void FindNearestPolyLinePoint(double *currentPoint, double *lineClosestPoint, int &lineCellId, int &lineCellSubId, double &linePCoord)
void Backtrace(vtkPolyData *input, vtkIdType seedId)
void PrintSelf(std::ostream &os, vtkIndent indent) override
#define VTK_VMTK_DOUBLE_TOL
#define VTK_VMTK_LARGE_DOUBLE
vtkStandardNewMacro(vtkvmtkSteepestDescentShooter)