2019-09-13 09:30:50 +00:00
|
|
|
#ifndef slic3r_MeshUtils_hpp_
|
|
|
|
#define slic3r_MeshUtils_hpp_
|
|
|
|
|
|
|
|
#include "libslic3r/Point.hpp"
|
|
|
|
#include "libslic3r/Geometry.hpp"
|
2019-11-11 10:41:14 +00:00
|
|
|
#include "libslic3r/SLA/EigenMesh3D.hpp"
|
2019-09-26 11:30:22 +00:00
|
|
|
#include "admesh/stl.h"
|
|
|
|
|
2019-09-13 09:30:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include <cfloat>
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
class TriangleMesh;
|
|
|
|
class TriangleMeshSlicer;
|
|
|
|
|
|
|
|
namespace GUI {
|
|
|
|
|
2019-09-17 13:48:39 +00:00
|
|
|
struct Camera;
|
2019-09-17 07:17:53 +00:00
|
|
|
|
2019-09-13 09:30:50 +00:00
|
|
|
|
2019-12-10 08:52:20 +00:00
|
|
|
// lm_FIXME: Following class might possibly be replaced by Eigen::Hyperplane
|
2019-09-13 09:30:50 +00:00
|
|
|
class ClippingPlane
|
|
|
|
{
|
|
|
|
double m_data[4];
|
|
|
|
|
|
|
|
public:
|
|
|
|
ClippingPlane()
|
|
|
|
{
|
2019-11-01 11:23:49 +00:00
|
|
|
*this = ClipsNothing();
|
2019-09-13 09:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ClippingPlane(const Vec3d& direction, double offset)
|
|
|
|
{
|
|
|
|
Vec3d norm_dir = direction.normalized();
|
|
|
|
m_data[0] = norm_dir(0);
|
|
|
|
m_data[1] = norm_dir(1);
|
|
|
|
m_data[2] = norm_dir(2);
|
|
|
|
m_data[3] = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const ClippingPlane& cp) const {
|
|
|
|
return m_data[0]==cp.m_data[0] && m_data[1]==cp.m_data[1] && m_data[2]==cp.m_data[2] && m_data[3]==cp.m_data[3];
|
|
|
|
}
|
|
|
|
bool operator!=(const ClippingPlane& cp) const { return ! (*this==cp); }
|
|
|
|
|
|
|
|
double distance(const Vec3d& pt) const {
|
2019-11-13 14:55:37 +00:00
|
|
|
// FIXME: this fails: assert(is_approx(get_normal().norm(), 1.));
|
2019-09-13 09:30:50 +00:00
|
|
|
return (-get_normal().dot(pt) + m_data[3]);
|
|
|
|
}
|
|
|
|
|
2019-09-24 10:48:05 +00:00
|
|
|
bool is_point_clipped(const Vec3d& point) const { return distance(point) < 0.; }
|
2019-09-13 09:30:50 +00:00
|
|
|
void set_normal(const Vec3d& normal) { for (size_t i=0; i<3; ++i) m_data[i] = normal(i); }
|
|
|
|
void set_offset(double offset) { m_data[3] = offset; }
|
|
|
|
Vec3d get_normal() const { return Vec3d(m_data[0], m_data[1], m_data[2]); }
|
|
|
|
bool is_active() const { return m_data[3] != DBL_MAX; }
|
|
|
|
static ClippingPlane ClipsNothing() { return ClippingPlane(Vec3d(0., 0., 1.), DBL_MAX); }
|
|
|
|
const double* get_data() const { return m_data; }
|
|
|
|
|
|
|
|
// Serialization through cereal library
|
|
|
|
template <class Archive>
|
|
|
|
void serialize( Archive & ar )
|
|
|
|
{
|
|
|
|
ar( m_data[0], m_data[1], m_data[2], m_data[3] );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-12-10 08:52:20 +00:00
|
|
|
// MeshClipper class cuts a mesh and is able to return a triangulated cut.
|
2019-09-13 09:30:50 +00:00
|
|
|
class MeshClipper {
|
|
|
|
public:
|
2019-12-10 08:52:20 +00:00
|
|
|
// Inform MeshClipper about which plane we want to use to cut the mesh
|
|
|
|
// This is supposed to be in world coordinates.
|
2019-09-13 09:30:50 +00:00
|
|
|
void set_plane(const ClippingPlane& plane);
|
2019-12-10 08:52:20 +00:00
|
|
|
|
|
|
|
// Which mesh to cut. MeshClipper remembers const * to it, caller
|
|
|
|
// must make sure that it stays valid.
|
2019-09-13 09:30:50 +00:00
|
|
|
void set_mesh(const TriangleMesh& mesh);
|
2019-12-10 08:52:20 +00:00
|
|
|
|
|
|
|
// Inform the MeshClipper about the transformation that transforms the mesh
|
|
|
|
// into world coordinates.
|
2019-09-13 09:30:50 +00:00
|
|
|
void set_transformation(const Geometry::Transformation& trafo);
|
|
|
|
|
2019-12-10 08:52:20 +00:00
|
|
|
// Return the triangulated cut. The points are returned directly
|
|
|
|
// in world coordinates.
|
2019-09-13 09:30:50 +00:00
|
|
|
const std::vector<Vec3f>& get_triangles();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void recalculate_triangles();
|
|
|
|
|
|
|
|
Geometry::Transformation m_trafo;
|
|
|
|
const TriangleMesh* m_mesh = nullptr;
|
|
|
|
ClippingPlane m_plane;
|
|
|
|
std::vector<Vec2f> m_triangles2d;
|
|
|
|
std::vector<Vec3f> m_triangles3d;
|
|
|
|
bool m_triangles_valid = false;
|
|
|
|
std::unique_ptr<TriangleMeshSlicer> m_tms;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-09-17 07:17:53 +00:00
|
|
|
|
2019-12-10 08:52:20 +00:00
|
|
|
// MeshRaycaster class answers queries such as where on the mesh someone clicked,
|
|
|
|
// whether certain points are visible or obscured by the mesh etc.
|
2019-09-17 07:17:53 +00:00
|
|
|
class MeshRaycaster {
|
|
|
|
public:
|
2019-12-12 12:19:16 +00:00
|
|
|
// The class makes a copy of the mesh as EigenMesh3D.
|
|
|
|
// The pointer can be invalidated after constructor returns.
|
2019-11-08 19:18:14 +00:00
|
|
|
MeshRaycaster(const TriangleMesh& mesh)
|
2019-12-12 12:19:16 +00:00
|
|
|
: m_emesh(mesh)
|
2020-05-15 12:16:56 +00:00
|
|
|
{
|
|
|
|
m_normals.reserve(mesh.stl.facet_start.size());
|
|
|
|
for (const stl_facet& facet : mesh.stl.facet_start)
|
|
|
|
m_normals.push_back(facet.normal);
|
|
|
|
}
|
2019-09-17 08:47:01 +00:00
|
|
|
|
2019-10-08 08:15:06 +00:00
|
|
|
void line_from_mouse_pos(const Vec2d& mouse_pos, const Transform3d& trafo, const Camera& camera,
|
|
|
|
Vec3d& point, Vec3d& direction) const;
|
|
|
|
|
2019-12-10 08:52:20 +00:00
|
|
|
// Given a mouse position, this returns true in case it is on the mesh.
|
|
|
|
bool unproject_on_mesh(
|
|
|
|
const Vec2d& mouse_pos,
|
|
|
|
const Transform3d& trafo, // how to get the mesh into world coords
|
|
|
|
const Camera& camera, // current camera position
|
|
|
|
Vec3f& position, // where to save the positibon of the hit (mesh coords)
|
|
|
|
Vec3f& normal, // normal of the triangle that was hit
|
2019-09-26 11:30:22 +00:00
|
|
|
const ClippingPlane* clipping_plane = nullptr, // clipping plane (if active)
|
|
|
|
size_t* facet_idx = nullptr // index of the facet hit
|
2019-12-10 08:52:20 +00:00
|
|
|
) const;
|
|
|
|
|
|
|
|
// Given a vector of points in woorld coordinates, this returns vector
|
|
|
|
// of indices of points that are visible (i.e. not cut by clipping plane
|
|
|
|
// or obscured by part of the mesh.
|
|
|
|
std::vector<unsigned> get_unobscured_idxs(
|
|
|
|
const Geometry::Transformation& trafo, // how to get the mesh into world coords
|
|
|
|
const Camera& camera, // current camera position
|
|
|
|
const std::vector<Vec3f>& points, // points in world coords
|
|
|
|
const ClippingPlane* clipping_plane = nullptr // clipping plane (if active)
|
|
|
|
) const;
|
|
|
|
|
|
|
|
// Given a point in world coords, the method returns closest point on the mesh.
|
|
|
|
// The output is in mesh coords.
|
|
|
|
// normal* can be used to also get normal of the respective triangle.
|
2019-09-26 11:30:22 +00:00
|
|
|
|
2019-09-17 12:56:46 +00:00
|
|
|
Vec3f get_closest_point(const Vec3f& point, Vec3f* normal = nullptr) const;
|
|
|
|
|
2020-05-15 12:16:56 +00:00
|
|
|
Vec3f get_triangle_normal(size_t facet_idx) const;
|
2019-09-26 11:30:22 +00:00
|
|
|
|
2019-09-17 07:17:53 +00:00
|
|
|
private:
|
2019-11-08 19:18:14 +00:00
|
|
|
sla::EigenMesh3D m_emesh;
|
2020-05-15 12:16:56 +00:00
|
|
|
std::vector<stl_normal> m_normals;
|
2019-09-17 07:17:53 +00:00
|
|
|
};
|
|
|
|
|
2019-09-13 09:30:50 +00:00
|
|
|
|
|
|
|
} // namespace GUI
|
|
|
|
} // namespace Slic3r
|
|
|
|
|
|
|
|
|
|
|
|
#endif // slic3r_MeshUtils_hpp_
|