VMTK
Loading...
Searching...
No Matches
vtkvmtkDolfinWriter.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkDolfinWriter.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#include <cassert>
24#include <algorithm>
25
26#include "vtkvmtkDolfinWriter.h"
27#include "vtkUnstructuredGrid.h"
28#include "vtkCellType.h"
29#include "vtkCell.h"
30#include "vtkCellData.h"
31#include "vtkIdTypeArray.h"
32#include "vtkObjectFactory.h"
33#include "vtkvmtkConstants.h"
34
35
37
39{
40 this->BoundaryDataArrayName = NULL; // TODO: Rename to CellEntityIdsArrayName
41 this->BoundaryDataIdOffset = 0;
42 this->StoreCellMarkers = 0;
43}
44
46{
47 if (this->BoundaryDataArrayName)
48 {
49 delete[] this->BoundaryDataArrayName;
50 this->BoundaryDataArrayName = NULL;
51 }
52}
53
55{
56 // Open output file
57 if (!this->FileName)
58 {
59 vtkErrorMacro(<<"FileName not set.");
60 return;
61 }
62 std::ofstream out (this->FileName);
63 if (!out.good())
64 {
65 vtkErrorMacro(<<"Could not open file for writing.");
66 return;
67 }
68
69 // Get and prepare input mesh
70 vtkUnstructuredGrid *input = vtkUnstructuredGrid::SafeDownCast(this->GetInput());
71 input->BuildLinks();
72 const int numberOfPoints = input->GetNumberOfPoints();
73 const int numberOfCells = input->GetNumberOfCells();
74
75 // Create copy of cell entity ids array
76 vtkIdTypeArray* cellEntityIds = NULL;
77 if (this->BoundaryDataArrayName)
78 {
79 vtkDataArray * array = input->GetCellData()->GetArray(this->BoundaryDataArrayName);
80 if (array)
81 {
82 cellEntityIds = vtkIdTypeArray::New();
83 cellEntityIds->DeepCopy(array);
84 }
85 else
86 {
87 vtkErrorMacro(<<"Array with specified BoundaryDataArrayName does not exist");
88 }
89 }
90
91 // Build an array with the vtk cell ids of all tetrahedras
92 vtkIdTypeArray* tetraCellIdArray = vtkIdTypeArray::New();
93 input->GetIdsOfCellsOfType(VTK_TETRA, tetraCellIdArray);
94 const int numberOfTetras = tetraCellIdArray->GetNumberOfTuples();
95 const int numberOfTetraPoints = 4; // Points in a tetrahedron(!)
96
97 // Build the inverted array with mapping from contiguous tetrahedron numbering to vtk cell numbering
98 vtkIdList* volumeCellIdMap = vtkIdList::New();
99 volumeCellIdMap->SetNumberOfIds(numberOfCells);
100 for (int i=0; i<numberOfTetras; i++)
101 {
102 vtkIdType tetraCellId = tetraCellIdArray->GetValue(i);
103 volumeCellIdMap->SetId(tetraCellId,i);
104 }
105
106 // Write out dolfin mesh header
107 out << "<?xml version=\"1.0\"?>" << endl;
108 out << "<dolfin xmlns:dolfin=\"http://www.fenicsproject.org\">" << endl;
109 out << " <mesh celltype=\"tetrahedron\" dim=\"3\">" << endl;
110
111 // Write out all vertices
112 out << " <vertices size=\""<< numberOfPoints << "\">" << endl;
113 for (int i=0; i<numberOfPoints; i++)
114 {
115 double point[3];
116 input->GetPoint(i,point);
117 out << " <vertex index=\"" << i << "\" ";
118 out << "x=\"" << point[0] << "\" ";
119 out << "y=\"" << point[1] << "\" ";
120 out << "z=\"" << point[2] << "\" ";
121 out << "/>" <<endl;
122 }
123 out << " </vertices>" << endl;
124
125 // Write out all cells
126 out << " <cells size=\"" << numberOfTetras << "\">" << endl;
127 for (int i=0; i<numberOfTetras; i++)
128 {
129 vtkIdType tetraCellId = tetraCellIdArray->GetValue(i);
130 vtkIdList* cellPointIds = input->GetCell(tetraCellId)->GetPointIds();
131
132 // Sort point ids in cell, the way dolfin likes it (this works only for simplices!)
133 vtkIdType dolfinCellPointIds[numberOfTetraPoints];
134 for (int k=0; k<numberOfTetraPoints; k++)
135 {
136 dolfinCellPointIds[k] = cellPointIds->GetId(k);
137 }
138 std::sort(dolfinCellPointIds, dolfinCellPointIds+numberOfTetraPoints);
139
140 // Write out vertex ids for a single tetrahedron
141 out << " <tetrahedron index=\"" << i << "\" ";
142 for (int k=0; k<numberOfTetraPoints; k++)
143 {
144 out << "v" << k << "=\"" << dolfinCellPointIds[k] << "\" ";
145 }
146 out << "/>" << endl;
147 }
148 out << " </cells>" << endl;
149
150 // Build and write subdomains if available
151 if (cellEntityIds)
152 {
153 vtkIdTypeArray* triangleCellIdArray = vtkIdTypeArray::New();
154 input->GetIdsOfCellsOfType(VTK_TRIANGLE,triangleCellIdArray);
155 const int numberOfTriangles = triangleCellIdArray->GetNumberOfTuples();
156
157 vtkIdList* triangleToTetrahedron = vtkIdList::New();
158 triangleToTetrahedron->SetNumberOfIds(numberOfTriangles);
159
160 vtkIdList* triangleToLocalFacetId = vtkIdList::New();
161 triangleToLocalFacetId->SetNumberOfIds(numberOfTriangles);
162
163 int interiorFacetsFound = 0;
164 int exteriorFacetsFound = 0;
165 for (int i=0; i<numberOfTriangles; i++)
166 {
167 const vtkIdType triangleCellId = triangleCellIdArray->GetValue(i);
168
169 vtkIdList* faceCellPoints = vtkIdList::New();
170 input->GetCellPoints(triangleCellId,faceCellPoints);
171
172 vtkIdList* cellIds = vtkIdList::New();
173 input->GetCellNeighbors(triangleCellId,faceCellPoints,cellIds);
174
175 if (cellIds->GetNumberOfIds() != 1)
176 {
177 interiorFacetsFound++;
178 }
179 else
180 {
181 exteriorFacetsFound++;
182 }
183
184 // Get neighbor cell to facet, pick the one with smallest index if two (interior facet)
185 vtkIdType cellId = cellIds->GetId(0);
186 if (cellIds->GetNumberOfIds() == 2 && cellIds->GetId(1) < cellId)
187 {
188 cellId = cellIds->GetId(1);
189 }
190 vtkCell* cell = input->GetCell(cellId);
191 int cellType = cell->GetCellType();
192 vtkIdList* cellPointIds = cell->GetPointIds();
193
194 // Check that all neighbor cells are tets
195 if (cellType != VTK_TETRA)
196 {
197 vtkErrorMacro(<<"Volume cell adjacent to triangle is not tetrahedron (volume cell id: "<<cellId <<") and it is unsupported by Dolfin. Skipping face.");
198 faceCellPoints->Delete();
199 cellIds->Delete();
200 continue;
201 }
202
203 // Sort point ids in cell, the way dolfin likes it (this works only for simplices!)
204 vtkIdType dolfinCellPointIds[numberOfTetraPoints];
205 for (int k=0; k<numberOfTetraPoints; k++)
206 {
207 dolfinCellPointIds[k] = cellPointIds->GetId(k);
208 }
209 std::sort(dolfinCellPointIds, dolfinCellPointIds+numberOfTetraPoints);
210
211 // Find local facet id in dolfin numbering, opposite of point in cell that is not part of facet
212 vtkIdType dolfinFaceId = -1;
213 for (int k=0; k<numberOfTetraPoints; k++)
214 {
215 bool found = false;
216 const int numberOfTrianglePoints = 3;
217 for (int j=0; j<numberOfTrianglePoints; j++)
218 {
219 if (dolfinCellPointIds[k] == faceCellPoints->GetId(j))
220 {
221 found = true;
222 break;
223 }
224 }
225 if (!found)
226 {
227 dolfinFaceId = k;
228 break;
229 }
230 }
231
232 // Store tetrahedron number for vtk triangle i
233 triangleToTetrahedron->SetId(i, volumeCellIdMap->GetId(cellId));
234 // Store local dolfin facet number for vtk triangle i
235 triangleToLocalFacetId->SetId(i, dolfinFaceId);
236
237 // Cleanup
238 faceCellPoints->Delete();
239 cellIds->Delete();
240 }
241
242 // Start subdomains section in file
243 if (numberOfTriangles || this->StoreCellMarkers)
244 {
245 out << " <domains>" << endl;
246 }
247
248 // Write facet subdomains
249 if (numberOfTriangles)
250 {
251 out << " <mesh_value_collection type=\"uint\" dim=\"2\" size=\""<< numberOfTriangles<< "\">" << endl;
252 for (int i=0; i<numberOfTriangles; i++)
253 {
254 const vtkIdType triangleCellId = triangleCellIdArray->GetValue(i);
255 const vtkIdType tetrahedronCellId = triangleToTetrahedron->GetId(i);
256 const vtkIdType value = cellEntityIds->GetValue(triangleCellId) + this->BoundaryDataIdOffset;
257 const vtkIdType localEntity = triangleToLocalFacetId->GetId(i);
258
259 out << " <value cell_index=\"" << tetrahedronCellId <<"\""
260 << " local_entity=\"" << localEntity << "\""
261 << " value=\"" << value << "\""
262 << " />" << endl;
263 }
264 out << " </mesh_value_collection>" << endl;
265 }
266
267 // Write cell subdomains
268 if (this->StoreCellMarkers)
269 {
270 out << " <mesh_value_collection type=\"uint\" dim=\"3\" size=\""<< numberOfTetras << "\">" << endl;
271 for (int i=0; i<numberOfTetras; i++)
272 {
273 const vtkIdType cellId = tetraCellIdArray->GetValue(i);
274 const vtkIdType value = cellEntityIds->GetValue(cellId);
275 out << " <value cell_index=\"" << i << "\""
276 << " local_entity=\"" << 0 << "\""
277 << " value=\"" << value << "\""
278 << " />" << endl;
279 }
280 out << " </mesh_value_collection>" << endl;
281 }
282
283 // End subdomains section in file
284 if (numberOfTriangles || this->StoreCellMarkers)
285 {
286 out << " </domains>" << endl;
287 }
288
289 if (interiorFacetsFound)
290 {
291 vtkWarningMacro("Found boundary cells not on boundary!");
292 }
293
294 triangleCellIdArray->Delete();
295 triangleToTetrahedron->Delete();
296 triangleToLocalFacetId->Delete();
297 }
298
299 out << " </mesh>" << endl;
300 out << "</dolfin>" << endl;
301
302 tetraCellIdArray->Delete();
303 volumeCellIdMap->Delete();
304
305 if (cellEntityIds)
306 {
307 cellEntityIds->Delete();
308 }
309}
310
311void vtkvmtkDolfinWriter::PrintSelf(std::ostream& os, vtkIndent indent)
312{
313 vtkUnstructuredGridWriter::PrintSelf(os,indent);
314}
Write mesh data in Dolfin file formats.
void PrintSelf(std::ostream &os, vtkIndent indent) override
vtkStandardNewMacro(vtkvmtkDolfinWriter)