VMTK
Loading...
Searching...
No Matches
vtkvmtkCapPolyData.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkCapPolyData.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:46:43 $
7Version: $Revision: 1.5 $
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
22#include "vtkvmtkCapPolyData.h"
26#include "vtkvmtkConstants.h"
27#include "vtkCellArray.h"
28#include "vtkPointData.h"
29#include "vtkCellData.h"
30#include "vtkMath.h"
31#include "vtkPolyLine.h"
32#include "vtkLine.h"
33#include "vtkIdTypeArray.h"
34#include "vtkIntArray.h"
35#include "vtkInformation.h"
36#include "vtkInformationVector.h"
37#include "vtkNew.h"
38#include "vtkObjectFactory.h"
39#include "vtkVersion.h"
40
41
43
45{
46 this->BoundaryIds = NULL;
47 this->Displacement = 1E-1;
48 this->InPlaneDisplacement = 1E-1;
49 this->CapCenterIds = NULL;
50 this->CellEntityIdsArrayName = NULL;
51 this->CellEntityIdOffset = 1;
52 this->BoundaryLabelsArrayName = NULL;
53 this->BoundaryPointOrderArrayName = NULL;
54 this->BoundaryCellEntityIds = NULL;
55}
56
58{
59 if (this->BoundaryIds)
60 {
61 this->BoundaryIds->Delete();
62 this->BoundaryIds = NULL;
63 }
64 if (this->CapCenterIds)
65 {
66 this->CapCenterIds->Delete();
67 this->CapCenterIds = NULL;
68 }
69 if (this->CellEntityIdsArrayName)
70 {
71 delete[] this->CellEntityIdsArrayName;
72 this->CellEntityIdsArrayName = NULL;
73 }
76 if (this->BoundaryCellEntityIds)
77 {
78 this->BoundaryCellEntityIds->Delete();
79 this->BoundaryCellEntityIds = NULL;
80 }
81}
82
84 vtkInformation *vtkNotUsed(request),
85 vtkInformationVector **inputVector,
86 vtkInformationVector *outputVector)
87{
88 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
89 vtkInformation *outInfo = outputVector->GetInformationObject(0);
90
91 vtkPolyData *input = vtkPolyData::SafeDownCast(
92 inInfo->Get(vtkDataObject::DATA_OBJECT()));
93 vtkPolyData *output = vtkPolyData::SafeDownCast(
94 outInfo->Get(vtkDataObject::DATA_OBJECT()));
95
96 // Declare
97 vtkIdType barycenterId, trianglePoints[3];
98 vtkIdType i, j;
99 vtkvmtkPolyDataBoundaryExtractor* boundaryExtractor;
100 vtkPolyData* boundaries;
101 vtkPoints* newPoints;
102 vtkCellArray* newPolys;
103 vtkPolyLine* boundary;
104
105 // Initialize
106 if ( ((input->GetNumberOfPoints()) < 1) )
107 {
108 //vtkErrorMacro(<< "No input!");
109 return 1;
110 }
111 input->BuildLinks();
112
113 // Allocate
114 newPoints = vtkPoints::New();
115 newPoints->DeepCopy(input->GetPoints());
116 newPolys = vtkCellArray::New();
117 newPolys->DeepCopy(input->GetPolys());
118 boundaryExtractor = vtkvmtkPolyDataBoundaryExtractor::New();
119
120 // Copy cell entity ids array
121 vtkIdTypeArray* cellEntityIdsArray = NULL;
122 bool markCells = this->CellEntityIdsArrayName && this->CellEntityIdsArrayName[0];
123 if (markCells)
124 {
125 cellEntityIdsArray = vtkIdTypeArray::New();
126 cellEntityIdsArray->SetName(this->CellEntityIdsArrayName);
127 if (input->GetCellData()->GetArray(this->CellEntityIdsArrayName))
128 {
129 cellEntityIdsArray->DeepCopy(input->GetCellData()->GetArray(this->CellEntityIdsArrayName));
130 }
131 else
132 {
133 cellEntityIdsArray->SetNumberOfTuples(newPolys->GetNumberOfCells());
134 cellEntityIdsArray->FillComponent(0,static_cast<double>(this->CellEntityIdOffset));
135 }
136 }
137
138 // Execute
139 // The boundaries either come from the labels the input already carries, which are the same
140 // boundaries in the same order every other filter reading those labels sees, or they are
141 // extracted here as they always were.
142 vtkNew<vtkPolyData> labeledBoundaries;
143 vtkNew<vtkIdList> boundaryLabels;
144 boundaries = NULL;
145 bool useBoundaryLabels = false;
148 {
149 useBoundaryLabels = vtkvmtkBoundaryLabels::GetBoundaries(
151 labeledBoundaries,boundaryLabels);
152 if (useBoundaryLabels)
153 {
154 boundaries = labeledBoundaries;
155 }
156 else
157 {
158 vtkWarningMacro(<<"The boundary label arrays are missing from the input surface or no longer describe it; "
159 <<"extracting its boundaries instead, and naming the caps by position.");
160 }
161 }
162 if (!boundaries)
163 {
164 boundaryExtractor->SetInputData(input);
165 boundaryExtractor->Update();
166 boundaries = boundaryExtractor->GetOutput();
167 }
168
169 if (this->BoundaryCellEntityIds)
170 {
171 // Silence here is how a caller ends up with caps carrying ids it did not choose and no way
172 // of telling. Say which boundaries were left out instead.
173 for (vtkIdType boundaryIndex=0; boundaryIndex<boundaries->GetNumberOfCells(); boundaryIndex++)
174 {
175 vtkIdType boundaryId = useBoundaryLabels ? boundaryLabels->GetId(boundaryIndex) : boundaryIndex;
176 if (boundaryId >= this->BoundaryCellEntityIds->GetNumberOfTuples())
177 {
178 vtkWarningMacro(<<"BoundaryCellEntityIds has no entry for the boundary with id "<<boundaryId
179 <<"; its cap takes the id its position gives it.");
180 }
181 }
182 }
183
184 if (this->CapCenterIds)
185 {
186 this->CapCenterIds->Delete();
187 this->CapCenterIds = NULL;
188 }
189
190 this->CapCenterIds = vtkIdList::New();
191 this->CapCenterIds->SetNumberOfIds(boundaries->GetNumberOfCells());
192 for (i=0; i<this->CapCenterIds->GetNumberOfIds(); i++)
193 {
194 this->CapCenterIds->SetId(i,-1);
195 }
196
197 double barycenter[3], normal[3], outwardNormal[3], meanRadius;
198
199 for (i=0; i<boundaries->GetNumberOfCells(); i++)
200 {
201 if (this->BoundaryIds)
202 {
203 // A boundary is named by its label when the labels are in use, and by its position in the
204 // extraction order otherwise, here as everywhere else in this filter.
205 if (this->BoundaryIds->IsId(useBoundaryLabels ? boundaryLabels->GetId(i) : i) == -1)
206 {
207 continue;
208 }
209 }
210 boundary = vtkPolyLine::SafeDownCast(boundaries->GetCell(i));
211
212 vtkvmtkBoundaryReferenceSystems::ComputeBoundaryBarycenter(boundary->GetPoints(),barycenter);
213 vtkvmtkBoundaryReferenceSystems::ComputeBoundaryNormal(boundary->GetPoints(),barycenter,normal);
214 vtkvmtkBoundaryReferenceSystems::OrientBoundaryNormalOutwards(input,boundaries,i,normal,outwardNormal);
215 meanRadius = vtkvmtkBoundaryReferenceSystems::ComputeBoundaryMeanRadius(boundary->GetPoints(),barycenter);
216
217 for (j=0; j<3; j++)
218 {
219 barycenter[j] += meanRadius * this->Displacement * outwardNormal[j];
220 }
221
222 double inplane1[3], inplane2[3];
223 vtkMath::Perpendiculars(outwardNormal,inplane1,inplane2,0.0);
224 for (j=0; j<3; j++)
225 {
226 barycenter[j] += meanRadius * this->InPlaneDisplacement * inplane1[j];
227 }
228
229 barycenterId = newPoints->InsertNextPoint(barycenter);
230 this->CapCenterIds->SetId(i,barycenterId);
231
232 vtkIdType numberOfBoundaryPoints = boundary->GetNumberOfPoints();
233 for (j=0; j<numberOfBoundaryPoints; j++)
234 {
235 trianglePoints[0] = static_cast<vtkIdType>(boundaries->GetPointData()->GetScalars()->GetTuple1(boundary->GetPointId(j)));
236 trianglePoints[1] = barycenterId;
237 trianglePoints[2] = static_cast<vtkIdType>(boundaries->GetPointData()->GetScalars()->GetTuple1(boundary->GetPointId((j+1)%numberOfBoundaryPoints)));
238
239 newPolys->InsertNextCell(3,trianglePoints);
240
241 if (markCells)
242 {
243 // An id the caller chose for this boundary is used as it stands; CellEntityIdOffset is
244 // what moves the ids this filter derives itself out of the way of the input's, and has
245 // no business shifting one that was picked deliberately.
246 // With the labels in use the boundary's label is its name, so it is also the cap's id
247 // unless the caller chose another: a label is as deliberate as an entry in
248 // BoundaryCellEntityIds, and neither is shifted by CellEntityIdOffset. Without them
249 // there is nothing to go on but the boundary's position.
250 vtkIdType boundaryId = useBoundaryLabels ? boundaryLabels->GetId(i) : i;
251 vtkIdType capCellEntityId = useBoundaryLabels ? boundaryId : i+1+this->CellEntityIdOffset;
252 if (this->BoundaryCellEntityIds
253 && boundaryId < this->BoundaryCellEntityIds->GetNumberOfTuples()
254 && this->BoundaryCellEntityIds->GetValue(boundaryId) >= 0)
255 {
256 capCellEntityId = this->BoundaryCellEntityIds->GetValue(boundaryId);
257 }
258 cellEntityIdsArray->InsertNextValue(capCellEntityId);
259 }
260
261 }
262 }
263
264 output->SetPoints(newPoints);
265 output->SetPolys(newPolys);
266
267 if (markCells)
268 {
269 output->GetCellData()->AddArray(cellEntityIdsArray);
270 cellEntityIdsArray->Delete();
271 }
272
273 if (useBoundaryLabels)
274 {
275 // The input's points keep the ids they had - they are deep copied first and the cap centers
276 // only appended - so the labels copy across as they stand. They describe no open boundary
277 // any more, every one of them having just been closed, but they are left in place as a
278 // record of which vessel end each ring of points was.
279 const vtkIdType invalidLabel = vtkvmtkBoundaryLabels::GetInvalidBoundaryLabel();
280 vtkIdType numberOfInputPoints = input->GetNumberOfPoints();
281 vtkIdType numberOfOutputPoints = newPoints->GetNumberOfPoints();
282
283 vtkDataArray* inputLabels = input->GetPointData()->GetArray(this->BoundaryLabelsArrayName);
284 vtkDataArray* inputOrder = input->GetPointData()->GetArray(this->BoundaryPointOrderArrayName);
285
286 vtkNew<vtkIntArray> outputLabels;
287 outputLabels->SetName(this->BoundaryLabelsArrayName);
288 outputLabels->SetNumberOfTuples(numberOfOutputPoints);
289 outputLabels->FillComponent(0,static_cast<double>(invalidLabel));
290
291 vtkNew<vtkIntArray> outputOrder;
292 outputOrder->SetName(this->BoundaryPointOrderArrayName);
293 outputOrder->SetNumberOfTuples(numberOfOutputPoints);
294 outputOrder->FillComponent(0,static_cast<double>(invalidLabel));
295
296 for (vtkIdType pointId=0; pointId<numberOfInputPoints; pointId++)
297 {
298 outputLabels->SetValue(pointId,static_cast<int>(vtkMath::Round(inputLabels->GetComponent(pointId,0))));
299 outputOrder->SetValue(pointId,static_cast<int>(vtkMath::Round(inputOrder->GetComponent(pointId,0))));
300 }
301
302 output->GetPointData()->AddArray(outputLabels);
303 output->GetPointData()->AddArray(outputOrder);
304 }
305
306 // TODO: the filter throws all the point and cell data
307 //output->GetPointData()->PassData(input->GetPointData()); // Like this?
308
309 // Destroy
310 newPoints->Delete();
311 newPolys->Delete();
312 boundaryExtractor->Delete();
313
314 return 1;
315}
316
317void vtkvmtkCapPolyData::PrintSelf(std::ostream& os, vtkIndent indent)
318{
319 this->Superclass::PrintSelf(os,indent);
320}
static bool GetBoundaries(vtkPolyData *surface, const char *boundaryLabelsArrayName, const char *boundaryPointOrderArrayName, vtkPolyData *boundaries, vtkIdList *boundaryLabels)
static vtkIdType GetInvalidBoundaryLabel()
static void ComputeBoundaryBarycenter(vtkPoints *points, double barycenter[3])
static void OrientBoundaryNormalOutwards(vtkPolyData *surface, vtkPolyData *boundaries, vtkIdType boundaryCellId, double normal[3], double outwardNormal[3])
static void ComputeBoundaryNormal(vtkPoints *points, double barycenter[3], double normal[3])
static double ComputeBoundaryMeanRadius(vtkPoints *points, double barycenter[3])
Close holes in a surface by creating a cap made of triangles sharing a common vertex at the boundary ...
virtual void SetBoundaryLabelsArrayName(const char *)
vtkIdTypeArray * BoundaryCellEntityIds
void PrintSelf(std::ostream &os, vtkIndent indent) override
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
virtual void SetBoundaryPointOrderArrayName(const char *)
Extract boundary edges as poly lines.
static vtkvmtkPolyDataBoundaryExtractor * New()
vtkStandardNewMacro(vtkvmtkCapPolyData)