VMTK
Loading...
Searching...
No Matches
vtkvmtkActiveTubeFilter.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkActiveTubeFilter.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:48:25 $
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 Portions of this code are covered under the ITK copyright.
17 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm
18 for details.
19
20 This software is distributed WITHOUT ANY WARRANTY; without even
21 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
22 PURPOSE. See the above copyright notices for more information.
23
24=========================================================================*/
25
26
28
30
31#include "vtkvmtkConstants.h"
32#include "vtkMath.h"
33#include "vtkPolyData.h"
34#include "vtkImageData.h"
35#include "vtkPointData.h"
36#include "vtkImageGradient.h"
37#include "vtkDoubleArray.h"
38
39#include "vtkPolyLine.h"
40#include "vtkVoxel.h"
41
42#include "vtkInformation.h"
43#include "vtkInformationVector.h"
44#include "vtkObjectFactory.h"
45#include "vtkVersion.h"
46
47
49
51{
52 this->RadiusArrayName = NULL;
53
54 this->PotentialImage = NULL;
55 this->PotentialGradientImage = NULL;
58 this->Convergence = 1E-1;
59 this->PotentialWeight = 1.0;
60 this->StiffnessWeight = 1.0;
61 this->CFLCoefficient = 0.1;
62
63 this->PotentialMaxNorm = 0.0;
64 this->MinimumRadius = 0.0;
66 this->FixedEndpointRadius = 0;
67
69
70 this->NegativeNormWarnings = 0;
71}
72
74{
75 if (this->RadiusArrayName)
76 {
77 delete[] this->RadiusArrayName;
78 this->RadiusArrayName = NULL;
79 }
80
81 if (this->PotentialImage)
82 {
83 this->PotentialImage->Delete();
84 this->PotentialImage = NULL;
85 }
86
87 if (this->PotentialGradientImage)
88 {
89 this->PotentialGradientImage->Delete();
90 this->PotentialGradientImage = NULL;
91 }
92}
93
94vtkCxxSetObjectMacro(vtkvmtkActiveTubeFilter,PotentialImage,vtkImageData);
95
96void vtkvmtkActiveTubeFilter::EvaluateForce(double point[3], double force[3], bool normalize)
97{
98 int ijk[3];
99 double pcoords[3];
100 double weights[8];
101 int inBounds;
102 int i;
103
104 force[0] = force[1] = force[2] = 0.0;
105
106 inBounds = this->PotentialGradientImage->ComputeStructuredCoordinates(point,ijk,pcoords);
107
108 if (!inBounds)
109 {
110 //vtkWarningMacro("Point out of extent");
111 return;
112 }
113
114 vtkCell* cell = this->PotentialGradientImage->GetCell(this->PotentialGradientImage->ComputeCellId(ijk));
115
116 if (cell->GetCellType() == VTK_VOXEL)
117 {
118 vtkVoxel::InterpolationFunctions(pcoords,weights);
119 }
120 else
121 {
122 vtkErrorMacro("Non voxel cell found in PotentialImage");
123 }
124
125 int numberOfCellPoints = cell->GetNumberOfPoints();
126
127 vtkDataArray* gradientVectorsArray = this->PotentialGradientImage->GetPointData()->GetScalars();
128
129 for (i=0; i<numberOfCellPoints; i++)
130 {
131 double vectorValue[3];
132 gradientVectorsArray->GetTuple(cell->GetPointId(i),vectorValue);
133 force[0] += weights[i] * vectorValue[0];
134 force[1] += weights[i] * vectorValue[1];
135 force[2] += weights[i] * vectorValue[2];
136 }
137
138 if (normalize && this->PotentialMaxNorm > VTK_VMTK_DOUBLE_TOL)
139 {
140 force[0] /= this->PotentialMaxNorm;
141 force[1] /= this->PotentialMaxNorm;
142 force[2] /= this->PotentialMaxNorm;
143 }
144
145 force[0] *= -1.0;
146 force[1] *= -1.0;
147 force[2] *= -1.0;
148}
149
151{
152 int ijk[3];
153 double pcoords[3];
154 double weights[8];
155 int inBounds;
156 int i;
157
158 double potential = 0.0;
159
160 inBounds = this->PotentialImage->ComputeStructuredCoordinates(point,ijk,pcoords);
161
162 if (!inBounds)
163 {
164 //vtkWarningMacro("Point out of extent");
165 return 0.0;
166 }
167
168 vtkCell* cell = this->PotentialImage->GetCell(this->PotentialImage->ComputeCellId(ijk));
169
170 if (cell->GetCellType() == VTK_VOXEL)
171 {
172 vtkVoxel::InterpolationFunctions(pcoords,weights);
173 }
174 else
175 {
176 vtkErrorMacro("Non voxel cell found in PotentialImage");
177 return 0.0;
178 }
179
180 int numberOfCellPoints = cell->GetNumberOfPoints();
181
182 vtkDataArray* potentialArray = this->PotentialImage->GetPointData()->GetScalars();
183
184 for (i=0; i<numberOfCellPoints; i++)
185 {
186 double value = potentialArray->GetTuple1(cell->GetPointId(i));
187 potential += weights[i] * value;
188 }
189
190 return potential;
191}
192
193void vtkvmtkActiveTubeFilter::EvolveCellSpline(vtkPolyData* lines, vtkIdType cellId)
194{
195 //TODO: whip strategy - from start to end of spline, create displacement field simulating
196 //inertia (i.e. force on a point dependent on displacement on previous point)
197
198 vtkDataArray* radiusArray = lines->GetPointData()->GetArray(this->RadiusArrayName);
199
200 vtkPolyLine* polyLine = vtkPolyLine::SafeDownCast(lines->GetCell(cellId));
201
202 if (!polyLine)
203 {
204 return;
205 }
206
207 vtkPoints* polyLinePoints = polyLine->GetPoints();
208
213
214 int numberOfPoints = polyLine->GetNumberOfPoints();
215 int numberOfSubIds = numberOfPoints - 1;
216
217 double cellLength = 0.0;
218 double point0[3], point1[3];
219 int i;
220 for (i=0; i<numberOfSubIds; i++)
221 {
222 polyLinePoints->GetPoint(i,point0);
223 polyLinePoints->GetPoint(i+1,point1);
224 cellLength += sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
225 }
226
227 xSpline->SetParametricRange(0.0,cellLength);
228 ySpline->SetParametricRange(0.0,cellLength);
229 zSpline->SetParametricRange(0.0,cellLength);
230 rSpline->SetParametricRange(0.0,cellLength);
231
232 vtkDoubleArray* parametricCoordinates = vtkDoubleArray::New();
233 parametricCoordinates->SetNumberOfTuples(numberOfPoints);
234
235 double currentLength = 0.0;
236 polyLinePoints->GetPoint(0,point0);
237 double radius = radiusArray->GetTuple1(polyLine->GetPointId(0));
238 double t = 0.0;
239 xSpline->AddPoint(0.0,point0[0]);
240 ySpline->AddPoint(0.0,point0[1]);
241 zSpline->AddPoint(0.0,point0[2]);
242 rSpline->AddPoint(0.0,radius);
243 parametricCoordinates->SetValue(0,0.0);
244 for (i=1; i<numberOfPoints; i++)
245 {
246 polyLinePoints->GetPoint(i-1,point0);
247 polyLinePoints->GetPoint(i,point1);
248 radius = radiusArray->GetTuple1(polyLine->GetPointId(i));
249 currentLength += sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
250 t = currentLength;
251 xSpline->AddPoint(t,point1[0]);
252 ySpline->AddPoint(t,point1[1]);
253 zSpline->AddPoint(t,point1[2]);
254 rSpline->AddPoint(t,radius);
255 parametricCoordinates->SetValue(i,t);
256 }
257
259 {
260 double point[3];
261 for (i=0; i<numberOfPoints; i++)
262 {
263 t = (double)i / (numberOfPoints-1) * cellLength;
264 point[0] = xSpline->Evaluate(t);
265 point[1] = ySpline->Evaluate(t);
266 point[2] = zSpline->Evaluate(t);
267 radius = rSpline->Evaluate(t);
268 polyLinePoints->SetPoint(i,point);
269 radiusArray->SetTuple1(i,radius);
270 parametricCoordinates->SetValue(i,t);
271 }
272 }
273
274 vtkDoubleArray* probedForces = vtkDoubleArray::New();
275 probedForces->SetNumberOfComponents(3);
276 probedForces->SetNumberOfTuples(this->NumberOfAngularEvaluations);
277
278 vtkDoubleArray* tubeNormals = vtkDoubleArray::New();
279 tubeNormals->SetNumberOfComponents(3);
280 tubeNormals->SetNumberOfTuples(this->NumberOfAngularEvaluations);
281
282 vtkDoubleArray* dxArray = vtkDoubleArray::New();
283 dxArray->SetNumberOfTuples(numberOfPoints);
284 dxArray->FillComponent(0,0.0);
285 vtkDoubleArray* dyArray = vtkDoubleArray::New();
286 dyArray->SetNumberOfTuples(numberOfPoints);
287 dyArray->FillComponent(0,0.0);
288 vtkDoubleArray* dzArray = vtkDoubleArray::New();
289 dzArray->SetNumberOfTuples(numberOfPoints);
290 dzArray->FillComponent(0,0.0);
291 vtkDoubleArray* drArray = vtkDoubleArray::New();
292 drArray->SetNumberOfTuples(numberOfPoints);
293 drArray->FillComponent(0,0.0);
294
295 //TODO: choose numberOfLongitudinalEvaluations with a strategy
296 // (fixed number, based on length, adaptive - higher curve or radius derivatives, more points)
297 int numberOfLongitudinalEvaluations = numberOfPoints * 3 / 2;
298
299 for (i=0; i<numberOfLongitudinalEvaluations; i++)
300 {
301 t = (double)i / (numberOfLongitudinalEvaluations-1) * cellLength;
302 double x = xSpline->Evaluate(t);
303 double y = ySpline->Evaluate(t);
304 double z = zSpline->Evaluate(t);
305 double r = rSpline->Evaluate(t);
306 double xp = xSpline->EvaluateDerivative(t);
307 double yp = ySpline->EvaluateDerivative(t);
308 double zp = zSpline->EvaluateDerivative(t);
309 double rp = rSpline->EvaluateDerivative(t);
310 double xpp = xSpline->EvaluateSecondDerivative(t);
311 double ypp = ySpline->EvaluateSecondDerivative(t);
312 double zpp = zSpline->EvaluateSecondDerivative(t);
313 double rpp = rSpline->EvaluateSecondDerivative(t);
314
315 double tangent[3], normal[3];
316
317 tangent[0] = xp;
318 tangent[1] = yp;
319 tangent[2] = zp;
320
321 double tubeNormSquared = xp * xp + yp * yp + zp * zp - rp * rp;
322
323 //if (tubeNormSquared < 0.0)
324 if (tubeNormSquared <= 0.0)
325 {
326 if (this->NegativeNormWarnings)
327 {
328 vtkWarningMacro("Negative tubeNormSquared. Skipping.");
329 }
330 continue;
331 }
332
333 double tubeNorm = sqrt(tubeNormSquared);
334
335 double isotropicForce = 0.0;
336 double anisotropicForce[3];
337 anisotropicForce[0] = anisotropicForce[1] = anisotropicForce[2] = 0.0;
338
339 double tubeNormal[3], probePoint[3], probedForce[3];
340 double theta;
341 int j;
342 for (j=0; j<this->NumberOfAngularEvaluations; j++)
343 {
344 theta = j * 2.0 * vtkMath::Pi() / this->NumberOfAngularEvaluations;
345
346 vtkMath::Perpendiculars(tangent,normal,NULL,theta);
347
348 tubeNormal[0] = - tangent[0] * rp + normal[0] * tubeNorm;
349 tubeNormal[1] = - tangent[1] * rp + normal[1] * tubeNorm;
350 tubeNormal[2] = - tangent[2] * rp + normal[2] * tubeNorm;
351 //optional, but better be on the safe side
352 vtkMath::Normalize(tubeNormal);
353
354 probePoint[0] = x + tubeNormal[0] * r;
355 probePoint[1] = y + tubeNormal[1] * r;
356 probePoint[2] = z + tubeNormal[2] * r;
357
358 //double probedPotential = this->EvaluatePotential(probePoint);
359
360 this->EvaluateForce(probePoint,probedForce,false);
361
362 probedForces->SetTuple(j,probedForce);
363 tubeNormals->SetTuple(j,tubeNormal);
364
365 isotropicForce += vtkMath::Dot(probedForce,tubeNormal);
366 }
367
368 isotropicForce /= this->NumberOfAngularEvaluations;
369
370 for (j=0; j<this->NumberOfAngularEvaluations; j++)
371 {
372 probedForces->GetTuple(j,probedForce);
373 tubeNormals->GetTuple(j,tubeNormal);
374 anisotropicForce[0] += probedForce[0] - tubeNormal[0] * isotropicForce;
375 anisotropicForce[1] += probedForce[1] - tubeNormal[1] * isotropicForce;
376 anisotropicForce[2] += probedForce[2] - tubeNormal[2] * isotropicForce;
377 }
378
379 anisotropicForce[0] /= this->NumberOfAngularEvaluations;
380 anisotropicForce[1] /= this->NumberOfAngularEvaluations;
381 anisotropicForce[2] /= this->NumberOfAngularEvaluations;
382
383 //TODO: define influence (based on parametric distance? Or also consider derivatives?)
384 // implement Gaussian RBF?
385 for (j=0; j<numberOfPoints; j++)
386 {
387 double parametricCoordinate = parametricCoordinates->GetValue(j);
388 double influence = 0.1 * cellLength;
389 if (fabs(parametricCoordinate-t) > influence)
390 {
391 continue;
392 }
393 double weight = (influence - fabs(parametricCoordinate-t)) / influence;
394 double dx, dy, dz, dr;
395 dx = dxArray->GetValue(j);
396 dy = dyArray->GetValue(j);
397 dz = dzArray->GetValue(j);
398 dr = drArray->GetValue(j);
399 dx += anisotropicForce[0] * weight * this->PotentialWeight;
400 dy += anisotropicForce[1] * weight * this->PotentialWeight;
401 dz += anisotropicForce[2] * weight * this->PotentialWeight;
402 dr += isotropicForce * weight * this->PotentialWeight;
403 dx += xpp * weight * this->StiffnessWeight;
404 dy += ypp * weight * this->StiffnessWeight;
405 dz += zpp * weight * this->StiffnessWeight;
406 dr += rpp * weight * this->StiffnessWeight;
407 dxArray->SetValue(j,dx);
408 dyArray->SetValue(j,dy);
409 dzArray->SetValue(j,dz);
410 drArray->SetValue(j,dr);
411 }
412 }
413
414 double spacing[3];
415#if 0
416 if (this->PotientialImage)
417 {
418 this->PotentialImage->GetSpacing(spacing);
419 }
420 else
421#endif
422 {
423 this->PotentialGradientImage->GetSpacing(spacing);
424 }
425
426 double minSpacing = VTK_VMTK_LARGE_DOUBLE;
427 for (i=0; i<3; i++)
428 {
429 minSpacing = spacing[i] < minSpacing ? spacing[i] : minSpacing;
430 }
431
432 double maxChange = 0.0;
433 for (i=0; i<numberOfPoints; i++)
434 {
435 double dx = dxArray->GetValue(i);
436 double dy = dyArray->GetValue(i);
437 double dz = dzArray->GetValue(i);
438 double dr = drArray->GetValue(i);
439 double change = sqrt(dx*dx + dy*dy + dz*dz) + dr;
440 maxChange = change > maxChange ? change : maxChange;
441 }
442
443 double timeStep;
444 if (maxChange > 0.0)
445 {
446 timeStep = minSpacing / maxChange * this->CFLCoefficient;
447 }
448 else
449 {
450 timeStep = 0.0;
451 }
452
453 vtkIdType pointId;
454 double point[3];
455 for (i=0; i<numberOfPoints; i++)
456 {
457 pointId = polyLine->GetPointId(i);
458 polyLinePoints->GetPoint(i,point);
459 radius = radiusArray->GetTuple1(pointId);
460 point[0] += dxArray->GetValue(i) * timeStep;
461 point[1] += dyArray->GetValue(i) * timeStep;
462 point[2] += dzArray->GetValue(i) * timeStep;
463 radius += drArray->GetValue(i) * timeStep;
464 if (radius < this->MinimumRadius)
465 {
466 radius = this->MinimumRadius;
467 }
468 if (!(this->FixedEndpointCoordinates && (i==0 || i==numberOfPoints-1)))
469 {
470 lines->GetPoints()->SetPoint(pointId,point);
471 }
472 if (!(this->FixedEndpointRadius && (i==0 || i==numberOfPoints-1)))
473 {
474 radiusArray->SetTuple1(pointId,radius);
475 }
476 }
477
478 //TODO: longitudinal evolution
479
480 xSpline->Delete();
481 ySpline->Delete();
482 zSpline->Delete();
483 rSpline->Delete();
484 parametricCoordinates->Delete();
485 probedForces->Delete();
486 tubeNormals->Delete();
487 dxArray->Delete();
488 dyArray->Delete();
489 dzArray->Delete();
490 drArray->Delete();
491}
492
493int vtkvmtkActiveTubeFilter::RequestData(vtkInformation *vtkNotUsed(request), vtkInformationVector **inputVector, vtkInformationVector *outputVector)
494{
495 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
496 vtkInformation *outInfo = outputVector->GetInformationObject(0);
497
498 vtkPolyData *input = vtkPolyData::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
499 vtkPolyData *output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
500
501 output->DeepCopy(input);
502
503 if (!this->RadiusArrayName)
504 {
505 vtkErrorMacro("Error: RadiusArrayName not specified.");
506 return 0;
507 }
508
509 vtkDataArray* radiusArray = output->GetPointData()->GetArray(this->RadiusArrayName);
510
511 if (!radiusArray)
512 {
513 vtkErrorMacro("Error: RadiusArray with name specified does not exist.");
514 return 0;
515 }
516
517 vtkImageGradient* gradientFilter = vtkImageGradient::New();
518 gradientFilter->SetInputData(this->PotentialImage);
519 gradientFilter->SetDimensionality(3);
520 gradientFilter->Update();
521
522 if (this->PotentialGradientImage)
523 {
524 this->PotentialGradientImage->Delete();
525 this->PotentialGradientImage = NULL;
526 }
527
528 this->PotentialGradientImage = vtkImageData::New();
529 this->PotentialGradientImage->DeepCopy(gradientFilter->GetOutput());
530
531 this->PotentialMaxNorm = this->PotentialGradientImage->GetPointData()->GetScalars()->GetMaxNorm();
532
533 int numberOfCells = output->GetNumberOfCells();
534
535 int i;
536 for (i=0; i<this->NumberOfIterations; i++)
537 {
538 int c;
539 for (c=0; c<numberOfCells; c++)
540 {
541 this->EvolveCellSpline(output,c);
542 }
543 }
544
545 gradientFilter->Delete();
546
547 return 1;
548}
549
550void vtkvmtkActiveTubeFilter::PrintSelf(std::ostream& os, vtkIndent indent)
551{
552 this->Superclass::PrintSelf(os,indent);
553}
554
Experimental: generates centerlines from an image.
void EvolveCellSpline(vtkPolyData *lines, vtkIdType cellId)
void PrintSelf(std::ostream &os, vtkIndent indent) override
double EvaluatePotential(double point[3])
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void EvaluateForce(double point[3], double force[3], bool normalize)
Extends vtkCardinalSpline with methods to evaluate the spline's first derivative, second derivative,...
virtual double EvaluateDerivative(double t)
virtual double EvaluateSecondDerivative(double t)
static vtkvmtkCardinalSpline * New()
vtkStandardNewMacro(vtkvmtkActiveTubeFilter)
vtkCxxSetObjectMacro(vtkvmtkActiveTubeFilter, PotentialImage, vtkImageData)
#define VTK_VMTK_LARGE_INTEGER
#define VTK_VMTK_DOUBLE_TOL
#define VTK_VMTK_LARGE_DOUBLE