From 5a84b46ec9a037adf677489d2051b3baef9fe4e1 Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Thu, 16 Sep 2021 13:38:02 +0200 Subject: [PATCH] Fix of detection of the out of bed state for sinking objects --- src/libslic3r/Technologies.hpp | 2 ++ src/libslic3r/TriangleMesh.cpp | 26 ++++++++++++++++++++++++++ src/libslic3r/TriangleMesh.hpp | 4 ++++ src/slic3r/GUI/3DScene.cpp | 21 +++++++++++++++++++++ src/slic3r/GUI/3DScene.hpp | 18 ++++++++++++++++++ 5 files changed, 71 insertions(+) diff --git a/src/libslic3r/Technologies.hpp b/src/libslic3r/Technologies.hpp index 8da21d56a6..df7414eda7 100644 --- a/src/libslic3r/Technologies.hpp +++ b/src/libslic3r/Technologies.hpp @@ -62,6 +62,8 @@ #define ENABLE_FIX_PREVIEW_OPTIONS_Z (1 && ENABLE_SEAMS_USING_MODELS && ENABLE_2_4_0_ALPHA2) // Enable replacing a missing file during reload from disk command #define ENABLE_RELOAD_FROM_DISK_REPLACE_FILE (1 && ENABLE_2_4_0_ALPHA2) +// Enable the fix for the detection of the out of bed state for sinking objects +#define ENABLE_FIX_SINKING_OBJECT_OUT_OF_BED_DETECTION (1 && ENABLE_2_4_0_ALPHA2) #endif // _prusaslicer_technologies_h_ diff --git a/src/libslic3r/TriangleMesh.cpp b/src/libslic3r/TriangleMesh.cpp index d289fca141..822548412b 100644 --- a/src/libslic3r/TriangleMesh.cpp +++ b/src/libslic3r/TriangleMesh.cpp @@ -574,6 +574,32 @@ BoundingBoxf3 TriangleMesh::transformed_bounding_box(const Transform3d &trafo) c return bbox; } +#if ENABLE_FIX_SINKING_OBJECT_OUT_OF_BED_DETECTION +BoundingBoxf3 TriangleMesh::transformed_bounding_box(const Transform3d& trafo, double world_min_z) const +{ + assert(!its.vertices.empty()); + + BoundingBoxf3 bbox; + // add vertices above the cut + for (const stl_vertex& v : its.vertices) { + const Vec3d world_v = trafo * v.cast(); + if (world_v.z() > world_min_z) + bbox.merge(world_v); + } + + // add new vertices along the cut + MeshSlicingParams slicing_params; + slicing_params.trafo = trafo; + Polygons polygons = union_(slice_mesh(its, world_min_z, slicing_params)); + for (const Polygon& polygon : polygons) { + for (const Point& p : polygon.points) { + bbox.merge(unscale(p.x(), p.y(), world_min_z)); + } + } + return bbox; +} +#endif // ENABLE_FIX_SINKING_OBJECT_OUT_OF_BED_DETECTION + TriangleMesh TriangleMesh::convex_hull_3d() const { // The qhull call: diff --git a/src/libslic3r/TriangleMesh.hpp b/src/libslic3r/TriangleMesh.hpp index 60ab975c4c..a603dd0645 100644 --- a/src/libslic3r/TriangleMesh.hpp +++ b/src/libslic3r/TriangleMesh.hpp @@ -57,6 +57,10 @@ public: BoundingBoxf3 bounding_box() const; // Returns the bbox of this TriangleMesh transformed by the given transformation BoundingBoxf3 transformed_bounding_box(const Transform3d &trafo) const; +#if ENABLE_FIX_SINKING_OBJECT_OUT_OF_BED_DETECTION + // Variant returning the bbox of the part of this TriangleMesh above the given world_min_z + BoundingBoxf3 transformed_bounding_box(const Transform3d& trafo, double world_min_z) const; +#endif // ENABLE_FIX_SINKING_OBJECT_OUT_OF_BED_DETECTION // Return the size of the mesh in coordinates. Vec3d size() const { return stl.stats.size.cast(); } /// Return the center of the related bounding box. diff --git a/src/slic3r/GUI/3DScene.cpp b/src/slic3r/GUI/3DScene.cpp index f693143c42..19864b36a4 100644 --- a/src/slic3r/GUI/3DScene.cpp +++ b/src/slic3r/GUI/3DScene.cpp @@ -524,6 +524,23 @@ BoundingBoxf3 GLVolume::transformed_convex_hull_bounding_box(const Transform3d & bounding_box().transformed(trafo); } +#if ENABLE_FIX_SINKING_OBJECT_OUT_OF_BED_DETECTION +BoundingBoxf3 GLVolume::transformed_non_sinking_bounding_box(const Transform3d& trafo) const +{ + return GUI::wxGetApp().plater()->model().objects[object_idx()]->volumes[volume_idx()]->mesh().transformed_bounding_box(trafo, 0.0); +} + +const BoundingBoxf3& GLVolume::transformed_non_sinking_bounding_box() const +{ + if (!m_transformed_non_sinking_bounding_box.has_value()) { + std::optional* trans_box = const_cast*>(&m_transformed_non_sinking_bounding_box); + const Transform3d& trafo = world_matrix(); + *trans_box = transformed_non_sinking_bounding_box(trafo); + } + return *m_transformed_non_sinking_bounding_box; +} +#endif // ENABLE_FIX_SINKING_OBJECT_OUT_OF_BED_DETECTION + void GLVolume::set_range(double min_z, double max_z) { this->qverts_range.first = 0; @@ -936,7 +953,11 @@ bool GLVolumeCollection::check_outside_state(const DynamicPrintConfig* config, M if (volume->is_modifier || (!volume->shader_outside_printer_detection_enabled && (volume->is_wipe_tower || volume->composite_id.volume_id < 0))) continue; +#if ENABLE_FIX_SINKING_OBJECT_OUT_OF_BED_DETECTION + const BoundingBoxf3& bb = volume->transformed_non_sinking_bounding_box(); +#else const BoundingBoxf3& bb = volume->transformed_convex_hull_bounding_box(); +#endif // ENABLE_FIX_SINKING_OBJECT_OUT_OF_BED_DETECTION bool contained = print_volume.contains(bb); volume->is_outside = !contained; diff --git a/src/slic3r/GUI/3DScene.hpp b/src/slic3r/GUI/3DScene.hpp index 78b9a96d9d..4db1611e7e 100644 --- a/src/slic3r/GUI/3DScene.hpp +++ b/src/slic3r/GUI/3DScene.hpp @@ -279,6 +279,10 @@ private: std::shared_ptr m_convex_hull; // Bounding box of this volume, in unscaled coordinates. std::optional m_transformed_convex_hull_bounding_box; +#if ENABLE_FIX_SINKING_OBJECT_OUT_OF_BED_DETECTION + // Bounding box of the non sinking part of this volume, in unscaled coordinates. + std::optional m_transformed_non_sinking_bounding_box; +#endif // ENABLE_FIX_SINKING_OBJECT_OUT_OF_BED_DETECTION class SinkingContours { @@ -469,6 +473,12 @@ public: BoundingBoxf3 transformed_convex_hull_bounding_box(const Transform3d &trafo) const; // caching variant const BoundingBoxf3& transformed_convex_hull_bounding_box() const; +#if ENABLE_FIX_SINKING_OBJECT_OUT_OF_BED_DETECTION + // non-caching variant + BoundingBoxf3 transformed_non_sinking_bounding_box(const Transform3d& trafo) const; + // caching variant + const BoundingBoxf3& transformed_non_sinking_bounding_box() const; +#endif // ENABLE_FIX_SINKING_OBJECT_OUT_OF_BED_DETECTION // convex hull const TriangleMesh* convex_hull() const { return m_convex_hull.get(); } @@ -481,7 +491,15 @@ public: void finalize_geometry(bool opengl_initialized) { this->indexed_vertex_array.finalize_geometry(opengl_initialized); } void release_geometry() { this->indexed_vertex_array.release_geometry(); } +#if ENABLE_FIX_SINKING_OBJECT_OUT_OF_BED_DETECTION + void set_bounding_boxes_as_dirty() { + m_transformed_bounding_box.reset(); + m_transformed_convex_hull_bounding_box.reset(); + m_transformed_non_sinking_bounding_box.reset(); + } +#else void set_bounding_boxes_as_dirty() { m_transformed_bounding_box.reset(); m_transformed_convex_hull_bounding_box.reset(); } +#endif // ENABLE_FIX_SINKING_OBJECT_OUT_OF_BED_DETECTION bool is_sla_support() const; bool is_sla_pad() const;