2019-11-11 10:41:14 +00:00
|
|
|
#ifndef SLA_EIGENMESH3D_H
|
|
|
|
#define SLA_EIGENMESH3D_H
|
2019-02-01 15:12:00 +00:00
|
|
|
|
2019-11-11 10:41:14 +00:00
|
|
|
#include <libslic3r/SLA/Common.hpp>
|
2019-11-26 10:36:09 +00:00
|
|
|
#include "libslic3r/SLA/Hollowing.hpp"
|
2019-02-01 15:12:00 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
2019-07-02 14:42:23 +00:00
|
|
|
|
2019-11-11 10:41:14 +00:00
|
|
|
class TriangleMesh;
|
2019-02-01 15:12:00 +00:00
|
|
|
|
2019-11-11 10:41:14 +00:00
|
|
|
namespace sla {
|
2019-09-26 07:42:08 +00:00
|
|
|
|
2019-11-04 13:33:29 +00:00
|
|
|
struct Contour3D;
|
|
|
|
|
2020-01-24 13:26:05 +00:00
|
|
|
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 &);
|
|
|
|
|
2019-02-04 07:40:20 +00:00
|
|
|
/// An index-triangle structure for libIGL functions. Also serves as an
|
2019-11-04 13:33:29 +00:00
|
|
|
/// alternative (raw) input format for the SLASupportTree.
|
2019-11-15 14:48:52 +00:00
|
|
|
// Implemented in libslic3r/SLA/Common.cpp
|
2019-02-04 07:40:20 +00:00
|
|
|
class EigenMesh3D {
|
|
|
|
class AABBImpl;
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2019-02-04 07:40:20 +00:00
|
|
|
Eigen::MatrixXd m_V;
|
|
|
|
Eigen::MatrixXi m_F;
|
2019-06-11 15:57:39 +00:00
|
|
|
double m_ground_level = 0, m_gnd_offset = 0;
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2019-02-04 07:40:20 +00:00
|
|
|
std::unique_ptr<AABBImpl> m_aabb;
|
2019-11-26 10:36:09 +00:00
|
|
|
|
|
|
|
// This holds a copy of holes in the mesh. Initialized externally
|
|
|
|
// by load_mesh setter.
|
|
|
|
std::vector<DrainHole> m_holes;
|
|
|
|
|
2019-02-04 07:40:20 +00:00
|
|
|
public:
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2020-01-24 13:26:05 +00:00
|
|
|
explicit EigenMesh3D(const TriangleMesh&);
|
|
|
|
explicit EigenMesh3D(const Contour3D &other);
|
|
|
|
|
2019-02-04 07:40:20 +00:00
|
|
|
EigenMesh3D(const EigenMesh3D& other);
|
|
|
|
EigenMesh3D& operator=(const EigenMesh3D&);
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2020-01-24 13:26:05 +00:00
|
|
|
EigenMesh3D(EigenMesh3D &&other);
|
|
|
|
EigenMesh3D& operator=(EigenMesh3D &&other);
|
|
|
|
|
2019-02-04 07:40:20 +00:00
|
|
|
~EigenMesh3D();
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2019-06-11 15:57:39 +00:00
|
|
|
inline double ground_level() const { return m_ground_level + m_gnd_offset; }
|
|
|
|
inline void ground_level_offset(double o) { m_gnd_offset = o; }
|
2019-06-20 11:01:48 +00:00
|
|
|
inline double ground_level_offset() const { return m_gnd_offset; }
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2019-02-04 07:40:20 +00:00
|
|
|
inline const Eigen::MatrixXd& V() const { return m_V; }
|
|
|
|
inline const Eigen::MatrixXi& F() const { return m_F; }
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2019-02-04 07:40:20 +00:00
|
|
|
// Result of a raycast
|
|
|
|
class hit_result {
|
2019-11-26 10:36:09 +00:00
|
|
|
// m_t holds a distance from m_source to the intersection.
|
|
|
|
double m_t = infty();
|
2019-03-01 16:45:29 +00:00
|
|
|
const EigenMesh3D *m_mesh = nullptr;
|
2019-02-04 07:40:20 +00:00
|
|
|
Vec3d m_dir;
|
2019-02-28 18:05:11 +00:00
|
|
|
Vec3d m_source;
|
2019-11-26 10:36:09 +00:00
|
|
|
Vec3d m_normal;
|
2019-02-04 07:40:20 +00:00
|
|
|
friend class EigenMesh3D;
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2019-02-28 18:05:11 +00:00
|
|
|
// A valid object of this class can only be obtained from
|
|
|
|
// EigenMesh3D::query_ray_hit method.
|
2019-03-01 16:45:29 +00:00
|
|
|
explicit inline hit_result(const EigenMesh3D& em): m_mesh(&em) {}
|
|
|
|
public:
|
2019-11-26 10:36:09 +00:00
|
|
|
// This denotes no hit on the mesh.
|
|
|
|
static inline constexpr double infty() { return std::numeric_limits<double>::infinity(); }
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2019-11-26 10:36:09 +00:00
|
|
|
explicit inline hit_result(double val = infty()) : m_t(val) {}
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2019-02-04 07:40:20 +00:00
|
|
|
inline double distance() const { return m_t; }
|
2019-02-04 08:50:25 +00:00
|
|
|
inline const Vec3d& direction() const { return m_dir; }
|
2019-11-19 13:27:05 +00:00
|
|
|
inline const Vec3d& source() const { return m_source; }
|
2019-02-28 18:05:11 +00:00
|
|
|
inline Vec3d position() const { return m_source + m_dir * m_t; }
|
2019-03-01 16:45:29 +00:00
|
|
|
inline bool is_valid() const { return m_mesh != nullptr; }
|
2020-01-08 16:12:06 +00:00
|
|
|
inline bool is_hit() const { return !std::isinf(m_t); }
|
2019-11-26 10:36:09 +00:00
|
|
|
|
2019-11-19 13:27:05 +00:00
|
|
|
inline const Vec3d& normal() const {
|
2019-11-26 10:36:09 +00:00
|
|
|
assert(is_valid());
|
2019-11-19 13:27:05 +00:00
|
|
|
return m_normal;
|
2019-02-04 07:40:20 +00:00
|
|
|
}
|
2019-11-26 10:36:09 +00:00
|
|
|
|
2019-11-19 13:27:05 +00:00
|
|
|
inline bool is_inside() const {
|
2019-11-26 10:36:09 +00:00
|
|
|
return is_hit() && normal().dot(m_dir) > 0;
|
2019-02-04 07:40:20 +00:00
|
|
|
}
|
|
|
|
};
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2019-11-26 10:36:09 +00:00
|
|
|
// Inform the object about location of holes
|
|
|
|
// creates internal copy of the vector
|
|
|
|
void load_holes(const std::vector<DrainHole>& holes) {
|
|
|
|
m_holes = holes;
|
|
|
|
}
|
|
|
|
|
2019-02-04 07:40:20 +00:00
|
|
|
// Casting a ray on the mesh, returns the distance where the hit occures.
|
2019-11-26 10:36:09 +00:00
|
|
|
hit_result query_ray_hit(const Vec3d &s, const Vec3d &dir) const;
|
2019-11-11 10:41:14 +00:00
|
|
|
|
|
|
|
// Casts a ray on the mesh and returns all hits
|
|
|
|
std::vector<hit_result> query_ray_hits(const Vec3d &s, const Vec3d &dir) const;
|
2019-11-15 14:48:52 +00:00
|
|
|
|
2019-11-19 13:27:05 +00:00
|
|
|
// Iterates over hits and holes and returns the true hit, possibly
|
|
|
|
// on the inside of a hole.
|
2020-02-29 10:39:54 +00:00
|
|
|
// 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.
|
2019-11-26 10:36:09 +00:00
|
|
|
hit_result filter_hits(const std::vector<EigenMesh3D::hit_result>& obj_hits) const;
|
2019-11-19 13:27:05 +00:00
|
|
|
|
2019-02-04 07:40:20 +00:00
|
|
|
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:
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2019-02-04 07:40:20 +00:00
|
|
|
si_result() = delete;
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2019-02-04 07:40:20 +00:00
|
|
|
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; }
|
|
|
|
};
|
|
|
|
|
2019-02-17 12:05:22 +00:00
|
|
|
#ifdef SLIC3R_SLA_NEEDS_WINDTREE
|
2019-02-04 07:40:20 +00:00
|
|
|
// 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;
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2019-02-04 07:40:20 +00:00
|
|
|
bool inside(const Vec3d& p) const;
|
2019-02-17 12:05:22 +00:00
|
|
|
#endif /* SLIC3R_SLA_NEEDS_WINDTREE */
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2019-02-26 16:13:33 +00:00
|
|
|
double squared_distance(const Vec3d& p, int& i, Vec3d& c) const;
|
2019-06-11 10:40:07 +00:00
|
|
|
inline double squared_distance(const Vec3d &p) const
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Vec3d c;
|
|
|
|
return squared_distance(p, i, c);
|
|
|
|
}
|
2019-11-19 13:27:05 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
Eigen::Vector3d U = p2 - p1;
|
|
|
|
Eigen::Vector3d V = p3 - p1;
|
|
|
|
return U.cross(V).normalized();
|
|
|
|
}
|
2019-02-01 15:12:00 +00:00
|
|
|
};
|
|
|
|
|
2019-11-04 13:33:29 +00:00
|
|
|
// Calculate the normals for the selected points (from 'points' set) on the
|
|
|
|
// mesh. This will call squared distance for each point.
|
|
|
|
PointSet normals(const PointSet& points,
|
|
|
|
const EigenMesh3D& convert_mesh,
|
|
|
|
double eps = 0.05, // min distance from edges
|
|
|
|
std::function<void()> throw_on_cancel = [](){},
|
|
|
|
const std::vector<unsigned>& selected_points = {});
|
|
|
|
|
2019-11-11 10:41:14 +00:00
|
|
|
}} // namespace Slic3r::sla
|
2019-02-01 15:12:00 +00:00
|
|
|
|
2019-11-11 10:41:14 +00:00
|
|
|
#endif // EIGENMESH3D_H
|