From cdf80c3b3f9447437257b9fa2acd72b3d93dabb2 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Fri, 22 May 2020 13:27:00 +0200 Subject: [PATCH 1/5] Switched to new AABB tree implementation for raycasting --- src/libslic3r/SLA/Common.cpp | 86 +++++++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 6 deletions(-) diff --git a/src/libslic3r/SLA/Common.cpp b/src/libslic3r/SLA/Common.cpp index b57039ad13..806dc4d663 100644 --- a/src/libslic3r/SLA/Common.cpp +++ b/src/libslic3r/SLA/Common.cpp @@ -7,6 +7,7 @@ #include #include #include +#include // Workaround: IGL signed_distance.h will define PI in the igl namespace. @@ -23,11 +24,23 @@ #pragma warning(disable: 4244) #pragma warning(disable: 4267) #endif -#include -#include + +#define USE_AABB_INDIRECT // Vojta's AABB (defined) vs igl::AABB (undefined) + +#ifdef SLIC3R_SLA_NEEDS_WINDTREE + #ifdef USE_AABB_INDIRECT + #error These two options contradict each other. + #endif + #include +#endif + +#ifndef USE_AABB_INDIRECT + #include +#endif + #include -#include -#include + + #ifdef _MSC_VER #pragma warning(pop) #endif @@ -40,7 +53,7 @@ namespace Slic3r { namespace sla { // Bring back PI from the igl namespace -using igl::PI; +//using igl::PI; /* ************************************************************************** * PointIndex implementation @@ -188,8 +201,59 @@ void BoxIndex::foreach(std::function fn) * EigenMesh3D implementation * ****************************************************************************/ +#ifdef USE_AABB_INDIRECT +class EigenMesh3D::AABBImpl { +#else class EigenMesh3D::AABBImpl: public igl::AABB { +#endif + public: +#ifdef USE_AABB_INDIRECT +private: + AABBTreeIndirect::Tree3f m_tree; + TriangleMesh m_triangle_mesh; // FIXME: There should be no extra copy + // maybe even the m_V and m_F are extra. This is just to see the new + // AABB tree in action +public: + void init(const TriangleMesh& tmesh) + { + m_triangle_mesh = tmesh; + m_tree = AABBTreeIndirect::build_aabb_tree_over_indexed_triangle_set( + m_triangle_mesh.its.vertices, m_triangle_mesh.its.indices); + } + + void intersect_ray(const Eigen::MatrixXd&, const Eigen::MatrixXi&, + const Vec3d& s, const Vec3d& dir, igl::Hit& hit) + { + AABBTreeIndirect::intersect_ray_first_hit(m_triangle_mesh.its.vertices, + m_triangle_mesh.its.indices, + m_tree, + s, dir, hit); + } + + void intersect_ray(const Eigen::MatrixXd&, const Eigen::MatrixXi&, + const Vec3d& s, const Vec3d& dir, std::vector& hits) + { + AABBTreeIndirect::intersect_ray_all_hits(m_triangle_mesh.its.vertices, + m_triangle_mesh.its.indices, + m_tree, + s, dir, hits); + } + + double squared_distance(const Eigen::MatrixXd&, const Eigen::MatrixXi&, + const Vec3d& point, int& i, Eigen::Matrix& closest) { + size_t idx_unsigned = 0; + Vec3d closest_vec3d(closest); + double dist = AABBTreeIndirect::squared_distance_to_indexed_triangle_set( + m_triangle_mesh.its.vertices, + m_triangle_mesh.its.indices, + m_tree, point, idx_unsigned, closest_vec3d); + i = int(idx_unsigned); + closest = closest_vec3d; + return dist; + } +#endif + #ifdef SLIC3R_SLA_NEEDS_WINDTREE igl::WindingNumberAABB windtree; #endif /* SLIC3R_SLA_NEEDS_WINDTREE */ @@ -225,7 +289,11 @@ void to_eigen_mesh(const TriangleMesh &tmesh, Eigen::MatrixXd &V, Eigen::MatrixX } } -void to_triangle_mesh(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, TriangleMesh &out) +void to_triangle_mesh(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, TriangleMesh &out); +#if 0 +Does this function really work? There seems to be an issue with it. +Currently it is not used anywhere, this way it stays visible but +trigger linking error when attempting to use it. { Pointf3s points(size_t(V.rows())); std::vector facets(size_t(F.rows())); @@ -238,6 +306,7 @@ void to_triangle_mesh(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, Triang out = {points, facets}; } +#endif EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh): m_aabb(new AABBImpl()) { auto&& bb = tmesh.bounding_box(); @@ -246,7 +315,12 @@ EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh): m_aabb(new AABBImpl()) { to_eigen_mesh(tmesh, m_V, m_F); // Build the AABB accelaration tree +#ifdef USE_AABB_INDIRECT + m_aabb->init(tmesh); +#else m_aabb->init(m_V, m_F); +#endif + #ifdef SLIC3R_SLA_NEEDS_WINDTREE m_aabb->windtree.set_mesh(m_V, m_F); #endif /* SLIC3R_SLA_NEEDS_WINDTREE */ From 9224a6a3e68b9f749beedf713cf01a3042d3ea41 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Fri, 22 May 2020 17:21:54 +0200 Subject: [PATCH 2/5] Removed some unused code - removed define USE_AABB_INDIRECT (which switched between old and new AABB implementation) - removed define SLIC3R_SLA_NEEDS_WINDTREE (relied on igl and was not used anyway) - new define SLIC3R_HOLE_RAYCASTER (hides currently unused code) - slight include cleanup - removed obsolete source file SupportTreeIGL.cpp --- src/libslic3r/SLA/Common.cpp | 95 +-- src/libslic3r/SLA/Common.hpp | 6 - src/libslic3r/SLA/EigenMesh3D.hpp | 36 +- src/libslic3r/SLA/SupportPointGenerator.hpp | 1 + src/libslic3r/SLA/SupportTreeIGL.cpp | 621 -------------------- 5 files changed, 45 insertions(+), 714 deletions(-) delete mode 100644 src/libslic3r/SLA/SupportTreeIGL.cpp diff --git a/src/libslic3r/SLA/Common.cpp b/src/libslic3r/SLA/Common.cpp index 806dc4d663..56c9d12ff3 100644 --- a/src/libslic3r/SLA/Common.cpp +++ b/src/libslic3r/SLA/Common.cpp @@ -1,20 +1,12 @@ #include #include #include -#include #include #include #include #include -#include #include - -// Workaround: IGL signed_distance.h will define PI in the igl namespace. -#undef PI - -// HEAVY headers... takes eternity to compile - // for concave hull merging decisions #include #include "boost/geometry/index/rtree.hpp" @@ -25,35 +17,22 @@ #pragma warning(disable: 4267) #endif -#define USE_AABB_INDIRECT // Vojta's AABB (defined) vs igl::AABB (undefined) - -#ifdef SLIC3R_SLA_NEEDS_WINDTREE - #ifdef USE_AABB_INDIRECT - #error These two options contradict each other. - #endif - #include -#endif - -#ifndef USE_AABB_INDIRECT - #include -#endif #include +#ifdef SLIC3R_HOLE_RAYCASTER + #include +#endif + #ifdef _MSC_VER #pragma warning(pop) #endif -#include - -#include "ClipperUtils.hpp" namespace Slic3r { namespace sla { -// Bring back PI from the igl namespace -//using igl::PI; /* ************************************************************************** * PointIndex implementation @@ -201,14 +180,8 @@ void BoxIndex::foreach(std::function fn) * EigenMesh3D implementation * ****************************************************************************/ -#ifdef USE_AABB_INDIRECT -class EigenMesh3D::AABBImpl { -#else -class EigenMesh3D::AABBImpl: public igl::AABB { -#endif -public: -#ifdef USE_AABB_INDIRECT +class EigenMesh3D::AABBImpl { private: AABBTreeIndirect::Tree3f m_tree; TriangleMesh m_triangle_mesh; // FIXME: There should be no extra copy @@ -252,11 +225,6 @@ public: closest = closest_vec3d; return dist; } -#endif - -#ifdef SLIC3R_SLA_NEEDS_WINDTREE - igl::WindingNumberAABB windtree; -#endif /* SLIC3R_SLA_NEEDS_WINDTREE */ }; static const constexpr double MESH_EPS = 1e-6; @@ -315,15 +283,7 @@ EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh): m_aabb(new AABBImpl()) { to_eigen_mesh(tmesh, m_V, m_F); // Build the AABB accelaration tree -#ifdef USE_AABB_INDIRECT m_aabb->init(tmesh); -#else - m_aabb->init(m_V, m_F); -#endif - -#ifdef SLIC3R_SLA_NEEDS_WINDTREE - m_aabb->windtree.set_mesh(m_V, m_F); -#endif /* SLIC3R_SLA_NEEDS_WINDTREE */ } EigenMesh3D::~EigenMesh3D() {} @@ -371,24 +331,25 @@ EigenMesh3D::query_ray_hit(const Vec3d &s, const Vec3d &dir) const igl::Hit hit; hit.t = std::numeric_limits::infinity(); - if (m_holes.empty()) { - m_aabb->intersect_ray(m_V, m_F, s, dir, hit); - hit_result ret(*this); - ret.m_t = double(hit.t); - ret.m_dir = dir; - ret.m_source = s; - if(!std::isinf(hit.t) && !std::isnan(hit.t)) { - ret.m_normal = this->normal_by_face_id(hit.id); - ret.m_face_id = hit.id; - } - - return ret; - } - else { +#ifdef SLIC3R_HOLE_RAYCASTER + if (! m_holes.empty()) { // If there are holes, the hit_results will be made by // query_ray_hits (object) and filter_hits (holes): return filter_hits(query_ray_hits(s, dir)); } +#endif + + m_aabb->intersect_ray(m_V, m_F, s, dir, hit); + hit_result ret(*this); + ret.m_t = double(hit.t); + ret.m_dir = dir; + ret.m_source = s; + if(!std::isinf(hit.t) && !std::isnan(hit.t)) { + ret.m_normal = this->normal_by_face_id(hit.id); + ret.m_face_id = hit.id; + } + + return ret; } std::vector @@ -425,6 +386,8 @@ EigenMesh3D::query_ray_hits(const Vec3d &s, const Vec3d &dir) const return outs; } + +#ifdef SLIC3R_HOLE_RAYCASTER EigenMesh3D::hit_result EigenMesh3D::filter_hits( const std::vector& object_hits) const { @@ -519,20 +482,8 @@ EigenMesh3D::hit_result EigenMesh3D::filter_hits( // if we got here, the ray ended up in infinity return out; } +#endif -#ifdef SLIC3R_SLA_NEEDS_WINDTREE -EigenMesh3D::si_result EigenMesh3D::signed_distance(const Vec3d &p) const { - double sign = 0; double sqdst = 0; int i = 0; Vec3d c; - igl::signed_distance_winding_number(*m_aabb, m_V, m_F, m_aabb->windtree, - p, sign, sqdst, i, c); - - return si_result(sign * std::sqrt(sqdst), i, c); -} - -bool EigenMesh3D::inside(const Vec3d &p) const { - return m_aabb->windtree.inside(p); -} -#endif /* SLIC3R_SLA_NEEDS_WINDTREE */ double EigenMesh3D::squared_distance(const Vec3d &p, int& i, Vec3d& c) const { double sqdst = 0; diff --git a/src/libslic3r/SLA/Common.hpp b/src/libslic3r/SLA/Common.hpp index e1c5930e2a..91bdc0eb1d 100644 --- a/src/libslic3r/SLA/Common.hpp +++ b/src/libslic3r/SLA/Common.hpp @@ -7,12 +7,6 @@ #include #include -//#include "SLASpatIndex.hpp" - -//#include -//#include - -// #define SLIC3R_SLA_NEEDS_WINDTREE namespace Slic3r { diff --git a/src/libslic3r/SLA/EigenMesh3D.hpp b/src/libslic3r/SLA/EigenMesh3D.hpp index 014a57e820..248f3577fc 100644 --- a/src/libslic3r/SLA/EigenMesh3D.hpp +++ b/src/libslic3r/SLA/EigenMesh3D.hpp @@ -2,7 +2,16 @@ #define SLA_EIGENMESH3D_H #include -#include "libslic3r/SLA/Hollowing.hpp" + + +// There is an implementation of a hole-aware raycaster that was eventually +// not used in production version. It is now hidden under following define +// for possible future use. +//#define SLIC3R_HOLE_RAYCASTER + +#ifdef SLIC3R_HOLE_RAYCASTER + #include "libslic3r/SLA/Hollowing.hpp" +#endif namespace Slic3r { @@ -27,9 +36,11 @@ class EigenMesh3D { std::unique_ptr m_aabb; +#ifdef SLIC3R_HOLE_RAYCASTER // This holds a copy of holes in the mesh. Initialized externally // by load_mesh setter. std::vector m_holes; +#endif public: @@ -88,25 +99,27 @@ public: return is_hit() && normal().dot(m_dir) > 0; } }; - + +#ifdef SLIC3R_HOLE_RAYCASTER // Inform the object about location of holes // creates internal copy of the vector void load_holes(const std::vector& holes) { m_holes = holes; } - // Casting a ray on the mesh, returns the distance where the hit occures. - hit_result query_ray_hit(const Vec3d &s, const Vec3d &dir) const; - - // Casts a ray on the mesh and returns all hits - std::vector query_ray_hits(const Vec3d &s, const Vec3d &dir) const; - // Iterates over hits and holes and returns the true hit, possibly // on the inside of a hole. // This function is currently not used anywhere, it was written when the // holes were subtracted on slices, that is, before we started using CGAL // to actually cut the holes into the mesh. hit_result filter_hits(const std::vector& obj_hits) const; +#endif + + // Casting a ray on the mesh, returns the distance where the hit occures. + hit_result query_ray_hit(const Vec3d &s, const Vec3d &dir) const; + + // Casts a ray on the mesh and returns all hits + std::vector query_ray_hits(const Vec3d &s, const Vec3d &dir) const; class si_result { double m_value; @@ -125,13 +138,6 @@ public: int F_idx() const { return m_fidx; } }; -#ifdef SLIC3R_SLA_NEEDS_WINDTREE - // The signed distance from a point to the mesh. Outputs the distance, - // the index of the triangle and the closest point in mesh coordinate space. - si_result signed_distance(const Vec3d& p) const; - - bool inside(const Vec3d& p) const; -#endif /* SLIC3R_SLA_NEEDS_WINDTREE */ double squared_distance(const Vec3d& p, int& i, Vec3d& c) const; inline double squared_distance(const Vec3d &p) const diff --git a/src/libslic3r/SLA/SupportPointGenerator.hpp b/src/libslic3r/SLA/SupportPointGenerator.hpp index 1621cde50f..2fe8e11fc7 100644 --- a/src/libslic3r/SLA/SupportPointGenerator.hpp +++ b/src/libslic3r/SLA/SupportPointGenerator.hpp @@ -7,6 +7,7 @@ #include #include +#include #include #include diff --git a/src/libslic3r/SLA/SupportTreeIGL.cpp b/src/libslic3r/SLA/SupportTreeIGL.cpp deleted file mode 100644 index ea2be6be11..0000000000 --- a/src/libslic3r/SLA/SupportTreeIGL.cpp +++ /dev/null @@ -1,621 +0,0 @@ -#include -#include "SLA/SLASupportTree.hpp" -#include "SLA/SLACommon.hpp" -#include "SLA/SLASpatIndex.hpp" - -// Workaround: IGL signed_distance.h will define PI in the igl namespace. -#undef PI - -// HEAVY headers... takes eternity to compile - -// for concave hull merging decisions -#include "SLABoostAdapter.hpp" -#include "boost/geometry/index/rtree.hpp" - -#ifdef _MSC_VER -#pragma warning(push) -#pragma warning(disable: 4244) -#pragma warning(disable: 4267) -#endif -#include -#include -#include -#include -#ifdef _MSC_VER -#pragma warning(pop) -#endif - -#include - -#include "SLASpatIndex.hpp" -#include "ClipperUtils.hpp" - -namespace Slic3r { -namespace sla { - -// Bring back PI from the igl namespace -using igl::PI; - -/* ************************************************************************** - * PointIndex implementation - * ************************************************************************** */ - -class PointIndex::Impl { -public: - using BoostIndex = boost::geometry::index::rtree< PointIndexEl, - boost::geometry::index::rstar<16, 4> /* ? */ >; - - BoostIndex m_store; -}; - -PointIndex::PointIndex(): m_impl(new Impl()) {} -PointIndex::~PointIndex() {} - -PointIndex::PointIndex(const PointIndex &cpy): m_impl(new Impl(*cpy.m_impl)) {} -PointIndex::PointIndex(PointIndex&& cpy): m_impl(std::move(cpy.m_impl)) {} - -PointIndex& PointIndex::operator=(const PointIndex &cpy) -{ - m_impl.reset(new Impl(*cpy.m_impl)); - return *this; -} - -PointIndex& PointIndex::operator=(PointIndex &&cpy) -{ - m_impl.swap(cpy.m_impl); - return *this; -} - -void PointIndex::insert(const PointIndexEl &el) -{ - m_impl->m_store.insert(el); -} - -bool PointIndex::remove(const PointIndexEl& el) -{ - return m_impl->m_store.remove(el) == 1; -} - -std::vector -PointIndex::query(std::function fn) const -{ - namespace bgi = boost::geometry::index; - - std::vector ret; - m_impl->m_store.query(bgi::satisfies(fn), std::back_inserter(ret)); - return ret; -} - -std::vector PointIndex::nearest(const Vec3d &el, unsigned k = 1) const -{ - namespace bgi = boost::geometry::index; - std::vector ret; ret.reserve(k); - m_impl->m_store.query(bgi::nearest(el, k), std::back_inserter(ret)); - return ret; -} - -size_t PointIndex::size() const -{ - return m_impl->m_store.size(); -} - -void PointIndex::foreach(std::function fn) -{ - for(auto& el : m_impl->m_store) fn(el); -} - -void PointIndex::foreach(std::function fn) const -{ - for(const auto &el : m_impl->m_store) fn(el); -} - -/* ************************************************************************** - * BoxIndex implementation - * ************************************************************************** */ - -class BoxIndex::Impl { -public: - using BoostIndex = boost::geometry::index:: - rtree /* ? */>; - - BoostIndex m_store; -}; - -BoxIndex::BoxIndex(): m_impl(new Impl()) {} -BoxIndex::~BoxIndex() {} - -BoxIndex::BoxIndex(const BoxIndex &cpy): m_impl(new Impl(*cpy.m_impl)) {} -BoxIndex::BoxIndex(BoxIndex&& cpy): m_impl(std::move(cpy.m_impl)) {} - -BoxIndex& BoxIndex::operator=(const BoxIndex &cpy) -{ - m_impl.reset(new Impl(*cpy.m_impl)); - return *this; -} - -BoxIndex& BoxIndex::operator=(BoxIndex &&cpy) -{ - m_impl.swap(cpy.m_impl); - return *this; -} - -void BoxIndex::insert(const BoxIndexEl &el) -{ - m_impl->m_store.insert(el); -} - -bool BoxIndex::remove(const BoxIndexEl& el) -{ - return m_impl->m_store.remove(el) == 1; -} - -std::vector BoxIndex::query(const BoundingBox &qrbb, - BoxIndex::QueryType qt) -{ - namespace bgi = boost::geometry::index; - - std::vector ret; ret.reserve(m_impl->m_store.size()); - - switch (qt) { - case qtIntersects: - m_impl->m_store.query(bgi::intersects(qrbb), std::back_inserter(ret)); - break; - case qtWithin: - m_impl->m_store.query(bgi::within(qrbb), std::back_inserter(ret)); - } - - return ret; -} - -size_t BoxIndex::size() const -{ - return m_impl->m_store.size(); -} - -void BoxIndex::foreach(std::function fn) -{ - for(auto& el : m_impl->m_store) fn(el); -} - -/* **************************************************************************** - * EigenMesh3D implementation - * ****************************************************************************/ - -class EigenMesh3D::AABBImpl: public igl::AABB { -public: -#ifdef SLIC3R_SLA_NEEDS_WINDTREE - igl::WindingNumberAABB windtree; -#endif /* SLIC3R_SLA_NEEDS_WINDTREE */ -}; - -EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh): m_aabb(new AABBImpl()) { - static const double dEPS = 1e-6; - - const stl_file& stl = tmesh.stl; - - auto&& bb = tmesh.bounding_box(); - m_ground_level += bb.min(Z); - - Eigen::MatrixXd V; - Eigen::MatrixXi F; - - V.resize(3*stl.stats.number_of_facets, 3); - F.resize(stl.stats.number_of_facets, 3); - for (unsigned int i = 0; i < stl.stats.number_of_facets; ++i) { - const stl_facet &facet = stl.facet_start[i]; - V.block<1, 3>(3 * i + 0, 0) = facet.vertex[0].cast(); - V.block<1, 3>(3 * i + 1, 0) = facet.vertex[1].cast(); - V.block<1, 3>(3 * i + 2, 0) = facet.vertex[2].cast(); - F(i, 0) = int(3*i+0); - F(i, 1) = int(3*i+1); - F(i, 2) = int(3*i+2); - } - - // We will convert this to a proper 3d mesh with no duplicate points. - Eigen::VectorXi SVI, SVJ; - igl::remove_duplicate_vertices(V, F, dEPS, m_V, SVI, SVJ, m_F); - - // Build the AABB accelaration tree - m_aabb->init(m_V, m_F); -#ifdef SLIC3R_SLA_NEEDS_WINDTREE - m_aabb->windtree.set_mesh(m_V, m_F); -#endif /* SLIC3R_SLA_NEEDS_WINDTREE */ -} - -EigenMesh3D::~EigenMesh3D() {} - -EigenMesh3D::EigenMesh3D(const EigenMesh3D &other): - m_V(other.m_V), m_F(other.m_F), m_ground_level(other.m_ground_level), - m_aabb( new AABBImpl(*other.m_aabb) ) {} - -EigenMesh3D::EigenMesh3D(const Contour3D &other) -{ - m_V.resize(Eigen::Index(other.points.size()), 3); - m_F.resize(Eigen::Index(other.faces3.size() + 2 * other.faces4.size()), 3); - - for (Eigen::Index i = 0; i < Eigen::Index(other.points.size()); ++i) - m_V.row(i) = other.points[size_t(i)]; - - for (Eigen::Index i = 0; i < Eigen::Index(other.faces3.size()); ++i) - m_F.row(i) = other.faces3[size_t(i)]; - - size_t N = other.faces3.size() + 2 * other.faces4.size(); - for (size_t i = other.faces3.size(); i < N; i += 2) { - size_t quad_idx = (i - other.faces3.size()) / 2; - auto & quad = other.faces4[quad_idx]; - m_F.row(Eigen::Index(i)) = Vec3i{quad(0), quad(1), quad(2)}; - m_F.row(Eigen::Index(i + 1)) = Vec3i{quad(2), quad(3), quad(0)}; - } -} - -EigenMesh3D &EigenMesh3D::operator=(const EigenMesh3D &other) -{ - m_V = other.m_V; - m_F = other.m_F; - m_ground_level = other.m_ground_level; - m_aabb.reset(new AABBImpl(*other.m_aabb)); return *this; -} - -EigenMesh3D::hit_result -EigenMesh3D::query_ray_hit(const Vec3d &s, const Vec3d &dir) const -{ - igl::Hit hit; - hit.t = std::numeric_limits::infinity(); - m_aabb->intersect_ray(m_V, m_F, s, dir, hit); - - hit_result ret(*this); - ret.m_t = double(hit.t); - ret.m_dir = dir; - ret.m_source = s; - if(!std::isinf(hit.t) && !std::isnan(hit.t)) ret.m_face_id = hit.id; - - return ret; -} - -std::vector -EigenMesh3D::query_ray_hits(const Vec3d &s, const Vec3d &dir) const -{ - std::vector outs; - std::vector hits; - m_aabb->intersect_ray(m_V, m_F, s, dir, hits); - - // The sort is necessary, the hits are not always sorted. - std::sort(hits.begin(), hits.end(), - [](const igl::Hit& a, const igl::Hit& b) { return a.t < b.t; }); - - // Convert the igl::Hit into hit_result - outs.reserve(hits.size()); - for (const igl::Hit& hit : hits) { - outs.emplace_back(EigenMesh3D::hit_result(*this)); - outs.back().m_t = double(hit.t); - outs.back().m_dir = dir; - outs.back().m_source = s; - if(!std::isinf(hit.t) && !std::isnan(hit.t)) - outs.back().m_face_id = hit.id; - } - - return outs; -} - -#ifdef SLIC3R_SLA_NEEDS_WINDTREE -EigenMesh3D::si_result EigenMesh3D::signed_distance(const Vec3d &p) const { - double sign = 0; double sqdst = 0; int i = 0; Vec3d c; - igl::signed_distance_winding_number(*m_aabb, m_V, m_F, m_aabb->windtree, - p, sign, sqdst, i, c); - - return si_result(sign * std::sqrt(sqdst), i, c); -} - -bool EigenMesh3D::inside(const Vec3d &p) const { - return m_aabb->windtree.inside(p); -} -#endif /* SLIC3R_SLA_NEEDS_WINDTREE */ - -double EigenMesh3D::squared_distance(const Vec3d &p, int& i, Vec3d& c) const { - double sqdst = 0; - Eigen::Matrix pp = p; - Eigen::Matrix cc; - sqdst = m_aabb->squared_distance(m_V, m_F, pp, i, cc); - c = cc; - return sqdst; -} - -/* **************************************************************************** - * Misc functions - * ****************************************************************************/ - -namespace { - -bool point_on_edge(const Vec3d& p, const Vec3d& e1, const Vec3d& e2, - double eps = 0.05) -{ - using Line3D = Eigen::ParametrizedLine; - - auto line = Line3D::Through(e1, e2); - double d = line.distance(p); - return std::abs(d) < eps; -} - -template double distance(const Vec& pp1, const Vec& pp2) { - auto p = pp2 - pp1; - return std::sqrt(p.transpose() * p); -} - -} - -PointSet normals(const PointSet& points, - const EigenMesh3D& mesh, - double eps, - std::function thr, // throw on cancel - const std::vector& pt_indices) -{ - if(points.rows() == 0 || mesh.V().rows() == 0 || mesh.F().rows() == 0) - return {}; - - std::vector range = pt_indices; - if(range.empty()) { - range.resize(size_t(points.rows()), 0); - std::iota(range.begin(), range.end(), 0); - } - - PointSet ret(range.size(), 3); - -// for (size_t ridx = 0; ridx < range.size(); ++ridx) - tbb::parallel_for(size_t(0), range.size(), - [&ret, &range, &mesh, &points, thr, eps](size_t ridx) - { - thr(); - auto eidx = Eigen::Index(range[ridx]); - int faceid = 0; - Vec3d p; - - mesh.squared_distance(points.row(eidx), faceid, p); - - auto trindex = mesh.F().row(faceid); - - const Vec3d& p1 = mesh.V().row(trindex(0)); - const Vec3d& p2 = mesh.V().row(trindex(1)); - const Vec3d& p3 = mesh.V().row(trindex(2)); - - // We should check if the point lies on an edge of the hosting triangle. - // If it does then all the other triangles using the same two points - // have to be searched and the final normal should be some kind of - // aggregation of the participating triangle normals. We should also - // consider the cases where the support point lies right on a vertex - // of its triangle. The procedure is the same, get the neighbor - // triangles and calculate an average normal. - - // mark the vertex indices of the edge. ia and ib marks and edge ic - // will mark a single vertex. - int ia = -1, ib = -1, ic = -1; - - if(std::abs(distance(p, p1)) < eps) { - ic = trindex(0); - } - else if(std::abs(distance(p, p2)) < eps) { - ic = trindex(1); - } - else if(std::abs(distance(p, p3)) < eps) { - ic = trindex(2); - } - else if(point_on_edge(p, p1, p2, eps)) { - ia = trindex(0); ib = trindex(1); - } - else if(point_on_edge(p, p2, p3, eps)) { - ia = trindex(1); ib = trindex(2); - } - else if(point_on_edge(p, p1, p3, eps)) { - ia = trindex(0); ib = trindex(2); - } - - // vector for the neigboring triangles including the detected one. - std::vector neigh; - if(ic >= 0) { // The point is right on a vertex of the triangle - for(int n = 0; n < mesh.F().rows(); ++n) { - thr(); - Vec3i ni = mesh.F().row(n); - if((ni(X) == ic || ni(Y) == ic || ni(Z) == ic)) - neigh.emplace_back(ni); - } - } - else if(ia >= 0 && ib >= 0) { // the point is on and edge - // now get all the neigboring triangles - for(int n = 0; n < mesh.F().rows(); ++n) { - thr(); - Vec3i ni = mesh.F().row(n); - if((ni(X) == ia || ni(Y) == ia || ni(Z) == ia) && - (ni(X) == ib || ni(Y) == ib || ni(Z) == ib)) - neigh.emplace_back(ni); - } - } - - // Calculate the normals for the neighboring triangles - std::vector neighnorms; neighnorms.reserve(neigh.size()); - for(const Vec3i& tri : neigh) { - const Vec3d& pt1 = mesh.V().row(tri(0)); - const Vec3d& pt2 = mesh.V().row(tri(1)); - const Vec3d& pt3 = mesh.V().row(tri(2)); - Eigen::Vector3d U = pt2 - pt1; - Eigen::Vector3d V = pt3 - pt1; - neighnorms.emplace_back(U.cross(V).normalized()); - } - - // Throw out duplicates. They would cause trouble with summing. We will - // use std::unique which works on sorted ranges. We will sort by the - // coefficient-wise sum of the normals. It should force the same - // elements to be consecutive. - std::sort(neighnorms.begin(), neighnorms.end(), - [](const Vec3d& v1, const Vec3d& v2){ - return v1.sum() < v2.sum(); - }); - - auto lend = std::unique(neighnorms.begin(), neighnorms.end(), - [](const Vec3d& n1, const Vec3d& n2) { - // Compare normals for equivalence. This is controvers stuff. - auto deq = [](double a, double b) { return std::abs(a-b) < 1e-3; }; - return deq(n1(X), n2(X)) && deq(n1(Y), n2(Y)) && deq(n1(Z), n2(Z)); - }); - - if(!neighnorms.empty()) { // there were neighbors to count with - // sum up the normals and then normalize the result again. - // This unification seems to be enough. - Vec3d sumnorm(0, 0, 0); - sumnorm = std::accumulate(neighnorms.begin(), lend, sumnorm); - sumnorm.normalize(); - ret.row(long(ridx)) = sumnorm; - } - else { // point lies safely within its triangle - Eigen::Vector3d U = p2 - p1; - Eigen::Vector3d V = p3 - p1; - ret.row(long(ridx)) = U.cross(V).normalized(); - } - }); - - return ret; -} - -namespace bgi = boost::geometry::index; -using Index3D = bgi::rtree< PointIndexEl, bgi::rstar<16, 4> /* ? */ >; - -namespace { - -bool cmp_ptidx_elements(const PointIndexEl& e1, const PointIndexEl& e2) -{ - return e1.second < e2.second; -}; - -ClusteredPoints cluster(Index3D &sindex, - unsigned max_points, - std::function( - const Index3D &, const PointIndexEl &)> qfn) -{ - using Elems = std::vector; - - // Recursive function for visiting all the points in a given distance to - // each other - std::function group = - [&sindex, &group, max_points, qfn](Elems& pts, Elems& cluster) - { - for(auto& p : pts) { - std::vector tmp = qfn(sindex, p); - - std::sort(tmp.begin(), tmp.end(), cmp_ptidx_elements); - - Elems newpts; - std::set_difference(tmp.begin(), tmp.end(), - cluster.begin(), cluster.end(), - std::back_inserter(newpts), cmp_ptidx_elements); - - int c = max_points && newpts.size() + cluster.size() > max_points? - int(max_points - cluster.size()) : int(newpts.size()); - - cluster.insert(cluster.end(), newpts.begin(), newpts.begin() + c); - std::sort(cluster.begin(), cluster.end(), cmp_ptidx_elements); - - if(!newpts.empty() && (!max_points || cluster.size() < max_points)) - group(newpts, cluster); - } - }; - - std::vector clusters; - for(auto it = sindex.begin(); it != sindex.end();) { - Elems cluster = {}; - Elems pts = {*it}; - group(pts, cluster); - - for(auto& c : cluster) sindex.remove(c); - it = sindex.begin(); - - clusters.emplace_back(cluster); - } - - ClusteredPoints result; - for(auto& cluster : clusters) { - result.emplace_back(); - for(auto c : cluster) result.back().emplace_back(c.second); - } - - return result; -} - -std::vector distance_queryfn(const Index3D& sindex, - const PointIndexEl& p, - double dist, - unsigned max_points) -{ - std::vector tmp; tmp.reserve(max_points); - sindex.query( - bgi::nearest(p.first, max_points), - std::back_inserter(tmp) - ); - - for(auto it = tmp.begin(); it < tmp.end(); ++it) - if(distance(p.first, it->first) > dist) it = tmp.erase(it); - - return tmp; -} - -} // namespace - -// Clustering a set of points by the given criteria -ClusteredPoints cluster( - const std::vector& indices, - std::function pointfn, - double dist, - unsigned max_points) -{ - // A spatial index for querying the nearest points - Index3D sindex; - - // Build the index - for(auto idx : indices) sindex.insert( std::make_pair(pointfn(idx), idx)); - - return cluster(sindex, max_points, - [dist, max_points](const Index3D& sidx, const PointIndexEl& p) - { - return distance_queryfn(sidx, p, dist, max_points); - }); -} - -// Clustering a set of points by the given criteria -ClusteredPoints cluster( - const std::vector& indices, - std::function pointfn, - std::function predicate, - unsigned max_points) -{ - // A spatial index for querying the nearest points - Index3D sindex; - - // Build the index - for(auto idx : indices) sindex.insert( std::make_pair(pointfn(idx), idx)); - - return cluster(sindex, max_points, - [max_points, predicate](const Index3D& sidx, const PointIndexEl& p) - { - std::vector tmp; tmp.reserve(max_points); - sidx.query(bgi::satisfies([p, predicate](const PointIndexEl& e){ - return predicate(p, e); - }), std::back_inserter(tmp)); - return tmp; - }); -} - -ClusteredPoints cluster(const PointSet& pts, double dist, unsigned max_points) -{ - // A spatial index for querying the nearest points - Index3D sindex; - - // Build the index - for(Eigen::Index i = 0; i < pts.rows(); i++) - sindex.insert(std::make_pair(Vec3d(pts.row(i)), unsigned(i))); - - return cluster(sindex, max_points, - [dist, max_points](const Index3D& sidx, const PointIndexEl& p) - { - return distance_queryfn(sidx, p, dist, max_points); - }); -} - -} // namespace sla -} // namespace Slic3r From d85fa8e9ab5a3a4d245487dca43c0aeabf1062b9 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Fri, 22 May 2020 18:21:52 +0200 Subject: [PATCH 3/5] EigenMesh3D now stores TriangleMesh inside, not a mesh in Eigen format Rotfinder was apparently building the AABB tree needlessly --- src/libslic3r/SLA/Common.cpp | 73 +++++++++++++++---------------- src/libslic3r/SLA/Contour3D.cpp | 12 ++--- src/libslic3r/SLA/EigenMesh3D.hpp | 26 ++++++----- src/libslic3r/SLA/Rotfinder.cpp | 20 +++------ 4 files changed, 62 insertions(+), 69 deletions(-) diff --git a/src/libslic3r/SLA/Common.cpp b/src/libslic3r/SLA/Common.cpp index 56c9d12ff3..3182a2febe 100644 --- a/src/libslic3r/SLA/Common.cpp +++ b/src/libslic3r/SLA/Common.cpp @@ -184,42 +184,39 @@ void BoxIndex::foreach(std::function fn) class EigenMesh3D::AABBImpl { private: AABBTreeIndirect::Tree3f m_tree; - TriangleMesh m_triangle_mesh; // FIXME: There should be no extra copy - // maybe even the m_V and m_F are extra. This is just to see the new - // AABB tree in action + public: - void init(const TriangleMesh& tmesh) + void init(const TriangleMesh& tm) { - m_triangle_mesh = tmesh; m_tree = AABBTreeIndirect::build_aabb_tree_over_indexed_triangle_set( - m_triangle_mesh.its.vertices, m_triangle_mesh.its.indices); + tm.its.vertices, tm.its.indices); } - void intersect_ray(const Eigen::MatrixXd&, const Eigen::MatrixXi&, + void intersect_ray(const TriangleMesh& tm, const Vec3d& s, const Vec3d& dir, igl::Hit& hit) { - AABBTreeIndirect::intersect_ray_first_hit(m_triangle_mesh.its.vertices, - m_triangle_mesh.its.indices, + AABBTreeIndirect::intersect_ray_first_hit(tm.its.vertices, + tm.its.indices, m_tree, s, dir, hit); } - void intersect_ray(const Eigen::MatrixXd&, const Eigen::MatrixXi&, + void intersect_ray(const TriangleMesh& tm, const Vec3d& s, const Vec3d& dir, std::vector& hits) { - AABBTreeIndirect::intersect_ray_all_hits(m_triangle_mesh.its.vertices, - m_triangle_mesh.its.indices, + AABBTreeIndirect::intersect_ray_all_hits(tm.its.vertices, + tm.its.indices, m_tree, s, dir, hits); } - double squared_distance(const Eigen::MatrixXd&, const Eigen::MatrixXi&, + double squared_distance(const TriangleMesh& tm, const Vec3d& point, int& i, Eigen::Matrix& closest) { size_t idx_unsigned = 0; Vec3d closest_vec3d(closest); double dist = AABBTreeIndirect::squared_distance_to_indexed_triangle_set( - m_triangle_mesh.its.vertices, - m_triangle_mesh.its.indices, + tm.its.vertices, + tm.its.indices, m_tree, point, idx_unsigned, closest_vec3d); i = int(idx_unsigned); closest = closest_vec3d; @@ -276,12 +273,12 @@ trigger linking error when attempting to use it. } #endif -EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh): m_aabb(new AABBImpl()) { +EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh) + : m_aabb(new AABBImpl()), m_tm(tmesh) +{ auto&& bb = tmesh.bounding_box(); m_ground_level += bb.min(Z); - to_eigen_mesh(tmesh, m_V, m_F); - // Build the AABB accelaration tree m_aabb->init(tmesh); } @@ -289,10 +286,10 @@ EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh): m_aabb(new AABBImpl()) { EigenMesh3D::~EigenMesh3D() {} EigenMesh3D::EigenMesh3D(const EigenMesh3D &other): - m_V(other.m_V), m_F(other.m_F), m_ground_level(other.m_ground_level), + m_tm(other.m_tm), m_ground_level(other.m_ground_level), m_aabb( new AABBImpl(*other.m_aabb) ) {} -EigenMesh3D::EigenMesh3D(const Contour3D &other) +/*EigenMesh3D::EigenMesh3D(const Contour3D &other) { m_V.resize(Eigen::Index(other.points.size()), 3); m_F.resize(Eigen::Index(other.faces3.size() + 2 * other.faces4.size()), 3); @@ -310,12 +307,11 @@ EigenMesh3D::EigenMesh3D(const Contour3D &other) m_F.row(Eigen::Index(i)) = Vec3i{quad(0), quad(1), quad(2)}; m_F.row(Eigen::Index(i + 1)) = Vec3i{quad(2), quad(3), quad(0)}; } -} +}*/ EigenMesh3D &EigenMesh3D::operator=(const EigenMesh3D &other) { - m_V = other.m_V; - m_F = other.m_F; + m_tm = other.m_tm; m_ground_level = other.m_ground_level; m_aabb.reset(new AABBImpl(*other.m_aabb)); return *this; } @@ -333,13 +329,14 @@ EigenMesh3D::query_ray_hit(const Vec3d &s, const Vec3d &dir) const #ifdef SLIC3R_HOLE_RAYCASTER if (! m_holes.empty()) { + // If there are holes, the hit_results will be made by // query_ray_hits (object) and filter_hits (holes): return filter_hits(query_ray_hits(s, dir)); } #endif - m_aabb->intersect_ray(m_V, m_F, s, dir, hit); + m_aabb->intersect_ray(m_tm, s, dir, hit); hit_result ret(*this); ret.m_t = double(hit.t); ret.m_dir = dir; @@ -357,7 +354,7 @@ EigenMesh3D::query_ray_hits(const Vec3d &s, const Vec3d &dir) const { std::vector outs; std::vector hits; - m_aabb->intersect_ray(m_V, m_F, s, dir, hits); + m_aabb->intersect_ray(m_tm, s, dir, hits); // The sort is necessary, the hits are not always sorted. std::sort(hits.begin(), hits.end(), @@ -489,7 +486,7 @@ double EigenMesh3D::squared_distance(const Vec3d &p, int& i, Vec3d& c) const { double sqdst = 0; Eigen::Matrix pp = p; Eigen::Matrix cc; - sqdst = m_aabb->squared_distance(m_V, m_F, pp, i, cc); + sqdst = m_aabb->squared_distance(m_tm, pp, i, cc); c = cc; return sqdst; } @@ -523,7 +520,7 @@ PointSet normals(const PointSet& points, std::function thr, // throw on cancel const std::vector& pt_indices) { - if (points.rows() == 0 || mesh.V().rows() == 0 || mesh.F().rows() == 0) + if (points.rows() == 0 || mesh.vertices().empty() || mesh.indices().empty()) return {}; std::vector range = pt_indices; @@ -545,11 +542,11 @@ PointSet normals(const PointSet& points, mesh.squared_distance(points.row(eidx), faceid, p); - auto trindex = mesh.F().row(faceid); + auto trindex = mesh.indices(faceid); - const Vec3d &p1 = mesh.V().row(trindex(0)); - const Vec3d &p2 = mesh.V().row(trindex(1)); - const Vec3d &p3 = mesh.V().row(trindex(2)); + const Vec3d &p1 = mesh.vertices(trindex(0)).cast(); + const Vec3d &p2 = mesh.vertices(trindex(1)).cast(); + const Vec3d &p3 = mesh.vertices(trindex(2)).cast(); // We should check if the point lies on an edge of the hosting // triangle. If it does then all the other triangles using the @@ -584,17 +581,17 @@ PointSet normals(const PointSet& points, // vector for the neigboring triangles including the detected one. std::vector neigh; if (ic >= 0) { // The point is right on a vertex of the triangle - for (int n = 0; n < mesh.F().rows(); ++n) { + for (size_t n = 0; n < mesh.indices().size(); ++n) { thr(); - Vec3i ni = mesh.F().row(n); + Vec3i ni = mesh.indices(n); if ((ni(X) == ic || ni(Y) == ic || ni(Z) == ic)) neigh.emplace_back(ni); } } else if (ia >= 0 && ib >= 0) { // the point is on and edge // now get all the neigboring triangles - for (int n = 0; n < mesh.F().rows(); ++n) { + for (size_t n = 0; n < mesh.indices().size(); ++n) { thr(); - Vec3i ni = mesh.F().row(n); + Vec3i ni = mesh.indices(n); if ((ni(X) == ia || ni(Y) == ia || ni(Z) == ia) && (ni(X) == ib || ni(Y) == ib || ni(Z) == ib)) neigh.emplace_back(ni); @@ -605,9 +602,9 @@ PointSet normals(const PointSet& points, std::vector neighnorms; neighnorms.reserve(neigh.size()); for (const Vec3i &tri : neigh) { - const Vec3d & pt1 = mesh.V().row(tri(0)); - const Vec3d & pt2 = mesh.V().row(tri(1)); - const Vec3d & pt3 = mesh.V().row(tri(2)); + const Vec3d & pt1 = mesh.vertices(tri(0)).cast(); + const Vec3d & pt2 = mesh.vertices(tri(1)).cast(); + const Vec3d & pt3 = mesh.vertices(tri(2)).cast(); Eigen::Vector3d U = pt2 - pt1; Eigen::Vector3d V = pt3 - pt1; neighnorms.emplace_back(U.cross(V).normalized()); diff --git a/src/libslic3r/SLA/Contour3D.cpp b/src/libslic3r/SLA/Contour3D.cpp index e39672b8bd..408465d43e 100644 --- a/src/libslic3r/SLA/Contour3D.cpp +++ b/src/libslic3r/SLA/Contour3D.cpp @@ -28,14 +28,14 @@ Contour3D::Contour3D(TriangleMesh &&trmesh) } Contour3D::Contour3D(const EigenMesh3D &emesh) { - points.reserve(size_t(emesh.V().rows())); - faces3.reserve(size_t(emesh.F().rows())); + points.reserve(emesh.vertices().size()); + faces3.reserve(emesh.indices().size()); - for (int r = 0; r < emesh.V().rows(); r++) - points.emplace_back(emesh.V().row(r).cast()); + for (const Vec3f& vert : emesh.vertices()) + points.emplace_back(vert.cast()); - for (int i = 0; i < emesh.F().rows(); i++) - faces3.emplace_back(emesh.F().row(i)); + for (const auto& ind : emesh.indices()) + faces3.emplace_back(ind); } Contour3D &Contour3D::merge(const Contour3D &ctr) diff --git a/src/libslic3r/SLA/EigenMesh3D.hpp b/src/libslic3r/SLA/EigenMesh3D.hpp index 248f3577fc..bd42020b27 100644 --- a/src/libslic3r/SLA/EigenMesh3D.hpp +++ b/src/libslic3r/SLA/EigenMesh3D.hpp @@ -2,6 +2,7 @@ #define SLA_EIGENMESH3D_H #include +#include // There is an implementation of a hole-aware raycaster that was eventually @@ -15,8 +16,6 @@ namespace Slic3r { -class TriangleMesh; - namespace sla { struct Contour3D; @@ -30,8 +29,7 @@ void to_triangle_mesh(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, Triang class EigenMesh3D { class AABBImpl; - Eigen::MatrixXd m_V; - Eigen::MatrixXi m_F; + TriangleMesh m_tm; double m_ground_level = 0, m_gnd_offset = 0; std::unique_ptr m_aabb; @@ -59,8 +57,14 @@ public: inline void ground_level_offset(double o) { m_gnd_offset = o; } inline double ground_level_offset() const { return m_gnd_offset; } - inline const Eigen::MatrixXd& V() const { return m_V; } - inline const Eigen::MatrixXi& F() const { return m_F; } + const std::vector& vertices() const { return m_tm.its.vertices; } + const std::vector& indices() const { return m_tm.its.indices; } + const stl_vertex& vertices(size_t idx) const { + return m_tm.its.vertices[idx]; + } + const stl_triangle_vertex_indices& indices(size_t idx) const { + return m_tm.its.indices[idx]; + } // Result of a raycast class hit_result { @@ -148,10 +152,12 @@ public: } Vec3d normal_by_face_id(int face_id) const { - auto trindex = F().row(face_id); - const Vec3d& p1 = V().row(trindex(0)); - const Vec3d& p2 = V().row(trindex(1)); - const Vec3d& p3 = V().row(trindex(2)); + // FIXME: normals should be cached in TriangleMesh, there should be + // no need to recalculate them. + auto trindex = this->indices(face_id); + const Vec3d& p1 = this->vertices(trindex(0)).cast(); + const Vec3d& p2 = this->vertices(trindex(1)).cast(); + const Vec3d& p3 = this->vertices(trindex(2)).cast(); Eigen::Vector3d U = p2 - p1; Eigen::Vector3d V = p3 - p1; return U.cross(V).normalized(); diff --git a/src/libslic3r/SLA/Rotfinder.cpp b/src/libslic3r/SLA/Rotfinder.cpp index 9ec9837e2a..fda8383b11 100644 --- a/src/libslic3r/SLA/Rotfinder.cpp +++ b/src/libslic3r/SLA/Rotfinder.cpp @@ -28,7 +28,7 @@ std::array find_best_rotation(const ModelObject& modelobj, // We will use only one instance of this converted mesh to examine different // rotations - EigenMesh3D emesh(modelobj.raw_mesh()); + const TriangleMesh& mesh = modelobj.raw_mesh(); // For current iteration number unsigned status = 0; @@ -44,10 +44,10 @@ std::array find_best_rotation(const ModelObject& modelobj, // call the status callback in each iteration but the actual value may be // the same for subsequent iterations (status goes from 0 to 100 but // iterations can be many more) - auto objfunc = [&emesh, &status, &statuscb, &stopcond, max_tries] + auto objfunc = [&mesh, &status, &statuscb, &stopcond, max_tries] (double rx, double ry, double rz) { - EigenMesh3D& m = emesh; + const TriangleMesh& m = mesh; // prepare the rotation transformation Transform3d rt = Transform3d::Identity(); @@ -68,18 +68,8 @@ std::array find_best_rotation(const ModelObject& modelobj, // area. The current function is only an example of how to optimize. // Later we can add more criteria like the number of overhangs, etc... - for(int i = 0; i < m.F().rows(); i++) { - auto idx = m.F().row(i); - - Vec3d p1 = m.V().row(idx(0)); - Vec3d p2 = m.V().row(idx(1)); - Vec3d p3 = m.V().row(idx(2)); - - Eigen::Vector3d U = p2 - p1; - Eigen::Vector3d V = p3 - p1; - - // So this is the normal - auto n = U.cross(V).normalized(); + for(size_t i = 0; i < m.stl.facet_start.size(); i++) { + Vec3d n = m.stl.facet_start[i].normal.cast(); // rotate the normal with the current rotation given by the solver n = rt * n; From 1f833921a20e2f161a19b28a6339641d7ea2e023 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Sat, 23 May 2020 00:45:53 +0200 Subject: [PATCH 4/5] More code cleaning,... optimizations regarding normals calculation removed unused EigenMesh3D(const Contour3D &other) constructor removed unused class si_result --- src/libslic3r/SLA/Common.cpp | 82 ++----------------------------- src/libslic3r/SLA/EigenMesh3D.hpp | 34 +------------ 2 files changed, 6 insertions(+), 110 deletions(-) diff --git a/src/libslic3r/SLA/Common.cpp b/src/libslic3r/SLA/Common.cpp index 3182a2febe..6eb6629c36 100644 --- a/src/libslic3r/SLA/Common.cpp +++ b/src/libslic3r/SLA/Common.cpp @@ -226,53 +226,6 @@ public: static const constexpr double MESH_EPS = 1e-6; -void to_eigen_mesh(const TriangleMesh &tmesh, Eigen::MatrixXd &V, Eigen::MatrixXi &F) -{ - const stl_file& stl = tmesh.stl; - - V.resize(3*stl.stats.number_of_facets, 3); - F.resize(stl.stats.number_of_facets, 3); - for (unsigned int i = 0; i < stl.stats.number_of_facets; ++i) { - const stl_facet &facet = stl.facet_start[i]; - V.block<1, 3>(3 * i + 0, 0) = facet.vertex[0].cast(); - V.block<1, 3>(3 * i + 1, 0) = facet.vertex[1].cast(); - V.block<1, 3>(3 * i + 2, 0) = facet.vertex[2].cast(); - F(i, 0) = int(3*i+0); - F(i, 1) = int(3*i+1); - F(i, 2) = int(3*i+2); - } - - if (!tmesh.has_shared_vertices()) - { - Eigen::MatrixXd rV; - Eigen::MatrixXi rF; - // We will convert this to a proper 3d mesh with no duplicate points. - Eigen::VectorXi SVI, SVJ; - igl::remove_duplicate_vertices(V, F, MESH_EPS, rV, SVI, SVJ, rF); - V = std::move(rV); - F = std::move(rF); - } -} - -void to_triangle_mesh(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, TriangleMesh &out); -#if 0 -Does this function really work? There seems to be an issue with it. -Currently it is not used anywhere, this way it stays visible but -trigger linking error when attempting to use it. -{ - Pointf3s points(size_t(V.rows())); - std::vector facets(size_t(F.rows())); - - for (Eigen::Index i = 0; i < V.rows(); ++i) - points[size_t(i)] = V.row(i); - - for (Eigen::Index i = 0; i < F.rows(); ++i) - facets[size_t(i)] = F.row(i); - - out = {points, facets}; -} -#endif - EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh) : m_aabb(new AABBImpl()), m_tm(tmesh) { @@ -289,25 +242,6 @@ EigenMesh3D::EigenMesh3D(const EigenMesh3D &other): m_tm(other.m_tm), m_ground_level(other.m_ground_level), m_aabb( new AABBImpl(*other.m_aabb) ) {} -/*EigenMesh3D::EigenMesh3D(const Contour3D &other) -{ - m_V.resize(Eigen::Index(other.points.size()), 3); - m_F.resize(Eigen::Index(other.faces3.size() + 2 * other.faces4.size()), 3); - - for (Eigen::Index i = 0; i < Eigen::Index(other.points.size()); ++i) - m_V.row(i) = other.points[size_t(i)]; - - for (Eigen::Index i = 0; i < Eigen::Index(other.faces3.size()); ++i) - m_F.row(i) = other.faces3[size_t(i)]; - - size_t N = other.faces3.size() + 2 * other.faces4.size(); - for (size_t i = other.faces3.size(); i < N; i += 2) { - size_t quad_idx = (i - other.faces3.size()) / 2; - auto & quad = other.faces4[quad_idx]; - m_F.row(Eigen::Index(i)) = Vec3i{quad(0), quad(1), quad(2)}; - m_F.row(Eigen::Index(i + 1)) = Vec3i{quad(2), quad(3), quad(0)}; - } -}*/ EigenMesh3D &EigenMesh3D::operator=(const EigenMesh3D &other) { @@ -579,13 +513,13 @@ PointSet normals(const PointSet& points, } // vector for the neigboring triangles including the detected one. - std::vector neigh; + std::vector neigh; if (ic >= 0) { // The point is right on a vertex of the triangle for (size_t n = 0; n < mesh.indices().size(); ++n) { thr(); Vec3i ni = mesh.indices(n); if ((ni(X) == ic || ni(Y) == ic || ni(Z) == ic)) - neigh.emplace_back(ni); + neigh.emplace_back(n); } } else if (ia >= 0 && ib >= 0) { // the point is on and edge // now get all the neigboring triangles @@ -594,21 +528,15 @@ PointSet normals(const PointSet& points, Vec3i ni = mesh.indices(n); if ((ni(X) == ia || ni(Y) == ia || ni(Z) == ia) && (ni(X) == ib || ni(Y) == ib || ni(Z) == ib)) - neigh.emplace_back(ni); + neigh.emplace_back(n); } } // Calculate the normals for the neighboring triangles std::vector neighnorms; neighnorms.reserve(neigh.size()); - for (const Vec3i &tri : neigh) { - const Vec3d & pt1 = mesh.vertices(tri(0)).cast(); - const Vec3d & pt2 = mesh.vertices(tri(1)).cast(); - const Vec3d & pt3 = mesh.vertices(tri(2)).cast(); - Eigen::Vector3d U = pt2 - pt1; - Eigen::Vector3d V = pt3 - pt1; - neighnorms.emplace_back(U.cross(V).normalized()); - } + for (size_t &tri_id : neigh) + neighnorms.emplace_back(mesh.normal_by_face_id(tri_id)); // Throw out duplicates. They would cause trouble with summing. We // will use std::unique which works on sorted ranges. We will sort diff --git a/src/libslic3r/SLA/EigenMesh3D.hpp b/src/libslic3r/SLA/EigenMesh3D.hpp index bd42020b27..8ab198efb1 100644 --- a/src/libslic3r/SLA/EigenMesh3D.hpp +++ b/src/libslic3r/SLA/EigenMesh3D.hpp @@ -18,11 +18,6 @@ namespace Slic3r { namespace sla { -struct Contour3D; - -void to_eigen_mesh(const TriangleMesh &mesh, Eigen::MatrixXd &V, Eigen::MatrixXi &F); -void to_triangle_mesh(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, TriangleMesh &); - /// An index-triangle structure for libIGL functions. Also serves as an /// alternative (raw) input format for the SLASupportTree. // Implemented in libslic3r/SLA/Common.cpp @@ -43,7 +38,6 @@ class EigenMesh3D { public: explicit EigenMesh3D(const TriangleMesh&); - explicit EigenMesh3D(const Contour3D &other); EigenMesh3D(const EigenMesh3D& other); EigenMesh3D& operator=(const EigenMesh3D&); @@ -125,24 +119,6 @@ public: // Casts a ray on the mesh and returns all hits std::vector query_ray_hits(const Vec3d &s, const Vec3d &dir) const; - class si_result { - double m_value; - int m_fidx; - Vec3d m_p; - si_result(double val, int i, const Vec3d& c): - m_value(val), m_fidx(i), m_p(c) {} - friend class EigenMesh3D; - public: - - si_result() = delete; - - double value() const { return m_value; } - operator double() const { return m_value; } - const Vec3d& point_on_mesh() const { return m_p; } - int F_idx() const { return m_fidx; } - }; - - double squared_distance(const Vec3d& p, int& i, Vec3d& c) const; inline double squared_distance(const Vec3d &p) const { @@ -152,15 +128,7 @@ public: } Vec3d normal_by_face_id(int face_id) const { - // FIXME: normals should be cached in TriangleMesh, there should be - // no need to recalculate them. - auto trindex = this->indices(face_id); - const Vec3d& p1 = this->vertices(trindex(0)).cast(); - const Vec3d& p2 = this->vertices(trindex(1)).cast(); - const Vec3d& p3 = this->vertices(trindex(2)).cast(); - Eigen::Vector3d U = p2 - p1; - Eigen::Vector3d V = p3 - p1; - return U.cross(V).normalized(); + return m_tm.stl.facet_start[face_id].normal.cast(); } }; From 55395e046f00fb6509b41c9b06a50ed6c8dfbd49 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Sat, 23 May 2020 13:54:41 +0200 Subject: [PATCH 5/5] EigenMesh3D does not store a copy of the mesh Instead, it stores a pointer to extern TriangleMesh (which must not be destroyed before the EigenMesh3D object) --- src/libslic3r/SLA/Common.cpp | 44 ++++++++++++++++++++++++++++--- src/libslic3r/SLA/Common.hpp | 1 + src/libslic3r/SLA/EigenMesh3D.hpp | 21 ++++++--------- src/slic3r/GUI/MeshUtils.hpp | 4 +-- 4 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/libslic3r/SLA/Common.cpp b/src/libslic3r/SLA/Common.cpp index 6eb6629c36..a7420a7fb8 100644 --- a/src/libslic3r/SLA/Common.cpp +++ b/src/libslic3r/SLA/Common.cpp @@ -227,7 +227,7 @@ public: static const constexpr double MESH_EPS = 1e-6; EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh) - : m_aabb(new AABBImpl()), m_tm(tmesh) + : m_aabb(new AABBImpl()), m_tm(&tmesh) { auto&& bb = tmesh.bounding_box(); m_ground_level += bb.min(Z); @@ -254,6 +254,42 @@ EigenMesh3D &EigenMesh3D::operator=(EigenMesh3D &&other) = default; EigenMesh3D::EigenMesh3D(EigenMesh3D &&other) = default; + + +const std::vector& EigenMesh3D::vertices() const +{ + return m_tm->its.vertices; +} + + + +const std::vector& EigenMesh3D::indices() const +{ + return m_tm->its.indices; +} + + + +const Vec3f& EigenMesh3D::vertices(size_t idx) const +{ + return m_tm->its.vertices[idx]; +} + + + +const Vec3i& EigenMesh3D::indices(size_t idx) const +{ + return m_tm->its.indices[idx]; +} + + + +Vec3d EigenMesh3D::normal_by_face_id(int face_id) const { + return m_tm->stl.facet_start[face_id].normal.cast(); +} + + + EigenMesh3D::hit_result EigenMesh3D::query_ray_hit(const Vec3d &s, const Vec3d &dir) const { @@ -270,7 +306,7 @@ EigenMesh3D::query_ray_hit(const Vec3d &s, const Vec3d &dir) const } #endif - m_aabb->intersect_ray(m_tm, s, dir, hit); + m_aabb->intersect_ray(*m_tm, s, dir, hit); hit_result ret(*this); ret.m_t = double(hit.t); ret.m_dir = dir; @@ -288,7 +324,7 @@ EigenMesh3D::query_ray_hits(const Vec3d &s, const Vec3d &dir) const { std::vector outs; std::vector hits; - m_aabb->intersect_ray(m_tm, s, dir, hits); + m_aabb->intersect_ray(*m_tm, s, dir, hits); // The sort is necessary, the hits are not always sorted. std::sort(hits.begin(), hits.end(), @@ -420,7 +456,7 @@ double EigenMesh3D::squared_distance(const Vec3d &p, int& i, Vec3d& c) const { double sqdst = 0; Eigen::Matrix pp = p; Eigen::Matrix cc; - sqdst = m_aabb->squared_distance(m_tm, pp, i, cc); + sqdst = m_aabb->squared_distance(*m_tm, pp, i, cc); c = cc; return sqdst; } diff --git a/src/libslic3r/SLA/Common.hpp b/src/libslic3r/SLA/Common.hpp index 91bdc0eb1d..ca616cabce 100644 --- a/src/libslic3r/SLA/Common.hpp +++ b/src/libslic3r/SLA/Common.hpp @@ -13,6 +13,7 @@ namespace Slic3r { // Typedefs from Point.hpp typedef Eigen::Matrix Vec3f; typedef Eigen::Matrix Vec3d; +typedef Eigen::Matrix Vec3i; typedef Eigen::Matrix Vec4i; namespace sla { diff --git a/src/libslic3r/SLA/EigenMesh3D.hpp b/src/libslic3r/SLA/EigenMesh3D.hpp index 8ab198efb1..a427dde3c2 100644 --- a/src/libslic3r/SLA/EigenMesh3D.hpp +++ b/src/libslic3r/SLA/EigenMesh3D.hpp @@ -2,7 +2,6 @@ #define SLA_EIGENMESH3D_H #include -#include // There is an implementation of a hole-aware raycaster that was eventually @@ -16,6 +15,8 @@ namespace Slic3r { +class TriangleMesh; + namespace sla { /// An index-triangle structure for libIGL functions. Also serves as an @@ -24,7 +25,7 @@ namespace sla { class EigenMesh3D { class AABBImpl; - TriangleMesh m_tm; + const TriangleMesh* m_tm; double m_ground_level = 0, m_gnd_offset = 0; std::unique_ptr m_aabb; @@ -51,14 +52,10 @@ public: inline void ground_level_offset(double o) { m_gnd_offset = o; } inline double ground_level_offset() const { return m_gnd_offset; } - const std::vector& vertices() const { return m_tm.its.vertices; } - const std::vector& indices() const { return m_tm.its.indices; } - const stl_vertex& vertices(size_t idx) const { - return m_tm.its.vertices[idx]; - } - const stl_triangle_vertex_indices& indices(size_t idx) const { - return m_tm.its.indices[idx]; - } + const std::vector& vertices() const; + const std::vector& indices() const; + const Vec3f& vertices(size_t idx) const; + const Vec3i& indices(size_t idx) const; // Result of a raycast class hit_result { @@ -127,9 +124,7 @@ public: return squared_distance(p, i, c); } - Vec3d normal_by_face_id(int face_id) const { - return m_tm.stl.facet_start[face_id].normal.cast(); - } + Vec3d normal_by_face_id(int face_id) const; }; // Calculate the normals for the selected points (from 'points' set) on the diff --git a/src/slic3r/GUI/MeshUtils.hpp b/src/slic3r/GUI/MeshUtils.hpp index 3cb9b58f28..2758577a25 100644 --- a/src/slic3r/GUI/MeshUtils.hpp +++ b/src/slic3r/GUI/MeshUtils.hpp @@ -104,8 +104,8 @@ private: // whether certain points are visible or obscured by the mesh etc. class MeshRaycaster { public: - // The class makes a copy of the mesh as EigenMesh3D. - // The pointer can be invalidated after constructor returns. + // The class references extern TriangleMesh, which must stay alive + // during MeshRaycaster existence. MeshRaycaster(const TriangleMesh& mesh) : m_emesh(mesh) {