VMTK
Loading...
Searching...
No Matches
vtkvmtkPolyDataCenterlineBranchSplitting.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4
5 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
6 See LICENSE file for details.
7
8 Portions of this code are covered under the VTK copyright.
9 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
10 for details.
11
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
15
16 This file is derived from vtkvmtkPolyDataCenterlineSections of SimVascular
17 (https://github.com/SimVascular/SimVascular), provided under the following
18 license:
19
20 Copyright (c) Stanford University, The Regents of the University of
21 California, and others.
22
23 All Rights Reserved.
24
25 Permission is hereby granted, free of charge, to any person obtaining a copy
26 of this software and associated documentation files (the "Software"), to deal
27 in the Software without restriction, including without limitation the rights
28 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29 copies of the Software, and to permit persons to whom the Software is
30 furnished to do so, subject to the following conditions:
31
32 The above copyright notice and this permission notice shall be included in
33 all copies or substantial portions of the Software.
34
35 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41 SOFTWARE.
42
43=========================================================================*/
44
47
48#include "vtkAppendFilter.h"
49#include "vtkCellArray.h"
50#include "vtkCellData.h"
51#include "vtkCleanPolyData.h"
52#include "vtkConnectivityFilter.h"
53#include "vtkDoubleArray.h"
54#include "vtkIdList.h"
55#include "vtkIdTypeArray.h"
56#include "vtkInformation.h"
57#include "vtkInformationVector.h"
58#include "vtkIntArray.h"
59#include "vtkMath.h"
60#include "vtkNew.h"
61#include "vtkObjectFactory.h"
62#include "vtkPointData.h"
63#include "vtkPointDataToCellData.h"
64#include "vtkPointLocator.h"
65#include "vtkPolyData.h"
66#include "vtkPolyDataNormals.h"
67#include "vtkThreshold.h"
68#include "vtkUnstructuredGrid.h"
69
70#include <algorithm>
71#include <set>
72#include <vector>
73
75
76namespace
77{
78// Arrays used only while the filter runs
79const char* BranchIdTmpArrayName = "BranchIdTmp";
80const char* BifurcationIdTmpArrayName = "BifurcationIdTmp";
81const char* RemoveArrayName = "CenterlineSectionRemove";
82const char* RegionIdArrayName = "RegionId";
83
84// Values of the bifurcation classification arrays
85const int Undefined = -1;
86const int Branch = 0;
87const int Bifurcation = 1;
88const int BifurcationDownstream = 2;
89
90// The cells of each point of lines. Adjacency is computed here rather than
91// with vtkPolyData links, which hold a reference back to the polydata.
92std::vector<std::vector<vtkIdType>> GetPointCells(vtkPolyData* lines)
93{
94 std::vector<std::vector<vtkIdType>> pointCells(lines->GetNumberOfPoints());
95 vtkNew<vtkIdList> cellPointIds;
96 for (vtkIdType cellId=0; cellId<lines->GetNumberOfCells(); cellId++)
97 {
98 lines->GetCellPoints(cellId,cellPointIds);
99 for (vtkIdType i=0; i<cellPointIds->GetNumberOfIds(); i++)
100 {
101 pointCells[cellPointIds->GetId(i)].push_back(cellId);
102 }
103 }
104 return pointCells;
105}
106
107bool IsOnePiece(vtkPolyData* polyData)
108{
109 vtkNew<vtkConnectivityFilter> connectivity;
110 connectivity->SetInputData(polyData);
111 connectivity->SetExtractionModeToAllRegions();
112 connectivity->Update();
113 return connectivity->GetNumberOfExtractedRegions() == 1;
114}
115
116void BuildPointLocator(vtkPolyData* polyData, vtkPointLocator* locator)
117{
118 vtkNew<vtkPolyData> points;
119 points->SetPoints(polyData->GetPoints());
120 locator->SetDataSet(points);
121 locator->BuildLocator();
122}
123
124// The lines of an unstructured grid, such as the output of vtkThreshold, as
125// polydata with the same points, point order, cell order and attributes
126void LinesToPolyData(vtkUnstructuredGrid* grid, vtkPolyData* polyData)
127{
128 vtkNew<vtkPolyData> lines;
129 vtkNew<vtkPoints> points;
130 if (grid->GetPoints())
131 {
132 points->ShallowCopy(grid->GetPoints());
133 }
134 lines->SetPoints(points);
135 vtkNew<vtkCellArray> cells;
136 vtkNew<vtkIdList> cellPointIds;
137 for (vtkIdType cellId=0; cellId<grid->GetNumberOfCells(); cellId++)
138 {
139 grid->GetCellPoints(cellId,cellPointIds);
140 cells->InsertNextCell(cellPointIds);
141 }
142 lines->SetLines(cells);
143 lines->GetPointData()->ShallowCopy(grid->GetPointData());
144 lines->GetCellData()->ShallowCopy(grid->GetCellData());
145 polyData->DeepCopy(lines);
146}
147
148// Start of each step in the progress of the whole filter, from approximate
149// running times
150const double PROGRESS_NORMALS = 0.0;
151const double PROGRESS_CLEAN = 0.01;
152const double PROGRESS_LABEL_PRELIMINARY = 0.02;
153const double PROGRESS_SECTIONS = 0.12;
154const double PROGRESS_GROUP = 0.88;
155const double PROGRESS_LABEL = 0.89;
156
157// While it exists, maps progress from 0 to 1 onto the part of the progress
158// range of algorithm from start to end, and sets the progress text
159class ProgressStep
160{
161 public:
162 ProgressStep(vtkAlgorithm* algorithm, const char* text, double start, double end)
163 : Algorithm(algorithm), Shift(algorithm->GetProgressShift()), Scale(algorithm->GetProgressScale())
164 {
165 algorithm->SetProgressText(text);
166 algorithm->SetProgressShiftScale(this->Shift + this->Scale * start, this->Scale * (end - start));
167 algorithm->UpdateProgress(0.0);
168 }
169 ~ProgressStep()
170 {
171 this->Algorithm->SetProgressShiftScale(this->Shift,this->Scale);
172 }
173 ProgressStep(const ProgressStep&) = delete;
174 ProgressStep& operator=(const ProgressStep&) = delete;
175
176 private:
177 vtkAlgorithm* Algorithm;
178 double Shift;
179 double Scale;
180};
181
182// vtkConnectivityFilter stores region ids in an implicit array since VTK 9.4,
183// which vtkAppendFilter does not merge with the explicit arrays of other
184// inputs. Replace it with an explicit array of the given name.
185void RenameRegionIds(vtkDataSetAttributes* attributes, const char* name)
186{
187 vtkNew<vtkIdTypeArray> regionIds;
188 regionIds->DeepCopy(attributes->GetArray(RegionIdArrayName));
189 regionIds->SetName(name);
190 attributes->RemoveArray(RegionIdArrayName);
191 attributes->AddArray(regionIds);
192}
193}
194
196{
197 public:
198 // Number of cells of each centerline point while sections are computed
199 std::vector<vtkIdType> NumberOfPointCells;
200 vtkIntArray* SectionBifurcation = nullptr;
201 vtkIntArray* SectionGlobalNodeId = nullptr;
202 vtkIntArray* CenterlineBifurcation = nullptr;
203 vtkDataArray* CenterlineNormal = nullptr;
204};
205
207{
208 this->SetCenterlineSectionAreaArrayName("CenterlineSectionArea");
209 this->SetCenterlineSectionMinSizeArrayName("CenterlineSectionMinSize");
210 this->SetCenterlineSectionMaxSizeArrayName("CenterlineSectionMaxSize");
211 this->SetCenterlineSectionShapeArrayName("CenterlineSectionShape");
212 this->SetCenterlineSectionClosedArrayName("CenterlineSectionClosed");
213
214 this->Surface = vtkPolyData::New();
215
216 this->RadiusArrayName = nullptr;
217 this->BranchIdArrayName = nullptr;
218 this->BifurcationIdArrayName = nullptr;
219 this->PathArrayName = nullptr;
220 this->CenterlineIdArrayName = nullptr;
221 this->GlobalNodeIdArrayName = nullptr;
222 this->CenterlineSectionNormalArrayName = nullptr;
224
225 this->SetRadiusArrayName("MaximumInscribedSphereRadius");
226 this->SetBranchIdArrayName("BranchId");
227 this->SetBifurcationIdArrayName("BifurcationId");
228 this->SetPathArrayName("Path");
229 this->SetCenterlineIdArrayName("CenterlineId");
230 this->SetGlobalNodeIdArrayName("GlobalNodeId");
231 this->SetCenterlineSectionNormalArrayName("CenterlineSectionNormal");
232 this->SetCenterlineSectionBifurcationArrayName("CenterlineSectionBifurcation");
233
234 this->NumberOfCenterlines = 0;
235
236 this->Internals = new vtkInternals;
237}
238
240{
241 this->Surface->Delete();
242 this->Surface = nullptr;
243
244 this->SetRadiusArrayName(nullptr);
245 this->SetBranchIdArrayName(nullptr);
246 this->SetBifurcationIdArrayName(nullptr);
247 this->SetPathArrayName(nullptr);
248 this->SetCenterlineIdArrayName(nullptr);
249 this->SetGlobalNodeIdArrayName(nullptr);
252
253 delete this->Internals;
254}
255
257 vtkInformation *vtkNotUsed(request),
258 vtkInformationVector **inputVector,
259 vtkInformationVector *outputVector)
260{
261 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
262 vtkInformation *outInfo = outputVector->GetInformationObject(0);
263
264 vtkPolyData *input = vtkPolyData::SafeDownCast(
265 inInfo->Get(vtkDataObject::DATA_OBJECT()));
266 vtkPolyData *output = vtkPolyData::SafeDownCast(
267 outInfo->Get(vtkDataObject::DATA_OBJECT()));
268
269 if (!this->Centerlines)
270 {
271 vtkErrorMacro(<<"Centerlines not set");
272 return 0;
273 }
274
275 const char* arrayNames[] = {this->CenterlineSectionAreaArrayName, this->CenterlineSectionMinSizeArrayName,
280 for (const char* arrayName : arrayNames)
281 {
282 if (!arrayName)
283 {
284 vtkErrorMacro(<<"An array name is not specified");
285 return 0;
286 }
287 }
288
289 if (!this->Centerlines->GetPointData()->GetArray(this->RadiusArrayName))
290 {
291 vtkErrorMacro(<<"Centerlines have no point data array named " << this->RadiusArrayName);
292 return 0;
293 }
294
295 if (input->GetNumberOfPolys() == 0)
296 {
297 vtkErrorMacro(<<"Input surface has no polygons");
298 return 0;
299 }
300
301 {
302 ProgressStep step(this,"Computing surface normals",PROGRESS_NORMALS,PROGRESS_CLEAN);
303 // vtkPolyDataNormals keeps normals the surface already has, which may point
304 // inward, instead of computing oriented ones
305 vtkNew<vtkPolyData> surface;
306 surface->ShallowCopy(input);
307 surface->GetPointData()->SetActiveNormals(nullptr);
308
309 vtkNew<vtkPolyDataNormals> surfaceNormals;
310 surfaceNormals->SetInputData(surface);
311 surfaceNormals->SplittingOff();
312 surfaceNormals->AutoOrientNormalsOn();
313 surfaceNormals->ComputePointNormalsOn();
314 surfaceNormals->ConsistencyOn();
315 surfaceNormals->Update();
316 this->Surface->DeepCopy(surfaceNormals->GetOutput());
317 }
318 if (this->AbortRequested())
319 {
320 return 1;
321 }
322
323 {
324 ProgressStep step(this,"Merging and smoothing centerlines",PROGRESS_CLEAN,PROGRESS_LABEL_PRELIMINARY);
325 if (!this->GenerateCleanCenterlines())
326 {
327 return 0;
328 }
329
330 this->ComputeTangents();
331
332 if (!this->RefineCapPoints())
333 {
334 return 0;
335 }
336 }
337 if (this->AbortRequested())
338 {
339 return 1;
340 }
341
342 // Whether each centerline point is in a bifurcation region, as found from
343 // its section. Points that get no section count as bifurcation.
344 vtkNew<vtkIntArray> centerlineBifurcation;
345 centerlineBifurcation->SetName(this->CenterlineSectionBifurcationArrayName);
346 centerlineBifurcation->SetNumberOfValues(this->Centerlines->GetNumberOfPoints());
347 centerlineBifurcation->Fill(Bifurcation);
348 this->Centerlines->GetPointData()->AddArray(centerlineBifurcation);
349
350 vtkNew<vtkIntArray> sectionBifurcation;
351 sectionBifurcation->SetName(this->CenterlineSectionBifurcationArrayName);
352 output->GetCellData()->AddArray(sectionBifurcation);
353 vtkNew<vtkIntArray> sectionGlobalNodeId;
354 sectionGlobalNodeId->SetName(this->GlobalNodeIdArrayName);
355 output->GetCellData()->AddArray(sectionGlobalNodeId);
356
357 {
358 ProgressStep step(this,"Labeling surface",PROGRESS_LABEL_PRELIMINARY,PROGRESS_SECTIONS);
359 // Preliminary labels of the surface, which tell whether a section runs
360 // across a boundary between branches
361 if (!this->LabelSurface(BranchIdTmpArrayName,BifurcationIdTmpArrayName))
362 {
363 return 0;
364 }
365 }
366 if (this->AbortRequested())
367 {
368 return 1;
369 }
370
371 std::vector<std::vector<vtkIdType>> pointCells = GetPointCells(this->Centerlines);
372 this->Internals->NumberOfPointCells.resize(pointCells.size());
373 for (size_t i=0; i<pointCells.size(); i++)
374 {
375 this->Internals->NumberOfPointCells[i] = static_cast<vtkIdType>(pointCells[i].size());
376 }
377 this->Internals->SectionBifurcation = sectionBifurcation;
378 this->Internals->SectionGlobalNodeId = sectionGlobalNodeId;
379 this->Internals->CenterlineBifurcation = centerlineBifurcation;
380 this->Internals->CenterlineNormal = this->Centerlines->GetPointData()->GetArray(this->CenterlineSectionNormalArrayName);
381
382 {
383 ProgressStep step(this,"Computing centerline sections",PROGRESS_SECTIONS,PROGRESS_GROUP);
384 this->ComputeCenterlineSections(this->Surface,output);
385 }
386
387 this->Internals->NumberOfPointCells.clear();
388 this->Internals->SectionBifurcation = nullptr;
389 this->Internals->SectionGlobalNodeId = nullptr;
390 this->Internals->CenterlineBifurcation = nullptr;
391 this->Internals->CenterlineNormal = nullptr;
392
393 if (this->AbortRequested())
394 {
395 return 1;
396 }
397
398 {
399 ProgressStep step(this,"Grouping branches and bifurcations",PROGRESS_GROUP,PROGRESS_LABEL);
400 if (!this->CleanBifurcations())
401 {
402 return 0;
403 }
404
405 if (!this->GroupCenterlines())
406 {
407 return 0;
408 }
409 }
410 if (this->AbortRequested())
411 {
412 return 1;
413 }
414
415 {
416 ProgressStep step(this,"Labeling surface",PROGRESS_LABEL,1.0);
417 if (!this->LabelSurface(this->BranchIdArrayName,this->BifurcationIdArrayName) ||
419 {
420 return 0;
421 }
422 }
423
424 this->Centerlines->GetPointData()->RemoveArray(BranchIdTmpArrayName);
425 this->Centerlines->GetPointData()->RemoveArray(BifurcationIdTmpArrayName);
426 this->Surface->GetPointData()->RemoveArray(BranchIdTmpArrayName);
427
428 this->UpdateProgress(1.0);
429 return 1;
430}
431
433{
434 vtkNew<vtkCleanPolyData> cleaner;
435 cleaner->SetInputData(this->Centerlines);
436 cleaner->PointMergingOn();
437 cleaner->Update();
438 vtkPolyData* centerlines = cleaner->GetOutput();
439
440 this->NumberOfCenterlines = centerlines->GetNumberOfCells();
441 if (this->NumberOfCenterlines == 0)
442 {
443 vtkErrorMacro(<<"Centerlines have no cells");
444 return false;
445 }
446
447 if (!IsOnePiece(centerlines))
448 {
449 vtkErrorMacro(<<"Centerlines consist of more than one piece");
450 return false;
451 }
452
453 const vtkIdType numberOfPoints = centerlines->GetNumberOfPoints();
454 vtkDataArray* inputRadius = centerlines->GetPointData()->GetArray(this->RadiusArrayName);
455
456 vtkNew<vtkPoints> points;
457 vtkNew<vtkCellArray> lines;
458
459 vtkNew<vtkDoubleArray> radius;
460 radius->SetName(this->RadiusArrayName);
461 radius->SetNumberOfValues(numberOfPoints);
462 radius->Fill(0.0);
463
464 vtkNew<vtkIntArray> globalNodeId;
465 globalNodeId->SetName(this->GlobalNodeIdArrayName);
466 globalNodeId->SetNumberOfValues(numberOfPoints);
467 globalNodeId->Fill(0);
468
469 vtkNew<vtkIntArray> centerlineId;
470 centerlineId->SetName(this->CenterlineIdArrayName);
471 centerlineId->SetNumberOfComponents(this->NumberOfCenterlines);
472 centerlineId->SetNumberOfTuples(numberOfPoints);
473 centerlineId->Fill(0);
474
475 // Number the points in the order the centerlines reach them, starting from
476 // the inlet, and connect each new point to the previous one on its
477 // centerline. Points shared with an earlier centerline are not repeated.
478 std::vector<vtkIdType> newPointIds(numberOfPoints,-1);
479 newPointIds[0] = points->InsertNextPoint(centerlines->GetPoint(0));
480 radius->SetValue(0,inputRadius->GetTuple1(0));
481
482 vtkNew<vtkIdList> cellPointIds;
483 for (vtkIdType c=0; c<this->NumberOfCenterlines; c++)
484 {
485 centerlines->GetCellPoints(c,cellPointIds);
486 if (cellPointIds->GetNumberOfIds() == 0 || newPointIds[cellPointIds->GetId(0)] < 0)
487 {
488 vtkErrorMacro(<<"Centerlines must all start from the same inlet point");
489 return false;
490 }
491 for (vtkIdType p=0; p<cellPointIds->GetNumberOfIds(); p++)
492 {
493 vtkIdType id = cellPointIds->GetId(p);
494 if (newPointIds[id] < 0)
495 {
496 vtkIdType newId = points->InsertNextPoint(centerlines->GetPoint(id));
497 newPointIds[id] = newId;
498 vtkIdType line[2] = {newPointIds[cellPointIds->GetId(p-1)], newId};
499 lines->InsertNextCell(2,line);
500 radius->SetValue(newId,inputRadius->GetTuple1(id));
501 globalNodeId->SetValue(newId,newId);
502 }
503 centerlineId->SetComponent(newPointIds[id],c,1);
504 }
505 }
506
507 vtkNew<vtkPolyData> polyData;
508 polyData->SetPoints(points);
509 polyData->SetLines(lines);
510
511 if (polyData->GetNumberOfPoints() != numberOfPoints)
512 {
513 vtkErrorMacro(<<"Centerlines have points that belong to no centerline");
514 return false;
515 }
516
517 if (polyData->GetNumberOfPoints() != polyData->GetNumberOfCells() + 1)
518 {
519 vtkErrorMacro(<<"Centerlines do not form a tree");
520 return false;
521 }
522
523 // Preliminary branches: a new branch starts after each outlet and each
524 // point where the centerlines split
525 vtkNew<vtkIntArray> bifurcationIds;
526 bifurcationIds->SetName(BifurcationIdTmpArrayName);
527 bifurcationIds->SetNumberOfValues(numberOfPoints);
528 bifurcationIds->Fill(Undefined);
529 vtkNew<vtkIntArray> branchIds;
530 branchIds->SetName(BranchIdTmpArrayName);
531 branchIds->SetNumberOfValues(numberOfPoints);
532 branchIds->Fill(Undefined);
533
534 polyData->GetPointData()->AddArray(bifurcationIds);
535 polyData->GetPointData()->AddArray(branchIds);
536 polyData->GetPointData()->AddArray(radius);
537 polyData->GetPointData()->AddArray(globalNodeId);
538 polyData->GetPointData()->AddArray(centerlineId);
539
540 std::vector<std::vector<vtkIdType>> pointCells = GetPointCells(polyData);
541
542 int numberOfBranches = 0;
543 for (vtkIdType p=0; p<numberOfPoints; p++)
544 {
545 branchIds->SetValue(p,numberOfBranches);
546 if (pointCells[p].size() == 1 && p != 0)
547 {
548 numberOfBranches++;
549 }
550 else if (pointCells[p].size() > 2)
551 {
552 bifurcationIds->SetValue(p,Bifurcation);
553 numberOfBranches++;
554 }
555 }
556
557 vtkNew<vtkPointLocator> locator;
558 BuildPointLocator(polyData,locator);
559 vtkNew<vtkIdList> closePoints;
560 double point[3];
561
562 // Points of the branches leaving a bifurcation that are within the
563 // inscribed sphere of the bifurcation point are part of the bifurcation
564 for (vtkIdType i=0; i<numberOfPoints; i++)
565 {
566 if (pointCells[i].size() <= 2)
567 {
568 continue;
569 }
570 int upstreamBranchId = branchIds->GetValue(i);
571 std::set<int> downstreamBranchIds;
572 for (vtkIdType cellId : pointCells[i])
573 {
574 polyData->GetCellPoints(cellId,cellPointIds);
575 for (vtkIdType k=0; k<cellPointIds->GetNumberOfIds(); k++)
576 {
577 int id = branchIds->GetValue(cellPointIds->GetId(k));
578 if (id != upstreamBranchId)
579 {
580 downstreamBranchIds.insert(id);
581 }
582 }
583 }
584 polyData->GetPoint(i,point);
585 locator->FindPointsWithinRadius(radius->GetValue(i),point,closePoints);
586 for (vtkIdType k=0; k<closePoints->GetNumberOfIds(); k++)
587 {
588 vtkIdType closePointId = closePoints->GetId(k);
589 if (downstreamBranchIds.count(branchIds->GetValue(closePointId)))
590 {
591 bifurcationIds->SetValue(closePointId,BifurcationDownstream);
592 }
593 }
594 }
595
596 // Points within two inscribed sphere radii of a cap are smoothed more
597 // strongly, perpendicular to the centerline, to remove the wiggles often
598 // seen there
599 std::vector<bool> nearCap(numberOfPoints,false);
600 for (vtkIdType i=0; i<numberOfPoints; i++)
601 {
602 if (pointCells[i].size() != 1)
603 {
604 continue;
605 }
606 polyData->GetPoint(i,point);
607 locator->FindPointsWithinRadius(2.0 * radius->GetValue(i),point,closePoints);
608 for (vtkIdType k=0; k<closePoints->GetNumberOfIds(); k++)
609 {
610 vtkIdType closePointId = closePoints->GetId(k);
611 if (branchIds->GetValue(closePointId) == branchIds->GetValue(i))
612 {
613 nearCap[closePointId] = true;
614 }
615 }
616 }
617
618 // Moving average along each branch. Points of a branch are numbered
619 // consecutively, and only points whose two neighbors are in the same branch
620 // are moved.
621 const int numberOfIterations = 200;
622 const double capRelaxation = 1.0;
623 const double relaxation = 0.01;
624 double point0[3], point1[3], point2[3], displacement[3], tangent[3];
625 for (int iteration=0; iteration<numberOfIterations; iteration++)
626 {
627 for (vtkIdType k=1; k<numberOfPoints-1; k++)
628 {
629 int branchId = branchIds->GetValue(k);
630 if (branchIds->GetValue(k-1) != branchId || branchIds->GetValue(k+1) != branchId)
631 {
632 continue;
633 }
634 points->GetPoint(k-1,point0);
635 points->GetPoint(k,point1);
636 points->GetPoint(k+1,point2);
637 for (int l=0; l<3; l++)
638 {
639 displacement[l] = 0.5 * (point0[l] + point2[l]) - point1[l];
640 }
641 if (nearCap[k])
642 {
643 double distance01 = sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
644 double distance12 = sqrt(vtkMath::Distance2BetweenPoints(point1,point2));
645 for (int l=0; l<3; l++)
646 {
647 tangent[l] = (point1[l] - point0[l]) / distance01 + (point2[l] - point1[l]) / distance12;
648 }
649 vtkMath::Normalize(tangent);
650 double tangentialDisplacement = vtkMath::Dot(displacement,tangent);
651 for (int l=0; l<3; l++)
652 {
653 point1[l] += capRelaxation * (displacement[l] - tangentialDisplacement * tangent[l]);
654 }
655 }
656 else
657 {
658 for (int l=0; l<3; l++)
659 {
660 point1[l] += relaxation * displacement[l];
661 }
662 }
663 points->SetPoint(k,point1);
664 }
665 }
666
667 this->Centerlines->DeepCopy(polyData);
668 return true;
669}
670
672{
673 const vtkIdType numberOfPoints = this->Centerlines->GetNumberOfPoints();
674 vtkNew<vtkDoubleArray> tangents;
675 tangents->SetName(this->CenterlineSectionNormalArrayName);
676 tangents->SetNumberOfComponents(3);
677 tangents->SetNumberOfTuples(numberOfPoints);
678
679 vtkDataArray* surfaceNormals = this->Surface->GetPointData()->GetNormals();
680 vtkNew<vtkPointLocator> locator;
681 BuildPointLocator(this->Surface,locator);
682
683 std::vector<std::vector<vtkIdType>> pointCells = GetPointCells(this->Centerlines);
684 vtkNew<vtkIdList> cellPointIds;
685 double point[3], point0[3], point1[3], direction[3];
686 for (vtkIdType p=0; p<numberOfPoints; p++)
687 {
688 double tangent[3] = {0.0, 0.0, 0.0};
689 if (pointCells[p].size() == 1)
690 {
691 // A cap: use the normal of the surface, which points out of it, and
692 // flip it at the inlet so that tangents follow the tree
693 this->Centerlines->GetPoint(p,point);
694 surfaceNormals->GetTuple(locator->FindClosestPoint(point),tangent);
695 if (p == 0)
696 {
697 vtkMath::MultiplyScalar(tangent,-1.0);
698 }
699 }
700 else
701 {
702 for (vtkIdType cellId : pointCells[p])
703 {
704 this->Centerlines->GetCellPoints(cellId,cellPointIds);
705 this->Centerlines->GetPoint(cellPointIds->GetId(0),point0);
706 this->Centerlines->GetPoint(cellPointIds->GetId(1),point1);
707 vtkMath::Subtract(point1,point0,direction);
708 vtkMath::Normalize(direction);
709 vtkMath::Add(tangent,direction,tangent);
710 }
711 vtkMath::Normalize(tangent);
712 }
713 tangents->SetTuple(p,tangent);
714 }
715
716 this->Centerlines->GetPointData()->AddArray(tangents);
717}
718
720{
721 vtkPolyData* centerlines = this->Centerlines;
722 const vtkIdType numberOfPoints = centerlines->GetNumberOfPoints();
723 std::vector<std::vector<vtkIdType>> pointCells = GetPointCells(centerlines);
724
725 vtkDataArray* radius = centerlines->GetPointData()->GetArray(this->RadiusArrayName);
726 vtkDataArray* tangents = centerlines->GetPointData()->GetArray(this->CenterlineSectionNormalArrayName);
727 vtkDataArray* bifurcationIds = centerlines->GetPointData()->GetArray(BifurcationIdTmpArrayName);
728 vtkDataArray* branchIds = centerlines->GetPointData()->GetArray(BranchIdTmpArrayName);
729 vtkDataArray* centerlineId = centerlines->GetPointData()->GetArray(this->CenterlineIdArrayName);
730
731 // New point ids: a cap is preceded by the new point at the outlets and
732 // followed by it at the inlet, so that ids keep increasing along the tree
733 std::vector<vtkIdType> newPointIds(numberOfPoints,-1);
734 std::vector<vtkIdType> newCapNeighborIds(numberOfPoints,-1);
735 vtkIdType numberOfNewPoints = 0;
736 for (vtkIdType i=0; i<numberOfPoints; i++)
737 {
738 if (pointCells[i].size() != 1)
739 {
740 newPointIds[i] = numberOfNewPoints++;
741 }
742 else if (i == 0)
743 {
744 newPointIds[i] = numberOfNewPoints++;
745 newCapNeighborIds[i] = numberOfNewPoints++;
746 }
747 else
748 {
749 newCapNeighborIds[i] = numberOfNewPoints++;
750 newPointIds[i] = numberOfNewPoints++;
751 }
752 }
753
754 vtkNew<vtkPoints> points;
755 points->SetNumberOfPoints(numberOfNewPoints);
756
757 vtkNew<vtkDoubleArray> newRadius;
758 newRadius->SetName(this->RadiusArrayName);
759 newRadius->SetNumberOfValues(numberOfNewPoints);
760 vtkNew<vtkDoubleArray> newTangents;
761 newTangents->SetName(this->CenterlineSectionNormalArrayName);
762 newTangents->SetNumberOfComponents(3);
763 newTangents->SetNumberOfTuples(numberOfNewPoints);
764 vtkNew<vtkIntArray> newBifurcationIds;
765 newBifurcationIds->SetName(BifurcationIdTmpArrayName);
766 newBifurcationIds->SetNumberOfValues(numberOfNewPoints);
767 vtkNew<vtkIntArray> newBranchIds;
768 newBranchIds->SetName(BranchIdTmpArrayName);
769 newBranchIds->SetNumberOfValues(numberOfNewPoints);
770 vtkNew<vtkIntArray> newGlobalNodeId;
771 newGlobalNodeId->SetName(this->GlobalNodeIdArrayName);
772 newGlobalNodeId->SetNumberOfValues(numberOfNewPoints);
773 vtkNew<vtkIntArray> newCenterlineId;
774 newCenterlineId->SetName(this->CenterlineIdArrayName);
775 newCenterlineId->SetNumberOfComponents(this->NumberOfCenterlines);
776 newCenterlineId->SetNumberOfTuples(numberOfNewPoints);
777
778 vtkNew<vtkIdList> cellPointIds;
779 double point[3], neighborPoint[3], tangent[3], neighborTangent[3];
780 for (vtkIdType i=0; i<numberOfPoints; i++)
781 {
782 centerlines->GetPoint(i,point);
783 tangents->GetTuple(i,tangent);
784
785 std::vector<vtkIdType> ids = {newPointIds[i]};
786 if (newCapNeighborIds[i] >= 0)
787 {
788 ids.push_back(newCapNeighborIds[i]);
789 }
790 for (vtkIdType id : ids)
791 {
792 newBifurcationIds->SetValue(id,static_cast<int>(bifurcationIds->GetTuple1(i)));
793 newBranchIds->SetValue(id,static_cast<int>(branchIds->GetTuple1(i)));
794 newCenterlineId->SetTuple(id,i,centerlineId);
795 newGlobalNodeId->SetValue(id,static_cast<int>(id));
796 }
797
798 points->SetPoint(newPointIds[i],point);
799 newRadius->SetValue(newPointIds[i],radius->GetTuple1(i));
800 newTangents->SetTuple(newPointIds[i],tangent);
801
802 if (newCapNeighborIds[i] < 0)
803 {
804 continue;
805 }
806 // The new point is halfway to the other point of the cap's line
807 centerlines->GetCellPoints(pointCells[i][0],cellPointIds);
808 vtkIdType neighborId = cellPointIds->GetId(0) == i ? cellPointIds->GetId(1) : cellPointIds->GetId(0);
809 centerlines->GetPoint(neighborId,neighborPoint);
810 tangents->GetTuple(neighborId,neighborTangent);
811 double newPoint[3], newTangent[3];
812 for (int j=0; j<3; j++)
813 {
814 newPoint[j] = 0.5 * (point[j] + neighborPoint[j]);
815 newTangent[j] = 0.5 * (tangent[j] + neighborTangent[j]);
816 }
817 vtkMath::Normalize(newTangent);
818 points->SetPoint(newCapNeighborIds[i],newPoint);
819 newTangents->SetTuple(newCapNeighborIds[i],newTangent);
820 newRadius->SetValue(newCapNeighborIds[i],0.5 * (radius->GetTuple1(i) + radius->GetTuple1(neighborId)));
821 }
822
823 // Lines keep the order of the cells, with the line to a new point inserted
824 // next to the line of its cap
825 vtkNew<vtkCellArray> lines;
826 if (newCapNeighborIds[0] >= 0)
827 {
828 vtkIdType line[2] = {newPointIds[0], newCapNeighborIds[0]};
829 lines->InsertNextCell(2,line);
830 }
831 for (vtkIdType cellId=0; cellId<centerlines->GetNumberOfCells(); cellId++)
832 {
833 centerlines->GetCellPoints(cellId,cellPointIds);
834 vtkIdType pointId0 = cellPointIds->GetId(0);
835 vtkIdType pointId1 = cellPointIds->GetId(1);
836 if (pointId1 == 0 || (pointId0 != 0 && newCapNeighborIds[pointId0] >= 0))
837 {
838 vtkErrorMacro(<<"Centerline lines are not oriented from the inlet");
839 return false;
840 }
841 vtkIdType newPointId0 = pointId0 == 0 && newCapNeighborIds[0] >= 0 ? newCapNeighborIds[0] : newPointIds[pointId0];
842 if (newCapNeighborIds[pointId1] >= 0)
843 {
844 vtkIdType line0[2] = {newPointId0, newCapNeighborIds[pointId1]};
845 vtkIdType line1[2] = {newCapNeighborIds[pointId1], newPointIds[pointId1]};
846 lines->InsertNextCell(2,line0);
847 lines->InsertNextCell(2,line1);
848 }
849 else
850 {
851 vtkIdType line[2] = {newPointId0, newPointIds[pointId1]};
852 lines->InsertNextCell(2,line);
853 }
854 }
855
856 vtkNew<vtkPolyData> refined;
857 refined->SetPoints(points);
858 refined->SetLines(lines);
859 refined->GetPointData()->AddArray(newBifurcationIds);
860 refined->GetPointData()->AddArray(newBranchIds);
861 refined->GetPointData()->AddArray(newRadius);
862 refined->GetPointData()->AddArray(newCenterlineId);
863 refined->GetPointData()->AddArray(newGlobalNodeId);
864 refined->GetPointData()->AddArray(newTangents);
865
866 this->Centerlines->DeepCopy(refined);
867 return true;
868}
869
870bool vtkvmtkPolyDataCenterlineBranchSplitting::LabelSurface(const char* labelArrayName, const char* otherArrayName)
871{
872 vtkDataArray* centerlineLabels = this->Centerlines->GetPointData()->GetArray(labelArrayName);
873 vtkDataArray* centerlineOtherLabels = this->Centerlines->GetPointData()->GetArray(otherArrayName);
874 if (!centerlineLabels || !centerlineOtherLabels)
875 {
876 vtkErrorMacro(<<"Centerlines have no point data array named " << (centerlineLabels ? otherArrayName : labelArrayName));
877 return false;
878 }
879
880 const vtkIdType numberOfSurfacePoints = this->Surface->GetNumberOfPoints();
881 vtkNew<vtkIntArray> surfaceLabels;
882 surfaceLabels->SetName(labelArrayName);
883 surfaceLabels->SetNumberOfValues(numberOfSurfacePoints);
884 surfaceLabels->Fill(-1);
885 std::vector<double> labelDistances(numberOfSurfacePoints,-1.0);
886
887 vtkNew<vtkPointLocator> locator;
888 BuildPointLocator(this->Surface,locator);
889
890 vtkDataArray* radius = this->Centerlines->GetPointData()->GetArray(this->RadiusArrayName);
891 vtkDataArray* surfaceNormals = this->Surface->GetPointData()->GetNormals();
892
893 vtkNew<vtkIdList> surfacePointIds;
894 double centerlinePoint[3], surfacePoint[3], normal[3], offset[3];
895 for (vtkIdType i=0; i<this->Centerlines->GetNumberOfPoints(); i++)
896 {
897 if (centerlineOtherLabels->GetTuple1(i) != -1)
898 {
899 continue;
900 }
901 int label = static_cast<int>(centerlineLabels->GetTuple1(i));
902
903 this->Centerlines->GetPoint(i,centerlinePoint);
904 locator->FindPointsWithinRadius(10.0 * radius->GetTuple1(i),centerlinePoint,surfacePointIds);
905 for (vtkIdType j=0; j<surfacePointIds->GetNumberOfIds(); j++)
906 {
907 vtkIdType surfacePointId = surfacePointIds->GetId(j);
908 this->Surface->GetPoint(surfacePointId,surfacePoint);
909 surfaceNormals->GetTuple(surfacePointId,normal);
910 vtkMath::Subtract(surfacePoint,centerlinePoint,offset);
911 double distance = vtkMath::Norm(offset);
912 // Only surface points that face the centerline point from the outside
913 // TODO: the tolerance is in absolute units
914 if (vtkMath::Dot(offset,normal) < -1.0e-2)
915 {
916 continue;
917 }
918 if (surfaceLabels->GetValue(surfacePointId) > -1 && distance > labelDistances[surfacePointId])
919 {
920 continue;
921 }
922 surfaceLabels->SetValue(surfacePointId,label);
923 labelDistances[surfacePointId] = distance;
924 }
925 }
926
927 this->Surface->GetPointData()->AddArray(surfaceLabels);
928 return true;
929}
930
932{
933 vtkPolyData* centerlines = this->Centerlines;
934 const vtkIdType numberOfPoints = centerlines->GetNumberOfPoints();
935
936 vtkIntArray* bifurcation = vtkIntArray::SafeDownCast(centerlines->GetPointData()->GetArray(this->CenterlineSectionBifurcationArrayName));
937 vtkDataArray* bifurcationIds = centerlines->GetPointData()->GetArray(BifurcationIdTmpArrayName);
938
939 vtkNew<vtkIntArray> remove;
940 remove->SetName(RemoveArrayName);
941 remove->SetNumberOfValues(numberOfPoints);
942 remove->Fill(0);
943 centerlines->GetPointData()->AddArray(remove);
944
945 // Points where the centerlines split are always in a bifurcation region, so
946 // that each bifurcation region is connected
947 for (vtkIdType i=0; i<numberOfPoints; i++)
948 {
949 if (bifurcationIds->GetTuple1(i) == Bifurcation)
950 {
951 bifurcation->SetValue(i,Bifurcation);
952 }
953 }
954
955 vtkNew<vtkPolyData> bifurcations;
956 vtkNew<vtkPolyData> branches;
957 this->SplitCenterlines(bifurcations,branches);
958
959 // A bifurcation region whose points are all in one preliminary branch is in
960 // the middle of a branch: it becomes branch again, and its points other
961 // than caps are removed
962 if (bifurcations->GetNumberOfCells() > 0)
963 {
964 vtkNew<vtkConnectivityFilter> connectivity;
965 connectivity->SetInputData(bifurcations);
966 connectivity->SetExtractionModeToAllRegions();
967 connectivity->ColorRegionsOn();
968 connectivity->Update();
969
970 vtkNew<vtkThreshold> threshold;
971 threshold->SetInputConnection(connectivity->GetOutputPort());
972 threshold->SetInputArrayToProcess(0,0,0,vtkDataObject::FIELD_ASSOCIATION_CELLS,RegionIdArrayName);
973 threshold->SetThresholdFunction(vtkThreshold::THRESHOLD_BETWEEN);
974
975 vtkNew<vtkIdList> cellPointIds;
976 for (vtkIdType regionId=0; regionId<connectivity->GetNumberOfExtractedRegions(); regionId++)
977 {
978 threshold->SetLowerThreshold(regionId);
979 threshold->SetUpperThreshold(regionId);
980 threshold->Update();
981 vtkUnstructuredGrid* region = threshold->GetOutput();
982 vtkDataArray* regionBranchIds = region->GetPointData()->GetArray(BranchIdTmpArrayName);
983 vtkDataArray* regionGlobalNodeIds = region->GetPointData()->GetArray(this->GlobalNodeIdArrayName);
984
985 std::set<int> branchIdsInRegion;
986 for (vtkIdType j=0; j<region->GetNumberOfPoints(); j++)
987 {
988 branchIdsInRegion.insert(static_cast<int>(regionBranchIds->GetTuple1(j)));
989 }
990 if (branchIdsInRegion.size() != 1)
991 {
992 continue;
993 }
994
995 std::vector<int> numberOfRegionPointCells(region->GetNumberOfPoints(),0);
996 for (vtkIdType cellId=0; cellId<region->GetNumberOfCells(); cellId++)
997 {
998 region->GetCellPoints(cellId,cellPointIds);
999 for (vtkIdType k=0; k<cellPointIds->GetNumberOfIds(); k++)
1000 {
1001 numberOfRegionPointCells[cellPointIds->GetId(k)]++;
1002 }
1003 }
1004 for (vtkIdType j=0; j<region->GetNumberOfPoints(); j++)
1005 {
1006 vtkIdType pointId = static_cast<vtkIdType>(regionGlobalNodeIds->GetTuple1(j));
1007 bifurcation->SetValue(pointId,Branch);
1008 if (numberOfRegionPointCells[j] != 1)
1009 {
1010 remove->SetValue(pointId,1);
1011 }
1012 }
1013 }
1014 }
1015
1016 // A branch point with no branch neighbor between bifurcations is part of
1017 // the bifurcation
1018 std::vector<std::vector<vtkIdType>> pointCells = GetPointCells(centerlines);
1019 vtkNew<vtkIdList> cellPointIds;
1020 for (vtkIdType i=0; i<numberOfPoints; i++)
1021 {
1022 if (bifurcation->GetValue(i) != Branch)
1023 {
1024 continue;
1025 }
1026 bool isolated = true;
1027 for (vtkIdType cellId : pointCells[i])
1028 {
1029 centerlines->GetCellPoints(cellId,cellPointIds);
1030 for (vtkIdType k=0; k<cellPointIds->GetNumberOfIds(); k++)
1031 {
1032 vtkIdType id = cellPointIds->GetId(k);
1033 if (id != i && bifurcation->GetValue(id) == Branch)
1034 {
1035 isolated = false;
1036 }
1037 }
1038 }
1039 if (isolated)
1040 {
1041 bifurcation->SetValue(i,Bifurcation);
1042 }
1043 }
1044
1045 // The two points at each cap are in a branch
1046 bifurcation->SetValue(0,Branch);
1047 bifurcation->SetValue(1,Branch);
1048 for (vtkIdType i=1; i<numberOfPoints; i++)
1049 {
1050 if (pointCells[i].size() == 1)
1051 {
1052 bifurcation->SetValue(i-1,Branch);
1053 bifurcation->SetValue(i,Branch);
1054 }
1055 }
1056
1057 // Remove the points marked for removal with the lines that use them
1058 vtkNew<vtkPointDataToCellData> pointToCellData;
1059 pointToCellData->SetInputData(centerlines);
1060 pointToCellData->PassPointDataOn();
1061
1062 vtkNew<vtkThreshold> threshold;
1063 threshold->SetInputConnection(pointToCellData->GetOutputPort());
1064 threshold->SetInputArrayToProcess(0,0,0,vtkDataObject::FIELD_ASSOCIATION_CELLS,RemoveArrayName);
1065 threshold->SetThresholdFunction(vtkThreshold::THRESHOLD_BETWEEN);
1066 threshold->SetLowerThreshold(0.0);
1067 threshold->SetUpperThreshold(0.0);
1068
1069 threshold->Update();
1070 vtkNew<vtkPolyData> kept;
1071 LinesToPolyData(threshold->GetOutput(),kept);
1072
1073 // Points keep their order, so a gap in the old point ids is where points
1074 // were removed: connect the points on either side of it
1075 vtkDataArray* oldGlobalNodeIds = kept->GetPointData()->GetArray(this->GlobalNodeIdArrayName);
1076 vtkNew<vtkCellArray> lines;
1077 lines->DeepCopy(kept->GetLines());
1078 vtkNew<vtkIntArray> globalNodeIds;
1079 globalNodeIds->SetName(this->GlobalNodeIdArrayName);
1080 globalNodeIds->SetNumberOfValues(kept->GetNumberOfPoints());
1081 if (kept->GetNumberOfPoints() > 0)
1082 {
1083 globalNodeIds->SetValue(0,0);
1084 }
1085 for (vtkIdType i=1; i<kept->GetNumberOfPoints(); i++)
1086 {
1087 if (oldGlobalNodeIds->GetTuple1(i-1) + 1 != oldGlobalNodeIds->GetTuple1(i))
1088 {
1089 vtkIdType line[2] = {i-1, i};
1090 lines->InsertNextCell(2,line);
1091 }
1092 globalNodeIds->SetValue(i,static_cast<int>(i));
1093 }
1094
1095 vtkNew<vtkPolyData> cleaned;
1096 cleaned->SetPoints(kept->GetPoints());
1097 cleaned->SetLines(lines);
1098 cleaned->GetPointData()->ShallowCopy(kept->GetPointData());
1099 cleaned->GetPointData()->RemoveArray(RemoveArrayName);
1100 cleaned->GetPointData()->AddArray(globalNodeIds);
1101
1102 if (cleaned->GetNumberOfPoints() != cleaned->GetNumberOfCells() + 1)
1103 {
1104 vtkErrorMacro(<<"Centerlines do not form a tree after removing bifurcation regions within branches");
1105 return false;
1106 }
1107
1108 this->Centerlines->DeepCopy(cleaned);
1109 return true;
1110}
1111
1112void vtkvmtkPolyDataCenterlineBranchSplitting::SplitCenterlines(vtkPolyData* bifurcations, vtkPolyData* branches)
1113{
1114 // A line is in a bifurcation region if either of its points is
1115 vtkDataArray* pointBifurcation = this->Centerlines->GetPointData()->GetArray(this->CenterlineSectionBifurcationArrayName);
1116 vtkNew<vtkIntArray> cellBifurcation;
1117 cellBifurcation->SetName(this->CenterlineSectionBifurcationArrayName);
1118 cellBifurcation->SetNumberOfValues(this->Centerlines->GetNumberOfCells());
1119 vtkNew<vtkIdList> cellPointIds;
1120 for (vtkIdType cellId=0; cellId<this->Centerlines->GetNumberOfCells(); cellId++)
1121 {
1122 this->Centerlines->GetCellPoints(cellId,cellPointIds);
1123 int value = Branch;
1124 for (vtkIdType k=0; k<cellPointIds->GetNumberOfIds(); k++)
1125 {
1126 if (pointBifurcation->GetTuple1(cellPointIds->GetId(k)) == Bifurcation)
1127 {
1128 value = Bifurcation;
1129 }
1130 }
1131 cellBifurcation->SetValue(cellId,value);
1132 }
1133 this->Centerlines->GetCellData()->AddArray(cellBifurcation);
1134
1135 vtkNew<vtkThreshold> threshold;
1136 threshold->SetInputData(this->Centerlines);
1137 threshold->SetThresholdFunction(vtkThreshold::THRESHOLD_BETWEEN);
1138 threshold->SetInputArrayToProcess(0,0,0,vtkDataObject::FIELD_ASSOCIATION_CELLS,this->CenterlineSectionBifurcationArrayName);
1139
1140 for (int value : {Branch, Bifurcation})
1141 {
1142 threshold->SetLowerThreshold(value);
1143 threshold->SetUpperThreshold(value);
1144 threshold->Update();
1145 LinesToPolyData(threshold->GetOutput(),value == Branch ? branches : bifurcations);
1146 }
1147}
1148
1149void vtkvmtkPolyDataCenterlineBranchSplitting::LabelConnectedRegions(vtkPolyData* lines, const char* labelArrayName, const char* otherArrayName)
1150{
1151 vtkNew<vtkConnectivityFilter> connectivity;
1152 connectivity->SetInputData(lines);
1153 connectivity->SetExtractionModeToAllRegions();
1154 connectivity->ColorRegionsOn();
1155 connectivity->Update();
1156
1157 vtkNew<vtkPolyData> labeled;
1158 labeled->DeepCopy(connectivity->GetOutput());
1159 vtkDataArray* regionIds = labeled->GetPointData()->GetArray(RegionIdArrayName);
1160 vtkDataArray* globalNodeIds = labeled->GetPointData()->GetArray(this->GlobalNodeIdArrayName);
1161 const vtkIdType numberOfPoints = labeled->GetNumberOfPoints();
1162
1163 // The path of each point is the length of the polyline through the points
1164 // of its region, in the order of their ids, up to the point
1165 vtkNew<vtkDoubleArray> path;
1166 path->SetName(this->PathArrayName);
1167 path->SetNumberOfValues(numberOfPoints);
1168 path->Fill(-1.0);
1169
1170 std::vector<std::vector<std::pair<double,vtkIdType>>> regionPoints(connectivity->GetNumberOfExtractedRegions());
1171 for (vtkIdType i=0; i<numberOfPoints; i++)
1172 {
1173 regionPoints[static_cast<size_t>(regionIds->GetTuple1(i))].push_back({globalNodeIds->GetTuple1(i), i});
1174 }
1175 double point[3], previousPoint[3];
1176 for (std::vector<std::pair<double,vtkIdType>>& points : regionPoints)
1177 {
1178 std::sort(points.begin(),points.end());
1179 double distance = 0.0;
1180 for (size_t k=0; k<points.size(); k++)
1181 {
1182 labeled->GetPoint(points[k].second,point);
1183 if (k > 0)
1184 {
1185 distance += sqrt(vtkMath::Distance2BetweenPoints(point,previousPoint));
1186 }
1187 path->SetValue(points[k].second,distance);
1188 previousPoint[0] = point[0];
1189 previousPoint[1] = point[1];
1190 previousPoint[2] = point[2];
1191 }
1192 }
1193 labeled->GetPointData()->AddArray(path);
1194
1195 RenameRegionIds(labeled->GetPointData(),labelArrayName);
1196 RenameRegionIds(labeled->GetCellData(),labelArrayName);
1197
1198 vtkNew<vtkIdTypeArray> otherPointIds;
1199 otherPointIds->SetName(otherArrayName);
1200 otherPointIds->SetNumberOfValues(numberOfPoints);
1201 otherPointIds->Fill(-1);
1202 labeled->GetPointData()->AddArray(otherPointIds);
1203 vtkNew<vtkIdTypeArray> otherCellIds;
1204 otherCellIds->SetName(otherArrayName);
1205 otherCellIds->SetNumberOfValues(labeled->GetNumberOfCells());
1206 otherCellIds->Fill(-1);
1207 labeled->GetCellData()->AddArray(otherCellIds);
1208
1209 lines->DeepCopy(labeled);
1210}
1211
1213{
1214 vtkNew<vtkPolyData> bifurcations;
1215 vtkNew<vtkPolyData> branches;
1216 this->SplitCenterlines(bifurcations,branches);
1217
1218 vtkNew<vtkAppendFilter> append;
1219 append->MergePointsOn();
1220 if (bifurcations->GetNumberOfCells() > 0)
1221 {
1222 this->LabelConnectedRegions(bifurcations,this->BifurcationIdArrayName,this->BranchIdArrayName);
1223 append->AddInputData(bifurcations);
1224 }
1225 if (branches->GetNumberOfCells() > 0)
1226 {
1228 append->AddInputData(branches);
1229 }
1230 if (append->GetNumberOfInputConnections(0) == 0)
1231 {
1232 vtkErrorMacro(<<"Centerlines have no lines left");
1233 return false;
1234 }
1235
1236 append->Update();
1237 vtkNew<vtkPolyData> merged;
1238 LinesToPolyData(append->GetOutput(),merged);
1239
1240 if (!IsOnePiece(merged))
1241 {
1242 vtkErrorMacro(<<"Centerlines consist of more than one piece after grouping");
1243 return false;
1244 }
1245
1246 // Put the points back in the order of their ids
1247 const vtkIdType numberOfPoints = merged->GetNumberOfPoints();
1248 vtkDataArray* globalNodeIds = merged->GetPointData()->GetArray(this->GlobalNodeIdArrayName);
1249 std::vector<vtkIdType> mergedPointIds(numberOfPoints,-1);
1250 for (vtkIdType i=0; i<numberOfPoints; i++)
1251 {
1252 vtkIdType id = static_cast<vtkIdType>(globalNodeIds->GetTuple1(i));
1253 if (id < 0 || id >= numberOfPoints || mergedPointIds[id] >= 0)
1254 {
1255 vtkErrorMacro(<<"Centerline point ids are not unique after grouping");
1256 return false;
1257 }
1258 mergedPointIds[id] = i;
1259 }
1260
1261 vtkNew<vtkPoints> points;
1262 points->SetNumberOfPoints(numberOfPoints);
1263 vtkNew<vtkPolyData> ordered;
1264 ordered->GetPointData()->CopyAllocate(merged->GetPointData(),numberOfPoints);
1265 for (vtkIdType id=0; id<numberOfPoints; id++)
1266 {
1267 points->SetPoint(id,merged->GetPoint(mergedPointIds[id]));
1268 ordered->GetPointData()->CopyData(merged->GetPointData(),mergedPointIds[id],id);
1269 }
1270
1271 vtkNew<vtkCellArray> lines;
1272 vtkNew<vtkIdList> cellPointIds;
1273 for (vtkIdType cellId=0; cellId<merged->GetNumberOfCells(); cellId++)
1274 {
1275 merged->GetCellPoints(cellId,cellPointIds);
1276 lines->InsertNextCell(cellPointIds->GetNumberOfIds());
1277 for (vtkIdType k=0; k<cellPointIds->GetNumberOfIds(); k++)
1278 {
1279 lines->InsertCellPoint(static_cast<vtkIdType>(globalNodeIds->GetTuple1(cellPointIds->GetId(k))));
1280 }
1281 }
1282
1283 ordered->SetPoints(points);
1284 ordered->SetLines(lines);
1285
1286 this->Centerlines->DeepCopy(ordered);
1287 return true;
1288}
1289
1290bool vtkvmtkPolyDataCenterlineBranchSplitting::ComputeSectionPlane(vtkIdType cellId, vtkIdType cellPointIndex, double origin[3], double normal[3])
1291{
1292 vtkNew<vtkIdList> cellPointIds;
1293 this->Centerlines->GetCellPoints(cellId,cellPointIds);
1294 vtkIdType pointId = cellPointIds->GetId(cellPointIndex);
1295
1296 this->Centerlines->GetPoint(pointId,origin);
1297 this->Internals->CenterlineNormal->GetTuple(pointId,normal);
1298 if (vtkMath::Norm(normal) == 0.0)
1299 {
1300 return false;
1301 }
1302
1303 // Move the plane slightly into the tree at caps, so that it cuts the vessel
1304 // wall rather than the cap
1305 if (this->Internals->NumberOfPointCells[pointId] == 1)
1306 {
1307 const double offset = pointId == 0 ? -1.0e-3 : 1.0e-3;
1308 for (int j=0; j<3; j++)
1309 {
1310 origin[j] -= offset * normal[j];
1311 }
1312 }
1313 return true;
1314}
1315
1316void vtkvmtkPolyDataCenterlineBranchSplitting::ExtractSection(vtkPolyData* input, double origin[3], double normal[3], vtkPolyData* section, bool & closed)
1317{
1318 vtkvmtkPolyDataBranchSections::ExtractCylinderSection(input,origin,normal,section,closed,BranchIdTmpArrayName);
1319 // Sections of only a few points are slivers of the surface
1320 if (section->GetNumberOfPoints() < 4)
1321 {
1322 section->Initialize();
1323 }
1324}
1325
1326void vtkvmtkPolyDataCenterlineBranchSplitting::ProcessSection(vtkPolyData* vtkNotUsed(input), vtkIdType pointId, vtkIdType vtkNotUsed(sectionId), vtkPolyData* section, double origin[3], double normal[3], bool vtkNotUsed(closed))
1327{
1328 // A section is in a single branch if one centerline passes through it and
1329 // it touches the surface of one branch
1330 int numberOfCenterlines = vtkvmtkPolyDataBranchSections::CountBranchSectionCenterlines(section,this->Centerlines,origin,normal);
1331 int numberOfBranches = vtkvmtkPolyDataBranchSections::CountBranchSectionIds(section,BranchIdTmpArrayName);
1332 int bifurcation = numberOfCenterlines == 1 && numberOfBranches == 1 ? Branch : Bifurcation;
1333
1334 this->Internals->SectionBifurcation->InsertNextValue(bifurcation);
1335 this->Internals->SectionGlobalNodeId->InsertNextValue(static_cast<int>(pointId));
1336 this->Internals->CenterlineBifurcation->SetValue(pointId,bifurcation);
1337}
1338
1339void vtkvmtkPolyDataCenterlineBranchSplitting::PrintSelf(std::ostream& os, vtkIndent indent)
1340{
1341 this->Superclass::PrintSelf(os,indent);
1342 os << indent << "RadiusArrayName: " << (this->RadiusArrayName ? this->RadiusArrayName : "(none)") << "\n";
1343 os << indent << "BranchIdArrayName: " << (this->BranchIdArrayName ? this->BranchIdArrayName : "(none)") << "\n";
1344 os << indent << "BifurcationIdArrayName: " << (this->BifurcationIdArrayName ? this->BifurcationIdArrayName : "(none)") << "\n";
1345 os << indent << "PathArrayName: " << (this->PathArrayName ? this->PathArrayName : "(none)") << "\n";
1346 os << indent << "CenterlineIdArrayName: " << (this->CenterlineIdArrayName ? this->CenterlineIdArrayName : "(none)") << "\n";
1347 os << indent << "GlobalNodeIdArrayName: " << (this->GlobalNodeIdArrayName ? this->GlobalNodeIdArrayName : "(none)") << "\n";
1348 os << indent << "CenterlineSectionNormalArrayName: " << (this->CenterlineSectionNormalArrayName ? this->CenterlineSectionNormalArrayName : "(none)") << "\n";
1349 os << indent << "CenterlineSectionBifurcationArrayName: " << (this->CenterlineSectionBifurcationArrayName ? this->CenterlineSectionBifurcationArrayName : "(none)") << "\n";
1350}
static int CountBranchSectionIds(vtkPolyData *section, const char *idsArrayName)
static void ExtractCylinderSection(vtkPolyData *cylinder, double origin[3], double normal[3], vtkPolyData *section, bool &closed, const char *idsArrayName=nullptr)
static int CountBranchSectionCenterlines(vtkPolyData *section, vtkPolyData *centerlines, double origin[3], double normal[3])
Split centerlines into branches and bifurcations and cut vessel sections along them.
bool ComputeSectionPlane(vtkIdType cellId, vtkIdType cellPointIndex, double origin[3], double normal[3]) override
void LabelConnectedRegions(vtkPolyData *lines, const char *labelArrayName, const char *otherArrayName)
virtual void SetPathArrayName(const char *)
void ProcessSection(vtkPolyData *input, vtkIdType pointId, vtkIdType sectionId, vtkPolyData *section, double origin[3], double normal[3], bool closed) override
void SplitCenterlines(vtkPolyData *bifurcations, vtkPolyData *branches)
virtual void SetCenterlineSectionBifurcationArrayName(const char *)
virtual void SetBranchIdArrayName(const char *)
virtual void SetCenterlineSectionNormalArrayName(const char *)
int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void ExtractSection(vtkPolyData *input, double origin[3], double normal[3], vtkPolyData *section, bool &closed) override
bool LabelSurface(const char *labelArrayName, const char *otherArrayName)
virtual void SetGlobalNodeIdArrayName(const char *)
virtual void SetRadiusArrayName(const char *)
virtual void SetCenterlineIdArrayName(const char *)
void PrintSelf(std::ostream &os, vtkIndent indent) override
virtual void SetBifurcationIdArrayName(const char *)
virtual void SetCenterlineSectionMaxSizeArrayName(const char *)
virtual void SetCenterlineSectionAreaArrayName(const char *)
virtual void SetCenterlineSectionShapeArrayName(const char *)
virtual void SetCenterlineSectionClosedArrayName(const char *)
virtual void SetCenterlineSectionMinSizeArrayName(const char *)
void PrintSelf(std::ostream &os, vtkIndent indent) override
void ComputeCenterlineSections(vtkPolyData *input, vtkPolyData *output)
vtkStandardNewMacro(vtkvmtkPolyDataCenterlineBranchSplitting)