VMTK
Loading...
Searching...
No Matches
vtkvmtkPolyDataFlowExtensionsFilter.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkPolyDataFlowExtensionsFilter.cxx,v $
5Language: C++
6Date: $Date: 2006/07/07 10:46:19 $
7Version: $Revision: 1.12 $
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
26#include "vtkvmtkPolyBallLine.h"
27#include "vtkvmtkMath.h"
28#include "vtkThinPlateSplineTransform.h"
29#include "vtkTransform.h"
30#include "vtkPolyLine.h"
31#include "vtkPointData.h"
32#include "vtkDoubleArray.h"
33#include "vtkIntArray.h"
34#include "vtkIdTypeArray.h"
35#include "vtkMath.h"
36#include "vtkCellArray.h"
37#include "vtkInformation.h"
38#include "vtkInformationVector.h"
39#include "vtkNew.h"
40#include "vtkObjectFactory.h"
41#include "vtkVersion.h"
42
43#include <vector>
44
45
47
48namespace {
49
50// Resamples a closed polygon into numberOfPoints points equally spaced along its perimeter. The
51// first sample is placed at offsetRatio times the sampling step past the polygon's first point,
52// so that offsetRatio 0.5 gives the set of points staggered halfway between the unshifted ones.
53// The polygon is implicitly closed: the segment from its last to its first point is part of the
54// outline. If companionPoints is given, it must have as many points as the polygon and is sampled
55// at the same segment and parametric coordinate as the polygon itself, which pairs each sample
56// with its counterpart on the companion polygon. Returns the perimeter of the polygon.
57double ResampleClosedPolygon(vtkPoints* polygonPoints, int numberOfPoints, double offsetRatio, vtkPoints* resampledPoints, vtkPoints* companionPoints = nullptr, vtkPoints* resampledCompanionPoints = nullptr)
58{
59 vtkIdType numberOfPolygonPoints = polygonPoints->GetNumberOfPoints();
60
61 std::vector<double> arcLength(numberOfPolygonPoints+1);
62 arcLength[0] = 0.0;
63 double point0[3], point1[3];
64 vtkIdType i;
65 for (i=0; i<numberOfPolygonPoints; i++)
66 {
67 polygonPoints->GetPoint(i,point0);
68 polygonPoints->GetPoint((i+1)%numberOfPolygonPoints,point1);
69 arcLength[i+1] = arcLength[i] + sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
70 }
71 double perimeter = arcLength[numberOfPolygonPoints];
72
73 double step = perimeter / numberOfPoints;
74 vtkIdType segmentId = 0;
75 int j, k;
76 double resampledPoint[3];
77 for (j=0; j<numberOfPoints; j++)
78 {
79 // stations are increasing, so the segment they fall in can be tracked incrementally
80 double station = (j + offsetRatio) * step;
81 while (segmentId < numberOfPolygonPoints-1 && arcLength[segmentId+1] < station)
82 {
83 segmentId++;
84 }
85 double segmentLength = arcLength[segmentId+1] - arcLength[segmentId];
86 double parametricCoordinate = 0.0;
87 if (segmentLength > 0.0)
88 {
89 parametricCoordinate = (station - arcLength[segmentId]) / segmentLength;
90 parametricCoordinate = parametricCoordinate < 0.0 ? 0.0 : (parametricCoordinate > 1.0 ? 1.0 : parametricCoordinate);
91 }
92 polygonPoints->GetPoint(segmentId,point0);
93 polygonPoints->GetPoint((segmentId+1)%numberOfPolygonPoints,point1);
94 for (k=0; k<3; k++)
95 {
96 resampledPoint[k] = point0[k] + parametricCoordinate * (point1[k] - point0[k]);
97 }
98 resampledPoints->InsertNextPoint(resampledPoint);
99
100 if (companionPoints && resampledCompanionPoints)
101 {
102 companionPoints->GetPoint(segmentId,point0);
103 companionPoints->GetPoint((segmentId+1)%numberOfPolygonPoints,point1);
104 for (k=0; k<3; k++)
105 {
106 resampledPoint[k] = point0[k] + parametricCoordinate * (point1[k] - point0[k]);
107 }
108 resampledCompanionPoints->InsertNextPoint(resampledPoint);
109 }
110 }
111
112 return perimeter;
113}
114
115// Samples a closed polygon along numberOfPoints rays cast from center within the plane orthogonal
116// to planeNormal, the j-th ray pointing at (j + offsetRatio) * angleStep degrees from
117// startDirection, so that the samples pair up with the points of a ring built at the same angular
118// stations. The polygon is sampled where the ray leaves it, that is at the outermost crossing, and
119// the sample is taken on companionPoints, which must have as many points as the polygon, at the
120// same segment and parametric coordinate. Returns false, leaving sampledPoints incomplete, if any
121// ray misses the polygon, which happens when the polygon is not star-shaped about center.
122bool SampleClosedPolygonByAngle(vtkPoints* polygonPoints, vtkPoints* companionPoints, double center[3], double planeNormal[3], double startDirection[3], double angleStep, double offsetRatio, int numberOfPoints, vtkPoints* sampledPoints)
123{
124 vtkIdType numberOfPolygonPoints = polygonPoints->GetNumberOfPoints();
125
126 double firstAxis[3], secondAxis[3];
127 int k;
128 for (k=0; k<3; k++)
129 {
130 firstAxis[k] = startDirection[k];
131 }
132 vtkMath::Cross(planeNormal,firstAxis,secondAxis);
133
134 // in-plane coordinates of the polygon points relative to the center
135 std::vector<double> firstCoordinate(numberOfPolygonPoints), secondCoordinate(numberOfPolygonPoints);
136 double point[3], centerToPoint[3];
137 vtkIdType i;
138 for (i=0; i<numberOfPolygonPoints; i++)
139 {
140 polygonPoints->GetPoint(i,point);
141 for (k=0; k<3; k++)
142 {
143 centerToPoint[k] = point[k] - center[k];
144 }
145 firstCoordinate[i] = vtkMath::Dot(centerToPoint,firstAxis);
146 secondCoordinate[i] = vtkMath::Dot(centerToPoint,secondAxis);
147 }
148
149 double point0[3], point1[3], sampledPoint[3];
150 int j;
151 for (j=0; j<numberOfPoints; j++)
152 {
153 double angle = vtkMath::RadiansFromDegrees((j + offsetRatio) * angleStep);
154 double cosAngle = cos(angle);
155 double sinAngle = sin(angle);
156
157 // the ray is the positive half of the axis the polygon points are projected onto below; a
158 // segment crosses it where its distance to the ray's line changes sign
159 double bestRadius = 0.0;
160 vtkIdType bestSegmentId = -1;
161 double bestParametricCoordinate = 0.0;
162 for (i=0; i<numberOfPolygonPoints; i++)
163 {
164 vtkIdType nextId = (i+1)%numberOfPolygonPoints;
165 double distance0 = -firstCoordinate[i]*sinAngle + secondCoordinate[i]*cosAngle;
166 double distance1 = -firstCoordinate[nextId]*sinAngle + secondCoordinate[nextId]*cosAngle;
167 if (!((distance0 <= 0.0 && distance1 > 0.0) || (distance0 > 0.0 && distance1 <= 0.0)))
168 {
169 continue;
170 }
171 double parametricCoordinate = distance0 / (distance0 - distance1);
172 double radius0 = firstCoordinate[i]*cosAngle + secondCoordinate[i]*sinAngle;
173 double radius1 = firstCoordinate[nextId]*cosAngle + secondCoordinate[nextId]*sinAngle;
174 double radius = radius0 + parametricCoordinate * (radius1 - radius0);
175 if (radius > bestRadius)
176 {
177 bestRadius = radius;
178 bestSegmentId = i;
179 bestParametricCoordinate = parametricCoordinate;
180 }
181 }
182
183 if (bestSegmentId == -1)
184 {
185 return false;
186 }
187
188 companionPoints->GetPoint(bestSegmentId,point0);
189 companionPoints->GetPoint((bestSegmentId+1)%numberOfPolygonPoints,point1);
190 for (k=0; k<3; k++)
191 {
192 sampledPoint[k] = point0[k] + bestParametricCoordinate * (point1[k] - point0[k]);
193 }
194 sampledPoints->InsertNextPoint(sampledPoint);
195 }
196
197 return true;
198}
199
200}
201
203{
204 this->Centerlines = NULL;
205 this->ExtensionRatio = 1.0;
206 this->TransitionRatio = 0.5;
207 this->ExtensionLength = 0.0;
208 this->ExtensionRadius = 1.0;
209 this->ExtensionLengthScaleFactors = NULL;
210 this->BoundaryLabelsArrayName = NULL;
211 this->BoundaryPointOrderArrayName = NULL;
213 this->AdaptiveExtensionLength = 1;
214 this->AdaptiveExtensionRadius = 1;
216 this->NumberOfBoundaryPoints = 50;
218 this->BoundaryIds = NULL;
219 this->Sigma = 1.0;
222}
223
225{
226 if (this->Centerlines)
227 {
228 this->Centerlines->Delete();
229 this->Centerlines = NULL;
230 }
231
232 if (this->BoundaryIds)
233 {
234 this->BoundaryIds->Delete();
235 this->BoundaryIds = NULL;
236 }
237
239 {
240 this->ExtensionLengthScaleFactors->Delete();
241 this->ExtensionLengthScaleFactors = NULL;
242 }
243
244 this->SetBoundaryLabelsArrayName(NULL);
246}
247
249 vtkInformation *vtkNotUsed(request),
250 vtkInformationVector **inputVector,
251 vtkInformationVector *outputVector)
252{
253 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
254 vtkInformation *outInfo = outputVector->GetInformationObject(0);
255
256 vtkPolyData *input = vtkPolyData::SafeDownCast(
257 inInfo->Get(vtkDataObject::DATA_OBJECT()));
258 vtkPolyData *output = vtkPolyData::SafeDownCast(
259 outInfo->Get(vtkDataObject::DATA_OBJECT()));
260
262 {
263 if (!this->Centerlines)
264 {
265 vtkErrorMacro(<< "Centerlines not set.");
266 return 1;
267 }
268 }
269
270 vtkNew<vtkPoints> outputPoints;
271 vtkNew<vtkCellArray> outputPolys;
272
273 outputPoints->DeepCopy(input->GetPoints());
274 outputPolys->DeepCopy(input->GetPolys());
275
276 // The boundaries either come from the labels the input already carries, in which case they
277 // cost a bucket sort rather than an extraction and, more to the point, are the very same
278 // boundaries every other filter reading those labels sees, or they are extracted here as they
279 // always were.
280 bool useBoundaryLabels = false;
281
282 // Outlives the block below: the label of each boundary is needed wherever a boundary id is
283 // read, which with the labels in use is the label rather than the position.
284 vtkNew<vtkIdList> boundaryLabels;
285 vtkSmartPointer<vtkPolyData> boundaries;
288 {
289 vtkNew<vtkPolyData> labeledBoundaries;
290 useBoundaryLabels = vtkvmtkBoundaryLabels::GetBoundaries(
292 labeledBoundaries, boundaryLabels);
293 if (useBoundaryLabels)
294 {
295 boundaries = labeledBoundaries;
296 }
297 else
298 {
299 vtkWarningMacro(<<"The boundary label arrays are missing from the input surface or no longer describe it; "
300 <<"extracting its boundaries instead, and leaving the output unlabeled.");
301 }
302 }
303 if (!boundaries)
304 {
305 vtkNew<vtkvmtkPolyDataBoundaryExtractor> boundaryExtractor;
306 boundaryExtractor->SetInputData(input);
307 boundaryExtractor->Update();
308 boundaries = boundaryExtractor->GetOutput();
309 }
310
311 vtkNew<vtkPolyData> centerlines;
312 vtkNew<vtkvmtkPolyBallLine> tube;
313 vtkNew<vtkDoubleArray> zeroRadiusArray;
314
316 {
317 centerlines->DeepCopy(this->Centerlines);
318
319 const char zeroRadiusArrayName[] = "ZeroRadiusArray";
320
321 zeroRadiusArray->SetName(zeroRadiusArrayName);
322 zeroRadiusArray->SetNumberOfTuples(centerlines->GetNumberOfPoints());
323 zeroRadiusArray->FillComponent(0,0.0);
324
325 centerlines->GetPointData()->AddArray(zeroRadiusArray);
326
327 tube->SetInput(centerlines);
328 tube->SetPolyBallRadiusArrayName(zeroRadiusArrayName);
329 }
330
331 input->BuildCells();
332 input->BuildLinks();
333
334 // Per boundary, the ring it started from and the ring it ended as, so that its label can be
335 // moved from the one to the other once the mesh is built. Empty for a boundary that was not
336 // extended, whose own ring is still a boundary of the output and keeps its label where it is.
337 std::vector<std::vector<vtkIdType> > originalRingByBoundary(boundaries->GetNumberOfCells());
338 std::vector<std::vector<vtkIdType> > tipRingByBoundary(boundaries->GetNumberOfCells());
339
340 int i, k;
341 for (i=0; i<boundaries->GetNumberOfCells(); i++)
342 {
343 if (this->BoundaryIds)
344 {
345 // A boundary is named by its label when the labels are in use, and by its position in the
346 // extraction order otherwise, here as everywhere else in this filter.
347 if (this->BoundaryIds->IsId(useBoundaryLabels ? boundaryLabels->GetId(i) : i) == -1)
348 {
349 continue;
350 }
351 }
352
353 vtkPolyLine* boundary = vtkPolyLine::SafeDownCast(boundaries->GetCell(i));
354
355 if (!boundary)
356 {
357 vtkErrorMacro(<<"Boundary not a vtkPolyLine");
358 continue;
359 }
360
361 int numberOfBoundaryPoints = boundary->GetNumberOfPoints();
362
363 vtkNew<vtkIdList> boundaryIds;
364 int j;
365 for (j=0; j<numberOfBoundaryPoints; j++)
366 {
367 boundaryIds->InsertNextId(static_cast<vtkIdType>(vtkMath::Round(boundaries->GetPointData()->GetScalars()->GetComponent(boundary->GetPointId(j),0))));
368 }
369
370 double barycenter[3];
371 double normal[3], outwardNormal[3];
372 double meanRadius;
373
374 vtkvmtkBoundaryReferenceSystems::ComputeBoundaryBarycenter(boundary->GetPoints(),barycenter);
375 meanRadius = vtkvmtkBoundaryReferenceSystems::ComputeBoundaryMeanRadius(boundary->GetPoints(),barycenter);
376 vtkvmtkBoundaryReferenceSystems::ComputeBoundaryNormal(boundary->GetPoints(),barycenter,normal);
377 vtkvmtkBoundaryReferenceSystems::OrientBoundaryNormalOutwards(input,boundaries,i,normal,outwardNormal);
378
379 double flowExtensionNormal[3];
380 flowExtensionNormal[0] = flowExtensionNormal[1] = flowExtensionNormal[2] = 0.0;
381
383 {
384 tube->EvaluateFunction(barycenter);
385
386 double centerlinePoint[3];
387 vtkIdType cellId, subId;
388 double pcoord;
389 tube->GetLastPolyBallCenter(centerlinePoint);
390 cellId = tube->GetLastPolyBallCellId();
391 subId = tube->GetLastPolyBallCellSubId();
392 pcoord = tube->GetLastPolyBallCellPCoord();
393
394 vtkCell* centerline = centerlines->GetCell(cellId);
395
396 vtkIdType pointId0, pointId1;
397 double abscissa;
398
399 double point0[3], point1[3];
400
401 pointId0 = 0;
402 abscissa = sqrt(vtkMath::Distance2BetweenPoints(centerlinePoint,centerline->GetPoints()->GetPoint(subId)));
403 for (j=subId-1; j>=0; j--)
404 {
405 centerline->GetPoints()->GetPoint(j,point0);
406 centerline->GetPoints()->GetPoint(j+1,point1);
407 abscissa += sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
408 if (abscissa > meanRadius * this->CenterlineNormalEstimationDistanceRatio)
409 {
410 pointId0 = j;
411 break;
412 }
413 }
414
415 pointId1 = centerline->GetNumberOfPoints()-1;
416 abscissa = sqrt(vtkMath::Distance2BetweenPoints(centerlinePoint,centerline->GetPoints()->GetPoint(subId+1)));
417 for (j=subId+1; j<centerline->GetNumberOfPoints()-2; j++)
418 {
419 centerline->GetPoints()->GetPoint(j,point0);
420 centerline->GetPoints()->GetPoint(j+1,point1);
421 abscissa += sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
422 if (abscissa > meanRadius * this->CenterlineNormalEstimationDistanceRatio)
423 {
424 pointId1 = j+1;
425 break;
426 }
427 }
428
429 // use an approximating spline or smooth centerline points to better catch the trend in computing centerlineNormal?
430
431 double centerlineNormal[3];
432
433 centerline->GetPoints()->GetPoint(pointId0,point0);
434 centerline->GetPoints()->GetPoint(pointId1,point1);
435
436 double toleranceFactor = 1E-4;
437
438 for (k=0; k<3; k++)
439 {
440 centerlineNormal[k] = 0.0;
441 }
442 if (sqrt(vtkMath::Distance2BetweenPoints(point1,centerlinePoint)) > toleranceFactor*meanRadius)
443 {
444 for (k=0; k<3; k++)
445 {
446 centerlineNormal[k] += point1[k] - centerlinePoint[k];
447 }
448 }
449 if (sqrt(vtkMath::Distance2BetweenPoints(centerlinePoint,point0)) > toleranceFactor*meanRadius)
450 {
451 for (k=0; k<3; k++)
452 {
453 centerlineNormal[k] += centerlinePoint[k] - point0[k];
454 }
455 }
456
457 vtkMath::Normalize(centerlineNormal);
458
459 for (k=0; k<3; k++)
460 {
461 flowExtensionNormal[k] = centerlineNormal[k];
462 }
463
464 if (vtkMath::Dot(outwardNormal,centerlineNormal) < 0.0)
465 {
466 for (k=0; k<3; k++)
467 {
468 flowExtensionNormal[k] *= -1.0;
469 }
470 }
471 }
472 else if (this->ExtensionMode == USE_NORMAL_TO_BOUNDARY)
473 {
474 for (k=0; k<3; k++)
475 {
476 flowExtensionNormal[k] = outwardNormal[k];
477 }
478 }
479 else
480 {
481 vtkErrorMacro(<< "Invalid ExtensionMode.");
482 return 1;
483 }
484
485 double extensionLength;
486
487 if (this->AdaptiveExtensionLength)
488 {
489 extensionLength = meanRadius * this->ExtensionRatio;
490 }
491 else
492 {
493 extensionLength = this->ExtensionLength;
494 }
495
497 {
498 vtkIdType boundaryId = useBoundaryLabels ? boundaryLabels->GetId(i) : i;
499 if (boundaryId < this->ExtensionLengthScaleFactors->GetNumberOfTuples())
500 {
501 extensionLength *= this->ExtensionLengthScaleFactors->GetValue(boundaryId);
502 }
503 }
504
505 double point[3], extensionPoint[3];
506
507 // Project the boundary onto the plane through the barycenter orthogonal to the extension
508 // direction. This is the outline the extension is swept from when PreserveCrossSectionShape
509 // is on, and its mean radius is the extension radius when AdaptiveExtensionRadius is on.
510 vtkNew<vtkPoints> projectedBoundaryPoints;
511 double meanProjectedRadius = 0.0;
512
513 double barycenterToPoint[3];
514 double outOfPlaneDistance;
515 double projectedPoint[3];
516 for (j=0; j<numberOfBoundaryPoints; j++)
517 {
518 boundary->GetPoints()->GetPoint(j,point);
519 for (k=0; k<3; k++)
520 {
521 barycenterToPoint[k] = point[k] - barycenter[k];
522 }
523 outOfPlaneDistance = vtkMath::Dot(barycenterToPoint,flowExtensionNormal);
524 for (k=0; k<3; k++)
525 {
526 barycenterToPoint[k] -= outOfPlaneDistance*flowExtensionNormal[k];
527 projectedPoint[k] = barycenter[k] + barycenterToPoint[k];
528 }
529 projectedBoundaryPoints->InsertNextPoint(projectedPoint);
530 meanProjectedRadius += vtkMath::Norm(barycenterToPoint);
531 }
532 meanProjectedRadius /= numberOfBoundaryPoints;
533
534 double targetRadius = 0.0;
535
536 if (this->AdaptiveExtensionRadius)
537 {
538 targetRadius = meanProjectedRadius;
539 }
540 else
541 {
542 targetRadius = this->ExtensionRadius;
543 }
544
545 if (this->PreserveCrossSectionShape && meanProjectedRadius < 1E-4 * meanRadius)
546 {
547 vtkWarningMacro(<<"Degenerate boundary outline, skipping flow extension for boundary "<<i);
548 continue;
549 }
550
551 vtkNew<vtkIdList> newBoundaryIds;
552 vtkNew<vtkIdList> previousBoundaryIds;
553 vtkIdType pointId;
554
555 previousBoundaryIds->DeepCopy(boundaryIds);
556
557 // TODO: use area, not meanRadius as targetRadius
558
559 int targetNumberOfBoundaryPoints = this->NumberOfBoundaryPoints;
561 {
562 targetNumberOfBoundaryPoints = numberOfBoundaryPoints;
563 }
564
565 double targetDistanceBetweenPoints = 0.0;
566
567 vtkNew<vtkThinPlateSplineTransform> thinPlateSplineTransform;
568 thinPlateSplineTransform->SetSigma(this->Sigma);
569 thinPlateSplineTransform->SetBasisToR2LogR();
570// thinPlateSplineTransform->SetBasisToR();
571
572 vtkNew<vtkPoints> sourceLandmarks;
573 vtkNew<vtkPoints> targetLandmarks;
574
575 vtkNew<vtkPoints> targetBoundaryPoints;
576 vtkNew<vtkPoints> targetStaggeredBoundaryPoints;
577
578 // The ramp interpolation modes need, for each point of the target cross-section, the point of
579 // the real boundary it grows from. The pairing is built along with the target cross-section
580 // itself, so that it is ordered and one-to-one whatever the shape of the boundary.
581 bool useRampInterpolation = (this->InterpolationMode == USE_LINEAR_INTERPOLATION) || (this->InterpolationMode == USE_RAMP_INTERPOLATION);
582 vtkNew<vtkPoints> rimBoundaryPoints;
583 vtkNew<vtkPoints> rimStaggeredBoundaryPoints;
584
585 int startNumberOfBoundaryPoints = numberOfBoundaryPoints;
586
588 {
589 // The target cross-section is the boundary's own outline, projected onto the plane
590 // orthogonal to the extension direction and uniformly resampled along its perimeter. Since
591 // the samples follow the order of the boundary points, their winding matches the boundary's.
592 // Sampling the boundary itself at the same stations pairs each target point with the point
593 // it is the projection of.
594 vtkPoints* rimPoints = useRampInterpolation ? rimBoundaryPoints.GetPointer() : nullptr;
595 vtkPoints* rimStaggeredPoints = useRampInterpolation ? rimStaggeredBoundaryPoints.GetPointer() : nullptr;
596 double perimeter = ResampleClosedPolygon(projectedBoundaryPoints,targetNumberOfBoundaryPoints,0.0,targetBoundaryPoints,boundary->GetPoints(),rimPoints);
597 ResampleClosedPolygon(projectedBoundaryPoints,targetNumberOfBoundaryPoints,0.5,targetStaggeredBoundaryPoints,boundary->GetPoints(),rimStaggeredPoints);
598
599 double targetPoint[3];
600 double scale = 1.0;
601
602 if (!this->AdaptiveExtensionRadius)
603 {
604 // scale the outline about the barycenter so that its mean radius is the requested
605 // extension radius; the outline is left at its natural size when the radius is adaptive
606 double meanResampledRadius = 0.0;
607 for (j=0; j<targetNumberOfBoundaryPoints; j++)
608 {
609 targetBoundaryPoints->GetPoint(j,targetPoint);
610 for (k=0; k<3; k++)
611 {
612 targetPoint[k] -= barycenter[k];
613 }
614 meanResampledRadius += vtkMath::Norm(targetPoint);
615 }
616 meanResampledRadius /= targetNumberOfBoundaryPoints;
617 scale = targetRadius / meanResampledRadius;
618
619 for (j=0; j<targetNumberOfBoundaryPoints; j++)
620 {
621 targetBoundaryPoints->GetPoint(j,targetPoint);
622 for (k=0; k<3; k++)
623 {
624 targetPoint[k] = barycenter[k] + scale * (targetPoint[k] - barycenter[k]);
625 }
626 targetBoundaryPoints->SetPoint(j,targetPoint);
627 targetStaggeredBoundaryPoints->GetPoint(j,targetPoint);
628 for (k=0; k<3; k++)
629 {
630 targetPoint[k] = barycenter[k] + scale * (targetPoint[k] - barycenter[k]);
631 }
632 targetStaggeredBoundaryPoints->SetPoint(j,targetPoint);
633 }
634 }
635
636 targetDistanceBetweenPoints = scale * perimeter / targetNumberOfBoundaryPoints;
637 }
638 else
639 {
640 targetDistanceBetweenPoints = 2.0 * sin (vtkMath::Pi() / targetNumberOfBoundaryPoints) * targetRadius;
641
642 double baseRadialNormal[3];
643 input->GetPoint(previousBoundaryIds->GetId(0),point);
644 for (k=0; k<3; k++)
645 {
646 baseRadialNormal[k] = point[k] - barycenter[k];
647 }
648 double outOfPlaneComponent = vtkMath::Dot(baseRadialNormal,flowExtensionNormal);
649 for (k=0; k<3; k++)
650 {
651 baseRadialNormal[k] -= outOfPlaneComponent * flowExtensionNormal[k];
652 }
653 vtkMath::Normalize(baseRadialNormal);
654 double angle = 360.0 / targetNumberOfBoundaryPoints;
655 vtkNew<vtkTransform> transform;
656 transform->RotateWXYZ(0.5*angle,flowExtensionNormal);
657 double testRadialNormal[3];
658 transform->TransformPoint(baseRadialNormal,testRadialNormal);
659 double cross[3], testCross[3], point1[3];
660 vtkMath::Cross(baseRadialNormal,testRadialNormal,testCross);
661 double dist = 0.0;
662 int testId = 1;
663 int numberOfPreviousBoundaryIds = previousBoundaryIds->GetNumberOfIds();
664 while (dist < 1E-8 && testId < numberOfPreviousBoundaryIds)
665 {
666 input->GetPoint(previousBoundaryIds->GetId(testId),point1);
667 dist = sqrt(vtkMath::Distance2BetweenPoints(point,point1));
668 testId++;
669 }
670 double testRadialVector[3];
671 for (k=0; k<3; k++)
672 {
673 testRadialVector[k] = point1[k] - barycenter[k];
674 }
675 vtkMath::Cross(baseRadialNormal,testRadialVector,cross);
676 if (vtkMath::Dot(cross,testCross) < 0.0)
677 {
678 angle *= -1.0;
679 transform->Identity();
680 transform->RotateWXYZ(0.5*angle,flowExtensionNormal);
681 }
682 double radialVector[3];
683 for (k=0; k<3; k++)
684 {
685 radialVector[k] = targetRadius * baseRadialNormal[k];
686 }
687 double targetPoint[3];
688 for (j=0; j<targetNumberOfBoundaryPoints; j++)
689 {
690 for (k=0; k<3; k++)
691 {
692 targetPoint[k] = barycenter[k] + radialVector[k];
693 }
694 targetBoundaryPoints->InsertNextPoint(targetPoint);
695 transform->TransformPoint(radialVector,radialVector);
696 for (k=0; k<3; k++)
697 {
698 targetPoint[k] = barycenter[k] + radialVector[k];
699 }
700 targetStaggeredBoundaryPoints->InsertNextPoint(targetPoint);
701 transform->TransformPoint(radialVector,radialVector);
702 }
703
704 if (useRampInterpolation)
705 {
706 // Pair each point of the circle with the point of the boundary that lies on the same ray
707 // from the barycenter, so that the transition is a purely radial morph. Matching by
708 // proximity instead would collapse whole arcs of the circle onto the same few boundary
709 // points on a strongly non-circular boundary, and would pair the two facing sides of a
710 // flat one with each other.
711 if (!SampleClosedPolygonByAngle(projectedBoundaryPoints,boundary->GetPoints(),barycenter,flowExtensionNormal,baseRadialNormal,angle,0.0,targetNumberOfBoundaryPoints,rimBoundaryPoints)
712 || !SampleClosedPolygonByAngle(projectedBoundaryPoints,boundary->GetPoints(),barycenter,flowExtensionNormal,baseRadialNormal,angle,0.5,targetNumberOfBoundaryPoints,rimStaggeredBoundaryPoints))
713 {
714 vtkWarningMacro(<<"Boundary "<<i<<" is not star-shaped about its barycenter, pairing it with the extension by arc length instead of by angle");
715 rimBoundaryPoints->Initialize();
716 rimStaggeredBoundaryPoints->Initialize();
717 vtkNew<vtkPoints> discardedPoints;
718 ResampleClosedPolygon(projectedBoundaryPoints,targetNumberOfBoundaryPoints,0.0,discardedPoints,boundary->GetPoints(),rimBoundaryPoints);
719 discardedPoints->Initialize();
720 ResampleClosedPolygon(projectedBoundaryPoints,targetNumberOfBoundaryPoints,0.5,discardedPoints,boundary->GetPoints(),rimStaggeredBoundaryPoints);
721 }
722 }
723 }
724
725 // Displacement taking each point of the target cross-section onto the point of the real
726 // boundary it is paired with. Fading it out over the transition length is what the ramp
727 // interpolation modes do, so the extension starts on the real boundary and reaches the target
728 // cross-section exactly at the end of the transition, whatever the shape of the boundary.
729 std::vector<double> displacement, staggeredDisplacement;
730
731 if (useRampInterpolation)
732 {
733 displacement.resize(3*targetNumberOfBoundaryPoints);
734 staggeredDisplacement.resize(3*targetNumberOfBoundaryPoints);
735 double targetPoint[3], rimPoint[3];
736 for (j=0; j<targetNumberOfBoundaryPoints; j++)
737 {
738 targetBoundaryPoints->GetPoint(j,targetPoint);
739 rimBoundaryPoints->GetPoint(j,rimPoint);
740 for (k=0; k<3; k++)
741 {
742 displacement[3*j+k] = rimPoint[k] - targetPoint[k];
743 }
744 targetStaggeredBoundaryPoints->GetPoint(j,targetPoint);
745 rimStaggeredBoundaryPoints->GetPoint(j,rimPoint);
746 for (k=0; k<3; k++)
747 {
748 staggeredDisplacement[3*j+k] = rimPoint[k] - targetPoint[k];
749 }
750 }
751 }
752
754 {
755 for (j=0; j<targetNumberOfBoundaryPoints; j++)
756 {
757 double firstBoundaryPoint[3], lastBoundaryPoint[3];
758 targetBoundaryPoints->GetPoint(j,firstBoundaryPoint);
759 double distance = 1E20;
760 double currentPoint[3];
761 for (int j2=0; j2<startNumberOfBoundaryPoints; j2++)
762 {
763 input->GetPoint(previousBoundaryIds->GetId(j2),currentPoint);
764 double currentDistance = vtkMath::Distance2BetweenPoints(currentPoint,firstBoundaryPoint);
765 if (currentDistance < distance)
766 {
767 distance = currentDistance;
768 for (k=0; k<3; k++)
769 {
770 point[k] = currentPoint[k];
771 }
772 }
773 }
774 sourceLandmarks->InsertNextPoint(firstBoundaryPoint);
775 targetLandmarks->InsertNextPoint(point);
776 for (k=0; k<3; k++)
777 {
778 lastBoundaryPoint[k] = firstBoundaryPoint[k] + extensionLength * this->TransitionRatio * flowExtensionNormal[k];
779 }
780 sourceLandmarks->InsertNextPoint(lastBoundaryPoint);
781 targetLandmarks->InsertNextPoint(lastBoundaryPoint);
782 }
783 thinPlateSplineTransform->SetSourceLandmarks(sourceLandmarks);
784 thinPlateSplineTransform->SetTargetLandmarks(targetLandmarks);
785 }
786
787 double transitionLength = extensionLength * this->TransitionRatio;
788 int numberOfLayers = extensionLength / targetDistanceBetweenPoints;
789 int numberOfTransitionLayers = transitionLength / targetDistanceBetweenPoints;
790 int l;
791 for (l=0; l<numberOfLayers; l++)
792 {
793 newBoundaryIds->Initialize();
794 for (j=0; j<targetNumberOfBoundaryPoints; j++)
795 {
796 if (l%2 != 0)
797 {
798 targetBoundaryPoints->GetPoint(j,extensionPoint);
799 }
800 else
801 {
802 targetStaggeredBoundaryPoints->GetPoint(j,extensionPoint);
803 }
804 for (k=0; k<3; k++)
805 {
806 extensionPoint[k] += (l+1) * targetDistanceBetweenPoints * flowExtensionNormal[k];
807 }
808 if (l<numberOfTransitionLayers)
809 {
811 {
812 thinPlateSplineTransform->TransformPoint(extensionPoint,extensionPoint);
813 }
814 else
815 {
816 // fade the displacement onto the real boundary from full at the boundary to none at
817 // the end of the transition; since the whole displacement is scaled by the same
818 // weight, the transition spans exactly transitionLength however irregular the
819 // boundary is
820 double parametricHeight = (l+1) * targetDistanceBetweenPoints / transitionLength;
821 parametricHeight = parametricHeight > 1.0 ? 1.0 : parametricHeight;
822 double weight = 0.0;
824 {
825 weight = 1.0 - parametricHeight;
826 }
827 else
828 {
829 // smoothstep, flat at both ends, so that the extension leaves the boundary
830 // tangentially and settles into the uniform tube without a crease
831 weight = 1.0 - parametricHeight * parametricHeight * (3.0 - 2.0 * parametricHeight);
832 }
833 const std::vector<double>& layerDisplacement = (l%2 != 0) ? displacement : staggeredDisplacement;
834 for (k=0; k<3; k++)
835 {
836 extensionPoint[k] += weight * layerDisplacement[3*j+k];
837 }
838 }
839 }
840 pointId = outputPoints->InsertNextPoint(extensionPoint);
841 newBoundaryIds->InsertNextId(pointId);
842 }
843
844 if (l==0)
845 {
846 vtkIdType pts[3];
847 int j2 = 0;
848 for (j=0; j<targetNumberOfBoundaryPoints; j++)
849 {
850 double point0[3], point1[3], point2[3], point3[3];
851 outputPoints->GetPoint(previousBoundaryIds->GetId(j2%startNumberOfBoundaryPoints),point0);
852 outputPoints->GetPoint(previousBoundaryIds->GetId((j2+1)%startNumberOfBoundaryPoints),point1);
853 outputPoints->GetPoint(newBoundaryIds->GetId(j),point2);
854 outputPoints->GetPoint(newBoundaryIds->GetId((j+1)%targetNumberOfBoundaryPoints),point3);
855
856 bool advance = false;
857 if ((j==0) || (j==targetNumberOfBoundaryPoints-1) || (vtkMath::Distance2BetweenPoints(point0,point3) > vtkMath::Distance2BetweenPoints(point1,point2)))
858 {
859 advance = true;
860 }
861 if (j2 == startNumberOfBoundaryPoints)
862 {
863 advance = false;
864 }
865 while(advance)
866 {
867 pts[0] = previousBoundaryIds->GetId(j2%startNumberOfBoundaryPoints);
868 pts[1] = newBoundaryIds->GetId(j);
869 pts[2] = previousBoundaryIds->GetId((j2+1)%startNumberOfBoundaryPoints);
870 outputPolys->InsertNextCell(3,pts);
871 j2 += 1;
872
873 outputPoints->GetPoint(previousBoundaryIds->GetId(j2%startNumberOfBoundaryPoints),point0);
874 outputPoints->GetPoint(previousBoundaryIds->GetId((j2+1)%startNumberOfBoundaryPoints),point1);
875 outputPoints->GetPoint(newBoundaryIds->GetId(j),point2);
876 outputPoints->GetPoint(newBoundaryIds->GetId((j+1)%targetNumberOfBoundaryPoints),point3);
877
878 if (j2 == startNumberOfBoundaryPoints || vtkMath::Distance2BetweenPoints(point0,point3) < vtkMath::Distance2BetweenPoints(point1,point2))
879 {
880 advance = false;
881 if (j2 < startNumberOfBoundaryPoints && j == targetNumberOfBoundaryPoints-1)
882 {
883 advance = true;
884 }
885 }
886 }
887 pts[0] = newBoundaryIds->GetId(j);
888 pts[1] = newBoundaryIds->GetId((j+1)%targetNumberOfBoundaryPoints);
889 pts[2] = previousBoundaryIds->GetId(j2%startNumberOfBoundaryPoints);
890 outputPolys->InsertNextCell(3,pts);
891 }
892 }
893 else
894 {
895 vtkIdType pts[3];
896 for (j=0; j<targetNumberOfBoundaryPoints; j++)
897 {
898 if (l%2 != 0)
899 {
900 pts[0] = newBoundaryIds->GetId(j);
901 pts[1] = newBoundaryIds->GetId((j-1+targetNumberOfBoundaryPoints)%targetNumberOfBoundaryPoints);
902 pts[2] = previousBoundaryIds->GetId((j-1+targetNumberOfBoundaryPoints)%targetNumberOfBoundaryPoints);
903 outputPolys->InsertNextCell(3,pts);
904
905 pts[0] = previousBoundaryIds->GetId(j);
906 pts[1] = newBoundaryIds->GetId(j);
907 pts[2] = previousBoundaryIds->GetId((j-1+targetNumberOfBoundaryPoints)%targetNumberOfBoundaryPoints);
908 outputPolys->InsertNextCell(3,pts);
909 }
910 else
911 {
912 pts[0] = newBoundaryIds->GetId(j);
913 pts[1] = newBoundaryIds->GetId((j-1+targetNumberOfBoundaryPoints)%targetNumberOfBoundaryPoints);
914 pts[2] = previousBoundaryIds->GetId(j);
915 outputPolys->InsertNextCell(3,pts);
916
917 pts[0] = previousBoundaryIds->GetId(j);
918 pts[1] = newBoundaryIds->GetId((j-1+targetNumberOfBoundaryPoints)%targetNumberOfBoundaryPoints);
919 pts[2] = previousBoundaryIds->GetId((j-1+targetNumberOfBoundaryPoints)%targetNumberOfBoundaryPoints);
920 outputPolys->InsertNextCell(3,pts);
921 }
922 }
923 }
924
925 previousBoundaryIds->DeepCopy(newBoundaryIds);
926 }
927
928 // previousBoundaryIds is now the last ring laid down, which is the new open boundary at the
929 // tip of this extension - or, when the extension was too short for a single layer, still the
930 // ring this started from, which is then the right answer too.
931 if (useBoundaryLabels)
932 {
933 for (j=0; j<boundaryIds->GetNumberOfIds(); j++)
934 {
935 originalRingByBoundary[i].push_back(boundaryIds->GetId(j));
936 }
937 for (j=0; j<previousBoundaryIds->GetNumberOfIds(); j++)
938 {
939 tipRingByBoundary[i].push_back(previousBoundaryIds->GetId(j));
940 }
941 }
942
943 }
944
945 output->SetPoints(outputPoints);
946 output->SetPolys(outputPolys);
947
948 if (useBoundaryLabels)
949 {
950 // Carry the labels onto the output. The points of the input keep the ids they had - they are
951 // deep copied first and the extensions only append - so the arrays copy across as they
952 // stand, and only the boundaries that were extended have to be moved: the ring an extension
953 // grew from is inside the surface now and is no longer anything's boundary, while the ring
954 // at the tip is, and is the same vessel end.
955 const vtkIdType invalidLabel = vtkvmtkBoundaryLabels::GetInvalidBoundaryLabel();
956 vtkIdType numberOfInputPoints = input->GetNumberOfPoints();
957 vtkIdType numberOfOutputPoints = outputPoints->GetNumberOfPoints();
958
959 vtkDataArray* inputLabels = input->GetPointData()->GetArray(this->BoundaryLabelsArrayName);
960 vtkDataArray* inputOrder = input->GetPointData()->GetArray(this->BoundaryPointOrderArrayName);
961
962 vtkNew<vtkIntArray> outputLabels;
963 outputLabels->SetName(this->BoundaryLabelsArrayName);
964 outputLabels->SetNumberOfTuples(numberOfOutputPoints);
965 outputLabels->FillComponent(0,static_cast<double>(invalidLabel));
966
967 vtkNew<vtkIntArray> outputOrder;
968 outputOrder->SetName(this->BoundaryPointOrderArrayName);
969 outputOrder->SetNumberOfTuples(numberOfOutputPoints);
970 outputOrder->FillComponent(0,static_cast<double>(invalidLabel));
971
972 for (vtkIdType pointId=0; pointId<numberOfInputPoints; pointId++)
973 {
974 outputLabels->SetValue(pointId,static_cast<int>(vtkMath::Round(inputLabels->GetComponent(pointId,0))));
975 outputOrder->SetValue(pointId,static_cast<int>(vtkMath::Round(inputOrder->GetComponent(pointId,0))));
976 }
977
978 for (size_t boundaryIndex=0; boundaryIndex<tipRingByBoundary.size(); boundaryIndex++)
979 {
980 if (tipRingByBoundary[boundaryIndex].empty())
981 {
982 continue;
983 }
984 vtkIdType boundaryLabel = boundaryLabels->GetId(static_cast<vtkIdType>(boundaryIndex));
985 for (size_t j2=0; j2<originalRingByBoundary[boundaryIndex].size(); j2++)
986 {
987 outputLabels->SetValue(originalRingByBoundary[boundaryIndex][j2],static_cast<int>(invalidLabel));
988 outputOrder->SetValue(originalRingByBoundary[boundaryIndex][j2],static_cast<int>(invalidLabel));
989 }
990 for (size_t j2=0; j2<tipRingByBoundary[boundaryIndex].size(); j2++)
991 {
992 outputLabels->SetValue(tipRingByBoundary[boundaryIndex][j2],static_cast<int>(boundaryLabel));
993 outputOrder->SetValue(tipRingByBoundary[boundaryIndex][j2],static_cast<int>(j2));
994 }
995 }
996
997 output->GetPointData()->AddArray(outputLabels);
998 output->GetPointData()->AddArray(outputOrder);
999 }
1000
1001 return 1;
1002}
1003
1004void vtkvmtkPolyDataFlowExtensionsFilter::PrintSelf(std::ostream& os, vtkIndent indent)
1005{
1006 this->Superclass::PrintSelf(os,indent);
1007}
static bool GetBoundaries(vtkPolyData *surface, const char *boundaryLabelsArrayName, const char *boundaryPointOrderArrayName, vtkPolyData *boundaries, vtkIdList *boundaryLabels)
static vtkIdType GetInvalidBoundaryLabel()
static void ComputeBoundaryBarycenter(vtkPoints *points, double barycenter[3])
static void OrientBoundaryNormalOutwards(vtkPolyData *surface, vtkPolyData *boundaries, vtkIdType boundaryCellId, double normal[3], double outwardNormal[3])
static void ComputeBoundaryNormal(vtkPoints *points, double barycenter[3], double normal[3])
static double ComputeBoundaryMeanRadius(vtkPoints *points, double barycenter[3])
Extend open boundaries of a surface with straight (or centerline-following) tubes,...
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
virtual void SetBoundaryPointOrderArrayName(const char *)
void PrintSelf(std::ostream &os, vtkIndent indent) override
virtual void SetBoundaryLabelsArrayName(const char *)
vtkStandardNewMacro(vtkvmtkPolyDataFlowExtensionsFilter)