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;
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
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);
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
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
169 vtkIdList* boundaryFaceIds = vtkIdList::New();
170
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
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}
static void GetDolfinCellConnectivity(vtkCell *cell, vtkIdList *dolfinConnectivity)
static void GetDolfinCellFaceOrder(vtkCell *cell, vtkIdList *dolfinFaceOrder)