Read Mesh#
Synopsis#
Read a mesh and display the Euclidean distance in between 2 given vertices
Results#
Distance:
0.604093
Code#
C++#
#include "itkMesh.h"
#include "itkMeshFileReader.h"
#include "itkMeshFileWriter.h"
int
main(int argc, char * argv[])
{
if ((argc != 2) && (argc != 4))
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0];
std::cerr << " <InputFileName> <id 1 (optional)> <id 2 (optional)>";
std::cerr << std::endl;
return EXIT_FAILURE;
}
constexpr unsigned int Dimension = 3;
using CoordinateType = double;
using MeshType = itk::Mesh<CoordinateType, Dimension>;
using ReaderType = itk::MeshFileReader<MeshType>;
auto reader = ReaderType::New();
reader->SetFileName(argv[1]);
reader->Update();
MeshType::Pointer mesh = reader->GetOutput();
if (argc == 4)
{
MeshType::PointIdentifier id1 = std::stoi(argv[2]);
MeshType::PointIdentifier id2 = std::stoi(argv[3]);
MeshType::PointType p = mesh->GetPoint(id1);
MeshType::PointType q = mesh->GetPoint(id2);
std::cout << p.EuclideanDistanceTo(q) << std::endl;
}
return EXIT_SUCCESS;
}
Classes demonstrated#
-
template<typename TOutputMesh, typename ConvertPointPixelTraits = MeshConvertPixelTraits<typename TOutputMesh::PixelType>, class ConvertCellPixelTraits = MeshConvertPixelTraits<typename TOutputMesh::CellPixelType>>
class MeshFileReader : public itk::MeshSource<TOutputMesh> Mesh source that reads mesh data from a single file.
This source object is a general filter to read data from a variety of file formats. It works with a MeshIOBase subclass to actually do the reading of the data. Object factory machinery can be used to automatically create the MeshIOBase, or the MeshIOBase can be manually created and set.
TOutputMesh is the type expected by the external users of the filter. If data stored in the file is stored in a different format then specified by TOutputMesh, than this filter converts data between the file type and the external expected type. The ConvertTraits template argument is used to do the conversion.
A Pluggable factory pattern is used this allows different kinds of readers to be registered (even at run time) without having to modify the code in this class. Normally just setting the FileName with the appropriate suffix is enough to get the reader to instantiate the correct MeshIO and read the file properly. However, some files have no accepted suffix, so you will have to manually create the MeshIO instance of the write type.
- See
MeshIOBase
- Author
Wanlin Zhu. Uviversity of New South Wales, Australia.
- ITK Sphinx Examples: