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) {