VMTK
Loading...
Searching...
No Matches
vtkvmtkSmoothCapPolyData.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkSmoothCapPolyData.cxx,v $
5Language: C++
6Date: $Date: 2006/07/17 09:53:14 $
7Version: $Revision: 1.6 $
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 "vtkNew.h"
25#include "vtkPolyData.h"
26#include "vtkCellArray.h"
27#include "vtkPointData.h"
28#include "vtkCellData.h"
29#include "vtkPolyLine.h"
30#include "vtkInformation.h"
31#include "vtkInformationVector.h"
32#include "vtkObjectFactory.h"
33#include "vtkThinPlateSplineTransform.h"
34#include "vtkPoints.h"
35#include "vtkIdList.h"
36#include "vtkIdTypeArray.h"
37#include "vtkMath.h"
38#include "vtkTriangle.h"
39#include "vtkVersion.h"
40
41
43
45{
46 this->BoundaryIds = NULL;
47 this->ConstraintFactor = 1.0;
48 this->NumberOfRings = 8;
49 this->CellEntityIdsArrayName = NULL;
50 this->CellEntityIdOffset = 1;
51 this->BoundaryLabelsArrayName = NULL;
52 this->BoundaryPointOrderArrayName = NULL;
53 this->BoundaryCellEntityIds = NULL;
54}
55
57{
58 if (this->BoundaryIds)
59 {
60 this->BoundaryIds->Delete();
61 this->BoundaryIds = NULL;
62 }
63
66
67 if (this->BoundaryCellEntityIds)
68 {
69 this->BoundaryCellEntityIds->Delete();
70 this->BoundaryCellEntityIds = NULL;
71 }
72 if (this->CellEntityIdsArrayName)
73 {
74 delete[] this->CellEntityIdsArrayName;
75 this->CellEntityIdsArrayName = NULL;
76 }
77}
78
80 vtkInformation *vtkNotUsed(request),
81 vtkInformationVector **inputVector,
82 vtkInformationVector *outputVector)
83{
84 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
85 vtkInformation *outInfo = outputVector->GetInformationObject(0);
86
87 vtkPolyData *input = vtkPolyData::SafeDownCast(
88 inInfo->Get(vtkDataObject::DATA_OBJECT()));
89 vtkPolyData *output = vtkPolyData::SafeDownCast(
90 outInfo->Get(vtkDataObject::DATA_OBJECT()));
91
92 if ( input->GetNumberOfPoints() < 1 )
93 {
94 return 1;
95 }
96
97 input->BuildCells();
98 input->BuildLinks();
99
100 vtkPoints* newPoints = vtkPoints::New();
101 newPoints->DeepCopy(input->GetPoints());
102
103 vtkCellArray* newPolys = vtkCellArray::New();
104 newPolys->DeepCopy(input->GetPolys());
105
106 // Copy cell entity ids array
107 vtkIdTypeArray* cellEntityIdsArray = NULL;
108 bool markCells = this->CellEntityIdsArrayName && this->CellEntityIdsArrayName[0];
109 if (markCells)
110 {
111 cellEntityIdsArray = vtkIdTypeArray::New();
112 cellEntityIdsArray->SetName(this->CellEntityIdsArrayName);
113 if (input->GetCellData()->GetArray(this->CellEntityIdsArrayName))
114 {
115 cellEntityIdsArray->DeepCopy(input->GetCellData()->GetArray(this->CellEntityIdsArrayName));
116 }
117 else
118 {
119 cellEntityIdsArray->SetNumberOfTuples(newPolys->GetNumberOfCells());
120 cellEntityIdsArray->FillComponent(0,static_cast<double>(this->CellEntityIdOffset));
121 }
122 }
123
124 // The boundaries either come from the labels the input already carries, which are the same
125 // boundaries in the same order every other filter reading those labels sees, or they are
126 // extracted here as they always were.
127 vtkNew<vtkPolyData> boundaries;
128 vtkNew<vtkIdList> boundaryLabels;
129 bool useBoundaryLabels = vtkvmtkBoundaryLabels::GetOrExtractBoundaries(
130 input,this->BoundaryLabelsArrayName,this->BoundaryPointOrderArrayName,boundaries,boundaryLabels,this);
131
132 int boundaryId;
133 for (boundaryId=0; boundaryId<boundaries->GetNumberOfCells(); boundaryId++)
134 {
135 // boundaryId is the boundary's position here; capBoundaryId is what names it, which is its
136 // label when the labels are in use and its position otherwise.
137 vtkIdType capBoundaryId = useBoundaryLabels ? boundaryLabels->GetId(boundaryId) : boundaryId;
138 if (this->BoundaryIds)
139 {
140 if (this->BoundaryIds->IsId(capBoundaryId) == -1)
141 {
142 continue;
143 }
144 }
145
146 vtkPolyLine* boundaryCell = vtkPolyLine::SafeDownCast(boundaries->GetCell(boundaryId));
147
148 int numberOfBoundaryPoints = boundaryCell->GetNumberOfPoints();
149 double boundaryDiagonal = sqrt(boundaryCell->GetLength2());
150
151 vtkIdList* boundaryPointIds = vtkIdList::New();
152 int i;
153 for (i=0; i<numberOfBoundaryPoints; i++)
154 {
155 vtkIdType boundaryPointId = static_cast<vtkIdType>(boundaries->GetPointData()->GetScalars()->GetComponent(boundaryCell->GetPointId(i),0));
156 boundaryPointIds->InsertNextId(boundaryPointId);
157 }
158
159 vtkPoints* outerRingPoints = vtkPoints::New();
160 vtkIdList* pointCells = vtkIdList::New();
161 vtkIdList* cellPoints = vtkIdList::New();
162 for (i=0; i<numberOfBoundaryPoints; i++)
163 {
164 vtkIdType boundaryPointId = boundaryPointIds->GetId(i);
165 pointCells->Initialize();
166 input->GetPointCells(boundaryPointId,pointCells);
167 double outerRingPoint[3];
168 outerRingPoint[0] = outerRingPoint[1] = outerRingPoint[2] = 0.0;
169 double areaSum = 0.0;
170 int j;
171 for (j=0; j<pointCells->GetNumberOfIds(); j++)
172 {
173 vtkIdType cellId = pointCells->GetId(j);
174 input->GetCellPoints(cellId,cellPoints);
175 //TODO: assert cell is vtkTriangle
176 double point0[3], point1[3], point2[3];
177 input->GetPoint(cellPoints->GetId(0),point0);
178 input->GetPoint(cellPoints->GetId(1),point1);
179 input->GetPoint(cellPoints->GetId(2),point2);
180 double triangleCenter[3];
181 vtkTriangle::TriangleCenter(point0,point1,point2,triangleCenter);
182 double triangleArea = vtkTriangle::TriangleArea(point0,point1,point2);
183 outerRingPoint[0] += triangleArea * triangleCenter[0];
184 outerRingPoint[1] += triangleArea * triangleCenter[1];
185 outerRingPoint[2] += triangleArea * triangleCenter[2];
186 areaSum += triangleArea;
187 }
188 outerRingPoint[0] /= areaSum;
189 outerRingPoint[1] /= areaSum;
190 outerRingPoint[2] /= areaSum;
191 double boundaryPoint[3];
192 input->GetPoint(boundaryPointId,boundaryPoint);
193 double boundaryVector[3];
194 boundaryVector[0] = outerRingPoint[0] - boundaryPoint[0];
195 boundaryVector[1] = outerRingPoint[1] - boundaryPoint[1];
196 boundaryVector[2] = outerRingPoint[2] - boundaryPoint[2];
197 vtkMath::Normalize(boundaryVector);
198 double distance = this->ConstraintFactor * 0.125 * boundaryDiagonal;
199 outerRingPoint[0] = distance * boundaryVector[0] + boundaryPoint[0];
200 outerRingPoint[1] = distance * boundaryVector[1] + boundaryPoint[1];
201 outerRingPoint[2] = distance * boundaryVector[2] + boundaryPoint[2];
202 outerRingPoints->InsertNextPoint(outerRingPoint);
203 }
204
205 pointCells->Delete();
206 cellPoints->Delete();
207
208 vtkThinPlateSplineTransform* thinPlateSplineTransform = vtkThinPlateSplineTransform::New();
209 thinPlateSplineTransform->SetBasisToR2LogR();
210 thinPlateSplineTransform->SetSigma(1.0);
211 vtkPoints* sourceLandmarks = vtkPoints::New();
212 vtkPoints* targetLandmarks = vtkPoints::New();
213
214 for (i=0; i<numberOfBoundaryPoints; i++)
215 {
216 double angle = (double)i / (double)numberOfBoundaryPoints * 2.0*vtkMath::Pi();
217 double sourcePoint[3];
218 sourcePoint[0] = cos(angle);
219 sourcePoint[1] = sin(angle);
220 sourcePoint[2] = 0.0;
221 sourceLandmarks->InsertNextPoint(sourcePoint);
222 targetLandmarks->InsertNextPoint(boundaryCell->GetPoints()->GetPoint(i));
223 double outerRingRadius = 1.25;
224 sourcePoint[0] = outerRingRadius * cos(angle);
225 sourcePoint[1] = outerRingRadius * sin(angle);
226 sourcePoint[2] = 0.0;
227 sourceLandmarks->InsertNextPoint(sourcePoint);
228 targetLandmarks->InsertNextPoint(outerRingPoints->GetPoint(i));
229 }
230
231 thinPlateSplineTransform->SetSourceLandmarks(sourceLandmarks);
232 thinPlateSplineTransform->SetTargetLandmarks(targetLandmarks);
233
234 outerRingPoints->Delete();
235
236 vtkIdList* circlePointIds = vtkIdList::New();
237 vtkIdList* previousCirclePointIds = vtkIdList::New();
238
239 circlePointIds->DeepCopy(boundaryPointIds);
240
241 int k;
242 for (k=1; k<this->NumberOfRings; k++)
243 {
244 previousCirclePointIds->DeepCopy(circlePointIds);
245 circlePointIds->Initialize();
246 for (i=0; i<numberOfBoundaryPoints; i++)
247 {
248 double angle = (double)i / (double)numberOfBoundaryPoints * 2.0*vtkMath::Pi();
249 double radius = 1.0 - (double)k/(double)this->NumberOfRings;
250 double sourcePoint[3];
251 sourcePoint[0] = radius * cos(angle);
252 sourcePoint[1] = radius * sin(angle);
253 sourcePoint[2] = 0.0;
254 double transformedPoint[3];
255 thinPlateSplineTransform->TransformPoint(sourcePoint,transformedPoint);
256 vtkIdType circlePointId = newPoints->InsertNextPoint(transformedPoint);
257 circlePointIds->InsertNextId(circlePointId);
258 }
259 for (i=0; i<numberOfBoundaryPoints; i++)
260 {
261 // Two triangles rather than the quad they would make. The four points are not reliably
262 // a simple quadrilateral: the warp that places a ring can bring neighbouring points of
263 // it past each other, and the quad through them then crosses itself. vtkTriangleFilter
264 // gives up on such a cell and emits nothing for it, which takes a hole out of a cap that
265 // was closed - so the split is made here, where the two triangles are known, instead of
266 // being left to a filter that cannot always make it.
267 vtkIdType previousId = previousCirclePointIds->GetId(i);
268 vtkIdType previousNextId = previousCirclePointIds->GetId((i+1)%numberOfBoundaryPoints);
269 vtkIdType currentId = circlePointIds->GetId(i);
270 vtkIdType currentNextId = circlePointIds->GetId((i+1)%numberOfBoundaryPoints);
271
272 vtkIdType firstTriangle[3] = {previousId, previousNextId, currentId};
273 vtkIdType secondTriangle[3] = {previousNextId, currentNextId, currentId};
274 newPolys->InsertNextCell(3,firstTriangle);
275 newPolys->InsertNextCell(3,secondTriangle);
276 if (markCells)
277 {
278 vtkIdType capCellEntityId = this->CapCellEntityId(boundaryId,capBoundaryId,useBoundaryLabels);
279 cellEntityIdsArray->InsertNextValue(capCellEntityId);
280 cellEntityIdsArray->InsertNextValue(capCellEntityId);
281 }
282 }
283 }
284
285 // The innermost ring closes the cap. It used to be one polygon, which leaves the same trap
286 // as the ring cells did: the ring is not reliably convex, or even simple, so a filter asked
287 // to triangulate it can fail and leave the tip open. Close it with a fan from its own centre
288 // instead, which is a triangle either way.
289 double tipCentre[3] = {0.0, 0.0, 0.0};
290 vtkIdType numberOfTipPoints = circlePointIds->GetNumberOfIds();
291 for (i=0; i<numberOfTipPoints; i++)
292 {
293 double tipPoint[3];
294 newPoints->GetPoint(circlePointIds->GetId(i),tipPoint);
295 for (int component=0; component<3; component++)
296 {
297 tipCentre[component] += tipPoint[component];
298 }
299 }
300 for (int component=0; component<3; component++)
301 {
302 tipCentre[component] /= static_cast<double>(numberOfTipPoints);
303 }
304 vtkIdType tipCentreId = newPoints->InsertNextPoint(tipCentre);
305 for (i=0; i<numberOfTipPoints; i++)
306 {
307 vtkIdType tipTriangle[3] = {circlePointIds->GetId(i),
308 circlePointIds->GetId((i+1)%numberOfTipPoints),
309 tipCentreId};
310 newPolys->InsertNextCell(3,tipTriangle);
311 if (markCells)
312 {
313 cellEntityIdsArray->InsertNextValue(this->CapCellEntityId(boundaryId,capBoundaryId,useBoundaryLabels));
314 }
315 }
316
317 boundaryPointIds->Delete();
318
319 circlePointIds->Delete();
320 previousCirclePointIds->Delete();
321
322 thinPlateSplineTransform->Delete();
323 sourceLandmarks->Delete();
324 targetLandmarks->Delete();
325 }
326
327 output->SetPoints(newPoints);
328 output->SetPolys(newPolys);
329
330 if (markCells)
331 {
332 output->GetCellData()->AddArray(cellEntityIdsArray);
333 cellEntityIdsArray->Delete();
334 }
335
336 newPoints->Delete();
337 newPolys->Delete();
338
339 return 1;
340}
341
342vtkIdType vtkvmtkSmoothCapPolyData::CapCellEntityId(vtkIdType boundaryIndex, vtkIdType boundaryId, bool useBoundaryLabels)
343{
344 // An id the caller chose for this boundary is used as it stands; CellEntityIdOffset is what
345 // moves the ids this filter derives itself out of the way of the input's, and has no business
346 // shifting one that was picked deliberately.
347 if (this->BoundaryCellEntityIds
348 && boundaryId < this->BoundaryCellEntityIds->GetNumberOfTuples()
349 && this->BoundaryCellEntityIds->GetValue(boundaryId) >= 0)
350 {
351 return this->BoundaryCellEntityIds->GetValue(boundaryId);
352 }
353 if (useBoundaryLabels)
354 {
355 // The boundary's label is its name, and so the cap's id; neither it nor a chosen id is
356 // shifted by CellEntityIdOffset.
357 return boundaryId;
358 }
359 return boundaryIndex+1+this->CellEntityIdOffset;
360}
361
362void vtkvmtkSmoothCapPolyData::PrintSelf(std::ostream& os, vtkIndent indent)
363{
364 this->Superclass::PrintSelf(os,indent);
365}
static bool GetOrExtractBoundaries(vtkPolyData *surface, const char *boundaryLabelsArrayName, const char *boundaryPointOrderArrayName, vtkPolyData *boundaries, vtkIdList *boundaryLabels, vtkObject *warningSource=nullptr)
Close holes in a surface with a smooth, rounded (domed) cap built with concentric thin-plate-spline r...
void PrintSelf(std::ostream &os, vtkIndent indent) override
virtual void SetBoundaryPointOrderArrayName(const char *)
vtkIdType CapCellEntityId(vtkIdType boundaryIndex, vtkIdType boundaryId, bool useBoundaryLabels)
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
virtual void SetBoundaryLabelsArrayName(const char *)
vtkStandardNewMacro(vtkvmtkSmoothCapPolyData)