Wildmeshing Toolkit
Loading...
Searching...
No Matches
test_component_fusion.cpp
Go to the documentation of this file.
1#include <catch2/catch_test_macros.hpp>
2#include <nlohmann/json.hpp>
3#include <tools/DEBUG_TetMesh.hpp>
4#include <tools/DEBUG_TriMesh.hpp>
5#include <tools/TetMesh_examples.hpp>
6#include <tools/TriMesh_examples.hpp>
7#include <wmtk/Mesh.hpp>
8#include <wmtk/TriMesh.hpp>
9#include <wmtk/Types.hpp>
10#include <wmtk/components/fusion/fusion.hpp>
12#include <wmtk/components/utils/Paths.hpp>
13#include <wmtk/io/Cache.hpp>
17
18using namespace wmtk::components::utils;
19using namespace wmtk;
20using namespace wmtk::tests;
21using namespace wmtk::tests_3d;
22
23using json = nlohmann::json;
24
25const std::filesystem::path data_dir = WMTK_DATA_DIR;
26
27
28TEST_CASE("fusion_2d", "[components][fusion][.]")
29{
30 wmtk::io::Cache cache("wmtk_cache", ".");
31
33 V.resize(15, 3);
34 V.row(0) << 1.0, 0.0, 0.0;
35 V.row(1) << 1.25, 0.0, 0.0;
36 V.row(2) << 1.5, 0.0, 0.0;
37 V.row(3) << 1.75, 0.0, 0.0;
38 V.row(4) << 2.0, 0.0, 0.0;
39 V.row(5) << 1.0, 1.0, 0.0;
40 V.row(6) << 1.25, 1.0, 0.0;
41 V.row(7) << 1.5, 1.0, 0.0;
42 V.row(8) << 1.75, 1.0, 0.0;
43 V.row(9) << 2.0, 1.0, 0.0;
44 V.row(10) << 1.0, 2.0, 0.0;
45 V.row(11) << 1.25, 2.0, 0.0;
46 V.row(12) << 1.5, 2.0, 0.0;
47 V.row(13) << 1.75, 2.0, 0.0;
48 V.row(14) << 2.0, 2.0, 0.0;
49
51 F.resize(16, 3);
52 F.row(0) << 0, 1, 6;
53 F.row(1) << 0, 6, 5;
54 F.row(2) << 1, 2, 7;
55 F.row(3) << 1, 7, 6;
56 F.row(4) << 2, 3, 8;
57 F.row(5) << 2, 8, 7;
58 F.row(6) << 3, 4, 9;
59 F.row(7) << 3, 9, 8;
60 F.row(8) << 5, 6, 11;
61 F.row(9) << 5, 11, 10;
62 F.row(10) << 6, 7, 12;
63 F.row(11) << 6, 12, 11;
64 F.row(12) << 7, 8, 13;
65 F.row(13) << 7, 13, 12;
66 F.row(14) << 8, 9, 14;
67 F.row(15) << 8, 14, 13;
68
69 DEBUG_TriMesh m;
70 m.initialize(F);
71 mesh_utils::set_matrix_attribute(V, "vertices", PrimitiveType::Vertex, m);
72
73 cache.write_mesh(m, "test_mesh");
74
75 SECTION("x")
76 {
77 json input =
78 R"({
79 "input": "test_mesh",
80 "name": "on_x",
81 "fusion_X": true,
82 "fusion_Y": false,
83 "fusion_Z": false
84 })"_json;
85
86 wmtk::components::fusion(Paths(), input, cache);
87
88 auto p_mesh = cache.read_mesh("on_x");
89 DEBUG_TriMesh& periodic_mesh = static_cast<DEBUG_TriMesh&>(*p_mesh);
90
92 periodic_mesh.serialize(writer);
93
95 writer.get_FV_matrix(F_p);
96
97 std::cout << F_p << std::endl << std::endl;
98
99 auto childs = periodic_mesh.get_child_meshes();
100 CHECK(childs.size() == 1);
101 CHECK(
102 childs[0]->get_all(PrimitiveType::Triangle).size() ==
103 periodic_mesh.get_all(PrimitiveType::Triangle).size());
104 CHECK(periodic_mesh.get_all(PrimitiveType::Vertex).size() == 12);
105 CHECK(childs[0]->get_all(PrimitiveType::Vertex).size() == 15);
106
108 childs[0]->serialize(writer_child);
109 MatrixX<int64_t> FV_child;
110 MatrixX<double> V_child;
111
112 writer_child.get_position_matrix(V_child);
113 writer_child.get_FV_matrix(FV_child);
114
115 std::cout << V_child << std::endl << std::endl;
116 std::cout << FV_child << std::endl << std::endl;
117 }
118 SECTION("y")
119 {
120 json input =
121 R"({
122 "input": "test_mesh",
123 "name": "on_y",
124 "fusion_X": false,
125 "fusion_Y": true,
126 "fusion_Z": false
127 })"_json;
128
129 wmtk::components::fusion(Paths(), input, cache);
130
131 auto p_mesh = cache.read_mesh("on_y");
132 DEBUG_TriMesh& periodic_mesh = static_cast<DEBUG_TriMesh&>(*p_mesh);
133
135 periodic_mesh.serialize(writer);
136
137 // MatrixX<double> V_p;
139 // writer.get_position_matrix(V_p);
140 writer.get_FV_matrix(F_p);
141
142 // std::cout << V_p << std::endl << std::endl;
143 // std::cout << F_p << std::endl;
144
145 auto childs = periodic_mesh.get_child_meshes();
146 CHECK(childs.size() == 1);
147 CHECK(
148 childs[0]->get_all(PrimitiveType::Triangle).size() ==
149 periodic_mesh.get_all(PrimitiveType::Triangle).size());
150 }
151 SECTION("all")
152 {
153 json input =
154 R"({
155 "input": "test_mesh",
156 "name": "on_all",
157 "fusion_X": true,
158 "fusion_Y": true,
159 "fusion_Z": false
160 })"_json;
161
162 wmtk::components::fusion(Paths(), input, cache);
163
164 auto p_mesh = cache.read_mesh("on_all");
165 DEBUG_TriMesh& periodic_mesh = static_cast<DEBUG_TriMesh&>(*p_mesh);
166
168 periodic_mesh.serialize(writer);
169
170 // MatrixX<double> V_p;
172 // writer.get_position_matrix(V_p);
173 writer.get_FV_matrix(F_p);
174
175 // std::cout << V_p << std::endl << std::endl;
176 // std::cout << F_p << std::endl;
177 auto childs = periodic_mesh.get_child_meshes();
178 CHECK(childs.size() == 1);
179 CHECK(
180 childs[0]->get_all(PrimitiveType::Triangle).size() ==
181 periodic_mesh.get_all(PrimitiveType::Triangle).size());
182 }
183}
184
185TEST_CASE("fusion_3d", "[components][fusion][.]")
186{
187 wmtk::io::Cache cache("wmtk_cache", ".");
188
189 RowVectors3d V;
190 V.resize(8, 3);
191 V.row(0) << 1.0, 1.0, 1.0;
192 V.row(1) << 3.0, 1.0, 1.0;
193 V.row(2) << 3.0, 1.5, 1.0;
194 V.row(3) << 1.0, 1.5, 1.0;
195 V.row(4) << 1.0, 1.0, 4.0;
196 V.row(5) << 3.0, 1.0, 4.0;
197 V.row(6) << 3.0, 1.5, 4.0;
198 V.row(7) << 1.0, 1.5, 4.0;
199
200 RowVectors4l T;
201 T.resize(6, 4);
202 T.row(0) << 0, 1, 2, 3;
203 T.row(1) << 5, 2, 6, 7;
204 T.row(2) << 4, 1, 5, 3;
205 T.row(3) << 4, 3, 7, 5;
206 T.row(4) << 3, 1, 5, 2;
207 T.row(5) << 2, 3, 7, 5;
208
209 DEBUG_TetMesh m;
210 m.initialize(T);
211 mesh_utils::set_matrix_attribute(V, "vertices", PrimitiveType::Vertex, m);
212
213 cache.write_mesh(m, "test_mesh");
214
215 SECTION("x")
216 {
217 json input =
218 R"({
219 "input": "test_mesh",
220 "name": "on_x",
221 "fusion_X": true,
222 "fusion_Y": false,
223 "fusion_Z": false
224 })"_json;
225
226 wmtk::components::fusion(Paths(), input, cache);
227
228 auto p_mesh = cache.read_mesh("on_x");
229 DEBUG_TetMesh& periodic_mesh = static_cast<DEBUG_TetMesh&>(*p_mesh);
230
232 periodic_mesh.serialize(writer);
233
234 // MatrixX<double> V_p;
236 // writer.get_position_matrix(V_p);
237 writer.get_TV_matrix(T_p);
238
239 // std::cout << V_p << std::endl << std::endl;
240 // std::cout << T_p << std::endl;
241 auto childs = periodic_mesh.get_child_meshes();
242 CHECK(childs.size() == 1);
243 CHECK(
244 childs[0]->get_all(PrimitiveType::Tetrahedron).size() ==
245 periodic_mesh.get_all(PrimitiveType::Tetrahedron).size());
246 CHECK(periodic_mesh.get_all(PrimitiveType::Vertex).size() == 4);
247
249 childs[0]->serialize(writer_child);
250 MatrixX<int64_t> TV_child;
251 MatrixX<double> V_child;
252
253 writer_child.get_position_matrix(V_child);
254 writer_child.get_TV_matrix(TV_child);
255
256 std::cout << V_child << std::endl << std::endl;
257 std::cout << TV_child << std::endl << std::endl;
258 }
259
260 SECTION("xy")
261 {
262 json input =
263 R"({
264 "input": "test_mesh",
265 "name": "on_xy",
266 "fusion_X": true,
267 "fusion_Y": true,
268 "fusion_Z": false
269 })"_json;
270
271 wmtk::components::fusion(Paths(), input, cache);
272
273 auto p_mesh = cache.read_mesh("on_xy");
274 DEBUG_TetMesh& periodic_mesh = static_cast<DEBUG_TetMesh&>(*p_mesh);
275
277 periodic_mesh.serialize(writer);
278
279 // MatrixX<double> V_p;
281 // writer.get_position_matrix(V_p);
282 writer.get_TV_matrix(T_p);
283
284 // std::cout << V_p << std::endl << std::endl;
285 // std::cout << T_p << std::endl;
286 auto childs = periodic_mesh.get_child_meshes();
287 CHECK(childs.size() == 1);
288 CHECK(
289 childs[0]->get_all(PrimitiveType::Tetrahedron).size() ==
290 periodic_mesh.get_all(PrimitiveType::Tetrahedron).size());
291 CHECK(periodic_mesh.get_all(PrimitiveType::Vertex).size() == 2);
292 }
293
294 SECTION("xyz")
295 {
296 json input =
297 R"({
298 "input": "test_mesh",
299 "name": "on_xyz",
300 "fusion_X": true,
301 "fusion_Y": true,
302 "fusion_Z": true
303 })"_json;
304
305 wmtk::components::fusion(Paths(), input, cache);
306
307 auto p_mesh = cache.read_mesh("on_xyz");
308 DEBUG_TetMesh& periodic_mesh = static_cast<DEBUG_TetMesh&>(*p_mesh);
309
311 periodic_mesh.serialize(writer);
312
313 // MatrixX<double> V_p;
315 // writer.get_position_matrix(V_p);
316 writer.get_TV_matrix(T_p);
317
318 // std::cout << V_p << std::endl << std::endl;
319 // std::cout << T_p << std::endl;
320 auto childs = periodic_mesh.get_child_meshes();
321 CHECK(childs.size() == 1);
322 CHECK(
323 childs[0]->get_all(PrimitiveType::Tetrahedron).size() ==
324 periodic_mesh.get_all(PrimitiveType::Tetrahedron).size());
325 CHECK(periodic_mesh.get_all(PrimitiveType::Vertex).size() == 1);
326 }
327}
void write_mesh(const Mesh &m, const std::string &name, const std::map< std::string, std::vector< int64_t > > &multimesh_names={})
Write a mesh to cache.
Definition Cache.cpp:194
std::shared_ptr< Mesh > read_mesh(const std::string &name) const
Load a mesh from cache.
Definition Cache.cpp:171
void get_position_matrix(MatrixX< double > &matrix)
void get_TV_matrix(MatrixX< int64_t > &matrix)
void get_FV_matrix(MatrixX< int64_t > &matrix)
std::shared_ptr< Mesh > input(const std::filesystem::path &file, const bool ignore_z_if_zero, const std::vector< std::string > &tetrahedron_attributes)
Definition input.cpp:12
attribute::MeshAttributeHandle set_matrix_attribute(const Mat &data, const std::string &name, const PrimitiveType &type, Mesh &mesh)
Definition mesh_utils.hpp:9
RowVectors< int64_t, 3 > RowVectors3l
Definition Types.hpp:47
RowVectors< int64_t, 4 > RowVectors4l
Definition Types.hpp:48
RowVectors< double, 3 > RowVectors3d
Definition Types.hpp:51
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixX
Definition Types.hpp:14
const std::filesystem::path data_dir
TEST_CASE("fusion_2d", "[components][fusion][.]")
nlohmann::json json
Definition input.cpp:9