iMSTK
Interactive Medical Simulation Toolkit
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
imstkMeshIO.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkMeshIO.h"
8 #include "imstkAssimpMeshIO.h"
9 #include "imstkLogger.h"
10 #include "imstkMshMeshIO.h"
11 #include "imstkSurfaceMesh.h"
12 #include "imstkTetrahedralMesh.h"
13 #include "imstkVegaMeshIO.h"
14 #include "imstkVTKMeshIO.h"
15 
16 #include <cctype>
17 #include <sys/stat.h>
18 #include <unordered_map>
19 
20 namespace imstk
21 {
22 static std::unordered_map<std::string, MeshFileType> extToType =
23 {
24  { "vtk", MeshFileType::VTK },
25  { "vtp", MeshFileType::VTP },
26  { "vtu", MeshFileType::VTU },
27  { "obj", MeshFileType::OBJ },
28  { "stl", MeshFileType::STL },
29  { "ply", MeshFileType::PLY },
30  { "dae", MeshFileType::DAE },
31  { "fbx", MeshFileType::FBX },
32  { "3ds", MeshFileType::_3DS },
33  { "veg", MeshFileType::VEG },
34  { "msh", MeshFileType::MSH },
35  { "dcm", MeshFileType::DCM },
36  { "nrrd", MeshFileType::NRRD },
37  { "nii", MeshFileType::NII },
38  { "mhd", MeshFileType::MHD },
39  { "jpg", MeshFileType::JPG },
40  { "jpeg", MeshFileType::JPG },
41  { "png", MeshFileType::PNG },
42  { "bmp", MeshFileType::BMP },
43 };
44 
45 std::shared_ptr<PointSet>
46 MeshIO::read(const std::string& filePath)
47 {
48  bool isDirectory = false;
49  bool exists = fileExists(filePath, isDirectory);
50 
51  CHECK(exists && !isDirectory) << "File " << filePath << " doesn't exist or is a directory.";
52 
53  MeshFileType meshType = MeshIO::getFileType(filePath);
54  switch (meshType)
55  {
56  case MeshFileType::VTK:
57  case MeshFileType::VTU:
58  case MeshFileType::VTP:
59  case MeshFileType::STL:
60  case MeshFileType::PLY:
61  case MeshFileType::NRRD:
62  case MeshFileType::NII:
63  case MeshFileType::DCM:
64  case MeshFileType::MHD:
65  case MeshFileType::JPG:
66  case MeshFileType::PNG:
67  case MeshFileType::BMP:
68  return VTKMeshIO::read(filePath, meshType);
69  break;
70  case MeshFileType::OBJ:
71  case MeshFileType::DAE:
72  case MeshFileType::FBX:
73  case MeshFileType::_3DS:
74  return AssimpMeshIO::read(filePath, meshType);
75  break;
76  case MeshFileType::VEG:
77  return VegaMeshIO::read(filePath, meshType);
78  break;
79  case MeshFileType::MSH:
80  return MshMeshIO::read(filePath);
81  break;
82  case MeshFileType::UNKNOWN:
83  default:
84  break;
85  }
86  LOG(FATAL) << "Error: file type not supported for input " << filePath;
87  return nullptr;
88 }
89 
90 bool
91 MeshIO::fileExists(const std::string& file, bool& isDirectory)
92 {
93  struct stat buf;
94  if (stat(file.c_str(), &buf) == 0)
95  {
96  if (buf.st_mode & S_IFDIR)
97  {
98  isDirectory = true;
99  }
100  else
101  {
102  isDirectory = false;
103  }
104  return true;
105  }
106  else
107  {
108  return false;
109  }
110 }
111 
112 const MeshFileType
113 MeshIO::getFileType(const std::string& filePath)
114 {
115  std::string extString = filePath.substr(filePath.find_last_of(".") + 1);
116 
117  CHECK(!extString.empty()) << "Error: invalid file name " << filePath;
118 
119  // To lowercase
120  std::transform(extString.begin(), extString.end(), extString.begin(),
121  [](unsigned char c) { return static_cast<unsigned char>(std::tolower(c)); });
122 
123  if (extToType.count(extString) == 0)
124  {
125  LOG(FATAL) << "Error: unknown file extension " << extString;
126  }
127 
128  return extToType[extString];
129 }
130 
131 bool
132 MeshIO::write(const std::shared_ptr<imstk::PointSet> imstkMesh, const std::string& filePath)
133 {
134  if (imstkMesh == nullptr)
135  {
136  LOG(WARNING) << "Error: Mesh object supplied is not valid!";
137  return false;
138  }
139 
140  MeshFileType meshType = MeshIO::getFileType(filePath);
141  switch (meshType)
142  {
143  case MeshFileType::VEG:
144  return VegaMeshIO::write(imstkMesh, filePath, meshType);
145  break;
146  case MeshFileType::NII:
147  case MeshFileType::NRRD:
148  case MeshFileType::VTU:
149  case MeshFileType::VTK:
150  case MeshFileType::VTP:
151  case MeshFileType::STL:
152  case MeshFileType::PLY:
153  case MeshFileType::OBJ:
154  case MeshFileType::MHD:
155  case MeshFileType::BMP:
156  case MeshFileType::PNG:
157  case MeshFileType::JPG:
158  return VTKMeshIO::write(imstkMesh, filePath, meshType);
159  break;
160  case MeshFileType::UNKNOWN:
161  default:
162  break;
163  }
164 
165  LOG(FATAL) << "Error: file type not supported for input " << filePath;
166  return false;
167 }
168 } // namespace imstk
static std::shared_ptr< PointSet > read(const std::string &filePath, MeshFileType type)
Ensures file can be read and reads it if possible.
MeshFileType
Enumeration the mesh file type.
Definition: imstkMeshIO.h:19
static std::shared_ptr< PointSet > read(const std::string &filePath)
Read and generate a volumetric mesh given a external msh file.
Compound Geometry.
static bool write(const std::shared_ptr< PointSet > imstkMesh, const std::string &filePath, const MeshFileType meshType)
Writes the given mesh to the specified file path.
static std::shared_ptr< PointSet > read(const std::string &filePath)
Read external file.
Definition: imstkMeshIO.cpp:46
static const MeshFileType getFileType(const std::string &filePath)
Returns the type of the file.
static bool write(const std::shared_ptr< imstk::PointSet > imstkMesh, const std::string &filePath, const MeshFileType meshType)
Write a volumetric mesh in vega file format.
static std::shared_ptr< PointSet > read(const std::string &filePath, MeshFileType meshType)
Read and generate volumetric mesh given a external vega mesh file.
static bool fileExists(const std::string &file, bool &isDirectory)
Returns true if the file exists, else false. Also sets isDirectory to true if the path is a directory...
Definition: imstkMeshIO.cpp:91
static bool write(const std::shared_ptr< imstk::PointSet > imstkMesh, const std::string &filePath)
Write external file.