VMTK
Loading...
Searching...
No Matches
vtkvmtkPolyDataCenterlineSections.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkPolyDataCenterlineSections.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
24#include "vtkPolyData.h"
25#include "vtkPolyLine.h"
26#include "vtkPolygon.h"
27#include "vtkTriangle.h"
28#include "vtkPointData.h"
29#include "vtkCellData.h"
30#include "vtkCellArray.h"
31#include "vtkDoubleArray.h"
32#include "vtkIntArray.h"
33#include "vtkPlane.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 "vtkIdList.h"
46#include "vtkNew.h"
47#include "vtkVersionMacros.h"
48
49#include <algorithm>
50#include <vector>
51
54
55
57
58namespace
59{
60// Selects the cells of a polygonal surface that a plane crosses. Cutting only
61// these cells gives the same cut as cutting all cells, as the others contribute
62// nothing to it. Crossing cells are found from the signed distance of each
63// point to the plane, with a small tolerance, so that no cell that a cutter
64// would cut is left out.
65class CrossingCells
66{
67 public:
68 explicit CrossingCells(vtkPolyData* surface)
69 : Surface(surface),
70 Tolerance(1.0e-6 * surface->GetLength())
71 {
72 if (surface->GetNumberOfCells() == 0 || surface->GetNumberOfCells() != surface->GetNumberOfPolys())
73 {
74 return;
75 }
76 // Point coordinates and cell connectivity are copied once, as reading them
77 // through vtkPoints and vtkCellArray for every plane would take longer than
78 // the cut itself
79 this->Points.resize(3 * surface->GetNumberOfPoints());
80 for (vtkIdType k=0; k<surface->GetNumberOfPoints(); k++)
81 {
82 surface->GetPoint(k,&this->Points[3*k]);
83 }
84 vtkIdType numberOfCellPoints;
85 const vtkIdType* cellPoints;
86 vtkCellArray* polys = surface->GetPolys();
87 polys->InitTraversal();
88 this->CellOffsets.push_back(0);
89 while (polys->GetNextCell(numberOfCellPoints,cellPoints))
90 {
91 this->CellConnectivity.insert(this->CellConnectivity.end(),cellPoints,cellPoints + numberOfCellPoints);
92 this->CellOffsets.push_back(static_cast<vtkIdType>(this->CellConnectivity.size()));
93 }
94 this->SignedDistances.resize(surface->GetNumberOfPoints());
95 }
96
97 // Only surfaces that consist of polygons are supported
98 bool CanSelect() const
99 {
100 return !this->CellOffsets.empty();
101 }
102
103 // Set cells to the cells of the surface that the plane specified by origin and
104 // normal crosses, with the points and point data of the surface
105 void Select(const double origin[3], const double normal[3], vtkPolyData* cells)
106 {
107 for (size_t k=0; k<this->SignedDistances.size(); k++)
108 {
109 const double* point = &this->Points[3*k];
110 this->SignedDistances[k] = normal[0] * (point[0] - origin[0]) + normal[1] * (point[1] - origin[1]) + normal[2] * (point[2] - origin[2]);
111 }
112 vtkNew<vtkCellArray> crossingPolys;
113 for (size_t c=0; c+1<this->CellOffsets.size(); c++)
114 {
115 double minimumDistance = VTK_DOUBLE_MAX;
116 double maximumDistance = -VTK_DOUBLE_MAX;
117 for (vtkIdType k=this->CellOffsets[c]; k<this->CellOffsets[c+1]; k++)
118 {
119 minimumDistance = std::min(minimumDistance,this->SignedDistances[this->CellConnectivity[k]]);
120 maximumDistance = std::max(maximumDistance,this->SignedDistances[this->CellConnectivity[k]]);
121 }
122 if (minimumDistance <= this->Tolerance && maximumDistance >= -this->Tolerance)
123 {
124 crossingPolys->InsertNextCell(this->CellOffsets[c+1] - this->CellOffsets[c],&this->CellConnectivity[this->CellOffsets[c]]);
125 }
126 }
127 cells->Initialize();
128 cells->SetPoints(this->Surface->GetPoints());
129 cells->SetPolys(crossingPolys);
130 cells->GetPointData()->ShallowCopy(this->Surface->GetPointData());
131 }
132
133 private:
134 vtkPolyData* Surface;
135 const double Tolerance;
136 std::vector<double> Points;
137 std::vector<vtkIdType> CellOffsets;
138 std::vector<vtkIdType> CellConnectivity;
139 std::vector<double> SignedDistances;
140};
141}
142
153
155{
156 if (this->Centerlines)
157 {
158 this->Centerlines->Delete();
159 this->Centerlines = NULL;
160 }
161
163 {
164 delete[] this->CenterlineSectionAreaArrayName;
166 }
167
169 {
172 }
173
175 {
178 }
179
181 {
182 delete[] this->CenterlineSectionShapeArrayName;
184 }
185
187 {
190 }
191}
192
194 vtkInformation *vtkNotUsed(request),
195 vtkInformationVector **inputVector,
196 vtkInformationVector *outputVector)
197{
198 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
199 vtkInformation *outInfo = outputVector->GetInformationObject(0);
200
201 vtkPolyData *input = vtkPolyData::SafeDownCast(
202 inInfo->Get(vtkDataObject::DATA_OBJECT()));
203 vtkPolyData *output = vtkPolyData::SafeDownCast(
204 outInfo->Get(vtkDataObject::DATA_OBJECT()));
205
206 if (!this->Centerlines)
207 {
208 vtkErrorMacro(<<"Centerlines not set");
209 return 1;
210 }
211
213 {
214 vtkErrorMacro(<<"CenterlineSectionAreaArrayName not specified");
215 return 1;
216 }
217
219 {
220 vtkErrorMacro(<<"CenterlineSectionMinSizeArrayName not specified");
221 return 1;
222 }
223
225 {
226 vtkErrorMacro(<<"CenterlineSectionMaxSizeArrayName not specified");
227 return 1;
228 }
229
231 {
232 vtkErrorMacro(<<"CenterlineSectionShapeArrayName not specified");
233 return 1;
234 }
235
237 {
238 vtkErrorMacro(<<"CenterlineSectionClosedArrayName not specified");
239 return 1;
240 }
241
242 this->SetProgressText("Computing centerline sections");
243 this->UpdateProgress(0.0);
244 this->ComputeCenterlineSections(input,output);
245 this->UpdateProgress(1.0);
246
247 return 1;
248}
249
251{
252#if VTK_MAJOR_VERSION > 9 || (VTK_MAJOR_VERSION == 9 && VTK_MINOR_VERSION >= 4)
253 return this->CheckAbort();
254#else
255 return this->GetAbortExecute() != 0;
256#endif
257}
258
259void vtkvmtkPolyDataCenterlineSections::ComputeCenterlineSections(vtkPolyData* input, vtkPolyData* output)
260{
261 vtkNew<vtkPoints> outputPoints;
262 vtkNew<vtkCellArray> outputPolys;
263
264 output->SetPoints(outputPoints);
265 output->SetPolys(outputPolys);
266 vtkPoints* centerlineSectionPoints = outputPoints;
267 vtkCellArray* centerlineSectionPolys = outputPolys;
268
269 int numberOfCenterlinePoints = this->Centerlines->GetNumberOfPoints();
270
271 // Sections are stored both on the output, one tuple per section, and on
272 // the centerline points. Centerline points that get no section keep 0.
273 vtkNew<vtkDoubleArray> centerlineSectionAreaArray;
274 centerlineSectionAreaArray->SetName(this->CenterlineSectionAreaArrayName);
275 vtkNew<vtkDoubleArray> centerlineSectionMinSizeArray;
276 centerlineSectionMinSizeArray->SetName(this->CenterlineSectionMinSizeArrayName);
277 vtkNew<vtkDoubleArray> centerlineSectionMaxSizeArray;
278 centerlineSectionMaxSizeArray->SetName(this->CenterlineSectionMaxSizeArrayName);
279 vtkNew<vtkDoubleArray> centerlineSectionShapeArray;
280 centerlineSectionShapeArray->SetName(this->CenterlineSectionShapeArrayName);
281 vtkNew<vtkIntArray> centerlineSectionClosedArray;
282 centerlineSectionClosedArray->SetName(this->CenterlineSectionClosedArrayName);
283
284 output->GetCellData()->AddArray(centerlineSectionAreaArray);
285 output->GetCellData()->AddArray(centerlineSectionMinSizeArray);
286 output->GetCellData()->AddArray(centerlineSectionMaxSizeArray);
287 output->GetCellData()->AddArray(centerlineSectionShapeArray);
288 output->GetCellData()->AddArray(centerlineSectionClosedArray);
289
290 vtkNew<vtkDoubleArray> centerlineAreaArray;
291 centerlineAreaArray->SetName(this->CenterlineSectionAreaArrayName);
292 vtkNew<vtkDoubleArray> centerlineMinSizeArray;
293 centerlineMinSizeArray->SetName(this->CenterlineSectionMinSizeArrayName);
294 vtkNew<vtkDoubleArray> centerlineMaxSizeArray;
295 centerlineMaxSizeArray->SetName(this->CenterlineSectionMaxSizeArrayName);
296 vtkNew<vtkDoubleArray> centerlineShapeArray;
297 centerlineShapeArray->SetName(this->CenterlineSectionShapeArrayName);
298 vtkNew<vtkIntArray> centerlineClosedArray;
299 centerlineClosedArray->SetName(this->CenterlineSectionClosedArrayName);
300
301 vtkDataArray* centerlineArrays[] = {centerlineAreaArray, centerlineMinSizeArray, centerlineMaxSizeArray, centerlineShapeArray, centerlineClosedArray};
302 for (vtkDataArray* centerlineArray : centerlineArrays)
303 {
304 centerlineArray->SetNumberOfTuples(numberOfCenterlinePoints);
305 centerlineArray->Fill(0.0);
306 this->Centerlines->GetPointData()->AddArray(centerlineArray);
307 }
308
309 // Points are visited in the order of the centerline cells and of the points
310 // in each cell. A point shared by several cells gets a single section.
311 std::vector<bool> visited(this->Centerlines->GetNumberOfPoints(),false);
312 // Progress is reported, and an abort checked for, about every 1% of points
313 const vtkIdType progressInterval = std::max<vtkIdType>(numberOfCenterlinePoints / 100, 1);
314 vtkIdType numberOfVisitedPoints = 0;
315
316 // The cutter visits every cell of its input, which dominates the time spent
317 // on a section, so only the cells that the plane crosses are cut
318 CrossingCells crossingCells(input);
319 vtkNew<vtkPolyData> sectionInput;
320
321 vtkNew<vtkIdList> cellPointIds;
322 for (vtkIdType cellId=0; cellId<this->Centerlines->GetNumberOfCells(); cellId++)
323 {
324 this->Centerlines->GetCellPoints(cellId,cellPointIds);
325 for (vtkIdType i=0; i<cellPointIds->GetNumberOfIds(); i++)
326 {
327 vtkIdType pointId = cellPointIds->GetId(i);
328 if (visited[pointId])
329 {
330 continue;
331 }
332 visited[pointId] = true;
333
334 if (numberOfVisitedPoints % progressInterval == 0)
335 {
336 this->UpdateProgress(static_cast<double>(numberOfVisitedPoints) / numberOfCenterlinePoints);
337 if (this->AbortRequested())
338 {
339 return;
340 }
341 }
342 numberOfVisitedPoints++;
343
344 double origin[3], normal[3];
345 if (!this->ComputeSectionPlane(cellId,i,origin,normal))
346 {
347 continue;
348 }
349
350 vtkNew<vtkPolyData> section;
351 bool closed = false;
352 if (crossingCells.CanSelect())
353 {
354 crossingCells.Select(origin,normal,sectionInput);
355 this->ExtractSection(sectionInput,origin,normal,section,closed);
356 }
357 else
358 {
359 this->ExtractSection(input,origin,normal,section,closed);
360 }
361
362 section->BuildCells();
363 if (section->GetNumberOfCells() == 0)
364 {
365 // The plane does not cut the surface, for example past an open end
366 continue;
367 }
368
369 vtkPoints* sectionCellPoints = section->GetCell(0)->GetPoints();
370 int numberOfSectionCellPoints = sectionCellPoints->GetNumberOfPoints();
371 vtkIdType sectionId = centerlineSectionPolys->InsertNextCell(numberOfSectionCellPoints);
372 for (int k=0; k<numberOfSectionCellPoints; k++)
373 {
374 vtkIdType sectionPointId = centerlineSectionPoints->InsertNextPoint(sectionCellPoints->GetPoint(k));
375 centerlineSectionPolys->InsertCellPoint(sectionPointId);
376 }
377
379 double sizeRange[2];
380 double shape = vtkvmtkPolyDataBranchSections::ComputeBranchSectionShape(section,origin,sizeRange);
381
382 centerlineSectionAreaArray->InsertNextValue(area);
383 centerlineSectionMinSizeArray->InsertNextValue(sizeRange[0]);
384 centerlineSectionMaxSizeArray->InsertNextValue(sizeRange[1]);
385 centerlineSectionShapeArray->InsertNextValue(shape);
386 centerlineSectionClosedArray->InsertNextValue(closed);
387
388 centerlineAreaArray->SetValue(pointId,area);
389 centerlineMinSizeArray->SetValue(pointId,sizeRange[0]);
390 centerlineMaxSizeArray->SetValue(pointId,sizeRange[1]);
391 centerlineShapeArray->SetValue(pointId,shape);
392 centerlineClosedArray->SetValue(pointId,closed);
393
394 this->ProcessSection(input,pointId,sectionId,section,origin,normal,closed);
395 }
396 }
397}
398
399bool vtkvmtkPolyDataCenterlineSections::ComputeSectionPlane(vtkIdType cellId, vtkIdType cellPointIndex, double origin[3], double normal[3])
400{
401 vtkNew<vtkIdList> cellPointIds;
402 this->Centerlines->GetCellPoints(cellId,cellPointIds);
403 const vtkIdType numberOfCellPoints = cellPointIds->GetNumberOfIds();
404
405 this->Centerlines->GetPoint(cellPointIds->GetId(cellPointIndex),origin);
406
407 // Average the directions from the previous and to the next point. Points
408 // that coincide with the section point, such as a duplicated end point, have
409 // no direction, so the nearest distinct point on each side is used instead.
410 normal[0] = normal[1] = normal[2] = 0.0;
411 double neighborPoint[3], direction[3];
412 for (vtkIdType step : {-1, 1})
413 {
414 for (vtkIdType j=cellPointIndex+step; j>=0 && j<numberOfCellPoints; j+=step)
415 {
416 this->Centerlines->GetPoint(cellPointIds->GetId(j),neighborPoint);
417 vtkMath::Subtract(neighborPoint,origin,direction);
418 double distance = vtkMath::Normalize(direction);
419 if (distance > 0.0)
420 {
421 normal[0] += step * direction[0];
422 normal[1] += step * direction[1];
423 normal[2] += step * direction[2];
424 break;
425 }
426 }
427 }
428
429 return vtkMath::Normalize(normal) > 0.0;
430}
431
432void vtkvmtkPolyDataCenterlineSections::ExtractSection(vtkPolyData* input, double origin[3], double normal[3], vtkPolyData* section, bool & closed)
433{
434 vtkvmtkPolyDataBranchSections::ExtractCylinderSection(input,origin,normal,section,closed);
435}
436
437void vtkvmtkPolyDataCenterlineSections::ProcessSection(vtkPolyData* vtkNotUsed(input), vtkIdType vtkNotUsed(pointId), vtkIdType vtkNotUsed(sectionId), vtkPolyData* vtkNotUsed(section), double* vtkNotUsed(origin), double* vtkNotUsed(normal), bool vtkNotUsed(closed))
438{
439}
440
441void vtkvmtkPolyDataCenterlineSections::PrintSelf(std::ostream& os, vtkIndent indent)
442{
443 this->Superclass::PrintSelf(os,indent);
444}
static double ComputeBranchSectionArea(vtkPolyData *branchSection)
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])
Extract a set of vessel sections from a surface along a centerline.
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
virtual void ExtractSection(vtkPolyData *input, double origin[3], double normal[3], vtkPolyData *section, bool &closed)
void PrintSelf(std::ostream &os, vtkIndent indent) override
void ComputeCenterlineSections(vtkPolyData *input, vtkPolyData *output)
virtual void ProcessSection(vtkPolyData *input, vtkIdType pointId, vtkIdType sectionId, vtkPolyData *section, double origin[3], double normal[3], bool closed)
virtual bool ComputeSectionPlane(vtkIdType cellId, vtkIdType cellPointIndex, double origin[3], double normal[3])
vtkStandardNewMacro(vtkvmtkPolyDataCenterlineSections)