Wildmeshing Toolkit
named_multimesh.cpp
Go to the documentation of this file.
1 #include <fmt/ranges.h>
2 #include <catch2/catch_test_macros.hpp>
3 #include <nlohmann/json.hpp>
4 #include <wmtk/Mesh.hpp>
6 #include "tools/TriMesh_examples.hpp"
8 
10 
12 
13 namespace {
14 const std::filesystem::path data_dir = WMTK_DATA_DIR;
15 
16 auto make_mesh()
17 {
18  return wmtk::tests::disk(5);
19 }
20 
21 auto make_child(wmtk::Mesh& m, const std::vector<int64_t>& path)
22 {
23  if (path.size() == 0) {
24  // multimesh root mesh already exists so nothing to be done
25  return;
26  }
27  for (size_t j = 0; j < path.size(); ++j) {
28  std::vector<int64_t> p(path.begin(), path.begin() + j);
29  auto& cur_mesh = m.get_multi_mesh_mesh(p);
30  int64_t child_index = path[j];
31  const auto child_meshes = cur_mesh.get_child_meshes();
32  for (int64_t index = child_meshes.size(); index <= child_index; ++index) {
33  auto new_mesh = make_mesh();
34  auto map = wmtk::multimesh::same_simplex_dimension_bijection(cur_mesh, *new_mesh);
35 
36  cur_mesh.register_child_mesh(new_mesh, map);
37  }
38  }
39 }
40 } // namespace
41 
42 
43 TEST_CASE("named_multimesh_parse", "[components][multimesh]")
44 {
45  {
46  auto m = make_mesh();
48  named_mm.set_mesh(*m);
49 
50  named_mm.set_name("roo");
51 
52  CHECK(std::vector<int64_t>{} == named_mm.get_id("roo"));
53  CHECK(m == named_mm.root().shared_from_this());
54  CHECK(m == named_mm.get_mesh("roo").shared_from_this());
55  }
56 
57 
58  {
59  auto m = make_mesh();
60  make_child(*m, {0});
61 
62 
64  named_mm.set_mesh(*m);
65  {
66  nlohmann::json js;
67  js["roo"] = nlohmann::json::array({"child"});
68  named_mm.set_names(js);
69  }
70  CHECK(std::vector<int64_t>{} == named_mm.get_id("roo"));
71  CHECK(std::vector<int64_t>{0} == named_mm.get_id("roo.child"));
72  CHECK(m == named_mm.root().shared_from_this());
73  CHECK(m == named_mm.get_mesh("roo").shared_from_this());
74  CHECK(
75  m->get_multi_mesh_child_mesh({0}).shared_from_this() ==
76  named_mm.get_mesh("roo.child").shared_from_this());
77  }
78  {
80  nlohmann::json js;
81  js["roo"] = nlohmann::json("child");
82  named_mm.set_names(js);
83  CHECK(std::vector<int64_t>{} == named_mm.get_id("roo"));
84  CHECK(std::vector<int64_t>{0} == named_mm.get_id("roo.child"));
85  }
86  {
88  nlohmann::json js;
89  js["roo"]["child"] = {};
90  named_mm.set_names(js);
91  CHECK(std::vector<int64_t>{} == named_mm.get_id("roo"));
92  CHECK(std::vector<int64_t>{0} == named_mm.get_id("roo.child"));
93  }
94 
95  {
97  auto m = make_mesh();
98  {
99  make_child(*m, {0});
100  make_child(*m, {0, 0, 0});
101  make_child(*m, {1, 1});
102 
103  named_mm.set_mesh(*m);
104  }
105  {
106  nlohmann::json js;
107  js["roo"]["c"]["d"]["e"] = {};
108  js["roo"]["child"] = nlohmann::json::array({"c1", "c2"});
109  named_mm.set_names(js);
110  }
111  CHECK(std::vector<int64_t>{} == named_mm.get_id("roo"));
112  CHECK(std::vector<int64_t>{0, 0, 0} == named_mm.get_id("roo.c.d.e"));
113  CHECK(std::vector<int64_t>{1} == named_mm.get_id("roo.child"));
114  CHECK(std::vector<int64_t>{1, 0} == named_mm.get_id("roo.child.c1"));
115  CHECK(std::vector<int64_t>{1, 1} == named_mm.get_id("roo.child.c2"));
116  CHECK(
117  m->get_multi_mesh_child_mesh({0, 0, 0}).shared_from_this() ==
118  named_mm.get_mesh("roo.c.d.e").shared_from_this());
119  CHECK(
120  m->get_multi_mesh_child_mesh({1, 1}).shared_from_this() ==
121  named_mm.get_mesh("roo.child.c2").shared_from_this());
122 
123  CHECK(
124  m->get_multi_mesh_child_mesh({0, 0, 0}).shared_from_this() ==
125  named_mm.get_mesh(".c.d.e").shared_from_this());
126  CHECK(
127  m->get_multi_mesh_child_mesh({1, 1}).shared_from_this() ==
128  named_mm.get_mesh(".child.c2").shared_from_this());
129  }
130 }
Mesh & get_multi_mesh_mesh(const std::vector< int64_t > &absolute_id)
std::vector< std::shared_ptr< Mesh > > get_child_meshes() const
returns the direct multimesh child meshes for the current mesh
std::vector< int64_t > get_id(const std::string_view &path) const
void set_mesh(Mesh &m)
Navigates to the root of the multimesh.
Mesh & get_mesh(const std::string_view &path) const
void set_names(const nlohmann::json &js)
void set_name(const std::string_view &root_name="")
TEST_CASE("named_multimesh_parse", "[components][multimesh]")
nlohmann::json json
std::shared_ptr< TriMesh > make_mesh(const DiskOptions &opt)
Definition: DiskOptions.cpp:10
std::vector< std::array< Tuple, 2 > > same_simplex_dimension_bijection(const Mesh &parent, const Mesh &child)
const std::filesystem::path data_dir
nlohmann::json json
Definition: input.cpp:9