1
// Copyright (C) 2003-2006 Anders Logg.
2
// Licensed under the GNU LGPL Version 2.1.
4
// First added: 2003-10-21
5
// Last changed: 2006-06-22
7
#include <dolfin/dolfin_log.h>
8
#include <dolfin/CellType.h>
9
#include <dolfin/Mesh.h>
10
#include <dolfin/XMLMesh.h>
12
using namespace dolfin;
14
//-----------------------------------------------------------------------------
15
XMLMesh::XMLMesh(Mesh& mesh) : XMLObject(), _mesh(mesh), state(OUTSIDE)
19
//-----------------------------------------------------------------------------
24
//-----------------------------------------------------------------------------
25
void XMLMesh::startElement(const xmlChar *name, const xmlChar **attrs)
31
if ( xmlStrcasecmp(name, (xmlChar *) "mesh") == 0 )
33
readMesh(name, attrs);
41
if ( xmlStrcasecmp(name, (xmlChar *) "vertices") == 0 )
43
readVertices(name, attrs);
44
state = INSIDE_VERTICES;
46
else if ( xmlStrcasecmp(name, (xmlChar *) "cells") == 0 )
48
readCells(name, attrs);
56
if ( xmlStrcasecmp(name, (xmlChar *) "vertex") == 0 )
57
readVertex(name, attrs);
63
if ( xmlStrcasecmp(name, (xmlChar *) "interval") == 0 )
64
readInterval(name, attrs);
65
else if ( xmlStrcasecmp(name, (xmlChar *) "triangle") == 0 )
66
readTriangle(name, attrs);
67
else if ( xmlStrcasecmp(name, (xmlChar *) "tetrahedron") == 0 )
68
readTetrahedron(name, attrs);
76
//-----------------------------------------------------------------------------
77
void XMLMesh::endElement(const xmlChar *name)
83
if ( xmlStrcasecmp(name, (xmlChar *) "mesh") == 0 )
93
if ( xmlStrcasecmp(name, (xmlChar *) "vertices") == 0 )
100
if ( xmlStrcasecmp(name, (xmlChar *) "cells") == 0 )
109
//-----------------------------------------------------------------------------
110
void XMLMesh::open(std::string filename)
114
//-----------------------------------------------------------------------------
115
bool XMLMesh::close()
117
return state == DONE;
119
//-----------------------------------------------------------------------------
120
void XMLMesh::readMesh(const xmlChar *name, const xmlChar **attrs)
123
std::string type = parseString(name, attrs, "celltype");
124
uint gdim = parseUnsignedInt(name, attrs, "dim");
126
// Create cell type to get topological dimension
127
CellType* cell_type = CellType::create(type);
128
uint tdim = cell_type->dim();
131
// Open mesh for editing
132
editor.open(_mesh, CellType::string2type(type), tdim, gdim);
134
//-----------------------------------------------------------------------------
135
void XMLMesh::readVertices(const xmlChar *name, const xmlChar **attrs)
138
uint num_vertices = parseUnsignedInt(name, attrs, "size");
140
// Set number of vertices
141
editor.initVertices(num_vertices);
143
//-----------------------------------------------------------------------------
144
void XMLMesh::readCells(const xmlChar *name, const xmlChar **attrs)
147
uint num_cells = parseUnsignedInt(name, attrs, "size");
149
// Set number of vertices
150
editor.initCells(num_cells);
152
//-----------------------------------------------------------------------------
153
void XMLMesh::readVertex(const xmlChar *name, const xmlChar **attrs)
156
uint v = parseUnsignedInt(name, attrs, "index");
158
// Handle differently depending on geometric dimension
159
switch ( _mesh.geometry().dim() )
163
real x = parseReal(name, attrs, "x");
164
editor.addVertex(v, x);
169
real x = parseReal(name, attrs, "x");
170
real y = parseReal(name, attrs, "y");
171
editor.addVertex(v, x, y);
176
real x = parseReal(name, attrs, "x");
177
real y = parseReal(name, attrs, "y");
178
real z = parseReal(name, attrs, "z");
179
editor.addVertex(v, x, y, z);
183
error("Dimension of mesh must be 1, 2 or 3.");
186
//-----------------------------------------------------------------------------
187
void XMLMesh::readInterval(const xmlChar *name, const xmlChar **attrs)
190
if ( _mesh.topology().dim() != 1 )
191
error("Mesh entity (interval) does not match dimension of mesh (%d).",
192
_mesh.topology().dim());
195
uint c = parseUnsignedInt(name, attrs, "index");
196
uint v0 = parseUnsignedInt(name, attrs, "v0");
197
uint v1 = parseUnsignedInt(name, attrs, "v1");
200
editor.addCell(c, v0, v1);
202
//-----------------------------------------------------------------------------
203
void XMLMesh::readTriangle(const xmlChar *name, const xmlChar **attrs)
206
if ( _mesh.topology().dim() != 2 )
207
error("Mesh entity (triangle) does not match dimension of mesh (%d).",
208
_mesh.topology().dim());
211
uint c = parseUnsignedInt(name, attrs, "index");
212
uint v0 = parseUnsignedInt(name, attrs, "v0");
213
uint v1 = parseUnsignedInt(name, attrs, "v1");
214
uint v2 = parseUnsignedInt(name, attrs, "v2");
217
editor.addCell(c, v0, v1, v2);
219
//-----------------------------------------------------------------------------
220
void XMLMesh::readTetrahedron(const xmlChar *name, const xmlChar **attrs)
223
if ( _mesh.topology().dim() != 3 )
224
error("Mesh entity (tetrahedron) does not match dimension of mesh (%d).",
225
_mesh.topology().dim());
228
uint c = parseUnsignedInt(name, attrs, "index");
229
uint v0 = parseUnsignedInt(name, attrs, "v0");
230
uint v1 = parseUnsignedInt(name, attrs, "v1");
231
uint v2 = parseUnsignedInt(name, attrs, "v2");
232
uint v3 = parseUnsignedInt(name, attrs, "v3");
235
editor.addCell(c, v0, v1, v2, v3);
237
//-----------------------------------------------------------------------------
238
void XMLMesh::closeMesh()
242
//-----------------------------------------------------------------------------