VMTK
Loading...
Searching...
No Matches
vtkvmtkTetGenWriter.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: vtkvmtkTetGenWriter.cxx
5Language: C++
6Date: Sat Feb 19 22:47:34 CET 2011
7Version: Revision: 1.0
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 "vtkvmtkTetGenWriter.h"
23#include "vtkUnstructuredGrid.h"
24#include "vtkTetra.h"
25#include "vtkCellType.h"
26#include "vtkCell.h"
27#include "vtkCellData.h"
28#include "vtkIntArray.h"
29#include "vtkIdTypeArray.h"
30#include "vtkMath.h"
31#include "vtkObjectFactory.h"
32#include "vtkvmtkConstants.h"
33
34
36
41
43{
44 if (this->BoundaryDataArrayName)
45 {
46 delete[] this->BoundaryDataArrayName;
47 this->BoundaryDataArrayName = NULL;
48 }
49}
50
52{
53 vtkUnstructuredGrid *input= vtkUnstructuredGrid::SafeDownCast(this->GetInput());
54
55 if (!this->GetFileName())
56 {
57 vtkErrorMacro(<<"FileName not set.");
58 return;
59 }
60
61 std::string nodeFileName = this->GetFileName();
62 nodeFileName += ".node";
63
64 std::string eleFileName = this->GetFileName();
65 eleFileName += ".ele";
66
67 std::ofstream nodeStream(nodeFileName.c_str());
68 std::ofstream eleStream(eleFileName.c_str());
69
70 if (!nodeStream.good())
71 {
72 vtkErrorMacro(<<"Could not open node file for writing.");
73 return;
74 }
75
76 if (!eleStream.good())
77 {
78 vtkErrorMacro(<<"Could not open ele file for writing.");
79 return;
80 }
81
82 input->BuildLinks();
83
84 int numberOfPoints = input->GetNumberOfPoints();
85
86 int i, j;
87
88 //TODO: add attributes and boundary markers
89
90 nodeStream << numberOfPoints << " 3 0 0" << std::endl;
91
92 double point[3];
93 for (i=0; i<numberOfPoints; i++)
94 {
95 input->GetPoint(i,point);
96 nodeStream << i+1 << " " << point[0] << " " << point[1] << " " << point[2] << std::endl;
97 }
98
99#if 0
100 vtkIntArray* boundaryDataArray = vtkIntArray::New();
101 if (this->BoundaryDataArrayName)
102 {
103 if (input->GetCellData()->GetArray(this->BoundaryDataArrayName))
104 {
105 boundaryDataArray->DeepCopy(input->GetCellData()->GetArray(this->BoundaryDataArrayName));
106 }
107 else
108 {
109 vtkErrorMacro(<<"BoundaryDataArray with name specified does not exist");
110 boundaryDataArray->Delete();
111 return;
112 }
113 }
114 else
115 {
116 boundaryDataArray->SetNumberOfValues(numberOfCells);
117 boundaryDataArray->FillComponent(0,0.0);
118 }
119#endif
120
121 vtkIdTypeArray* tetraCellIdArray = vtkIdTypeArray::New();
122 input->GetIdsOfCellsOfType(VTK_TETRA,tetraCellIdArray);
123 int numberOfTetras = tetraCellIdArray->GetNumberOfTuples();
124
125 vtkIdTypeArray* quadraticTetraCellIdArray = vtkIdTypeArray::New();
126 input->GetIdsOfCellsOfType(VTK_QUADRATIC_TETRA,quadraticTetraCellIdArray);
127 int numberOfQuadraticTetras = quadraticTetraCellIdArray->GetNumberOfTuples();
128
129 int pointsInTet = 4;
130 vtkIdTypeArray* tetIdsArray = tetraCellIdArray;
131 int numberOfOutputTetras = tetraCellIdArray->GetNumberOfTuples();
132
133 if (numberOfQuadraticTetras > numberOfTetras)
134 {
135 pointsInTet = 10;
136 tetIdsArray = quadraticTetraCellIdArray;
137 numberOfOutputTetras = quadraticTetraCellIdArray->GetNumberOfTuples();
138 }
139
140 //TODO: add attributes
141
142 eleStream << numberOfOutputTetras << " " << pointsInTet << " 0" << std::endl;
143
144 double point0[3], point1[3], point2[3], point3[3];
145 double cross[3], vector01[3], vector21[3], vector31[3];
146 double dot;
147 int tmp;
148
149 int cellPointIds[10];
150 for (i=0; i<numberOfOutputTetras; i++)
151 {
152 vtkIdType cellId = tetraCellIdArray->GetValue(i);
153 vtkCell* cell = input->GetCell(cellId);
154 for (j=0; j<pointsInTet; j++)
155 {
156 cellPointIds[j] = cell->GetPointId(j);
157 }
158 input->GetPoint(cellPointIds[0],point0);
159 input->GetPoint(cellPointIds[1],point1);
160 input->GetPoint(cellPointIds[2],point2);
161 input->GetPoint(cellPointIds[3],point3);
162 vector01[0] = point0[0] - point1[0];
163 vector01[1] = point0[1] - point1[1];
164 vector01[2] = point0[2] - point1[2];
165 vector21[0] = point2[0] - point1[0];
166 vector21[1] = point2[1] - point1[1];
167 vector21[2] = point2[2] - point1[2];
168 vector31[0] = point3[0] - point1[0];
169 vector31[1] = point3[1] - point1[1];
170 vector31[2] = point3[2] - point1[2];
171 vtkMath::Cross(vector21,vector31,cross);
172 dot = vtkMath::Dot(cross,vector01);
173 if (dot < 0.0)
174 {
175 tmp = cellPointIds[2];
176 cellPointIds[2] = cellPointIds[3];
177 cellPointIds[3] = tmp;
178 if (pointsInTet == 10)
179 {
180 tmp = cellPointIds[6];
181 cellPointIds[6] = cellPointIds[7];
182 cellPointIds[7] = tmp;
183 tmp = cellPointIds[5];
184 cellPointIds[5] = cellPointIds[8];
185 cellPointIds[8] = tmp;
186 }
187 }
188
189 eleStream << i+1 << " ";
190 for (j=0; j<pointsInTet; j++)
191 {
192 eleStream << cellPointIds[j]+1 << " ";
193 }
194 eleStream << std::endl;
195 }
196
197 tetraCellIdArray->Delete();
198 quadraticTetraCellIdArray->Delete();
199}
200
201void vtkvmtkTetGenWriter::PrintSelf(std::ostream& os, vtkIndent indent)
202{
203 vtkUnstructuredGridWriter::PrintSelf(os,indent);
204}
Writes TetGen .msh files.
void PrintSelf(std::ostream &os, vtkIndent indent) override
vtkStandardNewMacro(vtkvmtkTetGenWriter)