Wildmeshing Toolkit
read_mesh.cpp
Go to the documentation of this file.
1 #include "read_mesh.hpp"
2 #include "HDF5Reader.hpp"
3 #include "MshReader.hpp"
4 
5 #include <wmtk/utils/Logger.hpp>
6 
7 namespace wmtk::io {
8 namespace utils {
9 FileType guess_file_type_from_extension(const std::string_view& suffix)
10 {
11  if (suffix == ".hdf5") {
12  return FileType::HDF5;
13  } else if (suffix == ".msh") {
14  return FileType::Msh;
15  } else {
16  return FileType::Auto;
17  }
18 }
19 
20 FileType guess_file_type(const std::filesystem::path& filename)
21 {
22  const auto extension = filename.extension().string();
23 
24  const FileType ret = guess_file_type_from_extension(extension);
25  if (ret == FileType::Auto) {
27  "Automatic file type detection could not identify type of file {} with extension of {}",
28  filename.string(),
29  extension);
30  }
31  return ret;
32 }
33 
34 } // namespace utils
35 std::shared_ptr<Mesh> read_mesh(const std::filesystem::path& filename, FileType file_type)
36 {
37  if (file_type == FileType::Auto) {
38  file_type = utils::guess_file_type(filename);
39  }
40  switch (file_type) {
41  case FileType::HDF5: {
42  HDF5Reader reader;
43  return reader.read(filename);
44  }
45  case FileType::Msh: {
46  MshReader reader;
47  return reader.read(filename);
48  }
49  default:
50  case FileType::Auto: {
51  assert(false); // get_file_type should have caught failure to obtain right ifle type already
52  }
53  }
54  return {};
55 }
56 std::shared_ptr<Mesh> read_mesh(
57  const std::filesystem::path& filename,
58  const std::vector<std::vector<std::string>>& retrieved_attributes,
59  FileType file_type)
60 {
61  if (file_type == FileType::Auto) {
62  file_type = utils::guess_file_type(filename);
63  }
64  switch (file_type) {
65  case FileType::HDF5: {
66  HDF5Reader reader;
67  return reader.read(filename);
68  }
69  case FileType::Msh: {
70  MshReader reader;
71  return reader.read(filename, -1, retrieved_attributes);
72  }
73  default:
74  case FileType::Auto: {
75  assert(false); // get_file_type should have caught failure to obtain right ifle type already
76  }
77  }
78  return {};
79 }
80 
81 std::shared_ptr<Mesh> read_mesh(
82  const std::filesystem::path& filename,
83  const bool ignore_z_if_zero,
84  const std::vector<std::string>& tetrahedron_attributes,
85  FileType file_type)
86 {
87  if (file_type == FileType::Auto) {
88  file_type = utils::guess_file_type(filename);
89  }
90  switch (file_type) {
91  case FileType::HDF5: {
92  HDF5Reader reader;
93  return reader.read(filename);
94  }
95  case FileType::Msh: {
96  MshReader reader;
97  return reader.read(filename, ignore_z_if_zero, tetrahedron_attributes);
98  }
99  default:
100  case FileType::Auto: {
101  assert(false); // get_file_type should have caught failure to obtain right ifle type already
102  }
103  }
104  return {};
105 }
106 } // namespace wmtk::io
std::shared_ptr< Mesh > read(const std::filesystem::path &filename)
Definition: HDF5Reader.cpp:44
std::shared_ptr< Mesh > read(const std::filesystem::path &filename, const bool ignore_z_if_zero, const std::vector< std::string > &extra_facet_attributes={})
Definition: MshReader.cpp:38
FileType guess_file_type(const std::filesystem::path &filename)
Definition: read_mesh.cpp:20
FileType guess_file_type_from_extension(const std::string_view &suffix)
Definition: read_mesh.cpp:9
std::shared_ptr< Mesh > read_mesh(const std::filesystem::path &filename, FileType file_type)
Definition: read_mesh.cpp:35
void log_and_throw_error(const std::string &msg)
Definition: Logger.cpp:101