VMTK
Loading...
Searching...
No Matches
vtkvmtkBoundaryLayerGenerator2.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: VMTK
4 Module: $RCSfile: vtkvmtkBoundaryLayerGenerator2.cxx,v $
5 Language: C++
6 Date: $$
7 Version: $$
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 Note: this class was contributed by
21 Tangui Morvan
22 Kalkulo AS
23 Simula Research Laboratory
24=========================================================================*/
25
26//TODO: Bug in WarpPoints in parent class?
27
29#include "vtkvmtkConstants.h"
30#include "vtkUnstructuredGrid.h"
31#include "vtkPointData.h"
32#include "vtkPoints.h"
33#include "vtkCellArray.h"
34#include "vtkIntArray.h"
35#include "vtkMath.h"
36#include "vtkIdTypeArray.h"
37#include "vtkInformation.h"
38#include "vtkInformationVector.h"
39#include "vtkObjectFactory.h"
40#include "vtkCellData.h"
41#include "vtkGeometryFilter.h"
42#include "vtkOrderedTriangulator.h"
43#include "vtkVersion.h"
44
46
47
49
51{
52 this->CellEntityIdsArrayName = NULL;
53 this->OpenProfilesIdsArrayName = NULL;
57 this->LayerEntityId = 0;
58 this->SurfaceEntityId = 1;
59 this->OpenProfilesEntityId = 2;
60 this->Triangulator = vtkOrderedTriangulator::New();
61}
62
64{
65 if (this->CellEntityIdsArrayName)
66 {
67 delete[] this->CellEntityIdsArrayName;
68 this->CellEntityIdsArrayName = NULL;
69 }
71 {
72 delete[] this->OpenProfilesIdsArrayName;
73 this->OpenProfilesIdsArrayName = NULL;
74 }
75 this->Triangulator->Delete();
76}
77
79 vtkInformation *vtkNotUsed(request),
80 vtkInformationVector **inputVector,
81 vtkInformationVector *outputVector)
82{
83 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
84 vtkInformation *outInfo = outputVector->GetInformationObject(0);
85
86 vtkUnstructuredGrid *input = vtkUnstructuredGrid::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
87 vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
88
89 vtkPoints* inputPoints = input->GetPoints();
90
91 vtkIdType numberOfInputPoints = inputPoints->GetNumberOfPoints();
92 vtkIdType numberOfInputCells = input->GetNumberOfCells();
93
94 if (!this->WarpVectorsArrayName)
95 {
96 vtkErrorMacro("WarpVectors array name not specified.");
97 return 1;
98 }
99
100 if (!input->GetPointData()->GetArray(this->WarpVectorsArrayName))
101 {
102 vtkErrorMacro(<< "WarpVectors array with name specified does not exist!");
103 return 1;
104 }
105
106 this->WarpVectorsArray = input->GetPointData()->GetArray(this->WarpVectorsArrayName);
107
109 {
110 if (!this->LayerThicknessArrayName)
111 {
112 vtkErrorMacro("LayerThickness array name not specified.");
113 return 1;
114 }
115
116 if (!input->GetPointData()->GetArray(this->LayerThicknessArrayName))
117 {
118 vtkErrorMacro(<< "LayerThickness array with name specified does not exist!");
119 return 1;
120 }
121
122 this->LayerThicknessArray = input->GetPointData()->GetArray(this->LayerThicknessArrayName);
123 }
124
125 vtkPoints* outputPoints = vtkPoints::New();
126 vtkPoints* warpedPoints = vtkPoints::New();
127
128 vtkCellArray* boundaryLayerCellArray = vtkCellArray::New();
129 vtkIdList* boundaryLayerCellTypes = vtkIdList::New();
130
131 vtkIntArray *cellEntityIdsArray = NULL;
132
133 if ((this->CellEntityIdsArrayName) && (strcmp(this->CellEntityIdsArrayName,"") != 0))
134 {
135 cellEntityIdsArray = vtkIntArray::New();
136 cellEntityIdsArray->SetName(this->CellEntityIdsArrayName);
137 cellEntityIdsArray->SetNumberOfComponents(1);
138 }
139 //Keep track of the id to use
140 int currentSurfaceEntityId = this->SurfaceEntityId;
141
142 int cellType;
143 cellType = input->GetCellType(0); // TODO: check if all elements are consistent
144 bool warpQuadratic = false;
145 if (cellType == VTK_QUADRATIC_TRIANGLE)
146 {
147 warpQuadratic = true;
148 }
149
150 vtkIdType numberOfLayerPoints = numberOfInputPoints;
151 if (warpQuadratic)
152 {
153 numberOfLayerPoints = 2 * numberOfInputPoints;
154 }
155
156 outputPoints->SetNumberOfPoints(numberOfInputPoints + numberOfLayerPoints * this->NumberOfSubLayers);
157
158 double point[3];
159 int i;
160 for (i=0; i<numberOfInputPoints; i++)
161 {
162 inputPoints->GetPoint(i,point);
163 outputPoints->SetPoint(i,point);
164 }
165
166 vtkIdType npts;
167 const vtkIdType *pts;
168
169 vtkIdType *surfacePts;
170
172 {
173 for (i=0; i<numberOfInputCells; i++)
174 {
175 input->GetCellPoints(i,npts,pts);
176 cellType = input->GetCellType(i);
177 surfacePts = new vtkIdType[npts];
178 switch(cellType)
179 {
180 case VTK_TRIANGLE:
181 boundaryLayerCellTypes->InsertNextId(VTK_TRIANGLE);
182 surfacePts[0] = pts[0];
183 surfacePts[1] = pts[1];
184 surfacePts[2] = pts[2];
185 break;
186 case VTK_QUAD:
187 boundaryLayerCellTypes->InsertNextId(VTK_QUAD);
188 surfacePts[0] = pts[0];
189 surfacePts[1] = pts[1];
190 surfacePts[2] = pts[2];
191 surfacePts[3] = pts[3];
192 break;
193 case VTK_QUADRATIC_TRIANGLE:
194 boundaryLayerCellTypes->InsertNextId(VTK_QUADRATIC_TRIANGLE);
195 surfacePts[0] = pts[0];
196 surfacePts[1] = pts[1];
197 surfacePts[2] = pts[2];
198 surfacePts[3] = pts[3];
199 surfacePts[4] = pts[4];
200 surfacePts[5] = pts[5];
201 break;
202 default:
203 vtkErrorMacro(<<"Unsupported surface element.");
204 return 1;
205 break;
206 }
207 boundaryLayerCellArray->InsertNextCell(npts,surfacePts);
208 if (cellEntityIdsArray)
209 {
210 cellEntityIdsArray->InsertNextValue(currentSurfaceEntityId);
211 }
212 delete[] surfacePts;
213 }
214 //Switch to next surface entity
215 currentSurfaceEntityId++;
216 }
217
218 if (this->InnerSurface)
219 {
220 this->InnerSurface->Delete();
221 this->InnerSurface = NULL;
222 }
223
224 this->InnerSurface = vtkUnstructuredGrid::New();
225 this->InnerSurface->DeepCopy(input);
226 vtkPoints* innerSurfacePoints = vtkPoints::New();
227 this->WarpPoints(inputPoints,innerSurfacePoints,this->NumberOfSubLayers-1,warpQuadratic);
228 this->InnerSurface->GetPoints()->DeepCopy(innerSurfacePoints);
229
230 //Get the ids of the open profiles if necessary
231 vtkDataArray *openProfilesIdsArray = NULL;
232 bool allocateArray = false;
233
234
236 {
237 //Find or build the ids of the open profiles
238 //TODO: Check that the open profiles ids are consistent
239 if (this->OpenProfilesIdsArrayName)
240 {
241 openProfilesIdsArray = input->GetPointData()->GetArray(this->OpenProfilesIdsArrayName);
242 }
243
244 allocateArray = (openProfilesIdsArray == NULL);
245 if (allocateArray)
246 {
247 //The open profiles ids have to be created
248 openProfilesIdsArray = vtkIntArray::New();
249 openProfilesIdsArray->SetName("OpenProfilesIds");
250 openProfilesIdsArray->SetNumberOfComponents(1);
251 openProfilesIdsArray->SetNumberOfTuples(numberOfInputPoints);
252 openProfilesIdsArray->FillComponent(0, -1);
253
254 //First convert the unstructured grid to poly data
255 vtkGeometryFilter *meshToSurface = vtkGeometryFilter::New();
256 meshToSurface->SetInputData(input);
257 meshToSurface->MergingOff();
258 meshToSurface->Update();
259
260 //Extract the open profiles
262 openProfilesExtractor->SetInputData(meshToSurface->GetOutput());
263 openProfilesExtractor->Update();
264
265 //Update the openProfilesIdsArray
266 vtkCellArray *openProfiles = openProfilesExtractor->GetOutput()->GetLines();
267 vtkDataArray *openProfilesScalars = openProfilesExtractor->GetOutput()->GetPointData()->GetScalars();
268
269 vtkIdType npts = 0;
270 const vtkIdType *pts = NULL;
271 int profileId;
272 for (profileId=0, openProfiles->InitTraversal(); openProfiles->GetNextCell(npts,pts); profileId++)
273 {
274 for (int i=0;i<npts;i++)
275 {
276 openProfilesIdsArray->SetComponent(openProfilesScalars->GetComponent(pts[i],0),0,profileId);
277 }
278 }
279
280 openProfilesExtractor->Delete();
281 meshToSurface->Delete();
282 }
283
284 }
285
286
287 //Add the volumetric elements and the open profiles extensions if necessary
288 int k;
289 for (k=0; k<this->NumberOfSubLayers; k++)
290 {
291 warpedPoints->Initialize();
292 this->WarpPoints(inputPoints,warpedPoints,k,warpQuadratic);
293
294 for (i=0; i<numberOfLayerPoints; i++)
295 {
296 warpedPoints->GetPoint(i,point);
297 outputPoints->SetPoint(i + numberOfInputPoints + k*numberOfLayerPoints,point);
298 }
299
300 vtkIdType prismNPts, *prismPts;
301 vtkIdType nTetraPts = 0;
302 const vtkIdType *tetraPts = NULL;
303
304 for (i=0; i<numberOfInputCells; i++)
305 {
306 input->GetCellPoints(i,npts,pts);
307 cellType = input->GetCellType(i);
308 if (cellType == VTK_TRIANGLE || cellType == VTK_QUAD)
309 {
310 prismNPts = npts * 2;
311 prismPts = new vtkIdType[prismNPts];
312 int j;
313 for (j=0; j<npts; j++)
314 {
315 prismPts[j] = pts[j] + k*numberOfLayerPoints;
316 }
317 for (j=0; j<npts; j++)
318 {
319 prismPts[j+npts] = pts[j] + (k+1)*numberOfLayerPoints;
320 }
321
322 //Tetrahedralize the cell
323 //Wedge parametric coords
324 double pCoordsWedge[] = {0.0,0.0,0.0, 1.0,0.0,0.0, 0.0,1.0,0.0,
325 0.0,0.0,1.0, 1.0,0.0,1.0, 0.0,1.0,1.0};
326 //Hexahedron parametric coords
327 double pCoordsHex[] = {0.0,0.0,0.0, 1.0,0.0,0.0,
328 1.0,1.0,0.0, 0.0,1.0,0.0,
329 0.0,0.0,1.0, 1.0,0.0,1.0,
330 1.0,1.0,1.0, 0.0,1.0,1.0};
331 double *pCoords = NULL;
332 int prismNEdges = 0;
333 int prismType = 0;
334
335 if (cellType == VTK_TRIANGLE)
336 {
337 prismType = VTK_WEDGE;
338 pCoords = pCoordsWedge;
339 prismNEdges = 9;
340 }
341 else if (cellType == VTK_QUAD)
342 {
343 prismType = VTK_HEXAHEDRON;
344 pCoords = pCoordsHex;
345 prismNEdges = 12;
346 }
347
348 this->Triangulator->InitTriangulation(0.0,1.0, 0.0,1.0, 0.0,1.0, prismNPts);
349
350 double prismPt[3];
351 double *p;
352 for (p=pCoords, j=0; j<prismNPts; j++, p+=3)
353 {
354 outputPoints->GetPoint(prismPts[j], prismPt);
355 this->Triangulator->InsertPoint(prismPts[j], prismPt, p, 0);
356 }
357
358 this->Triangulator->TemplateTriangulate(prismType, prismNPts, prismNEdges);
359
360 //Index of the first tetrahedron that is about to be inserted
361 vtkIdType tetraStartId = boundaryLayerCellArray->GetNumberOfCells();
362 //Insert the new tetras
363 vtkIdType numTets = this->Triangulator->AddTetras(0,boundaryLayerCellArray);
364
365 for (j=0; j<numTets; j++)
366 {
367 boundaryLayerCellTypes->InsertNextId(VTK_TETRA);
368 if (cellEntityIdsArray)
369 {
370 cellEntityIdsArray->InsertNextValue(this->LayerEntityId);
371 }
372 }
373
374
375 //Add the extruded open profiles
376 if (openProfilesIdsArray)
377 {
378 for (j=0;j<npts;j++)
379 {
380 //TODO: what to do if the profileid is not -1 but different between the 2 points?
381 if ((openProfilesIdsArray->GetComponent(pts[j],0) != -1) && (openProfilesIdsArray->GetComponent(pts[(j+1)%npts],0) != -1))
382 {
383 //The points forming the extruded open profile
384 vtkIdList *openProfilePts = vtkIdList::New();
385 openProfilePts->InsertNextId(prismPts[j]);
386 openProfilePts->InsertNextId(prismPts[(j+1)%npts]);
387 openProfilePts->InsertNextId(prismPts[j+npts]);
388 openProfilePts->InsertNextId(prismPts[(j+1)%npts + npts]);
389
390 vtkIdList *tetraPtsList = vtkIdList::New();
391 //Go through the tetrahedra we just added
392 for (int k=0; k<numTets; k++)
393 {
394 boundaryLayerCellArray->GetCellAtId(tetraStartId + k, nTetraPts, tetraPts);
395 tetraPtsList->Initialize();
396 for (int l=0; l<nTetraPts; l++)
397 {
398 tetraPtsList->InsertNextId(tetraPts[l]);
399 }
400 tetraPtsList->IntersectWith(openProfilePts);
401 //If this tetrahedra has a face on the extruded open profile, include it
402 if (tetraPtsList->GetNumberOfIds() == 3)
403 {
404 boundaryLayerCellArray->InsertNextCell(tetraPtsList);
405 boundaryLayerCellTypes->InsertNextId(VTK_TRIANGLE);
406 if (cellEntityIdsArray)
407 {
408 cellEntityIdsArray->InsertNextValue(this->OpenProfilesEntityId + openProfilesIdsArray->GetComponent(pts[j],0));
409 }
410 }
411 }
412 tetraPtsList->Delete();
413 openProfilePts->Delete();
414 }
415 }
416 }
417
418 delete[] prismPts;
419 }
420 //FIXME: NOT HANDLED
421// else if (cellType == VTK_QUADRATIC_TRIANGLE)
422// {
423// prismNPts = npts * 3 - 3;
424// // prismNPts = npts * 3;
425// prismPts = new vtkIdType[prismNPts];
426//
427// boundaryLayerCellTypes->InsertNextId(VTK_QUADRATIC_WEDGE);
428//
429// prismPts[0] = pts[0] + k*numberOfLayerPoints;
430// prismPts[1] = pts[1] + k*numberOfLayerPoints;
431// prismPts[2] = pts[2] + k*numberOfLayerPoints;
432//
433// prismPts[3] = pts[0] + k*numberOfLayerPoints + numberOfLayerPoints;
434// prismPts[4] = pts[1] + k*numberOfLayerPoints + numberOfLayerPoints;
435// prismPts[5] = pts[2] + k*numberOfLayerPoints + numberOfLayerPoints;
436//
437// prismPts[6] = pts[3] + k*numberOfLayerPoints;
438// prismPts[7] = pts[4] + k*numberOfLayerPoints;
439// prismPts[8] = pts[5] + k*numberOfLayerPoints;
440//
441// prismPts[9] = pts[3] + k*numberOfLayerPoints + numberOfLayerPoints;
442// prismPts[10] = pts[4] + k*numberOfLayerPoints + numberOfLayerPoints;
443// prismPts[11] = pts[5] + k*numberOfLayerPoints + numberOfLayerPoints;
444//
445// prismPts[12] = pts[0] + k*numberOfLayerPoints + numberOfLayerPoints/2;
446// prismPts[13] = pts[1] + k*numberOfLayerPoints + numberOfLayerPoints/2;
447// prismPts[14] = pts[2] + k*numberOfLayerPoints + numberOfLayerPoints/2;
448//
449// // TODO: this creates a 18-noded wedge, which is not supported by VTK, but it works as a 15-node (the last 3 points are ignored). Better solutions? Could put it in as a vtkGenericCell, but harder to identify it afterwards
450// // prismPts[15] = pts[3] + k*numberOfLayerPoints + numberOfLayerPoints/2;
451// // prismPts[16] = pts[4] + k*numberOfLayerPoints + numberOfLayerPoints/2;
452// // prismPts[17] = pts[5] + k*numberOfLayerPoints + numberOfLayerPoints/2;
453//
454// boundaryLayerCellArray->InsertNextCell(prismNPts,prismPts);
455// if (cellEntityIdsArray) cellEntityIdsArray->InsertNextValue(this->LayerEntityId);
456// delete[] prismPts;
457// }
458 else
459 {
460 vtkErrorMacro(<<"Unsupported surface element.");
461 return 1;
462 }
463 }
464 }
465
466 if (allocateArray)
467 {
468 openProfilesIdsArray->Delete();
469 }
470
471
473 {
474 for (i=0; i<numberOfInputCells; i++)
475 {
476 input->GetCellPoints(i,npts,pts);
477 cellType = input->GetCellType(i);
478 surfacePts = new vtkIdType[npts];
479 switch(cellType)
480 {
481 case VTK_TRIANGLE:
482 boundaryLayerCellTypes->InsertNextId(VTK_TRIANGLE);
483 surfacePts[0] = pts[0] + this->NumberOfSubLayers*numberOfLayerPoints;
484 surfacePts[1] = pts[1] + this->NumberOfSubLayers*numberOfLayerPoints;
485 surfacePts[2] = pts[2] + this->NumberOfSubLayers*numberOfLayerPoints;
486 break;
487 case VTK_QUAD:
488 boundaryLayerCellTypes->InsertNextId(VTK_QUAD);
489 surfacePts[0] = pts[0] + this->NumberOfSubLayers*numberOfLayerPoints;
490 surfacePts[1] = pts[1] + this->NumberOfSubLayers*numberOfLayerPoints;
491 surfacePts[2] = pts[2] + this->NumberOfSubLayers*numberOfLayerPoints;
492 surfacePts[3] = pts[3] + this->NumberOfSubLayers*numberOfLayerPoints;
493 break;
494 case VTK_QUADRATIC_TRIANGLE:
495 boundaryLayerCellTypes->InsertNextId(VTK_QUADRATIC_TRIANGLE);
496 surfacePts[0] = pts[0] + this->NumberOfSubLayers*numberOfLayerPoints;
497 surfacePts[1] = pts[1] + this->NumberOfSubLayers*numberOfLayerPoints;
498 surfacePts[2] = pts[2] + this->NumberOfSubLayers*numberOfLayerPoints;
499 surfacePts[3] = pts[3] + this->NumberOfSubLayers*numberOfLayerPoints;
500 surfacePts[4] = pts[4] + this->NumberOfSubLayers*numberOfLayerPoints;
501 surfacePts[5] = pts[5] + this->NumberOfSubLayers*numberOfLayerPoints;
502 break;
503 default:
504 vtkErrorMacro(<<"Unsupported surface element.");
505 return 1;
506 break;
507 }
508 boundaryLayerCellArray->InsertNextCell(npts,surfacePts);
509 if (cellEntityIdsArray) cellEntityIdsArray->InsertNextValue(currentSurfaceEntityId);
510 delete[] surfacePts;
511 }
512 }
513
514 output->SetPoints(outputPoints);
515
516 int* boundaryLayerCellTypesInt = new int[boundaryLayerCellTypes->GetNumberOfIds()];
517 for (i=0; i<boundaryLayerCellTypes->GetNumberOfIds(); i++)
518 {
519 boundaryLayerCellTypesInt[i] = boundaryLayerCellTypes->GetId(i);
520 }
521
522 output->SetCells(boundaryLayerCellTypesInt,boundaryLayerCellArray);
523
524 delete[] boundaryLayerCellTypesInt;
525
526 if (cellEntityIdsArray)
527 {
528 output->GetCellData()->AddArray(cellEntityIdsArray);
529 cellEntityIdsArray->Delete();
530 }
531
532 outputPoints->Delete();
533 warpedPoints->Delete();
534 boundaryLayerCellArray->Delete();
535 boundaryLayerCellTypes->Delete();
536 innerSurfacePoints->Delete();
537
538 return 1;
539}
540
541void vtkvmtkBoundaryLayerGenerator2::PrintSelf(std::ostream& os, vtkIndent indent)
542{
543 this->Superclass::PrintSelf(os,indent);
544}
Provides an extended version of vtkvmtkBoundaryLayerGenerator.
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void PrintSelf(std::ostream &os, vtkIndent indent) override
void WarpPoints(vtkPoints *inputPoints, vtkPoints *warpedPoints, int subLayerId, bool quadratic)
void PrintSelf(std::ostream &os, vtkIndent indent) override
Extract boundary edges as poly lines.
static vtkvmtkPolyDataBoundaryExtractor * New()
vtkStandardNewMacro(vtkvmtkBoundaryLayerGenerator2)