Wildmeshing Toolkit
Loading...
Searching...
No Matches
test_component_delaunay.cpp
Go to the documentation of this file.
1#include <random>
2
3#include <catch2/catch_test_macros.hpp>
4#include <nlohmann/json.hpp>
5#include <paraviewo/VTUWriter.hpp>
12
13using json = nlohmann::json;
14
15const std::filesystem::path data_dir = WMTK_DATA_DIR;
16
17// all rows in p appear also in v
19 const Eigen::Ref<Eigen::MatrixXd> p,
20 const Eigen::Ref<Eigen::MatrixXd> v)
21{
22 REQUIRE(p.cols() == v.cols());
23 REQUIRE(p.rows() <= v.rows());
24
25 std::vector<Eigen::VectorXd> vv;
26 vv.reserve(v.rows());
27 for (Eigen::Index i = 0; i < v.rows(); ++i) {
28 vv.emplace_back(v.row(i));
29 }
30
31 auto v_less = [](const Eigen::Ref<Eigen::VectorXd> a, const Eigen::Ref<Eigen::VectorXd> b) {
32 for (Eigen::Index i = 0; i < a.rows() - 1; ++i) {
33 if (a[i] != b[i]) {
34 return a[i] < b[i];
35 }
36 }
37 return a[a.rows() - 1] < b[b.rows() - 1];
38 };
39
40 std::sort(vv.begin(), vv.end(), v_less);
41 for (Eigen::Index i = 0; i < p.rows(); ++i) {
42 const Eigen::VectorXd r = p.row(i);
43 CHECK(std::find(vv.begin(), vv.end(), r) != vv.end());
44 }
45}
46
47// TEST_CASE("component_delaunay", "[components][delaunay]")
48// {
49// std::map<std::string, std::filesystem::path> files;
50
51// // input
52// {
53// json input_component_json = {
54// {"type", "input"},
55// {"name", "input_mesh_test_component_delaunay"},
56// {"cell_dimension", 0},
57// {"file", data_dir / "piece_0.msh"}};
58// wmtk::components::input(input_component_json, files);
59// }
60
61// json component_json = {
62// {"type", "input"},
63// {"input", "input_mesh_test_component_delaunay"},
64// {"output", "output_mesh_test_component_delaunay"},
65// {"cell_dimension", 3}};
66
67// CHECK_NOTHROW(wmtk::components::delaunay(component_json, files));
68
69// //{
70// // json component_json = {
71// // {"type", "output"},
72// // {"input", "output_mesh"},
73// // {"cell_dimension", 3},
74// // {"file", "component_delaunay_3d"}};
75// //
76// // CHECK_NOTHROW(wmtk::components::output(component_json, files));
77// //}
78// }
79
80TEST_CASE("delaunay_2d_five_points", "[components][delaunay]")
81{
82 // create points
83 wmtk::RowVectors2d points(5, 2);
84 points.row(0) << -1, -1;
85 points.row(1) << -1, 1;
86 points.row(2) << 1, -1;
87 points.row(3) << 1, 1;
88 points.row(4) << 0, 0;
89
90 Eigen::MatrixXd vertices;
91 Eigen::MatrixXi faces;
92 CHECK_NOTHROW(std::tie(vertices, faces) = wmtk::components::internal::delaunay_2d(points));
93 CHECK(points == vertices);
94
95 if (false) {
96 paraviewo::VTUWriter writer;
97 writer.write_mesh("delaunay_2d_five_points.vtu", vertices, faces);
98 }
99}
100
101TEST_CASE("delaunay_2d_random", "[components][delaunay]")
102{
103 std::uniform_real_distribution<double> distribution(-1, 1);
104 std::default_random_engine random_engine;
105
106 // create points
107 wmtk::RowVectors2d points(100, 2);
108 for (size_t i = 0; i < 100; ++i) {
109 const double x = distribution(random_engine);
110 const double y = distribution(random_engine);
111 points.row(i) << x, y;
112 }
113
114 Eigen::MatrixXd vertices;
115 Eigen::MatrixXi faces;
116 CHECK_NOTHROW(std::tie(vertices, faces) = wmtk::components::internal::delaunay_2d(points));
117 check_p_is_contained_in_v(points, vertices);
118
119 if (false) {
120 paraviewo::VTUWriter writer;
121 writer.write_mesh("delaunay_2d_random.vtu", vertices, faces);
122 }
123}
124
125TEST_CASE("delaunay_3d_nine_points", "[components][delaunay]")
126{
127 // create points
128 wmtk::RowVectors3d points(9, 3);
129 points.row(0) << -1, -1, -1;
130 points.row(1) << 1, -1, -1;
131 points.row(2) << -1, 1, -1;
132 points.row(3) << -1, -1, 1;
133 points.row(4) << 1, 1, -1;
134 points.row(5) << -1, 1, 1;
135 points.row(6) << 1, -1, 1;
136 points.row(7) << 1, 1, 1;
137 points.row(8) << 0, 0, 0;
138
139 Eigen::MatrixXd vertices;
140 Eigen::MatrixXi faces;
141 CHECK_NOTHROW(std::tie(vertices, faces) = wmtk::components::internal::delaunay_3d(points));
142 CHECK(points == vertices);
143
144 if (false) {
145 paraviewo::VTUWriter writer;
146 writer.write_mesh("delaunay_3d_nine_points.vtu", vertices, faces);
147 }
148}
149
150TEST_CASE("delaunay_3d_random", "[components][delaunay]")
151{
152 std::uniform_real_distribution<double> distribution(-1, 1);
153 std::default_random_engine random_engine;
154
155 // create points
156 wmtk::RowVectors3d points(100, 3);
157 for (size_t i = 0; i < 100; ++i) {
158 const double x = distribution(random_engine);
159 const double y = distribution(random_engine);
160 const double z = distribution(random_engine);
161 points.row(i) << x, y, z;
162 }
163
164 Eigen::MatrixXd vertices;
165 Eigen::MatrixXi faces;
166 CHECK_NOTHROW(std::tie(vertices, faces) = wmtk::components::internal::delaunay_3d(points));
167 check_p_is_contained_in_v(points, vertices);
168
169 if (false) {
170 paraviewo::VTUWriter writer;
171 writer.write_mesh("delaunay_3d_random.vtu", vertices, faces);
172 }
173}
174
175TEST_CASE("delaunay_throw", "[components][delaunay]")
176{
177 Eigen::MatrixXd points;
178 SECTION("0d")
179 {
180 points.resize(1, 1);
181 points.row(0) << 0;
182 }
183 SECTION("4d")
184 {
185 points.resize(1, 4);
186 points.row(0) << 0, 1, 2, 3;
187 }
188
190}
191
192TEST_CASE("delaunay_empty_points", "[components][delaunay]")
193{
194 Eigen::MatrixXd points;
195 Eigen::MatrixXd vertices;
196 Eigen::MatrixXi faces;
197 CHECK_NOTHROW(std::tie(vertices, faces) = wmtk::components::internal::delaunay_geogram(points));
198
199 CHECK(vertices.rows() == 0);
200 CHECK(faces.rows() == 0);
201}
std::tuple< Eigen::MatrixXd, Eigen::MatrixXi > delaunay_3d(Eigen::Ref< const RowVectors3d > points)
std::tuple< Eigen::MatrixXd, Eigen::MatrixXi > delaunay_geogram(Eigen::Ref< const Eigen::MatrixXd > points)
std::tuple< Eigen::MatrixXd, Eigen::MatrixXi > delaunay_2d(Eigen::Ref< const RowVectors2d > points)
RowVectors< double, 2 > RowVectors2d
Definition Types.hpp:50
RowVectors< double, 3 > RowVectors3d
Definition Types.hpp:51
const std::filesystem::path data_dir
TEST_CASE("delaunay_2d_five_points", "[components][delaunay]")
void check_p_is_contained_in_v(const Eigen::Ref< Eigen::MatrixXd > p, const Eigen::Ref< Eigen::MatrixXd > v)
nlohmann::json json
Definition input.cpp:9