VMTK
Loading...
Searching...
No Matches
vtkvmtkAnnularCapPolyData.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkAnnularCapPolyData.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"
26#include "vtkPolyData.h"
27#include "vtkCellArray.h"
28#include "vtkPointData.h"
29#include "vtkCellData.h"
30#include "vtkPolyLine.h"
31#include "vtkIdTypeArray.h"
32#include "vtkIntArray.h"
33#include "vtkMath.h"
34#include "vtkInformation.h"
35#include "vtkInformationVector.h"
36#include "vtkObjectFactory.h"
37#include "vtkVersion.h"
38
39
41
51
53{
54 if (this->CellEntityIdsArrayName)
55 {
56 delete[] this->CellEntityIdsArrayName;
57 this->CellEntityIdsArrayName = NULL;
58 }
59 if (this->BoundaryIds)
60 {
61 this->BoundaryIds->Delete();
62 this->BoundaryIds = NULL;
63 }
64
67
68 if (this->BoundaryCellEntityIds)
69 {
70 this->BoundaryCellEntityIds->Delete();
71 this->BoundaryCellEntityIds = NULL;
72 }
73}
74
76 vtkInformation *vtkNotUsed(request),
77 vtkInformationVector **inputVector,
78 vtkInformationVector *outputVector)
79{
80 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
81 vtkInformation *outInfo = outputVector->GetInformationObject(0);
82
83 vtkPolyData *input = vtkPolyData::SafeDownCast(
84 inInfo->Get(vtkDataObject::DATA_OBJECT()));
85 vtkPolyData *output = vtkPolyData::SafeDownCast(
86 outInfo->Get(vtkDataObject::DATA_OBJECT()));
87
88 if ( input->GetNumberOfPoints() < 1 )
89 {
90 return 1;
91 }
92
93 input->BuildLinks();
94
95 vtkPoints* newPoints = vtkPoints::New();
96 newPoints->DeepCopy(input->GetPoints());
97
98 vtkCellArray* newPolys = vtkCellArray::New();
99 newPolys->DeepCopy(input->GetPolys());
100
101 vtkIdTypeArray* cellEntityIdsArray = NULL;
102 bool markCells = this->CellEntityIdsArrayName && this->CellEntityIdsArrayName[0];
103 if (markCells)
104 {
105 cellEntityIdsArray = vtkIdTypeArray::New();
106 cellEntityIdsArray->SetName(this->CellEntityIdsArrayName);
107 if (input->GetCellData()->GetArray(this->CellEntityIdsArrayName))
108 {
109 cellEntityIdsArray->DeepCopy(input->GetCellData()->GetArray(this->CellEntityIdsArrayName));
110 }
111 else
112 {
113 cellEntityIdsArray->SetNumberOfTuples(newPolys->GetNumberOfCells());
114 cellEntityIdsArray->FillComponent(0,static_cast<double>(this->CellEntityIdOffset));
115 }
116 }
117
118 // The boundaries either come from the labels the input already carries, which are the same
119 // boundaries in the same order every other filter reading those labels sees, or they are
120 // extracted here as they always were.
121 vtkNew<vtkPolyData> boundaries;
122 vtkNew<vtkIdList> boundaryLabels;
123 bool useBoundaryLabels = vtkvmtkBoundaryLabels::GetOrExtractBoundaries(
124 input,this->BoundaryLabelsArrayName,this->BoundaryPointOrderArrayName,boundaries,boundaryLabels,this);
125 int numberOfBoundaries = boundaries->GetNumberOfCells();
126
127 if ( (this->BoundaryIds && this->BoundaryIds->GetNumberOfIds() % 2)
128 || (!this->BoundaryIds && numberOfBoundaries % 2) )
129 {
130 vtkErrorMacro(<< "Error: the number of boundaries must be even.");
131 newPoints->Delete();
132 newPolys->Delete();
133 if (markCells)
134 {
135 cellEntityIdsArray->Delete();
136 }
137 }
138
139 vtkPoints* barycenters = vtkPoints::New();
140 barycenters->SetNumberOfPoints(numberOfBoundaries);
141
142 vtkIdList* boundaryPairings = vtkIdList::New();
143 boundaryPairings->SetNumberOfIds(numberOfBoundaries);
144 vtkIdList* visitedBoundaries = vtkIdList::New();
145 visitedBoundaries->SetNumberOfIds(numberOfBoundaries);
146
147 double barycenter[3];
148 for (int i=0; i<numberOfBoundaries; i++)
149 {
150 vtkCell* boundary = boundaries->GetCell(i);
151 vtkvmtkBoundaryReferenceSystems::ComputeBoundaryBarycenter(boundary->GetPoints(),barycenter);
152 barycenters->SetPoint(i,barycenter);
153 boundaryPairings->SetId(i,-1);
154 visitedBoundaries->SetId(i,-1);
155 }
156
157 double currentBarycenter[3];
158 double distance2, minDistance2 = 0.0;
159 vtkIdType closestBoundaryId;
160 for (int i=0; i<numberOfBoundaries; i++)
161 {
162 if (this->BoundaryIds && this->BoundaryIds->IsId(useBoundaryLabels ? boundaryLabels->GetId(i) : i) == -1)
163 {
164 continue;
165 }
166 if (boundaryPairings->GetId(i) != -1 || boundaryPairings->IsId(i) != -1)
167 {
168 continue;
169 }
170 barycenters->GetPoint(i,barycenter);
171 closestBoundaryId = -1;
172 for (int j=i+1; j<numberOfBoundaries; j++)
173 {
174 if (this->BoundaryIds && this->BoundaryIds->IsId(useBoundaryLabels ? boundaryLabels->GetId(j) : j) == -1)
175 {
176 continue;
177 }
178 if (boundaryPairings->GetId(j) != -1 || boundaryPairings->IsId(j) != -1)
179 {
180 continue;
181 }
182 barycenters->GetPoint(j,currentBarycenter);
183 distance2 = vtkMath::Distance2BetweenPoints(barycenter,currentBarycenter);
184 if (closestBoundaryId == -1 || distance2 < minDistance2)
185 {
186 minDistance2 = distance2;
187 closestBoundaryId = j;
188 }
189 }
190 boundaryPairings->SetId(i,closestBoundaryId);
191 boundaryPairings->SetId(closestBoundaryId,i);
192 }
193
194 for (int i=0; i<numberOfBoundaries; i++)
195 {
196 if (this->BoundaryIds && this->BoundaryIds->IsId(useBoundaryLabels ? boundaryLabels->GetId(i) : i) == -1)
197 {
198 continue;
199 }
200
201 if (boundaryPairings->GetId(i) == -1)
202 {
203 continue;
204 }
205
206 if (visitedBoundaries->GetId(i) != -1)
207 {
208 continue;
209 }
210
211 visitedBoundaries->SetId(i,i);
212 visitedBoundaries->SetId(boundaryPairings->GetId(i),i);
213
214 vtkIdTypeArray* boundaryPointIdsArray = vtkIdTypeArray::New();
215 boundaryPointIdsArray->DeepCopy(boundaries->GetPointData()->GetScalars());
216
217 vtkPolyLine* boundary0 = vtkPolyLine::SafeDownCast(boundaries->GetCell(i));
218 vtkIdType numberOfBoundaryPoints0 = boundary0->GetNumberOfPoints();
219 vtkIdList* boundaryPointIds0 = vtkIdList::New();
220 boundaryPointIds0->SetNumberOfIds(numberOfBoundaryPoints0);
221 for (int j=0; j<numberOfBoundaryPoints0; j++)
222 {
223 boundaryPointIds0->SetId(j,boundaryPointIdsArray->GetValue(boundary0->GetPointId(j)));
224 }
225
226 vtkPolyLine* boundary1 = vtkPolyLine::SafeDownCast(boundaries->GetCell(boundaryPairings->GetId(i)));
227 vtkIdType numberOfBoundaryPoints1 = boundary1->GetNumberOfPoints();
228 vtkIdList* boundaryPointIds1 = vtkIdList::New();
229 boundaryPointIds1->SetNumberOfIds(numberOfBoundaryPoints1);
230 for (int j=0; j<numberOfBoundaryPoints1; j++)
231 {
232 boundaryPointIds1->SetId(j,boundaryPointIdsArray->GetValue(boundary1->GetPointId(j)));
233 }
234
235 double startingPoint[3], currentPoint[3];
236 input->GetPoint(boundaryPointIds0->GetId(0),startingPoint);
237
238 vtkIdType offset = -1;
239 for (int j=0; j<numberOfBoundaryPoints1; j++)
240 {
241 input->GetPoint(boundaryPointIds1->GetId(j),currentPoint);
242 distance2 = vtkMath::Distance2BetweenPoints(startingPoint,currentPoint);
243 if (offset == -1 || distance2 < minDistance2)
244 {
245 offset = j;
246 minDistance2 = distance2;
247 }
248 }
249
250 double vectorToStart[3], vectorToForward[3], cross0[3], cross1[3];
251 double pointForward[3];
252
253 barycenters->GetPoint(i,barycenter);
254 input->GetPoint(boundaryPointIds0->GetId(0),startingPoint);
255 input->GetPoint(boundaryPointIds0->GetId(numberOfBoundaryPoints0/8),pointForward);
256
257 vectorToStart[0] = startingPoint[0] - barycenter[0];
258 vectorToStart[1] = startingPoint[1] - barycenter[1];
259 vectorToStart[2] = startingPoint[2] - barycenter[2];
260
261 vectorToForward[0] = pointForward[0] - barycenter[0];
262 vectorToForward[1] = pointForward[1] - barycenter[1];
263 vectorToForward[2] = pointForward[2] - barycenter[2];
264
265 vtkMath::Cross(vectorToStart,vectorToForward,cross0);
266
267 barycenters->GetPoint(boundaryPairings->GetId(i),barycenter);
268 input->GetPoint(boundaryPointIds1->GetId(0),startingPoint);
269 input->GetPoint(boundaryPointIds1->GetId(numberOfBoundaryPoints1/8),pointForward);
270
271 vectorToStart[0] = startingPoint[0] - barycenter[0];
272 vectorToStart[1] = startingPoint[1] - barycenter[1];
273 vectorToStart[2] = startingPoint[2] - barycenter[2];
274
275 vectorToForward[0] = pointForward[0] - barycenter[0];
276 vectorToForward[1] = pointForward[1] - barycenter[1];
277 vectorToForward[2] = pointForward[2] - barycenter[2];
278
279 vtkMath::Cross(vectorToStart,vectorToForward,cross1);
280
281 bool backward = false;
282 if (vtkMath::Dot(cross0,cross1) < 0.0)
283 {
284 backward = true;
285 }
286
287 double point0[3], nextPoint0[3];
288 double point1[3], nextPoint1[3];
289 vtkIdType pointId0, nextPointId0;
290 vtkIdType pointId1, nextPointId1;
291 int j = 0;
292 int k = 0;
293 while (true)
294 {
295 if (j == numberOfBoundaryPoints0 && k == numberOfBoundaryPoints1)
296 {
297 break;
298 }
299
300 pointId0 = j % numberOfBoundaryPoints0;
301 nextPointId0 = (pointId0 + 1) % numberOfBoundaryPoints0;
302 pointId1 = (k + offset) % numberOfBoundaryPoints1;
303 nextPointId1 = (pointId1 + 1) % numberOfBoundaryPoints1;
304 if (backward)
305 {
306 pointId1 = (numberOfBoundaryPoints1 - k + offset + numberOfBoundaryPoints1) % numberOfBoundaryPoints1;
307 nextPointId1 = (pointId1 - 1 + numberOfBoundaryPoints1) % numberOfBoundaryPoints1;
308 }
309
310 input->GetPoint(boundaryPointIds0->GetId(pointId0),point0);
311 input->GetPoint(boundaryPointIds0->GetId(nextPointId0),nextPoint0);
312 input->GetPoint(boundaryPointIds1->GetId(pointId1),point1);
313 input->GetPoint(boundaryPointIds1->GetId(nextPointId1),nextPoint1);
314
315 newPolys->InsertNextCell(3);
316 newPolys->InsertCellPoint(boundaryPointIds0->GetId(pointId0));
317 newPolys->InsertCellPoint(boundaryPointIds1->GetId(pointId1));
318
319 bool next1 = false;
320 if (vtkMath::Distance2BetweenPoints(point0,nextPoint1) < vtkMath::Distance2BetweenPoints(point1,nextPoint0))
321 {
322 next1 = true;
323 }
324 if (k == numberOfBoundaryPoints1)
325 {
326 next1 = false;
327 }
328 else if (j == numberOfBoundaryPoints0)
329 {
330 next1 = true;
331 }
332
333 if (next1)
334 {
335 newPolys->InsertCellPoint(boundaryPointIds1->GetId(nextPointId1));
336 k++;
337 }
338 else
339 {
340 newPolys->InsertCellPoint(boundaryPointIds0->GetId(nextPointId0));
341 j++;
342 }
343
344 if (markCells)
345 {
346 // A cap here closes two boundaries, so the id comes from the pair rather than from one
347 // of them. i is the pair's position; the ids that name it are the two boundaries'.
348 vtkIdType boundaryId = useBoundaryLabels ? boundaryLabels->GetId(i) : i;
349 vtkIdType partnerBoundaryId = useBoundaryLabels ? boundaryLabels->GetId(boundaryPairings->GetId(i)) : boundaryPairings->GetId(i);
350 cellEntityIdsArray->InsertNextValue(
351 this->PairCapCellEntityId(i+1+this->CellEntityIdOffset,boundaryId,partnerBoundaryId,useBoundaryLabels));
352 }
353 }
354
355 boundaryPointIdsArray->Delete();
356 boundaryPointIds0->Delete();
357 boundaryPointIds1->Delete();
358 }
359
360 output->SetPoints(newPoints);
361 output->SetPolys(newPolys);
362
363 output->GetPointData()->PassData(input->GetPointData());
364
365 if (markCells)
366 {
367 output->GetCellData()->AddArray(cellEntityIdsArray);
368 cellEntityIdsArray->Delete();
369 }
370
371 newPoints->Delete();
372 newPolys->Delete();
373 barycenters->Delete();
374 boundaryPairings->Delete();
375 visitedBoundaries->Delete();
376
377 return 1;
378}
379
380vtkIdType vtkvmtkAnnularCapPolyData::PairCapCellEntityId(vtkIdType positionalId, vtkIdType boundaryId, vtkIdType partnerBoundaryId, bool useBoundaryLabels)
381{
382 // With the labels in use a boundary's label is its name, so the pair is named by the
383 // lower of its two labels -- the inner boundary, when the surface was labelled in annular
384 // mode -- unless the caller chose an id for it. Without them there is only position.
385 vtkIdType unchosenId = useBoundaryLabels
386 ? (boundaryId < partnerBoundaryId ? boundaryId : partnerBoundaryId)
387 : positionalId;
388 if (!this->BoundaryCellEntityIds)
389 {
390 return unchosenId;
391 }
392
393 vtkIdType lowerId = boundaryId < partnerBoundaryId ? boundaryId : partnerBoundaryId;
394 vtkIdType higherId = boundaryId < partnerBoundaryId ? partnerBoundaryId : boundaryId;
395
396 vtkIdType numberOfEntries = this->BoundaryCellEntityIds->GetNumberOfTuples();
397 vtkIdType lowerEntry = (lowerId >= 0 && lowerId < numberOfEntries) ? this->BoundaryCellEntityIds->GetValue(lowerId) : -1;
398 vtkIdType higherEntry = (higherId >= 0 && higherId < numberOfEntries) ? this->BoundaryCellEntityIds->GetValue(higherId) : -1;
399
400 if (lowerEntry >= 0 && higherEntry >= 0 && lowerEntry != higherEntry)
401 {
402 // Silence here would name the cap after whichever boundary the pairing happened to reach
403 // first, which is not something the caller can predict or see afterwards.
404 vtkWarningMacro(<<"The two boundaries of a pair were given different cap ids ("<<lowerEntry<<" for boundary "<<lowerId
405 <<" and "<<higherEntry<<" for boundary "<<higherId<<"); the cap takes "<<lowerEntry
406 <<", the id of the lower boundary.");
407 }
408
409 if (lowerEntry >= 0)
410 {
411 return lowerEntry;
412 }
413 if (higherEntry >= 0)
414 {
415 return higherEntry;
416 }
417 return unchosenId;
418}
419
420void vtkvmtkAnnularCapPolyData::PrintSelf(std::ostream& os, vtkIndent indent)
421{
422 this->Superclass::PrintSelf(os,indent);
423}
Add annular caps between the boundaries of a walled surface.
virtual void SetBoundaryPointOrderArrayName(const char *)
virtual void SetBoundaryLabelsArrayName(const char *)
void PrintSelf(std::ostream &os, vtkIndent indent) override
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
vtkIdType PairCapCellEntityId(vtkIdType positionalId, vtkIdType boundaryId, vtkIdType partnerBoundaryId, bool useBoundaryLabels)
static bool GetOrExtractBoundaries(vtkPolyData *surface, const char *boundaryLabelsArrayName, const char *boundaryPointOrderArrayName, vtkPolyData *boundaries, vtkIdList *boundaryLabels, vtkObject *warningSource=nullptr)
static void ComputeBoundaryBarycenter(vtkPoints *points, double barycenter[3])
vtkStandardNewMacro(vtkvmtkAnnularCapPolyData)