Wildmeshing Toolkit
MeshCollection.cpp
Go to the documentation of this file.
1 
2 #include "MeshCollection.hpp"
3 #include <wmtk/Mesh.hpp>
4 #include <wmtk/utils/Logger.hpp>
6 
7 
9 
10 
12 {
13  auto mptr = std::make_unique<NamedMultiMesh>(std::move(m));
14  auto [it, did] = m_meshes.emplace(mptr->root_name(), std::move(mptr));
15 
16 
17  return *it->second;
18 }
19 
20 // NamedMultiMesh& MeshCollection::add_mesh(const InputOptions& opts)
21 //{
22 // return add_mesh(multimesh(opts));
23 // }
24 
25 
26 const Mesh& MeshCollection::get_mesh(const std::string_view& path) const
27 {
28  return get_named_multimesh(path).get_mesh(path);
29 }
30 
31 Mesh& MeshCollection::get_mesh(const std::string_view& path)
32 {
33  return get_named_multimesh(path).get_mesh(path);
34 }
35 bool MeshCollection::has_named_multimesh(const std::string_view& path) const
36 {
37  const std::string_view nmm_name = *internal::split_path(path).begin();
38  if (nmm_name.empty() && m_meshes.size() == 1) {
39  return true;
40  }
41  return m_meshes.find(nmm_name) != m_meshes.end();
42 }
43 bool MeshCollection::has_mesh(const std::string_view& path) const
44 {
45  if (!has_named_multimesh(path)) {
46  return false;
47  } else {
48  const auto& nmm = get_named_multimesh(path);
49  return nmm.has_mesh(path);
50  }
51 }
52 
53 const NamedMultiMesh& MeshCollection::get_named_multimesh(const std::string_view& path) const
54 {
55  assert(!m_meshes.empty());
56  using namespace std;
57 #if defined(WMTK_ENABLED_CPP20)
58  std::ranges::view auto split = internal::split_path(path);
59 #else
60  auto split = internal::split_path(path);
61 #endif
62  const auto nmm_name = *split.begin();
63  if (nmm_name.empty() && m_meshes.size() == 1) {
64  wmtk::logger().debug("MeshCollection accessed with an empty name, but has only 1 mesh so "
65  "assuming that is the right mesh");
66  return *m_meshes.begin()->second;
67  }
68  if (auto it = m_meshes.find(nmm_name); it == m_meshes.end()) {
69  std::vector<std::string_view> names;
70  std::transform(
71  m_meshes.begin(),
72  m_meshes.end(),
73  std::back_inserter(names),
74  [](const auto& pr) { return pr.first; });
75  wmtk::logger().error(
76  "Was unable to find root mesh name {} among {} names [{}] in MeshCollection",
77  nmm_name,
78  m_meshes.size(),
79  fmt::join(names, ","));
80  }
81  return *m_meshes.at(nmm_name);
82 }
84 {
85  using namespace std;
86  using namespace std;
87 #if defined(WMTK_ENABLED_CPP20)
88  std::ranges::view auto split = internal::split_path(path);
89 #else
90  auto split = internal::split_path(path);
91 #endif
92  const auto nmm_name = *split.begin();
93  if (nmm_name.empty() && m_meshes.size() == 1) {
94  wmtk::logger().debug("MeshCollection accessed with an empty name, but has only 1 mesh so "
95  "assuming that is the right mesh");
96  return *m_meshes.begin()->second;
97  }
98  try {
99  return *m_meshes.at(nmm_name);
100  } catch (const std::runtime_error& e) {
101  wmtk::logger().warn("Failed to find mesh named {} in mesh list. Path was ", nmm_name, path);
102  throw e;
103  }
104 }
105 std::map<std::string, const Mesh&> MeshCollection::all_meshes() const
106 // std::map<std::string, std::shared_ptr<const Mesh>> MeshCollection::all_meshes() const
107 {
108  // std::map<std::string, std::shared_ptr<const Mesh>> meshes;
109  std::map<std::string, const Mesh&> meshes;
110  for (const auto& [name, nnptr] : m_meshes) {
111  const auto pr = nnptr->all_meshes();
112  meshes.insert(pr.begin(), pr.end());
113  }
114  return meshes;
115 }
116 
118 {
119  for (auto it = m_meshes.begin(); it != m_meshes.end();) {
120  if (it->second->root().is_multi_mesh_root()) {
121  ++it;
122  } else {
123  it = m_meshes.erase(it);
124  }
125  }
126 }
127 } // namespace wmtk::components::multimesh
const NamedMultiMesh & get_named_multimesh(const std::string_view &path) const
const Mesh & get_mesh(const std::string_view &path) const
NamedMultiMesh & add_mesh(NamedMultiMesh &&o)
bool has_mesh(const std::string_view &path) const
bool has_named_multimesh(const std::string_view &path) const
std::map< std::string, const Mesh & > all_meshes() const
std::map< std::string_view, std::unique_ptr< NamedMultiMesh > > m_meshes
Mesh & get_mesh(const std::string_view &path) const
Definition: autodiff.h:995
auto split_path(const std::string_view &view)
Definition: split_path.hpp:8
spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:58