VMTK
Loading...
Searching...
No Matches
vtkvmtkPolyDataBranchSections.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkPolyDataBranchSections.cxx,v $
5Language: C++
6Date: $Date: 2006/10/17 15:16:16 $
7Version: $Revision: 1.1 $
8
9 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
10 See LICENSE file for details.
11
12 Portions of this code are covered under the VTK copyright.
13 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
14 for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notices for more information.
19
20=========================================================================*/
21
23#include "vtkPolyData.h"
24#include "vtkPolyLine.h"
25#include "vtkPolygon.h"
26#include "vtkTriangle.h"
27#include "vtkPointData.h"
28#include "vtkCellData.h"
29#include "vtkCellArray.h"
30#include "vtkDoubleArray.h"
31#include "vtkIntArray.h"
32#include "vtkPlane.h"
33#include "vtkLine.h"
34#include "vtkCutter.h"
35#include "vtkStripper.h"
36#include "vtkPolyDataConnectivityFilter.h"
37#include "vtkMath.h"
38#include "vtkCleanPolyData.h"
39#include "vtkAppendPolyData.h"
40#include "vtkvmtkMath.h"
42#include "vtkInformation.h"
43#include "vtkInformationVector.h"
44#include "vtkObjectFactory.h"
45#include "vtkVersion.h"
46#include "vtkNew.h"
47#include "vtkGenericCell.h"
48#include "vtkIdList.h"
49#include "vtkIdTypeArray.h"
50#include "vtkSmartPointer.h"
51
52#include <array>
53#include <set>
54#include <vector>
55
58
59
61
63{
64 this->GroupIdsArrayName = NULL;
65
66 this->Centerlines = NULL;
67
68 this->CenterlineRadiusArrayName = NULL;
69 this->CenterlineGroupIdsArrayName = NULL;
70 this->CenterlineIdsArrayName = NULL;
71 this->CenterlineTractIdsArrayName = NULL;
72 this->BlankingArrayName = NULL;
73
75 this->BranchSectionAreaArrayName = NULL;
78 this->BranchSectionShapeArrayName = NULL;
81
83 this->ReverseDirection = 0;
84}
85
87{
88 if (this->GroupIdsArrayName)
89 {
90 delete[] this->GroupIdsArrayName;
91 this->GroupIdsArrayName = NULL;
92 }
93
94 if (this->Centerlines)
95 {
96 this->Centerlines->Delete();
97 this->Centerlines = NULL;
98 }
99
101 {
102 delete[] this->CenterlineRadiusArrayName;
103 this->CenterlineRadiusArrayName = NULL;
104 }
105
107 {
108 delete[] this->CenterlineGroupIdsArrayName;
109 this->CenterlineGroupIdsArrayName = NULL;
110 }
111
112 if (this->CenterlineIdsArrayName)
113 {
114 delete[] this->CenterlineIdsArrayName;
115 this->CenterlineIdsArrayName = NULL;
116 }
117
119 {
120 delete[] this->CenterlineTractIdsArrayName;
121 this->CenterlineTractIdsArrayName = NULL;
122 }
123
124 if (this->BlankingArrayName)
125 {
126 delete[] this->BlankingArrayName;
127 this->BlankingArrayName = NULL;
128 }
129
131 {
132 delete[] this->BranchSectionGroupIdsArrayName;
134 }
135
137 {
138 delete[] this->BranchSectionAreaArrayName;
139 this->BranchSectionAreaArrayName = NULL;
140 }
141
143 {
144 delete[] this->BranchSectionMinSizeArrayName;
146 }
147
149 {
150 delete[] this->BranchSectionMaxSizeArrayName;
152 }
153
155 {
156 delete[] this->BranchSectionShapeArrayName;
157 this->BranchSectionShapeArrayName = NULL;
158 }
159
161 {
162 delete[] this->BranchSectionClosedArrayName;
163 this->BranchSectionClosedArrayName = NULL;
164 }
165
167 {
170 }
171}
172
174 vtkInformation *vtkNotUsed(request),
175 vtkInformationVector **inputVector,
176 vtkInformationVector *outputVector)
177{
178 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
179 vtkInformation *outInfo = outputVector->GetInformationObject(0);
180
181 vtkPolyData *input = vtkPolyData::SafeDownCast(
182 inInfo->Get(vtkDataObject::DATA_OBJECT()));
183 vtkPolyData *output = vtkPolyData::SafeDownCast(
184 outInfo->Get(vtkDataObject::DATA_OBJECT()));
185
186 if (!this->GroupIdsArrayName)
187 {
188 vtkErrorMacro(<<"GroupIdsArrayName not specified");
189 return 1;
190 }
191
192 vtkDataArray* groupIdsArray = input->GetPointData()->GetArray(this->GroupIdsArrayName);
193
194 if (!groupIdsArray)
195 {
196 vtkErrorMacro(<<"GroupIdsArray with name specified does not exist");
197 return 1;
198 }
199
200 if (!this->Centerlines)
201 {
202 vtkErrorMacro(<<"Centerlines not set");
203 return 1;
204 }
205
206 if (!this->CenterlineRadiusArrayName)
207 {
208 vtkErrorMacro(<<"CenterlineRadiusArrayName not specified");
209 return 1;
210 }
211
212 vtkDataArray* centerlineRadiusArray = this->Centerlines->GetPointData()->GetArray(this->CenterlineRadiusArrayName);
213
214 if (!centerlineRadiusArray)
215 {
216 vtkErrorMacro(<<"CenterlineRadiusArray with name specified does not exist");
217 return 1;
218 }
219
221 {
222 vtkErrorMacro(<<"CenterlineGroupIdsArrayName not specified");
223 return 1;
224 }
225
226 vtkDataArray* centerlineGroupIdsArray = this->Centerlines->GetCellData()->GetArray(this->CenterlineGroupIdsArrayName);
227
228 if (!centerlineGroupIdsArray)
229 {
230 vtkErrorMacro(<<"CenterlineGroupIdsArray with name specified does not exist");
231 return 1;
232 }
233
234 if (!this->CenterlineIdsArrayName)
235 {
236 vtkErrorMacro(<<"CenterlineIdsArrayName not specified");
237 return 1;
238 }
239
240 vtkDataArray* centerlineIdsArray = this->Centerlines->GetCellData()->GetArray(this->CenterlineIdsArrayName);
241
242 if (!centerlineIdsArray)
243 {
244 vtkErrorMacro(<<"CenterlineIdsArray with name specified does not exist");
245 return 1;
246 }
247
249 {
250 vtkErrorMacro(<<"CenterlineTractIdsArrayName not specified");
251 return 1;
252 }
253
254 vtkDataArray* centerlineTractIdsArray = this->Centerlines->GetCellData()->GetArray(this->CenterlineTractIdsArrayName);
255
256 if (!centerlineTractIdsArray)
257 {
258 vtkErrorMacro(<<"CenterlineTractIdsArray with name specified does not exist");
259 return 1;
260 }
261
262 if (!this->BlankingArrayName)
263 {
264 vtkErrorMacro(<<"BlankingArrayName not specified");
265 return 1;
266 }
267
268 vtkDataArray* blankingArray = this->Centerlines->GetCellData()->GetArray(this->BlankingArrayName);
269
270 if (!blankingArray)
271 {
272 vtkErrorMacro(<<"BlankingArray with name specified does not exist");
273 return 1;
274 }
275
277 {
278 vtkErrorMacro(<<"BranchSectionGroupIdsArrayName not specified");
279 return 1;
280 }
281
283 {
284 vtkErrorMacro(<<"BranchSectionAreaArrayName not specified");
285 return 1;
286 }
287
289 {
290 vtkErrorMacro(<<"BranchSectionMinSizeArrayName not specified");
291 return 1;
292 }
293
295 {
296 vtkErrorMacro(<<"BranchSectionMaxSizeArrayName not specified");
297 return 1;
298 }
299
301 {
302 vtkErrorMacro(<<"BranchSectionShapeArrayName not specified");
303 return 1;
304 }
305
307 {
308 vtkErrorMacro(<<"BranchSectionClosedArrayName not specified");
309 return 1;
310 }
311
313 {
314 vtkErrorMacro(<<"BranchSectionDistanceSpheresArrayName not specified");
315 return 1;
316 }
317
318 vtkPoints* outputPoints = vtkPoints::New();
319 vtkCellArray* outputPolys = vtkCellArray::New();
320
321 output->SetPoints(outputPoints);
322 output->SetPolys(outputPolys);
323
324 vtkIntArray* branchSectionGroupIdsArray = vtkIntArray::New();
325 branchSectionGroupIdsArray->SetName(this->BranchSectionGroupIdsArrayName);
326
327 vtkDoubleArray* branchSectionAreaArray = vtkDoubleArray::New();
328 branchSectionAreaArray->SetName(this->BranchSectionAreaArrayName);
329
330 vtkDoubleArray* branchSectionShapeArray = vtkDoubleArray::New();
331 branchSectionShapeArray->SetName(this->BranchSectionShapeArrayName);
332
333 vtkDoubleArray* branchSectionMinSizeArray = vtkDoubleArray::New();
334 branchSectionMinSizeArray->SetName(this->BranchSectionMinSizeArrayName);
335
336 vtkDoubleArray* branchSectionMaxSizeArray = vtkDoubleArray::New();
337 branchSectionMaxSizeArray->SetName(this->BranchSectionMaxSizeArrayName);
338
339 vtkIntArray* branchSectionClosedArray = vtkIntArray::New();
340 branchSectionClosedArray->SetName(this->BranchSectionClosedArrayName);
341
342 vtkIntArray* branchSectionDistanceSpheresArray = vtkIntArray::New();
343 branchSectionDistanceSpheresArray->SetName(this->BranchSectionDistanceSpheresArrayName);
344
345 output->GetCellData()->AddArray(branchSectionGroupIdsArray);
346 output->GetCellData()->AddArray(branchSectionAreaArray);
347 output->GetCellData()->AddArray(branchSectionMinSizeArray);
348 output->GetCellData()->AddArray(branchSectionMaxSizeArray);
349 output->GetCellData()->AddArray(branchSectionShapeArray);
350 output->GetCellData()->AddArray(branchSectionClosedArray);
351 output->GetCellData()->AddArray(branchSectionDistanceSpheresArray);
352
353 vtkIdList* nonBlankedGroupIds = vtkIdList::New();
355 int i;
356 for (i=0; i<nonBlankedGroupIds->GetNumberOfIds(); i++)
357 {
358 vtkIdType groupId = nonBlankedGroupIds->GetId(i);
359
360 this->ComputeBranchSections(input,groupId,output);
361 }
362
363 nonBlankedGroupIds->Delete();
364
365 outputPoints->Delete();
366 outputPolys->Delete();
367
368 branchSectionGroupIdsArray->Delete();
369 branchSectionAreaArray->Delete();
370 branchSectionMinSizeArray->Delete();
371 branchSectionMaxSizeArray->Delete();
372 branchSectionShapeArray->Delete();
373 branchSectionClosedArray->Delete();
374 branchSectionDistanceSpheresArray->Delete();
375
376 return 1;
377}
378
379void vtkvmtkPolyDataBranchSections::ComputeBranchSections(vtkPolyData* input, int groupId, vtkPolyData* output)
380{
381 vtkPoints* branchSectionPoints = output->GetPoints();
382 vtkCellArray* branchSectionPolys = output->GetPolys();
383
384 vtkIntArray* branchSectionGroupIdsArray = vtkIntArray::SafeDownCast(output->GetCellData()->GetArray(this->BranchSectionGroupIdsArrayName));
385 vtkDoubleArray* branchSectionAreaArray = vtkDoubleArray::SafeDownCast(output->GetCellData()->GetArray(this->BranchSectionAreaArrayName));
386 vtkDoubleArray* branchSectionMinSizeArray = vtkDoubleArray::SafeDownCast(output->GetCellData()->GetArray(this->BranchSectionMinSizeArrayName));
387 vtkDoubleArray* branchSectionMaxSizeArray = vtkDoubleArray::SafeDownCast(output->GetCellData()->GetArray(this->BranchSectionMaxSizeArrayName));
388 vtkDoubleArray* branchSectionShapeArray = vtkDoubleArray::SafeDownCast(output->GetCellData()->GetArray(this->BranchSectionShapeArrayName));
389 vtkIntArray* branchSectionClosedArray = vtkIntArray::SafeDownCast(output->GetCellData()->GetArray(this->BranchSectionClosedArrayName));
390 vtkIntArray* branchSectionDistanceSpheresArray = vtkIntArray::SafeDownCast(output->GetCellData()->GetArray(this->BranchSectionDistanceSpheresArrayName));
391
392 int i, j;
393
394 for (i=0; ; i++)
395 {
396 double averagePoint[3];
397 averagePoint[0] = averagePoint[1] = averagePoint[2] = 0.0;
398
399 double averageTangent[3];
400 averageTangent[0] = averageTangent[1] = averageTangent[2] = 0.0;
401
402 double weightSum = 0.0;
403
404 int totalNumberOfSpheres = i * this->NumberOfDistanceSpheres;
405 bool anyPoint = false;
406
407 vtkIdList* groupCellIds = vtkIdList::New();
409 for (j=0; j<groupCellIds->GetNumberOfIds(); j++)
410 {
411 vtkIdType centerlineCellId = groupCellIds->GetId(j);
412 vtkPoints* centerlineCellPoints = this->Centerlines->GetCell(centerlineCellId)->GetPoints();
413
414 vtkIdType firstSubId = 0;
415 double firstPCoord = 0.0;
416 bool reverseTouchingDirection = false;
417
418 if (this->ReverseDirection) {
419 firstSubId = centerlineCellPoints->GetNumberOfPoints()-2;
420 firstPCoord = 1.0;
421 reverseTouchingDirection = true;
422 }
423
424 vtkIdType touchingSubId = -1;
425 double touchingPCoord = 0.0;
426 vtkvmtkCenterlineSphereDistance::FindNTouchingSphereCenter(this->Centerlines,this->CenterlineRadiusArrayName,centerlineCellId,firstSubId,firstPCoord,totalNumberOfSpheres,touchingSubId,touchingPCoord,reverseTouchingDirection);
427
428 if (touchingSubId == -1)
429 {
430 continue;
431 }
432
433 anyPoint = true;
434 double touchingPoint[3];
435 vtkvmtkCenterlineUtilities::InterpolatePoint(this->Centerlines,centerlineCellId,touchingSubId,touchingPCoord,touchingPoint);
436
437 double touchingPoint0[3], touchingPoint1[3];
438 centerlineCellPoints->GetPoint(touchingSubId,touchingPoint0);
439 centerlineCellPoints->GetPoint(touchingSubId+1,touchingPoint1);
440
441 double touchingPointTangent[3];
442 touchingPointTangent[0] = touchingPoint1[0] - touchingPoint0[0];
443 touchingPointTangent[1] = touchingPoint1[1] - touchingPoint0[1];
444 touchingPointTangent[2] = touchingPoint1[2] - touchingPoint0[2];
445
446 vtkMath::Normalize(touchingPointTangent);
447 double touchingPointRadius = 0.0;
448 vtkvmtkCenterlineUtilities::InterpolateTuple1(this->Centerlines,this->CenterlineRadiusArrayName,centerlineCellId,touchingSubId,touchingPCoord,touchingPointRadius);
449
450 averagePoint[0] += touchingPointRadius * touchingPointRadius * touchingPoint[0];
451 averagePoint[1] += touchingPointRadius * touchingPointRadius * touchingPoint[1];
452 averagePoint[2] += touchingPointRadius * touchingPointRadius * touchingPoint[2];
453
454 averageTangent[0] += touchingPointRadius * touchingPointRadius * touchingPointTangent[0];
455 averageTangent[1] += touchingPointRadius * touchingPointRadius * touchingPointTangent[1];
456 averageTangent[2] += touchingPointRadius * touchingPointRadius * touchingPointTangent[2];
457
458 weightSum += touchingPointRadius * touchingPointRadius;
459 }
460
461 if (!anyPoint)
462 {
463 break;
464 }
465
466 averagePoint[0] /= weightSum;
467 averagePoint[1] /= weightSum;
468 averagePoint[2] /= weightSum;
469
470 averageTangent[0] /= weightSum;
471 averageTangent[1] /= weightSum;
472 averageTangent[2] /= weightSum;
473
474 vtkMath::Normalize(averageTangent);
475
476 //now cut branch with plane and get section. Compute section properties and store them.
477
478 vtkPolyData* cylinder = vtkPolyData::New();
479 vtkvmtkPolyDataBranchUtilities::ExtractGroup(input,this->GroupIdsArrayName,groupId,false,cylinder);
480
481 vtkPolyData* section = vtkPolyData::New();
482 bool closed = false;
483 this->ExtractCylinderSection(cylinder,averagePoint,averageTangent,section,closed);
484
485 section->BuildCells();
486 if (section->GetNumberOfCells() == 0)
487 {
488 groupCellIds->Delete();
489 cylinder->Delete();
490 section->Delete();
491 continue;
492 }
493 vtkPoints* sectionCellPoints = section->GetCell(0)->GetPoints();
494 int numberOfSectionCellPoints = sectionCellPoints->GetNumberOfPoints();
495 branchSectionPolys->InsertNextCell(numberOfSectionCellPoints);
496 int k;
497 for (k=0; k<numberOfSectionCellPoints; k++)
498 {
499 vtkIdType branchPointId = branchSectionPoints->InsertNextPoint(sectionCellPoints->GetPoint(k));
500 branchSectionPolys->InsertCellPoint(branchPointId);
501 }
502
503 double area = this->ComputeBranchSectionArea(section);
504 double sizeRange[2];
505 double shape = this->ComputeBranchSectionShape(section,averagePoint,sizeRange);
506
507 branchSectionGroupIdsArray->InsertNextValue(groupId);
508 branchSectionAreaArray->InsertNextValue(area);
509 branchSectionMinSizeArray->InsertNextValue(sizeRange[0]);
510 branchSectionMaxSizeArray->InsertNextValue(sizeRange[1]);
511 branchSectionShapeArray->InsertNextValue(shape);
512 branchSectionClosedArray->InsertNextValue(closed);
513 branchSectionDistanceSpheresArray->InsertNextValue(totalNumberOfSpheres);
514
515 groupCellIds->Delete();
516 cylinder->Delete();
517 section->Delete();
518 }
519}
520
521namespace
522{
523const char* SourceCellIdsArrayName = "vtkvmtkPolyDataBranchSectionsSourceCellIds";
524
525// Copy surfaceIdsArray to the points of section, which are the points where
526// the plane cuts surface edges. Interpolating along an edge would turn labels
527// into values that do not exist on the surface (between labels 0 and 3 it gives
528// 1 or 2), so each section point takes the value of the nearer end point of its
529// cut edge. The edge is found through the source cell ids that the cutter
530// passed on to the section lines as cell data. Any other point data of section
531// is removed.
532void CopyIdsFromNearestEdgePoint(vtkPolyData* surface, vtkDataArray* surfaceIdsArray, vtkPolyData* section)
533{
534 vtkIdTypeArray* sourceCellIds = vtkIdTypeArray::SafeDownCast(section->GetCellData()->GetArray(SourceCellIdsArrayName));
535
536 const vtkIdType numberOfSectionPoints = section->GetNumberOfPoints();
537 vtkSmartPointer<vtkDataArray> sectionIdsArray = vtkSmartPointer<vtkDataArray>::Take(surfaceIdsArray->NewInstance());
538 sectionIdsArray->SetName(surfaceIdsArray->GetName());
539 sectionIdsArray->SetNumberOfComponents(surfaceIdsArray->GetNumberOfComponents());
540 sectionIdsArray->SetNumberOfTuples(numberOfSectionPoints);
541 sectionIdsArray->Fill(0.0);
542
543 vtkNew<vtkGenericCell> sourceCell;
544 vtkNew<vtkIdList> pointCellIds;
545 double point[3], edgePoint0[3], edgePoint1[3], closestPoint[3];
546 for (vtkIdType i=0; sourceCellIds != NULL && i<numberOfSectionPoints; i++)
547 {
548 section->GetPointCells(i,pointCellIds);
549 if (pointCellIds->GetNumberOfIds() == 0)
550 {
551 continue;
552 }
553 section->GetPoint(i,point);
554 surface->GetCell(sourceCellIds->GetValue(pointCellIds->GetId(0)),sourceCell);
555 double minDistance2 = VTK_DOUBLE_MAX;
556 vtkIdType nearestSurfacePointId = -1;
557 for (int e=0; e<sourceCell->GetNumberOfEdges(); e++)
558 {
559 vtkCell* edge = sourceCell->GetEdge(e);
560 vtkIdType edgePointId0 = edge->GetPointId(0);
561 vtkIdType edgePointId1 = edge->GetPointId(1);
562 surface->GetPoint(edgePointId0,edgePoint0);
563 surface->GetPoint(edgePointId1,edgePoint1);
564 double t;
565 double distance2 = vtkLine::DistanceToLine(point,edgePoint0,edgePoint1,t,closestPoint);
566 if (distance2 < minDistance2)
567 {
568 minDistance2 = distance2;
569 nearestSurfacePointId = vtkMath::Distance2BetweenPoints(point,edgePoint0) <= vtkMath::Distance2BetweenPoints(point,edgePoint1) ? edgePointId0 : edgePointId1;
570 }
571 }
572 if (nearestSurfacePointId >= 0)
573 {
574 sectionIdsArray->SetTuple(i,nearestSurfacePointId,surfaceIdsArray);
575 }
576 }
577
578 section->GetPointData()->Initialize();
579 section->GetPointData()->AddArray(sectionIdsArray);
580}
581}
582
583void vtkvmtkPolyDataBranchSections::ExtractCylinderSection(vtkPolyData* cylinder, double origin[3], double normal[3], vtkPolyData* section, bool & closed, const char* idsArrayName)
584{
585 // Start from an empty section, so that a section with no cells always
586 // means that no section was found, even if section held an earlier cut.
587 section->Initialize();
588 closed = false;
589
590 vtkNew<vtkPlane> plane;
591 plane->SetOrigin(origin);
592 plane->SetNormal(normal);
593
594 vtkDataArray* surfaceIdsArray = idsArrayName != NULL ? cylinder->GetPointData()->GetArray(idsArrayName) : NULL;
595
596 vtkNew<vtkCutter> cutter;
597 vtkNew<vtkPolyData> cutInput;
598 if (surfaceIdsArray != NULL)
599 {
600 // The ids are not interpolated by the cutter but copied afterwards, so the
601 // surface is cut without its attributes, with only the source cell id of
602 // each cell attached.
603 cutInput->CopyStructure(cylinder);
604 vtkNew<vtkIdTypeArray> sourceCellIds;
605 sourceCellIds->SetName(SourceCellIdsArrayName);
606 sourceCellIds->SetNumberOfValues(cutInput->GetNumberOfCells());
607 for (vtkIdType i=0; i<cutInput->GetNumberOfCells(); i++)
608 {
609 sourceCellIds->SetValue(i,i);
610 }
611 cutInput->GetCellData()->AddArray(sourceCellIds);
612 cutter->SetInputData(cutInput);
613 }
614 else
615 {
616 cutter->SetInputData(cylinder);
617 }
618 cutter->SetCutFunction(plane);
619 // Without cut scalars the cutter passes no cell data on to its output, so
620 // section points could not be traced back to the cut surface cells.
621 cutter->GenerateCutScalarsOn();
622 cutter->SetValue(0,0.0);
623 cutter->Update();
624
625 vtkNew<vtkCleanPolyData> cleaner;
626 if (surfaceIdsArray != NULL)
627 {
628 cleaner->SetInputConnection(cutter->GetOutputPort());
629 }
630 else
631 {
632 // Interpolating and merging attributes nobody reads would be wasted
633 // work, so only the geometry of the cut is passed on.
634 vtkNew<vtkPolyData> cutGeometry;
635 cutGeometry->CopyStructure(cutter->GetOutput());
636 cleaner->SetInputData(cutGeometry);
637 }
638 cleaner->Update();
639
640 if (cleaner->GetOutput()->GetNumberOfPoints() == 0)
641 {
642 return;
643 }
644
645 vtkNew<vtkPolyDataConnectivityFilter> connectivityFilter;
646 connectivityFilter->SetInputConnection(cleaner->GetOutputPort());
647 connectivityFilter->SetExtractionModeToClosestPointRegion();
648 connectivityFilter->SetClosestPoint(origin);
649 connectivityFilter->Update();
650
651 section->DeepCopy(connectivityFilter->GetOutput());
652
653 // TODO: manually reconstruct single cell line from connectivity output
654
655 if (section->GetNumberOfCells() == 0)
656 {
657 return;
658 }
659
660 section->BuildCells();
661 section->BuildLinks();
662
663 if (surfaceIdsArray != NULL)
664 {
665 CopyIdsFromNearestEdgePoint(cylinder,surfaceIdsArray,section);
666 }
667
668 // find first point
669
670 int numberOfLinePoints = section->GetNumberOfPoints();
671
672 vtkIdType* cells;
673 vtkIdType npts;
674 const vtkIdType *pts;
675 vtkIdType ncells;
676
677 int numberOfSingleCellPoints = 0;
678 vtkIdType firstPointId = -1;
679
680 for (int i=0; i<numberOfLinePoints; i++)
681 {
682 section->GetPointCells(i,ncells,cells);
683 if (ncells == 1)
684 {
685 ++numberOfSingleCellPoints;
686 firstPointId = i;
687 }
688 }
689
690 if (numberOfSingleCellPoints == 0)
691 {
692 firstPointId = section->GetCell(0)->GetPointId(0);
693 }
694
695 vtkNew<vtkIdList> polygonPointIds;
696 polygonPointIds->InsertNextId(firstPointId);
697
698 bool done = false;
699 vtkIdType pointId = firstPointId;
700
701 vtkIdType cellId = -1;
702 while (!done)
703 {
704 section->GetPointCells(pointId,ncells,cells);
705 if (ncells == 1)
706 {
707 if (pointId == firstPointId)
708 {
709 cellId = cells[0];
710 }
711 else
712 {
713 done = true;
714 break;
715 }
716 }
717 else if (ncells == 2)
718 {
719 if (cells[0] == cellId)
720 {
721 cellId = cells[1];
722 }
723 else
724 {
725 cellId = cells[0];
726 }
727 }
728
729 section->GetCellPoints(cellId,npts,pts);
730
731 if (pts[0] == pointId)
732 {
733 pointId = pts[1];
734 }
735 else
736 {
737 pointId = pts[0];
738 }
739
740 if (pointId == firstPointId)
741 {
742 closed = true;
743 done = true;
744 break;
745 }
746
747 polygonPointIds->InsertNextId(pointId);
748 }
749
750 // Replace the connectivity line cells with the single reconstructed
751 // polygon. The arrays returned by GetLines()/GetPolys() must not be
752 // mutated in place: since VTK 9, GetPolys() hands back a dummy container
753 // when the polydata has no polys, so a polygon inserted into it would be
754 // lost. Also drop the cell map and links built above - they refer to the
755 // discarded line cells, and vtkPolyData::BuildCells() does not rebuild an
756 // existing map, so callers would crash reading the new cell through the
757 // stale bookkeeping.
758 vtkNew<vtkCellArray> sectionLines;
759 section->SetLines(sectionLines);
760
761 vtkNew<vtkCellArray> sectionPolys;
762 sectionPolys->InsertNextCell(polygonPointIds);
763 section->SetPolys(sectionPolys);
764
765 // The line cells carried the cell data; the polygon has none of its own.
766 section->GetCellData()->Initialize();
767
768 section->DeleteCells();
769 section->DeleteLinks();
770}
771
773{
774 branchSection->BuildCells();
775
776 if (branchSection->GetNumberOfCells() == 0)
777 {
778 return 0.0;
779 }
780
781 vtkPolygon* sectionPolygon = vtkPolygon::SafeDownCast(branchSection->GetCell(0));
782
783 vtkIdList* trianglePointIds = vtkIdList::New();
784
785 sectionPolygon->EarCutTriangulation(trianglePointIds);
786
787 int numberOfTriangles = trianglePointIds->GetNumberOfIds() / 3;
788
789 double polygonArea = 0.0;
790
791 for (int i=0; i<numberOfTriangles; i++)
792 {
793 vtkIdType pointId0 = trianglePointIds->GetId(3*i);
794 vtkIdType pointId1 = trianglePointIds->GetId(3*i+1);
795 vtkIdType pointId2 = trianglePointIds->GetId(3*i+2);
796
797 double point0[3], point1[3], point2[3];
798
799 sectionPolygon->GetPoints()->GetPoint(pointId0,point0);
800 sectionPolygon->GetPoints()->GetPoint(pointId1,point1);
801 sectionPolygon->GetPoints()->GetPoint(pointId2,point2);
802
803 double triangleArea = vtkTriangle::TriangleArea(point0,point1,point2);
804
805 polygonArea += triangleArea;
806 }
807
808 trianglePointIds->Delete();
809
810 return polygonArea;
811}
812
813#ifdef VMTK_ONE_SIDED_SECTION_SHAPE
814double vtkvmtkPolyDataBranchSections::ComputeBranchSectionShape(vtkPolyData* branchSection, double center[3], double sizeRange[2])
815{
816 branchSection->BuildCells();
817
818 if (branchSection->GetNumberOfCells() == 0)
819 {
820 sizeRange[0] = sizeRange[1] = 0.0;
821 return 0.0;
822 }
823
824 vtkPolygon* sectionPolygon = vtkPolygon::SafeDownCast(branchSection->GetCell(0));
825
826 int numberOfSectionPolygonPoints = sectionPolygon->GetNumberOfPoints();
827
828 double minDistance = VTK_VMTK_LARGE_DOUBLE;
829 double maxDistance = 0.0;
830
831 for (int i=0; i<numberOfSectionPolygonPoints; i++)
832 {
833 double point[3];
834 sectionPolygon->GetPoints()->GetPoint(i,point);
835 double distance = sqrt(vtkMath::Distance2BetweenPoints(point,center));
836
837 if (distance > maxDistance)
838 {
839 maxDistance = distance;
840 }
841
842 if (distance < minDistance)
843 {
844 minDistance = distance;
845 }
846 }
847
848 sizeRange[0] = minDistance;
849 sizeRange[1] = maxDistance;
850
851 double sectionShape = minDistance / maxDistance;
852
853 return sectionShape;
854}
855#else
856double vtkvmtkPolyDataBranchSections::ComputeBranchSectionShape(vtkPolyData* branchSection, double center[3], double sizeRange[2])
857{
858 branchSection->BuildCells();
859
860 if (branchSection->GetNumberOfCells() == 0)
861 {
862 sizeRange[0] = sizeRange[1] = 0.0;
863 return 0.0;
864 }
865
866 vtkPolygon* sectionPolygon = vtkPolygon::SafeDownCast(branchSection->GetCell(0));
867
868 int numberOfSectionPolygonPoints = sectionPolygon->GetNumberOfPoints();
869
870 double minDistance = VTK_VMTK_LARGE_DOUBLE;
871 double maxDistance = 0.0;
872
873 vtkIdType minDistanceId = -1;
874 vtkIdType maxDistanceId = -1;
875 double point[3];
876
877 for (int i=0; i<numberOfSectionPolygonPoints; i++)
878 {
879 sectionPolygon->GetPoints()->GetPoint(i,point);
880 double distance = sqrt(vtkMath::Distance2BetweenPoints(point,center));
881
882 if (distance > maxDistance)
883 {
884 maxDistance = distance;
885 maxDistanceId = i;
886 }
887
888 if (distance < minDistance)
889 {
890 minDistance = distance;
891 minDistanceId = i;
892 }
893 }
894
895 if (minDistance == -1 || maxDistance == -1)
896 {
897 sizeRange[0] = 0.0;
898 sizeRange[1] = 0.0;
899 return 0.0;
900 }
901
902 double point0[3];
903 double point1[3];
904
905 double planeNormal[3];
906 double radialVector0[3];
907 double radialVector1[3];
908 double cross[3];
909
910 planeNormal[0] = 0.0;
911 planeNormal[1] = 0.0;
912 planeNormal[2] = 0.0;
913
914 for (int i=0; i<numberOfSectionPolygonPoints; i++)
915 {
916 sectionPolygon->GetPoints()->GetPoint(i,point0);
917 sectionPolygon->GetPoints()->GetPoint((i+numberOfSectionPolygonPoints/4)%numberOfSectionPolygonPoints,point1);
918
919 radialVector0[0] = point0[0] - center[0];
920 radialVector0[1] = point0[1] - center[1];
921 radialVector0[2] = point0[2] - center[2];
922
923 radialVector1[0] = point1[0] - center[0];
924 radialVector1[1] = point1[1] - center[1];
925 radialVector1[2] = point1[2] - center[2];
926
927 vtkMath::Cross(point0,point1,cross);
928
929 planeNormal[0] += cross[0];
930 planeNormal[1] += cross[1];
931 planeNormal[2] += cross[2];
932 }
933
934 vtkMath::Normalize(planeNormal);
935
936 double minDistancePoint[3];
937 sectionPolygon->GetPoints()->GetPoint(minDistanceId,minDistancePoint);
938
939 double maxDistancePoint[3];
940 sectionPolygon->GetPoints()->GetPoint(maxDistanceId,maxDistancePoint);
941
942 double minDistanceNormal[3];
943 double maxDistanceNormal[3];
944
945 minDistanceNormal[0] = minDistancePoint[0] - center[0];
946 minDistanceNormal[1] = minDistancePoint[1] - center[1];
947 minDistanceNormal[2] = minDistancePoint[2] - center[2];
948
949 vtkMath::Normalize(minDistanceNormal);
950
951 maxDistanceNormal[0] = maxDistancePoint[0] - center[0];
952 maxDistanceNormal[1] = maxDistancePoint[1] - center[1];
953 maxDistanceNormal[2] = maxDistancePoint[2] - center[2];
954
955 vtkMath::Normalize(maxDistanceNormal);
956
957 double minDistanceOppositePoint[3];
958 double maxDistanceOppositePoint[3];
959
960 minDistanceOppositePoint[0] = center[0] - 2.0 * maxDistance * minDistanceNormal[0];
961 minDistanceOppositePoint[1] = center[1] - 2.0 * maxDistance * minDistanceNormal[1];
962 minDistanceOppositePoint[2] = center[2] - 2.0 * maxDistance * minDistanceNormal[2];
963
964 maxDistanceOppositePoint[0] = center[0] - 2.0 * maxDistance * maxDistanceNormal[0];
965 maxDistanceOppositePoint[1] = center[1] - 2.0 * maxDistance * maxDistanceNormal[1];
966 maxDistanceOppositePoint[2] = center[2] - 2.0 * maxDistance * maxDistanceNormal[2];
967
968 double intersectionPoint[3];
969 double maxIntersectionDistance = 0.0;
970
971 int intersection;
972 double u,v;
973 for (int i=0; i<numberOfSectionPolygonPoints; i++)
974 {
975 sectionPolygon->GetPoints()->GetPoint(i,point0);
976 sectionPolygon->GetPoints()->GetPoint((i+1)%numberOfSectionPolygonPoints,point1);
977
978 intersection = vtkLine::Intersection(minDistanceOppositePoint,center,point0,point1,u,v);
979
980 if (intersection == 0)
981 {
982 continue;
983 }
984
985 intersectionPoint[0] = (1.0 - u) * minDistanceOppositePoint[0] + u * center[0];
986 intersectionPoint[1] = (1.0 - u) * minDistanceOppositePoint[1] + u * center[1];
987 intersectionPoint[2] = (1.0 - u) * minDistanceOppositePoint[2] + u * center[2];
988
989 double intersectionDistance = sqrt(vtkMath::Distance2BetweenPoints(intersectionPoint,center));
990
991 if (intersectionDistance > maxIntersectionDistance)
992 {
993 maxIntersectionDistance = intersectionDistance;
994 }
995 }
996
997 minDistance += maxIntersectionDistance;
998
999 maxIntersectionDistance = 0.0;
1000
1001 for (int i=0; i<numberOfSectionPolygonPoints; i++)
1002 {
1003 sectionPolygon->GetPoints()->GetPoint(i,point0);
1004 sectionPolygon->GetPoints()->GetPoint((i+1)%numberOfSectionPolygonPoints,point1);
1005
1006 intersection = vtkLine::Intersection(maxDistanceOppositePoint,center,point0,point1,u,v);
1007
1008 if (intersection == 0)
1009 {
1010 continue;
1011 }
1012
1013 intersectionPoint[0] = (1.0 - u) * maxDistanceOppositePoint[0] + u * center[0];
1014 intersectionPoint[1] = (1.0 - u) * maxDistanceOppositePoint[1] + u * center[1];
1015 intersectionPoint[2] = (1.0 - u) * maxDistanceOppositePoint[2] + u * center[2];
1016
1017 double intersectionDistance = sqrt(vtkMath::Distance2BetweenPoints(intersectionPoint,center));
1018
1019 if (intersectionDistance > maxIntersectionDistance)
1020 {
1021 maxIntersectionDistance = intersectionDistance;
1022 }
1023 }
1024
1025 maxDistance += maxIntersectionDistance;
1026
1027 sizeRange[0] = minDistance;
1028 sizeRange[1] = maxDistance;
1029
1030 double sectionShape = minDistance / maxDistance;
1031
1032 return sectionShape;
1033}
1034#endif
1035
1036int vtkvmtkPolyDataBranchSections::CountBranchSectionCenterlines(vtkPolyData* section, vtkPolyData* centerlines, double origin[3], double normal[3])
1037{
1038 section->BuildCells();
1039
1040 if (section->GetNumberOfCells() == 0)
1041 {
1042 return 0;
1043 }
1044
1045 // The points where the centerline segments cross the plane. This is called
1046 // for every centerline point, and the setup of a vtkCutter for lines of a
1047 // few thousand points costs several times the crossings themselves, so they
1048 // are computed here, the same way as vtkCutter with vtkLine::Contour: the
1049 // signed distance of each point is stored as a float, a segment is crossed
1050 // where the distance of one end is below 0 and of the other at or above 0,
1051 // the crossing is interpolated from the end below 0 and stored with the
1052 // precision of the centerline points, and crossings at the same coordinates
1053 // are one point.
1054 std::set<std::array<double,3>> crossingPoints;
1055 if (centerlines->GetPoints())
1056 {
1057 std::vector<double> signedDistances(centerlines->GetNumberOfPoints());
1058 double point[3];
1059 for (vtkIdType i=0; i<centerlines->GetNumberOfPoints(); i++)
1060 {
1061 centerlines->GetPoint(i,point);
1062 signedDistances[i] = static_cast<float>(normal[0] * (point[0] - origin[0]) + normal[1] * (point[1] - origin[1]) + normal[2] * (point[2] - origin[2]));
1063 }
1064 const bool floatPoints = centerlines->GetPoints()->GetDataType() == VTK_FLOAT;
1065 vtkNew<vtkIdList> cellPointIds;
1066 double point0[3], point1[3];
1067 for (vtkIdType cellId=0; cellId<centerlines->GetNumberOfCells(); cellId++)
1068 {
1069 int cellType = centerlines->GetCellType(cellId);
1070 if (cellType != VTK_LINE && cellType != VTK_POLY_LINE)
1071 {
1072 continue;
1073 }
1074 centerlines->GetCellPoints(cellId,cellPointIds);
1075 for (vtkIdType k=0; k+1<cellPointIds->GetNumberOfIds(); k++)
1076 {
1077 vtkIdType pointId0 = cellPointIds->GetId(k);
1078 vtkIdType pointId1 = cellPointIds->GetId(k+1);
1079 double distance0 = signedDistances[pointId0];
1080 double distance1 = signedDistances[pointId1];
1081 if ((distance0 >= 0.0) == (distance1 >= 0.0))
1082 {
1083 continue;
1084 }
1085 // Interpolate from the end below 0 to the end at or above 0
1086 if (distance0 >= 0.0)
1087 {
1088 std::swap(pointId0,pointId1);
1089 std::swap(distance0,distance1);
1090 }
1091 centerlines->GetPoint(pointId0,point0);
1092 centerlines->GetPoint(pointId1,point1);
1093 const double t = (0.0 - distance0) / (distance1 - distance0);
1094 std::array<double,3> crossing;
1095 for (int j=0; j<3; j++)
1096 {
1097 crossing[j] = point0[j] + t * (point1[j] - point0[j]);
1098 if (floatPoints)
1099 {
1100 crossing[j] = static_cast<float>(crossing[j]);
1101 }
1102 }
1103 crossingPoints.insert(crossing);
1104 }
1105 }
1106 }
1107 if (crossingPoints.empty())
1108 {
1109 return 0;
1110 }
1111
1112 // Section and crossing points all lie in the cutting plane, so the
1113 // inside test is done in an orthonormal frame of that plane, with z set to
1114 // exactly 0. vtkPolygon::PointInPolygon rejects points outside the polygon
1115 // bounds without tolerance, and the bounds of a planar section have no
1116 // thickness along the normal, so a crossing point off the plane by rounding
1117 // alone would otherwise be counted as outside.
1118 double planeNormal[3] = {normal[0], normal[1], normal[2]};
1119 vtkMath::Normalize(planeNormal);
1120 double axis0[3], axis1[3];
1121 vtkMath::Perpendiculars(planeNormal,axis0,axis1,0.0);
1122
1123 vtkPoints* sectionPoints = section->GetCell(0)->GetPoints();
1124 const int numberOfPolygonPoints = static_cast<int>(sectionPoints->GetNumberOfPoints());
1125 if (numberOfPolygonPoints < 3)
1126 {
1127 return 0;
1128 }
1129 std::vector<double> polygon(3*numberOfPolygonPoints);
1130 double polygonBounds[6] = {VTK_DOUBLE_MAX, VTK_DOUBLE_MIN, VTK_DOUBLE_MAX, VTK_DOUBLE_MIN, 0.0, 0.0};
1131 double point[3], planePoint[3];
1132 for (int i=0; i<numberOfPolygonPoints; i++)
1133 {
1134 sectionPoints->GetPoint(i,point);
1135 vtkMath::Subtract(point,origin,point);
1136 double* polygonPoint = &polygon[3*i];
1137 polygonPoint[0] = vtkMath::Dot(point,axis0);
1138 polygonPoint[1] = vtkMath::Dot(point,axis1);
1139 polygonPoint[2] = 0.0;
1140 polygonBounds[0] = vtkMath::Min(polygonBounds[0],polygonPoint[0]);
1141 polygonBounds[1] = vtkMath::Max(polygonBounds[1],polygonPoint[0]);
1142 polygonBounds[2] = vtkMath::Min(polygonBounds[2],polygonPoint[1]);
1143 polygonBounds[3] = vtkMath::Max(polygonBounds[3],polygonPoint[1]);
1144 }
1145 double polygonNormal[3] = {0.0, 0.0, 1.0};
1146
1147 int numberOfCenterlines = 0;
1148 for (const std::array<double,3>& crossing : crossingPoints)
1149 {
1150 vtkMath::Subtract(crossing.data(),origin,point);
1151 planePoint[0] = vtkMath::Dot(point,axis0);
1152 planePoint[1] = vtkMath::Dot(point,axis1);
1153 planePoint[2] = 0.0;
1154 if (vtkPolygon::PointInPolygon(planePoint,numberOfPolygonPoints,polygon.data(),polygonBounds,polygonNormal) == 1)
1155 {
1156 ++numberOfCenterlines;
1157 }
1158 }
1159
1160 return numberOfCenterlines;
1161}
1162
1163int vtkvmtkPolyDataBranchSections::CountBranchSectionIds(vtkPolyData* section, const char* idsArrayName)
1164{
1165 vtkDataArray* idsArray = section->GetPointData()->GetArray(idsArrayName);
1166 if (idsArray == NULL)
1167 {
1168 return 0;
1169 }
1170
1171 std::set<vtkIdType> ids;
1172 for (vtkIdType i=0; i<idsArray->GetNumberOfTuples(); i++)
1173 {
1174 ids.insert(static_cast<vtkIdType>(idsArray->GetComponent(i,0)));
1175 }
1176
1177 return static_cast<int>(ids.size());
1178}
1179
1180void vtkvmtkPolyDataBranchSections::PrintSelf(std::ostream& os, vtkIndent indent)
1181{
1182 this->Superclass::PrintSelf(os,indent);
1183}
static void FindNTouchingSphereCenter(vtkPolyData *centerlines, const char *radiusArrayName, vtkIdType cellId, vtkIdType subId, double pcoord, int numberOfTouchingSpheres, vtkIdType &touchingSubId, double &touchingPCoord, bool forward=true)
static void InterpolatePoint(vtkPolyData *centerlines, int cellId, int subId, double pcoord, double interpolatedPoint[3])
static void GetGroupUniqueCellIds(vtkPolyData *centerlines, const char *groupIdsArrayName, vtkIdType groupId, vtkIdList *groupCellIds)
static void InterpolateTuple1(vtkPolyData *centerlines, const char *arrayName, int cellId, int subId, double pcoord, double &interpolatedTuple1)
static void GetNonBlankedGroupsIdList(vtkPolyData *centerlines, const char *groupIdsArrayName, const char *blankingArrayName, vtkIdList *groupIds)
Extract a vessel (cross) section n-spheres distance from from the start point of a branch.
static int CountBranchSectionIds(vtkPolyData *section, const char *idsArrayName)
void PrintSelf(std::ostream &os, vtkIndent indent) override
static double ComputeBranchSectionArea(vtkPolyData *branchSection)
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void ComputeBranchSections(vtkPolyData *input, int groupId, vtkPolyData *output)
static void ExtractCylinderSection(vtkPolyData *cylinder, double origin[3], double normal[3], vtkPolyData *section, bool &closed, const char *idsArrayName=nullptr)
static double ComputeBranchSectionShape(vtkPolyData *branchSection, double center[3], double sizeRange[2])
static int CountBranchSectionCenterlines(vtkPolyData *section, vtkPolyData *centerlines, double origin[3], double normal[3])
static void ExtractGroup(vtkPolyData *surface, const char *groupIdsArrayName, vtkIdType groupId, bool cleanGroupSurface, vtkPolyData *groupSurface)
#define VTK_VMTK_LARGE_DOUBLE
vtkStandardNewMacro(vtkvmtkPolyDataBranchSections)