Wildmeshing Toolkit
CachedMultiMesh.cpp
Go to the documentation of this file.
1 // #include <fmt/format.h> // duplicated in logger.hpp
2 #include <wmtk/Mesh.hpp>
3 #include <wmtk/io/MeshReader.hpp>
4 
5 #include <wmtk/utils/Logger.hpp>
6 
7 #include "CachedMultiMesh.hpp"
8 
9 namespace wmtk::io {
10 
11 
13  const std::string& name,
14  std::map<std::string, std::vector<int64_t>> multimesh_names,
15  std::shared_ptr<Mesh> root)
16  : m_name(name)
17  , m_root(root)
18  , m_multimesh_names(std::move(multimesh_names))
19 {}
21 
22 
23 void CachedMultiMesh::load(const std::filesystem::path& path)
24 {
25  load(wmtk::read_mesh(path));
26 }
27 
28 void CachedMultiMesh::load(std::shared_ptr<Mesh> root)
29 {
30  m_root = root;
31 }
33 {
34  m_root = nullptr;
35 }
36 // passed in mesh.uv
37 std::shared_ptr<Mesh> CachedMultiMesh::get_from_path(const std::string& name)
38 {
39  auto dot_it = name.find('.');
40  // double checks that this starts with //mesh.
41  assert(name.substr(0, dot_it) == m_name);
42  if (dot_it == std::string::npos) {
43  return get_root();
44  }
45 
46  // extracts uv and calls get
47  return get(name.substr(dot_it + 1));
48 }
49 
50 
51 std::shared_ptr<Mesh> CachedMultiMesh::get(const std::string& name)
52 {
53  if (m_multimesh_names.find(name) == m_multimesh_names.end()) {
54  wmtk::logger().warn("Cannot find {} in the cache", name);
55  return nullptr;
56  }
57  return m_root->get_multi_mesh_mesh(m_multimesh_names.at(name)).shared_from_this();
58 }
59 std::shared_ptr<Mesh> CachedMultiMesh::get_root()
60 {
61  return m_root;
62 }
63 namespace {
64 const static std::vector<int64_t> root_id = {};
65 }
66 
67 const std::vector<int64_t>& CachedMultiMesh::get_id(const std::string& name) const
68 {
69  if (name.empty()) {
70  return root_id;
71  }
72  if (!name.empty() && m_multimesh_names.find(name) == m_multimesh_names.end()) {
73  throw std::runtime_error(fmt::format(
74  "Could not find named multimesh {} in {}. Key {} was not among [{}]",
75  name,
76  m_name,
77  name,
78  "hi"));
79  }
80  return root_id;
81 }
82 const std::vector<int64_t>& CachedMultiMesh::get_id_from_path(const std::string& name) const
83 {
84  const auto idx = name.find('.');
85  // double checks that this starts with //mesh.
86  assert(name.substr(0, idx) == m_name);
87 
88  if (idx == std::string::npos) {
89  return get_id("");
90  }
91  const std::string subname = name.substr(idx + 1);
92  // extracts uv and calls get
93  return get_id(subname);
94 }
95 
96 const std::map<std::string, std::vector<int64_t>>& CachedMultiMesh::get_multimesh_names() const
97 {
98  return m_multimesh_names;
99 }
100 
103 
104 } // namespace wmtk::io
const std::map< std::string, std::vector< int64_t > > & get_multimesh_names() const
std::map< std::string, std::vector< int64_t > > m_multimesh_names
std::shared_ptr< Mesh > get_from_path(const std::string &name)
std::shared_ptr< Mesh > get(const std::string &name)
void load(const std::filesystem::path &path)
CachedMultiMesh(const std::string &name, std::map< std::string, std::vector< int64_t >> multimesh_names, std::shared_ptr< Mesh > root=nullptr)
CachedMultiMesh & operator=(CachedMultiMesh &&)
const std::vector< int64_t > & get_id(const std::string &name) const
const std::vector< int64_t > & get_id_from_path(const std::string &name) const
std::shared_ptr< Mesh > get_root()
std::shared_ptr< Mesh > m_root
Definition: autodiff.h:995
spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:58
std::shared_ptr< Mesh > read_mesh(const std::filesystem::path &filename, const bool ignore_z_if_zero, const std::vector< std::string > &tetrahedron_attributes)
Definition: MeshReader.cpp:11