From d9d11e5686607fa4659dbd9f9bfef3760f73bc60 Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Tue, 5 Nov 2019 09:43:42 +0100 Subject: [PATCH] Add additional parameters to openvdbutils --- src/libslic3r/OpenVDBUtils.cpp | 39 +++++++++++++++++++++++----- src/libslic3r/OpenVDBUtils.hpp | 14 +++++++--- tests/libslic3r/test_hollowing.cpp | 41 +++++++++++++++++++++++++----- 3 files changed, 76 insertions(+), 18 deletions(-) diff --git a/src/libslic3r/OpenVDBUtils.cpp b/src/libslic3r/OpenVDBUtils.cpp index a5d4f0db6..9842db2da 100644 --- a/src/libslic3r/OpenVDBUtils.cpp +++ b/src/libslic3r/OpenVDBUtils.cpp @@ -50,18 +50,32 @@ void Contour3DDataAdapter::getIndexSpacePoint(size_t n, pos = {p.x(), p.y(), p.z()}; } -openvdb::FloatGrid::Ptr meshToVolume(const TriangleMesh & mesh, +openvdb::FloatGrid::Ptr meshToVolume(const TriangleMesh &mesh, + float exteriorBandWidth, + float interiorBandWidth, + int flags, const openvdb::math::Transform &tr) { + openvdb::initialize(); return openvdb::tools::meshToVolume( - TriangleMeshDataAdapter{mesh}, tr); + TriangleMeshDataAdapter{mesh}, tr, exteriorBandWidth, + interiorBandWidth, flags); } +// TODO: Do I need to call initialize? Seems to work without it as well but the +// docs say it should be called ones. It does a mutex lock-unlock sequence all +// even if was called previously. + openvdb::FloatGrid::Ptr meshToVolume(const sla::Contour3D & mesh, + float exteriorBandWidth, + float interiorBandWidth, + int flags, const openvdb::math::Transform &tr) { + openvdb::initialize(); return openvdb::tools::meshToVolume( - Contour3DDataAdapter{mesh}, tr); + Contour3DDataAdapter{mesh}, tr, exteriorBandWidth, interiorBandWidth, + flags); } inline Vec3f to_vec3f(const openvdb::Vec3s &v) { return Vec3f{v.x(), v.y(), v.z()}; } @@ -69,11 +83,14 @@ inline Vec3d to_vec3d(const openvdb::Vec3s &v) { return to_vec3f(v).cast inline Vec3i to_vec3i(const openvdb::Vec3I &v) { return Vec3i{int(v[0]), int(v[1]), int(v[2])}; } inline Vec4i to_vec4i(const openvdb::Vec4I &v) { return Vec4i{int(v[0]), int(v[1]), int(v[2]), int(v[3])}; } -sla::Contour3D volumeToMesh(const openvdb::FloatGrid &grid, - double isovalue, - double adaptivity, - bool relaxDisorientedTriangles) +template +sla::Contour3D _volumeToMesh(const Grid &grid, + double isovalue, + double adaptivity, + bool relaxDisorientedTriangles) { + openvdb::initialize(); + std::vector points; std::vector triangles; std::vector quads; @@ -93,4 +110,12 @@ sla::Contour3D volumeToMesh(const openvdb::FloatGrid &grid, return ret; } +sla::Contour3D volumeToMesh(const openvdb::FloatGrid &grid, + double isovalue, + double adaptivity, + bool relaxDisorientedTriangles) +{ + return _volumeToMesh(grid, isovalue, adaptivity, relaxDisorientedTriangles); +} + } // namespace Slic3r diff --git a/src/libslic3r/OpenVDBUtils.hpp b/src/libslic3r/OpenVDBUtils.hpp index e42403887..ee740dd06 100644 --- a/src/libslic3r/OpenVDBUtils.hpp +++ b/src/libslic3r/OpenVDBUtils.hpp @@ -7,11 +7,17 @@ namespace Slic3r { -openvdb::FloatGrid::Ptr meshToVolume(const TriangleMesh & mesh, - const openvdb::math::Transform &tr); +openvdb::FloatGrid::Ptr meshToVolume(const TriangleMesh &mesh, + float exteriorBandWidth = 3.0f, + float interiorBandWidth = 3.0f, + int flags = 0, + const openvdb::math::Transform &tr = {}); -openvdb::FloatGrid::Ptr meshToVolume(const sla::Contour3D & mesh, - const openvdb::math::Transform &tr); +openvdb::FloatGrid::Ptr meshToVolume(const sla::Contour3D &mesh, + float exteriorBandWidth = 3.0f, + float interiorBandWidth = 3.0f, + int flags = 0, + const openvdb::math::Transform &tr = {}); sla::Contour3D volumeToMesh(const openvdb::FloatGrid &grid, double isovalue = 0.0, diff --git a/tests/libslic3r/test_hollowing.cpp b/tests/libslic3r/test_hollowing.cpp index c0acb4f25..b74eb76ab 100644 --- a/tests/libslic3r/test_hollowing.cpp +++ b/tests/libslic3r/test_hollowing.cpp @@ -29,23 +29,50 @@ static bool _check_normals(const Slic3r::sla::Contour3D &mesh) return false; } -TEST_CASE("Negative 3D offset should produce smaller object.", "[Hollowing]") +TEST_CASE("Passing OpenVDB grid conversion produce similar geometry.", "[Hollowing]") { - Slic3r::sla::Contour3D imesh = Slic3r::sla::Contour3D{load_model("20mm_cube.obj")}; + Slic3r::TriangleMesh in_mesh = load_model("20mm_cube.obj"); + Slic3r::sla::Contour3D imesh = Slic3r::sla::Contour3D{in_mesh}; auto ptr = Slic3r::meshToVolume(imesh, {}); REQUIRE(ptr); - Slic3r::sla::Contour3D omesh = Slic3r::volumeToMesh(*ptr, -1., 0.0, true); + std::cout << "Grid class = " << ptr->getGridClass() << std::endl; + + Slic3r::sla::Contour3D omesh = Slic3r::volumeToMesh(*ptr, -2.9, 1.0, true); + + std::cout << "Triangle count: " << omesh.faces3.size() << std::endl; + std::cout << "Quad count: " << omesh.faces4.size() << std::endl; REQUIRE(!omesh.empty()); + SECTION("Converting to Contour3D to TriangleMesh") { + Slic3r::TriangleMesh msh = Slic3r::sla::to_triangle_mesh(omesh); + + msh.require_shared_vertices(); + msh.WriteOBJFile("out_tr.obj"); + + REQUIRE(msh.volume() == Approx(in_mesh.volume())); + } - +// omesh.faces4.clear(); std::fstream outfile{"out.obj", std::ios::out}; omesh.to_obj(outfile); - imesh.merge(omesh); - std::fstream merged_outfile("merged_out.obj", std::ios::out); - imesh.to_obj(merged_outfile); } + +//TEST_CASE("Negative 3D offset should produce smaller object.", "[Hollowing]") +//{ +// Slic3r::sla::Contour3D imesh = Slic3r::sla::Contour3D{load_model("20mm_cube.obj")}; +// auto ptr = Slic3r::meshToVolume(imesh, {}); + +// REQUIRE(ptr); + +// Slic3r::sla::Contour3D omesh = Slic3r::volumeToMesh(*ptr, -1., 0.0, true); + +// REQUIRE(!omesh.empty()); + +// imesh.merge(omesh); +// std::fstream merged_outfile("merged_out.obj", std::ios::out); +// imesh.to_obj(merged_outfile); +//}