VMTK
Loading...
Searching...
No Matches
vtkvmtkFluentWriter.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkFluentWriter.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:47:47 $
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
22// #include <fstream>
23
24#include "vtkvmtkFluentWriter.h"
25#include "vtkUnstructuredGrid.h"
26#include "vtkTetra.h"
27#include "vtkCellType.h"
28#include "vtkCell.h"
29#include "vtkCellData.h"
30#include "vtkIntArray.h"
31#include "vtkIdTypeArray.h"
32#include "vtkMath.h"
33#include "vtkObjectFactory.h"
34#include "vtkvmtkConstants.h"
35
36
38
43
45{
46 if (this->BoundaryDataArrayName)
47 {
48 delete[] this->BoundaryDataArrayName;
49 this->BoundaryDataArrayName = NULL;
50 }
51}
52
53void vtkvmtkFluentWriter::ConvertFaceToLeftHanded(vtkUnstructuredGrid* input, vtkIdType tetraCellId, vtkIdType& id0, vtkIdType& id1, vtkIdType& id2)
54{
55 vtkTetra* tetra = vtkTetra::SafeDownCast(input->GetCell(tetraCellId));
56 vtkIdList* cellPointIds = tetra->GetPointIds();
57 vtkIdType id3 = -1;
58 vtkIdType tmpId = -1;
59 int k;
60 for (k=0; k<4; k++)
61 {
62 tmpId = cellPointIds->GetId(k);
63 if (tmpId != id0 && tmpId != id1 && tmpId != id2)
64 {
65 id3 = tmpId;
66 break;
67 }
68 }
69
70 bool reverse = false;
71 double point0[3], point1[3], point2[3], point3[3];
72 double vector0[3], vector1[3], vector2[3];
73 double cross[3];
74 input->GetPoint(id0,point0);
75 input->GetPoint(id1,point1);
76 input->GetPoint(id2,point2);
77 input->GetPoint(id3,point3);
78 vector0[0] = point1[0] - point0[0];
79 vector0[1] = point1[1] - point0[1];
80 vector0[2] = point1[2] - point0[2];
81 vector1[0] = point2[0] - point0[0];
82 vector1[1] = point2[1] - point0[1];
83 vector1[2] = point2[2] - point0[2];
84 vector2[0] = point3[0] - point0[0];
85 vector2[1] = point3[1] - point0[1];
86 vector2[2] = point3[2] - point0[2];
87 vtkMath::Cross(vector0,vector1,cross);
88 if (vtkMath::Dot(cross,vector2) < 0.0)
89 {
90 reverse = true;
91 }
92 if (reverse)
93 {
94 tmpId = id2;
95 id2 = id1;
96 id1 = tmpId;
97 }
98}
99
101{
102 vtkUnstructuredGrid *input= vtkUnstructuredGrid::SafeDownCast(this->GetInput());
103
104 int numberOfCells = input->GetNumberOfCells();
105 for (int i=0; i<numberOfCells; i++)
106 {
107 int cellType = input->GetCellType(i);
108 if (cellType != VTK_TRIANGLE && cellType != VTK_TETRA)
109 {
110 vtkErrorMacro(<<"Only triangles and linear tetrahedra supported.");
111 }
112 }
113
114 if (!this->GetFileName())
115 {
116 vtkErrorMacro(<<"FileName not set.");
117 return;
118 }
119
120 std::ofstream out (this->GetFileName());
121
122 if (!out.good())
123 {
124 vtkErrorMacro(<<"Could not open file for writing.");
125 return;
126 }
127
128 input->BuildLinks();
129
130 int numberOfPoints = input->GetNumberOfPoints();
131
132 vtkIntArray* boundaryDataArray = vtkIntArray::New();
133 if (this->BoundaryDataArrayName)
134 {
135 if (input->GetCellData()->GetArray(this->BoundaryDataArrayName))
136 {
137 boundaryDataArray->DeepCopy(input->GetCellData()->GetArray(this->BoundaryDataArrayName));
138 }
139 else
140 {
141 vtkErrorMacro(<<"BoundaryDataArray with name specified does not exist");
142 boundaryDataArray->Delete();
143 return;
144 }
145 }
146 else
147 {
148 boundaryDataArray->SetNumberOfValues(numberOfCells);
149 boundaryDataArray->FillComponent(0,0.0);
150 }
151
152 vtkIdTypeArray* tetraCellIdArray = vtkIdTypeArray::New();
153 input->GetIdsOfCellsOfType(VTK_TETRA,tetraCellIdArray);
154 int numberOfTetras = tetraCellIdArray->GetNumberOfTuples();
155
156 vtkIdList* tetraCellIdMap = vtkIdList::New();
157 tetraCellIdMap->SetNumberOfIds(numberOfCells);
158
159 int i;
160
161 for (i=0; i<numberOfCells; i++)
162 {
163 tetraCellIdMap->SetId(i,-1);
164 }
165
166 for (i=0; i<numberOfTetras; i++)
167 {
168 tetraCellIdMap->SetId(tetraCellIdArray->GetValue(i),i);
169 }
170
171 vtkIdTypeArray* triangleCellIdArray = vtkIdTypeArray::New();
172 input->GetIdsOfCellsOfType(VTK_TRIANGLE,triangleCellIdArray);
173 int numberOfTriangles = triangleCellIdArray->GetNumberOfTuples();
174
175// out << "(0 \"Fluent file generated by the Vascular Modeling Toolkit - vmtk.github.io\" )" << endl;
176 out << "(0 \"GAMBIT to Fluent File\")" << endl;
177 out << "(0 \"Dimension:\")" << endl;
178 out << "(2 3)" << endl;
179 out << endl;
180
181 char str[200];
182
183 sprintf(str,"(10 (0 1 %x 1 3))",numberOfPoints);
184 out << str << endl;
185 sprintf(str,"(10 (1 1 %x 1 3)(",numberOfPoints);
186 out << str << endl;
187
188 out.precision(6);
189
190 double point[3];
191 for (i=0; i<numberOfPoints; i++)
192 {
193 input->GetPoint(i,point);
194 sprintf(str," %17.10e %17.10e %17.10e",point[0],point[1],point[2]);
195 out << str << endl;
196 }
197 out << " ))" << endl << endl;
198
199 out << "(0 \"Faces:\")" << endl;
200
201 int numberOfInteriorFaces = 2*numberOfTetras - numberOfTriangles/2;
202
203 sprintf(str,"(13 (0 1 %x 0))",numberOfInteriorFaces+numberOfTriangles);
204 out << str << endl;
205
206 int faceOffset = 1;
207
208 vtkIdList* boundaryDataNumberOfTriangles = vtkIdList::New();
209 int boundaryDataRange = static_cast<int>(boundaryDataArray->GetRange()[1]);
210 boundaryDataNumberOfTriangles->SetNumberOfIds(boundaryDataRange+1);
211 for (i=0; i<boundaryDataRange+1; i++)
212 {
213 boundaryDataNumberOfTriangles->SetId(i,0);
214 }
215 int boundaryDataValue, value;
216 for (i=0; i<numberOfTriangles; i++)
217 {
218 vtkIdType triangleCellId = triangleCellIdArray->GetValue(i);
219 boundaryDataValue = boundaryDataArray->GetValue(triangleCellId);
220 value = boundaryDataNumberOfTriangles->GetId(boundaryDataValue);
221 boundaryDataNumberOfTriangles->SetId(boundaryDataValue,value+1);
222 }
223
224 vtkIdList* neighborCellIds = vtkIdList::New();
225 const int entityOffset = 3;
226 int entityId = entityOffset;
227 int n;
228 for (n=0; n<boundaryDataRange+1; n++)
229 {
230 int numberOfBoundaryTriangles = boundaryDataNumberOfTriangles->GetId(n);
231 if (numberOfBoundaryTriangles == 0)
232 {
233 continue;
234 }
235 //sprintf(str,"(13 (%x %x %x %x 0)(",entityId,faceOffset,faceOffset+numberOfBoundaryTriangles-1,entityId);
236 sprintf(str,"(13 (%x %x %x 3 0)(",entityId,faceOffset,faceOffset+numberOfBoundaryTriangles-1);
237 entityId++;
238 out << str << endl;
239 for (i=0; i<numberOfTriangles; i++)
240 {
241 vtkIdType triangleCellId = triangleCellIdArray->GetValue(i);
242 if (boundaryDataArray->GetValue(triangleCellId) != n)
243 {
244 continue;
245 }
246 vtkIdList* cellPointIds = input->GetCell(triangleCellId)->GetPointIds();
247 vtkIdType id0 = cellPointIds->GetId(0);
248 vtkIdType id1 = cellPointIds->GetId(1);
249 vtkIdType id2 = cellPointIds->GetId(2);
250 input->GetCellNeighbors(triangleCellId,cellPointIds,neighborCellIds);
251 vtkIdType tetraCellId = neighborCellIds->GetId(0);
252 this->ConvertFaceToLeftHanded(input,tetraCellId,id0,id1,id2);
253 sprintf(str," 3 %x %x %x %x 0",(int)id0+1,(int)id1+1,(int)id2+1,(int)tetraCellId+1);
254 out << str << endl;
255 }
256 out << "))" << endl << endl;
257 faceOffset += numberOfBoundaryTriangles;
258 }
259
260 sprintf(str,"(13 (%x %x %x 2 0)(",(int)entityId,faceOffset,faceOffset+numberOfInteriorFaces-1);
261 out << str << endl;
262
263//TODO: loop over tets and write interior faces.
264//one space, #points on the face, pid1, pid2, pid3, tetraid1, tetraid2
265 for (i=0; i<numberOfTetras; i++)
266 {
267 vtkIdType tetraCellId = tetraCellIdArray->GetValue(i);
268 vtkTetra* tetra = vtkTetra::SafeDownCast(input->GetCell(tetraCellId));
269 int j;
270 for (j=0; j<4; j++)
271 {
272 vtkCell* face = tetra->GetFace(j);
273 vtkIdList* facePointIds = face->GetPointIds();
274 vtkIdType id0 = facePointIds->GetId(0);
275 vtkIdType id1 = facePointIds->GetId(1);
276 vtkIdType id2 = facePointIds->GetId(2);
277 input->GetCellNeighbors(tetraCellId,facePointIds,neighborCellIds);
278 if (neighborCellIds->GetNumberOfIds() != 1)
279 {
280 continue;
281 }
282 if (input->GetCellType(neighborCellIds->GetId(0)) != VTK_TETRA)
283 {
284 continue;
285 }
286 if (neighborCellIds->GetId(0) < tetraCellId)
287 {
288 continue;
289 }
290 this->ConvertFaceToLeftHanded(input,tetraCellId,id0,id1,id2);
291 sprintf(str," 3 %x %x %x %x %x",(int)id0+1,(int)id1+1,(int)id2+1,(int)tetraCellIdMap->GetId(tetraCellId)+1,(int)tetraCellIdMap->GetId(neighborCellIds->GetId(0))+1);
292 out << str << endl;
293 }
294 }
295 out << "))" << endl << endl;
296 faceOffset += numberOfInteriorFaces;
297
298 neighborCellIds->Delete();
299
300 out << "(0 \"Cells:\")" << endl;
301 sprintf(str,"(12 (0 1 %x 0))",numberOfTetras);
302 out << str << endl;
303 sprintf(str,"(12 (2 1 %x 1 2))",numberOfTetras);
304 out << str << endl << endl;
305
306 out << "(0 \"Zones:\")" << endl;
307 out << "(45 (2 fluid blood)())" << endl;
308 int numberOfBoundaryTriangles = 0;
309 entityId = entityOffset;
310 for (n=0; n<boundaryDataRange+1; n++)
311 {
312 numberOfBoundaryTriangles = boundaryDataNumberOfTriangles->GetId(n);
313 if (numberOfBoundaryTriangles == 0)
314 {
315 continue;
316 }
317 sprintf(str,"(45 (%x wall surface%d)())",entityId,entityId);
318 entityId++;
319 out << str << endl;
320 }
321 sprintf(str,"(45 (%x interior default-interior)())",entityId);
322 out << str << endl;
323
324 boundaryDataNumberOfTriangles->Delete();
325 triangleCellIdArray->Delete();
326 tetraCellIdArray->Delete();
327 tetraCellIdMap->Delete();
328}
329
330void vtkvmtkFluentWriter::PrintSelf(std::ostream& os, vtkIndent indent)
331{
332 vtkUnstructuredGridWriter::PrintSelf(os,indent);
333}
Writes Fluent .msh files.
void PrintSelf(std::ostream &os, vtkIndent indent) override
void ConvertFaceToLeftHanded(vtkUnstructuredGrid *input, vtkIdType tetraCellId, vtkIdType &id0, vtkIdType &id1, vtkIdType &id2)
vtkStandardNewMacro(vtkvmtkFluentWriter)