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)
This commit is contained in:
Lukas Matena 2020-05-23 13:54:41 +02:00
parent 1f833921a2
commit 55395e046f
4 changed files with 51 additions and 19 deletions

View file

@ -227,7 +227,7 @@ public:
static const constexpr double MESH_EPS = 1e-6; static const constexpr double MESH_EPS = 1e-6;
EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh) EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh)
: m_aabb(new AABBImpl()), m_tm(tmesh) : m_aabb(new AABBImpl()), m_tm(&tmesh)
{ {
auto&& bb = tmesh.bounding_box(); auto&& bb = tmesh.bounding_box();
m_ground_level += bb.min(Z); m_ground_level += bb.min(Z);
@ -254,6 +254,42 @@ EigenMesh3D &EigenMesh3D::operator=(EigenMesh3D &&other) = default;
EigenMesh3D::EigenMesh3D(EigenMesh3D &&other) = default; EigenMesh3D::EigenMesh3D(EigenMesh3D &&other) = default;
const std::vector<Vec3f>& EigenMesh3D::vertices() const
{
return m_tm->its.vertices;
}
const std::vector<Vec3i>& 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<double>();
}
EigenMesh3D::hit_result EigenMesh3D::hit_result
EigenMesh3D::query_ray_hit(const Vec3d &s, const Vec3d &dir) const 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 #endif
m_aabb->intersect_ray(m_tm, s, dir, hit); m_aabb->intersect_ray(*m_tm, s, dir, hit);
hit_result ret(*this); hit_result ret(*this);
ret.m_t = double(hit.t); ret.m_t = double(hit.t);
ret.m_dir = dir; ret.m_dir = dir;
@ -288,7 +324,7 @@ EigenMesh3D::query_ray_hits(const Vec3d &s, const Vec3d &dir) const
{ {
std::vector<EigenMesh3D::hit_result> outs; std::vector<EigenMesh3D::hit_result> outs;
std::vector<igl::Hit> hits; std::vector<igl::Hit> 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. // The sort is necessary, the hits are not always sorted.
std::sort(hits.begin(), hits.end(), 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; double sqdst = 0;
Eigen::Matrix<double, 1, 3> pp = p; Eigen::Matrix<double, 1, 3> pp = p;
Eigen::Matrix<double, 1, 3> cc; Eigen::Matrix<double, 1, 3> cc;
sqdst = m_aabb->squared_distance(m_tm, pp, i, cc); sqdst = m_aabb->squared_distance(*m_tm, pp, i, cc);
c = cc; c = cc;
return sqdst; return sqdst;
} }

View file

@ -13,6 +13,7 @@ namespace Slic3r {
// Typedefs from Point.hpp // Typedefs from Point.hpp
typedef Eigen::Matrix<float, 3, 1, Eigen::DontAlign> Vec3f; typedef Eigen::Matrix<float, 3, 1, Eigen::DontAlign> Vec3f;
typedef Eigen::Matrix<double, 3, 1, Eigen::DontAlign> Vec3d; typedef Eigen::Matrix<double, 3, 1, Eigen::DontAlign> Vec3d;
typedef Eigen::Matrix<int, 3, 1, Eigen::DontAlign> Vec3i;
typedef Eigen::Matrix<int, 4, 1, Eigen::DontAlign> Vec4i; typedef Eigen::Matrix<int, 4, 1, Eigen::DontAlign> Vec4i;
namespace sla { namespace sla {

View file

@ -2,7 +2,6 @@
#define SLA_EIGENMESH3D_H #define SLA_EIGENMESH3D_H
#include <libslic3r/SLA/Common.hpp> #include <libslic3r/SLA/Common.hpp>
#include <libslic3r/TriangleMesh.hpp>
// There is an implementation of a hole-aware raycaster that was eventually // There is an implementation of a hole-aware raycaster that was eventually
@ -16,6 +15,8 @@
namespace Slic3r { namespace Slic3r {
class TriangleMesh;
namespace sla { namespace sla {
/// An index-triangle structure for libIGL functions. Also serves as an /// An index-triangle structure for libIGL functions. Also serves as an
@ -24,7 +25,7 @@ namespace sla {
class EigenMesh3D { class EigenMesh3D {
class AABBImpl; class AABBImpl;
TriangleMesh m_tm; const TriangleMesh* m_tm;
double m_ground_level = 0, m_gnd_offset = 0; double m_ground_level = 0, m_gnd_offset = 0;
std::unique_ptr<AABBImpl> m_aabb; std::unique_ptr<AABBImpl> m_aabb;
@ -51,14 +52,10 @@ public:
inline void ground_level_offset(double o) { m_gnd_offset = o; } inline void ground_level_offset(double o) { m_gnd_offset = o; }
inline double ground_level_offset() const { return m_gnd_offset; } inline double ground_level_offset() const { return m_gnd_offset; }
const std::vector<stl_vertex>& vertices() const { return m_tm.its.vertices; } const std::vector<Vec3f>& vertices() const;
const std::vector<stl_triangle_vertex_indices>& indices() const { return m_tm.its.indices; } const std::vector<Vec3i>& indices() const;
const stl_vertex& vertices(size_t idx) const { const Vec3f& vertices(size_t idx) const;
return m_tm.its.vertices[idx]; const Vec3i& indices(size_t idx) const;
}
const stl_triangle_vertex_indices& indices(size_t idx) const {
return m_tm.its.indices[idx];
}
// Result of a raycast // Result of a raycast
class hit_result { class hit_result {
@ -127,9 +124,7 @@ public:
return squared_distance(p, i, c); return squared_distance(p, i, c);
} }
Vec3d normal_by_face_id(int face_id) const { Vec3d normal_by_face_id(int face_id) const;
return m_tm.stl.facet_start[face_id].normal.cast<double>();
}
}; };
// Calculate the normals for the selected points (from 'points' set) on the // Calculate the normals for the selected points (from 'points' set) on the

View file

@ -104,8 +104,8 @@ private:
// whether certain points are visible or obscured by the mesh etc. // whether certain points are visible or obscured by the mesh etc.
class MeshRaycaster { class MeshRaycaster {
public: public:
// The class makes a copy of the mesh as EigenMesh3D. // The class references extern TriangleMesh, which must stay alive
// The pointer can be invalidated after constructor returns. // during MeshRaycaster existence.
MeshRaycaster(const TriangleMesh& mesh) MeshRaycaster(const TriangleMesh& mesh)
: m_emesh(mesh) : m_emesh(mesh)
{ {