Reading in a mesh¶
Applications commonly need to read a mesh file from disk. Quest provides the
STLReader
class, which can read binary or ASCII STL files, as well as the
PSTLReader
class for use in parallel codes. STL (stereolithography)
is a common file format for triangle surface meshes. The STL reader classes
will read the file from disk and build a mint::Mesh
object. Quest also
provides the ProEReader
class, for ASCII Pro/E files containing tetrahedra,
and the PProEReader
class for use in parallel codes. PTC Creo is a modeling
application formerly known as Pro/ENGINEER, and its file format is in use among
Axom’s users.
Reading an STL file¶
The code examples are excerpts from the file <axom>/src/tools/mesh_tester.cpp
.
We include the STL reader header
#include "axom/quest/readers/STLReader.hpp"
and also the mint Mesh and UnstructuredMesh headers.
#include "axom/mint/mesh/Mesh.hpp"
#include "axom/mint/mesh/UnstructuredMesh.hpp"
For convenience, we use typedefs in the axom namespace.
using namespace axom;
using UMesh = mint::UnstructuredMesh<mint::SINGLE_SHAPE>;
The following example shows usage of the STLReader class:
// Read file
SLIC_INFO("Reading file: '" << params.stlInput << "'...\n");
quest::STLReader* reader = new quest::STLReader();
reader->setFileName(params.stlInput);
reader->read();
// Get surface mesh
UMesh* surface_mesh = new UMesh(3, mint::TRIANGLE);
reader->getMesh(surface_mesh);
// Delete the reader
delete reader;
reader = nullptr;
SLIC_INFO("Mesh has " << surface_mesh->getNumberOfNodes() << " vertices and "
<< surface_mesh->getNumberOfCells() << " triangles.");
After reading the STL file, the STLReader::getMesh
method gives access to the
underlying mesh data. The reader may then be deleted.
Reading a Pro/E file¶
As read by Axom, an ASCII Pro/E tet file contains:
Zero or more comment lines starting with a
#
characterOne line with two integers: the number of nodes
n
and the number of tetrahedrat
n
lines, one for each node; each line contains a contiguous integer ID starting at 1 and three floating-point numbers specifying the node locationt
lines, one for each tetrahedron; each line contains a contiguous integer ID starting at 1 and four integers specifying the tet’s nodes
Reading an ASCII Pro/E tet file is similar to reading an STL file. The code
examples are excerpts from the file <axom>/src/axom/quest/examples/quest_proe_bbox.cpp
.
The Pro/E reader has the ability to read a subset of the mesh in the file,
defined by a user-supplied predicate function. The example code shows how
to use a convenience function to specify a predicate that keeps only tets
fully included in a user-supplied bounding box.
We include the ProEReader header
#include "axom/quest/readers/ProEReader.hpp"
and also the mint Mesh and UnstructuredMesh headers.
#include "axom/mint/mesh/Mesh.hpp"
#include "axom/mint/mesh/UnstructuredMesh.hpp"
#include "axom/mint/utils/vtk_utils.hpp" // for write_vtk
For convenience, we specify some type aliases.
namespace mint = axom::mint;
namespace primal = axom::primal;
namespace slic = axom::slic;
using IndexType = axom::IndexType;
using UMesh = mint::UnstructuredMesh<mint::SINGLE_SHAPE>;
The following example shows how to use the ProEReader class.
Calling reader.setTetPredFromBoundingBox(bbox, false)
, as shown in the
code, makes a tetrahedron predicate that accepts tets with all four nodes
falling in bbox
and rejects others. Alternately, the user can specify
an arbitrary predicate function with setTetPred()
. If the user specifies
no tetrahedron predicate, the reader reads all tets in the file.
// Read file
axom::quest::ProEReader reader;
reader.setFileName(args.file_name);
// Set up a bounding box to keep only certain tets.
// bbox_min and bbox_max are pointers to double.
axom::quest::ProEReader::BBox3D bbox;
bbox.addPoint(axom::quest::ProEReader::Point3D {bbox_min, 3});
bbox.addPoint(axom::quest::ProEReader::Point3D {bbox_max, 3});
// Keep only tets with all four nodes inside the bounding box.
reader.setTetPredFromBoundingBox(bbox, false);
// Pass true as the second argument of setTetPredFromBoundingBox() to
// keep tets with at least one node inside the bounding box.
// To keep all tets, do not set a TetPred.
// Read in the file.
reader.read();
// Get surface mesh
UMesh mesh(3, axom::mint::TET);
reader.getMesh(&mesh);
After reading the Pro/E file, the ProEReader::getMesh
method gives access
to the underlying mesh data. The reader may then be deleted.