VMTK
Loading...
Searching...
No Matches
vtkvmtkSteepestDescentLineTracer.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkSteepestDescentLineTracer.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 "vtkFloatArray.h"
24#include "vtkPolyData.h"
25#include "vtkPointData.h"
26#include "vtkCellArray.h"
27#include "vtkLine.h"
28#include "vtkMath.h"
29#include "vtkObjectFactory.h"
30#include "vtkInformation.h"
31#include "vtkInformationVector.h"
32#include "vtkvmtkConstants.h"
33
34
36
38{
39 this->Seeds = NULL;
40 this->Targets = NULL;
41 this->StopOnTargets = 0;
42 this->DataArrayName = NULL;
43 this->EdgeArrayName = NULL;
44 this->EdgePCoordArrayName = NULL;
45
46 this->MergePaths = 0;
48
49 this->LineDataArray = NULL;
50
51 this->HitTargets = vtkIdList::New();
52}
53
55{
56 this->HitTargets->Delete();
57
58 if (this->Seeds)
59 {
60 this->Seeds->Delete();
61 this->Seeds = NULL;
62 }
63
64 if (this->Targets)
65 {
66 this->Targets->Delete();
67 this->Targets = NULL;
68 }
69
70 if (this->EdgePCoordArrayName)
71 {
72 delete [] this->EdgePCoordArrayName;
73 this->EdgePCoordArrayName = NULL;
74 }
75
76 if (this->EdgeArrayName)
77 {
78 delete [] this->EdgeArrayName;
79 this->EdgeArrayName = NULL;
80 }
81
82 if (this->DescentArrayName)
83 {
84 delete[] this->DescentArrayName;
85 this->DescentArrayName = NULL;
86 }
87
88 if (this->DataArrayName)
89 {
90 delete[] this->DataArrayName;
91 this->DataArrayName = NULL;
92 }
93
94}
95
96void vtkvmtkSteepestDescentLineTracer::Backtrace(vtkPolyData* input, vtkIdType seedId)
97{
98 bool done;
99 double startingPoint[3], endingPoint[3], currentPoint[3], currentScalar;
100 double currentS;
101 double currentRadius;
102 vtkIdType currentEdge[2], steepestDescentEdge[2], previousEdge[2], previousEdge2[2];
103 double steepestDescentS, steepestDescent;
104 double previousS, previousS2;
105 double previousPoint[3];
106 double directionFactor;
107 vtkIdList* neighborCells;
108 vtkIdType pointId, j, targetId;
109 vtkIdList* lineIds;
110
111 vtkPoints* newPoints;
112 vtkDataArray* newScalars;
113 vtkCellArray* newLines;
114
115 newPoints = this->GetOutput()->GetPoints();
116 newScalars = this->GetOutput()->GetPointData()->GetArray(this->DataArrayName);
117 newLines = this->GetOutput()->GetLines();
118
119 neighborCells = vtkIdList::New();
120 lineIds = vtkIdList::New();
121
122 directionFactor = 0.0;
123 if (this->Direction==VTK_VMTK_DOWNWARD)
124 {
125 directionFactor = 1.0;
126 }
127 else if (this->Direction==VTK_VMTK_UPWARD)
128 {
129 directionFactor = - 1.0;
130 }
131
132 done = false;
133
134 input->GetPoint(seedId,startingPoint);
135
136 currentPoint[0] = startingPoint[0];
137 currentPoint[1] = startingPoint[1];
138 currentPoint[2] = startingPoint[2];
139
140 pointId = newPoints->InsertNextPoint(startingPoint);
141
142 currentScalar = this->DescentArray->GetTuple1(seedId);
143 currentRadius = this->LineDataArray->GetTuple1(seedId);
144 lineIds->InsertNextId(pointId);
145
146 currentEdge[0] = seedId;
147 currentEdge[1] = seedId;
148 currentS = 0.0;
149
150 previousS = 0.0;
151 previousS2 = 0.0;
152
153 newScalars->InsertComponent(pointId,0,currentRadius);
154 this->Edges->InsertComponent(pointId,0,currentEdge[0]);
155 this->Edges->InsertComponent(pointId,1,currentEdge[1]);
156 this->EdgeParCoords->InsertValue(pointId,currentS);
157
158 std::size_t numIterations = 0;
159
160 previousEdge[0] = currentEdge[0];
161 previousEdge[1] = currentEdge[1];
162
163 previousEdge2[0] = -1;
164 previousEdge2[1] = -1;
165
166 while (!done)
167 {
168 if ((this->StopOnTargets) && ((this->Targets->IsId(currentEdge[0]) != -1) || (this->Targets->IsId(currentEdge[1]) != -1)))
169 {
170 if (this->Targets->IsId(currentEdge[0]) != -1)
171 {
172 targetId = currentEdge[0];
173 }
174 else
175 {
176 targetId = currentEdge[1];
177 }
178
179 input->GetPoint(targetId,endingPoint);
180 if (vtkMath::Distance2BetweenPoints(currentPoint,endingPoint) > VTK_VMTK_DOUBLE_TOL)
181 {
182 pointId = newPoints->InsertNextPoint(endingPoint);
183 currentScalar = this->DescentArray->GetTuple1(targetId);
184 currentRadius = this->LineDataArray->GetTuple1(targetId);
185 lineIds->InsertNextId(pointId);
186
187 currentEdge[0] = targetId;
188 currentEdge[1] = targetId;
189 currentS = 0.0;
190
191 newScalars->InsertTuple1(pointId,currentRadius);
192 this->Edges->InsertComponent(pointId,0,currentEdge[0]);
193 this->Edges->InsertComponent(pointId,1,currentEdge[1]);
194 this->EdgeParCoords->InsertValue(pointId,currentS);
195 }
196
197 this->HitTargets->InsertNextId(targetId);
198
199 done = true;
200 break;
201 }
202 if (done)
203 {
204 break;
205 }
206
207 input->GetCellEdgeNeighbors(-1,currentEdge[0],currentEdge[1],neighborCells);
208
209 steepestDescent = this->GetSteepestDescent(input,currentEdge,currentS,steepestDescentEdge,steepestDescentS);
210
211 if (steepestDescentEdge[0] == -1 || steepestDescentEdge[1] == -1)
212 {
213 vtkWarningMacro(<<"Can't find a steepest descent edge. Target not reached.");
214 done = true;
215 break;
216 }
217
218 if (directionFactor*steepestDescent < VTK_VMTK_DOUBLE_TOL)
219 {
220 if (!this->StopOnTargets)
221 //if (!this->StopOnTargets || (previousEdge[0] == currentEdge[0] && previousEdge[1] == currentEdge[1]))
222 {
223 vtkWarningMacro(<<"Target not reached.");
224 done = true; // these two lines were outside the if (!this->StopOnTarget), but that may lead to unnecessary failure.
225 break; // Need of better detection of stall.
226 }
227 }
228
229 currentEdge[0] = steepestDescentEdge[0];
230 currentEdge[1] = steepestDescentEdge[1];
231 currentS = steepestDescentS;
232
233 if (numIterations > 0 &&
234 (
235 ((currentEdge[0] == previousEdge2[0] && currentEdge[1] == previousEdge2[1] && fabs(previousS2 - currentS) < VTK_VMTK_DOUBLE_TOL)) ||
236 ((currentEdge[0] == previousEdge2[1] && currentEdge[1] == previousEdge2[0]) && fabs(1.0 - previousS2 - currentS) < VTK_VMTK_DOUBLE_TOL)
237 )
238 )
239 {
240 vtkWarningMacro(<<"Degenerate descent detected. Target not reached.");
241 done = true;
242 break;
243 }
244
245 previousPoint[0] = currentPoint[0];
246 previousPoint[1] = currentPoint[1];
247 previousPoint[2] = currentPoint[2];
248
249 // Do not call GetPoint(id) twice in one expression: with float points VTK
250 // returns a shared temporary buffer, so the second call overwrites the first
251 // (VTK 9). Copy into locals first (same pattern as GetSteepestDescentInCell).
252 {
253 double edgePoint0[3], edgePoint1[3];
254 input->GetPoint(currentEdge[0], edgePoint0);
255 input->GetPoint(currentEdge[1], edgePoint1);
256 currentPoint[0] = edgePoint0[0] * (1.0 - currentS) + edgePoint1[0] * currentS;
257 currentPoint[1] = edgePoint0[1] * (1.0 - currentS) + edgePoint1[1] * currentS;
258 currentPoint[2] = edgePoint0[2] * (1.0 - currentS) + edgePoint1[2] * currentS;
259 }
260
261 currentScalar = this->DescentArray->GetTuple1(currentEdge[0]) * (1.0 - currentS) + this->DescentArray->GetTuple1(currentEdge[1]) * currentS;
262 currentRadius = this->LineDataArray->GetTuple1(currentEdge[0]) * (1.0 - currentS) + this->LineDataArray->GetTuple1(currentEdge[1]) * currentS;
263
264 if (this->MergePaths)
265 {
266 for (j=newPoints->GetNumberOfPoints()-1; j>=0; j--)
267 {
268 if (((this->Edges->GetComponent(j,0)==currentEdge[0])&&(this->Edges->GetComponent(j,1)==currentEdge[1]))||
269 ((this->Edges->GetComponent(j,0)==currentEdge[1])&&(this->Edges->GetComponent(j,1)==currentEdge[0])))
270 {
271 double newPoint[3];
272 newPoints->GetPoint(j,newPoint);
273 if (sqrt(vtkMath::Distance2BetweenPoints(currentPoint,newPoint)) <= this->MergeTolerance)
274 {
275 pointId = j;
276 lineIds->InsertNextId(pointId);
277 done = true;
278 break;
279 }
280 }
281 }
282 if (done)
283 break;
284 }
285
286 pointId = newPoints->InsertNextPoint(currentPoint);
287 lineIds->InsertNextId(pointId);
288
289 newScalars->InsertTuple1(pointId,currentRadius);
290 this->Edges->InsertComponent(pointId,0,currentEdge[0]);
291 this->Edges->InsertComponent(pointId,1,currentEdge[1]);
292 this->EdgeParCoords->InsertValue(pointId,currentS);
293
294 previousEdge2[0] = previousEdge[0];
295 previousEdge2[1] = previousEdge[1];
296 previousS2 = previousS;
297
298 previousEdge[0] = currentEdge[0];
299 previousEdge[1] = currentEdge[1];
300 previousS = currentS;
301
302 ++numIterations;
303 }
304
305 if (numIterations == 0)
306 {
307 // If no points can be added then we would end up with a line cell
308 // with a single point, which would make VTK9 crash.
309 // Until VTK is fixed (https://gitlab.kitware.com/vtk/vtk/-/merge_requests/9341)
310 // this change can be used to avoid the crash; once VTK fix is merged then this
311 // modification may be removed.
312 // Add the current point again to have a valid cell.
313 pointId = newPoints->InsertNextPoint(currentPoint);
314 lineIds->InsertNextId(pointId);
315 newScalars->InsertTuple1(pointId, currentRadius);
316 this->Edges->InsertComponent(pointId, 0, currentEdge[0]);
317 this->Edges->InsertComponent(pointId, 1, currentEdge[1]);
318 this->EdgeParCoords->InsertValue(pointId, currentS);
319 }
320
321 newLines->InsertNextCell(lineIds);
322
323 neighborCells->Delete();
324 lineIds->Delete();
325}
326
328 vtkInformation *vtkNotUsed(request),
329 vtkInformationVector **inputVector,
330 vtkInformationVector *outputVector)
331{
332 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
333 vtkInformation *outInfo = outputVector->GetInformationObject(0);
334
335 vtkPolyData *input = vtkPolyData::SafeDownCast(
336 inInfo->Get(vtkDataObject::DATA_OBJECT()));
337 vtkPolyData *output = vtkPolyData::SafeDownCast(
338 outInfo->Get(vtkDataObject::DATA_OBJECT()));
339
340 vtkIdType i;
341 vtkPoints* newPoints;
342 vtkCellArray* newLines;
343 vtkDoubleArray* newScalars;
344
345 if (!this->DescentArrayName)
346 {
347 vtkErrorMacro("Descent array name not specified.");
348 return 1;
349 }
350
351 if (!input->GetPointData()->GetArray(this->DescentArrayName))
352 {
353 vtkErrorMacro(<< "Descent array with name specified does not exist!");
354 return 1;
355 }
356
357 if (!this->Seeds)
358 {
359 vtkErrorMacro("No Seeds set");
360 return 1;
361 }
362
363 if (!this->Seeds->GetNumberOfIds())
364 {
365 vtkErrorMacro("Empty Seeds list");
366 return 1;
367 }
368
369 for (i=0; i<this->Seeds->GetNumberOfIds(); i++)
370 {
371 if ((this->Seeds->GetId(i)<0) || (this->Seeds->GetId(i)>input->GetNumberOfPoints()))
372 {
373 vtkErrorMacro("Seed id invalid or exceeds input number of points.");
374 return 1;
375 }
376 }
377
378 if (this->StopOnTargets)
379 {
380 for (i=0; i<this->Targets->GetNumberOfIds(); i++)
381 {
382 if ((this->Targets->GetId(i)<0) || (this->Targets->GetId(i) > input->GetNumberOfPoints()))
383 {
384 vtkErrorMacro("Invalid target id.");
385 return 1;
386 }
387 }
388 }
389
390 this->DescentArray = input->GetPointData()->GetArray(this->DescentArrayName);
391
392 if (this->DataArrayName)
393 {
394 this->LineDataArray = input->GetPointData()->GetArray(this->DataArrayName);
395 if (!this->LineDataArray)
396 {
397 vtkErrorMacro(<< "Line data array with name specified does not exist!");
398 return 1;
399 }
400 }
401 else
402 {
403 this->SetDataArrayName("Data Array");
404 this->LineDataArray = this->DescentArray;
405 }
406
407 this->Edges = vtkIntArray::New();
408 this->EdgeParCoords = vtkDoubleArray::New();
409 this->CellIdsArray = vtkIntArray::New();
410 this->PCoordsArray = vtkDoubleArray::New();
411
412 this->Edges->SetNumberOfComponents(2);
413 this->CellIdsArray->SetNumberOfComponents(2);
414
415 if (this->EdgeArrayName)
416 {
417 this->Edges->SetName(this->EdgeArrayName);
418 }
419 else
420 {
421 this->Edges->SetName("Edges");
422 }
423
424 if (this->EdgePCoordArrayName)
425 {
426 this->EdgeParCoords->SetName(this->EdgePCoordArrayName);
427 }
428 else
429 {
430 this->EdgeParCoords->SetName("EdgePCoords");
431 }
432
433 newPoints = vtkPoints::New();
434 newLines = vtkCellArray::New();
435 newScalars = vtkDoubleArray::New();
436 newScalars->SetName(this->DataArrayName);
437
439 {
441 }
442
443 output->SetPoints(newPoints);
444 output->SetLines(newLines);
445 output->GetPointData()->AddArray(newScalars);
446
447 newPoints->Delete();
448 newLines->Delete();
449 newScalars->Delete();
450
451 input->BuildCells();
452 input->BuildLinks();
453
454 this->HitTargets->Initialize();
455
456 for (i=0; i<this->Seeds->GetNumberOfIds(); i++)
457 {
458 this->Backtrace(input,this->Seeds->GetId(i));
459 }
460
461 output->GetPointData()->AddArray(this->Edges);
462 output->GetPointData()->AddArray(this->EdgeParCoords);
463
464 this->Edges->Delete();
465 this->EdgeParCoords->Delete();
466 this->CellIdsArray->Delete();
467 this->PCoordsArray->Delete();
468
469 return 1;
470}
471
472void vtkvmtkSteepestDescentLineTracer::PrintSelf(std::ostream& os, vtkIndent indent)
473{
474 this->Superclass::PrintSelf(os,indent);
475}
double GetSteepestDescent(vtkPolyData *input, vtkIdType *edge, double s, vtkIdType *steepestDescentEdge, double &steepestDescentS)
void PrintSelf(std::ostream &os, vtkIndent indent) override
Trace the steepest descent line over a polygonal non-manifold.
virtual void SetDataArrayName(const char *)
void PrintSelf(std::ostream &os, vtkIndent indent) override
void Backtrace(vtkPolyData *input, vtkIdType seedId)
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
#define VTK_VMTK_DOUBLE_TOL
vtkStandardNewMacro(vtkvmtkSteepestDescentLineTracer)