Skip to content

Commit

Permalink
Merge pull request #433 from wildmeshing/dzint/413-add-a-test-for-del…
Browse files Browse the repository at this point in the history
…aunay

Check that input and output vertices for `Delaunay` are the same
  • Loading branch information
daniel-zint authored Oct 16, 2023
2 parents 9385b55 + b988488 commit fc99c8e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ std::tuple<Eigen::MatrixXd, Eigen::MatrixXi> delaunay_geogram(
}

long dim = points.cols();
assert(dim == 2 || dim == 3);

static std::once_flag once_flag;
std::call_once(once_flag, []() { GEO::initialize(); });
Expand Down Expand Up @@ -67,12 +66,12 @@ std::tuple<Eigen::MatrixXd, Eigen::MatrixXi> delaunay_geogram(
vertices.row(i) = Eigen::Map<const Eigen::Vector3d>(engine->vertex_ptr(i)).transpose();
break;
}
default: {
// for generality allowing for arbitrary dimensions
vertices.row(i) =
Eigen::Map<const Eigen::VectorXd>(engine->vertex_ptr(i), dim).transpose();
break;
}
// default: {
// // for generality allowing for arbitrary dimensions
// vertices.row(i) =
// Eigen::Map<const Eigen::VectorXd>(engine->vertex_ptr(i), dim).transpose();
// break;
//}
}
}

Expand Down
95 changes: 79 additions & 16 deletions tests/components/test_component_delaunay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,75 @@
#include <wmtk_components/delaunay/delaunay.hpp>
#include <wmtk_components/delaunay/internal/delaunay_2d.hpp>
#include <wmtk_components/delaunay/internal/delaunay_3d.hpp>
#include <wmtk_components/delaunay/internal/delaunay_geogram.hpp>
#include <wmtk_components/input/input.hpp>
#include <wmtk_components/output/output.hpp>

using json = nlohmann::json;

const std::filesystem::path data_dir = WMTK_DATA_DIR;

TEST_CASE("component_delaunay", "[components][delaunay][.]")
// all rows in p appear also in v
void check_p_is_contained_in_v(
const Eigen::Ref<Eigen::MatrixXd> p,
const Eigen::Ref<Eigen::MatrixXd> v)
{
REQUIRE(p.cols() == v.cols());
REQUIRE(p.rows() <= v.rows());

std::vector<Eigen::VectorXd> vv;
vv.reserve(v.rows());
for (Eigen::Index i = 0; i < v.rows(); ++i) {
vv.emplace_back(v.row(i));
}

auto v_less = [](const Eigen::Ref<Eigen::VectorXd> a, const Eigen::Ref<Eigen::VectorXd> b) {
for (Eigen::Index i = 0; i < a.rows() - 1; ++i) {
if (a[i] != b[i]) {
return a[i] < b[i];
}
}
return a[a.rows() - 1] < b[b.rows() - 1];
};

std::sort(vv.begin(), vv.end(), v_less);
for (Eigen::Index i = 0; i < p.rows(); ++i) {
const Eigen::VectorXd r = p.row(i);
CHECK(std::find(vv.begin(), vv.end(), r) != vv.end());
}
}

TEST_CASE("component_delaunay", "[components][delaunay]")
{
std::map<std::string, std::filesystem::path> files;

// input
{
json input_component_json = {
{"type", "input"},
{"name", "input_mesh"},
{"name", "input_mesh_test_component_delaunay"},
{"cell_dimension", 0},
{"file", data_dir / "bunny.off"}};
{"file", data_dir / "piece_0.obj"}};
wmtk::components::input(input_component_json, files);
}

json component_json = {
{"type", "input"},
{"input", "input_mesh"},
{"output", "output_mesh"},
{"input", "input_mesh_test_component_delaunay"},
{"output", "output_mesh_test_component_delaunay"},
{"cell_dimension", 3}};

CHECK_NOTHROW(wmtk::components::delaunay(component_json, files));

{
json component_json = {
{"type", "output"},
{"input", "output_mesh"},
{"cell_dimension", 3},
{"file", "component_delaunay_3d"}};

CHECK_NOTHROW(wmtk::components::output(component_json, files));
}
//{
// json component_json = {
// {"type", "output"},
// {"input", "output_mesh"},
// {"cell_dimension", 3},
// {"file", "component_delaunay_3d"}};
//
// CHECK_NOTHROW(wmtk::components::output(component_json, files));
//}
}

TEST_CASE("delaunay_2d_five_points", "[components][delaunay]")
Expand All @@ -59,6 +90,7 @@ TEST_CASE("delaunay_2d_five_points", "[components][delaunay]")
Eigen::MatrixXd vertices;
Eigen::MatrixXi faces;
CHECK_NOTHROW(std::tie(vertices, faces) = wmtk::components::internal::delaunay_2d(points));
CHECK(points == vertices);

if (false) {
paraviewo::VTUWriter writer;
Expand All @@ -82,14 +114,15 @@ TEST_CASE("delaunay_2d_random", "[components][delaunay]")
Eigen::MatrixXd vertices;
Eigen::MatrixXi faces;
CHECK_NOTHROW(std::tie(vertices, faces) = wmtk::components::internal::delaunay_2d(points));
check_p_is_contained_in_v(points, vertices);

if (false) {
paraviewo::VTUWriter writer;
writer.write_mesh("delaunay_2d_random.vtu", vertices, faces);
}
}

TEST_CASE("delaunay_3d_nine_points", "[components][delaunay][.]")
TEST_CASE("delaunay_3d_nine_points", "[components][delaunay]")
{
// create points
wmtk::RowVectors3d points(9, 3);
Expand All @@ -106,14 +139,15 @@ TEST_CASE("delaunay_3d_nine_points", "[components][delaunay][.]")
Eigen::MatrixXd vertices;
Eigen::MatrixXi faces;
CHECK_NOTHROW(std::tie(vertices, faces) = wmtk::components::internal::delaunay_3d(points));
CHECK(points == vertices);

if (false) {
paraviewo::VTUWriter writer;
writer.write_mesh("delaunay_3d_nine_points.vtu", vertices, faces);
}
}

TEST_CASE("delaunay_3d_random", "[components][delaunay][.]")
TEST_CASE("delaunay_3d_random", "[components][delaunay]")
{
std::uniform_real_distribution<double> distribution(-1, 1);
std::default_random_engine random_engine;
Expand All @@ -130,9 +164,38 @@ TEST_CASE("delaunay_3d_random", "[components][delaunay][.]")
Eigen::MatrixXd vertices;
Eigen::MatrixXi faces;
CHECK_NOTHROW(std::tie(vertices, faces) = wmtk::components::internal::delaunay_3d(points));
check_p_is_contained_in_v(points, vertices);

if (false) {
paraviewo::VTUWriter writer;
writer.write_mesh("delaunay_3d_random.vtu", vertices, faces);
}
}

TEST_CASE("delaunay_throw", "[components][delaunay]")
{
Eigen::MatrixXd points;
SECTION("0d")
{
points.resize(1, 1);
points.row(0) << 0;
}
SECTION("4d")
{
points.resize(1, 4);
points.row(0) << 0, 1, 2, 3;
}

CHECK_THROWS(wmtk::components::internal::delaunay_geogram(points));
}

TEST_CASE("delaunay_empty_points", "[components][delaunay]")
{
Eigen::MatrixXd points;
Eigen::MatrixXd vertices;
Eigen::MatrixXi faces;
CHECK_NOTHROW(std::tie(vertices, faces) = wmtk::components::internal::delaunay_geogram(points));

CHECK(vertices.rows() == 0);
CHECK(faces.rows() == 0);
}

0 comments on commit fc99c8e

Please sign in to comment.