VMTK
Loading...
Searching...
No Matches
vtkvmtkDolfinWriter2.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkDolfinWriter2.cxx,v $
5Language: C++
6Date: $$
7Version: $$
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
27// #include <fstream>
28
30#include "vtkUnstructuredGrid.h"
31#include "vtkCellType.h"
32#include "vtkCell.h"
33#include "vtkCellData.h"
34#include "vtkIdTypeArray.h"
35#include "vtkObjectFactory.h"
36#include "vtkSortDataArray.h"
37#include "vtkTetra.h"
38#include "vtkvmtkConstants.h"
39#include <iostream>
40
41
43
49
58
60{
61 vtkUnstructuredGrid *input= vtkUnstructuredGrid::SafeDownCast(this->GetInput());
62
63 if (!this->FileName)
64 {
65 vtkErrorMacro(<<"FileName not set.");
66 return;
67 }
68
69 std::ofstream out (this->FileName);
70
71 if (!out.good())
72 {
73 vtkErrorMacro(<<"Could not open file for writing.");
74 return;
75 }
76
77 input->BuildLinks();
78
79 int numberOfPoints = input->GetNumberOfPoints();
80 int numberOfCells = input->GetNumberOfCells();
81
82 vtkIdTypeArray* cellEntityIdsArray = NULL;
83 if (this->CellEntityIdsArrayName)
84 {
85 if (input->GetCellData()->GetArray(this->CellEntityIdsArrayName))
86 {
87 cellEntityIdsArray = vtkIdTypeArray::New();
88 cellEntityIdsArray->DeepCopy(input->GetCellData()->GetArray(this->CellEntityIdsArrayName));
89 }
90 else
91 {
92 vtkErrorMacro(<<"BoundaryDataArray with name specified does not exist");
93 }
94 }
95
96 vtkIdTypeArray* tetraCellIdArray = vtkIdTypeArray::New();
97 input->GetIdsOfCellsOfType(VTK_TETRA,tetraCellIdArray);
98 int numberOfTetras = tetraCellIdArray->GetNumberOfTuples();
99
100 out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl << endl;
101 out << "<dolfin xmlns:dolfin=\"http://www.phi.chalmers.se/dolfin/\">" << endl;
102 out << " <mesh celltype=\"tetrahedron\" dim=\"3\">" << endl;
103 out << " <vertices size=\""<< numberOfPoints << "\">" << endl;
104
105 int i;
106 double point[3];
107 for (i=0; i<numberOfPoints; i++)
108 {
109 input->GetPoint(i,point);
110 out << " <vertex index=\"" << i << "\" ";
111 out << "x=\"" << point[0] << "\" ";
112 out << "y=\"" << point[1] << "\" ";
113 out << "z=\"" << point[2] << "\" ";
114 out << "/>" <<endl;
115 }
116
117 out << " </vertices>" << endl;
118 out << " <cells size=\"" << numberOfTetras << "\">" << endl;
119
120 vtkIdList* dolfinConnectivity = vtkIdList::New();
121// this->GetDolfinConnectivity(VTK_TETRA,dolfinConnectivity);
122
123 vtkIdList* volumeCellIdMap = vtkIdList::New();
124 volumeCellIdMap->SetNumberOfIds(numberOfCells);
125 int numberOfCellPoints = 4;
126 int k;
127
128 for (i=0; i<numberOfTetras; i++)
129 {
130 vtkIdType tetraCellId = tetraCellIdArray->GetValue(i);
131 volumeCellIdMap->SetId(tetraCellId,i);
132 vtkCell* tetra = input->GetCell(tetraCellId);
133 this->GetDolfinCellConnectivity(tetra,dolfinConnectivity);
134 vtkIdList* cellPointIds = input->GetCell(tetraCellId)->GetPointIds();
135
136 out << " <tetrahedron index=\"" << i << "\" ";
137 for (k=0; k<numberOfCellPoints; k++)
138 {
139 out << "v" << k << "=\"" << cellPointIds->GetId(dolfinConnectivity->GetId(k)) << "\" ";
140 }
141 out << "/>" << endl;
142 }
143
144 out << " </cells>" << endl;
145
146 if (cellEntityIdsArray)
147 {
148
149
150 out << " <data>" << endl;
151
152 //Set the id of the tetrahedra
153 out << " <array name=\"region indicators\" type=\"uint\" size=\"" << numberOfTetras << "\">" << endl;
154
155 for (i=0; i<numberOfTetras; i++)
156 {
157 vtkIdType tetraCellId = tetraCellIdArray->GetValue(i);
158 out << " <element index=\"" << i << "\" ";
159 out << "value=\"" << cellEntityIdsArray->GetValue(tetraCellId) + this->CellEntityIdsOffset << "\" ";
160 out << "/>" << endl;
161 }
162 out << " </array>" << endl;
163
164 vtkIdTypeArray* triangleCellIdArray = vtkIdTypeArray::New();
165 input->GetIdsOfCellsOfType(VTK_TRIANGLE,triangleCellIdArray);
166 int numberOfTriangles = triangleCellIdArray->GetNumberOfTuples();
167 vtkIdList* boundaryFaceCells = vtkIdList::New();
168// boundaryFaceCells->SetNumberOfIds(numberOfTriangles);
169 vtkIdList* boundaryFaceIds = vtkIdList::New();
170// boundaryFaceIds->SetNumberOfIds(numberOfTriangles);
171 vtkIdList* boundaryFaceIndicators = vtkIdList::New();
172
173 vtkIdType triangleCellId;
174 for (i=0; i<numberOfTriangles; i++)
175 {
176 triangleCellId = triangleCellIdArray->GetValue(i);
177 vtkIdList* faceCellPoints = vtkIdList::New();
178 input->GetCellPoints(triangleCellId,faceCellPoints);
179
180 vtkIdList* cellIds = vtkIdList::New();
181 input->GetCellNeighbors(triangleCellId,faceCellPoints,cellIds);
182 vtkIdType nNeighbors = cellIds->GetNumberOfIds();
183
184 for (int j=0; j<nNeighbors; j++)
185 {
186 vtkIdType cellId = cellIds->GetId(j);
187 vtkCell* cell = input->GetCell(cellId);
188 int cellType = cell->GetCellType();
189
190 if (cellType != VTK_TETRA)
191 {
192 vtkErrorMacro(<<"Volume cell adjacent to triangle is not tetrahedron (volume cell id: "<<cellId <<") and it is unsupported by Dolfin. Skipping face.");
193 continue;
194 }
195
196
197 int numberOfFaces = cell->GetNumberOfFaces();
198 vtkIdType faceId = -1;
199 int k;
200 for (k=0; k<numberOfFaces; k++)
201 {
202 vtkCell* face = cell->GetFace(k);
203 vtkIdList* matchingPointIds = vtkIdList::New();
204 matchingPointIds->DeepCopy(face->GetPointIds());
205 matchingPointIds->IntersectWith(faceCellPoints);
206 int numberOfNonMatching = face->GetNumberOfPoints() - matchingPointIds->GetNumberOfIds();
207 matchingPointIds->Delete();
208
209 if (numberOfNonMatching==0)
210 {
211 faceId = k;
212 break;
213 }
214 }
215
216 vtkIdList* dolfinFaceOrder = vtkIdList::New();
217// this->GetDolfinFaceOrder(cellType,dolfinFaceOrder);
218 this->GetDolfinCellFaceOrder(cell,dolfinFaceOrder);
219 vtkIdType dolfinFaceId = dolfinFaceOrder->GetId(faceId);
220 dolfinFaceOrder->Delete();
221
222 boundaryFaceCells->InsertNextId(volumeCellIdMap->GetId(cellId));
223 boundaryFaceIds->InsertNextId(dolfinFaceId);
224 boundaryFaceIndicators->InsertNextId(cellEntityIdsArray->GetValue(triangleCellId) + this->CellEntityIdsOffset);
225 }
226
227 faceCellPoints->Delete();
228 cellIds->Delete();
229 }
230
231 vtkIdType numberOfBoundaryFacets = boundaryFaceCells->GetNumberOfIds();
232
233
234 out << " <array name=\"boundary facet cells\" type=\"uint\" size=\"" << numberOfBoundaryFacets << "\">" << endl;
235 for (i=0; i<numberOfBoundaryFacets; i++)
236 {
237 out << " <element index=\"" << i << "\" ";
238 out << "value=\"" << boundaryFaceCells->GetId(i) << "\" ";
239 out << "/>" << endl;
240 }
241 out << " </array>" << endl;
242
243 out << " <array name=\"boundary facet numbers\" type=\"uint\" size=\"" << numberOfBoundaryFacets << "\">" << endl;
244 for (i=0; i<numberOfBoundaryFacets; i++)
245 {
246 out << " <element index=\"" << i << "\" ";
247 out << "value=\"" << boundaryFaceIds->GetId(i) << "\" ";
248 out << "/>" << endl;
249 }
250 out << " </array>" << endl;
251
252 out << " <array name=\"boundary indicators\" type=\"uint\" size=\"" << numberOfBoundaryFacets << "\">" << endl;
253 for (i=0; i<numberOfBoundaryFacets; i++)
254 {
255 out << " <element index=\"" << i << "\" ";
256 out << "value=\"" << boundaryFaceIndicators->GetId(i) << "\" ";
257 out << "/>" << endl;
258 }
259 out << " </array>" << endl;
260
261 out << " </data>" << endl;
262
263 triangleCellIdArray->Delete();
264 boundaryFaceCells->Delete();
265 boundaryFaceIds->Delete();
266 boundaryFaceIndicators->Delete();
267 }
268
269 out << " </mesh>" << endl;
270 out << "</dolfin>" << endl;
271
272 tetraCellIdArray->Delete();
273 dolfinConnectivity->Delete();
274 volumeCellIdMap->Delete();
275
276 if (cellEntityIdsArray)
277 {
278 cellEntityIdsArray->Delete();
279 }
280}
281
282
283
284
285void vtkvmtkDolfinWriter2::GetDolfinConnectivity(int cellType, vtkIdList* dolfinConnectivity)
286{
287 dolfinConnectivity->Initialize();
288
289 switch(cellType)
290 {
291 case VTK_TETRA:
292 dolfinConnectivity->SetNumberOfIds(4);
293 dolfinConnectivity->SetId(0,0);
294 dolfinConnectivity->SetId(1,1);
295 dolfinConnectivity->SetId(2,2);
296 dolfinConnectivity->SetId(3,3);
297 break;
298 default:
299 std::cerr<<"Element type not currently supported in dolfin. Skipping element for connectivity."<<std::endl;
300 break;
301 }
302}
303
304void vtkvmtkDolfinWriter2::GetDolfinFaceOrder(int cellType, vtkIdList* dolfinFaceOrder)
305{
306 dolfinFaceOrder->Initialize();
307
308 switch(cellType)
309 {
310 case VTK_TETRA:
311 dolfinFaceOrder->SetNumberOfIds(4);
312 dolfinFaceOrder->SetId(0,2);
313 dolfinFaceOrder->SetId(1,0);
314 dolfinFaceOrder->SetId(2,1);
315 dolfinFaceOrder->SetId(3,3);
316 break;
317 default:
318 std::cerr<<"Element type not currently supported in dolfin. Skipping element for face ordering."<<std::endl;
319 break;
320 }
321}
322
323void vtkvmtkDolfinWriter2::GetDolfinCellConnectivity(vtkCell* cell, vtkIdList* dolfinConnectivity)
324{
325 dolfinConnectivity->Initialize();
326 int cellType = cell->GetCellType();
327 vtkIdTypeArray* pointIds = vtkIdTypeArray::New();
328 switch(cellType)
329 {
330 case VTK_TETRA:
331 pointIds->SetNumberOfTuples(4);
332 pointIds->SetValue(0,cell->GetPointId(0));
333 pointIds->SetValue(1,cell->GetPointId(1));
334 pointIds->SetValue(2,cell->GetPointId(2));
335 pointIds->SetValue(3,cell->GetPointId(3));
336 dolfinConnectivity->SetNumberOfIds(4);
337 dolfinConnectivity->SetId(0,0);
338 dolfinConnectivity->SetId(1,1);
339 dolfinConnectivity->SetId(2,2);
340 dolfinConnectivity->SetId(3,3);
341 vtkSortDataArray::Sort(pointIds,dolfinConnectivity);
342 break;
343 default:
344 std::cerr<<"Element type not currently supported in dolfin. Skipping element for connectivity."<<std::endl;
345 break;
346 }
347 pointIds->Delete();
348}
349
350
351void vtkvmtkDolfinWriter2::GetDolfinCellFaceOrder(vtkCell* cell, vtkIdList *dolfinFaceOrder)
352{
353 dolfinFaceOrder->Initialize();
354
355 int cellType = cell->GetCellType();
356
357 vtkIdList* dolfinConnectivity = vtkIdList::New();
358 GetDolfinCellConnectivity(cell,dolfinConnectivity);
359 vtkIdList* inverseDolfinConnectivity = vtkIdList::New();
360 inverseDolfinConnectivity->SetNumberOfIds(dolfinConnectivity->GetNumberOfIds());
361 for (int i=0; i<dolfinConnectivity->GetNumberOfIds(); i++)
362 {
363 inverseDolfinConnectivity->SetId(dolfinConnectivity->GetId(i),i);
364 }
365
366
367 const vtkIdType *faceArray;
368 switch(cellType)
369 {
370 case VTK_TETRA:
371 dolfinFaceOrder->SetNumberOfIds(4);
372 for (int i=0; i<4; i++)
373 {
374 faceArray = vtkTetra::GetFaceArray(i);
375 for (int j=0; j<4;j++)
376 {
377 bool inFaceArray = false;
378 for (int k=0;k<3;k++)
379 {
380 if (inverseDolfinConnectivity->GetId(faceArray[k])==j)
381 {
382 inFaceArray = true;
383 }
384 }
385 if (!inFaceArray)
386 {
387 dolfinFaceOrder->SetId(i,j);
388 break;
389 }
390 }
391 }
392 break;
393 default:
394 std::cerr<<"Element type not currently supported in dolfin. Skipping element for face ordering."<<std::endl;
395 break;
396 }
397
398 dolfinConnectivity->Delete();
399}
400
401void vtkvmtkDolfinWriter2::PrintSelf(std::ostream& os, vtkIndent indent)
402{
403 vtkUnstructuredGridWriter::PrintSelf(os,indent);
404}
Provides a lightly modified version of vtkvmtkDolfinWriter, with bug fixes and a new region indicator...
static void GetDolfinCellConnectivity(vtkCell *cell, vtkIdList *dolfinConnectivity)
static void GetDolfinConnectivity(int cellType, vtkIdList *dolfinConnectivity)
static void GetDolfinFaceOrder(int cellType, vtkIdList *dolfinFaceOrder)
static void GetDolfinCellFaceOrder(vtkCell *cell, vtkIdList *dolfinFaceOrder)
void PrintSelf(std::ostream &os, vtkIndent indent) override
vtkStandardNewMacro(vtkvmtkDolfinWriter2)