VMTK
Loading...
Searching...
No Matches
vtkvmtkTetGenWrapper.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkTetGenWrapper.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:47:48 $
7Version: $Revision: 1.8 $
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
23#include "vtkvmtkConstants.h"
24#include "vtkUnstructuredGrid.h"
25#include "vtkPoints.h"
26#include "vtkCellArray.h"
27#include "vtkPointData.h"
28#include "vtkCellData.h"
29#include "vtkIntArray.h"
30#include "vtkStdString.h"
31#include "vtkInformation.h"
32#include "vtkInformationVector.h"
33#include "vtkObjectFactory.h"
34#include "tetgen.h"
35
36#include <iostream>
37
39
41{
42 this->PLC = 0; // -p switch, 0
43 this->Refine = 0; // -r switch, 0
44 this->Coarsen = 0; // -R switch, 0
45 this->Quality = 0; // -q switch, 0
46 this->NoBoundarySplit = 0; // -Y switch, 0
47 this->VarVolume = 0; // -a switch without number, 0
48 this->FixedVolume = 0; // -a switch with number, 0
49 this->MaxVolume = -1.0; // number after -a, -1.0
50 this->RemoveSliver = 0; // -s switch, 0
51 this->MinRatio = 2.0; // number after -q, 2.0
52 this->MinDihedral = 5.0; // number after -qq, 5.0
53 this->MaxDihedral = 165.0; // number after -qqq, 165.0
54 this->RegionAttrib = 0; // -A switch, 0
55 this->Epsilon = 1.0e-8; // number after -T switch, 1.0e-8
56 this->NoMerge = 0; // -M switch, 0
57 this->DetectInter = 0; // -d switch, 0
58 this->CheckClosure = 0; // -c switch, 0
59 this->Order = 1; // number after -o switch, 1 (e.g. -o2 for quadratic elements)
60 this->DoCheck = 0; // -C switch, 0
61 this->UseSizingFunction = 0; // -m switch, 0
62
63 this->Verbose = 0;
64
65 this->CellEntityIdsArrayName = NULL;
66 this->TetrahedronVolumeArrayName = NULL;
67 this->SizingFunctionArrayName = NULL;
68
69 this->OutputSurfaceElements = 1;
70 this->OutputVolumeElements = 1;
71
72 this->LastRunExitStatus = -1;
73}
74
76{
77 if (this->CellEntityIdsArrayName)
78 {
79 delete[] this->CellEntityIdsArrayName;
80 this->CellEntityIdsArrayName = NULL;
81 }
82
84 {
85 delete[] this->TetrahedronVolumeArrayName;
86 this->TetrahedronVolumeArrayName = NULL;
87 }
88
90 {
91 delete[] this->SizingFunctionArrayName;
92 this->SizingFunctionArrayName = NULL;
93 }
94}
95
97 vtkInformation *vtkNotUsed(request),
98 vtkInformationVector **inputVector,
99 vtkInformationVector *outputVector)
100{
101 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
102 vtkInformation *outInfo = outputVector->GetInformationObject(0);
103
104 vtkUnstructuredGrid *input = vtkUnstructuredGrid::SafeDownCast(
105 inInfo->Get(vtkDataObject::DATA_OBJECT()));
106 vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
107 outInfo->Get(vtkDataObject::DATA_OBJECT()));
108
109 if (this->VarVolume && !this->TetrahedronVolumeArrayName)
110 {
111 vtkErrorMacro(<<"VarVolumeOn but TetrahedronVolumeArrayName not specified");
112 return 1;
113 }
114
115 vtkStdString tetgenOptionString;
116
117 char buffer[64];
118
119 if (this->PLC)
120 {
121 tetgenOptionString += "p";
122 }
123
124 if (this->Refine)
125 {
126 tetgenOptionString += "r";
127 }
128
129 if (this->Coarsen)
130 {
131 tetgenOptionString += "R";
132 }
133
134 if (this->Quality)
135 {
136 tetgenOptionString += "q";
137 sprintf(buffer,"%f",this->MinRatio);
138 tetgenOptionString += buffer;
139 tetgenOptionString += "q";
140 sprintf(buffer,"%f",this->MinDihedral);
141 tetgenOptionString += buffer;
142 tetgenOptionString += "q";
143 sprintf(buffer,"%f",this->MaxDihedral);
144 tetgenOptionString += buffer;
145 }
146
147 if (this->NoBoundarySplit)
148 {
149 tetgenOptionString += "Y";
150 }
151
152 if (this->RemoveSliver)
153 {
154 tetgenOptionString += "s";
155 }
156
157 if (this->Order == 2)
158 {
159 tetgenOptionString += "o2";
160 }
161
162 tetgenOptionString += "T";
163 sprintf(buffer,"%e",this->Epsilon);
164 tetgenOptionString += buffer;
165
166 if (this->VarVolume)
167 {
168 tetgenOptionString += "a";
169 }
170
171 if (this->FixedVolume)
172 {
173 tetgenOptionString += "a";
174 sprintf(buffer,"%f",this->MaxVolume);
175 tetgenOptionString += buffer;
176 }
177
178 if (this->RegionAttrib)
179 {
180 tetgenOptionString += "A";
181 }
182
183 if (this->NoMerge)
184 {
185 tetgenOptionString += "M";
186 }
187
188 if (this->DetectInter)
189 {
190 tetgenOptionString += "d";
191 }
192
193 if (this->DoCheck)
194 {
195 tetgenOptionString += "C";
196 }
197
198 if (this->CheckClosure)
199 {
200 tetgenOptionString += "c";
201 }
202
203 tetgenOptionString += "z";
204// tetgenOptionString += "Y";
205
206 if (this->Verbose)
207 {
208 tetgenOptionString += "V";
209 }
210 else
211 {
212 tetgenOptionString += "Q";
213 }
214
215 vtkDataArray* sizingFunctionArray = NULL;
216 if (this->SizingFunctionArrayName)
217 {
218 sizingFunctionArray = input->GetPointData()->GetArray(this->SizingFunctionArrayName);
219 }
220
221 if (this->UseSizingFunction && sizingFunctionArray)
222 {
223 // TetGen reads a sizing function through its -m switch, and -m means "take the sizes from a
224 // background mesh". This wrapper has no way to hand one over, so TetGen makes its own by
225 // duplicating the mesh it is about to refine, and that path does not answer the same way
226 // twice: the same surface, meshed twice over in one session, comes back filled with
227 // tetrahedra one time and empty the next. Nothing is produced here rather than a mesh that
228 // cannot be reproduced.
229 vtkErrorMacro(<<"UseSizingFunction is on, and the sizing function array '"
230 <<this->SizingFunctionArrayName<<"' was found on the input, so TetGen would be run with its "
231 "-m switch. -m takes element sizes from a background mesh; with none given, TetGen builds "
232 "one out of the mesh it is meshing, and that gives a different answer from one run to the "
233 "next - the same input meshed twice in one session comes back tetrahedralized once and "
234 "empty once. No mesh is generated, rather than one that cannot be reproduced.\n"
235 " To ask for a size without it: leave UseSizingFunction off and bound the element volume "
236 "instead, with SetFixedVolume(1) and SetMaxVolume(v), where v is the volume of the regular "
237 "tetrahedron of the edge length wanted - v = a*a*a/(6*sqrt(2)) for edge length a. That "
238 "gives the same mesh every time.\n"
239 " A single volume bound is one size for the whole mesh, where the sizing function was a "
240 "size per point. Where the size has to vary, vary the triangles of the surface that is "
241 "handed in: the elements against it follow their size, and the bound then only says how "
242 "coarse the middle may become.");
243 this->SetLastRunExitStatus(1);
244 return 1;
245 }
246
247 tetgenio in_tetgenio;
248 tetgenio out_tetgenio;
249
250 in_tetgenio.firstnumber = 0;
251
252 const int meshDimensionality = 3; //TODO: set this
253 in_tetgenio.mesh_dim = meshDimensionality;
254
255 //TODO - all point arrays except marker array
256 in_tetgenio.pointattributelist = NULL; //REAL*
257 in_tetgenio.numberofpointattributes = 0;
258 //TODO
259
260 in_tetgenio.pointmtrlist = NULL;
261 in_tetgenio.numberofpointmtrs = 0;
262
263 int numberOfPoints = input->GetNumberOfPoints();
264
265 in_tetgenio.numberofpoints = numberOfPoints;
266 in_tetgenio.pointlist = new REAL[meshDimensionality * numberOfPoints];
267#if 0
268 if (pointMarkerArray)
269 {
270 in_tetgenio.pointmarkerlist = new int[numberOfPoints];
271 }
272#endif
273 if (sizingFunctionArray)
274 {
275 in_tetgenio.numberofpointmtrs = 1;
276 in_tetgenio.pointmtrlist = new REAL[numberOfPoints];
277 }
278
279 double point[3];
280 int i;
281 for (i=0; i<numberOfPoints; i++)
282 {
283 input->GetPoint(i,point);
284 in_tetgenio.pointlist[meshDimensionality * i + 0] = point[0];
285 in_tetgenio.pointlist[meshDimensionality * i + 1] = point[1];
286 in_tetgenio.pointlist[meshDimensionality * i + 2] = point[2];
287#if 0
288 if (pointMarkerArray)
289 {
290 in_tetgenio.pointmarkerlist[i] = static_cast<int>(pointMarkerArray->GetComponent(i,0));
291 }
292 else
293 {
294 in_tetgenio.pointmarkerlist[i] = 0;
295 }
296#endif
297 if (sizingFunctionArray)
298 {
299 in_tetgenio.pointmtrlist[i] = sizingFunctionArray->GetComponent(i,0);
300 if (in_tetgenio.pointmtrlist[i] == 0.0)
301 {
302 in_tetgenio.pointmtrlist[i] = VTK_VMTK_FLOAT_TOL;
303 }
304 }
305 }
306
307 vtkIdList* facetCellIds = vtkIdList::New();
308 vtkIdList* tetraCellIds = vtkIdList::New();
309
310 int numberOfCells = input->GetNumberOfCells();
311 for (i=0; i<numberOfCells; i++)
312 {
313 vtkCell* cell = input->GetCell(i);
314 int cellType = cell->GetCellType();
315 switch (cellType)
316 {
317 case VTK_TRIANGLE:
318 case VTK_QUADRATIC_TRIANGLE:
319 case VTK_POLYGON:
320 facetCellIds->InsertNextId(i);
321 break;
322 case VTK_TETRA:
323 if (this->Order != 1)
324 {
325 vtkErrorMacro(<<"Element of incorrect order found.");
326 break;
327 }
328 tetraCellIds->InsertNextId(i);
329 break;
330 case VTK_QUADRATIC_TETRA:
331 if (this->Order != 2)
332 {
333 vtkErrorMacro(<<"Element of incorrect order found.");
334 break;
335 }
336 tetraCellIds->InsertNextId(i);
337 break;
338 default:
339 vtkErrorMacro(<<"Invalid element found, cellId "<<i<<", cellType "<<cellType);
340 break;
341 }
342 }
343
344 int numberOfFacets = facetCellIds->GetNumberOfIds();
345 int numberOfTetras = tetraCellIds->GetNumberOfIds();
346
347 //TODO - input as vtkPointSet (point inside hole ([0],[1],[2]))
348 in_tetgenio.holelist = NULL; //REAL*
349 in_tetgenio.numberofholes = 0;
350 //TODO
351
352 if (numberOfFacets > 0)
353 {
354 in_tetgenio.numberoffacets = numberOfFacets;
355 in_tetgenio.facetlist = new tetgenio::facet[numberOfFacets];
356 in_tetgenio.facetmarkerlist = new int[numberOfFacets];
357
358 vtkDataArray* facetMarkerArray = input->GetCellData()->GetArray(this->CellEntityIdsArrayName);
359
360 for (i=0; i<numberOfFacets; i++)
361 {
362 vtkIdType npts;
363 const vtkIdType *pts;
364 input->GetCellPoints(facetCellIds->GetId(i),npts,pts);
365 in_tetgenio.facetlist[i].numberofpolygons = 1;
366 in_tetgenio.facetlist[i].polygonlist = new tetgenio::polygon[in_tetgenio.facetlist[i].numberofpolygons];
367 in_tetgenio.facetlist[i].numberofholes = 0;
368 in_tetgenio.facetlist[i].holelist = NULL;
369 in_tetgenio.facetlist[i].polygonlist[0].numberofvertices = npts;
370 in_tetgenio.facetlist[i].polygonlist[0].vertexlist = new int[npts];
371 for (int j=0; j<npts; j++)
372 {
373 in_tetgenio.facetlist[i].polygonlist[0].vertexlist[j] = pts[j];
374 }
375
376 if (facetMarkerArray)
377 {
378 in_tetgenio.facetmarkerlist[i] = static_cast<int>(facetMarkerArray->GetComponent(facetCellIds->GetId(i),0));
379 }
380 else
381 {
382 in_tetgenio.facetmarkerlist[i] = 0;
383 }
384 }
385 }
386
387 //TODO - all cell arrays except volume array
388 in_tetgenio.tetrahedronattributelist = NULL; //REAL*
389 in_tetgenio.numberoftetrahedronattributes = 0;
390 //TODO
391
392 if (numberOfTetras > 0)
393 {
394 vtkDataArray* tetrahedronVolumeArray = NULL;
395 if (this->VarVolume)
396 {
397 tetrahedronVolumeArray = input->GetCellData()->GetArray(this->TetrahedronVolumeArrayName);
398 }
399
400 in_tetgenio.numberoftetrahedra = numberOfTetras;
401 switch (this->Order)
402 {
403 case 1:
404 in_tetgenio.numberofcorners = 4;
405 break;
406 case 2:
407 in_tetgenio.numberofcorners = 10;
408 break;
409 default:
410 in_tetgenio.numberofcorners = 0;
411 break;
412 }
413
414 in_tetgenio.tetrahedronlist = new int[in_tetgenio.numberofcorners * numberOfTetras];
415
416 if (tetrahedronVolumeArray)
417 {
418 in_tetgenio.tetrahedronvolumelist = new REAL[numberOfTetras];
419 }
420
421 for (i=0; i<numberOfTetras; i++)
422 {
423 vtkIdType npts;
424 const vtkIdType *pts;
425 input->GetCellPoints(tetraCellIds->GetId(i),npts,pts);
426 for (int j=0; j<npts; j++)
427 {
428 in_tetgenio.tetrahedronlist[i*in_tetgenio.numberofcorners + j] = pts[j];
429 }
430
431 if (tetrahedronVolumeArray)
432 {
433 in_tetgenio.tetrahedronvolumelist[i] = tetrahedronVolumeArray->GetComponent(i,0);
434 }
435 }
436 }
437
438 facetCellIds->Delete();
439 tetraCellIds->Delete();
440
441 //TODO - input as vtkPointSet + arrays for attributes (point inside region ([0],[1],[2]), regional attribute [3], volume constraint [4])
442 in_tetgenio.regionlist = NULL; //REAL*
443 in_tetgenio.numberofregions = 0;
444 //TODO
445
446 char tetgenOptions[512];
447 strcpy(tetgenOptions,tetgenOptionString.c_str());
448 std::cout<<"TetGen command line options: "<<tetgenOptions<<endl;
449 try
450 {
451 tetrahedralize(tetgenOptions,&in_tetgenio,&out_tetgenio);
452 }
453 catch ( ... )
454 {
455 vtkErrorMacro(<<"TetGen quit with an exception.");
456 this->LastRunExitStatus = 1;
457 return 1;
458 }
459
460 this->LastRunExitStatus = 0;
461
462 //TODO
463// out_tetgenio.edgelist; //int*
464// out_tetgenio.edgemarkerlist; //int*
465// out_tetgenio.numberofedges;
466// out_tetgenio.neighborlist; //int*
467 //TODO
468
469 vtkPoints* outputPoints = vtkPoints::New();
470
471 outputPoints->SetNumberOfPoints(out_tetgenio.numberofpoints);
472 for (i=0; i<out_tetgenio.numberofpoints; i++)
473 {
474 point[0] = out_tetgenio.pointlist[meshDimensionality * i + 0];
475 point[1] = out_tetgenio.pointlist[meshDimensionality * i + 1];
476 point[2] = out_tetgenio.pointlist[meshDimensionality * i + 2];
477 outputPoints->SetPoint(i,point);
478 }
479
480 output->SetPoints(outputPoints);
481
482 vtkCellArray* outputCellArray = vtkCellArray::New();
483
484 int numberOfOutputCells = 0;
485
486 if (this->OutputVolumeElements)
487 {
488 numberOfOutputCells += out_tetgenio.numberoftetrahedra;
489 }
490
491 if (this->OutputSurfaceElements)
492 {
493 numberOfOutputCells += out_tetgenio.numberoftrifaces;
494 }
495
496 int* outputCellTypes = new int[numberOfOutputCells];
497
498 vtkIntArray* outputCellMarkerArray = vtkIntArray::New();
499 outputCellMarkerArray->SetNumberOfTuples(numberOfOutputCells);
500 outputCellMarkerArray->SetName(this->CellEntityIdsArrayName);
501 outputCellMarkerArray->FillComponent(0,0.0);
502
503 int cellIdOffset = 0;
504
505#if 1
506 if (this->OutputVolumeElements)
507 {
508 int outputCellType;
509 switch (this->Order)
510 {
511 case 1:
512 outputCellType = VTK_TETRA;
513 break;
514 case 2:
515 outputCellType = VTK_QUADRATIC_TETRA;
516 break;
517 default:
518 outputCellType = VTK_TETRA;
519 }
520
521 for (i=0; i<out_tetgenio.numberoftetrahedra; i++)
522 {
523 outputCellArray->InsertNextCell(out_tetgenio.numberofcorners);
524 for (int j=0; j<out_tetgenio.numberofcorners; j++)
525 {
526 outputCellArray->InsertCellPoint(out_tetgenio.tetrahedronlist[i*out_tetgenio.numberofcorners + j]);
527 }
528 outputCellTypes[cellIdOffset+i] = outputCellType;
529 }
530
531 cellIdOffset = out_tetgenio.numberoftetrahedra;
532 }
533
534 if (this->OutputSurfaceElements)
535 {
536 int outputCellType = VTK_TRIANGLE;
537 int numberOfTrifaceCorners = 3;
538
539 for (i=0; i<out_tetgenio.numberoftrifaces; i++)
540 {
541 outputCellArray->InsertNextCell(numberOfTrifaceCorners);
542 for (int j=0; j<numberOfTrifaceCorners; j++)
543 {
544 outputCellArray->InsertCellPoint(out_tetgenio.trifacelist[i*numberOfTrifaceCorners + j]);
545 }
546 outputCellTypes[cellIdOffset+i] = outputCellType;
547 outputCellMarkerArray->SetValue(cellIdOffset+i,out_tetgenio.trifacemarkerlist[i]);
548 }
549
550 cellIdOffset = out_tetgenio.numberoftrifaces;
551 }
552#else
553 if (this->OutputSurfaceElements)
554 {
555 int outputCellType = VTK_TRIANGLE;
556 int numberOfTrifaceCorners = 3;
557
558 for (i=0; i<out_tetgenio.numberoftrifaces; i++)
559 {
560 outputCellArray->InsertNextCell(numberOfTrifaceCorners);
561 for (int j=0; j<numberOfTrifaceCorners; j++)
562 {
563 outputCellArray->InsertCellPoint(out_tetgenio.trifacelist[i*numberOfTrifaceCorners + j]);
564 }
565 outputCellTypes[cellIdOffset+i] = outputCellType;
566 outputCellMarkerArray->SetValue(cellIdOffset+i,out_tetgenio.trifacemarkerlist[i]+1);
567 }
568
569 cellIdOffset = out_tetgenio.numberoftrifaces;
570 }
571
572 if (this->OutputVolumeElements)
573 {
574 int outputCellType;
575 switch (this->Order)
576 {
577 case 1:
578 outputCellType = VTK_TETRA;
579 break;
580 case 2:
581 outputCellType = VTK_QUADRATIC_TETRA;
582 break;
583 default:
584 outputCellType = VTK_TETRA;
585 }
586
587 for (i=0; i<out_tetgenio.numberoftetrahedra; i++)
588 {
589 outputCellArray->InsertNextCell(out_tetgenio.numberofcorners);
590 for (int j=0; j<out_tetgenio.numberofcorners; j++)
591 {
592 outputCellArray->InsertCellPoint(out_tetgenio.tetrahedronlist[i*out_tetgenio.numberofcorners + j]);
593 }
594 outputCellTypes[cellIdOffset+i] = outputCellType;
595 }
596
597 cellIdOffset = out_tetgenio.numberoftetrahedra;
598 }
599#endif
600
601 output->SetCells(outputCellTypes,outputCellArray);
602
603 output->GetCellData()->AddArray(outputCellMarkerArray);
604
605 outputPoints->Delete();
606 outputCellArray->Delete();
607 delete[] outputCellTypes;
608
609 outputCellMarkerArray->Delete();
610
611 return 1;
612}
613
614void vtkvmtkTetGenWrapper::PrintSelf(std::ostream& os, vtkIndent indent)
615{
616 this->Superclass::PrintSelf(os,indent);
617}
Wrapped tetgen volumetric meshing utility.
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void PrintSelf(std::ostream &os, vtkIndent indent) override
virtual void SetLastRunExitStatus(int)
#define VTK_VMTK_FLOAT_TOL
vtkStandardNewMacro(vtkvmtkTetGenWrapper)