VMTK
Loading...
Searching...
No Matches
vtkvmtkBoundaryLabels.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=========================================================================*/
17
20
21#include "vtkCellArray.h"
22#include "vtkDataArray.h"
23#include "vtkIdList.h"
24#include "vtkIdTypeArray.h"
25#include "vtkMath.h"
26#include "vtkNew.h"
27#include "vtkObjectFactory.h"
28#include "vtkPointData.h"
29#include "vtkPoints.h"
30#include "vtkPolyData.h"
31
32#include <algorithm>
33#include <map>
34#include <vector>
35
37
38namespace
39{
40
41// The two arrays are written as vtkIdTypeArray, but a filter that knows nothing about them may
42// have passed them through as something else, so read them as plain numbers and round, the way
43// the boundary point ids of the extractor's own output are read everywhere in these filters.
44vtkDataArray* GetSingleComponentPointArray(vtkPolyData* surface, const char* arrayName)
45{
46 if (!surface || !arrayName || !arrayName[0])
47 {
48 return nullptr;
49 }
50 vtkDataArray* array = surface->GetPointData()->GetArray(arrayName);
51 if (!array || array->GetNumberOfComponents() != 1
52 || array->GetNumberOfTuples() != surface->GetNumberOfPoints())
53 {
54 return nullptr;
55 }
56 return array;
57}
58
59vtkIdType GetValue(vtkDataArray* array, vtkIdType pointId)
60{
61 return static_cast<vtkIdType>(vtkMath::Round(array->GetComponent(pointId,0)));
62}
63
64}
65
66bool vtkvmtkBoundaryLabels::HasBoundaryLabels(vtkPolyData* surface, const char* boundaryLabelsArrayName, const char* boundaryPointOrderArrayName)
67{
68 return GetSingleComponentPointArray(surface,boundaryLabelsArrayName) != nullptr
69 && GetSingleComponentPointArray(surface,boundaryPointOrderArrayName) != nullptr;
70}
71
72bool vtkvmtkBoundaryLabels::GetBoundaries(vtkPolyData* surface, const char* boundaryLabelsArrayName, const char* boundaryPointOrderArrayName, vtkPolyData* boundaries, vtkIdList* boundaryLabels)
73{
74 if (!boundaries)
75 {
76 return false;
77 }
78
79 boundaries->Initialize();
80 if (boundaryLabels)
81 {
82 boundaryLabels->Initialize();
83 }
84
85 vtkDataArray* labelsArray = GetSingleComponentPointArray(surface,boundaryLabelsArrayName);
86 vtkDataArray* orderArray = GetSingleComponentPointArray(surface,boundaryPointOrderArrayName);
87 if (!labelsArray || !orderArray)
88 {
89 return false;
90 }
91
92 // Gather the points of each boundary, keyed by label so that the boundaries come out in
93 // ascending label order however the points happen to be numbered.
94 std::map<vtkIdType,std::vector<std::pair<vtkIdType,vtkIdType> > > pointsByLabel;
95 vtkIdType numberOfPoints = surface->GetNumberOfPoints();
96 for (vtkIdType pointId=0; pointId<numberOfPoints; pointId++)
97 {
98 vtkIdType label = GetValue(labelsArray,pointId);
99 if (label < 0)
100 {
101 continue;
102 }
103 vtkIdType order = GetValue(orderArray,pointId);
104 if (order < 0)
105 {
106 // On a boundary by one array and not by the other: the two no longer agree, so neither
107 // can be trusted.
108 return false;
109 }
110 pointsByLabel[label].push_back(std::make_pair(order,pointId));
111 }
112
113 if (pointsByLabel.empty())
114 {
115 // A closed surface has no boundaries, and saying so is a valid answer rather than a
116 // failure. The arrays are still there, so the caller has nothing to fall back to.
117 vtkNew<vtkPoints> emptyPoints;
118 vtkNew<vtkCellArray> emptyLines;
119 vtkNew<vtkIdTypeArray> emptyScalars;
120 boundaries->SetPoints(emptyPoints);
121 boundaries->SetLines(emptyLines);
122 boundaries->GetPointData()->SetScalars(emptyScalars);
123 return true;
124 }
125
126 // Cell links, to tell an edge that still has only one cell behind it from one that has been
127 // closed since. Built on a copy of the structure rather than on surface itself: BuildLinks on
128 // the caller's own polydata leaves it holding a reference it did not ask for.
129 vtkNew<vtkPolyData> working;
130 working->CopyStructure(surface);
131 working->BuildLinks();
132 vtkNew<vtkIdList> edgeNeighbors;
133
134 std::vector<vtkIdType> closedLabels;
135 for (std::map<vtkIdType,std::vector<std::pair<vtkIdType,vtkIdType> > >::iterator
136 boundary=pointsByLabel.begin(); boundary!=pointsByLabel.end(); ++boundary)
137 {
138 std::vector<std::pair<vtkIdType,vtkIdType> >& ring = boundary->second;
139 // A ring of fewer than three points is not one the extractor would have emitted
140 // (see vtkvmtkPolyDataBoundaryExtractor, which requires more than two).
141 if (ring.size() < 3)
142 {
143 return false;
144 }
145 std::sort(ring.begin(),ring.end());
146 // The order values have to be the whole run 0..n-1. A gap, a repeat, or a value off the end
147 // means the ring this describes is not the ring that is there now.
148 for (size_t index=0; index<ring.size(); index++)
149 {
150 if (ring[index].first != static_cast<vtkIdType>(index))
151 {
152 return false;
153 }
154 }
155 // A ring the arrays describe is not necessarily still an open boundary. The labels are left
156 // on the points of a boundary that has been capped, on purpose, as a record of which vessel
157 // end that ring was -- so a surface that has been through a capper carries rings with cells
158 // on both sides of them. Handing one of those back as a boundary has it capped a second
159 // time, over the cap already there, which is not an error anywhere it would be noticed: the
160 // surface comes back closed, of very nearly the right size, and non-manifold along every rim.
161 // What the extractor would return is the test, and it returns open boundaries only.
162 size_t numberOfRingPoints = ring.size();
163 bool stillOpen = true;
164 for (size_t index=0; index<numberOfRingPoints && stillOpen; index++)
165 {
166 working->GetCellEdgeNeighbors(-1,ring[index].second,
167 ring[(index+1)%numberOfRingPoints].second,edgeNeighbors);
168 // One cell behind the edge and it is on the boundary of the surface; two and it is inside
169 // it. Zero means the two points are no longer joined by an edge at all, which is no more
170 // an open boundary than the others.
171 stillOpen = (edgeNeighbors->GetNumberOfIds() == 1);
172 }
173 if (!stillOpen)
174 {
175 closedLabels.push_back(boundary->first);
176 }
177 }
178 for (size_t index=0; index<closedLabels.size(); index++)
179 {
180 pointsByLabel.erase(closedLabels[index]);
181 }
182
183 // Each point of a boundary is listed once, the way vtkvmtkPolyDataBoundaryExtractor lists
184 // them, so that a boundary reconstructed from the arrays is the boundary the extractor would
185 // have produced and the two ways of getting the boundaries of a surface are interchangeable.
186 vtkNew<vtkPoints> newPoints;
187 vtkNew<vtkCellArray> newLines;
188 vtkNew<vtkIdTypeArray> newScalars;
189 vtkNew<vtkIdList> newCell;
190
191 for (std::map<vtkIdType,std::vector<std::pair<vtkIdType,vtkIdType> > >::iterator
192 boundary=pointsByLabel.begin(); boundary!=pointsByLabel.end(); ++boundary)
193 {
194 const std::vector<std::pair<vtkIdType,vtkIdType> >& ring = boundary->second;
195
196 newCell->Initialize();
197 newCell->SetNumberOfIds(static_cast<vtkIdType>(ring.size()));
198 for (size_t index=0; index<ring.size(); index++)
199 {
200 vtkIdType pointId = ring[index].second;
201 newCell->SetId(static_cast<vtkIdType>(index),newPoints->InsertNextPoint(surface->GetPoint(pointId)));
202 newScalars->InsertNextValue(pointId);
203 }
204
205 newLines->InsertNextCell(newCell);
206
207 if (boundaryLabels)
208 {
209 boundaryLabels->InsertNextId(boundary->first);
210 }
211 }
212
213 boundaries->SetPoints(newPoints);
214 boundaries->SetLines(newLines);
215 boundaries->GetPointData()->SetScalars(newScalars);
216
217 return true;
218}
219
220bool vtkvmtkBoundaryLabels::GetOrExtractBoundaries(vtkPolyData* surface, const char* boundaryLabelsArrayName, const char* boundaryPointOrderArrayName, vtkPolyData* boundaries, vtkIdList* boundaryLabels, vtkObject* warningSource)
221{
222 if (!surface || !boundaries)
223 {
224 return false;
225 }
226
227 if (boundaryLabels)
228 {
229 boundaryLabels->Reset();
230 }
231
232 if (boundaryLabelsArrayName && boundaryLabelsArrayName[0]
233 && boundaryPointOrderArrayName && boundaryPointOrderArrayName[0])
234 {
235 if (vtkvmtkBoundaryLabels::GetBoundaries(surface,boundaryLabelsArrayName,boundaryPointOrderArrayName,boundaries,boundaryLabels))
236 {
237 return true;
238 }
239 if (warningSource)
240 {
241 vtkWarningWithObjectMacro(warningSource,<<"The boundary label arrays are missing from the input surface or no longer describe it; "
242 <<"extracting its boundaries instead, and naming the caps by position.");
243 }
244 }
245
246 vtkNew<vtkvmtkPolyDataBoundaryExtractor> boundaryExtractor;
247 boundaryExtractor->SetInputData(surface);
248 boundaryExtractor->Update();
249 boundaries->ShallowCopy(boundaryExtractor->GetOutput());
250
251 return false;
252}
253
254void vtkvmtkBoundaryLabels::PrintSelf(std::ostream& os, vtkIndent indent)
255{
256 this->Superclass::PrintSelf(os,indent);
257}
Boundaries of a surface, stored in the surface's own point data.
static bool GetBoundaries(vtkPolyData *surface, const char *boundaryLabelsArrayName, const char *boundaryPointOrderArrayName, vtkPolyData *boundaries, vtkIdList *boundaryLabels)
static bool HasBoundaryLabels(vtkPolyData *surface, const char *boundaryLabelsArrayName, const char *boundaryPointOrderArrayName)
static bool GetOrExtractBoundaries(vtkPolyData *surface, const char *boundaryLabelsArrayName, const char *boundaryPointOrderArrayName, vtkPolyData *boundaries, vtkIdList *boundaryLabels, vtkObject *warningSource=nullptr)
void PrintSelf(std::ostream &os, vtkIndent indent) override
vtkStandardNewMacro(vtkvmtkBoundaryLabels)