mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-06-12 23:13:32 +00:00
* Update eigen from v3.3.7 to v5.0.1. This updates eigen from v3.3.7 released on December 11, 2018-12-11 to v5.0.1 released on 2025-11-11. There have be a large number of bug-fixes, optimizations, and improvements between these releases. See the details at; https://gitlab.com/libeigen/eigen/-/releases It retains the previous custom minimal `CMakeLists.txt`, and adds a README-OrcaSlicer.md that explains what version and parts of the upstream eigen release have been included, and where the full release can be found. * Update libigl from v2.0.0 (or older) to v2.6.0. This updates libigl from what was probably v2.0.0 released on 2018-10-16 to v2.6.0 released on 2025-05-15. It's possible the old version was even older than that but there is no version indicators in the code and I ran out of patience identifying missing changes and only went back as far as v2.0.0. There have been a large number of bug-fixes, optimizations, and improvements between these versions. See the following for details; https://github.com/libigl/libigl/releases I retained the minimal custom `CMakeLists.txt`, added `README.md` from the libigl distribution which identifies the version, and added a README-OrcaSlicer.md that details the version and parts that have been included. * Update libslic3r for libigl v2.6.0 changes. This updates libslic3r for all changes moving to eigen v5.0.1 and libigl v2.6.0. Despite the large number of updates to both dependencies, no changes were required for the eigen update, and only one change was required for the libigl update. For libigl, `igl::Hit` was changed to a template taking the Scalar type to use. Previously it was hard-coded to `float`, so to minimize possible impact I've updated all places it is used from `igl::Hit` to `igl::Hit<float>`. * Add compiler option `-DNOMINMAX` for libigl with MSVC. MSVC by default defines `min(()` and `max()` macros that break `std::numeric_limits<>::max()`. The upstream cmake that we don't include adds `-DNOMINMAX` for the libigl module when compiling with MSVC, so we need to add the same thing here. * Fix src/libslic3r/TriangleMeshDeal.cpp for the unmodified upstream libigl. This fixes `TriangleMeshDeal.cpp` to work with the unmodified upstream libigl v2.6.0. loop.{h,cpp} implementation. This file and feature was added in PR "BBS Port: Mesh Subdivision" (#12150) which included changes to `loop.{h,cpp}` in the old version of libigl. This PR avoids modifying the included dependencies, and uses the updated upstream versions of those files without any modifications, which requires fixing TriangleMeshDeal.cpp to work with them. In particular, the modifications made to `loop.{h,cpp}` included changing the return type from void to bool, adding additional validation checking of the input meshes, and returning false if they failed validation. These added checks looked unnecessary and would only have caught problems if the input mesh was very corrupt. To make `TriangleMeshDeal.cpp` work without this built-in checking functionality, I removed checking/handling of any `false` return value. There was also a hell of a lot of redundant copying and casting back and forth between float and double, so I cleaned that up. The input and output meshs use floats for the vertexes, and there would be no accuracy benefits from casting to and from doubles for the simple weighted average operations done by igl::loop(). So this just uses `Eigen:Map` to use the original input mesh vertex data directly without requiring any copy or casting. * Move eigen from included `deps_src` to externaly fetched `deps`. This copys what PrusaSlicer did and moved it from an included dependency under `deps_src` to an externaly fetched dependency under `deps`. This requires updating some `CMakeList.txt` configs and removing the old and obsolete `cmake/modules/FindEigen3.cmake`. The details of when this was done in PrusaSlicer and the followup fixes are at; *21116995d7* https://github.com/prusa3d/PrusaSlicer/issues/13608 * https://github.com/prusa3d/PrusaSlicer/pull/13609 *e3c277b9eeFor some reason I don't fully understand this also required fixing `src/slic3r/GUI/GUI_App.cpp` by adding `#include <boost/nowide/cstdio.hpp>` to fix an `error: ‘remove’ is not a member of ‘boost::nowide'`. The main thing I don't understand is how it worked before. Note that this include is in the PrusaSlicer version of this file, but it also significantly deviates from what is currently in OrcaSlicer in many other ways. * Whups... I missed adding the deps/Eigen/Eigen.cmake file... * Tidy some whitespace indenting in CMakeLists.txt. * Ugh... tabs indenting needing fixes. * Change the include order of deps/Eigen. It turns out that although Boost includes some references to Eigen, Eigen also includes some references to Boost for supporting some of it's additional numeric types. I don't think it matters much since we are not using these features, but I think technically its more correct to say Eigen depends on Boost than the other way around, so I've re-ordered them. * Add source for Eigen 5.0.1 download to flatpak yml config. * Add explicit `DEPENDS dep_Boost to deps/Eigen. I missed this before. This ensures we don't rely on include orders to make sure Boost is installed before we configure Eigen. * Add `DEPENDS dep_Boost dep_GMP dep_MPFR` to deps/Eigen. It turns out Eigen can also use GMP and MPFR for multi-precision and multi-precision-rounded numeric types if they are available. Again, I don't think we are using these so it doesn't really matter, but it is technically correct and ensures they are there if we ever do need them. * Fix deps DEPENDENCY ordering for GMP, MPFR, Eigen, and CGAL. I think this is finally correct. Apparently CGAL also optionally depends on Eigen, so the correct dependency order from lowest to highest is GMP, MPFR, Eigen, and CGAL. --------- Co-authored-by: Donovan Baarda <dbaarda@google.com> Co-authored-by: Noisyfox <timemanager.rick@gmail.com>
323 lines
9.8 KiB
C++
323 lines
9.8 KiB
C++
#include "AABBMesh.hpp"
|
|
#include <Execution/ExecutionTBB.hpp>
|
|
|
|
#include <libslic3r/AABBTreeIndirect.hpp>
|
|
#include <libslic3r/TriangleMesh.hpp>
|
|
|
|
#include <numeric>
|
|
|
|
#ifdef SLIC3R_HOLE_RAYCASTER
|
|
#include <libslic3r/SLA/Hollowing.hpp>
|
|
#endif
|
|
|
|
namespace Slic3r {
|
|
|
|
class AABBMesh::AABBImpl {
|
|
private:
|
|
AABBTreeIndirect::Tree3f m_tree;
|
|
double m_triangle_ray_epsilon;
|
|
|
|
public:
|
|
void init(const indexed_triangle_set &its, bool calculate_epsilon)
|
|
{
|
|
m_triangle_ray_epsilon = 0.000001;
|
|
if (calculate_epsilon) {
|
|
// Calculate epsilon from average triangle edge length.
|
|
double l = its_average_edge_length(its);
|
|
if (l > 0)
|
|
m_triangle_ray_epsilon = 0.000001 * l * l;
|
|
}
|
|
m_tree = AABBTreeIndirect::build_aabb_tree_over_indexed_triangle_set(
|
|
its.vertices, its.indices);
|
|
}
|
|
|
|
void intersect_ray(const indexed_triangle_set &its,
|
|
const Vec3d & s,
|
|
const Vec3d & dir,
|
|
igl::Hit<float> & hit)
|
|
{
|
|
AABBTreeIndirect::intersect_ray_first_hit(its.vertices, its.indices,
|
|
m_tree, s, dir, hit, m_triangle_ray_epsilon);
|
|
}
|
|
|
|
void intersect_ray(const indexed_triangle_set &its,
|
|
const Vec3d & s,
|
|
const Vec3d & dir,
|
|
std::vector<igl::Hit<float>> & hits)
|
|
{
|
|
AABBTreeIndirect::intersect_ray_all_hits(its.vertices, its.indices,
|
|
m_tree, s, dir, hits, m_triangle_ray_epsilon);
|
|
}
|
|
|
|
double squared_distance(const indexed_triangle_set & its,
|
|
const Vec3d & point,
|
|
int & i,
|
|
Eigen::Matrix<double, 1, 3> &closest)
|
|
{
|
|
size_t idx_unsigned = 0;
|
|
Vec3d closest_vec3d(closest);
|
|
double dist =
|
|
AABBTreeIndirect::squared_distance_to_indexed_triangle_set(
|
|
its.vertices, its.indices, m_tree, point, idx_unsigned,
|
|
closest_vec3d);
|
|
i = int(idx_unsigned);
|
|
closest = closest_vec3d;
|
|
return dist;
|
|
}
|
|
};
|
|
|
|
template<class M> void AABBMesh::init(const M &mesh, bool calculate_epsilon)
|
|
{
|
|
// Build the AABB accelaration tree
|
|
m_aabb->init(*m_tm, calculate_epsilon);
|
|
}
|
|
|
|
AABBMesh::AABBMesh(const indexed_triangle_set &tmesh, bool calculate_epsilon)
|
|
: m_tm(&tmesh)
|
|
, m_aabb(new AABBImpl())
|
|
, m_vfidx{tmesh}
|
|
, m_fnidx{its_face_neighbors(tmesh)}
|
|
{
|
|
init(tmesh, calculate_epsilon);
|
|
}
|
|
|
|
AABBMesh::AABBMesh(const TriangleMesh &mesh, bool calculate_epsilon)
|
|
: m_tm(&mesh.its)
|
|
, m_aabb(new AABBImpl())
|
|
, m_vfidx{mesh.its}
|
|
, m_fnidx{its_face_neighbors(mesh.its)}
|
|
{
|
|
init(mesh, calculate_epsilon);
|
|
}
|
|
|
|
AABBMesh::~AABBMesh() {}
|
|
|
|
AABBMesh::AABBMesh(const AABBMesh &other)
|
|
: m_tm(other.m_tm)
|
|
, m_aabb(new AABBImpl(*other.m_aabb))
|
|
, m_vfidx{other.m_vfidx}
|
|
, m_fnidx{other.m_fnidx}
|
|
{}
|
|
|
|
AABBMesh &AABBMesh::operator=(const AABBMesh &other)
|
|
{
|
|
m_tm = other.m_tm;
|
|
m_aabb.reset(new AABBImpl(*other.m_aabb));
|
|
m_vfidx = other.m_vfidx;
|
|
m_fnidx = other.m_fnidx;
|
|
|
|
return *this;
|
|
}
|
|
|
|
AABBMesh &AABBMesh::operator=(AABBMesh &&other) = default;
|
|
|
|
AABBMesh::AABBMesh(AABBMesh &&other) = default;
|
|
|
|
|
|
|
|
const std::vector<Vec3f>& AABBMesh::vertices() const
|
|
{
|
|
return m_tm->vertices;
|
|
}
|
|
|
|
|
|
|
|
const std::vector<Vec3i32>& AABBMesh::indices() const
|
|
{
|
|
return m_tm->indices;
|
|
}
|
|
|
|
|
|
|
|
const Vec3f& AABBMesh::vertices(size_t idx) const
|
|
{
|
|
return m_tm->vertices[idx];
|
|
}
|
|
|
|
|
|
|
|
const Vec3i32& AABBMesh::indices(size_t idx) const
|
|
{
|
|
return m_tm->indices[idx];
|
|
}
|
|
|
|
|
|
Vec3d AABBMesh::normal_by_face_id(int face_id) const {
|
|
|
|
return its_unnormalized_normal(*m_tm, face_id).cast<double>().normalized();
|
|
}
|
|
|
|
|
|
AABBMesh::hit_result
|
|
AABBMesh::query_ray_hit(const Vec3d &s, const Vec3d &dir) const
|
|
{
|
|
assert(is_approx(dir.norm(), 1.));
|
|
igl::Hit<float> hit{-1, -1, 0.f, 0.f, 0.f};
|
|
hit.t = std::numeric_limits<float>::infinity();
|
|
|
|
#ifdef SLIC3R_HOLE_RAYCASTER
|
|
if (! m_holes.empty()) {
|
|
|
|
// If there are holes, the hit_results will be made by
|
|
// query_ray_hits (object) and filter_hits (holes):
|
|
return filter_hits(query_ray_hits(s, dir));
|
|
}
|
|
#endif
|
|
|
|
m_aabb->intersect_ray(*m_tm, s, dir, hit);
|
|
hit_result ret(*this);
|
|
ret.m_t = double(hit.t);
|
|
ret.m_dir = dir;
|
|
ret.m_source = s;
|
|
if(!std::isinf(hit.t) && !std::isnan(hit.t)) {
|
|
ret.m_normal = this->normal_by_face_id(hit.id);
|
|
ret.m_face_id = hit.id;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
std::vector<AABBMesh::hit_result>
|
|
AABBMesh::query_ray_hits(const Vec3d &s, const Vec3d &dir) const
|
|
{
|
|
std::vector<AABBMesh::hit_result> outs;
|
|
std::vector<igl::Hit<float>> 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(),
|
|
[](const igl::Hit<float>& a, const igl::Hit<float>& b) { return a.t < b.t; });
|
|
|
|
// Remove duplicates. They sometimes appear, for example when the ray is cast
|
|
// along an axis of a cube due to floating-point approximations in igl (?)
|
|
hits.erase(std::unique(hits.begin(), hits.end(),
|
|
[](const igl::Hit<float>& a, const igl::Hit<float>& b)
|
|
{ return a.t == b.t; }),
|
|
hits.end());
|
|
|
|
// Convert the igl::Hit<float> into hit_result
|
|
outs.reserve(hits.size());
|
|
for (const igl::Hit<float>& hit : hits) {
|
|
outs.emplace_back(AABBMesh::hit_result(*this));
|
|
outs.back().m_t = double(hit.t);
|
|
outs.back().m_dir = dir;
|
|
outs.back().m_source = s;
|
|
if(!std::isinf(hit.t) && !std::isnan(hit.t)) {
|
|
outs.back().m_normal = this->normal_by_face_id(hit.id);
|
|
outs.back().m_face_id = hit.id;
|
|
}
|
|
}
|
|
|
|
return outs;
|
|
}
|
|
|
|
|
|
#ifdef SLIC3R_HOLE_RAYCASTER
|
|
AABBMesh::hit_result IndexedMesh::filter_hits(
|
|
const std::vector<AABBMesh::hit_result>& object_hits) const
|
|
{
|
|
assert(! m_holes.empty());
|
|
hit_result out(*this);
|
|
|
|
if (object_hits.empty())
|
|
return out;
|
|
|
|
const Vec3d& s = object_hits.front().source();
|
|
const Vec3d& dir = object_hits.front().direction();
|
|
|
|
// A helper struct to save an intersetion with a hole
|
|
struct HoleHit {
|
|
HoleHit(float t_p, const Vec3d& normal_p, bool entry_p) :
|
|
t(t_p), normal(normal_p), entry(entry_p) {}
|
|
float t;
|
|
Vec3d normal;
|
|
bool entry;
|
|
};
|
|
std::vector<HoleHit> hole_isects;
|
|
hole_isects.reserve(m_holes.size());
|
|
|
|
auto sf = s.cast<float>();
|
|
auto dirf = dir.cast<float>();
|
|
|
|
// Collect hits on all holes, preserve information about entry/exit
|
|
for (const sla::DrainHole& hole : m_holes) {
|
|
std::array<std::pair<float, Vec3d>, 2> isects;
|
|
if (hole.get_intersections(sf, dirf, isects)) {
|
|
// Ignore hole hits behind the source
|
|
if (isects[0].first > 0.f) hole_isects.emplace_back(isects[0].first, isects[0].second, true);
|
|
if (isects[1].first > 0.f) hole_isects.emplace_back(isects[1].first, isects[1].second, false);
|
|
}
|
|
}
|
|
|
|
// Holes can intersect each other, sort the hits by t
|
|
std::sort(hole_isects.begin(), hole_isects.end(),
|
|
[](const HoleHit& a, const HoleHit& b) { return a.t < b.t; });
|
|
|
|
// Now inspect the intersections with object and holes, in the order of
|
|
// increasing distance. Keep track how deep are we nested in mesh/holes and
|
|
// pick the correct intersection.
|
|
// This needs to be done twice - first to find out how deep in the structure
|
|
// the source is, then to pick the correct intersection.
|
|
int hole_nested = 0;
|
|
int object_nested = 0;
|
|
for (int dry_run=1; dry_run>=0; --dry_run) {
|
|
hole_nested = -hole_nested;
|
|
object_nested = -object_nested;
|
|
|
|
bool is_hole = false;
|
|
bool is_entry = false;
|
|
const HoleHit* next_hole_hit = hole_isects.empty() ? nullptr : &hole_isects.front();
|
|
const hit_result* next_mesh_hit = &object_hits.front();
|
|
|
|
while (next_hole_hit || next_mesh_hit) {
|
|
if (next_hole_hit && next_mesh_hit) // still have hole and obj hits
|
|
is_hole = (next_hole_hit->t < next_mesh_hit->m_t);
|
|
else
|
|
is_hole = next_hole_hit; // one or the other ran out
|
|
|
|
// Is this entry or exit hit?
|
|
is_entry = is_hole ? next_hole_hit->entry : ! next_mesh_hit->is_inside();
|
|
|
|
if (! dry_run) {
|
|
if (! is_hole && hole_nested == 0) {
|
|
// This is a valid object hit
|
|
return *next_mesh_hit;
|
|
}
|
|
if (is_hole && ! is_entry && object_nested != 0) {
|
|
// This holehit is the one we seek
|
|
out.m_t = next_hole_hit->t;
|
|
out.m_normal = next_hole_hit->normal;
|
|
out.m_source = s;
|
|
out.m_dir = dir;
|
|
return out;
|
|
}
|
|
}
|
|
|
|
// Increase/decrease the counter
|
|
(is_hole ? hole_nested : object_nested) += (is_entry ? 1 : -1);
|
|
|
|
// Advance the respective pointer
|
|
if (is_hole && next_hole_hit++ == &hole_isects.back())
|
|
next_hole_hit = nullptr;
|
|
if (! is_hole && next_mesh_hit++ == &object_hits.back())
|
|
next_mesh_hit = nullptr;
|
|
}
|
|
}
|
|
|
|
// if we got here, the ray ended up in infinity
|
|
return out;
|
|
}
|
|
#endif
|
|
|
|
|
|
double AABBMesh::squared_distance(const Vec3d &p, int& i, Vec3d& c) const {
|
|
double sqdst = 0;
|
|
Eigen::Matrix<double, 1, 3> pp = p;
|
|
Eigen::Matrix<double, 1, 3> cc;
|
|
sqdst = m_aabb->squared_distance(*m_tm, pp, i, cc);
|
|
c = cc;
|
|
return sqdst;
|
|
}
|
|
|
|
} // namespace Slic3r
|