2022-05-11 10:08:32 +00:00
|
|
|
#ifndef PRUSASLICER_AABBMESH_H
|
|
|
|
#define PRUSASLICER_AABBMESH_H
|
2019-02-01 15:12:00 +00:00
|
|
|
|
2020-06-16 11:17:06 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
2020-05-22 15:21:54 +00:00
|
|
|
|
2020-06-16 11:17:06 +00:00
|
|
|
#include <libslic3r/Point.hpp>
|
2022-06-29 22:21:08 +00:00
|
|
|
#include <libslic3r/TriangleMesh.hpp>
|
2020-05-22 15:21:54 +00:00
|
|
|
|
|
|
|
// 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.
|
2020-06-02 15:15:08 +00:00
|
|
|
// #define SLIC3R_HOLE_RAYCASTER
|
2020-05-22 15:21:54 +00:00
|
|
|
|
|
|
|
#ifdef SLIC3R_HOLE_RAYCASTER
|
|
|
|
#include "libslic3r/SLA/Hollowing.hpp"
|
|
|
|
#endif
|
2019-02-01 15:12:00 +00:00
|
|
|
|
2021-05-21 12:08:05 +00:00
|
|
|
struct indexed_triangle_set;
|
|
|
|
|
2019-02-01 15:12:00 +00:00
|
|
|
namespace Slic3r {
|
2019-07-02 14:42:23 +00:00
|
|
|
|
2020-05-23 11:54:41 +00:00
|
|
|
class TriangleMesh;
|
|
|
|
|
2022-05-11 10:08:32 +00:00
|
|
|
// An index-triangle structure coupled with an AABB index to support ray
|
|
|
|
// casting and other higher level operations.
|
|
|
|
class AABBMesh {
|
2019-02-04 07:40:20 +00:00
|
|
|
class AABBImpl;
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2021-05-21 12:08:05 +00:00
|
|
|
const indexed_triangle_set* m_tm;
|
2022-05-11 10:08:32 +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;
|
2022-05-11 10:08:32 +00:00
|
|
|
VertexFaceIndex m_vfidx; // vertex-face index
|
|
|
|
std::vector<Vec3i> m_fnidx; // face-neighbor index
|
2019-11-26 10:36:09 +00:00
|
|
|
|
2020-05-22 15:21:54 +00:00
|
|
|
#ifdef SLIC3R_HOLE_RAYCASTER
|
2019-11-26 10:36:09 +00:00
|
|
|
// This holds a copy of holes in the mesh. Initialized externally
|
|
|
|
// by load_mesh setter.
|
2022-05-11 10:08:32 +00:00
|
|
|
std::vector<sla::DrainHole> m_holes;
|
2020-05-22 15:21:54 +00:00
|
|
|
#endif
|
2019-11-26 10:36:09 +00:00
|
|
|
|
2021-08-27 19:04:11 +00:00
|
|
|
template<class M> void init(const M &mesh, bool calculate_epsilon);
|
2021-05-21 12:08:05 +00:00
|
|
|
|
2019-02-04 07:40:20 +00:00
|
|
|
public:
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2021-08-27 19:04:11 +00:00
|
|
|
// calculate_epsilon ... calculate epsilon for triangle-ray intersection from an average triangle edge length.
|
|
|
|
// If set to false, a default epsilon is used, which works for "reasonable" meshes.
|
2022-05-11 10:08:32 +00:00
|
|
|
explicit AABBMesh(const indexed_triangle_set &tmesh, bool calculate_epsilon = false);
|
|
|
|
explicit AABBMesh(const TriangleMesh &mesh, bool calculate_epsilon = false);
|
2020-01-24 13:26:05 +00:00
|
|
|
|
2022-05-11 10:08:32 +00:00
|
|
|
AABBMesh(const AABBMesh& other);
|
|
|
|
AABBMesh& operator=(const AABBMesh&);
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2022-05-11 10:08:32 +00:00
|
|
|
AABBMesh(AABBMesh &&other);
|
|
|
|
AABBMesh& operator=(AABBMesh &&other);
|
2020-01-24 13:26:05 +00:00
|
|
|
|
2022-05-11 10:08:32 +00:00
|
|
|
~AABBMesh();
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2022-05-11 10:08:32 +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; }
|
|
|
|
// inline double ground_level_offset() const { return m_gnd_offset; }
|
2019-11-11 10:41:14 +00:00
|
|
|
|
2020-05-23 11:54:41 +00:00
|
|
|
const std::vector<Vec3f>& vertices() const;
|
|
|
|
const std::vector<Vec3i>& indices() const;
|
|
|
|
const Vec3f& vertices(size_t idx) const;
|
|
|
|
const Vec3i& indices(size_t idx) const;
|
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-09-26 11:30:22 +00:00
|
|
|
int m_face_id = -1;
|
2022-05-11 10:08:32 +00:00
|
|
|
const AABBMesh *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;
|
2022-05-11 10:08:32 +00:00
|
|
|
friend class AABBMesh;
|
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
|
2020-06-25 11:58:51 +00:00
|
|
|
// IndexedMesh::query_ray_hit method.
|
2022-05-11 10:08:32 +00:00
|
|
|
explicit inline hit_result(const AABBMesh& em): m_mesh(&em) {}
|
2019-03-01 16:45:29 +00:00
|
|
|
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-09-26 11:30:22 +00:00
|
|
|
inline int face() const { return m_face_id; }
|
2019-03-01 16:45:29 +00:00
|
|
|
inline bool is_valid() const { return m_mesh != nullptr; }
|
2020-07-16 09:49:30 +00:00
|
|
|
inline bool is_hit() const { return m_face_id >= 0 && !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
|
|
|
}
|
|
|
|
};
|
2020-05-22 15:21:54 +00:00
|
|
|
|
|
|
|
#ifdef SLIC3R_HOLE_RAYCASTER
|
2019-11-26 10:36:09 +00:00
|
|
|
// Inform the object about location of holes
|
|
|
|
// creates internal copy of the vector
|
2022-05-11 10:08:32 +00:00
|
|
|
void load_holes(const std::vector<sla::DrainHole>& holes) {
|
2019-11-26 10:36:09 +00:00
|
|
|
m_holes = holes;
|
|
|
|
}
|
|
|
|
|
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.
|
2022-05-11 10:08:32 +00:00
|
|
|
hit_result filter_hits(const std::vector<AABBMesh::hit_result>& obj_hits) const;
|
2020-05-22 15:21:54 +00:00
|
|
|
#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<hit_result> query_ray_hits(const Vec3d &s, const Vec3d &dir) const;
|
2019-11-19 13:27:05 +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
|
|
|
|
2020-05-23 11:54:41 +00:00
|
|
|
Vec3d normal_by_face_id(int face_id) const;
|
2020-06-05 18:19:19 +00:00
|
|
|
|
2021-05-21 12:08:05 +00:00
|
|
|
const indexed_triangle_set * get_triangle_mesh() const { return m_tm; }
|
2022-05-11 10:08:32 +00:00
|
|
|
|
|
|
|
const VertexFaceIndex &vertex_face_index() const { return m_vfidx; }
|
|
|
|
const std::vector<Vec3i> &face_neighbor_index() const { return m_fnidx; }
|
2019-02-01 15:12:00 +00:00
|
|
|
};
|
|
|
|
|
2019-11-04 13:33:29 +00:00
|
|
|
|
2022-05-11 10:08:32 +00:00
|
|
|
} // namespace Slic3r::sla
|
2019-02-01 15:12:00 +00:00
|
|
|
|
2020-06-25 11:58:51 +00:00
|
|
|
#endif // INDEXEDMESH_H
|