Do some hollowing finally.

This commit is contained in:
tamasmeszaros 2019-10-31 14:36:33 +01:00
parent ce9c2c5dd4
commit 2165537fa5
8 changed files with 202 additions and 62 deletions

View file

@ -1,5 +1,5 @@
if(TARGET OpenVDB::openvdb)
add_executable(hollowing_tests hollowing_tests.cpp)
add_executable(hollowing_tests hollowing_test_main.cpp hollowing_tests.cpp openvdb_utils.cpp openvdb_utils.hpp)
#find_package(GTest REQUIRED)
#target_link_libraries(hollowing_tests libslic3r OpenVDB::openvdb GTest::GTest GTest::Main)

View file

@ -0,0 +1 @@
#include <catch_main.hpp>

View file

@ -1,10 +1,8 @@
#include <iostream>
#include <catch_main.hpp>
#include <fstream>
#include <catch2/catch.hpp>
#include <openvdb/openvdb.h>
#include <openvdb/tools/MeshToVolume.h>
#include <openvdb/tools/VolumeToMesh.h>
#include <libslic3r/TriangleMesh.hpp>
#include "openvdb_utils.hpp"
#include "libslic3r/Format/OBJ.hpp"
#if defined(WIN32) || defined(_WIN32)
@ -13,22 +11,6 @@
#define PATH_SEPARATOR R"(/)"
#endif
class TriangleMeshDataAdapter {
public:
Slic3r::TriangleMesh mesh;
size_t polygonCount() const { return mesh.its.indices.size(); }
size_t pointCount() const { return mesh.its.vertices.size(); }
size_t vertexCount(size_t) const { return 3; }
// Return position pos in local grid index space for polygon n and vertex v
void getIndexSpacePoint(size_t n, size_t v, openvdb::Vec3d& pos) const {
auto vidx = size_t(mesh.its.indices[n](Eigen::Index(v)));
Slic3r::Vec3d p = mesh.its.vertices[vidx].cast<double>();
pos = {double(p.x()), double(p.y()), p.z()};
}
};
static Slic3r::TriangleMesh load_model(const std::string &obj_filename)
{
Slic3r::TriangleMesh mesh;
@ -38,24 +20,21 @@ static Slic3r::TriangleMesh load_model(const std::string &obj_filename)
}
TEST_CASE("Load object", "[Hollowing]") {
TriangleMeshDataAdapter mesh{load_model("20mm_cube.obj")};
auto ptr = openvdb::tools::meshToVolume<openvdb::FloatGrid>(mesh, {});
Slic3r::TriangleMesh mesh = load_model("20mm_cube.obj");
Slic3r::sla::Contour3D imesh = Slic3r::sla::convert_mesh(mesh);
auto ptr = Slic3r::meshToVolume(imesh, {});
REQUIRE(ptr);
std::vector<openvdb::Vec3s> points;
std::vector<openvdb::Vec4I> quad_indices;
std::vector<openvdb::Vec3I> triangle_indices;
Slic3r::sla::Contour3D omesh = Slic3r::volumeToMesh(*ptr, -1., 0.0, true);
openvdb::tools::volumeToMesh(*ptr, points, triangle_indices, quad_indices, 0.0, 1.0, true);
REQUIRE(!omesh.empty());
std::cout << "Triangle count: " << triangle_indices.size() << std::endl;
std::cout << "Quad count: " << quad_indices.size() << std::endl;
std::cout << "Point count: " << points.size() << " vs " << mesh.mesh.its.vertices.size() << std::endl;
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);
}
//int main(int argc, char **argv)
//{
// ::testing::InitGoogleTest(&argc, argv);
// return RUN_ALL_TESTS();
//}

View file

@ -0,0 +1,93 @@
#include "openvdb_utils.hpp"
namespace Slic3r {
class TriangleMeshDataAdapter {
public:
const TriangleMesh &mesh;
size_t polygonCount() const { return mesh.its.indices.size(); }
size_t pointCount() const { return mesh.its.vertices.size(); }
size_t vertexCount(size_t) const { return 3; }
// Return position pos in local grid index space for polygon n and vertex v
void getIndexSpacePoint(size_t n, size_t v, openvdb::Vec3d& pos) const;
};
class Contour3DDataAdapter {
public:
const sla::Contour3D &mesh;
size_t polygonCount() const { return mesh.faces3.size() + mesh.faces4.size(); }
size_t pointCount() const { return mesh.points.size(); }
size_t vertexCount(size_t n) const { return n < mesh.faces3.size() ? 3 : 4; }
// Return position pos in local grid index space for polygon n and vertex v
void getIndexSpacePoint(size_t n, size_t v, openvdb::Vec3d& pos) const;
};
void TriangleMeshDataAdapter::getIndexSpacePoint(size_t n,
size_t v,
openvdb::Vec3d &pos) const
{
auto vidx = size_t(mesh.its.indices[n](Eigen::Index(v)));
Slic3r::Vec3d p = mesh.its.vertices[vidx].cast<double>();
pos = {p.x(), p.y(), p.z()};
}
void Contour3DDataAdapter::getIndexSpacePoint(size_t n,
size_t v,
openvdb::Vec3d &pos) const
{
size_t vidx = 0;
if (n < mesh.faces3.size()) vidx = size_t(mesh.faces3[n](Eigen::Index(v)));
else vidx = size_t(mesh.faces4[n - mesh.faces3.size()](Eigen::Index(v)));
Slic3r::Vec3d p = mesh.points[vidx];
pos = {p.x(), p.y(), p.z()};
}
openvdb::FloatGrid::Ptr meshToVolume(const TriangleMesh & mesh,
const openvdb::math::Transform &tr)
{
return openvdb::tools::meshToVolume<openvdb::FloatGrid>(
TriangleMeshDataAdapter{mesh}, tr);
}
openvdb::FloatGrid::Ptr meshToVolume(const sla::Contour3D & mesh,
const openvdb::math::Transform &tr)
{
return openvdb::tools::meshToVolume<openvdb::FloatGrid>(
Contour3DDataAdapter{mesh}, tr);
}
inline Vec3f to_vec3f(const openvdb::Vec3s &v) { return Vec3f{v.x(), v.y(), v.z()}; }
inline Vec3d to_vec3d(const openvdb::Vec3s &v) { return to_vec3f(v).cast<double>(); }
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)
{
std::vector<openvdb::Vec3s> points;
std::vector<openvdb::Vec3I> triangles;
std::vector<openvdb::Vec4I> quads;
openvdb::tools::volumeToMesh(grid, points, triangles, quads, isovalue,
adaptivity, relaxDisorientedTriangles);
sla::Contour3D ret;
ret.points.reserve(points.size());
ret.faces3.reserve(triangles.size());
ret.faces4.reserve(quads.size());
for (auto &v : points) ret.points.emplace_back(to_vec3d(v));
for (auto &v : triangles) ret.faces3.emplace_back(to_vec3i(v));
for (auto &v : quads) ret.faces4.emplace_back(to_vec4i(v));
return ret;
}
} // namespace Slic3r

View file

@ -0,0 +1,25 @@
#ifndef OPENVDB_UTILS_HPP
#define OPENVDB_UTILS_HPP
#include <libslic3r/TriangleMesh.hpp>
#include <libslic3r/SLA/SLABoilerPlate.hpp>
#include <openvdb/openvdb.h>
#include <openvdb/tools/MeshToVolume.h>
#include <openvdb/tools/VolumeToMesh.h>
namespace Slic3r {
openvdb::FloatGrid::Ptr meshToVolume(const TriangleMesh & mesh,
const openvdb::math::Transform &tr);
openvdb::FloatGrid::Ptr meshToVolume(const sla::Contour3D & mesh,
const openvdb::math::Transform &tr);
sla::Contour3D volumeToMesh(const openvdb::FloatGrid &grid,
double isovalue = 0.0,
double adaptivity = 0.0,
bool relaxDisorientedTriangles = true);
} // namespace Slic3r
#endif // OPENVDB_UTILS_HPP