From ad9fa81b018263acaf554a6d6f541e48530d5a64 Mon Sep 17 00:00:00 2001 From: Arthur Date: Sat, 19 Aug 2023 10:57:07 +0800 Subject: [PATCH] ENH: add a new support style "Tree Organic" Add a new suport style "Tree Organic" from Prusa, as organic support is faster and seems more strong in some cases. Thanks to Prusa. Feature detection including sharp tail, small overhang, long cantilever, are still kept. Known issue: first layer support path may go outside build plate. Jira: STUDIO-2358 Github: #2420 Change-Id: I4cec149bf4fa9eb733ae720ac1a7f65098e3b951 (cherry picked from commit d977bc5d3b4609f4fec0aa68152a33cacf184c4a) --- src/libslic3r/BuildVolume.cpp | 11 + src/libslic3r/BuildVolume.hpp | 1 + src/libslic3r/CMakeLists.txt | 4 + src/libslic3r/ClipperUtils.cpp | 27 +- src/libslic3r/ClipperUtils.hpp | 20 +- src/libslic3r/ExtrusionEntity.hpp | 14 +- src/libslic3r/Layer.hpp | 2 +- src/libslic3r/MeshBoolean.cpp | 6 + src/libslic3r/Point.hpp | 34 +- src/libslic3r/Polygon.cpp | 19 + src/libslic3r/Polygon.hpp | 3 + src/libslic3r/Polyline.hpp | 5 +- src/libslic3r/PrintConfig.cpp | 8 +- src/libslic3r/PrintConfig.hpp | 2 +- src/libslic3r/PrintObject.cpp | 15 +- src/libslic3r/SLAPrintSteps.cpp | 16 +- src/libslic3r/SupportMaterial.cpp | 1102 ++++--- src/libslic3r/SupportMaterial.hpp | 422 +-- src/libslic3r/TreeModelVolumes.cpp | 895 ++++++ src/libslic3r/TreeModelVolumes.hpp | 679 ++++ src/libslic3r/TreeSupport.cpp | 86 +- src/libslic3r/TreeSupport.hpp | 17 +- src/libslic3r/TreeSupport3D.cpp | 4142 +++++++++++++++++++++++++ src/libslic3r/TreeSupport3D.hpp | 599 ++++ src/libslic3r/Utils.hpp | 11 +- src/slic3r/GUI/ConfigManipulation.cpp | 4 +- src/slic3r/GUI/Tab.cpp | 4 +- 27 files changed, 7474 insertions(+), 674 deletions(-) create mode 100644 src/libslic3r/TreeModelVolumes.cpp create mode 100644 src/libslic3r/TreeModelVolumes.hpp create mode 100644 src/libslic3r/TreeSupport3D.cpp create mode 100644 src/libslic3r/TreeSupport3D.hpp diff --git a/src/libslic3r/BuildVolume.cpp b/src/libslic3r/BuildVolume.cpp index 4e4e593c1..3e6972ffc 100644 --- a/src/libslic3r/BuildVolume.cpp +++ b/src/libslic3r/BuildVolume.cpp @@ -419,4 +419,15 @@ std::string_view BuildVolume::type_name(Type type) return {}; } +indexed_triangle_set BuildVolume::bounding_mesh(bool scale) const +{ + auto max_pt3 = m_bboxf.max; + if (scale) { + return its_make_cube(scale_(max_pt3.x()), scale_(max_pt3.y()), scale_(max_pt3.z())); + } + else { + return its_make_cube(max_pt3.x(), max_pt3.y(), max_pt3.z()); + } +} + } // namespace Slic3r diff --git a/src/libslic3r/BuildVolume.hpp b/src/libslic3r/BuildVolume.hpp index 715480763..18471f14b 100644 --- a/src/libslic3r/BuildVolume.hpp +++ b/src/libslic3r/BuildVolume.hpp @@ -53,6 +53,7 @@ public: // Bounding volume of printable_area(), printable_height(), unscaled. const BoundingBoxf3& bounding_volume() const { return m_bboxf; } BoundingBoxf bounding_volume2d() const { return { to_2d(m_bboxf.min), to_2d(m_bboxf.max) }; } + indexed_triangle_set bounding_mesh(bool scale=true) const; // Center of the print bed, unscaled. Vec2d bed_center() const { return to_2d(m_bboxf.center()); } diff --git a/src/libslic3r/CMakeLists.txt b/src/libslic3r/CMakeLists.txt index f55363519..715371521 100644 --- a/src/libslic3r/CMakeLists.txt +++ b/src/libslic3r/CMakeLists.txt @@ -264,6 +264,10 @@ set(lisbslic3r_sources SupportMaterial.hpp TreeSupport.hpp TreeSupport.cpp + TreeSupport3D.hpp + TreeSupport3D.cpp + TreeModelVolumes.hpp + TreeModelVolumes.cpp MinimumSpanningTree.hpp MinimumSpanningTree.cpp Surface.cpp diff --git a/src/libslic3r/ClipperUtils.cpp b/src/libslic3r/ClipperUtils.cpp index ecd0ab4f5..5ccf70b43 100644 --- a/src/libslic3r/ClipperUtils.cpp +++ b/src/libslic3r/ClipperUtils.cpp @@ -69,11 +69,17 @@ template inline void clip_clipper_polygon_with_subject_bbox_ const size_t cnt = src.size(); if (cnt < 3) return; - enum class Side { Left = 1, Right = 2, Top = 4, Bottom = 8 }; - + enum class Side { + Left = 1, + Right = 2, + Top = 4, + Bottom = 8 + }; auto sides = [bbox](const PointType &p) { - return int(p.x() < bbox.min.x()) * int(Side::Left) + int(p.x() > bbox.max.x()) * int(Side::Right) + int(p.y() < bbox.min.y()) * int(Side::Bottom) + - int(p.y() > bbox.max.y()) * int(Side::Top); + return int(p.x() < bbox.min.x()) * int(Side::Left) + + int(p.x() > bbox.max.x()) * int(Side::Right) + + int(p.y() < bbox.min.y()) * int(Side::Bottom) + + int(p.y() > bbox.max.y()) * int(Side::Top); }; int sides_prev = sides(src.back()); @@ -552,6 +558,8 @@ Slic3r::ExPolygons offset_ex(const Slic3r::ExPolygons &expolygons, const float d { return PolyTreeToExPolygons(expolygons_offset_pt(expolygons, delta, joinType, miterLimit)); } Slic3r::ExPolygons offset_ex(const Slic3r::Surfaces &surfaces, const float delta, ClipperLib::JoinType joinType, double miterLimit) { return PolyTreeToExPolygons(expolygons_offset_pt(surfaces, delta, joinType, miterLimit)); } +Slic3r::ExPolygons offset_ex(const Slic3r::SurfacesPtr &surfaces, const float delta, ClipperLib::JoinType joinType, double miterLimit) + { return PolyTreeToExPolygons(expolygons_offset_pt(surfaces, delta, joinType, miterLimit)); } Polygons offset2(const ExPolygons &expolygons, const float delta1, const float delta2, ClipperLib::JoinType joinType, double miterLimit) { @@ -655,6 +663,8 @@ Slic3r::Polygons diff(const Slic3r::Polygon &subject, const Slic3r::Polygon &cli { return _clipper(ClipperLib::ctDifference, ClipperUtils::SinglePathProvider(subject.points), ClipperUtils::SinglePathProvider(clip.points), do_safety_offset); } Slic3r::Polygons diff(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, ApplySafetyOffset do_safety_offset) { return _clipper(ClipperLib::ctDifference, ClipperUtils::PolygonsProvider(subject), ClipperUtils::PolygonsProvider(clip), do_safety_offset); } +Slic3r::Polygons diff_clipped(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, ApplySafetyOffset do_safety_offset) + { return diff(subject, ClipperUtils::clip_clipper_polygons_with_subject_bbox(clip, get_extents(subject).inflated(SCALED_EPSILON)), do_safety_offset); } Slic3r::Polygons diff(const Slic3r::Polygons &subject, const Slic3r::ExPolygons &clip, ApplySafetyOffset do_safety_offset) { return _clipper(ClipperLib::ctDifference, ClipperUtils::PolygonsProvider(subject), ClipperUtils::ExPolygonsProvider(clip), do_safety_offset); } Slic3r::Polygons diff(const Slic3r::ExPolygons &subject, const Slic3r::Polygons &clip, ApplySafetyOffset do_safety_offset) @@ -701,7 +711,8 @@ Slic3r::Polygons union_(const Slic3r::Polygons &subject, const Slic3r::Polygons polys.push_back(poly); return union_(polys); } - +Slic3r::Polygons union_(const Slic3r::Polygons &subject, const Slic3r::ExPolygon &subject2) + { return _clipper(ClipperLib::ctUnion, ClipperUtils::PolygonsProvider(subject), ClipperUtils::ExPolygonProvider(subject2), ApplySafetyOffset::No); } template static ExPolygons _clipper_ex(ClipperLib::ClipType clipType, TSubject &&subject, TClip &&clip, ApplySafetyOffset do_safety_offset, ClipperLib::PolyFillType fill_type = ClipperLib::pftNonZero) { return PolyTreeToExPolygons(clipper_do_polytree(clipType, std::forward(subject), std::forward(clip), fill_type, do_safety_offset)); } @@ -710,6 +721,8 @@ Slic3r::ExPolygons diff_ex(const Slic3r::Polygons &subject, const Slic3r::Polygo { return _clipper_ex(ClipperLib::ctDifference, ClipperUtils::PolygonsProvider(subject), ClipperUtils::PolygonsProvider(clip), do_safety_offset); } Slic3r::ExPolygons diff_ex(const Slic3r::Polygons &subject, const Slic3r::Surfaces &clip, ApplySafetyOffset do_safety_offset) { return _clipper_ex(ClipperLib::ctDifference, ClipperUtils::PolygonsProvider(subject), ClipperUtils::SurfacesProvider(clip), do_safety_offset); } +Slic3r::ExPolygons diff_ex(const Slic3r::Polygon &subject, const Slic3r::ExPolygons &clip, ApplySafetyOffset do_safety_offset) + { return _clipper_ex(ClipperLib::ctDifference, ClipperUtils::SinglePathProvider(subject.points), ClipperUtils::ExPolygonsProvider(clip), do_safety_offset); } Slic3r::ExPolygons diff_ex(const Slic3r::Polygons &subject, const Slic3r::ExPolygons &clip, ApplySafetyOffset do_safety_offset) { return _clipper_ex(ClipperLib::ctDifference, ClipperUtils::PolygonsProvider(subject), ClipperUtils::ExPolygonsProvider(clip), do_safety_offset); } Slic3r::ExPolygons diff_ex(const Slic3r::ExPolygon &subject, const Slic3r::Polygon &clip, ApplySafetyOffset do_safety_offset) @@ -864,6 +877,8 @@ Slic3r::Polylines diff_pl(const Slic3r::Polylines &subject, const Slic3r::Polygo { return _clipper_pl_open(ClipperLib::ctDifference, ClipperUtils::PolylinesProvider(subject), ClipperUtils::PolygonsProvider(clip)); } Slic3r::Polylines diff_pl(const Slic3r::Polyline &subject, const Slic3r::ExPolygon &clip) { return _clipper_pl_open(ClipperLib::ctDifference, ClipperUtils::SinglePathProvider(subject.points), ClipperUtils::ExPolygonProvider(clip)); } +Slic3r::Polylines diff_pl(const Slic3r::Polyline &subject, const Slic3r::ExPolygons &clip) + { return _clipper_pl_open(ClipperLib::ctDifference, ClipperUtils::SinglePathProvider(subject.points), ClipperUtils::ExPolygonsProvider(clip)); } Slic3r::Polylines diff_pl(const Slic3r::Polylines &subject, const Slic3r::ExPolygon &clip) { return _clipper_pl_open(ClipperLib::ctDifference, ClipperUtils::PolylinesProvider(subject), ClipperUtils::ExPolygonProvider(clip)); } Slic3r::Polylines diff_pl(const Slic3r::Polylines &subject, const Slic3r::ExPolygons &clip) @@ -878,6 +893,8 @@ Slic3r::Polylines intersection_pl(const Slic3r::Polylines &subject, const Slic3r { return _clipper_pl_open(ClipperLib::ctIntersection, ClipperUtils::PolylinesProvider(subject), ClipperUtils::ExPolygonProvider(clip)); } Slic3r::Polylines intersection_pl(const Slic3r::Polyline &subject, const Slic3r::Polygons &clip) { return _clipper_pl_open(ClipperLib::ctIntersection, ClipperUtils::SinglePathProvider(subject.points), ClipperUtils::PolygonsProvider(clip)); } +Slic3r::Polylines intersection_pl(const Slic3r::Polyline &subject, const Slic3r::ExPolygons &clip) + { return _clipper_pl_open(ClipperLib::ctIntersection, ClipperUtils::SinglePathProvider(subject.points), ClipperUtils::ExPolygonsProvider(clip)); } Slic3r::Polylines intersection_pl(const Slic3r::Polylines &subject, const Slic3r::Polygons &clip) { return _clipper_pl_open(ClipperLib::ctIntersection, ClipperUtils::PolylinesProvider(subject), ClipperUtils::PolygonsProvider(clip)); } Slic3r::Polylines intersection_pl(const Slic3r::Polylines &subject, const Slic3r::ExPolygons &clip) diff --git a/src/libslic3r/ClipperUtils.hpp b/src/libslic3r/ClipperUtils.hpp index c36778e82..13cbdcf7d 100644 --- a/src/libslic3r/ClipperUtils.hpp +++ b/src/libslic3r/ClipperUtils.hpp @@ -1,19 +1,31 @@ #ifndef slic3r_ClipperUtils_hpp_ #define slic3r_ClipperUtils_hpp_ +//#define SLIC3R_USE_CLIPPER2 + #include "libslic3r.h" -#include "clipper.hpp" #include "ExPolygon.hpp" #include "Polygon.hpp" #include "Surface.hpp" +#ifdef SLIC3R_USE_CLIPPER2 + +#include + +#else /* SLIC3R_USE_CLIPPER2 */ + +#include "clipper.hpp" // import these wherever we're included using Slic3r::ClipperLib::jtMiter; using Slic3r::ClipperLib::jtRound; using Slic3r::ClipperLib::jtSquare; +#endif /* SLIC3R_USE_CLIPPER2 */ + namespace Slic3r { +class BoundingBox; + static constexpr const float ClipperSafetyOffset = 10.f; static constexpr const Slic3r::ClipperLib::JoinType DefaultJoinType = Slic3r::ClipperLib::jtMiter; @@ -415,6 +427,9 @@ Slic3r::Lines _clipper_ln(ClipperLib::ClipType clipType, const Slic3r::Lines &su Slic3r::Polygons diff(const Slic3r::Polygon &subject, const Slic3r::Polygon &clip, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No); Slic3r::Polygons diff(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No); Slic3r::Polygons diff(const Slic3r::Polygons &subject, const Slic3r::ExPolygons &clip, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No); +// Optimized version clipping the "clipping" polygon using clip_clipper_polygon_with_subject_bbox(). +// To be used with complex clipping polygons, where majority of the clipping polygons are outside of the source polygon. +Slic3r::Polygons diff_clipped(const Slic3r::Polygons &src, const Slic3r::Polygons &clipping, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No); Slic3r::Polygons diff(const Slic3r::ExPolygons &subject, const Slic3r::Polygons &clip, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No); Slic3r::Polygons diff(const Slic3r::ExPolygons &subject, const Slic3r::ExPolygons &clip, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No); Slic3r::Polygons diff(const Slic3r::Surfaces &subject, const Slic3r::Polygons &clip, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No); @@ -434,6 +449,7 @@ Slic3r::ExPolygons diff_ex(const Slic3r::SurfacesPtr &subject, const Slic3r::Pol Slic3r::Polylines diff_pl(const Slic3r::Polyline& subject, const Slic3r::Polygons& clip); Slic3r::Polylines diff_pl(const Slic3r::Polylines &subject, const Slic3r::Polygons &clip); Slic3r::Polylines diff_pl(const Slic3r::Polyline &subject, const Slic3r::ExPolygon &clip); +Slic3r::Polylines diff_pl(const Slic3r::Polyline &subject, const Slic3r::ExPolygons &clip); Slic3r::Polylines diff_pl(const Slic3r::Polylines &subject, const Slic3r::ExPolygon &clip); Slic3r::Polylines diff_pl(const Slic3r::Polylines &subject, const Slic3r::ExPolygons &clip); Slic3r::Polylines diff_pl(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip); @@ -497,6 +513,7 @@ Slic3r::Polylines intersection_pl(const Slic3r::Polylines &subject, const Slic3 Slic3r::Polylines intersection_pl(const Slic3r::Polyline &subject, const Slic3r::ExPolygon &clip); Slic3r::Polylines intersection_pl(const Slic3r::Polylines &subject, const Slic3r::ExPolygon &clip); Slic3r::Polylines intersection_pl(const Slic3r::Polyline &subject, const Slic3r::Polygons &clip); +Slic3r::Polylines intersection_pl(const Slic3r::Polyline &subject, const Slic3r::ExPolygons &clip); Slic3r::Polylines intersection_pl(const Slic3r::Polylines &subject, const Slic3r::Polygons &clip); Slic3r::Polylines intersection_pl(const Slic3r::Polylines &subject, const Slic3r::ExPolygons &clip); Slic3r::Polylines intersection_pl(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip); @@ -517,6 +534,7 @@ Slic3r::Polygons union_(const Slic3r::Polygons &subject); Slic3r::Polygons union_(const Slic3r::ExPolygons &subject); Slic3r::Polygons union_(const Slic3r::Polygons &subject, const ClipperLib::PolyFillType fillType); Slic3r::Polygons union_(const Slic3r::Polygons &subject, const Slic3r::Polygons &subject2); +Slic3r::Polygons union_(const Slic3r::Polygons &subject, const Slic3r::ExPolygon &subject2); // May be used to "heal" unusual models (3DLabPrints etc.) by providing fill_type (pftEvenOdd, pftNonZero, pftPositive, pftNegative). Slic3r::ExPolygons union_ex(const Slic3r::Polygons &subject, ClipperLib::PolyFillType fill_type = ClipperLib::pftNonZero); Slic3r::ExPolygons union_ex(const Slic3r::ExPolygons &subject); diff --git a/src/libslic3r/ExtrusionEntity.hpp b/src/libslic3r/ExtrusionEntity.hpp index 34ee42304..4ab5d92ee 100644 --- a/src/libslic3r/ExtrusionEntity.hpp +++ b/src/libslic3r/ExtrusionEntity.hpp @@ -284,6 +284,16 @@ private: bool m_no_extrusion = false; }; +class ExtrusionPathOriented : public ExtrusionPath +{ +public: + ExtrusionPathOriented(ExtrusionRole role, double mm3_per_mm, float width, float height) : ExtrusionPath(role, mm3_per_mm, width, height) {} + ExtrusionEntity* clone() const override { return new ExtrusionPathOriented(*this); } + // Create a new object, initialize it with this object using the move semantics. + ExtrusionEntity* clone_move() override { return new ExtrusionPathOriented(std::move(*this)); } + virtual bool can_reverse() const override { return false; } +}; + typedef std::vector ExtrusionPaths; // Single continuous extrusion path, possibly with varying extrusion thickness, extrusion height or bridging / non bridging. @@ -485,12 +495,12 @@ inline void extrusion_entities_append_paths(ExtrusionEntitiesPtr &dst, Polylines } } -inline void extrusion_entities_append_paths(ExtrusionEntitiesPtr &dst, Polylines &&polylines, ExtrusionRole role, double mm3_per_mm, float width, float height) +inline void extrusion_entities_append_paths(ExtrusionEntitiesPtr &dst, Polylines &&polylines, ExtrusionRole role, double mm3_per_mm, float width, float height, bool can_reverse = true) { dst.reserve(dst.size() + polylines.size()); for (Polyline &polyline : polylines) if (polyline.is_valid()) { - ExtrusionPath *extrusion_path = new ExtrusionPath(role, mm3_per_mm, width, height); + ExtrusionPath *extrusion_path = can_reverse ? new ExtrusionPath(role, mm3_per_mm, width, height) : new ExtrusionPathOriented(role, mm3_per_mm, width, height); dst.push_back(extrusion_path); extrusion_path->polyline = std::move(polyline); } diff --git a/src/libslic3r/Layer.hpp b/src/libslic3r/Layer.hpp index ac3e054e7..dee83119f 100644 --- a/src/libslic3r/Layer.hpp +++ b/src/libslic3r/Layer.hpp @@ -271,6 +271,7 @@ public: // for tree supports ExPolygons base_areas; + ExPolygons overhang_areas; // Is there any valid extrusion assigned to this LayerRegion? @@ -294,7 +295,6 @@ protected: size_t m_interface_id; // for tree support - ExPolygons overhang_areas; ExPolygons roof_areas; ExPolygons roof_1st_layer; // the layer just below roof. When working with PolySupport, this layer should be printed with regular material ExPolygons floor_areas; diff --git a/src/libslic3r/MeshBoolean.cpp b/src/libslic3r/MeshBoolean.cpp index 61e5cef3e..50bbc099e 100644 --- a/src/libslic3r/MeshBoolean.cpp +++ b/src/libslic3r/MeshBoolean.cpp @@ -223,6 +223,12 @@ indexed_triangle_set cgal_to_indexed_triangle_set(const _Mesh &cgalmesh) return its; } +template TriangleMesh cgal_to_triangle_mesh(const _Mesh &cgalmesh) +{ + indexed_triangle_set its = cgal_to_indexed_triangle_set(cgalmesh); + return TriangleMesh(std::move(its)); +} + std::unique_ptr triangle_mesh_to_cgal(const std::vector &V, const std::vector &F) diff --git a/src/libslic3r/Point.hpp b/src/libslic3r/Point.hpp index b29115762..b3cc1574a 100644 --- a/src/libslic3r/Point.hpp +++ b/src/libslic3r/Point.hpp @@ -96,11 +96,35 @@ inline typename Derived::Scalar cross2(const Eigen::MatrixBase &v1, con template inline Eigen::Matrix perp(const Eigen::MatrixBase> &v) { return Eigen::Matrix(- v.y(), v.x()); } -template -Eigen::Matrix to_2d(const Eigen::MatrixBase> &ptN) { return { ptN.x(), ptN.y() }; } +// 2D vector perpendicular to the argument. +template +inline Eigen::Matrix perp(const Eigen::MatrixBase& v) +{ + static_assert(Derived::IsVectorAtCompileTime && int(Derived::SizeAtCompileTime) == 2, "perp(): parameter is not a 2D vector"); + return { -v.y(), v.x() }; +} -template -Eigen::Matrix to_3d(const Eigen::MatrixBase> & pt, const T z) { return { pt.x(), pt.y(), z }; } +// Angle from v1 to v2, returning double atan2(y, x) normalized to <-PI, PI>. +template +inline double angle(const Eigen::MatrixBase &v1, const Eigen::MatrixBase &v2) { + static_assert(Derived::IsVectorAtCompileTime && int(Derived::SizeAtCompileTime) == 2, "angle(): first parameter is not a 2D vector"); + static_assert(Derived2::IsVectorAtCompileTime && int(Derived2::SizeAtCompileTime) == 2, "angle(): second parameter is not a 2D vector"); + auto v1d = v1.template cast(); + auto v2d = v2.template cast(); + return atan2(cross2(v1d, v2d), v1d.dot(v2d)); +} + +template +Eigen::Matrix to_2d(const Eigen::MatrixBase &ptN) { + static_assert(Derived::IsVectorAtCompileTime && int(Derived::SizeAtCompileTime) >= 3, "to_2d(): first parameter is not a 3D or higher dimensional vector"); + return ptN.template head<2>(); +} + +template +inline Eigen::Matrix to_3d(const Eigen::MatrixBase &pt, const typename Derived::Scalar z) { + static_assert(Derived::IsVectorAtCompileTime && int(Derived::SizeAtCompileTime) == 2, "to_3d(): first parameter is not a 2D vector"); + return { pt.x(), pt.y(), z }; +} inline Vec2d unscale(coord_t x, coord_t y) { return Vec2d(unscale(x), unscale(y)); } inline Vec2d unscale(const Vec2crd &pt) { return Vec2d(unscale(pt.x()), unscale(pt.y())); } @@ -211,6 +235,8 @@ inline bool is_approx(const Point &p1, const Point &p2, coord_t epsilon = coord_ return d.x() < epsilon && d.y() < epsilon; } +inline Point turn90_ccw(const Point pt) { return Point(-pt(1), pt(0)); } + inline bool is_approx(const Vec2f &p1, const Vec2f &p2, float epsilon = float(EPSILON)) { Vec2f d = (p2 - p1).cwiseAbs(); diff --git a/src/libslic3r/Polygon.cpp b/src/libslic3r/Polygon.cpp index 525415791..0f2be6f24 100644 --- a/src/libslic3r/Polygon.cpp +++ b/src/libslic3r/Polygon.cpp @@ -664,4 +664,23 @@ bool contains(const Polygons &polygons, const Point &p, bool border_result) } return (poly_count_inside % 2) == 1; } + +Polygon make_circle(double radius, double error) +{ + double angle = 2. * acos(1. - error / radius); + size_t num_segments = size_t(ceil(2. * M_PI / angle)); + return make_circle_num_segments(radius, num_segments); +} + +Polygon make_circle_num_segments(double radius, size_t num_segments) +{ + Polygon out; + out.points.reserve(num_segments); + double angle_inc = 2.0 * M_PI / num_segments; + for (size_t i = 0; i < num_segments; ++ i) { + const double angle = angle_inc * i; + out.points.emplace_back(coord_t(cos(angle) * radius), coord_t(sin(angle) * radius)); + } + return out; +} } \ No newline at end of file diff --git a/src/libslic3r/Polygon.hpp b/src/libslic3r/Polygon.hpp index 7da63c2cc..c91f8a2d5 100644 --- a/src/libslic3r/Polygon.hpp +++ b/src/libslic3r/Polygon.hpp @@ -267,6 +267,9 @@ inline Polygons to_polygons(std::vector &&paths) // however their contours may be rotated. bool polygons_match(const Polygon &l, const Polygon &r); +Polygon make_circle(double radius, double error); +Polygon make_circle_num_segments(double radius, size_t num_segments); + bool overlaps(const Polygons& polys1, const Polygons& polys2); } // Slic3r diff --git a/src/libslic3r/Polyline.hpp b/src/libslic3r/Polyline.hpp index a9d919a22..ccd89bb2f 100644 --- a/src/libslic3r/Polyline.hpp +++ b/src/libslic3r/Polyline.hpp @@ -101,7 +101,10 @@ public: } void append(const Polyline& src); void append(Polyline&& src); - + + Point& operator[](Points::size_type idx) { return this->points[idx]; } + const Point& operator[](Points::size_type idx) const { return this->points[idx]; } + const Point& last_point() const override { return this->points.back(); } const Point& leftmost_point() const; Lines lines() const override; diff --git a/src/libslic3r/PrintConfig.cpp b/src/libslic3r/PrintConfig.cpp index a53da6e36..834e1ce86 100644 --- a/src/libslic3r/PrintConfig.cpp +++ b/src/libslic3r/PrintConfig.cpp @@ -193,7 +193,8 @@ static t_config_enum_values s_keys_map_SupportMaterialStyle { { "snug", smsSnug }, { "tree_slim", smsTreeSlim }, { "tree_strong", smsTreeStrong }, - { "tree_hybrid", smsTreeHybrid } + { "tree_hybrid", smsTreeHybrid }, + { "tree_organic", smsTreeOrganic } }; CONFIG_OPTION_ENUM_DEFINE_STATIC_MAPS(SupportMaterialStyle) @@ -3146,12 +3147,14 @@ void PrintConfigDef::init_fff_params() def->enum_values.push_back("tree_slim"); def->enum_values.push_back("tree_strong"); def->enum_values.push_back("tree_hybrid"); + def->enum_values.push_back("tree_organic"); def->enum_labels.push_back(L("Default")); def->enum_labels.push_back(L("Grid")); def->enum_labels.push_back(L("Snug")); def->enum_labels.push_back(L("Tree Slim")); def->enum_labels.push_back(L("Tree Strong")); def->enum_labels.push_back(L("Tree Hybrid")); + def->enum_labels.push_back(L("Tree Organic")); def->mode = comAdvanced; def->set_default_value(new ConfigOptionEnum(smsDefault)); @@ -5134,8 +5137,7 @@ CLIActionsConfigDef::CLIActionsConfigDef() def = this->add("no_check", coBool); def->label = L("No check"); def->tooltip = L("Do not run any validity checks, such as gcode path conflicts check."); - def->cli_params = "option"; - def->set_default_value(new ConfigOptionBool(false)); + def->set_default_value(new ConfigOptionBool(false)); def = this->add("normative_check", coBool); def->label = L("Normative check"); diff --git a/src/libslic3r/PrintConfig.hpp b/src/libslic3r/PrintConfig.hpp index a97f2bbfb..db5481937 100644 --- a/src/libslic3r/PrintConfig.hpp +++ b/src/libslic3r/PrintConfig.hpp @@ -101,7 +101,7 @@ enum SupportMaterialPattern { }; enum SupportMaterialStyle { - smsDefault, smsGrid, smsSnug, smsTreeSlim, smsTreeStrong, smsTreeHybrid + smsDefault, smsGrid, smsSnug, smsTreeSlim, smsTreeStrong, smsTreeHybrid, smsTreeOrganic }; enum SupportMaterialInterfacePattern { diff --git a/src/libslic3r/PrintObject.cpp b/src/libslic3r/PrintObject.cpp index 1669f8237..b47eb6faa 100644 --- a/src/libslic3r/PrintObject.cpp +++ b/src/libslic3r/PrintObject.cpp @@ -40,7 +40,6 @@ using namespace std::literals; #endif // #define SLIC3R_DEBUG - // Make assert active if SLIC3R_DEBUG #ifdef SLIC3R_DEBUG #undef NDEBUG @@ -2463,11 +2462,15 @@ void PrintObject::combine_infill() void PrintObject::_generate_support_material() { - PrintObjectSupportMaterial support_material(this, m_slicing_params); - support_material.generate(*this); - - TreeSupport tree_support(*this, m_slicing_params); - tree_support.generate(); + if (is_tree(m_config.support_type.value)) { + TreeSupport tree_support(*this, m_slicing_params); + tree_support.throw_on_cancel = [this]() { this->throw_if_canceled(); }; + tree_support.generate(); + } + else { + PrintObjectSupportMaterial support_material(this, m_slicing_params); + support_material.generate(*this); + } } // BBS diff --git a/src/libslic3r/SLAPrintSteps.cpp b/src/libslic3r/SLAPrintSteps.cpp index ba6fb7032..4de75edc4 100644 --- a/src/libslic3r/SLAPrintSteps.cpp +++ b/src/libslic3r/SLAPrintSteps.cpp @@ -398,14 +398,14 @@ void SLAPrint::Steps::drill_holes(SLAPrintObject &po) auto bb = bounding_box(m); Eigen::AlignedBox ebb{bb.min.cast(), bb.max.cast()}; - - AABBTreeIndirect::traverse( - tree, - AABBTreeIndirect::intersecting(ebb), - [&part_to_drill, &hollowed_mesh](size_t faceid) - { - part_to_drill.indices.emplace_back(hollowed_mesh.its.indices[faceid]); - }); + //BBS + //AABBTreeIndirect::traverse( + // tree, + // AABBTreeIndirect::intersecting(ebb), + // [&part_to_drill, &hollowed_mesh](size_t faceid) + //{ + // part_to_drill.indices.emplace_back(hollowed_mesh.its.indices[faceid]); + //}); auto cgal_meshpart = MeshBoolean::cgal::triangle_mesh_to_cgal( remove_unconnected_vertices(part_to_drill)); diff --git a/src/libslic3r/SupportMaterial.cpp b/src/libslic3r/SupportMaterial.cpp index 793f25dbd..aa3dc2e9c 100644 --- a/src/libslic3r/SupportMaterial.cpp +++ b/src/libslic3r/SupportMaterial.cpp @@ -3,7 +3,6 @@ #include "Layer.hpp" #include "Print.hpp" #include "SupportMaterial.hpp" -#include "Fill/FillBase.hpp" #include "Geometry.hpp" #include "Point.hpp" #include "MutablePolygon.hpp" @@ -69,17 +68,17 @@ namespace Slic3r { static constexpr bool support_with_sheath = false; #ifdef SLIC3R_DEBUG -const char* support_surface_type_to_color_name(const PrintObjectSupportMaterial::SupporLayerType surface_type) +const char* support_surface_type_to_color_name(const SupporLayerType surface_type) { switch (surface_type) { - case PrintObjectSupportMaterial::sltTopContact: return "rgb(255,0,0)"; // "red"; - case PrintObjectSupportMaterial::sltTopInterface: return "rgb(0,255,0)"; // "green"; - case PrintObjectSupportMaterial::sltBase: return "rgb(0,0,255)"; // "blue"; - case PrintObjectSupportMaterial::sltBottomInterface:return "rgb(255,255,128)"; // yellow - case PrintObjectSupportMaterial::sltBottomContact: return "rgb(255,0,255)"; // magenta - case PrintObjectSupportMaterial::sltRaftInterface: return "rgb(0,255,255)"; - case PrintObjectSupportMaterial::sltRaftBase: return "rgb(128,128,128)"; - case PrintObjectSupportMaterial::sltUnknown: return "rgb(128,0,0)"; // maroon + case SupporLayerType::sltTopContact: return "rgb(255,0,0)"; // "red"; + case SupporLayerType::sltTopInterface: return "rgb(0,255,0)"; // "green"; + case SupporLayerType::sltBase: return "rgb(0,0,255)"; // "blue"; + case SupporLayerType::sltBottomInterface:return "rgb(255,255,128)"; // yellow + case SupporLayerType::sltBottomContact: return "rgb(255,0,255)"; // magenta + case SupporLayerType::sltRaftInterface: return "rgb(0,255,255)"; + case SupporLayerType::sltRaftBase: return "rgb(128,128,128)"; + case SupporLayerType::sltUnknown: return "rgb(128,0,0)"; // maroon default: return "rgb(64,64,64)"; }; } @@ -96,28 +95,28 @@ void export_support_surface_type_legend_to_svg(SVG &svg, const Point &pos) coord_t pos_x = pos_x0; coord_t pos_y = pos(1) + scale_(1.5); coord_t step_x = scale_(10.); - svg.draw_legend(Point(pos_x, pos_y), "top contact" , support_surface_type_to_color_name(PrintObjectSupportMaterial::sltTopContact)); + svg.draw_legend(Point(pos_x, pos_y), "top contact" , support_surface_type_to_color_name(SupporLayerType::sltTopContact)); pos_x += step_x; - svg.draw_legend(Point(pos_x, pos_y), "top iface" , support_surface_type_to_color_name(PrintObjectSupportMaterial::sltTopInterface)); + svg.draw_legend(Point(pos_x, pos_y), "top iface" , support_surface_type_to_color_name(SupporLayerType::sltTopInterface)); pos_x += step_x; - svg.draw_legend(Point(pos_x, pos_y), "base" , support_surface_type_to_color_name(PrintObjectSupportMaterial::sltBase)); + svg.draw_legend(Point(pos_x, pos_y), "base" , support_surface_type_to_color_name(SupporLayerType::sltBase)); pos_x += step_x; - svg.draw_legend(Point(pos_x, pos_y), "bottom iface" , support_surface_type_to_color_name(PrintObjectSupportMaterial::sltBottomInterface)); + svg.draw_legend(Point(pos_x, pos_y), "bottom iface" , support_surface_type_to_color_name(SupporLayerType::sltBottomInterface)); pos_x += step_x; - svg.draw_legend(Point(pos_x, pos_y), "bottom contact" , support_surface_type_to_color_name(PrintObjectSupportMaterial::sltBottomContact)); + svg.draw_legend(Point(pos_x, pos_y), "bottom contact" , support_surface_type_to_color_name(SupporLayerType::sltBottomContact)); // 2nd row pos_x = pos_x0; pos_y = pos(1)+scale_(2.8); - svg.draw_legend(Point(pos_x, pos_y), "raft interface" , support_surface_type_to_color_name(PrintObjectSupportMaterial::sltRaftInterface)); + svg.draw_legend(Point(pos_x, pos_y), "raft interface" , support_surface_type_to_color_name(SupporLayerType::sltRaftInterface)); pos_x += step_x; - svg.draw_legend(Point(pos_x, pos_y), "raft base" , support_surface_type_to_color_name(PrintObjectSupportMaterial::sltRaftBase)); + svg.draw_legend(Point(pos_x, pos_y), "raft base" , support_surface_type_to_color_name(SupporLayerType::sltRaftBase)); pos_x += step_x; - svg.draw_legend(Point(pos_x, pos_y), "unknown" , support_surface_type_to_color_name(PrintObjectSupportMaterial::sltUnknown)); + svg.draw_legend(Point(pos_x, pos_y), "unknown" , support_surface_type_to_color_name(SupporLayerType::sltUnknown)); pos_x += step_x; - svg.draw_legend(Point(pos_x, pos_y), "intermediate" , support_surface_type_to_color_name(PrintObjectSupportMaterial::sltIntermediate)); + svg.draw_legend(Point(pos_x, pos_y), "intermediate" , support_surface_type_to_color_name(SupporLayerType::sltIntermediate)); } -void export_print_z_polygons_to_svg(const char *path, PrintObjectSupportMaterial::MyLayer ** const layers, size_t n_layers) +void export_print_z_polygons_to_svg(const char *path, SupportGeneratorLayer ** const layers, size_t n_layers) { BoundingBox bbox; for (int i = 0; i < n_layers; ++ i) @@ -137,7 +136,7 @@ void export_print_z_polygons_to_svg(const char *path, PrintObjectSupportMaterial void export_print_z_polygons_and_extrusions_to_svg( const char *path, - PrintObjectSupportMaterial::MyLayer ** const layers, + SupportGeneratorLayer ** const layers, size_t n_layers, SupportLayer &support_layer) { @@ -344,118 +343,126 @@ static std::string get_svg_filename(std::string layer_nr_or_z, std::string tag return prefix + tag + "_" + layer_nr_or_z /*+ "_" + std::to_string(rand_num)*/ + suffix; } -PrintObjectSupportMaterial::PrintObjectSupportMaterial(const PrintObject *object, const SlicingParameters &slicing_params) : - m_object (object), - m_print_config (&object->print()->config()), - m_object_config (&object->config()), - m_slicing_params (slicing_params) +SupportParameters::SupportParameters(const PrintObject &object) { - m_support_params.first_layer_flow = support_material_1st_layer_flow(object, float(slicing_params.first_print_layer_height)); - m_support_params.support_material_flow = support_material_flow(object, float(slicing_params.layer_height)); - m_support_params.support_material_interface_flow = support_material_interface_flow(object, float(slicing_params.layer_height)); - m_support_params.support_layer_height_min = 0.01; + const PrintConfig &print_config = object.print()->config(); + const PrintObjectConfig &object_config = object.config(); + const SlicingParameters &slicing_params = object.slicing_parameters(); + + this->first_layer_flow = Slic3r::support_material_1st_layer_flow(&object, float(slicing_params.first_print_layer_height)); + this->support_material_flow = Slic3r::support_material_flow(&object, float(slicing_params.layer_height)); + this->support_material_interface_flow = Slic3r::support_material_interface_flow(&object, float(slicing_params.layer_height)); // Calculate a minimum support layer height as a minimum over all extruders, but not smaller than 10um. - m_support_params.support_layer_height_min = 1000000.; - for (auto lh : m_print_config->min_layer_height.values) - m_support_params.support_layer_height_min = std::min(m_support_params.support_layer_height_min, std::max(0.01, lh)); - for (auto layer : m_object->layers()) - m_support_params.support_layer_height_min = std::min(m_support_params.support_layer_height_min, std::max(0.01, layer->height)); + this->support_layer_height_min = scaled(0.01); + for (auto lh : print_config.min_layer_height.values) + this->support_layer_height_min = std::min(this->support_layer_height_min, std::max(0.01, lh)); + for (auto layer : object.layers()) + this->support_layer_height_min = std::min(this->support_layer_height_min, std::max(0.01, layer->height)); - if (m_object_config->support_interface_top_layers.value == 0) { + if (object_config.support_interface_top_layers.value == 0) { // No interface layers allowed, print everything with the base support pattern. - m_support_params.support_material_interface_flow = m_support_params.support_material_flow; + this->support_material_interface_flow = this->support_material_flow; } // Evaluate the XY gap between the object outer perimeters and the support structures. // Evaluate the XY gap between the object outer perimeters and the support structures. coordf_t external_perimeter_width = 0.; - coordf_t bridge_flow = 0; - for (size_t region_id = 0; region_id < object->num_printing_regions(); ++ region_id) { - const PrintRegion ®ion = object->printing_region(region_id); - external_perimeter_width = std::max(external_perimeter_width, coordf_t(region.flow(*object, frExternalPerimeter, slicing_params.layer_height).width())); - bridge_flow += region.config().bridge_flow; + coordf_t bridge_flow_ratio = 0; + for (size_t region_id = 0; region_id < object.num_printing_regions(); ++ region_id) { + const PrintRegion ®ion = object.printing_region(region_id); + external_perimeter_width = std::max(external_perimeter_width, coordf_t(region.flow(object, frExternalPerimeter, slicing_params.layer_height).width())); + bridge_flow_ratio += region.config().bridge_flow; } - m_support_params.gap_xy = m_object_config->support_object_xy_distance.value; - bridge_flow /= object->num_printing_regions(); + this->gap_xy = object_config.support_object_xy_distance.value; + bridge_flow_ratio /= object.num_printing_regions(); - m_support_params.support_material_bottom_interface_flow = m_slicing_params.soluble_interface || ! m_object_config->thick_bridges ? - m_support_params.support_material_interface_flow.with_flow_ratio(bridge_flow) : - Flow::bridging_flow(bridge_flow * m_support_params.support_material_interface_flow.nozzle_diameter(), m_support_params.support_material_interface_flow.nozzle_diameter()); + this->support_material_bottom_interface_flow = slicing_params.soluble_interface || ! object_config.thick_bridges ? + this->support_material_interface_flow.with_flow_ratio(bridge_flow_ratio) : + Flow::bridging_flow(bridge_flow_ratio * this->support_material_interface_flow.nozzle_diameter(), this->support_material_interface_flow.nozzle_diameter()); - m_support_params.can_merge_support_regions = m_object_config->support_filament.value == m_object_config->support_interface_filament.value; - if (!m_support_params.can_merge_support_regions && (m_object_config->support_filament.value == 0 || m_object_config->support_interface_filament.value == 0)) { + this->can_merge_support_regions = object_config.support_filament.value == object_config.support_interface_filament.value; + if (!this->can_merge_support_regions && (object_config.support_filament.value == 0 || object_config.support_interface_filament.value == 0)) { // One of the support extruders is of "don't care" type. - auto object_extruders = m_object->object_extruders(); + auto object_extruders = object.object_extruders(); if (object_extruders.size() == 1 && // object_extruders are 0-based but object_config.support_filament's are 1-based - object_extruders[0]+1 == std::max(m_object_config->support_filament.value, m_object_config->support_interface_filament.value)) + object_extruders[0]+1 == std::max(object_config.support_filament.value, object_config.support_interface_filament.value)) // Object is printed with the same extruder as the support. - m_support_params.can_merge_support_regions = true; + this->can_merge_support_regions = true; } - m_support_params.base_angle = Geometry::deg2rad(float(m_object_config->support_angle.value)); - m_support_params.interface_angle = Geometry::deg2rad(float(m_object_config->support_angle.value + 90.)); - m_support_params.interface_spacing = m_object_config->support_interface_spacing.value + m_support_params.support_material_interface_flow.spacing(); - m_support_params.interface_density = std::min(1., m_support_params.support_material_interface_flow.spacing() / m_support_params.interface_spacing); - m_support_params.support_spacing = m_object_config->support_base_pattern_spacing.value + m_support_params.support_material_flow.spacing(); - m_support_params.support_density = std::min(1., m_support_params.support_material_flow.spacing() / m_support_params.support_spacing); - if (m_object_config->support_interface_top_layers.value == 0) { + this->base_angle = Geometry::deg2rad(float(object_config.support_threshold_angle.value)); + this->interface_angle = Geometry::deg2rad(float(object_config.support_threshold_angle.value + 90.)); + this->interface_spacing = object_config.support_interface_spacing.value + this->support_material_interface_flow.spacing(); + this->interface_density = std::min(1., this->support_material_interface_flow.spacing() / this->interface_spacing); + this->support_spacing = object_config.support_base_pattern_spacing.value + this->support_material_flow.spacing(); + this->support_density = std::min(1., this->support_material_flow.spacing() / this->support_spacing); + if (object_config.support_interface_top_layers.value == 0) { // No interface layers allowed, print everything with the base support pattern. - m_support_params.interface_spacing = m_support_params.support_spacing; - m_support_params.interface_density = m_support_params.support_density; + this->interface_spacing = this->support_spacing; + this->interface_density = this->support_density; } - SupportMaterialPattern support_pattern = m_object_config->support_base_pattern; - m_support_params.with_sheath = support_with_sheath; - m_support_params.base_fill_pattern = + SupportMaterialPattern support_pattern = object_config.support_base_pattern; + this->with_sheath = object_config.tree_support_wall_count>0; + this->base_fill_pattern = support_pattern == smpHoneycomb ? ipHoneycomb : - m_support_params.support_density > 0.95 || m_support_params.with_sheath ? ipRectilinear : ipSupportBase; - m_support_params.interface_fill_pattern = (m_support_params.interface_density > 0.95 ? ipRectilinear : ipSupportBase); - if (m_object_config->support_interface_pattern == smipGrid) - m_support_params.contact_fill_pattern = ipGrid; - else if (m_object_config->support_interface_pattern == smipRectilinearInterlaced) - m_support_params.contact_fill_pattern = ipRectilinear; + this->support_density > 0.95 || this->with_sheath ? ipRectilinear : ipSupportBase; + this->interface_fill_pattern = (this->interface_density > 0.95 ? ipRectilinear : ipSupportBase); + if (object_config.support_interface_pattern == smipGrid) + this->contact_fill_pattern = ipGrid; + else if (object_config.support_interface_pattern == smipRectilinearInterlaced) + this->contact_fill_pattern = ipRectilinear; else - m_support_params.contact_fill_pattern = - (m_object_config->support_interface_pattern == smipAuto && m_slicing_params.soluble_interface) || - m_object_config->support_interface_pattern == smipConcentric ? + this->contact_fill_pattern = + (object_config.support_interface_pattern == smipAuto && slicing_params.soluble_interface) || + object_config.support_interface_pattern == smipConcentric ? ipConcentric : - (m_support_params.interface_density > 0.95 ? ipRectilinear : ipSupportBase); + (this->interface_density > 0.95 ? ipRectilinear : ipSupportBase); +} + +PrintObjectSupportMaterial::PrintObjectSupportMaterial(const PrintObject *object, const SlicingParameters &slicing_params) : + m_print_config (&object->print()->config()), + m_object_config (&object->config()), + m_slicing_params (slicing_params), + m_support_params (*object), + m_object (object) +{ } // Using the std::deque as an allocator. -inline PrintObjectSupportMaterial::MyLayer& layer_allocate( - std::deque &layer_storage, - PrintObjectSupportMaterial::SupporLayerType layer_type) +inline SupportGeneratorLayer& layer_allocate( + std::deque &layer_storage, + SupporLayerType layer_type) { - layer_storage.push_back(PrintObjectSupportMaterial::MyLayer()); + layer_storage.push_back(SupportGeneratorLayer()); layer_storage.back().layer_type = layer_type; return layer_storage.back(); } -inline PrintObjectSupportMaterial::MyLayer& layer_allocate( - std::deque &layer_storage, +inline SupportGeneratorLayer& layer_allocate( + std::deque &layer_storage, tbb::spin_mutex &layer_storage_mutex, - PrintObjectSupportMaterial::SupporLayerType layer_type) + SupporLayerType layer_type) { layer_storage_mutex.lock(); - layer_storage.push_back(PrintObjectSupportMaterial::MyLayer()); - PrintObjectSupportMaterial::MyLayer *layer_new = &layer_storage.back(); + layer_storage.push_back(SupportGeneratorLayer()); + SupportGeneratorLayer *layer_new = &layer_storage.back(); layer_storage_mutex.unlock(); layer_new->layer_type = layer_type; return *layer_new; } -inline void layers_append(PrintObjectSupportMaterial::MyLayersPtr &dst, const PrintObjectSupportMaterial::MyLayersPtr &src) +inline void layers_append(SupportGeneratorLayersPtr &dst, const SupportGeneratorLayersPtr &src) { dst.insert(dst.end(), src.begin(), src.end()); } // Support layer that is covered by some form of dense interface. -static constexpr const std::initializer_list support_types_interface { - PrintObjectSupportMaterial::sltRaftInterface, PrintObjectSupportMaterial::sltBottomContact, PrintObjectSupportMaterial::sltBottomInterface, PrintObjectSupportMaterial::sltTopContact, PrintObjectSupportMaterial::sltTopInterface +static constexpr const std::initializer_list support_types_interface { + SupporLayerType::sltRaftInterface, SupporLayerType::sltBottomContact, SupporLayerType::sltBottomInterface, SupporLayerType::sltTopContact, SupporLayerType::sltTopInterface }; void PrintObjectSupportMaterial::generate(PrintObject &object) @@ -468,7 +475,7 @@ void PrintObjectSupportMaterial::generate(PrintObject &object) // Layer instances will be allocated by std::deque and they will be kept until the end of this function call. // The layers will be referenced by various LayersPtr (of type std::vector) - MyLayerStorage layer_storage; + SupportGeneratorLayerStorage layer_storage; BOOST_LOG_TRIVIAL(info) << "Support generator - Creating top contacts"; @@ -481,7 +488,7 @@ void PrintObjectSupportMaterial::generate(PrintObject &object) // should the support material expose to the object in order to guarantee // that it will be effective, regardless of how it's built below. // If raft is to be generated, the 1st top_contact layer will contain the 1st object layer silhouette without holes. - MyLayersPtr top_contacts = this->top_contact_layers(object, buildplate_covered, layer_storage); + SupportGeneratorLayersPtr top_contacts = this->top_contact_layers(object, buildplate_covered, layer_storage); if (top_contacts.empty()) // Nothing is supported, no supports are generated. return; @@ -492,7 +499,7 @@ void PrintObjectSupportMaterial::generate(PrintObject &object) #ifdef SLIC3R_DEBUG static int iRun = 0; iRun ++; - for (const MyLayer *layer : top_contacts) + for (const SupportGeneratorLayer *layer : top_contacts) Slic3r::SVG::export_expolygons( debug_out_path("top-contacts-%d-%lf.svg", iRun, layer->print_z), union_ex(layer->polygons)); @@ -505,7 +512,7 @@ void PrintObjectSupportMaterial::generate(PrintObject &object) // layer_support_areas contains the per object layer support areas. These per object layer support areas // may get merged and trimmed by this->generate_base_layers() if the support layers are not synchronized with object layers. std::vector layer_support_areas; - MyLayersPtr bottom_contacts = this->bottom_contact_layers_and_layer_support_areas( + SupportGeneratorLayersPtr bottom_contacts = this->bottom_contact_layers_and_layer_support_areas( object, top_contacts, buildplate_covered, layer_storage, layer_support_areas); @@ -526,13 +533,13 @@ void PrintObjectSupportMaterial::generate(PrintObject &object) // The layers may or may not be synchronized with the object layers, depending on the configuration. // For example, a single nozzle multi material printing will need to generate a waste tower, which in turn // wastes less material, if there are as little tool changes as possible. - MyLayersPtr intermediate_layers = this->raft_and_intermediate_support_layers( + SupportGeneratorLayersPtr intermediate_layers = this->raft_and_intermediate_support_layers( object, bottom_contacts, top_contacts, layer_storage); this->trim_support_layers_by_object(object, top_contacts, m_slicing_params.gap_support_object, m_slicing_params.gap_object_support, m_support_params.gap_xy); #ifdef SLIC3R_DEBUG - for (const MyLayer *layer : top_contacts) + for (const SupportGeneratorLayer *layer : top_contacts) Slic3r::SVG::export_expolygons( debug_out_path("top-contacts-trimmed-by-object-%d-%lf.svg", iRun, layer->print_z), union_ex(layer->polygons)); @@ -544,7 +551,7 @@ void PrintObjectSupportMaterial::generate(PrintObject &object) this->generate_base_layers(object, bottom_contacts, top_contacts, intermediate_layers, layer_support_areas); #ifdef SLIC3R_DEBUG - for (MyLayersPtr::const_iterator it = intermediate_layers.begin(); it != intermediate_layers.end(); ++ it) + for (SupportGeneratorLayersPtr::const_iterator it = intermediate_layers.begin(); it != intermediate_layers.end(); ++ it) Slic3r::SVG::export_expolygons( debug_out_path("support-base-layers-%d-%lf.svg", iRun, (*it)->print_z), union_ex((*it)->polygons)); @@ -570,17 +577,17 @@ void PrintObjectSupportMaterial::generate(PrintObject &object) // If raft is to be generated, the 1st top_contact layer will contain the 1st object layer silhouette with holes filled. // There is also a 1st intermediate layer containing bases of support columns. // Inflate the bases of the support columns and create the raft base under the object. - MyLayersPtr raft_layers = this->generate_raft_base(object, top_contacts, interface_layers, base_interface_layers, intermediate_layers, layer_storage); + SupportGeneratorLayersPtr raft_layers = generate_raft_base(object, m_support_params, m_slicing_params, top_contacts, interface_layers, base_interface_layers, intermediate_layers, layer_storage); if (object.print()->canceled()) return; #ifdef SLIC3R_DEBUG - for (const MyLayer *l : interface_layers) + for (const SupportGeneratorLayer *l : interface_layers) Slic3r::SVG::export_expolygons( debug_out_path("interface-layers-%d-%lf.svg", iRun, l->print_z), union_ex(l->polygons)); - for (const MyLayer *l : base_interface_layers) + for (const SupportGeneratorLayer *l : base_interface_layers) Slic3r::SVG::export_expolygons( debug_out_path("base-interface-layers-%d-%lf.svg", iRun, l->print_z), union_ex(l->polygons)); @@ -603,94 +610,10 @@ void PrintObjectSupportMaterial::generate(PrintObject &object) // intermediate_layers.clear(); // interface_layers.clear(); - // Install support layers into the object. - // A support layer installed on a PrintObject has a unique print_z. - MyLayersPtr layers_sorted; - layers_sorted.reserve(raft_layers.size() + bottom_contacts.size() + top_contacts.size() + intermediate_layers.size() + interface_layers.size() + base_interface_layers.size()); - layers_append(layers_sorted, raft_layers); - layers_append(layers_sorted, bottom_contacts); - layers_append(layers_sorted, top_contacts); - layers_append(layers_sorted, intermediate_layers); - layers_append(layers_sorted, interface_layers); - layers_append(layers_sorted, base_interface_layers); - // Sort the layers lexicographically by a raising print_z and a decreasing height. - std::sort(layers_sorted.begin(), layers_sorted.end(), [](auto *l1, auto *l2) { return *l1 < *l2; }); - - // BBS: MusangKing - erase mini layer heights (< 0.08mm) arised by top/bottom_z_distance & top_contacts under variable layer height - if (this->synchronize_layers() && !object.slicing_parameters().soluble_interface) { - auto thres = m_support_params.support_layer_height_min - EPSILON; - for (size_t i = 1; i < layers_sorted.size() - 1; ++i) { - auto& lowr = layers_sorted[i - 1]; - auto& curr = layers_sorted[i]; - auto& higr = layers_sorted[i + 1]; - // "Rounding" suspicious top/bottom contacts - if (curr->layer_type == sltTopContact || curr->layer_type == sltBottomContact) { - // Check adjacent-layer print_z diffs - coordf_t height_low = curr->print_z - lowr->print_z; - coordf_t height_high = higr->print_z - curr->print_z; - if (height_low < thres || height_high < thres) { - // Mark to-be-deleted layer as Unknown type - curr->layer_type = sltUnknown; - } - } - } - // Retains the order - layers_sorted.erase(std::remove_if(layers_sorted.begin(), layers_sorted.end(), [](MyLayer* l) {return l->layer_type == sltUnknown; }), layers_sorted.end()); - } - - int layer_id = 0; - int layer_id_interface = 0; - assert(object.support_layers().empty()); - for (size_t i = 0; i < layers_sorted.size();) { - // Find the last layer with roughly the same print_z, find the minimum layer height of all. - // Due to the floating point inaccuracies, the print_z may not be the same even if in theory they should. - size_t j = i + 1; - coordf_t zmax = layers_sorted[i]->print_z + EPSILON; - for (; j < layers_sorted.size() && layers_sorted[j]->print_z <= zmax; ++j) ; - // Assign an average print_z to the set of layers with nearly equal print_z. - coordf_t zavg = 0.5 * (layers_sorted[i]->print_z + layers_sorted[j - 1]->print_z); - coordf_t height_min = layers_sorted[i]->height; - bool empty = true; - // For snug supports, layers where the direction of the support interface shall change are accounted for. - size_t num_interfaces = 0; - size_t num_top_contacts = 0; - double top_contact_bottom_z = 0; - for (size_t u = i; u < j; ++u) { - MyLayer &layer = *layers_sorted[u]; - if (! layer.polygons.empty()) { - empty = false; - num_interfaces += one_of(layer.layer_type, support_types_interface); - if (layer.layer_type == sltTopContact) { - ++ num_top_contacts; - assert(num_top_contacts <= 1); - // All top contact layers sharing this print_z shall also share bottom_z. - //assert(num_top_contacts == 1 || (top_contact_bottom_z - layer.bottom_z) < EPSILON); - top_contact_bottom_z = layer.bottom_z; - } - } - layer.print_z = zavg; - height_min = std::min(height_min, layer.height); - } - if (! empty) { - // Here the upper_layer and lower_layer pointers are left to null at the support layers, - // as they are never used. These pointers are candidates for removal. - bool this_layer_contacts_only = num_top_contacts > 0 && num_top_contacts == num_interfaces; - size_t this_layer_id_interface = layer_id_interface; - if (this_layer_contacts_only) { - // Find a supporting layer for its interface ID. - for (auto it = object.support_layers().rbegin(); it != object.support_layers().rend(); ++ it) - if (const SupportLayer &other_layer = **it; std::abs(other_layer.print_z - top_contact_bottom_z) < EPSILON) { - // other_layer supports this top contact layer. Assign a different support interface direction to this layer - // from the layer that supports it. - this_layer_id_interface = other_layer.interface_id() + 1; - } - } - object.add_support_layer(layer_id ++, this_layer_id_interface, height_min, zavg); - if (num_interfaces && ! this_layer_contacts_only) - ++ layer_id_interface; - } - i = j; - } +#ifdef SLIC3R_DEBUG + SupportGeneratorLayersPtr layers_sorted = +#endif // SLIC3R_DEBUG + generate_support_layers(object, raft_layers, bottom_contacts, top_contacts, intermediate_layers, interface_layers, base_interface_layers); BOOST_LOG_TRIVIAL(info) << "Support generator - Generating tool paths"; @@ -702,7 +625,7 @@ void PrintObjectSupportMaterial::generate(PrintObject &object) // Due to the floating point inaccuracies, the print_z may not be the same even if in theory they should. int j = i + 1; coordf_t zmax = layers_sorted[i]->print_z + EPSILON; - bool empty = true; + bool empty = layers_sorted[i]->polygons.empty(); for (; j < layers_sorted.size() && layers_sorted[j]->print_z <= zmax; ++j) if (!layers_sorted[j]->polygons.empty()) empty = false; @@ -734,7 +657,7 @@ void PrintObjectSupportMaterial::generate(PrintObject &object) #endif /* SLIC3R_DEBUG */ // Generate the actual toolpaths and save them into each layer. - this->generate_toolpaths(object.support_layers(), raft_layers, bottom_contacts, top_contacts, intermediate_layers, interface_layers, base_interface_layers); + generate_support_toolpaths(object, object.support_layers(), *m_object_config, m_support_params, m_slicing_params, raft_layers, bottom_contacts, top_contacts, intermediate_layers, interface_layers, base_interface_layers); #ifdef SLIC3R_DEBUG { @@ -744,7 +667,7 @@ void PrintObjectSupportMaterial::generate(PrintObject &object) // Due to the floating point inaccuracies, the print_z may not be the same even if in theory they should. int j = i + 1; coordf_t zmax = layers_sorted[i]->print_z + EPSILON; - bool empty = true; + bool empty = layers_sorted[i]->polygons.empty(); for (; j < layers_sorted.size() && layers_sorted[j]->print_z <= zmax; ++j) if (! layers_sorted[j]->polygons.empty()) empty = false; @@ -831,7 +754,7 @@ public: m_extrusion_width(params.extrusion_width), m_support_material_closing_radius(params.support_closing_radius) { - if (m_style != smsSnug) m_style = smsGrid; + if (m_style == smsDefault) m_style = smsGrid; switch (m_style) { case smsGrid: { @@ -923,6 +846,12 @@ public: ) { switch (m_style) { + case smsTreeSlim: + case smsTreeStrong: + case smsTreeHybrid: + case smsTreeOrganic: + assert(false); + [[fallthrough]]; case smsGrid: { #ifdef SUPPORT_USE_AGG_RASTERIZER @@ -1841,17 +1770,17 @@ static inline std::tuple detect_contacts( // Allocate one, possibly two support contact layers. // For "thick" overhangs, one support layer will be generated to support normal extrusions, the other to support the "thick" extrusions. -static inline std::pair new_contact_layer( +static inline std::pair new_contact_layer( const PrintConfig &print_config, const PrintObjectConfig &object_config, const SlicingParameters &slicing_params, const coordf_t support_layer_height_min, const Layer &layer, - std::deque &layer_storage, + std::deque &layer_storage, tbb::spin_mutex &layer_storage_mutex) { double print_z, bottom_z, height; - PrintObjectSupportMaterial::MyLayer* bridging_layer = nullptr; + SupportGeneratorLayer* bridging_layer = nullptr; assert(layer.id() >= slicing_params.raft_layers()); size_t layer_id = layer.id() - slicing_params.raft_layers(); @@ -1877,7 +1806,7 @@ static inline std::pair(nullptr, nullptr); + return std::pair(nullptr, nullptr); } const bool has_raft = slicing_params.raft_layers() > 1; const coordf_t min_print_z = has_raft ? slicing_params.raft_contact_top_z : slicing_params.first_print_layer_height; @@ -1911,7 +1840,7 @@ static inline std::pairidx_object_layer_above = layer_id; bridging_layer->print_z = bridging_print_z; if (bridging_print_z == slicing_params.first_print_layer_height) { @@ -1929,7 +1858,7 @@ static inline std::pairprint_z < l2->print_z; }); + std::sort(layers.begin(), layers.end(), [](const SupportGeneratorLayer *l1, const SupportGeneratorLayer *l2) { return l1->print_z < l2->print_z; }); int i = 0; int k = 0; @@ -2103,7 +2032,7 @@ static void merge_contact_layers(const SlicingParameters &slicing_params, double for (; j < (int)layers.size() && layers[j]->print_z < slicing_params.first_print_layer_height + support_layer_height_min - EPSILON; ++ j); if (j > 0) { // Merge the layers layers (0) to (j - 1) into the layers[0]. - PrintObjectSupportMaterial::MyLayer &dst = *layers.front(); + SupportGeneratorLayer &dst = *layers.front(); for (int u = 1; u < j; ++ u) dst.merge(std::move(*layers[u])); // Snap the first layer to the 1st layer height. @@ -2121,7 +2050,7 @@ static void merge_contact_layers(const SlicingParameters &slicing_params, double for (; j < (int)layers.size() && layers[j]->print_z < zmax; ++ j) ; if (i + 1 < j) { // Merge the layers layers (i + 1) to (j - 1) into the layers[i]. - PrintObjectSupportMaterial::MyLayer &dst = *layers[i]; + SupportGeneratorLayer &dst = *layers[i]; for (int u = i + 1; u < j; ++ u) dst.merge(std::move(*layers[u])); } @@ -2205,8 +2134,8 @@ static OverhangCluster* add_overhang(std::vector& clusters, ExP // Generate top contact layers supporting overhangs. // For a soluble interface material synchronize the layer heights with the object, otherwise leave the layer height undefined. // If supports over bed surface only are requested, don't generate contact layers over an object. -PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::top_contact_layers( - const PrintObject &object, const std::vector &buildplate_covered, MyLayerStorage &layer_storage) const +SupportGeneratorLayersPtr PrintObjectSupportMaterial::top_contact_layers( + const PrintObject &object, const std::vector &buildplate_covered, SupportGeneratorLayerStorage &layer_storage) const { #ifdef SLIC3R_DEBUG static int iRun = 0; @@ -2217,14 +2146,14 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::top_contact_ // BBS: tree support is selected so normal supports need not be generated. // Note we still need to go through the following steps if support is disabled but raft is enabled. if (m_object_config->enable_support.value && (m_object_config->support_type.value != stNormalAuto && m_object_config->support_type.value != stNormal)) { - return MyLayersPtr(); + return SupportGeneratorLayersPtr(); } // Slice support enforcers / support blockers. SupportAnnotations annotations(object, buildplate_covered); // Output layers, sorted by top Z. - MyLayersPtr contact_out; + SupportGeneratorLayersPtr contact_out; BOOST_LOG_TRIVIAL(debug) << "PrintObjectSupportMaterial::top_contact_layers() in parallel - start"; // Determine top contact areas. @@ -2260,7 +2189,7 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::top_contact_ ); // end tbb::parallel_for if (object.print()->canceled()) - return MyLayersPtr(); + return SupportGeneratorLayersPtr(); // check if the sharp tails should be extended higher bool detect_first_sharp_tail_only = false; @@ -2349,7 +2278,7 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::top_contact_ } if (object.print()->canceled()) - return MyLayersPtr(); + return SupportGeneratorLayersPtr(); // BBS group overhang clusters const bool config_remove_small_overhangs = m_object_config->support_remove_small_overhang.value; @@ -2423,7 +2352,7 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::top_contact_ } if (object.print()->canceled()) - return MyLayersPtr(); + return SupportGeneratorLayersPtr(); for (size_t layer_id = layer_id_start; layer_id < num_layers; layer_id++) { const Layer& layer = *object.layers()[layer_id]; @@ -2480,17 +2409,17 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::top_contact_ } // Find the bottom contact layers above the top surfaces of this layer. -static inline PrintObjectSupportMaterial::MyLayer* detect_bottom_contacts( +static inline SupportGeneratorLayer* detect_bottom_contacts( const SlicingParameters &slicing_params, - const PrintObjectSupportMaterial::SupportParams &support_params, + const SupportParameters &support_params, const PrintObject &object, const Layer &layer, // Existing top contact layers, to which this newly created bottom contact layer will be snapped to guarantee a minimum layer height. - const PrintObjectSupportMaterial::MyLayersPtr &top_contacts, + const SupportGeneratorLayersPtr &top_contacts, // First top contact layer index overlapping with this new bottom interface layer. size_t contact_idx, // To allocate a new layer from. - std::deque &layer_storage, + std::deque &layer_storage, // To trim the support areas above this bottom interface layer with this newly created bottom interface layer. std::vector &layer_support_areas, // Support areas projected from top to bottom, starting with top support interfaces. @@ -2525,7 +2454,7 @@ static inline PrintObjectSupportMaterial::MyLayer* detect_bottom_contacts( size_t layer_id = layer.id() - slicing_params.raft_layers(); // Allocate a new bottom contact layer. - PrintObjectSupportMaterial::MyLayer &layer_new = layer_allocate(layer_storage, PrintObjectSupportMaterial::sltBottomContact); + SupportGeneratorLayer &layer_new = layer_allocate(layer_storage, SupporLayerType::sltBottomContact); // Grow top surfaces so that interface and support generation are generated // with some spacing from object - it looks we don't need the actual // top shapes so this can be done here @@ -2693,12 +2622,12 @@ static inline std::pair project_support_to_grid(const Layer // Generate bottom contact layers supporting the top contact layers. // For a soluble interface material synchronize the layer heights with the object, // otherwise set the layer height to a bridging flow of a support interface nozzle. -PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::bottom_contact_layers_and_layer_support_areas( - const PrintObject &object, const MyLayersPtr &top_contacts, std::vector &buildplate_covered, - MyLayerStorage &layer_storage, std::vector &layer_support_areas) const +SupportGeneratorLayersPtr PrintObjectSupportMaterial::bottom_contact_layers_and_layer_support_areas( + const PrintObject &object, const SupportGeneratorLayersPtr &top_contacts, std::vector &buildplate_covered, + SupportGeneratorLayerStorage &layer_storage, std::vector &layer_support_areas) const { if (top_contacts.empty()) - return MyLayersPtr(); + return SupportGeneratorLayersPtr(); #ifdef SLIC3R_DEBUG static size_t s_iRun = 0; @@ -2715,7 +2644,7 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::bottom_conta // find object top surfaces // we'll use them to clip our support and detect where does it stick - MyLayersPtr bottom_contacts; + SupportGeneratorLayersPtr bottom_contacts; // There is some support to be built, if there are non-empty top surfaces detected. // Sum of unsupported contact areas above the current layer.print_z. @@ -2734,7 +2663,7 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::bottom_conta Polygons enforcers_new; #endif // SLIC3R_DEBUG for (; contact_idx >= 0 && top_contacts[contact_idx]->print_z > layer.print_z - EPSILON; -- contact_idx) { - MyLayer &top_contact = *top_contacts[contact_idx]; + SupportGeneratorLayer &top_contact = *top_contacts[contact_idx]; #ifndef SLIC3R_DEBUG Polygons polygons_new; Polygons enforcers_new; @@ -2776,7 +2705,7 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::bottom_conta #endif // SLIC3R_DEBUG ] { // Find the bottom contact layers above the top surfaces of this layer. - MyLayer *layer_new = detect_bottom_contacts( + SupportGeneratorLayer *layer_new = detect_bottom_contacts( m_slicing_params, m_support_params, object, layer, top_contacts, contact_idx, layer_storage, layer_support_areas, overhangs_for_bottom_contacts #ifdef SLIC3R_DEBUG , iRun, polygons_new @@ -2880,19 +2809,19 @@ int idx_lower_or_equal(const std::vector &vec, int idx, FN_LOWER_EQUAL fn_lo // Trim the top_contacts layers with the bottom_contacts layers if they overlap, so there would not be enough vertical space for both of them. void PrintObjectSupportMaterial::trim_top_contacts_by_bottom_contacts( - const PrintObject &object, const MyLayersPtr &bottom_contacts, MyLayersPtr &top_contacts) const + const PrintObject &object, const SupportGeneratorLayersPtr &bottom_contacts, SupportGeneratorLayersPtr &top_contacts) const { tbb::parallel_for(tbb::blocked_range(0, int(top_contacts.size())), [&bottom_contacts, &top_contacts](const tbb::blocked_range& range) { int idx_bottom_overlapping_first = -2; // For all top contact layers, counting downwards due to the way idx_higher_or_equal caches the last index to avoid repeated binary search. for (int idx_top = range.end() - 1; idx_top >= range.begin(); -- idx_top) { - MyLayer &layer_top = *top_contacts[idx_top]; + SupportGeneratorLayer &layer_top = *top_contacts[idx_top]; // Find the first bottom layer overlapping with layer_top. - idx_bottom_overlapping_first = idx_lower_or_equal(bottom_contacts, idx_bottom_overlapping_first, [&layer_top](const MyLayer *layer_bottom){ return layer_bottom->bottom_print_z() - EPSILON <= layer_top.bottom_z; }); + idx_bottom_overlapping_first = idx_lower_or_equal(bottom_contacts, idx_bottom_overlapping_first, [&layer_top](const SupportGeneratorLayer *layer_bottom){ return layer_bottom->bottom_print_z() - EPSILON <= layer_top.bottom_z; }); // For all top contact layers overlapping with the thick bottom contact layer: for (int idx_bottom_overlapping = idx_bottom_overlapping_first; idx_bottom_overlapping >= 0; -- idx_bottom_overlapping) { - const MyLayer &layer_bottom = *bottom_contacts[idx_bottom_overlapping]; + const SupportGeneratorLayer &layer_bottom = *bottom_contacts[idx_bottom_overlapping]; assert(layer_bottom.bottom_print_z() - EPSILON <= layer_top.bottom_z); if (layer_top.print_z < layer_bottom.print_z + EPSILON) { // Layers overlap. Trim layer_top with layer_bottom. @@ -2904,16 +2833,16 @@ void PrintObjectSupportMaterial::trim_top_contacts_by_bottom_contacts( }); } -PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::raft_and_intermediate_support_layers( +SupportGeneratorLayersPtr PrintObjectSupportMaterial::raft_and_intermediate_support_layers( const PrintObject &object, - const MyLayersPtr &bottom_contacts, - const MyLayersPtr &top_contacts, - MyLayerStorage &layer_storage) const + const SupportGeneratorLayersPtr &bottom_contacts, + const SupportGeneratorLayersPtr &top_contacts, + SupportGeneratorLayerStorage &layer_storage) const { - MyLayersPtr intermediate_layers; + SupportGeneratorLayersPtr intermediate_layers; // Collect and sort the extremes (bottoms of the top contacts and tops of the bottom contacts). - MyLayersPtr extremes; + SupportGeneratorLayersPtr extremes; extremes.reserve(top_contacts.size() + bottom_contacts.size()); for (size_t i = 0; i < top_contacts.size(); ++ i) // Bottoms of the top contact layers. In case of non-soluble supports, @@ -2925,11 +2854,11 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::raft_and_int if (extremes.empty()) return intermediate_layers; - auto layer_extreme_lower = [](const MyLayer *l1, const MyLayer *l2) { + auto layer_extreme_lower = [](const SupportGeneratorLayer *l1, const SupportGeneratorLayer *l2) { coordf_t z1 = l1->extreme_z(); coordf_t z2 = l2->extreme_z(); // If the layers are aligned, return the top contact surface first. - return z1 < z2 || (z1 == z2 && l1->layer_type == PrintObjectSupportMaterial::sltTopContact && l2->layer_type == PrintObjectSupportMaterial::sltBottomContact); + return z1 < z2 || (z1 == z2 && l1->layer_type == SupporLayerType::sltTopContact && l2->layer_type == SupporLayerType::sltBottomContact); }; std::sort(extremes.begin(), extremes.end(), layer_extreme_lower); @@ -2965,7 +2894,7 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::raft_and_int ++ idx_extreme_first; } for (size_t idx_extreme = idx_extreme_first; idx_extreme < extremes.size(); ++ idx_extreme) { - MyLayer *extr2 = extremes[idx_extreme]; + SupportGeneratorLayer *extr2 = extremes[idx_extreme]; coordf_t extr2z = extr2->extreme_z(); if (std::abs(extr2z - m_slicing_params.first_print_layer_height) < EPSILON) { // This is a bottom of a synchronized (or soluble) top contact layer, its height has been decided in this->top_contact_layers(). @@ -2973,7 +2902,7 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::raft_and_int assert(std::abs(extr2->bottom_z - m_slicing_params.first_print_layer_height) < EPSILON); assert(extr2->print_z >= m_slicing_params.first_print_layer_height + m_support_params.support_layer_height_min - EPSILON); if (intermediate_layers.empty() || intermediate_layers.back()->print_z < m_slicing_params.first_print_layer_height) { - MyLayer &layer_new = layer_allocate(layer_storage, sltIntermediate); + SupportGeneratorLayer &layer_new = layer_allocate(layer_storage, sltIntermediate); layer_new.bottom_z = 0.; layer_new.print_z = m_slicing_params.first_print_layer_height; layer_new.height = m_slicing_params.first_print_layer_height; @@ -2983,7 +2912,7 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::raft_and_int } assert(extr2z >= m_slicing_params.raft_interface_top_z + EPSILON); assert(extr2z >= m_slicing_params.first_print_layer_height + EPSILON); - MyLayer *extr1 = (idx_extreme == idx_extreme_first) ? nullptr : extremes[idx_extreme - 1]; + SupportGeneratorLayer *extr1 = (idx_extreme == idx_extreme_first) ? nullptr : extremes[idx_extreme - 1]; // Fuse a support layer firmly to the raft top interface (not to the raft contacts). coordf_t extr1z = (extr1 == nullptr) ? m_slicing_params.raft_interface_top_z : extr1->extreme_z(); assert(extr2z >= extr1z); @@ -2995,7 +2924,7 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::raft_and_int // At this point only layers above first_print_layer_heigth + EPSILON are expected as the other cases were captured earlier. assert(extr2z >= m_slicing_params.first_print_layer_height + EPSILON); // Generate a new intermediate layer. - MyLayer &layer_new = layer_allocate(layer_storage, sltIntermediate); + SupportGeneratorLayer &layer_new = layer_allocate(layer_storage, sltIntermediate); layer_new.bottom_z = 0.; layer_new.print_z = extr1z = m_slicing_params.first_print_layer_height; layer_new.height = extr1z; @@ -3015,7 +2944,7 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::raft_and_int ++ idx_layer_object; if (idx_layer_object == 0 && extr1z == m_slicing_params.raft_interface_top_z) { // Insert one base support layer below the object. - MyLayer &layer_new = layer_allocate(layer_storage, sltIntermediate); + SupportGeneratorLayer &layer_new = layer_allocate(layer_storage, sltIntermediate); layer_new.print_z = m_slicing_params.object_print_z_min; layer_new.bottom_z = m_slicing_params.raft_interface_top_z; layer_new.height = layer_new.print_z - layer_new.bottom_z; @@ -3023,7 +2952,7 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::raft_and_int } // Emit all intermediate support layers synchronized with object layers up to extr2z. for (; idx_layer_object < object.layers().size() && object.layers()[idx_layer_object]->print_z < extr2z + EPSILON; ++ idx_layer_object) { - MyLayer &layer_new = layer_allocate(layer_storage, sltIntermediate); + SupportGeneratorLayer &layer_new = layer_allocate(layer_storage, sltIntermediate); layer_new.print_z = object.layers()[idx_layer_object]->print_z; layer_new.height = object.layers()[idx_layer_object]->height; layer_new.bottom_z = (idx_layer_object > 0) ? object.layers()[idx_layer_object - 1]->print_z : (layer_new.print_z - layer_new.height); @@ -3041,7 +2970,7 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::raft_and_int // between the 1st intermediate layer print_z and extr1->print_z is not too small. assert(extr1->bottom_z + m_support_params.support_layer_height_min < extr1->print_z + EPSILON); // Generate the first intermediate layer. - MyLayer &layer_new = layer_allocate(layer_storage, sltIntermediate); + SupportGeneratorLayer &layer_new = layer_allocate(layer_storage, sltIntermediate); layer_new.bottom_z = extr1->bottom_z; layer_new.print_z = extr1z = extr1->print_z; layer_new.height = extr1->height; @@ -3065,7 +2994,7 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::raft_and_int coordf_t extr2z_large_steps = extr2z; // Take the largest allowed step in the Z axis until extr2z_large_steps is reached. for (size_t i = 0; i < n_layers_extra; ++ i) { - MyLayer &layer_new = layer_allocate(layer_storage, sltIntermediate); + SupportGeneratorLayer &layer_new = layer_allocate(layer_storage, sltIntermediate); if (i + 1 == n_layers_extra) { // Last intermediate layer added. Align the last entered layer with extr2z_large_steps exactly. layer_new.bottom_z = (i == 0) ? extr1z : intermediate_layers.back()->print_z; @@ -3126,9 +3055,9 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::raft_and_int // Also the bottom/top_contacts shall have a layer thickness assigned already. void PrintObjectSupportMaterial::generate_base_layers( const PrintObject &object, - const MyLayersPtr &bottom_contacts, - const MyLayersPtr &top_contacts, - MyLayersPtr &intermediate_layers, + const SupportGeneratorLayersPtr &bottom_contacts, + const SupportGeneratorLayersPtr &top_contacts, + SupportGeneratorLayersPtr &intermediate_layers, const std::vector &layer_support_areas) const { #ifdef SLIC3R_DEBUG @@ -3152,7 +3081,7 @@ void PrintObjectSupportMaterial::generate_base_layers( { BOOST_LOG_TRIVIAL(trace) << "Support generator - generate_base_layers - creating layer " << idx_intermediate << " of " << intermediate_layers.size(); - MyLayer &layer_intermediate = *intermediate_layers[idx_intermediate]; + SupportGeneratorLayer &layer_intermediate = *intermediate_layers[idx_intermediate]; // Layers must be sorted by print_z. assert(idx_intermediate == 0 || layer_intermediate.print_z >= intermediate_layers[idx_intermediate - 1]->print_z); @@ -3175,10 +3104,10 @@ void PrintObjectSupportMaterial::generate_base_layers( // 4) base.print_z > top.bottom_z && base.bottom_z < top.bottom_z -> Base overlaps with top.bottom_z. This must not happen. // 5) base.print_z <= top.print_z && base.bottom_z >= top.bottom_z -> Base is fully inside top. Trim base by top. idx_top_contact_above = idx_lower_or_equal(top_contacts, idx_top_contact_above, - [&layer_intermediate](const MyLayer *layer){ return layer->bottom_z <= layer_intermediate.print_z - EPSILON; }); + [&layer_intermediate](const SupportGeneratorLayer *layer){ return layer->bottom_z <= layer_intermediate.print_z - EPSILON; }); // Collect all the top_contact layer intersecting with this layer. for (int idx_top_contact_overlapping = idx_top_contact_above; idx_top_contact_overlapping >= 0; -- idx_top_contact_overlapping) { - MyLayer &layer_top_overlapping = *top_contacts[idx_top_contact_overlapping]; + SupportGeneratorLayer &layer_top_overlapping = *top_contacts[idx_top_contact_overlapping]; if (layer_top_overlapping.print_z < layer_intermediate.bottom_z + EPSILON) break; // Base must not overlap with top.bottom_z. @@ -3195,7 +3124,7 @@ void PrintObjectSupportMaterial::generate_base_layers( polygons_new = layer_support_areas.front(); double first_layer_z = object.layers().front()->print_z; for (int i = idx_top_contact_above + 1; i < int(top_contacts.size()); ++ i) { - MyLayer &contacts = *top_contacts[i]; + SupportGeneratorLayer &contacts = *top_contacts[i]; if (contacts.print_z > first_layer_z + EPSILON) break; assert(contacts.bottom_z > layer_intermediate.print_z - EPSILON); @@ -3212,10 +3141,10 @@ void PrintObjectSupportMaterial::generate_base_layers( // 4) base.print_z > bottom.print_z && base.bottom_z >= bottom.print_z -> Base overlaps with bottom.print_z. This must not happen. // 5) base.print_z <= bottom.print_z && base.bottom_z >= bottom.bottom_z -> Base is fully inside top. Trim base by top. idx_bottom_contact_overlapping = idx_lower_or_equal(bottom_contacts, idx_bottom_contact_overlapping, - [&layer_intermediate](const MyLayer *layer){ return layer->bottom_print_z() <= layer_intermediate.print_z - EPSILON; }); + [&layer_intermediate](const SupportGeneratorLayer *layer){ return layer->bottom_print_z() <= layer_intermediate.print_z - EPSILON; }); // Collect all the bottom_contacts layer intersecting with this layer. for (int i = idx_bottom_contact_overlapping; i >= 0; -- i) { - MyLayer &layer_bottom_overlapping = *bottom_contacts[i]; + SupportGeneratorLayer &layer_bottom_overlapping = *bottom_contacts[i]; if (layer_bottom_overlapping.print_z < layer_intermediate.bottom_print_z() + EPSILON) break; // Base must not overlap with bottom.top_z. @@ -3267,7 +3196,7 @@ void PrintObjectSupportMaterial::generate_base_layers( BOOST_LOG_TRIVIAL(debug) << "PrintObjectSupportMaterial::generate_base_layers() in parallel - end"; #ifdef SLIC3R_DEBUG - for (MyLayersPtr::const_iterator it = intermediate_layers.begin(); it != intermediate_layers.end(); ++it) + for (SupportGeneratorLayersPtr::const_iterator it = intermediate_layers.begin(); it != intermediate_layers.end(); ++it) ::Slic3r::SVG::export_expolygons( debug_out_path("support-intermediate-layers-untrimmed-%d-%lf.svg", iRun, (*it)->print_z), union_ex((*it)->polygons)); @@ -3279,7 +3208,7 @@ void PrintObjectSupportMaterial::generate_base_layers( void PrintObjectSupportMaterial::trim_support_layers_by_object( const PrintObject &object, - MyLayersPtr &support_layers, + SupportGeneratorLayersPtr &support_layers, const coordf_t gap_extra_above, const coordf_t gap_extra_below, const coordf_t gap_xy) const @@ -3288,10 +3217,10 @@ void PrintObjectSupportMaterial::trim_support_layers_by_object( // Collect non-empty layers to be processed in parallel. // This is a good idea as pulling a thread from a thread pool for an empty task is expensive. - MyLayersPtr nonempty_layers; + SupportGeneratorLayersPtr nonempty_layers; nonempty_layers.reserve(support_layers.size()); for (size_t idx_layer = 0; idx_layer < support_layers.size(); ++ idx_layer) { - MyLayer *support_layer = support_layers[idx_layer]; + SupportGeneratorLayer *support_layer = support_layers[idx_layer]; if (! support_layer->polygons.empty() && support_layer->print_z >= m_slicing_params.raft_contact_top_z + EPSILON) // Non-empty support layer and not a raft layer. nonempty_layers.push_back(support_layer); @@ -3304,7 +3233,7 @@ void PrintObjectSupportMaterial::trim_support_layers_by_object( [this, &object, &nonempty_layers, gap_extra_above, gap_extra_below, gap_xy_scaled](const tbb::blocked_range& range) { size_t idx_object_layer_overlapping = size_t(-1); - auto is_layers_overlap = [](const MyLayer& support_layer, const Layer& object_layer, coordf_t bridging_height = 0.f) -> bool { + auto is_layers_overlap = [](const SupportGeneratorLayer& support_layer, const Layer& object_layer, coordf_t bridging_height = 0.f) -> bool { if (std::abs(support_layer.print_z - object_layer.print_z) < EPSILON) return true; @@ -3318,7 +3247,7 @@ void PrintObjectSupportMaterial::trim_support_layers_by_object( return false; }; for (size_t idx_layer = range.begin(); idx_layer < range.end(); ++ idx_layer) { - MyLayer &support_layer = *nonempty_layers[idx_layer]; + SupportGeneratorLayer &support_layer = *nonempty_layers[idx_layer]; // BOOST_LOG_TRIVIAL(trace) << "Support generator - trim_support_layers_by_object - trimmming non-empty layer " << idx_layer << " of " << nonempty_layers.size(); assert(! support_layer.polygons.empty() && support_layer.print_z >= m_slicing_params.raft_contact_top_z + EPSILON); // Find the overlapping object layers including the extra above / below gap. @@ -3378,13 +3307,15 @@ void PrintObjectSupportMaterial::trim_support_layers_by_object( BOOST_LOG_TRIVIAL(debug) << "PrintObjectSupportMaterial::trim_support_layers_by_object() in parallel - end"; } -PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::generate_raft_base( - const PrintObject &object, - const MyLayersPtr &top_contacts, - const MyLayersPtr &interface_layers, - const MyLayersPtr &base_interface_layers, - const MyLayersPtr &base_layers, - MyLayerStorage &layer_storage) const +SupportGeneratorLayersPtr generate_raft_base( + const PrintObject &object, + const SupportParameters &support_params, + const SlicingParameters &slicing_params, + const SupportGeneratorLayersPtr &top_contacts, + const SupportGeneratorLayersPtr &interface_layers, + const SupportGeneratorLayersPtr &base_interface_layers, + const SupportGeneratorLayersPtr &base_layers, + SupportGeneratorLayerStorage &layer_storage) { // If there is brim to be generated, calculate the trimming regions. Polygons brim; @@ -3418,22 +3349,22 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::generate_raf } // How much to inflate the support columns to be stable. This also applies to the 1st layer, if no raft layers are to be printed. - const float inflate_factor_fine = float(scale_((m_slicing_params.raft_layers() > 1) ? 0.5 : EPSILON)); + const float inflate_factor_fine = float(scale_((slicing_params.raft_layers() > 1) ? 0.5 : EPSILON)); const float inflate_factor_1st_layer = std::max(0.f, float(scale_(object.config().raft_first_layer_expansion)) - inflate_factor_fine); - MyLayer *contacts = top_contacts .empty() ? nullptr : top_contacts .front(); - MyLayer *interfaces = interface_layers .empty() ? nullptr : interface_layers .front(); - MyLayer *base_interfaces = base_interface_layers.empty() ? nullptr : base_interface_layers.front(); - MyLayer *columns_base = base_layers .empty() ? nullptr : base_layers .front(); - if (contacts != nullptr && contacts->print_z > std::max(m_slicing_params.first_print_layer_height, m_slicing_params.raft_contact_top_z) + EPSILON) + SupportGeneratorLayer *contacts = top_contacts .empty() ? nullptr : top_contacts .front(); + SupportGeneratorLayer *interfaces = interface_layers .empty() ? nullptr : interface_layers .front(); + SupportGeneratorLayer *base_interfaces = base_interface_layers.empty() ? nullptr : base_interface_layers.front(); + SupportGeneratorLayer *columns_base = base_layers .empty() ? nullptr : base_layers .front(); + if (contacts != nullptr && contacts->print_z > std::max(slicing_params.first_print_layer_height, slicing_params.raft_contact_top_z) + EPSILON) // This is not the raft contact layer. contacts = nullptr; - if (interfaces != nullptr && interfaces->bottom_print_z() > m_slicing_params.raft_interface_top_z + EPSILON) + if (interfaces != nullptr && interfaces->bottom_print_z() > slicing_params.raft_interface_top_z + EPSILON) // This is not the raft column base layer. interfaces = nullptr; - if (base_interfaces != nullptr && base_interfaces->bottom_print_z() > m_slicing_params.raft_interface_top_z + EPSILON) + if (base_interfaces != nullptr && base_interfaces->bottom_print_z() > slicing_params.raft_interface_top_z + EPSILON) // This is not the raft column base layer. base_interfaces = nullptr; - if (columns_base != nullptr && columns_base->bottom_print_z() > m_slicing_params.raft_interface_top_z + EPSILON) + if (columns_base != nullptr && columns_base->bottom_print_z() > slicing_params.raft_interface_top_z + EPSILON) // This is not the raft interface layer. columns_base = nullptr; @@ -3446,9 +3377,9 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::generate_raf polygons_append(interface_polygons, expand(base_interfaces->polygons, inflate_factor_fine, SUPPORT_SURFACES_OFFSET_PARAMETERS)); // Output vector. - MyLayersPtr raft_layers; + SupportGeneratorLayersPtr raft_layers; - if (m_slicing_params.raft_layers() > 1) { + if (slicing_params.raft_layers() > 1) { Polygons base; Polygons columns; if (columns_base != nullptr) { @@ -3465,30 +3396,30 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::generate_raf // Do not add the raft contact layer, only add the raft layers below the contact layer. // Insert the 1st layer. { - MyLayer &new_layer = layer_allocate(layer_storage, (m_slicing_params.base_raft_layers > 0) ? sltRaftBase : sltRaftInterface); + SupportGeneratorLayer &new_layer = layer_allocate(layer_storage, (slicing_params.base_raft_layers > 0) ? sltRaftBase : sltRaftInterface); raft_layers.push_back(&new_layer); - new_layer.print_z = m_slicing_params.first_print_layer_height; - new_layer.height = m_slicing_params.first_print_layer_height; + new_layer.print_z = slicing_params.first_print_layer_height; + new_layer.height = slicing_params.first_print_layer_height; new_layer.bottom_z = 0.; new_layer.polygons = inflate_factor_1st_layer > 0 ? expand(base, inflate_factor_1st_layer) : base; } // Insert the base layers. - for (size_t i = 1; i < m_slicing_params.base_raft_layers; ++ i) { + for (size_t i = 1; i < slicing_params.base_raft_layers; ++ i) { coordf_t print_z = raft_layers.back()->print_z; - MyLayer &new_layer = layer_allocate(layer_storage, sltRaftBase); + SupportGeneratorLayer &new_layer = layer_allocate(layer_storage, SupporLayerType::sltRaftBase); raft_layers.push_back(&new_layer); - new_layer.print_z = print_z + m_slicing_params.base_raft_layer_height; - new_layer.height = m_slicing_params.base_raft_layer_height; + new_layer.print_z = print_z + slicing_params.base_raft_layer_height; + new_layer.height = slicing_params.base_raft_layer_height; new_layer.bottom_z = print_z; new_layer.polygons = base; } // Insert the interface layers. - for (size_t i = 1; i < m_slicing_params.interface_raft_layers; ++ i) { + for (size_t i = 1; i < slicing_params.interface_raft_layers; ++ i) { coordf_t print_z = raft_layers.back()->print_z; - MyLayer &new_layer = layer_allocate(layer_storage, sltRaftInterface); + SupportGeneratorLayer &new_layer = layer_allocate(layer_storage, SupporLayerType::sltRaftInterface); raft_layers.push_back(&new_layer); - new_layer.print_z = print_z + m_slicing_params.interface_raft_layer_height; - new_layer.height = m_slicing_params.interface_raft_layer_height; + new_layer.print_z = print_z + slicing_params.interface_raft_layer_height; + new_layer.height = slicing_params.interface_raft_layer_height; new_layer.bottom_z = print_z; new_layer.polygons = interface_polygons; //FIXME misusing contact_polygons for support columns. @@ -3501,12 +3432,12 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::generate_raf Polygons trimming; // BBS: if first layer of support is intersected with object island, it must have the same function as brim unless in nobrim mode. if (object.has_brim()) - trimming = offset(m_object->layers().front()->lslices, (float)scale_(object.config().brim_object_gap.value), SUPPORT_SURFACES_OFFSET_PARAMETERS); + trimming = offset(object.layers().front()->lslices, (float)scale_(object.config().brim_object_gap.value), SUPPORT_SURFACES_OFFSET_PARAMETERS); else - trimming = offset(m_object->layers().front()->lslices, (float)scale_(m_support_params.gap_xy), SUPPORT_SURFACES_OFFSET_PARAMETERS); + trimming = offset(object.layers().front()->lslices, (float)scale_(support_params.gap_xy), SUPPORT_SURFACES_OFFSET_PARAMETERS); if (inflate_factor_1st_layer > SCALED_EPSILON) { // Inflate in multiple steps to avoid leaking of the support 1st layer through object walls. - auto nsteps = std::max(5, int(ceil(inflate_factor_1st_layer / m_support_params.first_layer_flow.scaled_width()))); + auto nsteps = std::max(5, int(ceil(inflate_factor_1st_layer / support_params.first_layer_flow.scaled_width()))); float step = inflate_factor_1st_layer / nsteps; for (int i = 0; i < nsteps; ++ i) raft = diff(expand(raft, step), trimming); @@ -3531,17 +3462,17 @@ PrintObjectSupportMaterial::MyLayersPtr PrintObjectSupportMaterial::generate_raf } // Convert some of the intermediate layers into top/bottom interface layers as well as base interface layers. -std::pair PrintObjectSupportMaterial::generate_interface_layers( - const MyLayersPtr &bottom_contacts, - const MyLayersPtr &top_contacts, - MyLayersPtr &intermediate_layers, - MyLayerStorage &layer_storage) const +std::pair PrintObjectSupportMaterial::generate_interface_layers( + const SupportGeneratorLayersPtr &bottom_contacts, + const SupportGeneratorLayersPtr &top_contacts, + SupportGeneratorLayersPtr &intermediate_layers, + SupportGeneratorLayerStorage &layer_storage) const { // my $area_threshold = $self->interface_flow->scaled_spacing ** 2; - std::pair base_and_interface_layers; - MyLayersPtr &interface_layers = base_and_interface_layers.first; - MyLayersPtr &base_interface_layers = base_and_interface_layers.second; + std::pair base_and_interface_layers; + SupportGeneratorLayersPtr &interface_layers = base_and_interface_layers.first; + SupportGeneratorLayersPtr &base_interface_layers = base_and_interface_layers.second; // distinguish between interface and base interface layers // Contact layer is considered an interface layer, therefore run the following block only if support_interface_top_layers > 1. @@ -3580,7 +3511,7 @@ std::pair MyLayer* { + SupportGeneratorLayer &intermediate_layer, Polygons &bottom, Polygons &&top, const Polygons *subtract, SupporLayerType type) -> SupportGeneratorLayer* { assert(! bottom.empty() || ! top.empty()); // Merge top into bottom, unite them with a safety offset. append(bottom, std::move(top)); @@ -3594,7 +3525,7 @@ std::pair::max() : intermediate_layers[std::min(num_intermediate - 1, idx_intermediate_layer + num_interface_layers_only_top - 1)]->print_z; // Move idx_top_contact_first up until above the current print_z. - idx_top_contact_first = idx_higher_or_equal(top_contacts, idx_top_contact_first, [&intermediate_layer](const MyLayer *layer){ return layer->print_z >= intermediate_layer.print_z; }); // - EPSILON + idx_top_contact_first = idx_higher_or_equal(top_contacts, idx_top_contact_first, [&intermediate_layer](const SupportGeneratorLayer *layer){ return layer->print_z >= intermediate_layer.print_z; }); // - EPSILON // Collect the top contact areas above this intermediate layer, below top_z. for (int idx_top_contact = idx_top_contact_first; idx_top_contact < int(top_contacts.size()); ++ idx_top_contact) { - const MyLayer &top_contact_layer = *top_contacts[idx_top_contact]; + const SupportGeneratorLayer &top_contact_layer = *top_contacts[idx_top_contact]; //FIXME maybe this adds one interface layer in excess? if (top_contact_layer.bottom_z - EPSILON > top_z) break; @@ -3664,16 +3595,16 @@ std::pair::max() : intermediate_layers[std::max(0, idx_intermediate_layer - num_interface_layers_only_bottom)]->bottom_z; // Move idx_bottom_contact_first up until touching bottom_z. - idx_bottom_contact_first = idx_higher_or_equal(bottom_contacts, idx_bottom_contact_first, [bottom_z](const MyLayer *layer){ return layer->print_z >= bottom_z - EPSILON; }); + idx_bottom_contact_first = idx_higher_or_equal(bottom_contacts, idx_bottom_contact_first, [bottom_z](const SupportGeneratorLayer *layer){ return layer->print_z >= bottom_z - EPSILON; }); // Collect the top contact areas above this intermediate layer, below top_z. for (int idx_bottom_contact = idx_bottom_contact_first; idx_bottom_contact < int(bottom_contacts.size()); ++ idx_bottom_contact) { - const MyLayer &bottom_contact_layer = *bottom_contacts[idx_bottom_contact]; + const SupportGeneratorLayer &bottom_contact_layer = *bottom_contacts[idx_bottom_contact]; if (bottom_contact_layer.print_z - EPSILON > intermediate_layer.bottom_z) break; polygons_append(bottom_contact_layer.print_z - EPSILON > bottom_interface_z ? polygons_bottom_contact_projected_interface : polygons_bottom_contact_projected_base, bottom_contact_layer.polygons); } } - MyLayer *interface_layer = nullptr; + SupportGeneratorLayer *interface_layer = nullptr; if (! polygons_bottom_contact_projected_interface.empty() || ! polygons_top_contact_projected_interface.empty()) { interface_layer = insert_layer( intermediate_layer, polygons_bottom_contact_projected_interface, std::move(polygons_top_contact_projected_interface), nullptr, @@ -3743,7 +3674,250 @@ static inline void fill_expolygons_generate_paths( fill_expolygons_generate_paths(dst, std::move(expolygons), filler, fill_params, role, flow); } -static inline void fill_expolygons_with_sheath_generate_paths( +static Polylines draw_perimeters(const ExPolygon &expoly, double clip_length) +{ + // Draw the perimeters. + Polylines polylines; + polylines.reserve(expoly.holes.size() + 1); + for (size_t i = 0; i <= expoly.holes.size(); ++ i) { + Polyline pl(i == 0 ? expoly.contour.points : expoly.holes[i - 1].points); + pl.points.emplace_back(pl.points.front()); + if (i > 0) + // It is a hole, reverse it. + pl.reverse(); + // so that all contours are CCW oriented. + pl.clip_end(clip_length); + polylines.emplace_back(std::move(pl)); + } + return polylines; +} + +static inline void tree_supports_generate_paths( + ExtrusionEntitiesPtr &dst, + const Polygons &polygons, + const Flow &flow) +{ + // Offset expolygon inside, returns number of expolygons collected (0 or 1). + // Vertices of output paths are marked with Z = source contour index of the expoly. + // Vertices at the intersection of source contours are marked with Z = -1. + auto shrink_expolygon_with_contour_idx = [](const Slic3r::ExPolygon &expoly, const float delta, ClipperLib::JoinType joinType, double miterLimit, ClipperLib_Z::Paths &out) -> int + { + assert(delta > 0); + auto append_paths_with_z = [](ClipperLib::Paths &src, coord_t contour_idx, ClipperLib_Z::Paths &dst) { + dst.reserve(next_highest_power_of_2(dst.size() + src.size())); + for (const ClipperLib::Path &contour : src) { + ClipperLib_Z::Path tmp; + tmp.reserve(contour.size()); + for (const Point &p : contour) + tmp.emplace_back(p.x(), p.y(), contour_idx); + dst.emplace_back(std::move(tmp)); + } + }; + + // 1) Offset the outer contour. + ClipperLib_Z::Paths contours; + { + ClipperLib::ClipperOffset co; + if (joinType == jtRound) + co.ArcTolerance = miterLimit; + else + co.MiterLimit = miterLimit; + co.ShortestEdgeLength = double(delta * 0.005); + co.AddPath(expoly.contour.points, joinType, ClipperLib::etClosedPolygon); + ClipperLib::Paths contours_raw; + co.Execute(contours_raw, - delta); + if (contours_raw.empty()) + // No need to try to offset the holes. + return 0; + append_paths_with_z(contours_raw, 0, contours); + } + + if (expoly.holes.empty()) { + // No need to subtract holes from the offsetted expolygon, we are done. + append(out, std::move(contours)); + } else { + // 2) Offset the holes one by one, collect the offsetted holes. + ClipperLib_Z::Paths holes; + { + for (const Polygon &hole : expoly.holes) { + ClipperLib::ClipperOffset co; + if (joinType == jtRound) + co.ArcTolerance = miterLimit; + else + co.MiterLimit = miterLimit; + co.ShortestEdgeLength = double(delta * 0.005); + co.AddPath(hole.points, joinType, ClipperLib::etClosedPolygon); + ClipperLib::Paths out2; + // Execute reorients the contours so that the outer most contour has a positive area. Thus the output + // contours will be CCW oriented even though the input paths are CW oriented. + // Offset is applied after contour reorientation, thus the signum of the offset value is reversed. + co.Execute(out2, delta); + append_paths_with_z(out2, 1 + (&hole - expoly.holes.data()), holes); + } + } + + // 3) Subtract holes from the contours. + if (holes.empty()) { + // No hole remaining after an offset. Just copy the outer contour. + append(out, std::move(contours)); + } else { + // Negative offset. There is a chance, that the offsetted hole intersects the outer contour. + // Subtract the offsetted holes from the offsetted contours. + ClipperLib_Z::Clipper clipper; + clipper.ZFillFunction([](const ClipperLib_Z::IntPoint &e1bot, const ClipperLib_Z::IntPoint &e1top, const ClipperLib_Z::IntPoint &e2bot, const ClipperLib_Z::IntPoint &e2top, ClipperLib_Z::IntPoint &pt) { + //pt.z() = std::max(std::max(e1bot.z(), e1top.z()), std::max(e2bot.z(), e2top.z())); + // Just mark the intersection. + pt.z() = -1; + }); + clipper.AddPaths(contours, ClipperLib_Z::ptSubject, true); + clipper.AddPaths(holes, ClipperLib_Z::ptClip, true); + ClipperLib_Z::Paths output; + clipper.Execute(ClipperLib_Z::ctDifference, output, ClipperLib_Z::pftNonZero, ClipperLib_Z::pftNonZero); + if (! output.empty()) { + append(out, std::move(output)); + } else { + // The offsetted holes have eaten up the offsetted outer contour. + return 0; + } + } + } + + return 1; + }; + + const double spacing = flow.scaled_spacing(); + // Clip the sheath path to avoid the extruder to get exactly on the first point of the loop. + const double clip_length = spacing * 0.15; + const double anchor_length = spacing * 6.; + ClipperLib_Z::Paths anchor_candidates; + for (ExPolygon& expoly : closing_ex(polygons, float(SCALED_EPSILON), float(SCALED_EPSILON + 0.5 * flow.scaled_width()))) { + std::unique_ptr eec; + double area = expoly.area(); + if (area > sqr(scaled(5.))) { + eec = std::make_unique(); + // Don't reoder internal / external loops of the same island, always start with the internal loop. + eec->no_sort = true; + // Make the tree branch stable by adding another perimeter. + ExPolygons level2 = offset2_ex({ expoly }, -1.5 * flow.scaled_width(), 0.5 * flow.scaled_width()); + if (level2.size() == 1) { + Polylines polylines; + extrusion_entities_append_paths(eec->entities, draw_perimeters(expoly, clip_length), erSupportMaterial, flow.mm3_per_mm(), flow.width(), flow.height(), + // Disable reversal of the path, always start with the anchor, always print CCW. + false); + expoly = level2.front(); + } + } + + // Try to produce one more perimeter to place the seam anchor. + // First genrate a 2nd perimeter loop as a source for anchor candidates. + // The anchor candidate points are annotated with an index of the source contour or with -1 if on intersection. + anchor_candidates.clear(); + shrink_expolygon_with_contour_idx(expoly, flow.scaled_width(), DefaultJoinType, 1.2, anchor_candidates); + // Orient all contours CW. + for (auto &path : anchor_candidates) + if (ClipperLib_Z::Area(path) > 0) + std::reverse(path.begin(), path.end()); + + // Draw the perimeters. + Polylines polylines; + polylines.reserve(expoly.holes.size() + 1); + for (size_t idx_loop = 0; idx_loop < expoly.num_contours(); ++ idx_loop) { + // Open the loop with a seam. + const Polygon &loop = expoly.contour_or_hole(idx_loop); + Polyline pl(loop.points); + // Orient all contours CW, because the anchor will be added to the end of polyline while we want to start a loop with the anchor. + if (idx_loop == 0) + // It is an outer contour. + pl.reverse(); + pl.points.emplace_back(pl.points.front()); + pl.clip_end(clip_length); + if (pl.size() < 2) + continue; + // Find the foot of the seam point on anchor_candidates. Only pick an anchor point that was created by offsetting the source contour. + ClipperLib_Z::Path *closest_contour = nullptr; + Vec2d closest_point; + int closest_point_idx = -1; + double closest_point_t; + double d2min = std::numeric_limits::max(); + Vec2d seam_pt = pl.back().cast(); + for (ClipperLib_Z::Path &path : anchor_candidates) + for (int i = 0; i < path.size(); ++ i) { + int j = next_idx_modulo(i, path); + if (path[i].z() == idx_loop || path[j].z() == idx_loop) { + Vec2d pi(path[i].x(), path[i].y()); + Vec2d pj(path[j].x(), path[j].y()); + Vec2d v = pj - pi; + Vec2d w = seam_pt - pi; + auto l2 = v.squaredNorm(); + auto t = std::clamp((l2 == 0) ? 0 : v.dot(w) / l2, 0., 1.); + if ((path[i].z() == idx_loop || t > EPSILON) && (path[j].z() == idx_loop || t < 1. - EPSILON)) { + // Closest point. + Vec2d fp = pi + v * t; + double d2 = (fp - seam_pt).squaredNorm(); + if (d2 < d2min) { + d2min = d2; + closest_contour = &path; + closest_point = fp; + closest_point_idx = i; + closest_point_t = t; + } + } + } + } + if (d2min < sqr(flow.scaled_width() * 3.)) { + // Try to cut an anchor from the closest_contour. + // Both closest_contour and pl are CW oriented. + pl.points.emplace_back(closest_point.cast()); + const ClipperLib_Z::Path &path = *closest_contour; + double remaining_length = anchor_length - (seam_pt - closest_point).norm(); + int i = closest_point_idx; + int j = next_idx_modulo(i, *closest_contour); + Vec2d pi(path[i].x(), path[i].y()); + Vec2d pj(path[j].x(), path[j].y()); + Vec2d v = pj - pi; + double l = v.norm(); + if (remaining_length < (1. - closest_point_t) * l) { + // Just trim the current line. + pl.points.emplace_back((closest_point + v * (remaining_length / l)).cast()); + } else { + // Take the rest of the current line, continue with the other lines. + pl.points.emplace_back(path[j].x(), path[j].y()); + pi = pj; + for (i = j; path[i].z() == idx_loop && remaining_length > 0; i = j, pi = pj) { + j = next_idx_modulo(i, path); + pj = Vec2d(path[j].x(), path[j].y()); + v = pj - pi; + l = v.norm(); + if (i == closest_point_idx) { + // Back at the first segment. Most likely this should not happen and we may end the anchor. + break; + } + if (remaining_length <= l) { + pl.points.emplace_back((pi + v * (remaining_length / l)).cast()); + break; + } + pl.points.emplace_back(path[j].x(), path[j].y()); + remaining_length -= l; + } + } + } + // Start with the anchor. + pl.reverse(); + polylines.emplace_back(std::move(pl)); + } + + ExtrusionEntitiesPtr &out = eec ? eec->entities : dst; + extrusion_entities_append_paths(out, std::move(polylines), erSupportMaterial, flow.mm3_per_mm(), flow.width(), flow.height(), + // Disable reversal of the path, always start with the anchor, always print CCW. + false); + if (eec) { + std::reverse(eec->entities.begin(), eec->entities.end()); + dst.emplace_back(eec.release()); + } + } +} + +void fill_expolygons_with_sheath_generate_paths( ExtrusionEntitiesPtr &dst, const Polygons &polygons, Fill *filler, @@ -3756,7 +3930,12 @@ static inline void fill_expolygons_with_sheath_generate_paths( if (polygons.empty()) return; - if (! with_sheath) { + if (with_sheath) { + if (density == 0) { + tree_supports_generate_paths(dst, polygons, flow); + return; + } + } else { fill_expolygons_generate_paths(dst, closing_ex(polygons, float(SCALED_EPSILON)), filler, density, role, flow); return; } @@ -3795,9 +3974,9 @@ static inline void fill_expolygons_with_sheath_generate_paths( } // Support layers, partially processed. -struct MyLayerExtruded +struct SupportGeneratorLayerExtruded { - MyLayerExtruded& operator=(MyLayerExtruded &&rhs) { + SupportGeneratorLayerExtruded& operator=(SupportGeneratorLayerExtruded &&rhs) { this->layer = rhs.layer; this->extrusions = std::move(rhs.extrusions); m_polygons_to_extrude = std::move(rhs.m_polygons_to_extrude); @@ -3818,14 +3997,14 @@ struct MyLayerExtruded Polygons& polygons_to_extrude() { return (m_polygons_to_extrude == nullptr) ? layer->polygons : *m_polygons_to_extrude; } const Polygons& polygons_to_extrude() const { return (m_polygons_to_extrude == nullptr) ? layer->polygons : *m_polygons_to_extrude; } - bool could_merge(const MyLayerExtruded &other) const { + bool could_merge(const SupportGeneratorLayerExtruded &other) const { return ! this->empty() && ! other.empty() && std::abs(this->layer->height - other.layer->height) < EPSILON && this->layer->bridging == other.layer->bridging; } // Merge regions, perform boolean union over the merged polygons. - void merge(MyLayerExtruded &&other) { + void merge(SupportGeneratorLayerExtruded &&other) { assert(this->could_merge(other)); // 1) Merge the rest polygons to extrude, if there are any. if (other.m_polygons_to_extrude != nullptr) { @@ -3859,7 +4038,7 @@ struct MyLayerExtruded } // The source layer. It carries the height and extrusion type (bridging / non bridging, extrusion height). - PrintObjectSupportMaterial::MyLayer *layer { nullptr }; + SupportGeneratorLayer *layer { nullptr }; // Collect extrusions. They will be exported sorted by the bottom height. ExtrusionEntitiesPtr extrusions; @@ -3869,7 +4048,7 @@ private: std::unique_ptr m_polygons_to_extrude; }; -typedef std::vector MyLayerExtrudedPtrs; +typedef std::vector SupportGeneratorLayerExtrudedPtrs; struct LoopInterfaceProcessor { @@ -3888,7 +4067,7 @@ struct LoopInterfaceProcessor // Generate loop contacts at the top_contact_layer, // trim the top_contact_layer->polygons with the areas covered by the loops. - void generate(MyLayerExtruded &top_contact_layer, const Flow &interface_flow_src) const; + void generate(SupportGeneratorLayerExtruded &top_contact_layer, const Flow &interface_flow_src) const; int n_contact_loops; coordf_t circle_radius; @@ -3896,7 +4075,7 @@ struct LoopInterfaceProcessor Polygon circle; }; -void LoopInterfaceProcessor::generate(MyLayerExtruded &top_contact_layer, const Flow &interface_flow_src) const +void LoopInterfaceProcessor::generate(SupportGeneratorLayerExtruded &top_contact_layer, const Flow &interface_flow_src) const { if (n_contact_loops == 0 || top_contact_layer.empty()) return; @@ -4127,9 +4306,9 @@ static std::string dbg_index_to_color(int idx) void modulate_extrusion_by_overlapping_layers( // Extrusions generated for this_layer. ExtrusionEntitiesPtr &extrusions_in_out, - const PrintObjectSupportMaterial::MyLayer &this_layer, + const SupportGeneratorLayer &this_layer, // Multiple layers overlapping with this_layer, sorted bottom up. - const PrintObjectSupportMaterial::MyLayersPtr &overlapping_layers) + const SupportGeneratorLayersPtr &overlapping_layers) { size_t n_overlapping_layers = overlapping_layers.size(); if (n_overlapping_layers == 0 || extrusions_in_out.empty()) @@ -4166,7 +4345,7 @@ void modulate_extrusion_by_overlapping_layers( ++ iRun; BoundingBox bbox; for (size_t i_overlapping_layer = 0; i_overlapping_layer < n_overlapping_layers; ++ i_overlapping_layer) { - const PrintObjectSupportMaterial::MyLayer &overlapping_layer = *overlapping_layers[i_overlapping_layer]; + const SupportGeneratorLayer &overlapping_layer = *overlapping_layers[i_overlapping_layer]; bbox.merge(get_extents(overlapping_layer.polygons)); } for (ExtrusionEntitiesPtr::const_iterator it = extrusions_in_out.begin(); it != extrusions_in_out.end(); ++ it) { @@ -4179,13 +4358,13 @@ void modulate_extrusion_by_overlapping_layers( // Filled polygons for the overlapping regions. svg.draw(union_ex(this_layer.polygons), dbg_index_to_color(-1), transparency); for (size_t i_overlapping_layer = 0; i_overlapping_layer < n_overlapping_layers; ++ i_overlapping_layer) { - const PrintObjectSupportMaterial::MyLayer &overlapping_layer = *overlapping_layers[i_overlapping_layer]; + const SupportGeneratorLayer &overlapping_layer = *overlapping_layers[i_overlapping_layer]; svg.draw(union_ex(overlapping_layer.polygons), dbg_index_to_color(int(i_overlapping_layer)), transparency); } // Contours of the overlapping regions. svg.draw(to_polylines(this_layer.polygons), dbg_index_to_color(-1), scale_(0.2)); for (size_t i_overlapping_layer = 0; i_overlapping_layer < n_overlapping_layers; ++ i_overlapping_layer) { - const PrintObjectSupportMaterial::MyLayer &overlapping_layer = *overlapping_layers[i_overlapping_layer]; + const SupportGeneratorLayer &overlapping_layer = *overlapping_layers[i_overlapping_layer]; svg.draw(to_polylines(overlapping_layer.polygons), dbg_index_to_color(int(i_overlapping_layer)), scale_(0.1)); } // Fill extrusion, the source. @@ -4226,7 +4405,7 @@ void modulate_extrusion_by_overlapping_layers( // Fragment the path segments by overlapping layers. The overlapping layers are sorted by an increasing print_z. // Trim by the highest overlapping layer first. for (int i_overlapping_layer = int(n_overlapping_layers) - 1; i_overlapping_layer >= 0; -- i_overlapping_layer) { - const PrintObjectSupportMaterial::MyLayer &overlapping_layer = *overlapping_layers[i_overlapping_layer]; + const SupportGeneratorLayer &overlapping_layer = *overlapping_layers[i_overlapping_layer]; ExtrusionPathFragment &frag = path_fragments[i_overlapping_layer]; Polygons polygons_trimming = offset(union_ex(overlapping_layer.polygons), float(scale_(0.5*extrusion_width))); frag.polylines = intersection_pl(path_fragments.back().polylines, polygons_trimming); @@ -4317,7 +4496,7 @@ void modulate_extrusion_by_overlapping_layers( ExtrusionPath *path = multipath.paths.empty() ? nullptr : &multipath.paths.back(); if (path != nullptr) { // Verify whether the path is compatible with the current fragment. - assert(this_layer.layer_type == PrintObjectSupportMaterial::sltBottomContact || path->height != frag.height || path->mm3_per_mm != frag.mm3_per_mm); + assert(this_layer.layer_type == sltBottomContact || path->height != frag.height || path->mm3_per_mm != frag.mm3_per_mm); if (path->height != frag.height || path->mm3_per_mm != frag.mm3_per_mm) { path = nullptr; } @@ -4364,22 +4543,103 @@ void modulate_extrusion_by_overlapping_layers( extrusion_entities_append_paths(extrusions_in_out, std::move(it_fragment->polylines), extrusion_role, it_fragment->mm3_per_mm, it_fragment->width, it_fragment->height); } -void PrintObjectSupportMaterial::generate_toolpaths( - SupportLayerPtrs &support_layers, - const MyLayersPtr &raft_layers, - const MyLayersPtr &bottom_contacts, - const MyLayersPtr &top_contacts, - const MyLayersPtr &intermediate_layers, - const MyLayersPtr &interface_layers, - const MyLayersPtr &base_interface_layers) const +SupportGeneratorLayersPtr generate_support_layers( + PrintObject &object, + const SupportGeneratorLayersPtr &raft_layers, + const SupportGeneratorLayersPtr &bottom_contacts, + const SupportGeneratorLayersPtr &top_contacts, + const SupportGeneratorLayersPtr &intermediate_layers, + const SupportGeneratorLayersPtr &interface_layers, + const SupportGeneratorLayersPtr &base_interface_layers) +{ + // Install support layers into the object. + // A support layer installed on a PrintObject has a unique print_z. + SupportGeneratorLayersPtr layers_sorted; + layers_sorted.reserve(raft_layers.size() + bottom_contacts.size() + top_contacts.size() + intermediate_layers.size() + interface_layers.size() + base_interface_layers.size()); + layers_append(layers_sorted, raft_layers); + layers_append(layers_sorted, bottom_contacts); + layers_append(layers_sorted, top_contacts); + layers_append(layers_sorted, intermediate_layers); + layers_append(layers_sorted, interface_layers); + layers_append(layers_sorted, base_interface_layers); + // Sort the layers lexicographically by a raising print_z and a decreasing height. + std::sort(layers_sorted.begin(), layers_sorted.end(), [](auto *l1, auto *l2) { return *l1 < *l2; }); + int layer_id = 0; + int layer_id_interface = 0; + assert(object.support_layers().empty()); + for (size_t i = 0; i < layers_sorted.size();) { + // Find the last layer with roughly the same print_z, find the minimum layer height of all. + // Due to the floating point inaccuracies, the print_z may not be the same even if in theory they should. + size_t j = i + 1; + coordf_t zmax = layers_sorted[i]->print_z + EPSILON; + for (; j < layers_sorted.size() && layers_sorted[j]->print_z <= zmax; ++j) ; + // Assign an average print_z to the set of layers with nearly equal print_z. + coordf_t zavg = 0.5 * (layers_sorted[i]->print_z + layers_sorted[j - 1]->print_z); + coordf_t height_min = layers_sorted[i]->height; + bool empty = true; + // For snug supports, layers where the direction of the support interface shall change are accounted for. + size_t num_interfaces = 0; + size_t num_top_contacts = 0; + double top_contact_bottom_z = 0; + for (size_t u = i; u < j; ++u) { + SupportGeneratorLayer &layer = *layers_sorted[u]; + if (! layer.polygons.empty()) { + empty = false; + num_interfaces += one_of(layer.layer_type, support_types_interface); + if (layer.layer_type == SupporLayerType::sltTopContact) { + ++ num_top_contacts; + assert(num_top_contacts <= 1); + // All top contact layers sharing this print_z shall also share bottom_z. + //assert(num_top_contacts == 1 || (top_contact_bottom_z - layer.bottom_z) < EPSILON); + top_contact_bottom_z = layer.bottom_z; + } + } + layer.print_z = zavg; + height_min = std::min(height_min, layer.height); + } + if (! empty) { + // Here the upper_layer and lower_layer pointers are left to null at the support layers, + // as they are never used. These pointers are candidates for removal. + bool this_layer_contacts_only = num_top_contacts > 0 && num_top_contacts == num_interfaces; + size_t this_layer_id_interface = layer_id_interface; + if (this_layer_contacts_only) { + // Find a supporting layer for its interface ID. + for (auto it = object.support_layers().rbegin(); it != object.support_layers().rend(); ++ it) + if (const SupportLayer &other_layer = **it; std::abs(other_layer.print_z - top_contact_bottom_z) < EPSILON) { + // other_layer supports this top contact layer. Assign a different support interface direction to this layer + // from the layer that supports it. + this_layer_id_interface = other_layer.interface_id() + 1; + } + } + object.add_support_layer(layer_id ++, this_layer_id_interface, height_min, zavg); + if (num_interfaces && ! this_layer_contacts_only) + ++ layer_id_interface; + } + i = j; + } + return layers_sorted; +} + +void generate_support_toolpaths( + PrintObject &object, + SupportLayerPtrs &support_layers, + const PrintObjectConfig &config, + const SupportParameters &support_params, + const SlicingParameters &slicing_params, + const SupportGeneratorLayersPtr &raft_layers, + const SupportGeneratorLayersPtr &bottom_contacts, + const SupportGeneratorLayersPtr &top_contacts, + const SupportGeneratorLayersPtr &intermediate_layers, + const SupportGeneratorLayersPtr &interface_layers, + const SupportGeneratorLayersPtr &base_interface_layers) { // loop_interface_processor with a given circle radius. - LoopInterfaceProcessor loop_interface_processor(1.5 * m_support_params.support_material_interface_flow.scaled_width()); - loop_interface_processor.n_contact_loops = this->has_contact_loops() ? 1 : 0; + LoopInterfaceProcessor loop_interface_processor(1.5 * support_params.support_material_interface_flow.scaled_width()); + loop_interface_processor.n_contact_loops = config.support_interface_loop_pattern ? 1 : 0; - std::vector angles { m_support_params.base_angle }; - if (m_object_config->support_base_pattern == smpRectilinearGrid) - angles.push_back(m_support_params.interface_angle); + std::vector angles { support_params.base_angle }; + if (config.support_base_pattern == smpRectilinearGrid) + angles.push_back(support_params.interface_angle); BoundingBox bbox_object(Point(-scale_(1.), -scale_(1.0)), Point(scale_(1.), scale_(1.))); @@ -4389,34 +4649,34 @@ void PrintObjectSupportMaterial::generate_toolpaths( float raft_angle_1st_layer = 0.f; float raft_angle_base = 0.f; float raft_angle_interface = 0.f; - if (m_slicing_params.base_raft_layers > 1) { + if (slicing_params.base_raft_layers > 1) { // There are all raft layer types (1st layer, base, interface & contact layers) available. - raft_angle_1st_layer = m_support_params.interface_angle; - raft_angle_base = m_support_params.base_angle; - raft_angle_interface = m_support_params.interface_angle; - } else if (m_slicing_params.base_raft_layers == 1 || m_slicing_params.interface_raft_layers > 1) { + raft_angle_1st_layer = support_params.interface_angle; + raft_angle_base = support_params.base_angle; + raft_angle_interface = support_params.interface_angle; + } else if (slicing_params.base_raft_layers == 1 || slicing_params.interface_raft_layers > 1) { // 1st layer, interface & contact layers available. - raft_angle_1st_layer = m_support_params.base_angle; - if (this->has_support()) + raft_angle_1st_layer = support_params.base_angle; + if (config.enable_support.value || config.enforce_support_layers) // has_support() // Print 1st layer at 45 degrees from both the interface and base angles as both can land on the 1st layer. raft_angle_1st_layer += 0.7854f; - raft_angle_interface = m_support_params.interface_angle; - } else if (m_slicing_params.interface_raft_layers == 1) { + raft_angle_interface = support_params.interface_angle; + } else if (slicing_params.interface_raft_layers == 1) { // Only the contact raft layer is non-empty, which will be printed as the 1st layer. - assert(m_slicing_params.base_raft_layers == 0); - assert(m_slicing_params.interface_raft_layers == 1); - assert(m_slicing_params.raft_layers() == 1 && raft_layers.size() == 0); + assert(slicing_params.base_raft_layers == 0); + assert(slicing_params.interface_raft_layers == 1); + assert(slicing_params.raft_layers() == 1 && raft_layers.size() == 0); } else { // No raft. - assert(m_slicing_params.base_raft_layers == 0); - assert(m_slicing_params.interface_raft_layers == 0); - assert(m_slicing_params.raft_layers() == 0 && raft_layers.size() == 0); + assert(slicing_params.base_raft_layers == 0); + assert(slicing_params.interface_raft_layers == 0); + assert(slicing_params.raft_layers() == 0 && raft_layers.size() == 0); } // Insert the raft base layers. - size_t n_raft_layers = size_t(std::max(0, int(m_slicing_params.raft_layers()) - 1)); + size_t n_raft_layers = size_t(std::max(0, int(slicing_params.raft_layers()) - 1)); tbb::parallel_for(tbb::blocked_range(0, n_raft_layers), - [this, &support_layers, &raft_layers, + [&support_layers, &raft_layers, &config, &support_params, &slicing_params, &bbox_object, raft_angle_1st_layer, raft_angle_base, raft_angle_interface, link_max_length_factor] (const tbb::blocked_range& range) { for (size_t support_layer_id = range.begin(); support_layer_id < range.end(); ++ support_layer_id) @@ -4424,55 +4684,55 @@ void PrintObjectSupportMaterial::generate_toolpaths( assert(support_layer_id < raft_layers.size()); SupportLayer &support_layer = *support_layers[support_layer_id]; assert(support_layer.support_fills.entities.empty()); - MyLayer &raft_layer = *raft_layers[support_layer_id]; + SupportGeneratorLayer &raft_layer = *raft_layers[support_layer_id]; - std::unique_ptr filler_interface = std::unique_ptr(Fill::new_from_type(m_support_params.interface_fill_pattern)); - std::unique_ptr filler_support = std::unique_ptr(Fill::new_from_type(m_support_params.base_fill_pattern)); + std::unique_ptr filler_interface = std::unique_ptr(Fill::new_from_type(support_params.interface_fill_pattern)); + std::unique_ptr filler_support = std::unique_ptr(Fill::new_from_type(support_params.base_fill_pattern)); filler_interface->set_bounding_box(bbox_object); filler_support->set_bounding_box(bbox_object); // Print the support base below the support columns, or the support base for the support columns plus the contacts. if (support_layer_id > 0) { - const Polygons &to_infill_polygons = (support_layer_id < m_slicing_params.base_raft_layers) ? + const Polygons &to_infill_polygons = (support_layer_id < slicing_params.base_raft_layers) ? raft_layer.polygons : //FIXME misusing contact_polygons for support columns. ((raft_layer.contact_polygons == nullptr) ? Polygons() : *raft_layer.contact_polygons); if (! to_infill_polygons.empty()) { assert(! raft_layer.bridging); - Flow flow(float(m_support_params.support_material_flow.width()), float(raft_layer.height), m_support_params.support_material_flow.nozzle_diameter()); + Flow flow(float(support_params.support_material_flow.width()), float(raft_layer.height), support_params.support_material_flow.nozzle_diameter()); Fill * filler = filler_support.get(); filler->angle = raft_angle_base; - filler->spacing = m_support_params.support_material_flow.spacing(); - filler->link_max_length = coord_t(scale_(filler->spacing * link_max_length_factor / m_support_params.support_density)); + filler->spacing = support_params.support_material_flow.spacing(); + filler->link_max_length = coord_t(scale_(filler->spacing * link_max_length_factor / support_params.support_density)); fill_expolygons_with_sheath_generate_paths( // Destination support_layer.support_fills.entities, // Regions to fill to_infill_polygons, // Filler and its parameters - filler, float(m_support_params.support_density), + filler, float(support_params.support_density), // Extrusion parameters erSupportMaterial, flow, - m_support_params.with_sheath, false); + support_params.with_sheath, false); } } Fill *filler = filler_interface.get(); - Flow flow = m_support_params.first_layer_flow; + Flow flow = support_params.first_layer_flow; float density = 0.f; if (support_layer_id == 0) { // Base flange. filler->angle = raft_angle_1st_layer; - filler->spacing = m_support_params.first_layer_flow.spacing(); - density = float(m_object_config->raft_first_layer_density.value * 0.01); - } else if (support_layer_id >= m_slicing_params.base_raft_layers) { + filler->spacing = support_params.first_layer_flow.spacing(); + density = float(config.raft_first_layer_density.value * 0.01); + } else if (support_layer_id >= slicing_params.base_raft_layers) { filler->angle = raft_angle_interface; // We don't use $base_flow->spacing because we need a constant spacing // value that guarantees that all layers are correctly aligned. - filler->spacing = m_support_params.support_material_flow.spacing(); + filler->spacing = support_params.support_material_flow.spacing(); assert(! raft_layer.bridging); - flow = Flow(float(m_support_params.support_material_interface_flow.width()), float(raft_layer.height), m_support_params.support_material_flow.nozzle_diameter()); - density = float(m_support_params.interface_density); + flow = Flow(float(support_params.support_material_interface_flow.width()), float(raft_layer.height), support_params.support_material_flow.nozzle_diameter()); + density = float(support_params.interface_density); } else continue; filler->link_max_length = coord_t(scale_(filler->spacing * link_max_length_factor / density)); @@ -4484,27 +4744,27 @@ void PrintObjectSupportMaterial::generate_toolpaths( // Filler and its parameters filler, density, // Extrusion parameters - (support_layer_id < m_slicing_params.base_raft_layers) ? erSupportMaterial : erSupportMaterialInterface, flow, + (support_layer_id < slicing_params.base_raft_layers) ? erSupportMaterial : erSupportMaterialInterface, flow, // sheath at first layer support_layer_id == 0, support_layer_id == 0); } }); struct LayerCacheItem { - LayerCacheItem(MyLayerExtruded *layer_extruded = nullptr) : layer_extruded(layer_extruded) {} - MyLayerExtruded *layer_extruded; - std::vector overlapping; + LayerCacheItem(SupportGeneratorLayerExtruded *layer_extruded = nullptr) : layer_extruded(layer_extruded) {} + SupportGeneratorLayerExtruded *layer_extruded; + std::vector overlapping; }; struct LayerCache { - MyLayerExtruded bottom_contact_layer; - MyLayerExtruded top_contact_layer; - MyLayerExtruded base_layer; - MyLayerExtruded interface_layer; - MyLayerExtruded base_interface_layer; + SupportGeneratorLayerExtruded bottom_contact_layer; + SupportGeneratorLayerExtruded top_contact_layer; + SupportGeneratorLayerExtruded base_layer; + SupportGeneratorLayerExtruded interface_layer; + SupportGeneratorLayerExtruded base_interface_layer; boost::container::static_vector nonempty; void add_nonempty_and_sort() { - for (MyLayerExtruded *item : { &bottom_contact_layer, &top_contact_layer, &interface_layer, &base_interface_layer, &base_layer }) + for (SupportGeneratorLayerExtruded *item : { &bottom_contact_layer, &top_contact_layer, &interface_layer, &base_interface_layer, &base_layer }) if (! item->empty()) this->nonempty.emplace_back(item); // Sort the layers with the same print_z coordinate by their heights, thickest first. @@ -4514,7 +4774,7 @@ void PrintObjectSupportMaterial::generate_toolpaths( std::vector layer_caches(support_layers.size()); tbb::parallel_for(tbb::blocked_range(n_raft_layers, support_layers.size()), - [this, &support_layers, &bottom_contacts, &top_contacts, &intermediate_layers, &interface_layers, &base_interface_layers, &layer_caches, &loop_interface_processor, + [&object, &config, &support_params, &slicing_params, &support_layers, &bottom_contacts, &top_contacts, &intermediate_layers, &interface_layers, &base_interface_layers, &layer_caches, &loop_interface_processor, &bbox_object, &angles, link_max_length_factor] (const tbb::blocked_range& range) { // Indices of the 1st layer in their respective container at the support layer height. @@ -4525,15 +4785,15 @@ void PrintObjectSupportMaterial::generate_toolpaths( size_t idx_layer_base_interface = size_t(-1); // BBS const auto fill_type_first_layer = ipConcentric; - auto filler_interface = std::unique_ptr(Fill::new_from_type(m_support_params.contact_fill_pattern)); + auto filler_interface = std::unique_ptr(Fill::new_from_type(support_params.contact_fill_pattern)); // Filler for the 1st layer interface, if different from filler_interface. - auto filler_first_layer_ptr = std::unique_ptr(range.begin() == 0 && m_support_params.contact_fill_pattern != fill_type_first_layer ? Fill::new_from_type(fill_type_first_layer) : nullptr); + auto filler_first_layer_ptr = std::unique_ptr(range.begin() == 0 && support_params.contact_fill_pattern != fill_type_first_layer ? Fill::new_from_type(fill_type_first_layer) : nullptr); // Pointer to the 1st layer interface filler. auto filler_first_layer = filler_first_layer_ptr ? filler_first_layer_ptr.get() : filler_interface.get(); // Filler for the base interface (to be used for soluble interface / non soluble base, to produce non soluble interface layer below soluble interface layer). auto filler_base_interface = std::unique_ptr(base_interface_layers.empty() ? nullptr : - Fill::new_from_type(m_support_params.interface_density > 0.95 || m_support_params.with_sheath ? ipRectilinear : ipSupportBase)); - auto filler_support = std::unique_ptr(Fill::new_from_type(m_support_params.base_fill_pattern)); + Fill::new_from_type(support_params.interface_density > 0.95 || support_params.with_sheath ? ipRectilinear : ipSupportBase)); + auto filler_support = std::unique_ptr(Fill::new_from_type(support_params.base_fill_pattern)); filler_interface->set_bounding_box(bbox_object); if (filler_first_layer_ptr) filler_first_layer_ptr->set_bounding_box(bbox_object); @@ -4544,19 +4804,19 @@ void PrintObjectSupportMaterial::generate_toolpaths( { SupportLayer &support_layer = *support_layers[support_layer_id]; LayerCache &layer_cache = layer_caches[support_layer_id]; - float interface_angle_delta = m_object_config->support_style.value == smsSnug ? + float interface_angle_delta = config.support_style.value == smsSnug || is_tree(config.support_type.value) ? (support_layer.interface_id() & 1) ? float(- M_PI / 4.) : float(+ M_PI / 4.) : 0; // Find polygons with the same print_z. - MyLayerExtruded &bottom_contact_layer = layer_cache.bottom_contact_layer; - MyLayerExtruded &top_contact_layer = layer_cache.top_contact_layer; - MyLayerExtruded &base_layer = layer_cache.base_layer; - MyLayerExtruded &interface_layer = layer_cache.interface_layer; - MyLayerExtruded &base_interface_layer = layer_cache.base_interface_layer; + SupportGeneratorLayerExtruded &bottom_contact_layer = layer_cache.bottom_contact_layer; + SupportGeneratorLayerExtruded &top_contact_layer = layer_cache.top_contact_layer; + SupportGeneratorLayerExtruded &base_layer = layer_cache.base_layer; + SupportGeneratorLayerExtruded &interface_layer = layer_cache.interface_layer; + SupportGeneratorLayerExtruded &base_interface_layer = layer_cache.base_interface_layer; // Increment the layer indices to find a layer at support_layer.print_z. { - auto fun = [&support_layer](const MyLayer *l){ return l->print_z >= support_layer.print_z - EPSILON; }; + auto fun = [&support_layer](const SupportGeneratorLayer *l){ return l->print_z >= support_layer.print_z - EPSILON; }; idx_layer_bottom_contact = idx_higher_or_equal(bottom_contacts, idx_layer_bottom_contact, fun); idx_layer_top_contact = idx_higher_or_equal(top_contacts, idx_layer_top_contact, fun); idx_layer_intermediate = idx_higher_or_equal(intermediate_layers, idx_layer_intermediate, fun); @@ -4575,23 +4835,23 @@ void PrintObjectSupportMaterial::generate_toolpaths( if (idx_layer_intermediate < intermediate_layers.size() && intermediate_layers[idx_layer_intermediate]->print_z < support_layer.print_z + EPSILON) base_layer.layer = intermediate_layers[idx_layer_intermediate]; - if (m_object_config->support_interface_top_layers == 0) { + if (config.support_interface_top_layers == 0) { // If no top interface layers were requested, we treat the contact layer exactly as a generic base layer. - if (m_support_params.can_merge_support_regions) { + if (support_params.can_merge_support_regions) { if (base_layer.could_merge(top_contact_layer)) base_layer.merge(std::move(top_contact_layer)); else if (base_layer.empty()) base_layer = std::move(top_contact_layer); } } else { - loop_interface_processor.generate(top_contact_layer, m_support_params.support_material_interface_flow); + loop_interface_processor.generate(top_contact_layer, support_params.support_material_interface_flow); // If no loops are allowed, we treat the contact layer exactly as a generic interface layer. // Merge interface_layer into top_contact_layer, as the top_contact_layer is not synchronized and therefore it will be used // to trim other layers. if (top_contact_layer.could_merge(interface_layer)) top_contact_layer.merge(std::move(interface_layer)); } - if ((m_object_config->support_interface_top_layers == 0 || m_object_config->support_interface_bottom_layers == 0) && m_support_params.can_merge_support_regions) { + if ((config.support_interface_top_layers == 0 || config.support_interface_bottom_layers == 0) && support_params.can_merge_support_regions) { if (base_layer.could_merge(bottom_contact_layer)) base_layer.merge(std::move(bottom_contact_layer)); else if (base_layer.empty() && ! bottom_contact_layer.empty() && ! bottom_contact_layer.layer->bridging) @@ -4618,11 +4878,11 @@ void PrintObjectSupportMaterial::generate_toolpaths( do { // Currently only works when thick_bridges is off - if (m_object->config().thick_bridges) + if (config.thick_bridges) break; - coordf_t object_layer_bottom_z = support_layer.print_z + m_slicing_params.gap_support_object; - const Layer* object_layer = m_object->get_layer_at_bottomz(object_layer_bottom_z, 10.0 * EPSILON); + coordf_t object_layer_bottom_z = support_layer.print_z + slicing_params.gap_support_object; + const Layer* object_layer = object.get_layer_at_bottomz(object_layer_bottom_z, 10.0 * EPSILON); if (object_layer == nullptr) break; @@ -4649,28 +4909,28 @@ void PrintObjectSupportMaterial::generate_toolpaths( // Top and bottom contacts, interface layers. for (size_t i = 0; i < 3; ++ i) { - MyLayerExtruded &layer_ex = (i == 0) ? top_contact_layer : (i == 1 ? bottom_contact_layer : interface_layer); + SupportGeneratorLayerExtruded &layer_ex = (i == 0) ? top_contact_layer : (i == 1 ? bottom_contact_layer : interface_layer); if (layer_ex.empty() || layer_ex.polygons_to_extrude().empty()) continue; - bool interface_as_base = m_object_config->support_interface_top_layers.value == 0 || - (m_object_config->support_interface_bottom_layers == 0 && &layer_ex == &bottom_contact_layer); + bool interface_as_base = config.support_interface_top_layers.value == 0 || + (config.support_interface_bottom_layers == 0 && &layer_ex == &bottom_contact_layer); //FIXME Bottom interfaces are extruded with the briding flow. Some bridging layers have its height slightly reduced, therefore // the bridging flow does not quite apply. Reduce the flow to area of an ellipse? (A = pi * a * b) Flow interface_flow; if (layer_ex.layer->bridging) - interface_flow = Flow::bridging_flow(layer_ex.layer->height, m_support_params.support_material_bottom_interface_flow.nozzle_diameter()); + interface_flow = Flow::bridging_flow(layer_ex.layer->height, support_params.support_material_bottom_interface_flow.nozzle_diameter()); else if (layer_ex.layer->bottom_z < EPSILON) { - interface_flow = m_support_params.first_layer_flow; + interface_flow = support_params.first_layer_flow; }else - interface_flow = (interface_as_base ? &m_support_params.support_material_flow : &m_support_params.support_material_interface_flow)->with_height(float(layer_ex.layer->height)); + interface_flow = (interface_as_base ? &support_params.support_material_flow : &support_params.support_material_interface_flow)->with_height(float(layer_ex.layer->height)); filler_interface->angle = interface_as_base ? // If zero interface layers are configured, use the same angle as for the base layers. angles[support_layer_id % angles.size()] : // Use interface angle for the interface layers. - m_support_params.interface_angle + interface_angle_delta; + support_params.interface_angle + interface_angle_delta; // BBS - bool can_adjust_top_interface_angle = (m_object_config->support_interface_top_layers.value > 1 && &layer_ex == &top_contact_layer); + bool can_adjust_top_interface_angle = (config.support_interface_top_layers.value > 1 && &layer_ex == &top_contact_layer); if (can_adjust_top_interface_angle && angle_of_biggest_bridge >= 0.f) { int bridge_degree = (int)Geometry::rad2deg(angle_of_biggest_bridge); int support_intf_degree = (int)Geometry::rad2deg(filler_interface->angle); @@ -4685,18 +4945,18 @@ void PrintObjectSupportMaterial::generate_toolpaths( } } } - double density = interface_as_base ? m_support_params.support_density : m_support_params.interface_density; - filler_interface->spacing = interface_as_base ? m_support_params.support_material_flow.spacing() : m_support_params.support_material_interface_flow.spacing(); + double density = interface_as_base ? support_params.support_density : support_params.interface_density; + filler_interface->spacing = interface_as_base ? support_params.support_material_flow.spacing() : support_params.support_material_interface_flow.spacing(); filler_interface->link_max_length = coord_t(scale_(filler_interface->spacing * link_max_length_factor / density)); // BBS support more interface patterns FillParams fill_params; fill_params.density = density; fill_params.dont_adjust = true; - if (m_object_config->support_interface_pattern == smipGrid) { - filler_interface->angle = Geometry::deg2rad(m_support_params.base_angle); + if (config.support_interface_pattern == smipGrid) { + filler_interface->angle = Geometry::deg2rad(support_params.base_angle); fill_params.dont_sort = true; } - if (m_object_config->support_interface_pattern == smipRectilinearInterlaced) + if (config.support_interface_pattern == smipRectilinearInterlaced) filler_interface->layer_id = support_layer.interface_id(); fill_expolygons_generate_paths( // Destination @@ -4715,10 +4975,10 @@ void PrintObjectSupportMaterial::generate_toolpaths( //FIXME Bottom interfaces are extruded with the briding flow. Some bridging layers have its height slightly reduced, therefore // the bridging flow does not quite apply. Reduce the flow to area of an ellipse? (A = pi * a * b) assert(! base_interface_layer.layer->bridging); - Flow interface_flow = m_support_params.support_material_flow.with_height(float(base_interface_layer.layer->height)); - filler->angle = m_support_params.interface_angle + interface_angle_delta; - filler->spacing = m_support_params.support_material_interface_flow.spacing(); - filler->link_max_length = coord_t(scale_(filler->spacing * link_max_length_factor / m_support_params.interface_density)); + Flow interface_flow = support_params.support_material_flow.with_height(float(base_interface_layer.layer->height)); + filler->angle = support_params.interface_angle + interface_angle_delta; + filler->spacing = support_params.support_material_interface_flow.spacing(); + filler->link_max_length = coord_t(scale_(filler->spacing * link_max_length_factor / support_params.interface_density)); fill_expolygons_generate_paths( // Destination base_interface_layer.extrusions, @@ -4726,7 +4986,7 @@ void PrintObjectSupportMaterial::generate_toolpaths( // Regions to fill union_safety_offset_ex(base_interface_layer.polygons_to_extrude()), // Filler and its parameters - filler, float(m_support_params.interface_density), + filler, float(support_params.interface_density), // Extrusion parameters erSupportMaterial, interface_flow); } @@ -4738,11 +4998,11 @@ void PrintObjectSupportMaterial::generate_toolpaths( // We don't use $base_flow->spacing because we need a constant spacing // value that guarantees that all layers are correctly aligned. assert(! base_layer.layer->bridging); - auto flow = m_support_params.support_material_flow.with_height(float(base_layer.layer->height)); - filler->spacing = m_support_params.support_material_flow.spacing(); - filler->link_max_length = coord_t(scale_(filler->spacing * link_max_length_factor / m_support_params.support_density)); - float density = float(m_support_params.support_density); - bool sheath = m_support_params.with_sheath; + auto flow = support_params.support_material_flow.with_height(float(base_layer.layer->height)); + filler->spacing = support_params.support_material_flow.spacing(); + filler->link_max_length = coord_t(scale_(filler->spacing * link_max_length_factor / support_params.support_density)); + float density = float(support_params.support_density); + bool sheath = support_params.with_sheath; bool no_sort = false; if (base_layer.layer->bottom_z < EPSILON) { // Base flange (the 1st layer). @@ -4750,8 +5010,8 @@ void PrintObjectSupportMaterial::generate_toolpaths( // BBS: the 1st layer use the same fill direction as other layers(in rectilinear) to avoid // that 2nd layer detaches from the 1st layer. //filler->angle = Geometry::deg2rad(float(m_object_config->support_angle.value + 90.)); - density = float(m_object_config->raft_first_layer_density.value * 0.01); - flow = m_support_params.first_layer_flow; + density = float(config.raft_first_layer_density.value * 0.01); + flow = support_params.first_layer_flow; // use the proper spacing for first layer as we don't need to align // its pattern to the other layers //FIXME When paralellizing, each thread shall have its own copy of the fillers. @@ -4798,12 +5058,12 @@ void PrintObjectSupportMaterial::generate_toolpaths( // Collect overlapping top/bottom surfaces. layer_cache_item.overlapping.reserve(20); coordf_t bottom_z = layer_cache_item.layer_extruded->layer->bottom_print_z() + EPSILON; - auto add_overlapping = [&layer_cache_item, bottom_z](const MyLayersPtr &layers, size_t idx_top) { + auto add_overlapping = [&layer_cache_item, bottom_z](const SupportGeneratorLayersPtr &layers, size_t idx_top) { for (int i = int(idx_top) - 1; i >= 0 && layers[i]->print_z > bottom_z; -- i) layer_cache_item.overlapping.push_back(layers[i]); }; add_overlapping(top_contacts, idx_layer_top_contact); - if (layer_cache_item.layer_extruded->layer->layer_type == sltBottomContact) { + if (layer_cache_item.layer_extruded->layer->layer_type == SupporLayerType::sltBottomContact) { // Bottom contact layer may overlap with a base layer, which may be changed to interface layer. add_overlapping(intermediate_layers, idx_layer_intermediate); add_overlapping(interface_layers, idx_layer_interface); @@ -4851,7 +5111,7 @@ void PrintObjectSupportMaterial::generate_toolpaths( } }; for (const SupportLayer *support_layer : support_layers) - assert(Test::verify_nonempty(&support_layer->support_fills)); + assert(Test::verify_nonempty(&support_layer->support_fills)); #endif // NDEBUG } diff --git a/src/libslic3r/SupportMaterial.hpp b/src/libslic3r/SupportMaterial.hpp index 861126fcb..870aaa57b 100644 --- a/src/libslic3r/SupportMaterial.hpp +++ b/src/libslic3r/SupportMaterial.hpp @@ -4,6 +4,7 @@ #include "Flow.hpp" #include "PrintConfig.hpp" #include "Slicing.hpp" +#include "Fill/FillBase.hpp" namespace Slic3r { @@ -11,148 +12,239 @@ class PrintObject; class PrintConfig; class PrintObjectConfig; +// Support layer type to be used by MyLayer. This type carries a much more detailed information +// about the support layer type than the final support layers stored in a PrintObject. +enum SupporLayerType { + sltUnknown = 0, + // Ratft base layer, to be printed with the support material. + sltRaftBase, + // Raft interface layer, to be printed with the support interface material. + sltRaftInterface, + // Bottom contact layer placed over a top surface of an object. To be printed with a support interface material. + sltBottomContact, + // Dense interface layer, to be printed with the support interface material. + // This layer is separated from an object by an sltBottomContact layer. + sltBottomInterface, + // Sparse base support layer, to be printed with a support material. + sltBase, + // Dense interface layer, to be printed with the support interface material. + // This layer is separated from an object with sltTopContact layer. + sltTopInterface, + // Top contact layer directly supporting an overhang. To be printed with a support interface material. + sltTopContact, + // Some undecided type yet. It will turn into sltBase first, then it may turn into sltBottomInterface or sltTopInterface. + sltIntermediate, +}; + +// A support layer type used internally by the SupportMaterial class. This class carries a much more detailed +// information about the support layer than the layers stored in the PrintObject, mainly +// the SupportGeneratorLayer is aware of the bridging flow and the interface gaps between the object and the support. +// This is from the old "MyLayer". +class SupportGeneratorLayer +{ +public: + void reset() { + *this = SupportGeneratorLayer(); + } + + bool operator==(const SupportGeneratorLayer &layer2) const { + return print_z == layer2.print_z && height == layer2.height && bridging == layer2.bridging; + } + + // Order the layers by lexicographically by an increasing print_z and a decreasing layer height. + bool operator<(const SupportGeneratorLayer &layer2) const { + if (print_z < layer2.print_z) { + return true; + } else if (print_z == layer2.print_z) { + if (height > layer2.height) + return true; + else if (height == layer2.height) { + // Bridging layers first. + return bridging && ! layer2.bridging; + } else + return false; + } else + return false; + } + + void merge(SupportGeneratorLayer &&rhs) { + // The union_() does not support move semantic yet, but maybe one day it will. + this->polygons = union_(this->polygons, std::move(rhs.polygons)); + auto merge = [](std::unique_ptr &dst, std::unique_ptr &src) { + if (! dst || dst->empty()) + dst = std::move(src); + else if (src && ! src->empty()) + *dst = union_(*dst, std::move(*src)); + }; + merge(this->contact_polygons, rhs.contact_polygons); + merge(this->overhang_polygons, rhs.overhang_polygons); + merge(this->enforcer_polygons, rhs.enforcer_polygons); + rhs.reset(); + } + + // For the bridging flow, bottom_print_z will be above bottom_z to account for the vertical separation. + // For the non-bridging flow, bottom_print_z will be equal to bottom_z. + coordf_t bottom_print_z() const { return print_z - height; } + + // To sort the extremes of top / bottom interface layers. + coordf_t extreme_z() const { return (this->layer_type == SupporLayerType::sltTopContact) ? this->bottom_z : this->print_z; } + + SupporLayerType layer_type { SupporLayerType::sltUnknown }; + // Z used for printing, in unscaled coordinates. + coordf_t print_z { 0 }; + // Bottom Z of this layer. For soluble layers, bottom_z + height = print_z, + // otherwise bottom_z + gap + height = print_z. + coordf_t bottom_z { 0 }; + // Layer height in unscaled coordinates. + coordf_t height { 0 }; + // Index of a PrintObject layer_id supported by this layer. This will be set for top contact layers. + // If this is not a contact layer, it will be set to size_t(-1). + size_t idx_object_layer_above { size_t(-1) }; + // Index of a PrintObject layer_id, which supports this layer. This will be set for bottom contact layers. + // If this is not a contact layer, it will be set to size_t(-1). + size_t idx_object_layer_below { size_t(-1) }; + // Use a bridging flow when printing this support layer. + bool bridging { false }; + + // Polygons to be filled by the support pattern. + Polygons polygons; + // Currently for the contact layers only. + std::unique_ptr contact_polygons; + std::unique_ptr overhang_polygons; + // Enforcers need to be propagated independently in case the "support on build plate only" option is enabled. + std::unique_ptr enforcer_polygons; +}; + +// Layers are allocated and owned by a deque. Once a layer is allocated, it is maintained +// up to the end of a generate() method. The layer storage may be replaced by an allocator class in the future, +// which would allocate layers by multiple chunks. +using SupportGeneratorLayerStorage = std::deque; +using SupportGeneratorLayersPtr = std::vector; + +struct SupportParameters { + SupportParameters(const PrintObject &object); + Flow first_layer_flow; + Flow support_material_flow; + Flow support_material_interface_flow; + Flow support_material_bottom_interface_flow; + // Is merging of regions allowed? Could the interface & base support regions be printed with the same extruder? + bool can_merge_support_regions; + + coordf_t support_layer_height_min; + // coordf_t support_layer_height_max; + + coordf_t gap_xy; + + float base_angle; + float interface_angle; + coordf_t interface_spacing; + coordf_t support_expansion; + coordf_t interface_density; + coordf_t support_spacing; + coordf_t support_density; + + InfillPattern base_fill_pattern; + InfillPattern interface_fill_pattern; + InfillPattern contact_fill_pattern; + bool with_sheath; +}; + +using LayerIndex = int; + +inline double layer_z(const SlicingParameters& slicing_params, const size_t layer_idx) +{ + return slicing_params.object_print_z_min + slicing_params.first_object_layer_height + layer_idx * slicing_params.layer_height; +} +inline LayerIndex layer_idx_ceil(const SlicingParameters& slicing_params, const double z) +{ + return LayerIndex(ceil((z - slicing_params.object_print_z_min - slicing_params.first_object_layer_height) / slicing_params.layer_height)); +} +inline LayerIndex layer_idx_floor(const SlicingParameters& slicing_params, const double z) +{ + return LayerIndex(floor((z - slicing_params.object_print_z_min - slicing_params.first_object_layer_height) / slicing_params.layer_height)); +} + +inline SupportGeneratorLayer& layer_initialize( + SupportGeneratorLayer& layer_new, + const SupporLayerType layer_type, + const SlicingParameters& slicing_params, + const size_t layer_idx) +{ + layer_new.layer_type = layer_type; + layer_new.print_z = layer_z(slicing_params, layer_idx); + layer_new.height = layer_idx == 0 ? slicing_params.first_object_layer_height : slicing_params.layer_height; + layer_new.bottom_z = layer_idx == 0 ? slicing_params.object_print_z_min : layer_new.print_z - layer_new.height; + return layer_new; +} + +// Using the std::deque as an allocator. +inline SupportGeneratorLayer& layer_allocate( + std::deque& layer_storage, + SupporLayerType layer_type, + const SlicingParameters& slicing_params, + size_t layer_idx) +{ + //FIXME take raft into account. + layer_storage.push_back(SupportGeneratorLayer()); + return layer_initialize(layer_storage.back(), layer_type, slicing_params, layer_idx); +} + +// Generate raft layers, also expand the 1st support layer +// in case there is no raft layer to improve support adhesion. +SupportGeneratorLayersPtr generate_raft_base( + const PrintObject &object, + const SupportParameters &support_params, + const SlicingParameters &slicing_params, + const SupportGeneratorLayersPtr &top_contacts, + const SupportGeneratorLayersPtr &interface_layers, + const SupportGeneratorLayersPtr &base_interface_layers, + const SupportGeneratorLayersPtr &base_layers, + SupportGeneratorLayerStorage &layer_storage); + +// returns sorted layers +SupportGeneratorLayersPtr generate_support_layers( + PrintObject &object, + const SupportGeneratorLayersPtr &raft_layers, + const SupportGeneratorLayersPtr &bottom_contacts, + const SupportGeneratorLayersPtr &top_contacts, + const SupportGeneratorLayersPtr &intermediate_layers, + const SupportGeneratorLayersPtr &interface_layers, + const SupportGeneratorLayersPtr &base_interface_layers); + +// Produce the support G-code. +// Used by both classic and tree supports. +void generate_support_toolpaths( + PrintObject &object, + SupportLayerPtrs &support_layers, + const PrintObjectConfig &config, + const SupportParameters &support_params, + const SlicingParameters &slicing_params, + const SupportGeneratorLayersPtr &raft_layers, + const SupportGeneratorLayersPtr &bottom_contacts, + const SupportGeneratorLayersPtr &top_contacts, + const SupportGeneratorLayersPtr &intermediate_layers, + const SupportGeneratorLayersPtr &interface_layers, + const SupportGeneratorLayersPtr &base_interface_layers); + +void fill_expolygons_with_sheath_generate_paths( + ExtrusionEntitiesPtr& dst, + const Polygons& polygons, + Fill* filler, + float density, + ExtrusionRole role, + const Flow& flow, + bool with_sheath, + bool no_sort); + +void export_print_z_polygons_to_svg(const char *path, SupportGeneratorLayer ** const layers, size_t n_layers); +void export_print_z_polygons_and_extrusions_to_svg(const char *path, SupportGeneratorLayer ** const layers, size_t n_layers, SupportLayer& support_layer); + // This class manages raft and supports for a single PrintObject. // Instantiated by Slic3r::Print::Object->_support_material() // This class is instantiated before the slicing starts as Object.pm will query // the parameters of the raft to determine the 1st layer height and thickness. class PrintObjectSupportMaterial { -public: - // Support layer type to be used by MyLayer. This type carries a much more detailed information - // about the support layer type than the final support layers stored in a PrintObject. - enum SupporLayerType { - sltUnknown = 0, - // Ratft base layer, to be printed with the support material. - sltRaftBase, - // Raft interface layer, to be printed with the support interface material. - sltRaftInterface, - // Bottom contact layer placed over a top surface of an object. To be printed with a support interface material. - sltBottomContact, - // Dense interface layer, to be printed with the support interface material. - // This layer is separated from an object by an sltBottomContact layer. - sltBottomInterface, - // Sparse base support layer, to be printed with a support material. - sltBase, - // Dense interface layer, to be printed with the support interface material. - // This layer is separated from an object with sltTopContact layer. - sltTopInterface, - // Top contact layer directly supporting an overhang. To be printed with a support interface material. - sltTopContact, - // Some undecided type yet. It will turn into sltBase first, then it may turn into sltBottomInterface or sltTopInterface. - sltIntermediate, - }; - - // A support layer type used internally by the SupportMaterial class. This class carries a much more detailed - // information about the support layer than the layers stored in the PrintObject, mainly - // the MyLayer is aware of the bridging flow and the interface gaps between the object and the support. - class MyLayer - { - public: - void reset() { - *this = MyLayer(); - } - - bool operator==(const MyLayer &layer2) const { - return print_z == layer2.print_z && height == layer2.height && bridging == layer2.bridging; - } - - // Order the layers by lexicographically by an increasing print_z and a decreasing layer height. - bool operator<(const MyLayer &layer2) const { - if (print_z < layer2.print_z) { - return true; - } else if (print_z == layer2.print_z) { - if (height > layer2.height) - return true; - else if (height == layer2.height) { - // Bridging layers first. - return bridging && ! layer2.bridging; - } else - return false; - } else - return false; - } - - void merge(MyLayer &&rhs) { - // The union_() does not support move semantic yet, but maybe one day it will. - this->polygons = union_(this->polygons, std::move(rhs.polygons)); - auto merge = [](std::unique_ptr &dst, std::unique_ptr &src) { - if (! dst || dst->empty()) - dst = std::move(src); - else if (src && ! src->empty()) - *dst = union_(*dst, std::move(*src)); - }; - merge(this->contact_polygons, rhs.contact_polygons); - merge(this->overhang_polygons, rhs.overhang_polygons); - merge(this->enforcer_polygons, rhs.enforcer_polygons); - rhs.reset(); - } - - // For the bridging flow, bottom_print_z will be above bottom_z to account for the vertical separation. - // For the non-bridging flow, bottom_print_z will be equal to bottom_z. - coordf_t bottom_print_z() const { return print_z - height; } - - // To sort the extremes of top / bottom interface layers. - coordf_t extreme_z() const { return (this->layer_type == sltTopContact) ? this->bottom_z : this->print_z; } - - SupporLayerType layer_type { sltUnknown }; - // Z used for printing, in unscaled coordinates. - coordf_t print_z { 0 }; - // Bottom Z of this layer. For soluble layers, bottom_z + height = print_z, - // otherwise bottom_z + gap + height = print_z. - coordf_t bottom_z { 0 }; - // Layer height in unscaled coordinates. - coordf_t height { 0 }; - // Index of a PrintObject layer_id supported by this layer. This will be set for top contact layers. - // If this is not a contact layer, it will be set to size_t(-1). - size_t idx_object_layer_above { size_t(-1) }; - // Index of a PrintObject layer_id, which supports this layer. This will be set for bottom contact layers. - // If this is not a contact layer, it will be set to size_t(-1). - size_t idx_object_layer_below { size_t(-1) }; - // Use a bridging flow when printing this support layer. - bool bridging { false }; - - // Polygons to be filled by the support pattern. - Polygons polygons; - // Currently for the contact layers only. - std::unique_ptr contact_polygons; - std::unique_ptr overhang_polygons; - // Enforcers need to be propagated independently in case the "support on build plate only" option is enabled. - std::unique_ptr enforcer_polygons; - }; - - struct SupportParams { - Flow first_layer_flow; - Flow support_material_flow; - Flow support_material_interface_flow; - Flow support_material_bottom_interface_flow; - // Is merging of regions allowed? Could the interface & base support regions be printed with the same extruder? - bool can_merge_support_regions; - - coordf_t support_layer_height_min; - // coordf_t support_layer_height_max; - - coordf_t gap_xy; - - float base_angle; - float interface_angle; - coordf_t interface_spacing; - coordf_t support_expansion; - coordf_t interface_density; - coordf_t support_spacing; - coordf_t support_density; - - InfillPattern base_fill_pattern; - InfillPattern interface_fill_pattern; - InfillPattern contact_fill_pattern; - bool with_sheath; - }; - - // Layers are allocated and owned by a deque. Once a layer is allocated, it is maintained - // up to the end of a generate() method. The layer storage may be replaced by an allocator class in the future, - // which would allocate layers by multiple chunks. - typedef std::deque MyLayerStorage; - typedef std::vector MyLayersPtr; - public: PrintObjectSupportMaterial(const PrintObject *object, const SlicingParameters &slicing_params); @@ -176,58 +268,48 @@ private: // Generate top contact layers supporting overhangs. // For a soluble interface material synchronize the layer heights with the object, otherwise leave the layer height undefined. // If supports over bed surface only are requested, don't generate contact layers over an object. - MyLayersPtr top_contact_layers(const PrintObject &object, const std::vector &buildplate_covered, MyLayerStorage &layer_storage) const; + SupportGeneratorLayersPtr top_contact_layers(const PrintObject &object, const std::vector &buildplate_covered, SupportGeneratorLayerStorage &layer_storage) const; // Generate bottom contact layers supporting the top contact layers. // For a soluble interface material synchronize the layer heights with the object, // otherwise set the layer height to a bridging flow of a support interface nozzle. - MyLayersPtr bottom_contact_layers_and_layer_support_areas( - const PrintObject &object, const MyLayersPtr &top_contacts, std::vector &buildplate_covered, - MyLayerStorage &layer_storage, std::vector &layer_support_areas) const; + SupportGeneratorLayersPtr bottom_contact_layers_and_layer_support_areas( + const PrintObject &object, const SupportGeneratorLayersPtr &top_contacts, std::vector &buildplate_covered, + SupportGeneratorLayerStorage &layer_storage, std::vector &layer_support_areas) const; // Trim the top_contacts layers with the bottom_contacts layers if they overlap, so there would not be enough vertical space for both of them. - void trim_top_contacts_by_bottom_contacts(const PrintObject &object, const MyLayersPtr &bottom_contacts, MyLayersPtr &top_contacts) const; + void trim_top_contacts_by_bottom_contacts(const PrintObject &object, const SupportGeneratorLayersPtr &bottom_contacts, SupportGeneratorLayersPtr &top_contacts) const; // Generate raft layers and the intermediate support layers between the bottom contact and top contact surfaces. - MyLayersPtr raft_and_intermediate_support_layers( + SupportGeneratorLayersPtr raft_and_intermediate_support_layers( const PrintObject &object, - const MyLayersPtr &bottom_contacts, - const MyLayersPtr &top_contacts, - MyLayerStorage &layer_storage) const; + const SupportGeneratorLayersPtr &bottom_contacts, + const SupportGeneratorLayersPtr &top_contacts, + SupportGeneratorLayerStorage &layer_storage) const; // Fill in the base layers with polygons. void generate_base_layers( const PrintObject &object, - const MyLayersPtr &bottom_contacts, - const MyLayersPtr &top_contacts, - MyLayersPtr &intermediate_layers, + const SupportGeneratorLayersPtr &bottom_contacts, + const SupportGeneratorLayersPtr &top_contacts, + SupportGeneratorLayersPtr &intermediate_layers, const std::vector &layer_support_areas) const; - // Generate raft layers, also expand the 1st support layer - // in case there is no raft layer to improve support adhesion. - MyLayersPtr generate_raft_base( - const PrintObject &object, - const MyLayersPtr &top_contacts, - const MyLayersPtr &interface_layers, - const MyLayersPtr &base_interface_layers, - const MyLayersPtr &base_layers, - MyLayerStorage &layer_storage) const; - // Turn some of the base layers into base interface layers. // For soluble interfaces with non-soluble bases, print maximum two first interface layers with the base // extruder to improve adhesion of the soluble filament to the base. - std::pair generate_interface_layers( - const MyLayersPtr &bottom_contacts, - const MyLayersPtr &top_contacts, - MyLayersPtr &intermediate_layers, - MyLayerStorage &layer_storage) const; + std::pair generate_interface_layers( + const SupportGeneratorLayersPtr &bottom_contacts, + const SupportGeneratorLayersPtr &top_contacts, + SupportGeneratorLayersPtr &intermediate_layers, + SupportGeneratorLayerStorage &layer_storage) const; // Trim support layers by an object to leave a defined gap between // the support volume and the object. void trim_support_layers_by_object( const PrintObject &object, - MyLayersPtr &support_layers, + SupportGeneratorLayersPtr &support_layers, const coordf_t gap_extra_above, const coordf_t gap_extra_below, const coordf_t gap_xy) const; @@ -237,16 +319,6 @@ private: void clip_with_shape(); */ - // Produce the actual G-code. - void generate_toolpaths( - SupportLayerPtrs &support_layers, - const MyLayersPtr &raft_layers, - const MyLayersPtr &bottom_contacts, - const MyLayersPtr &top_contacts, - const MyLayersPtr &intermediate_layers, - const MyLayersPtr &interface_layers, - const MyLayersPtr &base_interface_layers) const; - // Following objects are not owned by SupportMaterial class. const PrintObject *m_object; const PrintConfig *m_print_config; @@ -255,7 +327,7 @@ private: // carrying information on a raft, 1st layer height, 1st object layer height, gap between the raft and object etc. SlicingParameters m_slicing_params; // Various precomputed support parameters to be shared with external functions. - SupportParams m_support_params; + SupportParameters m_support_params; }; } // namespace Slic3r diff --git a/src/libslic3r/TreeModelVolumes.cpp b/src/libslic3r/TreeModelVolumes.cpp new file mode 100644 index 000000000..c7f7bb063 --- /dev/null +++ b/src/libslic3r/TreeModelVolumes.cpp @@ -0,0 +1,895 @@ +// Tree supports by Thomas Rahm, losely based on Tree Supports by CuraEngine. +// Original source of Thomas Rahm's tree supports: +// https://github.com/ThomasRahm/CuraEngine +// +// Original CuraEngine copyright: +// Copyright (c) 2021 Ultimaker B.V. +// CuraEngine is released under the terms of the AGPLv3 or higher. + +#include "TreeModelVolumes.hpp" +#include "TreeSupport3D.hpp" + +#include "BuildVolume.hpp" +#include "ClipperUtils.hpp" +#include "Flow.hpp" +#include "Layer.hpp" +#include "Point.hpp" +#include "Print.hpp" +#include "PrintConfig.hpp" +#include "Utils.hpp" + +#include + +#include + +#include +#include + +namespace Slic3r::TreeSupport3D +{ + +using namespace std::literals; + +// or warning +// had to use a define beacuse the macro processing inside macro BOOST_LOG_TRIVIAL() +#define error_level_not_in_cache error + +TreeSupportMeshGroupSettings::TreeSupportMeshGroupSettings(const PrintObject &print_object) +{ + const PrintConfig &print_config = print_object.print()->config(); + const PrintObjectConfig &config = print_object.config(); + const SlicingParameters &slicing_params = print_object.slicing_parameters(); +// const std::vector printing_extruders = print_object.object_extruders(); + + // Support must be enabled and set to Tree style. + //assert(config.support_material); + //assert(config.support_material_style == smsTree || config.support_material_style == smsOrganic); + + // Calculate maximum external perimeter width over all printing regions, taking into account the default layer height. + coordf_t external_perimeter_width = 0.; + for (size_t region_id = 0; region_id < print_object.num_printing_regions(); ++ region_id) { + const PrintRegion ®ion = print_object.printing_region(region_id); + external_perimeter_width = std::max(external_perimeter_width, region.flow(print_object, frExternalPerimeter, config.layer_height).width()); + } + + this->layer_height = scaled(config.layer_height.value); + this->resolution = scaled(print_config.resolution.value); + // Arache feature + this->min_feature_size = scaled(config.min_feature_size.value); + // +1 makes the threshold inclusive + this->support_angle = 0.5 * M_PI - std::clamp((config.support_threshold_angle + 1) * M_PI / 180., 0., 0.5 * M_PI); + this->support_line_width = support_material_flow(&print_object, config.layer_height).scaled_width(); + this->support_roof_line_width = support_material_interface_flow(&print_object, config.layer_height).scaled_width(); + //FIXME add it to SlicingParameters and reuse in both tree and normal supports? + this->support_bottom_enable = config.support_interface_top_layers.value > 0 && config.support_interface_bottom_layers.value != 0; + this->support_bottom_height = this->support_bottom_enable ? + (config.support_interface_bottom_layers.value > 0 ? + config.support_interface_bottom_layers.value : + config.support_interface_top_layers.value) * this->layer_height : + 0; + this->support_material_buildplate_only = config.support_on_build_plate_only; + this->support_xy_distance = scaled(config.support_object_xy_distance.value); + // Separation of interfaces, it is likely smaller than support_xy_distance. + this->support_xy_distance_overhang = std::min(this->support_xy_distance, scaled(0.5 * external_perimeter_width)); + this->support_top_distance = scaled(slicing_params.gap_support_object); + this->support_bottom_distance = scaled(slicing_params.gap_object_support); +// this->support_interface_skip_height = +// this->support_infill_angles = + this->support_roof_enable = config.support_interface_top_layers.value > 0; + this->support_roof_height = config.support_interface_top_layers.value * this->layer_height; +// this->minimum_roof_area = +// this->support_roof_angles = + this->support_roof_pattern = config.support_interface_pattern; + this->support_pattern = config.support_base_pattern; + this->support_line_spacing = scaled(config.support_base_pattern_spacing.value); +// this->support_bottom_offset = +// this->support_wall_count = config.support_material_with_sheath ? 1 : 0; + this->support_wall_count = 1; + this->support_roof_line_distance = scaled(config.support_interface_spacing.value) + this->support_roof_line_width; +// this->minimum_support_area = +// this->minimum_bottom_area = +// this->support_offset = +// this->support_tree_branch_distance = 2.5 * line_width ?? + double support_tree_angle_slow = 25;// TODO add a setting? + double support_tree_branch_diameter_angle = 5; // TODO add a setting? + double tree_support_tip_diameter = 0.8; + this->support_tree_angle = std::clamp(config.tree_support_branch_angle * M_PI / 180., 0., 0.5 * M_PI - EPSILON); + this->support_tree_angle_slow = std::clamp(support_tree_angle_slow * M_PI / 180., 0., this->support_tree_angle - EPSILON); + this->support_tree_branch_diameter = scaled(config.tree_support_branch_diameter.value); + this->support_tree_branch_diameter_angle = std::clamp(support_tree_branch_diameter_angle * M_PI / 180., 0., 0.5 * M_PI - EPSILON); + this->support_tree_top_rate = 30; // percent +// this->support_tree_tip_diameter = this->support_line_width; + this->support_tree_tip_diameter = std::clamp(scaled(tree_support_tip_diameter), 0, this->support_tree_branch_diameter); +} + +//FIXME Machine border is currently ignored. +static Polygons calculateMachineBorderCollision(Polygon machine_border) +{ + // Put a border of 1m around the print volume so that we don't collide. +#if 1 + //FIXME just returning no border will let tree support legs collide with print bed boundary + return {}; +#else + //FIXME offsetting by 1000mm easily overflows int32_tr coordinate. + Polygons out = offset(machine_border, scaled(1000.), jtMiter, 1.2); + machine_border.reverse(); // Makes the polygon negative so that we subtract the actual volume from the collision area. + out.emplace_back(std::move(machine_border)); + return out; +#endif +} + +TreeModelVolumes::TreeModelVolumes( + const PrintObject &print_object, + const BuildVolume &build_volume, + const coord_t max_move, const coord_t max_move_slow, size_t current_mesh_idx, +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + double progress_multiplier, double progress_offset, +#endif // SLIC3R_TREESUPPORTS_PROGRESS + const std::vector& additional_excluded_areas) : + // -2 to avoid rounding errors + m_max_move{ std::max(max_move - 2, 0) }, m_max_move_slow{ std::max(max_move_slow - 2, 0) }, +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + m_progress_multiplier{ progress_multiplier }, m_progress_offset{ progress_offset }, +#endif // SLIC3R_TREESUPPORTS_PROGRESS + m_machine_border{ calculateMachineBorderCollision(build_volume.polygon()) } +{ + m_bed_area = build_volume.polygon(); +#if 0 + std::unordered_map mesh_to_layeroutline_idx; + for (size_t mesh_idx = 0; mesh_idx < storage.meshes.size(); ++ mesh_idx) { + SliceMeshStorage mesh = storage.meshes[mesh_idx]; + bool added = false; + for (size_t idx = 0; idx < m_layer_outlines.size(); ++ idx) + if (TreeSupport::TreeSupportSettings(m_layer_outlines[idx].first) == TreeSupport::TreeSupportSettings(mesh.settings)) { + added = true; + mesh_to_layeroutline_idx[mesh_idx] = idx; + } + if (! added) { + mesh_to_layeroutline_idx[mesh_idx] = m_layer_outlines.size(); + m_layer_outlines.emplace_back(mesh.settings, std::vector(storage.support.supportLayers.size(), Polygons())); + } + } + for (size_t idx = 0; idx < m_layer_outlines.size(); ++ idx) { + tbb::parallel_for(tbb::blocked_range(0, m_layer_outlines[idx].second.size()), + [&](const tbb::blocked_range &range) { + for (const size_t layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx) + m_layer_outlines[idx].second[layer_idx] = union_(m_layer_outlines[idx].second[layer_idx]); + }); + } + m_current_outline_idx = mesh_to_layeroutline_idx[current_mesh_idx]; + +#else + { + m_anti_overhang = print_object.slice_support_blockers(); + TreeSupportMeshGroupSettings mesh_settings(print_object); + m_layer_outlines.emplace_back(mesh_settings, std::vector{}); + m_current_outline_idx = 0; + std::vector &outlines = m_layer_outlines.front().second; + outlines.assign(print_object.layer_count(), Polygons{}); + tbb::parallel_for(tbb::blocked_range(0, print_object.layer_count()), + [&](const tbb::blocked_range &range) { + for (size_t layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx) + outlines[layer_idx] = to_polygons(expolygons_simplify(print_object.get_layer(layer_idx)->lslices, mesh_settings.resolution)); + }); + } +#endif + + m_support_rests_on_model = false; + m_min_resolution = std::numeric_limits::max(); + for (auto data_pair : m_layer_outlines) { + m_support_rests_on_model |= ! data_pair.first.support_material_buildplate_only; + m_min_resolution = std::min(m_min_resolution, data_pair.first.resolution); + } + + const TreeSupportSettings config{ m_layer_outlines[m_current_outline_idx].first }; + m_current_min_xy_dist = config.xy_min_distance; + m_current_min_xy_dist_delta = config.xy_distance - m_current_min_xy_dist; + assert(m_current_min_xy_dist_delta >= 0); + m_increase_until_radius = config.increase_radius_until_radius; + m_radius_0 = config.getRadius(0); + +#if 0 + for (size_t mesh_idx = 0; mesh_idx < storage.meshes.size(); mesh_idx++) { + SliceMeshStorage mesh = storage.meshes[mesh_idx]; + tbb::parallel_for(tbb::blocked_range(0, m_layer_outlines[mesh_to_layeroutline_idx[mesh_idx]].second.size()), + [&](const tbb::blocked_range &range) { + for (size_t layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx) + if (layer_idx < mesh.layer_nr_max_filled_layer) { + Polygons outline = extractOutlineFromMesh(mesh, layer_idx); + append(m_layer_outlines[mesh_to_layeroutline_idx[mesh_idx]].second[layer_idx], outline); + } + }); + } + if (! additional_excluded_areas.empty()) { + tbb::parallel_for(tbb::blocked_range(0, m_anti_overhang.size()), + [&](const tbb::blocked_range &range) { + for (size_t layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx) { + if (layer_idx < coord_t(additional_excluded_areas.size())) + append(m_anti_overhang[layer_idx], additional_excluded_areas[layer_idx]); + // if (SUPPORT_TREE_AVOID_SUPPORT_BLOCKER) + // append(m_anti_overhang[layer_idx], storage.support.supportLayers[layer_idx].anti_overhang); + //FIXME block wipe tower + // if (storage.primeTower.enabled) + // append(m_anti_overhang[layer_idx], layer_idx == 0 ? storage.primeTower.outer_poly_first_layer : storage.primeTower.outer_poly); + m_anti_overhang[layer_idx] = union_(m_anti_overhang[layer_idx]); + } + }); + } +#endif +} + +void TreeModelVolumes::precalculate(const coord_t max_layer, std::function throw_on_cancel) +{ + auto t_start = std::chrono::high_resolution_clock::now(); + m_precalculated = true; + + // Get the config corresponding to one mesh that is in the current group. Which one has to be irrelevant. + // Not the prettiest way to do this, but it ensures some calculations that may be a bit more complex + // like inital layer diameter are only done in once. + TreeSupportSettings config(m_layer_outlines[m_current_outline_idx].first); + + { + // calculate which radius each layer in the tip may have. + std::vector possible_tip_radiis; + for (size_t distance_to_top = 0; distance_to_top <= config.tip_layers; ++ distance_to_top) { + possible_tip_radiis.emplace_back(ceilRadius(config.getRadius(distance_to_top))); + possible_tip_radiis.emplace_back(ceilRadius(config.getRadius(distance_to_top) + m_current_min_xy_dist_delta)); + } + sort_remove_duplicates(possible_tip_radiis); + // It theoretically may happen in the tip, that the radius can change so much in-between 2 layers, + // that a ceil step is skipped (as in there is a radius r so that ceilRadius(radius(dtt)) radius_until_layer; + // while it is possible to calculate, up to which layer the avoidance should be calculated, this simulation is easier to understand, and does not need to be adjusted if something of the radius calculation is changed. + // Overhead with an assumed worst case of 6600 layers was about 2ms + for (LayerIndex distance_to_top = 0; distance_to_top <= max_layer; ++ distance_to_top) { + const LayerIndex current_layer = max_layer - distance_to_top; + auto update_radius_until_layer = [&radius_until_layer, current_layer](coord_t r) { + auto it = radius_until_layer.find(r); + if (it == radius_until_layer.end()) + radius_until_layer.emplace_hint(it, r, current_layer); + }; + // regular radius + update_radius_until_layer(ceilRadius(config.getRadius(distance_to_top, 0) + m_current_min_xy_dist_delta)); + // the maximum radius that the radius with the min_xy_dist can achieve + update_radius_until_layer(ceilRadius(config.getRadius(distance_to_top, 0))); + update_radius_until_layer(ceilRadius(config.recommendedMinRadius(current_layer) + m_current_min_xy_dist_delta)); + } + + throw_on_cancel(); + + // Copy to deque to use in parallel for later. + std::vector relevant_avoidance_radiis{ radius_until_layer.begin(), radius_until_layer.end() }; + + // Append additional radiis needed for collision. + // To calculate collision holefree for every radius, the collision of radius m_increase_until_radius will be required. + radius_until_layer[ceilRadius(m_increase_until_radius + m_current_min_xy_dist_delta)] = max_layer; + // Collision for radius 0 needs to be calculated everywhere, as it will be used to ensure valid xy_distance in drawAreas. + radius_until_layer[0] = max_layer; + if (m_current_min_xy_dist_delta != 0) + radius_until_layer[m_current_min_xy_dist_delta] = max_layer; + + // Now that required_avoidance_limit contains the maximum of ild and regular required radius just copy. + std::vector relevant_collision_radiis{ radius_until_layer.begin(), radius_until_layer.end() }; + + // Calculate the relevant collisions + calculateCollision(relevant_collision_radiis, throw_on_cancel); + + // calculate a separate Collisions with all holes removed. These are relevant for some avoidances that try to avoid holes (called safe) + std::vector relevant_hole_collision_radiis; + for (RadiusLayerPair key : relevant_avoidance_radiis) + if (key.first < m_increase_until_radius + m_current_min_xy_dist_delta) + relevant_hole_collision_radiis.emplace_back(key); + + // Calculate collisions without holes, built from regular collision + calculateCollisionHolefree(relevant_hole_collision_radiis, throw_on_cancel); + // Let placables be calculated from calculateAvoidance() for better parallelization. + if (m_support_rests_on_model) + calculatePlaceables(relevant_avoidance_radiis, throw_on_cancel); + + auto t_coll = std::chrono::high_resolution_clock::now(); + + // Calculate the relevant avoidances in parallel as far as possible + { + tbb::task_group task_group; + task_group.run([this, relevant_avoidance_radiis, throw_on_cancel]{ calculateAvoidance(relevant_avoidance_radiis, true, m_support_rests_on_model, throw_on_cancel); }); + task_group.run([this, relevant_avoidance_radiis, throw_on_cancel]{ calculateWallRestrictions(relevant_avoidance_radiis, throw_on_cancel); }); + task_group.wait(); + } + auto t_end = std::chrono::high_resolution_clock::now(); + auto dur_col = 0.001 * std::chrono::duration_cast(t_coll - t_start).count(); + auto dur_avo = 0.001 * std::chrono::duration_cast(t_end - t_coll).count(); + +// m_precalculated = true; + BOOST_LOG_TRIVIAL(info) << "Precalculating collision took" << dur_col << " ms. Precalculating avoidance took " << dur_avo << " ms."; + +#if 0 + // Paint caches into SVGs: + auto paint_cache_into_SVGs = [this](const RadiusLayerPolygonCache &cache, std::string_view name) { + const std::vector>> sorted = cache.sorted(); + static constexpr const std::string_view colors[] = { + "red", "green", "blue", "magenta", "orange" + }; + static constexpr const size_t num_colors = sizeof(colors) / sizeof(colors[0]); + for (size_t i = 0; i < sorted.size();) { + // Find range of cache items with the same layer index. + size_t j = i; + for (++ j; j < sorted.size() && sorted[i].first.second == sorted[j].first.second; ++ j) ; + // Collect expolygons in reverse order (largest to smallest). + std::vector> expolygons_with_attributes; + for (int k = int(j - 1); k >= int(i); -- k) { + std::string legend = format("radius-%1%", unscaled(sorted[k].first.first)); + expolygons_with_attributes.push_back({ union_ex(sorted[k].second), SVG::ExPolygonAttributes(legend, std::string(colors[(k - int(i)) % num_colors]), 1.) }); + } + // Render the range of per radius collision polygons into a common SVG. + SVG::export_expolygons(debug_out_path("treesupport_cache-%s-%d.svg", name.data(), sorted[i].first.second), expolygons_with_attributes); + i = j; + } + }; + paint_cache_into_SVGs(m_collision_cache, "collision_cache"); + paint_cache_into_SVGs(m_collision_cache_holefree, "collision_cache_holefree"); + paint_cache_into_SVGs(m_avoidance_cache, "avoidance_cache"); + paint_cache_into_SVGs(m_avoidance_cache_slow, "avoidance_cache_slow"); + paint_cache_into_SVGs(m_avoidance_cache_to_model, "avoidance_cache_to_model"); + paint_cache_into_SVGs(m_avoidance_cache_to_model_slow, "avoidance_cache_to_model_slow"); + paint_cache_into_SVGs(m_placeable_areas_cache, "placable_areas_cache"); + paint_cache_into_SVGs(m_avoidance_cache_holefree, "avoidance_cache_holefree"); + paint_cache_into_SVGs(m_avoidance_cache_holefree_to_model, "avoidance_cache_holefree_to_model"); + paint_cache_into_SVGs(m_wall_restrictions_cache, "wall_restrictions_cache"); + paint_cache_into_SVGs(m_wall_restrictions_cache_min, "wall_restrictions_cache_min"); +#endif +} + +const Polygons& TreeModelVolumes::getCollision(const coord_t orig_radius, LayerIndex layer_idx, bool min_xy_dist) const +{ + const coord_t radius = this->ceilRadius(orig_radius, min_xy_dist); + if (std::optional> result = m_collision_cache.getArea({ radius, layer_idx }); result) + return (*result).get(); + if (m_precalculated) { + BOOST_LOG_TRIVIAL(error_level_not_in_cache) << "Had to calculate collision at radius " << radius << " and layer " << layer_idx << ", but precalculate was called. Performance may suffer!"; + tree_supports_show_error("Not precalculated Collision requested."sv, false); + } + const_cast(this)->calculateCollision(radius, layer_idx, {}); + return getCollision(orig_radius, layer_idx, min_xy_dist); +} + +// Get a collision area at a given layer for a radius that is a lower or equial to the key radius. +// It is expected that the collision area is precalculated for a given layer at least for the radius zero. +// Used for pushing tree supports away from object during the final Organic optimization step. +std::optional>> TreeModelVolumes::get_collision_lower_bound_area(LayerIndex layer_id, coord_t max_radius) const +{ + return m_collision_cache.get_lower_bound_area({ max_radius, layer_id }); +} + +// Private. Only called internally by calculateAvoidance() and calculateAvoidanceToModel(), radius is already snapped to grid. +const Polygons& TreeModelVolumes::getCollisionHolefree(coord_t radius, LayerIndex layer_idx) const +{ + assert(radius == this->ceilRadius(radius)); + assert(radius < m_increase_until_radius + m_current_min_xy_dist_delta); + if (std::optional> result = m_collision_cache_holefree.getArea({ radius, layer_idx }); result) + return (*result).get(); + if (m_precalculated) { + BOOST_LOG_TRIVIAL(error_level_not_in_cache) << "Had to calculate collision holefree at radius " << radius << " and layer " << layer_idx << ", but precalculate was called. Performance may suffer!"; + tree_supports_show_error("Not precalculated Holefree Collision requested."sv, false); + } + const_cast(this)->calculateCollisionHolefree({ radius, layer_idx }); + return getCollisionHolefree(radius, layer_idx); +} + +const Polygons& TreeModelVolumes::getAvoidance(const coord_t orig_radius, LayerIndex layer_idx, AvoidanceType type, bool to_model, bool min_xy_dist) const +{ + if (layer_idx == 0) // What on the layer directly above buildplate do i have to avoid to reach the buildplate ... + return getCollision(orig_radius, layer_idx, min_xy_dist); + + const coord_t radius = this->ceilRadius(orig_radius, min_xy_dist); + if (type == AvoidanceType::FastSafe && radius >= m_increase_until_radius + m_current_min_xy_dist_delta) + // no holes anymore by definition at this request + type = AvoidanceType::Fast; + + if (std::optional> result = + this->avoidance_cache(type, to_model).getArea({ radius, layer_idx }); + result) + return (*result).get(); + + if (m_precalculated) { + if (to_model) { + BOOST_LOG_TRIVIAL(error_level_not_in_cache) << "Had to calculate Avoidance to model at radius " << radius << " and layer " << layer_idx << ", but precalculate was called. Performance may suffer!"; + tree_supports_show_error("Not precalculated Avoidance(to model) requested."sv, false); + } else { + BOOST_LOG_TRIVIAL(error_level_not_in_cache) << "Had to calculate Avoidance at radius " << radius << " and layer " << layer_idx << ", but precalculate was called. Performance may suffer!"; + tree_supports_show_error("Not precalculated Avoidance(to buildplate) requested."sv, false); + } + } + const_cast(this)->calculateAvoidance({ radius, layer_idx }, ! to_model, to_model); + // Retrive failed and correct result was calculated. Now it has to be retrived. + return getAvoidance(orig_radius, layer_idx, type, to_model, min_xy_dist); +} + +const Polygons& TreeModelVolumes::getPlaceableAreas(const coord_t orig_radius, LayerIndex layer_idx, std::function throw_on_cancel) const +{ + if (orig_radius == 0) + return this->getCollision(0, layer_idx, true); + + const coord_t radius = ceilRadius(orig_radius); + if (std::optional> result = m_placeable_areas_cache.getArea({ radius, layer_idx }); result) + return (*result).get(); + if (m_precalculated) { + BOOST_LOG_TRIVIAL(error_level_not_in_cache) << "Had to calculate Placeable Areas at radius " << radius << " and layer " << layer_idx << ", but precalculate was called. Performance may suffer!"; + tree_supports_show_error("Not precalculated Placeable areas requested."sv, false); + } + const_cast(this)->calculatePlaceables(radius, layer_idx, throw_on_cancel); + return getPlaceableAreas(orig_radius, layer_idx, throw_on_cancel); +} + +const Polygons& TreeModelVolumes::getWallRestriction(const coord_t orig_radius, LayerIndex layer_idx, bool min_xy_dist) const +{ + assert(layer_idx > 0); + if (layer_idx == 0) + // Should never be requested as there will be no going below layer 0 ..., + // but just to be sure some semi-sane catch. Alternative would be empty Polygon. + return getCollision(orig_radius, layer_idx, min_xy_dist); + + min_xy_dist &= m_current_min_xy_dist_delta > 0; + + const coord_t radius = ceilRadius(orig_radius); + if (std::optional> result = + (min_xy_dist ? m_wall_restrictions_cache_min : m_wall_restrictions_cache).getArea({ radius, layer_idx }); + result) + return (*result).get(); + if (m_precalculated) { + BOOST_LOG_TRIVIAL(error_level_not_in_cache) << "Had to calculate Wall restricions at radius " << radius << " and layer " << layer_idx << ", but precalculate was called. Performance may suffer!"; + tree_supports_show_error( + min_xy_dist ? + "Not precalculated Wall restriction of minimum xy distance requested )." : + "Not precalculated Wall restriction requested )."sv + , false); + } + const_cast(this)->calculateWallRestrictions({ radius, layer_idx }); + return getWallRestriction(orig_radius, layer_idx, min_xy_dist); // Retrieve failed and correct result was calculated. Now it has to be retrieved. +} + +void TreeModelVolumes::calculateCollision(const std::vector &keys, std::function throw_on_cancel) +{ + tbb::parallel_for(tbb::blocked_range(0, keys.size()), + [&](const tbb::blocked_range &range) { + for (size_t ikey = range.begin(); ikey != range.end(); ++ ikey) { + const LayerIndex radius = keys[ikey].first; + const size_t max_layer_idx = keys[ikey].second; + // recursive call to parallel_for. + calculateCollision(radius, max_layer_idx, throw_on_cancel); + } + }); +} + +void TreeModelVolumes::calculateCollision(const coord_t radius, const LayerIndex max_layer_idx, std::function throw_on_cancel) +{ +// assert(radius == this->ceilRadius(radius)); + + // Process the outlines from least layers to most layers so that the final union will run over the longest vector. + std::vector layer_outline_indices(m_layer_outlines.size(), 0); + std::iota(layer_outline_indices.begin(), layer_outline_indices.end(), 0); + std::sort(layer_outline_indices.begin(), layer_outline_indices.end(), + [this](size_t i, size_t j) { return m_layer_outlines[i].second.size() < m_layer_outlines[j].second.size(); }); + + const LayerIndex min_layer_last = m_collision_cache.getMaxCalculatedLayer(radius); + std::vector data(max_layer_idx + 1 - min_layer_last, Polygons{}); + const bool calculate_placable = m_support_rests_on_model && radius == 0; + std::vector data_placeable; + if (calculate_placable) + data_placeable = std::vector(max_layer_idx + 1 - min_layer_last, Polygons{}); + + for (size_t outline_idx : layer_outline_indices) + if (const std::vector &outlines = m_layer_outlines[outline_idx].second; ! outlines.empty()) { + const TreeSupportMeshGroupSettings &settings = m_layer_outlines[outline_idx].first; + const coord_t layer_height = settings.layer_height; + const coord_t z_distance_bottom = settings.support_bottom_distance; + const int z_distance_bottom_layers = round_up_divide(z_distance_bottom, layer_height); + const int z_distance_top_layers = round_up_divide(settings.support_top_distance, layer_height); + const LayerIndex max_required_layer = std::min(outlines.size(), max_layer_idx + std::max(coord_t(1), z_distance_top_layers)); + const LayerIndex min_layer_bottom = std::max(0, min_layer_last - int(z_distance_bottom_layers)); + const coord_t xy_distance = outline_idx == m_current_outline_idx ? m_current_min_xy_dist : + // technically this causes collision for the normal xy_distance to be larger by m_current_min_xy_dist_delta for all + // not currently processing meshes as this delta will be added at request time. + // avoiding this would require saving each collision for each outline_idx separately. + // and later for each avoidance... But avoidance calculation has to be for the whole scene and can NOT be done for each outline_idx separately and combined later. + // so avoiding this inaccuracy seems infeasible as it would require 2x the avoidance calculations => 0.5x the performance. + //FIXME support_xy_distance is not corrected for "soluble" flag, see TreeSupportSettings constructor. + settings.support_xy_distance; + + // 1) Calculate offsets of collision areas in parallel. + std::vector collision_areas_offsetted(max_required_layer + 1 - min_layer_bottom); + tbb::parallel_for(tbb::blocked_range(min_layer_bottom, max_required_layer + 1), + [&outlines, &machine_border = m_machine_border, offset_value = radius + xy_distance, min_layer_bottom, &collision_areas_offsetted, &throw_on_cancel] + (const tbb::blocked_range &range) { + for (LayerIndex layer_idx = range.begin(); layer_idx != range.end(); ++ layer_idx) { + Polygons collision_areas = machine_border; + append(collision_areas, outlines[layer_idx]); + // jtRound is not needed here, as the overshoot can not cause errors in the algorithm, because no assumptions are made about the model. + // if a key does not exist when it is accessed it is added! + collision_areas_offsetted[layer_idx - min_layer_bottom] = offset_value == 0 ? union_(collision_areas) : offset(union_ex(collision_areas), offset_value, ClipperLib::jtMiter, 1.2); + if(throw_on_cancel) + throw_on_cancel(); + } + }); + + // 2) Sum over top / bottom ranges. + const bool last = outline_idx == layer_outline_indices.size(); + tbb::parallel_for(tbb::blocked_range(min_layer_last + 1, max_layer_idx + 1), + [&collision_areas_offsetted, &anti_overhang = m_anti_overhang, min_layer_bottom, radius, z_distance_bottom_layers, z_distance_top_layers, min_resolution = m_min_resolution, &data, min_layer_last, last, &throw_on_cancel] + (const tbb::blocked_range& range) { + for (LayerIndex layer_idx = range.begin(); layer_idx != range.end(); ++layer_idx) { + Polygons collisions; + for (int i = -z_distance_bottom_layers; i <= z_distance_top_layers; ++ i) { + int j = layer_idx + i - min_layer_bottom; + if (j >= 0 && j < int(collision_areas_offsetted.size())) + append(collisions, collision_areas_offsetted[j]); + } + collisions = last && layer_idx < int(anti_overhang.size()) ? union_(collisions, offset(union_ex(anti_overhang[layer_idx]), radius, ClipperLib::jtMiter, 1.2)) : union_(collisions); + auto &dst = data[layer_idx - (min_layer_last + 1)]; + if (last) { + if (! dst.empty()) + collisions = union_(collisions, dst); + dst = polygons_simplify(collisions, min_resolution); + } else + append(dst, collisions); + if (throw_on_cancel) + throw_on_cancel(); + } + }); + + // 3) Optionally calculate placables. + if (calculate_placable) { + // Calculating both the collision areas and placable areas. + tbb::parallel_for(tbb::blocked_range(std::max(min_layer_last + 1, z_distance_bottom_layers + 1), max_layer_idx + 1), + [&collision_areas_offsetted, &anti_overhang = m_anti_overhang, min_layer_bottom, z_distance_bottom_layers, last, min_resolution = m_min_resolution, &data_placeable, min_layer_last, &throw_on_cancel] + (const tbb::blocked_range& range) { + for (LayerIndex layer_idx = range.begin(); layer_idx != range.end(); ++ layer_idx) { + LayerIndex layer_idx_below = layer_idx - (z_distance_bottom_layers + 1) - min_layer_bottom; + assert(layer_idx_below >= 0); + auto ¤t = collision_areas_offsetted[layer_idx - min_layer_bottom]; + auto &below = collision_areas_offsetted[layer_idx_below]; + auto placable = diff(below, layer_idx < int(anti_overhang.size()) ? union_(current, anti_overhang[layer_idx - (z_distance_bottom_layers + 1)]) : current); + auto &dst = data_placeable[layer_idx - (min_layer_last + 1)]; + if (last) { + if (! dst.empty()) + placable = union_(placable, dst); + dst = polygons_simplify(placable, min_resolution); + } else + append(dst, placable); + if (throw_on_cancel) + throw_on_cancel(); + } + }); + } else { + // Calculating just the collision areas. + } + } +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + { + std::lock_guard critical_section(*m_critical_progress); + if (m_precalculated && m_precalculation_progress < TREE_PROGRESS_PRECALC_COLL) { + m_precalculation_progress += TREE_PROGRESS_PRECALC_COLL / keys.size(); + Progress::messageProgress(Progress::Stage::SUPPORT, m_precalculation_progress * m_progress_multiplier + m_progress_offset, TREE_PROGRESS_TOTAL); + } + } +#endif + if (throw_on_cancel) + throw_on_cancel(); + m_collision_cache.insert(std::move(data), min_layer_last + 1, radius); + if (calculate_placable) + m_placeable_areas_cache.insert(std::move(data_placeable), min_layer_last + 1, radius); +} + +void TreeModelVolumes::calculateCollisionHolefree(const std::vector &keys, std::function throw_on_cancel) +{ + LayerIndex max_layer = 0; + for (long long unsigned int i = 0; i < keys.size(); i++) + max_layer = std::max(max_layer, keys[i].second); + + tbb::parallel_for(tbb::blocked_range(0, max_layer + 1, keys.size()), + [&](const tbb::blocked_range &range) { + std::vector> data; + data.reserve(range.size() * keys.size()); + for (LayerIndex layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx) { + for (RadiusLayerPair key : keys) + if (layer_idx <= key.second) { + // Logically increase the collision by m_increase_until_radius + coord_t radius = key.first; + assert(radius == this->ceilRadius(radius)); + assert(radius < m_increase_until_radius + m_current_min_xy_dist_delta); + coord_t increase_radius_ceil = ceilRadius(m_increase_until_radius, false) - radius; + assert(increase_radius_ceil > 0); + // this union is important as otherwise holes(in form of lines that will increase to holes in a later step) can get unioned onto the area. + data.emplace_back(RadiusLayerPair(radius, layer_idx), polygons_simplify( + offset(union_ex(this->getCollision(m_increase_until_radius, layer_idx, false)), + 5 - increase_radius_ceil, ClipperLib::jtRound, m_min_resolution), + m_min_resolution)); + if (throw_on_cancel) + throw_on_cancel(); + } + } + m_collision_cache_holefree.insert(std::move(data)); + }); +} + +void TreeModelVolumes::calculateAvoidance(const std::vector &keys, bool to_build_plate, bool to_model, std::function throw_on_cancel) +{ + // For every RadiusLayer pair there are 3 avoidances that have to be calculated. + // Prepare tasks for parallelization. + struct AvoidanceTask { + AvoidanceType type; + coord_t radius; + LayerIndex max_required_layer; + bool to_model; + LayerIndex start_layer; + + bool slow() const { return this->type == AvoidanceType::Slow; } + bool holefree() const { return this->type == AvoidanceType::FastSafe; } + }; + + std::vector avoidance_tasks; + avoidance_tasks.reserve((int(to_build_plate) + int(to_model)) * keys.size() * size_t(AvoidanceType::Count)); + + for (int iter_idx = 0; iter_idx < 2 * int(keys.size()) * int(AvoidanceType::Count); ++ iter_idx) { + AvoidanceTask task{ + AvoidanceType(iter_idx % int(AvoidanceType::Count)), + keys[iter_idx / 6].first, // radius + keys[iter_idx / 6].second, // max_layer + ((iter_idx / 3) & 1) != 0 // to_model + }; + // Ensure start_layer is at least 1 as if no avoidance was calculated yet getMaxCalculatedLayer() returns -1. + task.start_layer = std::max(1, 1 + avoidance_cache(task.type, task.to_model).getMaxCalculatedLayer(task.radius)); + if (task.start_layer > task.max_required_layer) { + BOOST_LOG_TRIVIAL(debug) << "Calculation requested for value already calculated?"; + continue; + } + if (! task.holefree() || task.radius < m_increase_until_radius + m_current_min_xy_dist_delta) + avoidance_tasks.emplace_back(task); + } + + if(throw_on_cancel) + throw_on_cancel(); + + tbb::parallel_for(tbb::blocked_range(0, avoidance_tasks.size(), 1), + [this, &avoidance_tasks, &throw_on_cancel](const tbb::blocked_range &range) { + for (size_t task_idx = range.begin(); task_idx < range.end(); ++ task_idx) { + const AvoidanceTask &task = avoidance_tasks[task_idx]; + assert(! task.holefree() || task.radius < m_increase_until_radius + m_current_min_xy_dist_delta); + if (task.to_model) + // ensuring Placeableareas are calculated + //FIXME pass throw_on_cancel + getPlaceableAreas(task.radius, task.max_required_layer, throw_on_cancel); + // The following loop propagating avoidance regions bottom up is inherently serial. + const bool collision_holefree = (task.slow() || task.holefree()) && task.radius < m_increase_until_radius + m_current_min_xy_dist_delta; + const float max_move = task.slow() ? m_max_move_slow : m_max_move; + // Limiting the offset step so that unioning the shrunk latest_avoidance with the current layer collisions + // will not create gaps in the resulting avoidance region letting a tree support branch tunneling through an object wall. + float move_step = 1.9 * std::max(task.radius, m_current_min_xy_dist); + int move_steps = round_up_divide(max_move, move_step); + assert(move_steps > 0); + float last_move_step = max_move - (move_steps - 1) * move_step; + if (last_move_step < scaled(0.05)) { + assert(move_steps > 1); + if (move_steps > 1) { + // Avoid taking a very short last step, stretch the other steps a bit instead. + move_step = max_move / (-- move_steps); + last_move_step = move_step; + } + } + // minDist as the delta was already added, also avoidance for layer 0 will return the collision. + Polygons latest_avoidance = getAvoidance(task.radius, task.start_layer - 1, task.type, task.to_model, true); + std::vector> data; + data.reserve(task.max_required_layer + 1 - task.start_layer); + for (LayerIndex layer_idx = task.start_layer; layer_idx <= task.max_required_layer; ++ layer_idx) { + // Merge current layer collisions with shrunk last_avoidance. + const Polygons ¤t_layer_collisions = collision_holefree ? getCollisionHolefree(task.radius, layer_idx) : getCollision(task.radius, layer_idx, true); + // For mildly steep branch angles only one step will be taken. + for (int istep = 0; istep < move_steps; ++ istep) + latest_avoidance = union_(current_layer_collisions, + offset(latest_avoidance, + istep + 1 == move_steps ? - last_move_step : - move_step, + ClipperLib::jtRound, m_min_resolution)); + if (task.to_model) + latest_avoidance = diff(latest_avoidance, getPlaceableAreas(task.radius, layer_idx, throw_on_cancel)); + latest_avoidance = polygons_simplify(latest_avoidance, m_min_resolution); + data.emplace_back(RadiusLayerPair{task.radius, layer_idx}, latest_avoidance); + if (throw_on_cancel) + throw_on_cancel(); + } +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + { + std::lock_guard critical_section(*m_critical_progress); + if (m_precalculated && m_precalculation_progress < TREE_PROGRESS_PRECALC_COLL + TREE_PROGRESS_PRECALC_AVO) { + m_precalculation_progress += to_model ? + 0.4 * TREE_PROGRESS_PRECALC_AVO / (keys.size() * 3) : + m_support_rests_on_model ? 0.4 : 1 * TREE_PROGRESS_PRECALC_AVO / (keys.size() * 3); + Progress::messageProgress(Progress::Stage::SUPPORT, m_precalculation_progress * m_progress_multiplier + m_progress_offset, TREE_PROGRESS_TOTAL); + } + } +#endif + avoidance_cache(task.type, task.to_model).insert(std::move(data)); + } + }); +} + + +void TreeModelVolumes::calculatePlaceables(const std::vector &keys, std::function throw_on_cancel) +{ + tbb::parallel_for(tbb::blocked_range(0, keys.size()), + [&, keys](const tbb::blocked_range& range) { + for (size_t key_idx = range.begin(); key_idx < range.end(); ++ key_idx) + this->calculatePlaceables(keys[key_idx].first, keys[key_idx].second, throw_on_cancel); + }); +} + +void TreeModelVolumes::calculatePlaceables(const coord_t radius, const LayerIndex max_required_layer, std::function throw_on_cancel) +{ + LayerIndex start_layer = 1 + m_placeable_areas_cache.getMaxCalculatedLayer(radius); + if (start_layer > max_required_layer) { + BOOST_LOG_TRIVIAL(debug) << "Requested calculation for value already calculated ?"; + return; + } + + std::vector data(max_required_layer + 1 - start_layer, Polygons{}); + + if (start_layer == 0) + data[0] = diff(m_machine_border, getCollision(radius, 0, true)); + + tbb::parallel_for(tbb::blocked_range(std::max(1, start_layer), max_required_layer + 1), + [this, &data, radius, start_layer, &throw_on_cancel](const tbb::blocked_range& range) { + for (LayerIndex layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx) { + data[layer_idx - start_layer] = offset( + union_ex(getPlaceableAreas(0, layer_idx, throw_on_cancel)), + // As a placeable area is calculated by (collision of the layer below) - (collision of the current layer) and the collision is offset by xy_distance, + // it can happen that a small line is considered a flat area to place something onto, even though it is mostly + // xy_distance that cant support it. Making the area smaller by xy_distance fixes this. + - (radius + m_current_min_xy_dist + m_current_min_xy_dist_delta), + jtMiter, 1.2); + if(throw_on_cancel) + throw_on_cancel(); + } + }); +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + { + std::lock_guard critical_section(*m_critical_progress); + if (m_precalculated && m_precalculation_progress < TREE_PROGRESS_PRECALC_COLL + TREE_PROGRESS_PRECALC_AVO) { + m_precalculation_progress += 0.2 * TREE_PROGRESS_PRECALC_AVO / (keys.size()); + Progress::messageProgress(Progress::Stage::SUPPORT, m_precalculation_progress * m_progress_multiplier + m_progress_offset, TREE_PROGRESS_TOTAL); + } + } +#endif + m_placeable_areas_cache.insert(std::move(data), start_layer, radius); +} + +void TreeModelVolumes::calculateWallRestrictions(const std::vector &keys, std::function throw_on_cancel) +{ + // Wall restrictions are mainly important when they represent actual walls that are printed, and not "just" the configured z_distance, because technically valid placement is no excuse for moving through a wall. + // As they exist to prevent accidentially moving though a wall at high speed between layers like thie (x = wall,i = influence area,o= empty space,d = blocked area because of z distance) Assume maximum movement distance is two characters and maximum safe movement distance of one character + + /* Potential issue addressed by the wall restrictions: Influence area may lag through a wall + * layer z+1:iiiiiiiiiiioooo + * layer z+0:xxxxxiiiiiiiooo + * layer z-1:ooooixxxxxxxxxx + */ + + // The radius for the upper collission has to be 0 as otherwise one may not enter areas that may be forbidden on layer_idx but not one below (c = not an influence area even though it should ): + /* + * layer z+1:xxxxxiiiiiioo + * layer z+0:dddddiiiiiiio + * layer z-1:dddocdddddddd + */ + // Also there can not just the collision of the lower layer be used because if it were: + /* + * layer z+1:dddddiiiiiiiiiio + * layer z+0:xxxxxddddddddddc + * layer z-1:dddddxxxxxxxxxxc + */ + // Or of the upper layer be used because if it were: + /* + * layer z+1:dddddiiiiiiiiiio + * layer z+0:xxxxcddddddddddc + * layer z-1:ddddcxxxxxxxxxxc + */ + + // And just offseting with maximum movement distance (and not in multiple steps) could cause: + /* + * layer z: oxiiiiiiiiioo + * layer z-1: ixiiiiiiiiiii + */ + + tbb::parallel_for(tbb::blocked_range(0, keys.size()), + [&, keys](const tbb::blocked_range &range) { + for (size_t key_idx = range.begin(); key_idx < range.end(); ++ key_idx) { + const coord_t radius = keys[key_idx].first; + const LayerIndex max_required_layer = keys[key_idx].second; + const coord_t min_layer_bottom = std::max(1, m_wall_restrictions_cache.getMaxCalculatedLayer(radius)); + const size_t buffer_size = max_required_layer + 1 - min_layer_bottom; + std::vector data(buffer_size, Polygons{}); + std::vector data_min; + if (m_current_min_xy_dist_delta > 0) + data_min.assign(buffer_size, Polygons{}); + tbb::parallel_for(tbb::blocked_range(min_layer_bottom, max_required_layer + 1), + [this, &data, &data_min, radius, min_layer_bottom, &throw_on_cancel](const tbb::blocked_range &range) { + for (LayerIndex layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx) { + data[layer_idx - min_layer_bottom] = polygons_simplify( + // radius contains m_current_min_xy_dist_delta already if required + intersection(getCollision(0, layer_idx, false), getCollision(radius, layer_idx - 1, true)), + m_min_resolution); + if (! data_min.empty()) + data_min[layer_idx - min_layer_bottom] = + polygons_simplify( + intersection(getCollision(0, layer_idx, true), getCollision(radius, layer_idx - 1, true)), + m_min_resolution); + if (throw_on_cancel) + throw_on_cancel(); + } + }); + m_wall_restrictions_cache.insert(std::move(data), min_layer_bottom, radius); + if (! data_min.empty()) + m_wall_restrictions_cache_min.insert(std::move(data_min), min_layer_bottom, radius); + } + }); +} + +coord_t TreeModelVolumes::ceilRadius(const coord_t radius) const +{ + if (radius == 0) + return 0; + + coord_t out = m_radius_0; + if (radius > m_radius_0) { + // generate SUPPORT_TREE_PRE_EXPONENTIAL_STEPS of radiis before starting to exponentially increase them. + coord_t initial_radius_delta = SUPPORT_TREE_EXPONENTIAL_THRESHOLD - m_radius_0; + auto ignore = [this](coord_t r) { return std::binary_search(m_ignorable_radii.begin(), m_ignorable_radii.end(), r); }; + if (initial_radius_delta > SUPPORT_TREE_COLLISION_RESOLUTION) { + const int num_steps = round_up_divide(initial_radius_delta, SUPPORT_TREE_EXPONENTIAL_THRESHOLD); + const int stepsize = initial_radius_delta / num_steps; + out += stepsize; + for (auto step = 0; step < num_steps; ++ step) { + if (out >= radius && ! ignore(out)) + return out; + out += stepsize; + } + } else + out += SUPPORT_TREE_COLLISION_RESOLUTION; + while (out < radius || ignore(out)) { + assert(out * SUPPORT_TREE_EXPONENTIAL_FACTOR > out + SUPPORT_TREE_COLLISION_RESOLUTION); + out = out * SUPPORT_TREE_EXPONENTIAL_FACTOR; + } + } + return out; +} + +void TreeModelVolumes::RadiusLayerPolygonCache::allocate_layers(size_t num_layers) +{ + if (num_layers > m_data.size()) { + if (num_layers > m_data.capacity()) + m_data.reserve(next_highest_power_of_2(num_layers)); + m_data.resize(num_layers, {}); + } +} + +// For debugging purposes, sorted by layer index, then by radius. +std::vector>> TreeModelVolumes::RadiusLayerPolygonCache::sorted() const +{ + std::vector>> out; + for (auto &layer : m_data) { + auto layer_idx = LayerIndex(&layer - m_data.data()); + for (auto &radius_polygons : layer) + out.emplace_back(std::make_pair(radius_polygons.first, layer_idx), radius_polygons.second); + } + assert(std::is_sorted(out.begin(), out.end(), [](auto &l, auto &r){ return l.first.second < r.first.second || (l.first.second == r.first.second) && l.first.first < r.first.first; })); + return out; +} + +} // namespace Slic3r::TreeSupport3D diff --git a/src/libslic3r/TreeModelVolumes.hpp b/src/libslic3r/TreeModelVolumes.hpp new file mode 100644 index 000000000..5f9db2a1d --- /dev/null +++ b/src/libslic3r/TreeModelVolumes.hpp @@ -0,0 +1,679 @@ +// Tree supports by Thomas Rahm, losely based on Tree Supports by CuraEngine. +// Original source of Thomas Rahm's tree supports: +// https://github.com/ThomasRahm/CuraEngine +// +// Original CuraEngine copyright: +// Copyright (c) 2021 Ultimaker B.V. +// CuraEngine is released under the terms of the AGPLv3 or higher. + +#ifndef slic3r_TreeModelVolumes_hpp +#define slic3r_TreeModelVolumes_hpp + +#include +#include + +#include + +#include "Point.hpp" +#include "Polygon.hpp" +#include "PrintConfig.hpp" + +namespace Slic3r +{ + +class BuildVolume; +class PrintObject; + +namespace TreeSupport3D +{ + +using LayerIndex = int; + +struct TreeSupportMeshGroupSettings { + TreeSupportMeshGroupSettings() = default; + explicit TreeSupportMeshGroupSettings(const PrintObject &print_object); + +/*********************************************************************/ +/* Print parameters, not support specific: */ +/*********************************************************************/ + coord_t layer_height { scaled(0.15) }; + // Maximum Deviation (meshfix_maximum_deviation) + // The maximum deviation allowed when reducing the resolution for the Maximum Resolution setting. If you increase this, + // the print will be less accurate, but the g-code will be smaller. Maximum Deviation is a limit for Maximum Resolution, + // so if the two conflict the Maximum Deviation will always be held true. + coord_t resolution { scaled(0.025) }; + // Minimum Feature Size (aka minimum line width) - Arachne specific + // Minimum thickness of thin features. Model features that are thinner than this value will not be printed, while features thicker + // than the Minimum Feature Size will be widened to the Minimum Wall Line Width. + coord_t min_feature_size { scaled(0.1) }; + +/*********************************************************************/ +/* General support parameters: */ +/*********************************************************************/ + + // Support Overhang Angle + // The minimum angle of overhangs for which support is added. At a value of 0° all overhangs are supported, 90° will not provide any support. + double support_angle { 50. * M_PI / 180. }; + // Support Line Width + // Width of a single support structure line. + coord_t support_line_width { scaled(0.4) }; + // Support Roof Line Width: Width of a single support roof line. + coord_t support_roof_line_width { scaled(0.4) }; + // Enable Support Floor (aka bottom interfaces) + // Generate a dense slab of material between the bottom of the support and the model. This will create a skin between the model and support. + bool support_bottom_enable { false }; + // Support Floor Thickness + // The thickness of the support floors. This controls the number of dense layers that are printed on top of places of a model on which support rests. + coord_t support_bottom_height { scaled(1.) }; + bool support_material_buildplate_only { false }; + // Support X/Y Distance + // Distance of the support structure from the print in the X/Y directions. + // minimum: 0, maximum warning: 1.5 * machine_nozzle_tip_outer_diameter + coord_t support_xy_distance { scaled(0.7) }; + // Minimum Support X/Y Distance + // Distance of the support structure from the overhang in the X/Y directions. + // minimum_value: 0, minimum warning": support_xy_distance - support_line_width * 2, maximum warning: support_xy_distance + coord_t support_xy_distance_overhang { scaled(0.2) }; + // Support Top Distance + // Distance from the top of the support to the print. + coord_t support_top_distance { scaled(0.1) }; + // Support Bottom Distance + // Distance from the print to the bottom of the support. + coord_t support_bottom_distance { scaled(0.1) }; + //FIXME likely not needed, optimization for clipping of interface layers + // When checking where there's model above and below the support, take steps of the given height. Lower values will slice slower, while higher values + // may cause normal support to be printed in some places where there should have been support interface. + coord_t support_interface_skip_height { scaled(0.3) }; + // Support Infill Line Directions + // A list of integer line directions to use. Elements from the list are used sequentially as the layers progress and when the end + // of the list is reached, it starts at the beginning again. The list items are separated by commas and the whole list is contained + // in square brackets. Default is an empty list which means use the default angle 0 degrees. +// std::vector support_infill_angles {}; + // Enable Support Roof + // Generate a dense slab of material between the top of support and the model. This will create a skin between the model and support. + bool support_roof_enable { false }; + // Support Roof Thickness + // The thickness of the support roofs. This controls the amount of dense layers at the top of the support on which the model rests. + coord_t support_roof_height { scaled(1.) }; + // Minimum Support Roof Area + // Minimum area size for the roofs of the support. Polygons which have an area smaller than this value will be printed as normal support. + double minimum_roof_area { scaled(scaled(1.)) }; + // A list of integer line directions to use. Elements from the list are used sequentially as the layers progress + // and when the end of the list is reached, it starts at the beginning again. The list items are separated + // by commas and the whole list is contained in square brackets. Default is an empty list which means + // use the default angles (alternates between 45 and 135 degrees if interfaces are quite thick or 90 degrees). + std::vector support_roof_angles {}; + // Support Roof Pattern (aka top interface) + // The pattern with which the roofs of the support are printed. + SupportMaterialInterfacePattern support_roof_pattern { smipAuto }; + // Support Pattern + // The pattern of the support structures of the print. The different options available result in sturdy or easy to remove support. + SupportMaterialPattern support_pattern { smpRectilinear }; + // Support Line Distance + // Distance between the printed support structure lines. This setting is calculated by the support density. + coord_t support_line_spacing { scaled(2.66 - 0.4) }; + // Support Floor Horizontal Expansion + // Amount of offset applied to the floors of the support. + coord_t support_bottom_offset { scaled(0.) }; + // Support Wall Line Count + // The number of walls with which to surround support infill. Adding a wall can make support print more reliably + // and can support overhangs better, but increases print time and material used. + // tree: 1, zig-zag: 0, concentric: 1 + int support_wall_count { 1 }; + // Support Roof Line Distance + // Distance between the printed support roof lines. This setting is calculated by the Support Roof Density, but can be adjusted separately. + coord_t support_roof_line_distance { scaled(0.4) }; + // Minimum Support Area + // Minimum area size for support polygons. Polygons which have an area smaller than this value will not be generated. + coord_t minimum_support_area { scaled(0.) }; + // Minimum Support Floor Area + // Minimum area size for the floors of the support. Polygons which have an area smaller than this value will be printed as normal support. + coord_t minimum_bottom_area { scaled(1.0) }; + // Support Horizontal Expansion + // Amount of offset applied to all support polygons in each layer. Positive values can smooth out the support areas and result in more sturdy support. + coord_t support_offset { scaled(0.) }; + +/*********************************************************************/ +/* Parameters for the Cura tree supports implementation: */ +/*********************************************************************/ + + // Tree Support Maximum Branch Angle + // The maximum angle of the branches, when the branches have to avoid the model. Use a lower angle to make them more vertical and more stable. Use a higher angle to be able to have more reach. + // minimum: 0, minimum warning: 20, maximum: 89, maximum warning": 85 + double support_tree_angle { 60. * M_PI / 180. }; + // Tree Support Branch Diameter Angle + // The angle of the branches' diameter as they gradually become thicker towards the bottom. An angle of 0 will cause the branches to have uniform thickness over their length. + // A bit of an angle can increase stability of the tree support. + // minimum: 0, maximum: 89.9999, maximum warning: 15 + double support_tree_branch_diameter_angle { 5. * M_PI / 180. }; + // Tree Support Branch Distance + // How far apart the branches need to be when they touch the model. Making this distance small will cause + // the tree support to touch the model at more points, causing better overhang but making support harder to remove. + coord_t support_tree_branch_distance { scaled(1.) }; + // Tree Support Branch Diameter + // The diameter of the thinnest branches of tree support. Thicker branches are more sturdy. Branches towards the base will be thicker than this. + // minimum: 0.001, minimum warning: support_line_width * 2 + coord_t support_tree_branch_diameter { scaled(2.) }; + +/*********************************************************************/ +/* Parameters new to the Thomas Rahm's tree supports implementation: */ +/*********************************************************************/ + + // Tree Support Preferred Branch Angle + // The preferred angle of the branches, when they do not have to avoid the model. Use a lower angle to make them more vertical and more stable. Use a higher angle for branches to merge faster. + // minimum: 0, minimum warning: 10, maximum: support_tree_angle, maximum warning: support_tree_angle-1 + double support_tree_angle_slow { 50. * M_PI / 180. }; + // Tree Support Diameter Increase To Model + // The most the diameter of a branch that has to connect to the model may increase by merging with branches that could reach the buildplate. + // Increasing this reduces print time, but increases the area of support that rests on model + // minimum: 0 + coord_t support_tree_max_diameter_increase_by_merges_when_support_to_model { scaled(1.0) }; + // Tree Support Minimum Height To Model + // How tall a branch has to be if it is placed on the model. Prevents small blobs of support. This setting is ignored when a branch is supporting a support roof. + // minimum: 0, maximum warning: 5 + coord_t support_tree_min_height_to_model { scaled(1.0) }; + // Tree Support Inital Layer Diameter + // Diameter every branch tries to achieve when reaching the buildplate. Improves bed adhesion. + // minimum: 0, maximum warning: 20 + coord_t support_tree_bp_diameter { scaled(7.5) }; + // Tree Support Branch Density + // Adjusts the density of the support structure used to generate the tips of the branches. A higher value results in better overhangs, + // but the supports are harder to remove. Use Support Roof for very high values or ensure support density is similarly high at the top. + // 5%-35% + double support_tree_top_rate { 15. }; + // Tree Support Tip Diameter + // The diameter of the top of the tip of the branches of tree support. + // minimum: min_wall_line_width, minimum warning: min_wall_line_width+0.05, maximum_value: support_tree_branch_diameter, value: support_line_width + coord_t support_tree_tip_diameter { scaled(0.4) }; + + // Support Interface Priority + // How support interface and support will interact when they overlap. Currently only implemented for support roof. + //enum support_interface_priority { support_lines_overwrite_interface_area }; +}; + +class TreeModelVolumes +{ +public: + TreeModelVolumes() = default; + explicit TreeModelVolumes(const PrintObject &print_object, const BuildVolume &build_volume, + coord_t max_move, coord_t max_move_slow, size_t current_mesh_idx, +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + double progress_multiplier, + double progress_offset, +#endif // SLIC3R_TREESUPPORTS_PROGRESS + const std::vector &additional_excluded_areas = {}); + TreeModelVolumes(TreeModelVolumes&&) = default; + TreeModelVolumes& operator=(TreeModelVolumes&&) = default; + + TreeModelVolumes(const TreeModelVolumes&) = delete; + TreeModelVolumes& operator=(const TreeModelVolumes&) = delete; + + void clear() { + this->clear_all_but_object_collision(); + m_collision_cache.clear(); + } + void clear_all_but_object_collision() { + //m_collision_cache.clear_all_but_radius0(); + m_collision_cache_holefree.clear(); + m_avoidance_cache.clear(); + m_avoidance_cache_slow.clear(); + m_avoidance_cache_to_model.clear(); + m_avoidance_cache_to_model_slow.clear(); + m_placeable_areas_cache.clear(); + m_avoidance_cache_holefree.clear(); + m_avoidance_cache_holefree_to_model.clear(); + m_wall_restrictions_cache.clear(); + m_wall_restrictions_cache_min.clear(); + } + + enum class AvoidanceType : int8_t + { + Slow, + FastSafe, + Fast, + Count + }; + + /*! + * \brief Precalculate avoidances and collisions up to max_layer. + * + * Knowledge about branch angle is used to only calculate avoidances and collisions that may actually be needed. + * Not calling precalculate() will cause the class to lazily calculate avoidances and collisions as needed, which will be a lot slower on systems with more then one or two cores! + */ + void precalculate(const coord_t max_layer, std::function throw_on_cancel); + + /*! + * \brief Provides the areas that have to be avoided by the tree's branches to prevent collision with the model on this layer. + * + * The result is a 2D area that would cause nodes of radius \p radius to + * collide with the model. + * + * \param radius The radius of the node of interest + * \param layer_idx The layer of interest + * \param min_xy_dist Is the minimum xy distance used. + * \return Polygons object + */ + const Polygons& getCollision(const coord_t radius, LayerIndex layer_idx, bool min_xy_dist) const; + + // Get a collision area at a given layer for a radius that is a lower or equial to the key radius. + // It is expected that the collision area is precalculated for a given layer at least for the radius zero. + // Used for pushing tree supports away from object during the final Organic optimization step. + std::optional>> get_collision_lower_bound_area(LayerIndex layer_id, coord_t max_radius) const; + + /*! + * \brief Provides the areas that have to be avoided by the tree's branches + * in order to reach the build plate. + * + * The result is a 2D area that would cause nodes of radius \p radius to + * collide with the model or be unable to reach the build platform. + * + * The input collision areas are inset by the maximum move distance and + * propagated upwards. + * + * \param radius The radius of the node of interest + * \param layer_idx The layer of interest + * \param type Is the propagation with the maximum move distance slow required. + * \param to_model Does the avoidance allow good connections with the model. + * \param min_xy_dist is the minimum xy distance used. + * \return Polygons object + */ + const Polygons& getAvoidance(coord_t radius, LayerIndex layer_idx, AvoidanceType type, bool to_model, bool min_xy_dist) const; + /*! + * \brief Provides the area represents all areas on the model where the branch does completely fit on the given layer. + * \param radius The radius of the node of interest + * \param layer_idx The layer of interest + * \return Polygons object + */ + const Polygons& getPlaceableAreas(coord_t radius, LayerIndex layer_idx, std::function throw_on_cancel) const; + /*! + * \brief Provides the area that represents the walls, as in the printed area, of the model. This is an abstract representation not equal with the outline. See calculateWallRestrictions for better description. + * \param radius The radius of the node of interest. + * \param layer_idx The layer of interest. + * \param min_xy_dist is the minimum xy distance used. + * \return Polygons object + */ + const Polygons& getWallRestriction(coord_t radius, LayerIndex layer_idx, bool min_xy_dist) const; + /*! + * \brief Round \p radius upwards to either a multiple of m_radius_sample_resolution or a exponentially increasing value + * + * It also adds the difference between the minimum xy distance and the regular one. + * + * \param radius The radius of the node of interest + * \param min_xy_dist is the minimum xy distance used. + * \return The rounded radius + */ + coord_t ceilRadius(const coord_t radius, const bool min_xy_dist) const { + assert(radius >= 0); + return min_xy_dist ? + this->ceilRadius(radius) : + // special case as if a radius 0 is requested it could be to ensure correct xy distance. As such it is beneficial if the collision is as close to the configured values as possible. + radius > 0 ? this->ceilRadius(radius + m_current_min_xy_dist_delta) : m_current_min_xy_dist_delta; + } + /*! + * \brief Round \p radius upwards to the maximum that would still round up to the same value as the provided one. + * + * \param radius The radius of the node of interest + * \param min_xy_dist is the minimum xy distance used. + * \return The maximum radius, resulting in the same rounding. + */ + coord_t getRadiusNextCeil(coord_t radius, bool min_xy_dist) const { + assert(radius > 0); + return min_xy_dist ? + this->ceilRadius(radius) : + this->ceilRadius(radius + m_current_min_xy_dist_delta) - m_current_min_xy_dist_delta; + } + + Polygon m_bed_area; + +private: + /*! + * \brief Convenience typedef for the keys to the caches + */ + using RadiusLayerPair = std::pair; + class RadiusLayerPolygonCache { + // Map from radius to Polygons. Cache of one layer collision regions. + using LayerData = std::map; + // Vector of layers, at each layer map of radius to Polygons. + // Reference to Polygons returned shall be stable to insertion. + using Layers = std::vector; + public: + RadiusLayerPolygonCache() = default; + RadiusLayerPolygonCache(RadiusLayerPolygonCache &&rhs) : m_data(std::move(rhs.m_data)) {} + RadiusLayerPolygonCache& operator=(RadiusLayerPolygonCache &&rhs) { m_data = std::move(rhs.m_data); return *this; } + + RadiusLayerPolygonCache(const RadiusLayerPolygonCache&) = delete; + RadiusLayerPolygonCache& operator=(const RadiusLayerPolygonCache&) = delete; + + void insert(std::vector> &&in) { + std::lock_guard guard(m_mutex); + for (auto &d : in) + this->get_allocate_layer_data(d.first.second).emplace(d.first.first, std::move(d.second)); + } + // by layer + void insert(std::vector> &&in, coord_t radius) { + std::lock_guard guard(m_mutex); + for (auto &d : in) + this->get_allocate_layer_data(d.first).emplace(radius, std::move(d.second)); + } + void insert(std::vector &&in, coord_t first_layer_idx, coord_t radius) { + std::lock_guard guard(m_mutex); + allocate_layers(first_layer_idx + in.size()); + for (auto &d : in) + m_data[first_layer_idx ++].emplace(radius, std::move(d)); + } + /*! + * \brief Checks a cache for a given RadiusLayerPair and returns it if it is found + * \param key RadiusLayerPair of the requested areas. The radius will be calculated up to the provided layer. + * \return A wrapped optional reference of the requested area (if it was found, an empty optional if nothing was found) + */ + std::optional> getArea(const TreeModelVolumes::RadiusLayerPair &key) const { + std::lock_guard guard(m_mutex); + if (key.second >= m_data.size()) + return std::optional>{}; + const auto &layer = m_data[key.second]; + auto it = layer.find(key.first); + return it == layer.end() ? + std::optional>{} : std::optional>{ it->second }; + } + // Get a collision area at a given layer for a radius that is a lower or equial to the key radius. + std::optional>> get_lower_bound_area(const TreeModelVolumes::RadiusLayerPair &key) const { + std::lock_guard guard(m_mutex); + if (key.second >= m_data.size()) + return {}; + const auto &layer = m_data[key.second]; + if (layer.empty()) + return {}; + auto it = layer.lower_bound(key.first); + if (it == layer.end() || it->first != key.first) { + if (it == layer.begin()) + return {}; + -- it; + } + return std::make_pair(it->first, std::reference_wrapper(it->second)); + } + /*! + * \brief Get the highest already calculated layer in the cache. + * \param radius The radius for which the highest already calculated layer has to be found. + * \param map The cache in which the lookup is performed. + * + * \return A wrapped optional reference of the requested area (if it was found, an empty optional if nothing was found) + */ + LayerIndex getMaxCalculatedLayer(coord_t radius) const { + std::lock_guard guard(m_mutex); + auto layer_idx = LayerIndex(m_data.size()) - 1; + for (; layer_idx > 0; -- layer_idx) + if (const auto &layer = m_data[layer_idx]; layer.find(radius) != layer.end()) + break; + // The placeable on model areas do not exist on layer 0, as there can not be model below it. As such it may be possible that layer 1 is available, but layer 0 does not exist. + return layer_idx == 0 ? -1 : layer_idx; + } + + // For debugging purposes, sorted by layer index, then by radius. + [[nodiscard]] std::vector>> sorted() const; + + void clear() { m_data.clear(); } + void clear_all_but_radius0() { + for (LayerData &l : m_data) { + auto begin = l.begin(); + auto end = l.end(); + if (begin != end && ++ begin != end) + l.erase(begin, end); + } + } + + private: + LayerData& get_allocate_layer_data(LayerIndex layer_idx) { + allocate_layers(layer_idx + 1); + return m_data[layer_idx]; + } + void allocate_layers(size_t num_layers); + + Layers m_data; + mutable std::mutex m_mutex; + }; + + + /*! + * \brief Provides the areas that have to be avoided by the tree's branches to prevent collision with the model on this layer. Holes are removed. + * + * The result is a 2D area that would cause nodes of given radius to + * collide with the model or be inside a hole. + * A Hole is defined as an area, in which a branch with m_increase_until_radius radius would collide with the wall. + * minimum xy distance is always used. + * \param radius The radius of the node of interest + * \param layer_idx The layer of interest + * \param min_xy_dist Is the minimum xy distance used. + * \return Polygons object + */ + const Polygons& getCollisionHolefree(coord_t radius, LayerIndex layer_idx) const; + + /*! + * \brief Round \p radius upwards to either a multiple of m_radius_sample_resolution or a exponentially increasing value + * + * \param radius The radius of the node of interest + */ + coord_t ceilRadius(const coord_t radius) const; + + /*! + * \brief Creates the areas that have to be avoided by the tree's branches to prevent collision with the model on this layer. + * + * The result is a 2D area that would cause nodes of given radius to + * collide with the model. Result is saved in the cache. + * \param keys RadiusLayerPairs of all requested areas. Every radius will be calculated up to the provided layer. + */ + void calculateCollision(const std::vector &keys, std::function throw_on_cancel); + void calculateCollision(const coord_t radius, const LayerIndex max_layer_idx, std::function throw_on_cancel); + /*! + * \brief Creates the areas that have to be avoided by the tree's branches to prevent collision with the model on this layer. Holes are removed. + * + * The result is a 2D area that would cause nodes of given radius to + * collide with the model or be inside a hole. Result is saved in the cache. + * A Hole is defined as an area, in which a branch with m_increase_until_radius radius would collide with the wall. + * \param keys RadiusLayerPairs of all requested areas. Every radius will be calculated up to the provided layer. + */ + void calculateCollisionHolefree(const std::vector &keys, std::function throw_on_cancel); + + /*! + * \brief Creates the areas that have to be avoided by the tree's branches to prevent collision with the model on this layer. Holes are removed. + * + * The result is a 2D area that would cause nodes of given radius to + * collide with the model or be inside a hole. Result is saved in the cache. + * A Hole is defined as an area, in which a branch with m_increase_until_radius radius would collide with the wall. + * \param key RadiusLayerPairs the requested areas. The radius will be calculated up to the provided layer. + */ + void calculateCollisionHolefree(RadiusLayerPair key) + { + calculateCollisionHolefree(std::vector{ RadiusLayerPair(key) }, {}); + } + + /*! + * \brief Creates the areas that have to be avoided by the tree's branches to prevent collision with the model. + * + * The result is a 2D area that would cause nodes of radius \p radius to + * collide with the model. Result is saved in the cache. + * \param keys RadiusLayerPairs of all requested areas. Every radius will be calculated up to the provided layer. + */ + void calculateAvoidance(const std::vector &keys, bool to_build_plate, bool to_model, std::function throw_on_cancel); + + /*! + * \brief Creates the areas that have to be avoided by the tree's branches to prevent collision with the model. + * + * The result is a 2D area that would cause nodes of radius \p radius to + * collide with the model. Result is saved in the cache. + * \param key RadiusLayerPair of the requested areas. It will be calculated up to the provided layer. + */ + void calculateAvoidance(RadiusLayerPair key, bool to_build_plate, bool to_model) + { + calculateAvoidance(std::vector{ RadiusLayerPair(key) }, to_build_plate, to_model, {}); + } + + /*! + * \brief Creates the areas where a branch of a given radius can be place on the model. + * Result is saved in the cache. + * \param key RadiusLayerPair of the requested areas. It will be calculated up to the provided layer. + */ + void calculatePlaceables(const coord_t radius, const LayerIndex max_required_layer, std::function throw_on_cancel); + + + /*! + * \brief Creates the areas where a branch of a given radius can be placed on the model. + * Result is saved in the cache. + * \param keys RadiusLayerPair of the requested areas. The radius will be calculated up to the provided layer. + */ + void calculatePlaceables(const std::vector &keys, std::function throw_on_cancel); + + /*! + * \brief Creates the areas that can not be passed when expanding an area downwards. As such these areas are an somewhat abstract representation of a wall (as in a printed object). + * + * These areas are at least xy_min_dist wide. When calculating it is always assumed that every wall is printed on top of another (as in has an overlap with the wall a layer below). Result is saved in the corresponding cache. + * + * \param keys RadiusLayerPairs of all requested areas. Every radius will be calculated up to the provided layer. + */ + void calculateWallRestrictions(const std::vector &keys, std::function throw_on_cancel); + + /*! + * \brief Creates the areas that can not be passed when expanding an area downwards. As such these areas are an somewhat abstract representation of a wall (as in a printed object). + * These areas are at least xy_min_dist wide. When calculating it is always assumed that every wall is printed on top of another (as in has an overlap with the wall a layer below). Result is saved in the corresponding cache. + * \param key RadiusLayerPair of the requested area. It well be will be calculated up to the provided layer. + */ + void calculateWallRestrictions(RadiusLayerPair key) + { + calculateWallRestrictions(std::vector{ RadiusLayerPair(key) }, {}); + } + + /*! + * \brief The maximum distance that the center point of a tree branch may move in consecutive layers if it has to avoid the model. + */ + coord_t m_max_move; + /*! + * \brief The maximum distance that the centre-point of a tree branch may + * move in consecutive layers if it does not have to avoid the model + */ + coord_t m_max_move_slow; + /*! + * \brief The smallest maximum resolution for simplify + */ + coord_t m_min_resolution; + + bool m_precalculated = false; + /*! + * \brief The index to access the outline corresponding with the currently processing mesh + */ + size_t m_current_outline_idx; + /*! + * \brief The minimum required clearance between the model and the tree branches + */ + coord_t m_current_min_xy_dist; + /*! + * \brief The difference between the minimum required clearance between the model and the tree branches and the regular one. + */ + coord_t m_current_min_xy_dist_delta; + /*! + * \brief Does at least one mesh allow support to rest on a model. + */ + bool m_support_rests_on_model; +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + /*! + * \brief The progress of the precalculate function for communicating it to the progress bar. + */ + coord_t m_precalculation_progress = 0; + /*! + * \brief The progress multiplier of all values added progress bar. + * Required for the progress bar the behave as expected when areas have to be calculated multiple times + */ + double m_progress_multiplier; + /*! + * \brief The progress offset added to all values communicated to the progress bar. + * Required for the progress bar the behave as expected when areas have to be calculated multiple times + */ + double m_progress_offset; +#endif // SLIC3R_TREESUPPORTS_PROGRESS + /*! + * \brief Increase radius in the resulting drawn branches, even if the avoidance does not allow it. Will be cut later to still fit. + */ + coord_t m_increase_until_radius; + + /*! + * \brief Polygons representing the limits of the printable area of the + * machine + */ + Polygons m_machine_border; + /*! + * \brief Storage for layer outlines and the corresponding settings of the meshes grouped by meshes with identical setting. + */ + std::vector>> m_layer_outlines; + /*! + * \brief Storage for areas that should be avoided, like support blocker or previous generated trees. + */ + std::vector m_anti_overhang; + /*! + * \brief Radii that can be ignored by ceilRadius as they will never be requested, sorted. + */ + std::vector m_ignorable_radii; + + /*! + * \brief Smallest radius a branch can have. This is the radius of a SupportElement with DTT=0. + */ + coord_t m_radius_0; + + /*! + * \brief Caches for the collision, avoidance and areas on the model where support can be placed safely + * at given radius and layer indices. + */ + RadiusLayerPolygonCache m_collision_cache; + RadiusLayerPolygonCache m_collision_cache_holefree; + RadiusLayerPolygonCache m_avoidance_cache; + RadiusLayerPolygonCache m_avoidance_cache_slow; + RadiusLayerPolygonCache m_avoidance_cache_to_model; + RadiusLayerPolygonCache m_avoidance_cache_to_model_slow; + RadiusLayerPolygonCache m_placeable_areas_cache; + + /*! + * \brief Caches to avoid holes smaller than the radius until which the radius is always increased, as they are free of holes. + * Also called safe avoidances, as they are safe regarding not running into holes. + */ + RadiusLayerPolygonCache m_avoidance_cache_holefree; + RadiusLayerPolygonCache m_avoidance_cache_holefree_to_model; + + RadiusLayerPolygonCache& avoidance_cache(const AvoidanceType type, const bool to_model) { + if (to_model) { + switch (type) { + case AvoidanceType::Fast: return m_avoidance_cache_to_model; + case AvoidanceType::Slow: return m_avoidance_cache_to_model_slow; + case AvoidanceType::Count: assert(false); + case AvoidanceType::FastSafe: return m_avoidance_cache_holefree_to_model; + } + } else { + switch (type) { + case AvoidanceType::Fast: return m_avoidance_cache; + case AvoidanceType::Slow: return m_avoidance_cache_slow; + case AvoidanceType::Count: assert(false); + case AvoidanceType::FastSafe: return m_avoidance_cache_holefree; + } + } + assert(false); + return m_avoidance_cache; + } + const RadiusLayerPolygonCache& avoidance_cache(const AvoidanceType type, const bool to_model) const { + return const_cast(this)->avoidance_cache(type, to_model); + } + + /*! + * \brief Caches to represent walls not allowed to be passed over. + */ + RadiusLayerPolygonCache m_wall_restrictions_cache; + + // A different cache for min_xy_dist as the maximal safe distance an influence area can be increased(guaranteed overlap of two walls in consecutive layer) + // is much smaller when min_xy_dist is used. This causes the area of the wall restriction to be thinner and as such just using the min_xy_dist wall + // restriction would be slower. + RadiusLayerPolygonCache m_wall_restrictions_cache_min; + +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + std::unique_ptr m_critical_progress { std::make_unique() }; +#endif // SLIC3R_TREESUPPORTS_PROGRESS +}; + +} // namespace TreeSupport3D +} // namespace Slic3r + +#endif //slic3r_TreeModelVolumes_hpp diff --git a/src/libslic3r/TreeSupport.cpp b/src/libslic3r/TreeSupport.cpp index 830a3934c..5b70e0818 100644 --- a/src/libslic3r/TreeSupport.cpp +++ b/src/libslic3r/TreeSupport.cpp @@ -1,4 +1,5 @@ #include +#include #include "MinimumSpanningTree.hpp" #include "TreeSupport.hpp" @@ -11,10 +12,17 @@ #include "ShortestPath.hpp" #include "I18N.hpp" #include +#include "TreeModelVolumes.hpp" +#include "TreeSupport3D.hpp" +#include "SupportMaterial.hpp" +#include "Fill/FillBase.hpp" +#include "BuildVolume.hpp" +#include "ClipperUtils.hpp" #define _L(s) Slic3r::I18N::translate(s) #define USE_PLAN_LAYER_HEIGHTS 1 +#define HEIGHT_TO_SWITCH_INFILL_DIRECTION 30 // change infill direction every 20mm #ifndef M_PI #define M_PI 3.1415926535897932384626433832795 @@ -24,6 +32,7 @@ #endif #define TAU (2.0 * M_PI) #define NO_INDEX (std::numeric_limits::max()) +#define USE_SUPPORT_3D 1 //#define SUPPORT_TREE_DEBUG_TO_SVG @@ -33,15 +42,9 @@ namespace Slic3r { #define unscale_(val) ((val) * SCALING_FACTOR) +#define FIRST_LAYER_EXPANSION 1.2 -inline unsigned int round_divide(unsigned int dividend, unsigned int divisor) //!< Return dividend divided by divisor rounded to the nearest integer -{ - return (dividend + divisor / 2) / divisor; -} -inline unsigned int round_up_divide(unsigned int dividend, unsigned int divisor) //!< Return dividend divided by divisor rounded to the nearest integer -{ - return (dividend + divisor - 1) / divisor; -} +extern void generate_tree_support_3D(PrintObject& print_object, TreeSupport* tree_support, std::function throw_on_cancel); inline double dot_with_unscale(const Point a, const Point b) { @@ -53,15 +56,6 @@ inline double vsize2_with_unscale(const Point pt) return dot_with_unscale(pt, pt); } -inline Point turn90_ccw(const Point pt) -{ - Point ret; - - ret(0) = -pt(1); - ret(1) = pt(0); - return ret; -} - inline Point normal(Point pt, double scale) { double length = scale_(sqrt(vsize2_with_unscale(pt))); @@ -230,8 +224,8 @@ static void draw_contours_and_nodes_to_svg svg.draw_grid(bbox, "gray", coord_t(scale_(0.05))); // draw overhang areas - svg.draw_outline(union_ex(overhangs), colors[0]); - svg.draw_outline(union_ex(overhangs_after_offset), colors[1]); + svg.draw_outline(overhangs, colors[0]); + svg.draw_outline(overhangs_after_offset, colors[1]); svg.draw_outline(outlines_below, colors[2]); // draw legend @@ -664,6 +658,7 @@ static Point bounding_box_middle(const BoundingBox &bbox) TreeSupport::TreeSupport(PrintObject& object, const SlicingParameters &slicing_params) : m_object(&object), m_slicing_params(slicing_params), m_object_config(&object.config()) { + m_print_config = &m_object->print()->config(); m_raft_layers = slicing_params.base_raft_layers + slicing_params.interface_raft_layers; support_type = m_object_config->support_type; support_style = m_object_config->support_style; @@ -1585,10 +1580,14 @@ void TreeSupport::generate_toolpaths() if(m_object_config->support_base_pattern==smpDefault) need_infill &= area_group.need_infill; if (layer_id>0 && area_group.dist_to_top < 10 && !need_infill && support_style!=smsTreeHybrid) { +#if 0 if (area_group.dist_to_top < 5) // 1 wall at the top <5mm make_perimeter_and_inner_brim(ts_layer->support_fills.entities, poly, 1, flow, erSupportMaterial); else // at least 2 walls for range [5,10) make_perimeter_and_inner_brim(ts_layer->support_fills.entities, poly, std::max(wall_count, size_t(2)), flow, erSupportMaterial); +#else + fill_expolygons_with_sheath_generate_paths(ts_layer->support_fills.entities, to_polygons(poly), nullptr, 0, erSupportMaterial, flow, true, false); +#endif } else if (layer_id > 0 && need_infill && m_support_params.base_fill_pattern != ipLightning) { std::shared_ptr filler_support = std::shared_ptr(Fill::new_from_type(m_support_params.base_fill_pattern)); @@ -1874,6 +1873,19 @@ Polygons TreeSupport::contact_nodes_to_polygon(const std::vector& contact return polys; } +void TreeSupport::clear_contact_nodes(std::vector>& contact_nodes) +{ + for (auto& layer : contact_nodes) + { + for (Node* p_node : layer) + { + delete p_node; + } + layer.clear(); + } + contact_nodes.clear(); +} + void TreeSupport::generate() { @@ -1881,7 +1893,14 @@ void TreeSupport::generate() if (!tree_support_enable) return; - std::vector> contact_nodes(m_object->layers().size()); + auto t_start = std::chrono::high_resolution_clock::now(); + +#if USE_SUPPORT_3D + if (support_style == smsTreeOrganic) { + generate_tree_support_3D(*m_object, this, this->throw_on_cancel); + return; + } +#endif profiler.stage_start(STAGE_total); @@ -1895,6 +1914,7 @@ void TreeSupport::generate() m_ts_data->is_slim = is_slim; // Generate contact points of tree support + std::vector> contact_nodes(m_object->layers().size()); profiler.stage_start(STAGE_GENERATE_CONTACT_NODES); m_object->print()->set_status(56, _L("Support: generate contact points")); generate_contact_points(contact_nodes); @@ -1919,13 +1939,7 @@ void TreeSupport::generate() draw_circles(contact_nodes); profiler.stage_finish(STAGE_DRAW_CIRCLES); - for (auto& layer : contact_nodes) { - for (Node* p_node : layer) { - delete p_node; - } - layer.clear(); - } - contact_nodes.clear(); + clear_contact_nodes(contact_nodes); profiler.stage_start(STAGE_GENERATE_TOOLPATHS); m_object->print()->set_status(69, _L("Support: generate toolpath")); @@ -2087,7 +2101,7 @@ void TreeSupport::draw_circles(const std::vector>& contact_no // Use square support if there are too many nodes per layer because circle support needs much longer time to compute // Hower circle support can be printed faster, so we prefer circle for fewer nodes case. const bool SQUARE_SUPPORT = avg_node_per_layer > 200; - const int CIRCLE_RESOLUTION = SQUARE_SUPPORT ? 4 : 100; // The number of vertices in each circle. + const int CIRCLE_RESOLUTION = SQUARE_SUPPORT ? 4 : 25; // The number of vertices in each circle. for (int i = 0; i < CIRCLE_RESOLUTION; i++) @@ -2492,6 +2506,7 @@ void TreeSupport::draw_circles(const std::vector>& contact_no } else if (holePropagationInfos.find(&hole) != holePropagationInfos.end() && std::get<0>(holePropagationInfos[&hole]) > 0 && base_area_lower.contour.contains(std::get<2>(holePropagationInfos[&hole]))) { + // after the hole connects to contour, shrink it gradually until it vanishes while moving it outwards. The moving direction is defined in the previous step Polygon hole_lower = hole; auto&& direction = std::get<1>(holePropagationInfos[&hole]); hole_lower.translate(direction); @@ -2516,7 +2531,7 @@ void TreeSupport::draw_circles(const std::vector>& contact_no hole_reoriented.make_counter_clockwise(); else if (roof1.contour.is_clockwise()) hole_reoriented.make_clockwise(); - auto tmp = union_({ roof1.contour }, { hole_reoriented }); + auto tmp = union_({ roof1.contour , hole_reoriented }); if (!tmp.empty()) roof1.contour = tmp[0]; // make sure 1) roof1 and object 2) roof1 and roof, won't intersect @@ -2544,10 +2559,10 @@ void TreeSupport::draw_circles(const std::vector>& contact_no ExPolygons& base_areas = ts_layer->base_areas; ExPolygons& roof_areas = ts_layer->roof_areas; ExPolygons& roof_1st_layer = ts_layer->roof_1st_layer; - ExPolygons& floor_areas = ts_layer->floor_areas; + ExPolygons roofs = roof_areas; append(roofs, roof_1st_layer); if (base_areas.empty() && roof_areas.empty() && roof_1st_layer.empty()) continue; char fname[10]; sprintf(fname, "%d_%.2f", layer_nr, ts_layer->print_z); - draw_contours_and_nodes_to_svg("", base_areas, roof_areas, roof_1st_layer, {}, {}, get_svg_filename(fname, "circles"), {"base", "roof", "roof1st"}); + draw_contours_and_nodes_to_svg("", base_areas, roofs, ts_layer->lslices, {}, {}, get_svg_filename(fname, "circles"), { "base", "roof", "lslices" }, { "blue","red","black" }); } #endif // SUPPORT_TREE_DEBUG_TO_SVG @@ -2578,7 +2593,6 @@ void TreeSupport::drop_nodes(std::vector>& contact_nodes) const bool support_on_buildplate_only = config.support_on_build_plate_only.value; const size_t bottom_interface_layers = config.support_interface_bottom_layers.value; const size_t top_interface_layers = config.support_interface_top_layers.value; - float DO_NOT_MOVER_UNDER_MM = is_slim ? 0 : 5; // do not move contact points under 5mm auto get_branch_angle = [this,&config](coordf_t radius) { if (config.tree_support_branch_angle.value < 30.0) return config.tree_support_branch_angle.value; @@ -2992,7 +3006,9 @@ void TreeSupport::drop_nodes(std::vector>& contact_nodes) } // move to the averaged direction of neighbor center and contour edge if they are roughly same direction Point movement; - if (!is_strong) + if (support_on_buildplate_only) + movement = move_to_neighbor_center + direction_to_outer * 2; + else if (!is_strong) movement = move_to_neighbor_center*2 + (dist2_to_outer > EPSILON ? direction_to_outer * (1 / dist2_to_outer) : Point(0, 0)); else { if (movement.dot(move_to_neighbor_center) >= 0.2 || move_to_neighbor_center == Point(0, 0)) @@ -3354,7 +3370,7 @@ std::vector TreeSupport::plan_layer_heights(std::vectornext_layer_nr: " << layer_heights[i].print_z << " " << layer_heights[i].height << " " + BOOST_LOG_TRIVIAL(info) << "plan_layer_heights print_z, height, layer_nr->next_layer_nr: " << layer_heights[i].print_z << " " << layer_heights[i].height << " " << i << "->" << layer_heights[i].next_layer_nr; } @@ -3695,7 +3711,7 @@ const ExPolygons& TreeSupportData::calculate_avoidance(const RadiusLayerPair& ke const ExPolygons &collision = get_collision(radius, layer_nr); avoidance_areas.insert(avoidance_areas.end(), collision.begin(), collision.end()); avoidance_areas = std::move(union_ex(avoidance_areas)); - auto ret = m_avoidance_cache.insert({ key, std::move(avoidance_areas) }); + auto ret = m_avoidance_cache.insert({key, std::move(avoidance_areas)}); //assert(ret.second); return ret.first->second; } else { diff --git a/src/libslic3r/TreeSupport.hpp b/src/libslic3r/TreeSupport.hpp index 1a706e433..3a5635125 100644 --- a/src/libslic3r/TreeSupport.hpp +++ b/src/libslic3r/TreeSupport.hpp @@ -401,6 +401,14 @@ public: std::unique_ptr generator; std::unordered_map printZ_to_lightninglayer; + + std::function throw_on_cancel; + const PrintConfig* m_print_config; + /*! + * \brief Polygons representing the limits of the printable area of the + * machine + */ + ExPolygon m_machine_border; private: /*! * \brief Generator for model collision, avoidance and internal guide volumes @@ -410,7 +418,7 @@ private: */ std::shared_ptr m_ts_data; PrintObject *m_object; - const PrintObjectConfig *m_object_config; + const PrintObjectConfig* m_object_config; SlicingParameters m_slicing_params; // Various precomputed support parameters to be shared with external functions. SupportParams m_support_params; @@ -418,6 +426,7 @@ private: size_t m_highest_overhang_layer = 0; std::vector> m_spanning_trees; std::vector< std::unordered_map> m_mst_line_x_layer_contour_caches; + float DO_NOT_MOVER_UNDER_MM = 0.0; coordf_t MAX_BRANCH_RADIUS = 10.0; coordf_t MIN_BRANCH_RADIUS = 0.5; float tree_support_branch_diameter_angle = 5.0; @@ -426,11 +435,6 @@ private: bool with_infill = false; - /*! - * \brief Polygons representing the limits of the printable area of the - * machine - */ - ExPolygon m_machine_border; /*! * \brief Draws circles around each node of the tree into the final support. @@ -493,6 +497,7 @@ private: void generate_toolpaths(); Polygons spanning_tree_to_polygon(const std::vector& spanning_trees, Polygons layer_contours, int layer_nr); Polygons contact_nodes_to_polygon(const std::vector& contact_nodes, Polygons layer_contours, int layer_nr, std::vector& radiis, std::vector& is_interface); + void clear_contact_nodes(std::vector>& contact_nodes); coordf_t calc_branch_radius(coordf_t base_radius, size_t layers_to_top, size_t tip_layers, double diameter_angle_scale_factor); coordf_t calc_branch_radius(coordf_t base_radius, coordf_t mm_to_top, double diameter_angle_scale_factor); diff --git a/src/libslic3r/TreeSupport3D.cpp b/src/libslic3r/TreeSupport3D.cpp new file mode 100644 index 000000000..b8553662d --- /dev/null +++ b/src/libslic3r/TreeSupport3D.cpp @@ -0,0 +1,4142 @@ +// Tree supports by Thomas Rahm, losely based on Tree Supports by CuraEngine. +// Original source of Thomas Rahm's tree supports: +// https://github.com/ThomasRahm/CuraEngine +// +// Original CuraEngine copyright: +// Copyright (c) 2021 Ultimaker B.V. +// CuraEngine is released under the terms of the AGPLv3 or higher. + +#include "TreeSupport3D.hpp" +#include "AABBTreeIndirect.hpp" +#include "AABBTreeLines.hpp" +#include "BuildVolume.hpp" +#include "ClipperUtils.hpp" +#include "EdgeGrid.hpp" +#include "Fill/Fill.hpp" +#include "Layer.hpp" +#include "Print.hpp" +#include "MultiPoint.hpp" +#include "Polygon.hpp" +#include "Polyline.hpp" +#include "MutablePolygon.hpp" +#include "SupportMaterial.hpp" +#include "TriangleMeshSlicer.hpp" +#include "TreeSupport.hpp" +#include "I18N.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include + +#define TBB_PREVIEW_GLOBAL_CONTROL 1 +#include +#include +#include + +#if defined(TREE_SUPPORT_SHOW_ERRORS) && defined(_WIN32) + #define TREE_SUPPORT_SHOW_ERRORS_WIN32 +#endif + +#define TREE_SUPPORT_ORGANIC_NUDGE_NEW 1 + +#ifndef TREE_SUPPORT_ORGANIC_NUDGE_NEW + // Old version using OpenVDB, works but it is extremely slow for complex meshes. + #include "OpenVDBUtilsLegacy.hpp" + #include +#endif // TREE_SUPPORT_ORGANIC_NUDGE_NEW + +#ifndef _L +#define _L(s) Slic3r::I18N::translate(s) +#endif + + //#define TREESUPPORT_DEBUG_SVG + +namespace Slic3r +{ + +namespace TreeSupport3D +{ + +enum class LineStatus +{ + INVALID, + TO_MODEL, + TO_MODEL_GRACIOUS, + TO_MODEL_GRACIOUS_SAFE, + TO_BP, + TO_BP_SAFE +}; + +using LineInformation = std::vector>; +using LineInformations = std::vector; +using namespace std::literals; + +static inline void validate_range(const Point &pt) +{ + static constexpr const int32_t hi = 65536 * 16384; + if (pt.x() > hi || pt.y() > hi || -pt.x() > hi || -pt.y() > hi) + throw ClipperLib::clipperException("Coordinate outside allowed range"); +} + +static inline void validate_range(const Points &points) +{ + for (const Point &p : points) + validate_range(p); +} + +static inline void validate_range(const MultiPoint &mp) +{ + validate_range(mp.points); +} + +static inline void validate_range(const Polygons &polygons) +{ + for (const Polygon &p : polygons) + validate_range(p); +} + +static inline void validate_range(const Polylines &polylines) +{ + for (const Polyline &p : polylines) + validate_range(p); +} + +static inline void validate_range(const LineInformation &lines) +{ + for (const auto& p : lines) + validate_range(p.first); +} + +static inline void validate_range(const LineInformations &lines) +{ + for (const LineInformation &l : lines) + validate_range(l); +} + +static inline void check_self_intersections(const Polygons &polygons, const std::string_view message) +{ +#ifdef TREE_SUPPORT_SHOW_ERRORS_WIN32 + if (!intersecting_edges(polygons).empty()) + ::MessageBoxA(nullptr, (std::string("TreeSupport infill self intersections: ") + std::string(message)).c_str(), "Bug detected!", MB_OK | MB_SYSTEMMODAL | MB_SETFOREGROUND | MB_ICONWARNING); +#endif // TREE_SUPPORT_SHOW_ERRORS_WIN32 +} +static inline void check_self_intersections(const ExPolygon &expoly, const std::string_view message) +{ +#ifdef TREE_SUPPORT_SHOW_ERRORS_WIN32 + check_self_intersections(to_polygons(expoly), message); +#endif // TREE_SUPPORT_SHOW_ERRORS_WIN32 +} + +static constexpr const auto tiny_area_threshold = sqr(scaled(0.001)); + +static std::vector>> group_meshes(const Print &print, const std::vector &print_object_ids) +{ + std::vector>> grouped_meshes; + + //FIXME this is ugly, it does not belong here. + for (size_t object_id : print_object_ids) { + const PrintObject &print_object = *print.get_object(object_id); + const PrintObjectConfig &object_config = print_object.config(); + if (object_config.support_top_z_distance < EPSILON) + // || min_feature_size < scaled(0.1) that is the minimum line width + TreeSupportSettings::soluble = true; + } + + size_t largest_printed_mesh_idx = 0; + + // Group all meshes that can be processed together. NOTE this is different from mesh-groups! Only one setting object is needed per group, + // as different settings in the same group may only occur in the tip, which uses the original settings objects from the meshes. + for (size_t object_id : print_object_ids) { + const PrintObject &print_object = *print.get_object(object_id); +#ifndef NDEBUG + const PrintObjectConfig &object_config = print_object.config(); +#endif // NDEBUG + // Support must be enabled and set to Tree style. + //assert(object_config.support_material); + //assert(object_config.support_material_style == smsTree || object_config.support_material_style == smsOrganic); + + bool found_existing_group = false; + TreeSupportSettings next_settings{ TreeSupportMeshGroupSettings{ print_object } }; + //FIXME for now only a single object per group is enabled. +#if 0 + for (size_t idx = 0; idx < grouped_meshes.size(); ++ idx) + if (next_settings == grouped_meshes[idx].first) { + found_existing_group = true; + grouped_meshes[idx].second.emplace_back(object_id); + // handle some settings that are only used for performance reasons. This ensures that a horrible set setting intended to improve performance can not reduce it drastically. + grouped_meshes[idx].first.performance_interface_skip_layers = std::min(grouped_meshes[idx].first.performance_interface_skip_layers, next_settings.performance_interface_skip_layers); + } +#endif + if (! found_existing_group) + grouped_meshes.emplace_back(next_settings, std::vector{ object_id }); + + // no need to do this per mesh group as adaptive layers and raft setting are not setable per mesh. + if (print.get_object(largest_printed_mesh_idx)->layers().back()->print_z < print_object.layers().back()->print_z) + largest_printed_mesh_idx = object_id; + } + +#if 0 + { + std::vector known_z(storage.meshes[largest_printed_mesh_idx].layers.size()); + for (size_t z = 0; z < storage.meshes[largest_printed_mesh_idx].layers.size(); z++) + known_z[z] = storage.meshes[largest_printed_mesh_idx].layers[z].printZ; + for (size_t idx = 0; idx < grouped_meshes.size(); ++ idx) + grouped_meshes[idx].first.setActualZ(known_z); + } +#endif + + return grouped_meshes; +} + +#if 0 +// todo remove as only for debugging relevant +[[nodiscard]] static std::string getPolygonAsString(const Polygons& poly) +{ + std::string ret; + for (auto path : poly) + for (Point p : path) { + if (ret != "") + ret += ","; + ret += "(" + std::to_string(p.x()) + "," + std::to_string(p.y()) + ")"; + } + return ret; +} +#endif + +static bool inline g_showed_critical_error = false; +static bool inline g_showed_performance_warning = false; +void tree_supports_show_error(std::string_view message, bool critical) +{ // todo Remove! ONLY FOR PUBLIC BETA!! + +#ifdef TREE_SUPPORT_SHOW_ERRORS_WIN32 + static bool showed_critical = false; + static bool showed_performance = false; + auto bugtype = std::string(critical ? " This is a critical bug. It may cause missing or malformed branches.\n" : "This bug should only decrease performance.\n"); + bool show = (critical && !g_showed_critical_error) || (!critical && !g_showed_performance_warning); + (critical ? g_showed_critical_error : g_showed_performance_warning) = true; + if (show) + MessageBoxA(nullptr, std::string("TreeSupport_2 MOD detected an error while generating the tree support.\nPlease report this back to me with profile and model.\nRevision 5.0\n" + std::string(message) + "\n" + bugtype).c_str(), + "Bug detected!", MB_OK | MB_SYSTEMMODAL | MB_SETFOREGROUND | MB_ICONWARNING); +#endif // TREE_SUPPORT_SHOW_ERRORS_WIN32 +} + +[[nodiscard]] static const std::vector generate_overhangs(const PrintObject &print_object, std::function throw_on_cancel) +{ + std::vector out(print_object.layer_count(), Polygons{}); + + const PrintObjectConfig &config = print_object.config(); + const bool support_auto = is_auto(config.support_type.value); + const int support_enforce_layers = config.enforce_support_layers.value; + std::vector enforcers_layers{ print_object.slice_support_enforcers() }; + std::vector blockers_layers{ print_object.slice_support_blockers() }; + print_object.project_and_append_custom_facets(false, EnforcerBlockerType::ENFORCER, enforcers_layers); + print_object.project_and_append_custom_facets(false, EnforcerBlockerType::BLOCKER, blockers_layers); + const int support_threshold = config.support_threshold_angle.value; + const bool support_threshold_auto = support_threshold == 0; + // +1 makes the threshold inclusive + double tan_threshold = support_threshold_auto ? 0. : tan(M_PI * double(support_threshold + 1) / 180.); + //FIXME this is a fudge constant! + double support_tree_tip_diameter = 0.8; + auto enforcer_overhang_offset = scaled(support_tree_tip_diameter); + + tbb::parallel_for(tbb::blocked_range(1, out.size()), + [&print_object, &enforcers_layers, &blockers_layers, support_auto, support_enforce_layers, support_threshold_auto, tan_threshold, enforcer_overhang_offset, &throw_on_cancel, &out] + (const tbb::blocked_range &range) { + for (LayerIndex layer_id = range.begin(); layer_id < range.end(); ++ layer_id) { + const Layer ¤t_layer = *print_object.get_layer(layer_id); + const Layer &lower_layer = *print_object.get_layer(layer_id - 1); + // Full overhangs with zero lower_layer_offset and no blockers applied. + Polygons raw_overhangs; + bool raw_overhangs_calculated = false; + // Final overhangs. + Polygons overhangs; + // For how many layers full overhangs shall be supported. + const bool enforced_layer = layer_id < support_enforce_layers; + if (support_auto || enforced_layer) { + float lower_layer_offset; + if (enforced_layer) + lower_layer_offset = 0; + else if (support_threshold_auto) { + float external_perimeter_width = 0; + for (const LayerRegion *layerm : lower_layer.regions()) + external_perimeter_width += layerm->flow(frExternalPerimeter).scaled_width(); + external_perimeter_width /= lower_layer.region_count(); + lower_layer_offset = float(0.5 * external_perimeter_width); + } else + lower_layer_offset = scaled(lower_layer.height / tan_threshold); + overhangs = lower_layer_offset == 0 ? + diff(current_layer.lslices, lower_layer.lslices) : + diff(current_layer.lslices, offset(lower_layer.lslices, lower_layer_offset)); + if (lower_layer_offset == 0) { + raw_overhangs = overhangs; + raw_overhangs_calculated = true; + } + if (! (enforced_layer || blockers_layers.empty() || blockers_layers[layer_id].empty())) + overhangs = diff(overhangs, blockers_layers[layer_id], ApplySafetyOffset::Yes); + } + //check_self_intersections(overhangs, "generate_overhangs1"); + if (! enforcers_layers.empty() && ! enforcers_layers[layer_id].empty()) { + // Has some support enforcers at this layer, apply them to the overhangs, don't apply the support threshold angle. + //enforcers_layers[layer_id] = union_(enforcers_layers[layer_id]); + //check_self_intersections(enforcers_layers[layer_id], "generate_overhangs - enforcers"); + //check_self_intersections(to_polygons(lower_layer.lslices), "generate_overhangs - lowerlayers"); + if (Polygons enforced_overhangs = intersection(raw_overhangs_calculated ? raw_overhangs : diff(current_layer.lslices, lower_layer.lslices), enforcers_layers[layer_id] /*, ApplySafetyOffset::Yes */); + ! enforced_overhangs.empty()) { + //FIXME this is a hack to make enforcers work on steep overhangs. + //check_self_intersections(enforced_overhangs, "generate_overhangs - enforced overhangs1"); + //Polygons enforced_overhangs_prev = enforced_overhangs; + //check_self_intersections(to_polygons(union_ex(enforced_overhangs)), "generate_overhangs - enforced overhangs11"); + //check_self_intersections(offset(union_ex(enforced_overhangs), + //FIXME enforcer_overhang_offset is a fudge constant! + enforced_overhangs = diff(offset(union_ex(enforced_overhangs), enforcer_overhang_offset), + lower_layer.lslices); +#ifdef TREESUPPORT_DEBUG_SVG +// if (! intersecting_edges(enforced_overhangs).empty()) + { + static int irun = 0; + SVG::export_expolygons(debug_out_path("treesupport-self-intersections-%d.svg", ++irun), + { { { current_layer.lslices }, { "current_layer.lslices", "yellow", 0.5f } }, + { { lower_layer.lslices }, { "lower_layer.lslices", "gray", 0.5f } }, + { { union_ex(enforced_overhangs) }, { "enforced_overhangs", "red", "black", "", scaled(0.1f), 0.5f } } }); + } +#endif // TREESUPPORT_DEBUG_SVG + //check_self_intersections(enforced_overhangs, "generate_overhangs - enforced overhangs2"); + overhangs = overhangs.empty() ? std::move(enforced_overhangs) : union_(overhangs, enforced_overhangs); + //check_self_intersections(overhangs, "generate_overhangs - enforcers"); + } + } + out[layer_id] = std::move(overhangs); + throw_on_cancel(); + } + }); + + return out; +} + +/*! + * \brief Precalculates all avoidances, that could be required. + * + * \param storage[in] Background storage to access meshes. + * \param currently_processing_meshes[in] Indexes of all meshes that are processed in this iteration + */ +[[nodiscard]] static LayerIndex precalculate(const Print &print, const std::vector &overhangs, const TreeSupportSettings &config, const std::vector &object_ids, TreeModelVolumes &volumes, std::function throw_on_cancel) +{ + // calculate top most layer that is relevant for support + LayerIndex max_layer = 0; + for (size_t object_id : object_ids) { + const PrintObject &print_object = *print.get_object(object_id); + int max_support_layer_id = 0; + for (int layer_id = 1; layer_id < int(print_object.layer_count()); ++ layer_id) + if (! overhangs[layer_id].empty()) + max_support_layer_id = layer_id; + max_layer = std::max(max_support_layer_id - int(config.z_distance_top_layers), 0); + } + if (max_layer > 0) + // The actual precalculation happens in TreeModelVolumes. + volumes.precalculate(max_layer, throw_on_cancel); + return max_layer; +} + +/*! + * \brief Converts a Polygons object representing a line into the internal format. + * + * \param polylines[in] The Polyline that will be converted. + * \param layer_idx[in] The current layer. + * \return All lines of the \p polylines object, with information for each point regarding in which avoidance it is currently valid in. + */ +// Called by generate_initial_areas() +[[nodiscard]] static LineInformations convert_lines_to_internal( + const TreeModelVolumes &volumes, const TreeSupportSettings &config, + const Polylines &polylines, LayerIndex layer_idx) +{ + const bool min_xy_dist = config.xy_distance > config.xy_min_distance; + + LineInformations result; + // Also checks if the position is valid, if it is NOT, it deletes that point + for (const Polyline &line : polylines) { + LineInformation res_line; + for (Point p : line) { + if (! contains(volumes.getAvoidance(config.getRadius(0), layer_idx, TreeModelVolumes::AvoidanceType::FastSafe, false, min_xy_dist), p)) + res_line.emplace_back(p, LineStatus::TO_BP_SAFE); + else if (! contains(volumes.getAvoidance(config.getRadius(0), layer_idx, TreeModelVolumes::AvoidanceType::Fast, false, min_xy_dist), p)) + res_line.emplace_back(p, LineStatus::TO_BP); + else if (config.support_rests_on_model && ! contains(volumes.getAvoidance(config.getRadius(0), layer_idx, TreeModelVolumes::AvoidanceType::FastSafe, true, min_xy_dist), p)) + res_line.emplace_back(p, LineStatus::TO_MODEL_GRACIOUS_SAFE); + else if (config.support_rests_on_model && ! contains(volumes.getAvoidance(config.getRadius(0), layer_idx, TreeModelVolumes::AvoidanceType::Fast, true, min_xy_dist), p)) + res_line.emplace_back(p, LineStatus::TO_MODEL_GRACIOUS); + else if (config.support_rests_on_model && ! contains(volumes.getCollision(config.getRadius(0), layer_idx, min_xy_dist), p)) + res_line.emplace_back(p, LineStatus::TO_MODEL); + else if (!res_line.empty()) { + result.emplace_back(res_line); + res_line.clear(); + } + } + if (!res_line.empty()) { + result.emplace_back(res_line); + res_line.clear(); + } + } + + validate_range(result); + return result; +} + +/*! + * \brief Converts lines in internal format into a Polygons object representing these lines. + * + * \param lines[in] The lines that will be converted. + * \return All lines of the \p lines object as a Polygons object. + */ +[[nodiscard]] static Polylines convert_internal_to_lines(LineInformations lines) +{ + Polylines result; + for (LineInformation line : lines) { + Polyline path; + for (auto point_data : line) + path.points.emplace_back(point_data.first); + result.emplace_back(std::move(path)); + } + validate_range(result); + return result; +} + +/*! + * \brief Evaluates if a point has to be added now. Required for a split_lines call in generate_initial_areas(). + * + * \param current_layer[in] The layer on which the point lies, point and its status. + * \return whether the point is valid. + */ +[[nodiscard]] static bool evaluate_point_for_next_layer_function( + const TreeModelVolumes &volumes, const TreeSupportSettings &config, + size_t current_layer, const std::pair &p) +{ + using AvoidanceType = TreeModelVolumes::AvoidanceType; + const bool min_xy_dist = config.xy_distance > config.xy_min_distance; + if (! contains(volumes.getAvoidance(config.getRadius(0), current_layer - 1, p.second == LineStatus::TO_BP_SAFE ? AvoidanceType::FastSafe : AvoidanceType::Fast, false, min_xy_dist), p.first)) + return true; + if (config.support_rests_on_model && (p.second != LineStatus::TO_BP && p.second != LineStatus::TO_BP_SAFE)) + return ! contains( + p.second == LineStatus::TO_MODEL_GRACIOUS || p.second == LineStatus::TO_MODEL_GRACIOUS_SAFE ? + volumes.getAvoidance(config.getRadius(0), current_layer - 1, p.second == LineStatus::TO_MODEL_GRACIOUS_SAFE ? AvoidanceType::FastSafe : AvoidanceType::Fast, true, min_xy_dist) : + volumes.getCollision(config.getRadius(0), current_layer - 1, min_xy_dist), + p.first); + return false; +} + +/*! + * \brief Evaluates which points of some lines are not valid one layer below and which are. Assumes all points are valid on the current layer. Validity is evaluated using supplied lambda. + * + * \param lines[in] The lines that have to be evaluated. + * \param evaluatePoint[in] The function used to evaluate the points. + * \return A pair with which points are still valid in the first slot and which are not in the second slot. + */ +template +[[nodiscard]] static std::pair split_lines(const LineInformations &lines, EvaluatePointFn evaluatePoint) +{ + // assumes all Points on the current line are valid + + LineInformations keep; + LineInformations set_free; + for (const std::vector> &line : lines) { + bool current_keep = true; + LineInformation resulting_line; + for (const std::pair &me : line) { + if (evaluatePoint(me) != current_keep) { + if (! resulting_line.empty()) + (current_keep ? &keep : &set_free)->emplace_back(std::move(resulting_line)); + current_keep = !current_keep; + } + resulting_line.emplace_back(me); + } + if (! resulting_line.empty()) + (current_keep ? &keep : &set_free)->emplace_back(std::move(resulting_line)); + } + validate_range(keep); + validate_range(set_free); + return std::pair>>, std::vector>>>(keep, set_free); +} + +// Ported from CURA's PolygonUtils::getNextPointWithDistance() +// Sample a next point at distance "dist" from start_pt on polyline segment (start_idx, start_idx + 1). +// Returns sample point and start index of its segment on polyline if such sample exists. +static std::optional> polyline_sample_next_point_at_distance(const Points &polyline, const Point &start_pt, size_t start_idx, double dist) +{ + const double dist2 = sqr(dist); + const auto dist2i = int64_t(dist2); + static constexpr const auto eps = scaled(0.01); + + for (size_t i = start_idx + 1; i < polyline.size(); ++ i) { + const Point p1 = polyline[i]; + if ((p1 - start_pt).cast().squaredNorm() >= dist2i) { + // The end point is outside the circle with center "start_pt" and radius "dist". + const Point p0 = polyline[i - 1]; + Vec2d v = (p1 - p0).cast(); + double l2v = v.squaredNorm(); + if (l2v < sqr(eps)) { + // Very short segment. + Point c = (p0 + p1) / 2; + if (std::abs((start_pt - c).cast().norm() - dist) < eps) + return std::pair{ c, i - 1 }; + else + continue; + } + Vec2d p0f = (start_pt - p0).cast(); + // Foot point of start_pt into v. + Vec2d foot_pt = v * (p0f.dot(v) / l2v); + // Vector from foot point of "start_pt" to "start_pt". + Vec2d xf = p0f - foot_pt; + // Squared distance of "start_pt" from the ray (p0, p1). + double l2_from_line = xf.squaredNorm(); + double det = dist2 - l2_from_line; + + if (det > - SCALED_EPSILON) { + // The ray (p0, p1) touches or intersects a circle centered at "start_pt" with radius "dist". + // Distance of the circle intersection point from the foot point. + double dist_circle_intersection = std::sqrt(std::max(0., det)); + if ((v - foot_pt).cast().norm() > dist_circle_intersection) { + // Intersection of the circle with the segment (p0, p1) is on the right side (close to p1) from the foot point. + Point p = p0 + (foot_pt + v * (dist_circle_intersection / sqrt(l2v))).cast(); + validate_range(p); + return std::pair{ p, i - 1 }; + } + } + } + } + return {}; +} + +/*! + * \brief Eensures that every line segment is about distance in length. The resulting lines may differ from the original but all points are on the original + * + * \param input[in] The lines on which evenly spaced points should be placed. + * \param distance[in] The distance the points should be from each other. + * \param min_points[in] The amount of points that have to be placed. If not enough can be placed the distance will be reduced to place this many points. + * \return A Polygons object containing the evenly spaced points. Does not represent an area, more a collection of points on lines. + */ +[[nodiscard]] static Polylines ensure_maximum_distance_polyline(const Polylines &input, double distance, size_t min_points) +{ + Polylines result; + for (Polyline part : input) { + if (part.empty()) + continue; + + double len = length(part.points); + Polyline line; + double current_distance = std::max(distance, scaled(0.1)); + if (len < 2 * distance && min_points <= 1) + { + // Insert the opposite point of the first one. + //FIXME pretty expensive + Polyline pl(part); + pl.clip_end(len / 2); + line.points.emplace_back(pl.points.back()); + } + else + { + size_t optimal_end_index = part.size() - 1; + + if (part.front() == part.back()) { + size_t optimal_start_index = 0; + // If the polyline was a polygon, there is a high chance it was an overhang. Overhangs that are <60� tend to be very thin areas, so lets get the beginning and end of them and ensure that they are supported. + // The first point of the line will always be supported, so rotate the order of points in this polyline that one of the two corresponding points that are furthest from each other is in the beginning. + // The other will be manually added (optimal_end_index) + coord_t max_dist2_between_vertecies = 0; + for (size_t idx = 0; idx < part.size() - 1; ++ idx) { + for (size_t inner_idx = 0; inner_idx < part.size() - 1; inner_idx++) { + if ((part[idx] - part[inner_idx]).cast().squaredNorm() > max_dist2_between_vertecies) { + optimal_start_index = idx; + optimal_end_index = inner_idx; + max_dist2_between_vertecies = (part[idx] - part[inner_idx]).cast().squaredNorm(); + } + } + } + std::rotate(part.begin(), part.begin() + optimal_start_index, part.end() - 1); + part[part.size() - 1] = part[0]; // restore that property that this polyline ends where it started. + optimal_end_index = (part.size() + optimal_end_index - optimal_start_index - 1) % (part.size() - 1); + } + + while (line.size() < min_points && current_distance >= scaled(0.1)) + { + line.clear(); + Point current_point = part[0]; + line.points.emplace_back(part[0]); + if (min_points > 1 || (part[0] - part[optimal_end_index]).cast().norm() > current_distance) + line.points.emplace_back(part[optimal_end_index]); + size_t current_index = 0; + std::optional> next_point; + double next_distance = current_distance; + // Get points so that at least min_points are added and they each are current_distance away from each other. If that is impossible, decrease current_distance a bit. + // The input are lines, that means that the line from the last to the first vertex does not have to exist, so exclude all points that are on this line! + while ((next_point = polyline_sample_next_point_at_distance(part.points, current_point, current_index, next_distance))) { + // Not every point that is distance away, is valid, as it may be much closer to another point. This is especially the case when the overhang is very thin. + // So this ensures that the points are actually a certain distance from each other. + // This assurance is only made on a per polygon basis, as different but close polygon may not be able to use support below the other polygon. + double min_distance_to_existing_point = std::numeric_limits::max(); + for (Point p : line) + min_distance_to_existing_point = std::min(min_distance_to_existing_point, (p - next_point->first).cast().norm()); + if (min_distance_to_existing_point >= current_distance) { + // viable point was found. Add to possible result. + line.points.emplace_back(next_point->first); + current_point = next_point->first; + current_index = next_point->second; + next_distance = current_distance; + } else { + if (current_point == next_point->first) { + // In case a fixpoint is encountered, better aggressively overcompensate so the code does not become stuck here... + BOOST_LOG_TRIVIAL(warning) << "Tree Support: Encountered a fixpoint in polyline_sample_next_point_at_distance. This is expected to happen if the distance (currently " << next_distance << + ") is smaller than 100"; + tree_supports_show_error("Encountered issue while placing tips. Some tips may be missing."sv, true); + if (next_distance > 2 * current_distance) + // This case should never happen, but better safe than sorry. + break; + next_distance += current_distance; + continue; + } + // if the point was too close, the next possible viable point is at least distance-min_distance_to_existing_point away from the one that was just checked. + next_distance = std::max(current_distance - min_distance_to_existing_point, scaled(0.1)); + current_point = next_point->first; + current_index = next_point->second; + } + } + current_distance *= 0.9; + } + } + result.emplace_back(std::move(line)); + } + validate_range(result); + return result; +} + +/*! + * \brief Returns Polylines representing the (infill) lines that will result in slicing the given area + * + * \param area[in] The area that has to be filled with infill. + * \param roof[in] Whether the roofing or regular support settings should be used. + * \param layer_idx[in] The current layer index. + * \param support_infill_distance[in] The distance that should be between the infill lines. + * + * \return A Polygons object that represents the resulting infill lines. + */ +[[nodiscard]] static Polylines generate_support_infill_lines( + const Polygons &polygon, const SupportParameters &support_params, + bool roof, LayerIndex layer_idx, coord_t support_infill_distance) +{ +#if 0 + Polygons gaps; + // as we effectivly use lines to place our supportPoints we may use the Infill class for it, while not made for it it works perfect + + const EFillMethod pattern = roof ? config.roof_pattern : config.support_pattern; + +// const bool zig_zaggify_infill = roof ? pattern == EFillMethod::ZIG_ZAG : config.zig_zaggify_support; + const bool connect_polygons = false; + constexpr coord_t support_roof_overlap = 0; + constexpr size_t infill_multiplier = 1; + constexpr coord_t outline_offset = 0; + const int support_shift = roof ? 0 : support_infill_distance / 2; + const size_t wall_line_count = include_walls && !roof ? config.support_wall_count : 0; + const Point infill_origin; + constexpr Polygons* perimeter_gaps = nullptr; + constexpr bool use_endpieces = true; + const bool connected_zigzags = roof ? false : config.connect_zigzags; + const size_t zag_skip_count = roof ? 0 : config.zag_skip_count; + constexpr coord_t pocket_size = 0; + std::vector angles = roof ? config.support_roof_angles : config.support_infill_angles; + std::vector toolpaths; + + const coord_t z = config.getActualZ(layer_idx); + int divisor = static_cast(angles.size()); + int index = ((layer_idx % divisor) + divisor) % divisor; + const AngleRadians fill_angle = angles[index]; + Infill roof_computation(pattern, true /* zig_zaggify_infill */, connect_polygons, polygon, + roof ? config.support_roof_line_width : config.support_line_width, support_infill_distance, support_roof_overlap, infill_multiplier, + fill_angle, z, support_shift, config.resolution, wall_line_count, infill_origin, + perimeter_gaps, connected_zigzags, use_endpieces, false /* skip_some_zags */, zag_skip_count, pocket_size); + Polygons polygons; + Polygons lines; + roof_computation.generate(toolpaths, polygons, lines, config.settings); + append(lines, to_polylines(polygons)); + return lines; +#else + const Flow &flow = roof ? support_params.support_material_interface_flow : support_params.support_material_flow; + std::unique_ptr filler = std::unique_ptr(Fill::new_from_type(roof ? support_params.interface_fill_pattern : support_params.base_fill_pattern)); + FillParams fill_params; + + filler->layer_id = layer_idx; + filler->spacing = flow.spacing(); + filler->angle = roof ? + //fixme support_layer.interface_id() instead of layer_idx + (support_params.interface_angle + (layer_idx & 1) ? float(- M_PI / 4.) : float(+ M_PI / 4.)) : + support_params.base_angle; + + fill_params.density = float(roof ? support_params.interface_density : scaled(filler->spacing) / (scaled(filler->spacing) + float(support_infill_distance))); + fill_params.dont_adjust = true; + + Polylines out; + for (ExPolygon &expoly : union_ex(polygon)) { + // The surface type does not matter. + assert(area(expoly) > 0.); +#ifdef TREE_SUPPORT_SHOW_ERRORS_WIN32 + if (area(expoly) <= 0.) + ::MessageBoxA(nullptr, "TreeSupport infill negative area", "Bug detected!", MB_OK | MB_SYSTEMMODAL | MB_SETFOREGROUND | MB_ICONWARNING); +#endif // TREE_SUPPORT_SHOW_ERRORS_WIN32 + assert(intersecting_edges(to_polygons(expoly)).empty()); + check_self_intersections(expoly, "generate_support_infill_lines"); + Surface surface(stInternal, std::move(expoly)); + try { + Polylines pl = filler->fill_surface(&surface, fill_params); + assert(pl.empty() || get_extents(surface.expolygon).inflated(SCALED_EPSILON).contains(get_extents(pl))); +#ifdef TREE_SUPPORT_SHOW_ERRORS_WIN32 + if (! pl.empty() && ! get_extents(surface.expolygon).inflated(SCALED_EPSILON).contains(get_extents(pl))) + ::MessageBoxA(nullptr, "TreeSupport infill failure", "Bug detected!", MB_OK | MB_SYSTEMMODAL | MB_SETFOREGROUND | MB_ICONWARNING); +#endif // TREE_SUPPORT_SHOW_ERRORS_WIN32 + append(out, std::move(pl)); + } catch (InfillFailedException &) { + } + } + validate_range(out); + return out; +#endif +} + +/*! + * \brief Unions two Polygons. Ensures that if the input is non empty that the output also will be non empty. + * \param first[in] The first Polygon. + * \param second[in] The second Polygon. + * \return The union of both Polygons + */ +[[nodiscard]] static Polygons safe_union(const Polygons first, const Polygons second = {}) +{ + // unionPolygons can slowly remove Polygons under certain circumstances, because of rounding issues (Polygons that have a thin area). + // This does not cause a problem when actually using it on large areas, but as influence areas (representing centerpoints) can be very thin, this does occur so this ugly workaround is needed + // Here is an example of a Polygons object that will loose vertices when unioning, and will be gone after a few times unionPolygons was called: + /* + Polygons example; + Polygon exampleInner; + exampleInner.add(Point(120410,83599));//A + exampleInner.add(Point(120384,83643));//B + exampleInner.add(Point(120399,83618));//C + exampleInner.add(Point(120414,83591));//D + exampleInner.add(Point(120423,83570));//E + exampleInner.add(Point(120419,83580));//F + example.add(exampleInner); + for(int i=0;i<10;i++){ + log("Iteration %d Example area: %f\n",i,area(example)); + example=example.unionPolygons(); + } +*/ + + Polygons result; + if (! first.empty() || ! second.empty()) { + result = union_(first, second); + if (result.empty()) { + BOOST_LOG_TRIVIAL(debug) << "Caught an area destroying union, enlarging areas a bit."; + // just take the few lines we have, and offset them a tiny bit. Needs to be offsetPolylines, as offset may aleady have problems with the area. + result = union_(offset(to_polylines(first), scaled(0.002), jtMiter, 1.2), offset(to_polylines(second), scaled(0.002), jtMiter, 1.2)); + } + } + + return result; +} + +/*! + * \brief Offsets (increases the area of) a polygons object in multiple steps to ensure that it does not lag through over a given obstacle. + * \param me[in] Polygons object that has to be offset. + * \param distance[in] The distance by which me should be offset. Expects values >=0. + * \param collision[in] The area representing obstacles. + * \param last_step_offset_without_check[in] The most it is allowed to offset in one step. + * \param min_amount_offset[in] How many steps have to be done at least. As this uses round offset this increases the amount of vertices, which may be required if Polygons get very small. Required as arcTolerance is not exposed in offset, which should result with a similar result. + * \return The resulting Polygons object. + */ +[[nodiscard]] static Polygons safe_offset_inc(const Polygons& me, coord_t distance, const Polygons& collision, coord_t safe_step_size, coord_t last_step_offset_without_check, size_t min_amount_offset) +{ + bool do_final_difference = last_step_offset_without_check == 0; + Polygons ret = safe_union(me); // ensure sane input + + // Trim the collision polygons with the region of interest for diff() efficiency. + Polygons collision_trimmed_buffer; + auto collision_trimmed = [&collision_trimmed_buffer, &collision, &ret, distance]() -> const Polygons& { + if (collision_trimmed_buffer.empty() && ! collision.empty()) + collision_trimmed_buffer = ClipperUtils::clip_clipper_polygons_with_subject_bbox(collision, get_extents(ret).inflated(std::max(0, distance) + SCALED_EPSILON)); + return collision_trimmed_buffer; + }; + + if (distance == 0) + return do_final_difference ? diff(ret, collision_trimmed()) : union_(ret); + if (safe_step_size < 0 || last_step_offset_without_check < 0) { + BOOST_LOG_TRIVIAL(error) << "Offset increase got invalid parameter!"; + tree_supports_show_error("Negative offset distance... How did you manage this ?"sv, true); + return do_final_difference ? diff(ret, collision_trimmed()) : union_(ret); + } + + coord_t step_size = safe_step_size; + int steps = distance > last_step_offset_without_check ? (distance - last_step_offset_without_check) / step_size : 0; + if (distance - steps * step_size > last_step_offset_without_check) { + if ((steps + 1) * step_size <= distance) + // This will be the case when last_step_offset_without_check >= safe_step_size + ++ steps; + else + do_final_difference = true; + } + if (steps + (distance < last_step_offset_without_check || distance % step_size != 0) < min_amount_offset && min_amount_offset > 1) { + // yes one can add a bool as the standard specifies that a result from compare operators has to be 0 or 1 + // reduce the stepsize to ensure it is offset the required amount of times + step_size = distance / min_amount_offset; + if (step_size >= safe_step_size) { + // effectivly reduce last_step_offset_without_check + step_size = safe_step_size; + steps = min_amount_offset; + } else + steps = distance / step_size; + } + // offset in steps + for (int i = 0; i < steps; ++ i) { + ret = diff(offset(ret, step_size, ClipperLib::jtRound, scaled(0.01)), collision_trimmed()); + // ensure that if many offsets are done the performance does not suffer extremely by the new vertices of jtRound. + if (i % 10 == 7) + ret = polygons_simplify(ret, scaled(0.015)); + } + // offset the remainder + float last_offset = distance - steps * step_size; + if (last_offset > SCALED_EPSILON) + ret = offset(ret, distance - steps * step_size, ClipperLib::jtRound, scaled(0.01)); + ret = polygons_simplify(ret, scaled(0.015)); + + if (do_final_difference) + ret = diff(ret, collision_trimmed()); + return union_(ret); +} + +inline SupportGeneratorLayer& layer_allocate( + std::deque &layer_storage, + tbb::spin_mutex& layer_storage_mutex, + SupporLayerType layer_type, + const SlicingParameters &slicing_params, + size_t layer_idx) +{ + tbb::spin_mutex::scoped_lock lock(layer_storage_mutex); + layer_storage.push_back(SupportGeneratorLayer()); + return layer_initialize(layer_storage.back(), layer_type, slicing_params, layer_idx); +} + +/*! + * \brief Creates the initial influence areas (that can later be propagated down) by placing them below the overhang. + * + * Generates Points where the Model should be supported and creates the areas where these points have to be placed. + * + * \param mesh[in] The mesh that is currently processed. + * \param move_bounds[out] Storage for the influence areas. + * \param storage[in] Background storage, required for adding roofs. + */ +static void generate_initial_areas( + const PrintObject &print_object, + const TreeModelVolumes &volumes, + const TreeSupportSettings &config, + const std::vector &overhangs, + std::vector &move_bounds, + SupportGeneratorLayersPtr &top_contacts, + SupportGeneratorLayerStorage &layer_storage, + std::function throw_on_cancel) +{ + using AvoidanceType = TreeModelVolumes::AvoidanceType; + static constexpr const auto base_radius = scaled(0.01); + const Polygon base_circle = make_circle(base_radius, SUPPORT_TREE_CIRCLE_RESOLUTION); + TreeSupportMeshGroupSettings mesh_group_settings(print_object); + TreeSupportSettings mesh_config{ mesh_group_settings }; + SupportParameters support_params(print_object); + support_params.with_sheath = true; + support_params.support_density = 0; + + const size_t z_distance_delta = mesh_config.z_distance_top_layers + 1; // To ensure z_distance_top_layers are left empty between the overhang (zeroth empty layer), the support has to be added z_distance_top_layers+1 layers below + + const bool min_xy_dist = mesh_config.xy_distance > mesh_config.xy_min_distance; + +#if 0 + if (mesh.overhang_areas.size() <= z_distance_delta) + return; +#endif + + const coord_t connect_length = (mesh_config.support_line_width * 100. / mesh_group_settings.support_tree_top_rate) + std::max(2. * mesh_config.min_radius - 1.0 * mesh_config.support_line_width, 0.0); + // As r*r=x*x+y*y (circle equation): If a circle with center at (0,0) the top most point is at (0,r) as in y=r. + // This calculates how far one has to move on the x-axis so that y=r-support_line_width/2. + // In other words how far does one need to move on the x-axis to be support_line_width/2 away from the circle line. + // As a circle is round this length is identical for every axis as long as the 90 degrees angle between both remains. + const coord_t circle_length_to_half_linewidth_change = mesh_config.min_radius < mesh_config.support_line_width ? + mesh_config.min_radius / 2 : + sqrt(sqr(mesh_config.min_radius) - sqr(mesh_config.min_radius - mesh_config.support_line_width / 2)); + // Extra support offset to compensate for larger tip radiis. Also outset a bit more when z overwrites xy, because supporting something with a part of a support line is better than not supporting it at all. + //FIXME Vojtech: This is not sufficient for support enforcers to work. + //FIXME There is no account for the support overhang angle. + //FIXME There is no account for the width of the collision regions. + const coord_t extra_outset = std::max(coord_t(0), mesh_config.min_radius - mesh_config.support_line_width / 2) + (min_xy_dist ? mesh_config.support_line_width / 2 : 0) + //FIXME this is a heuristic value for support enforcers to work. +// + 10 * mesh_config.support_line_width; + ; + const size_t support_roof_layers = mesh_group_settings.support_roof_enable ? (mesh_group_settings.support_roof_height + mesh_config.layer_height / 2) / mesh_config.layer_height : 0; + const bool roof_enabled = support_roof_layers != 0; + const bool force_tip_to_roof = sqr(mesh_config.min_radius) * M_PI > mesh_group_settings.minimum_roof_area && roof_enabled; + //FIXME mesh_group_settings.support_angle does not apply to enforcers and also it does not apply to automatic support angle (by half the external perimeter width). + //used by max_overhang_insert_lag, only if not min_xy_dist. + const coord_t max_overhang_speed = mesh_group_settings.support_angle < 0.5 * M_PI ? coord_t(tan(mesh_group_settings.support_angle) * mesh_config.layer_height) : std::numeric_limits::max(); + // cap for how much layer below the overhang a new support point may be added, as other than with regular support every new inserted point + // may cause extra material and time cost. Could also be an user setting or differently calculated. Idea is that if an overhang + // does not turn valid in double the amount of layers a slope of support angle would take to travel xy_distance, nothing reasonable will come from it. + // The 2*z_distance_delta is only a catch for when the support angle is very high. + // Used only if not min_xy_dist. + const coord_t max_overhang_insert_lag = mesh_config.z_distance_top_layers > 0 ? + std::max(round_up_divide(mesh_config.xy_distance, max_overhang_speed / 2), 2 * mesh_config.z_distance_top_layers) : + 0; + + //FIXME + size_t num_support_layers = print_object.layer_count(); + std::vector> already_inserted(num_support_layers - z_distance_delta); + + std::mutex mutex_layer_storage, mutex_movebounds; + tbb::parallel_for(tbb::blocked_range(1, num_support_layers - z_distance_delta), + [&print_object, &volumes, &config, &overhangs, &mesh_config, &mesh_group_settings, &support_params, + z_distance_delta, min_xy_dist, force_tip_to_roof, roof_enabled, support_roof_layers, extra_outset, circle_length_to_half_linewidth_change, connect_length, max_overhang_insert_lag, + &base_circle, &mutex_layer_storage, &mutex_movebounds, &top_contacts, &layer_storage, &already_inserted, + &move_bounds, &throw_on_cancel](const tbb::blocked_range &range) { + for (size_t layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx) { + if (overhangs[layer_idx + z_distance_delta].empty()) + continue; + // take the least restrictive avoidance possible + Polygons relevant_forbidden; + { + const Polygons &relevant_forbidden_raw = mesh_config.support_rests_on_model ? + volumes.getCollision(mesh_config.getRadius(0), layer_idx, min_xy_dist) : + volumes.getAvoidance(mesh_config.getRadius(0), layer_idx, AvoidanceType::Fast, false, min_xy_dist); + // prevent rounding errors down the line, points placed directly on the line of the forbidden area may not be added otherwise. + relevant_forbidden = offset(union_ex(relevant_forbidden_raw), scaled(0.005), jtMiter, 1.2); + } + + auto generateLines = [&](const Polygons& area, bool roof, LayerIndex layer_idx) -> Polylines { + const coord_t support_infill_distance = roof ? mesh_group_settings.support_roof_line_distance : mesh_group_settings.support_tree_branch_distance; + return generate_support_infill_lines(area, support_params, roof, layer_idx, support_infill_distance); + }; + + // roof_tip_layers = force_tip_to_roof ? support_roof_layers - dtt_roof : 0 + // insert_layer_idx = layer_idx - dtt_roof + // supports_roof = dtt_roof > 0 + // dont_move_until = roof_enabled ? support_roof_layers - dtt_roof : 0 + auto addLinesAsInfluenceAreas = [&](LineInformations lines, size_t roof_tip_layers, LayerIndex insert_layer_idx, bool supports_roof, size_t dont_move_until) + { + auto addPointAsInfluenceArea = [&](std::pair p, size_t dtt, LayerIndex insert_layer, size_t dont_move_until, bool roof, bool skip_ovalisation) + { + bool to_bp = p.second == LineStatus::TO_BP || p.second == LineStatus::TO_BP_SAFE; + bool gracious = to_bp || p.second == LineStatus::TO_MODEL_GRACIOUS || p.second == LineStatus::TO_MODEL_GRACIOUS_SAFE; + bool safe_radius = p.second == LineStatus::TO_BP_SAFE || p.second == LineStatus::TO_MODEL_GRACIOUS_SAFE; + if (!mesh_config.support_rests_on_model && !to_bp) { + BOOST_LOG_TRIVIAL(warning) << "Tried to add an invalid support point"; + tree_supports_show_error("Unable to add tip. Some overhang may not be supported correctly."sv, true); + return; + } + Polygons circle{ base_circle }; + circle.front().translate(p.first); + { + std::lock_guard critical_section_movebounds(mutex_movebounds); + Point hash_pos = p.first / ((mesh_config.min_radius + 1) / 10); + if (! already_inserted[insert_layer].count(hash_pos)) { + // normalize the point a bit to also catch points which are so close that inserting it would achieve nothing + already_inserted[insert_layer].emplace(hash_pos); + SupportElementState state; + state.target_height = insert_layer; + state.target_position = p.first; + state.next_position = p.first; + state.layer_idx = insert_layer; + state.effective_radius_height = dtt; + state.to_buildplate = to_bp; + state.distance_to_top = dtt; + state.result_on_layer = p.first; + assert(state.result_on_layer_is_set()); + state.increased_to_model_radius = 0; + state.to_model_gracious = gracious; + state.elephant_foot_increases = 0; + state.use_min_xy_dist = min_xy_dist; + state.supports_roof = roof; + state.dont_move_until = dont_move_until; + state.can_use_safe_radius = safe_radius; + state.missing_roof_layers = force_tip_to_roof ? dont_move_until : 0; + state.skip_ovalisation = skip_ovalisation; + move_bounds[insert_layer].emplace_back(state, std::move(circle)); + } + } + }; + + validate_range(lines); + // Add tip area as roof (happens when minimum roof area > minimum tip area) if possible + size_t dtt_roof_tip; + for (dtt_roof_tip = 0; dtt_roof_tip < roof_tip_layers && insert_layer_idx - dtt_roof_tip >= 1; dtt_roof_tip++) + { + auto evaluateRoofWillGenerate = [&](std::pair p) { + //FIXME Vojtech: The circle is just shifted, it has a known size, the infill should fit all the time! +#if 0 + Polygon roof_circle; + for (Point corner : base_circle) + roof_circle.points.emplace_back(p.first + corner * mesh_config.min_radius); + return !generate_support_infill_lines({ roof_circle }, mesh_config, true, insert_layer_idx - dtt_roof_tip, mesh_config.support_roof_line_distance).empty(); +#else + return true; +#endif + }; + + { + std::pair split = + // keep all lines that are still valid on the next layer + split_lines(lines, [&volumes, &config, insert_layer_idx, dtt_roof_tip](const std::pair &p) + { return evaluate_point_for_next_layer_function(volumes, config, insert_layer_idx - dtt_roof_tip, p); }); + LineInformations points = std::move(split.second); + // Not all roofs are guaranteed to actually generate lines, so filter these out and add them as points. + split = split_lines(split.first, evaluateRoofWillGenerate); + lines = std::move(split.first); + append(points, split.second); + // add all points that would not be valid + for (const LineInformation &line : points) + for (const std::pair &point_data : line) + addPointAsInfluenceArea(point_data, 0, insert_layer_idx - dtt_roof_tip, roof_tip_layers - dtt_roof_tip, dtt_roof_tip != 0, false); + } + + // add all tips as roof to the roof storage + Polygons added_roofs; + for (const LineInformation &line : lines) + //FIXME sweep the tip radius along the line? + for (const std::pair &p : line) { + Polygon roof_circle{ base_circle }; + roof_circle.scale(mesh_config.min_radius / base_radius); + roof_circle.translate(p.first); + added_roofs.emplace_back(std::move(roof_circle)); + } + if (! added_roofs.empty()) { + added_roofs = union_(added_roofs); + { + std::lock_guard lock(mutex_layer_storage); + SupportGeneratorLayer *&l = top_contacts[insert_layer_idx - dtt_roof_tip]; + if (l == nullptr) + l = &layer_allocate(layer_storage, SupporLayerType::sltTopContact, print_object.slicing_parameters(), insert_layer_idx - dtt_roof_tip); + append(l->polygons, std::move(added_roofs)); + } + } + } + + for (LineInformation line : lines) { + bool disable_ovalistation = mesh_config.min_radius < 3 * mesh_config.support_line_width && roof_tip_layers == 0 && dtt_roof_tip == 0 && line.size() > 5; // If a line consists of enough tips, the assumption is that it is not a single tip, but part of a simulated support pattern. Ovalisation should be disabled for these to improve the quality of the lines when tip_diameter=line_width + for (auto point_data : line) + addPointAsInfluenceArea(point_data, 0, insert_layer_idx - dtt_roof_tip, dont_move_until > dtt_roof_tip ? dont_move_until - dtt_roof_tip : 0, dtt_roof_tip != 0 || supports_roof, disable_ovalistation); + } + }; + + // every overhang has saved if a roof should be generated for it. This can NOT be done in the for loop as an area may NOT have a roof + // even if it is larger than the minimum_roof_area when it is only larger because of the support horizontal expansion and + // it would not have a roof if the overhang is offset by support roof horizontal expansion instead. (At least this is the current behavior of the regular support) + Polygons overhang_regular; + { + const Polygons &overhang_raw = overhangs[layer_idx + z_distance_delta]; + // When support_offset = 0 safe_offset_inc will only be the difference between overhang_raw and relevant_forbidden, that has to be calculated anyway. + overhang_regular = safe_offset_inc(overhang_raw, mesh_group_settings.support_offset, relevant_forbidden, mesh_config.min_radius * 1.75 + mesh_config.xy_min_distance, 0, 1); + //check_self_intersections(overhang_regular, "overhang_regular1"); + + // offset ensures that areas that could be supported by a part of a support line, are not considered unsupported overhang + Polygons remaining_overhang = intersection( + diff(mesh_group_settings.support_offset == 0 ? + overhang_raw : + offset(union_ex(overhang_raw), mesh_group_settings.support_offset, jtMiter, 1.2), + offset(union_ex(overhang_regular), mesh_config.support_line_width * 0.5, jtMiter, 1.2)), + relevant_forbidden); + + // Offset the area to compensate for large tip radiis. Offset happens in multiple steps to ensure the tip is as close to the original overhang as possible. + //+mesh_config.support_line_width / 80 to avoid calculating very small (useless) offsets because of rounding errors. + //FIXME likely a better approach would be to find correspondences between the full overhang and the trimmed overhang + // and if there is no correspondence, project the missing points to the clipping curve. + for (coord_t extra_total_offset_acc = 0; ! remaining_overhang.empty() && extra_total_offset_acc + mesh_config.support_line_width / 8 < extra_outset; ) { + const coord_t offset_current_step = std::min( + extra_total_offset_acc + 2 * mesh_config.support_line_width > mesh_config.min_radius ? + mesh_config.support_line_width / 8 : + circle_length_to_half_linewidth_change, + extra_outset - extra_total_offset_acc); + extra_total_offset_acc += offset_current_step; + const Polygons &raw_collision = volumes.getCollision(0, layer_idx, true); + const coord_t offset_step = mesh_config.xy_min_distance + mesh_config.support_line_width; + // Reducing the remaining overhang by the areas already supported. + //FIXME 1.5 * extra_total_offset_acc seems to be too much, it may remove some remaining overhang without being supported at all. + remaining_overhang = diff(remaining_overhang, safe_offset_inc(overhang_regular, 1.5 * extra_total_offset_acc, raw_collision, offset_step, 0, 1)); + // Extending the overhangs by the inflated remaining overhangs. + overhang_regular = union_(overhang_regular, diff(safe_offset_inc(remaining_overhang, extra_total_offset_acc, raw_collision, offset_step, 0, 1), relevant_forbidden)); + //check_self_intersections(overhang_regular, "overhang_regular2"); + } + // If the xy distance overrides the z distance, some support needs to be inserted further down. + //=> Analyze which support points do not fit on this layer and check if they will fit a few layers down (while adding them an infinite amount of layers down would technically be closer the the setting description, it would not produce reasonable results. ) + if (! min_xy_dist) { + LineInformations overhang_lines; + { + //Vojtech: Generate support heads at support_tree_branch_distance spacing by producing a zig-zag infill at support_tree_branch_distance spacing, + // which is then resmapled + // support_line_width to form a line here as otherwise most will be unsupported. Technically this violates branch distance, + // mbut not only is this the only reasonable choice, but it ensures consistent behavior as some infill patterns generate + // each line segment as its own polyline part causing a similar line forming behavior. Also it is assumed that + // the area that is valid a layer below is to small for support roof. + Polylines polylines = ensure_maximum_distance_polyline(generateLines(remaining_overhang, false, layer_idx), mesh_config.min_radius, 1); + if (polylines.size() <= 3) + // add the outer wall to ensure it is correct supported instead + polylines = ensure_maximum_distance_polyline(to_polylines(remaining_overhang), connect_length, 3); + for (const auto &line : polylines) { + LineInformation res_line; + for (Point p : line) + res_line.emplace_back(p, LineStatus::INVALID); + overhang_lines.emplace_back(res_line); + } + validate_range(overhang_lines); + } + for (size_t lag_ctr = 1; lag_ctr <= max_overhang_insert_lag && !overhang_lines.empty() && layer_idx - coord_t(lag_ctr) >= 1; lag_ctr++) { + // get least restricted avoidance for layer_idx-lag_ctr + const Polygons &relevant_forbidden_below = mesh_config.support_rests_on_model ? + volumes.getCollision(mesh_config.getRadius(0), layer_idx - lag_ctr, min_xy_dist) : + volumes.getAvoidance(mesh_config.getRadius(0), layer_idx - lag_ctr, AvoidanceType::Fast, false, min_xy_dist); + // it is not required to offset the forbidden area here as the points wont change: If points here are not inside the forbidden area neither will they be later when placing these points, as these are the same points. + auto evaluatePoint = [&](std::pair p) { return contains(relevant_forbidden_below, p.first); }; + + std::pair split = split_lines(overhang_lines, evaluatePoint); // keep all lines that are invalid + overhang_lines = split.first; + // Set all now valid lines to their correct LineStatus. Easiest way is to just discard Avoidance information for each point and evaluate them again. + LineInformations fresh_valid_points = convert_lines_to_internal(volumes, config, convert_internal_to_lines(split.second), layer_idx - lag_ctr); + validate_range(fresh_valid_points); + + addLinesAsInfluenceAreas(fresh_valid_points, (force_tip_to_roof && lag_ctr <= support_roof_layers) ? support_roof_layers : 0, layer_idx - lag_ctr, false, roof_enabled ? support_roof_layers : 0); + } + } + } + + throw_on_cancel(); + + Polygons overhang_roofs; + std::vector> overhang_processing; + if (roof_enabled) { + static constexpr const coord_t support_roof_offset = 0; + overhang_roofs = safe_offset_inc(overhangs[layer_idx + z_distance_delta], support_roof_offset, relevant_forbidden, mesh_config.min_radius * 2 + mesh_config.xy_min_distance, 0, 1); + if (mesh_group_settings.minimum_support_area > 0) + remove_small(overhang_roofs, mesh_group_settings.minimum_roof_area); + overhang_regular = diff(overhang_regular, overhang_roofs, ApplySafetyOffset::Yes); + //check_self_intersections(overhang_regular, "overhang_regular3"); + for (ExPolygon &roof_part : union_ex(overhang_roofs)) + overhang_processing.emplace_back(std::move(roof_part), true); + } + if (mesh_group_settings.minimum_support_area > 0) + remove_small(overhang_regular, mesh_group_settings.minimum_support_area); + + for (ExPolygon &support_part : union_ex(overhang_regular)) + overhang_processing.emplace_back(std::move(support_part), false); + + for (const std::pair &overhang_pair : overhang_processing) { + const bool roof_allowed_for_this_part = overhang_pair.second; + Polygons overhang_outset = to_polygons(overhang_pair.first); + const size_t min_support_points = std::max(coord_t(1), std::min(coord_t(3), coord_t(total_length(overhang_outset) / connect_length))); + LineInformations overhang_lines; + Polygons last_overhang = overhang_outset; + size_t dtt_roof = 0; + // Sometimes roofs could be empty as the pattern does not generate lines if the area is narrow enough (i am looking at you, concentric infill). + // To catch these cases the added roofs are saved to be evaluated later. + std::vector added_roofs(support_roof_layers); + + // Assumption is that roof will support roof further up to avoid a lot of unnecessary branches. Each layer down it is checked whether the roof area + // is still large enough to be a roof and aborted as soon as it is not. This part was already reworked a few times, and there could be an argument + // made to change it again if there are actual issues encountered regarding supporting roofs. + // Main problem is that some patterns change each layer, so just calculating points and checking if they are still valid an layer below is not useful, + // as the pattern may be different one layer below. Same with calculating which points are now no longer being generated as result from + // a decreasing roof, as there is no guarantee that a line will be above these points. Implementing a separate roof support behavior + // for each pattern harms maintainability as it very well could be >100 LOC + if (roof_allowed_for_this_part) { + for (dtt_roof = 0; dtt_roof < support_roof_layers && layer_idx - dtt_roof >= 1; dtt_roof++) { + // here the roof is handled. If roof can not be added the branches will try to not move instead + Polygons forbidden_next; + { + const Polygons &forbidden_next_raw = mesh_config.support_rests_on_model ? + volumes.getCollision(mesh_config.getRadius(0), layer_idx - (dtt_roof + 1), min_xy_dist) : + volumes.getAvoidance(mesh_config.getRadius(0), layer_idx - (dtt_roof + 1), AvoidanceType::Fast, false, min_xy_dist); + // prevent rounding errors down the line + //FIXME maybe use SafetyOffset::Yes at the following diff() instead? + forbidden_next = offset(union_ex(forbidden_next_raw), scaled(0.005), jtMiter, 1.2); + } + Polygons overhang_outset_next = diff(overhang_outset, forbidden_next); + if (area(overhang_outset_next) < mesh_group_settings.minimum_roof_area) { + // next layer down the roof area would be to small so we have to insert our roof support here. Also convert squaremicrons to squaremilimeter + if (dtt_roof != 0) { + size_t dtt_before = dtt_roof > 0 ? dtt_roof - 1 : 0; + // Produce support head points supporting an interface layer: First produce the interface lines, then sample them. + overhang_lines = convert_lines_to_internal(volumes, config, + ensure_maximum_distance_polyline(generateLines(last_overhang, true, layer_idx - dtt_before), connect_length, 1), layer_idx - dtt_before); + overhang_lines = split_lines(overhang_lines, + [&volumes, &config, layer_idx, dtt_before](const std::pair &p) + { return evaluate_point_for_next_layer_function(volumes, config, layer_idx - dtt_before, p); }) + .first; + } + break; + } + added_roofs[dtt_roof] = overhang_outset; + last_overhang = overhang_outset; + overhang_outset = overhang_outset_next; + } + } + + size_t layer_generation_dtt = std::max(dtt_roof, size_t(1)) - 1; // 1 inside max and -1 outside to avoid underflow. layer_generation_dtt=dtt_roof-1 if dtt_roof!=0; + // if the roof should be valid, check that the area does generate lines. This is NOT guaranteed. + if (overhang_lines.empty() && dtt_roof != 0 && generateLines(overhang_outset, true, layer_idx - layer_generation_dtt).empty()) + for (size_t idx = 0; idx < dtt_roof; idx++) { + // check for every roof area that it has resulting lines. Remember idx 1 means the 2. layer of roof => higher idx == lower layer + if (generateLines(added_roofs[idx], true, layer_idx - idx).empty()) { + dtt_roof = idx; + layer_generation_dtt = std::max(dtt_roof, size_t(1)) - 1; + break; + } + } + + { + std::lock_guard lock(mutex_layer_storage); + for (size_t idx = 0; idx < dtt_roof; ++ idx) + if (! added_roofs[idx].empty()) { + SupportGeneratorLayer *&l = top_contacts[layer_idx - idx]; + if (l == nullptr) + l = &layer_allocate(layer_storage, SupporLayerType::sltTopContact, print_object.slicing_parameters(), layer_idx - idx); + // will be unioned in finalize_interface_and_support_areas() + append(l->polygons, std::move(added_roofs[idx])); + } + } + + if (overhang_lines.empty()) { + // support_line_width to form a line here as otherwise most will be unsupported. Technically this violates branch distance, but not only is this the only reasonable choice, + // but it ensures consistant behaviour as some infill patterns generate each line segment as its own polyline part causing a similar line forming behaviour. + // This is not doen when a roof is above as the roof will support the model and the trees only need to support the roof + Polylines polylines = ensure_maximum_distance_polyline( + generateLines(overhang_outset, dtt_roof != 0, layer_idx - layer_generation_dtt), dtt_roof == 0 ? mesh_config.min_radius / 2 : connect_length, 1); + size_t point_count = 0; + for (const Polyline &poly : polylines) + point_count += poly.size(); + if (point_count <= min_support_points) { + // add the outer wall (of the overhang) to ensure it is correct supported instead. Try placing the support points in a way that they fully support the outer wall, instead of just the with half of the the support line width. + // I assume that even small overhangs are over one line width wide, so lets try to place the support points in a way that the full support area generated from them + // will support the overhang (if this is not done it may only be half). This WILL NOT be the case when supporting an angle of about < 60� so there is a fallback, + // as some support is better than none. + Polygons reduced_overhang_outset = offset(union_ex(overhang_outset), -mesh_config.support_line_width / 2.2, jtMiter, 1.2); + polylines = ensure_maximum_distance_polyline( + to_polylines( + ! reduced_overhang_outset.empty() && + area(offset(diff_ex(overhang_outset, reduced_overhang_outset), std::max(mesh_config.support_line_width, connect_length), jtMiter, 1.2)) < sqr(scaled(0.001)) ? + reduced_overhang_outset : + overhang_outset), + connect_length, min_support_points); + } + LayerIndex last_insert_layer = layer_idx - dtt_roof; + overhang_lines = convert_lines_to_internal(volumes, config, polylines, last_insert_layer); + } + + if (int(dtt_roof) >= layer_idx && roof_allowed_for_this_part && ! overhang_outset.empty()) { + // reached buildplate + std::lock_guard lock(mutex_layer_storage); + SupportGeneratorLayer*& l = top_contacts[0]; + if (l == nullptr) + l = &layer_allocate(layer_storage, SupporLayerType::sltTopContact, print_object.slicing_parameters(), 0); + append(l->polygons, std::move(overhang_outset)); + } else // normal trees have to be generated + addLinesAsInfluenceAreas(overhang_lines, force_tip_to_roof ? support_roof_layers - dtt_roof : 0, layer_idx - dtt_roof, dtt_roof > 0, roof_enabled ? support_roof_layers - dtt_roof : 0); + throw_on_cancel(); + } + } + }); +} + +static unsigned int move_inside(const Polygons &polygons, Point &from, int distance = 0, int64_t maxDist2 = std::numeric_limits::max()) +{ + Point ret = from; + double bestDist2 = std::numeric_limits::max(); + auto bestPoly = static_cast(-1); + bool is_already_on_correct_side_of_boundary = false; // whether [from] is already on the right side of the boundary + for (unsigned int poly_idx = 0; poly_idx < polygons.size(); ++ poly_idx) { + const Polygon &poly = polygons[poly_idx]; + if (poly.size() < 2) + continue; + Point p0 = poly[poly.size() - 2]; + Point p1 = poly.back(); + // because we compare with vSize2 here (no division by zero), we also need to compare by vSize2 inside the loop + // to avoid integer rounding edge cases + bool projected_p_beyond_prev_segment = (p1 - p0).cast().dot((from - p0).cast()) >= (p1 - p0).cast().squaredNorm(); + for (const Point& p2 : poly) { + // X = A + Normal(B-A) * (((B-A) dot (P-A)) / VSize(B-A)); + // = A + (B-A) * ((B-A) dot (P-A)) / VSize2(B-A); + // X = P projected on AB + const Point& a = p1; + const Point& b = p2; + const Point& p = from; + auto ab = (b - a).cast(); + auto ap = (p - a).cast(); + int64_t ab_length2 = ab.squaredNorm(); + if (ab_length2 <= 0) { //A = B, i.e. the input polygon had two adjacent points on top of each other. + p1 = p2; //Skip only one of the points. + continue; + } + int64_t dot_prod = ab.dot(ap); + if (dot_prod <= 0) { // x is projected to before ab + if (projected_p_beyond_prev_segment) { + // case which looks like: > . + projected_p_beyond_prev_segment = false; + Point& x = p1; + + auto dist2 = (x - p).cast().squaredNorm(); + if (dist2 < bestDist2) { + bestDist2 = dist2; + bestPoly = poly_idx; + if (distance == 0) + ret = x; + else { + Vec2d abd = ab.cast(); + Vec2d p1p2 = (p1 - p0).cast(); + double lab = abd.norm(); + double lp1p2 = p1p2.norm(); + // inward direction irrespective of sign of [distance] + auto inward_dir = perp(abd * (scaled(10.0) / lab) + p1p2 * (scaled(10.0) / lp1p2)); + // MM2INT(10.0) to retain precision for the eventual normalization + ret = x + (inward_dir * (distance / inward_dir.norm())).cast(); + is_already_on_correct_side_of_boundary = inward_dir.dot((p - x).cast()) * distance >= 0; + } + } + } else { + projected_p_beyond_prev_segment = false; + p0 = p1; + p1 = p2; + continue; + } + } else if (dot_prod >= ab_length2) { + // x is projected to beyond ab + projected_p_beyond_prev_segment = true; + p0 = p1; + p1 = p2; + continue; + } else { + // x is projected to a point properly on the line segment (not onto a vertex). The case which looks like | . + projected_p_beyond_prev_segment = false; + Point x = a + (ab.cast() * (double(dot_prod) / double(ab_length2))).cast(); + auto dist2 = (p - x).cast().squaredNorm(); + if (dist2 < bestDist2) { + bestDist2 = dist2; + bestPoly = poly_idx; + if (distance == 0) + ret = x; + else { + Vec2d abd = ab.cast(); + Vec2d inward_dir = perp(abd * (distance / abd.norm())); // inward or outward depending on the sign of [distance] + ret = x + inward_dir.cast(); + is_already_on_correct_side_of_boundary = inward_dir.dot((p - x).cast()) >= 0; + } + } + } + p0 = p1; + p1 = p2; + } + } + // when the best point is already inside and we're moving inside, or when the best point is already outside and we're moving outside + if (is_already_on_correct_side_of_boundary) { + if (bestDist2 < distance * distance) + from = ret; + else { + // from = from; // original point stays unaltered. It is already inside by enough distance + } + return bestPoly; + } else if (bestDist2 < maxDist2) { + from = ret; + return bestPoly; + } + return -1; +} + +static Point move_inside_if_outside(const Polygons &polygons, Point from, int distance = 0, int64_t maxDist2 = std::numeric_limits::max()) +{ + if (! contains(polygons, from)) + move_inside(polygons, from); + return from; +} + +/*! + * \brief Checks if an influence area contains a valid subsection and returns the corresponding metadata and the new Influence area. + * + * Calculates an influence areas of the layer below, based on the influence area of one element on the current layer. + * Increases every influence area by maximum_move_distance_slow. If this is not enough, as in we would change our gracious or to_buildplate status the influence areas are instead increased by maximum_move_distance_slow. + * Also ensures that increasing the radius of a branch, does not cause it to change its status (like to_buildplate ). If this were the case, the radius is not increased instead. + * + * Warning: The used format inside this is different as the SupportElement does not have a valid area member. Instead this area is saved as value of the dictionary. This was done to avoid not needed heap allocations. + * + * \param settings[in] Which settings have to be used to check validity. + * \param layer_idx[in] Number of the current layer. + * \param parent[in] The metadata of the parents influence area. + * \param relevant_offset[in] The maximal possible influence area. No guarantee regarding validity with current layer collision required, as it is ensured in-function! + * \param to_bp_data[out] The part of the Influence area that can reach the buildplate. + * \param to_model_data[out] The part of the Influence area that do not have to reach the buildplate. This has overlap with new_layer_data. + * \param increased[out] Area than can reach all further up support points. No assurance is made that the buildplate or the model can be reached in accordance to the user-supplied settings. + * \param overspeed[in] How much should the already offset area be offset again. Usually this is 0. + * \param mergelayer[in] Will the merge method be called on this layer. This information is required as some calculation can be avoided if they are not required for merging. + * \return A valid support element for the next layer regarding the calculated influence areas. Empty if no influence are can be created using the supplied influence area and settings. + */ +[[nodiscard]] static std::optional increase_single_area( + const TreeModelVolumes &volumes, + const TreeSupportSettings &config, + const AreaIncreaseSettings &settings, + const LayerIndex layer_idx, + const SupportElement &parent, + const Polygons &relevant_offset, + Polygons &to_bp_data, + Polygons &to_model_data, + Polygons &increased, + const coord_t overspeed, + const bool mergelayer) +{ + SupportElementState current_elem{ SupportElementState::propagate_down(parent.state) }; + Polygons check_layer_data; + if (settings.increase_radius) + current_elem.effective_radius_height += 1; + coord_t radius = config.getCollisionRadius(current_elem); + + if (settings.move) { + increased = relevant_offset; + if (overspeed > 0) { + const coord_t safe_movement_distance = + (current_elem.use_min_xy_dist ? config.xy_min_distance : config.xy_distance) + + (std::min(config.z_distance_top_layers, config.z_distance_bottom_layers) > 0 ? config.min_feature_size : 0); + // The difference to ensure that the result not only conforms to wall_restriction, but collision/avoidance is done later. + // The higher last_safe_step_movement_distance comes exactly from the fact that the collision will be subtracted later. + increased = safe_offset_inc(increased, overspeed, volumes.getWallRestriction(config.getCollisionRadius(parent.state), layer_idx, parent.state.use_min_xy_dist), + safe_movement_distance, safe_movement_distance + radius, 1); + } + if (settings.no_error && settings.move) + // as ClipperLib::jtRound has to be used for offsets this simplify is VERY important for performance. + polygons_simplify(increased, scaled(0.025)); + } else + // if no movement is done the areas keep parent area as no move == offset(0) + increased = parent.influence_area; + + if (mergelayer || current_elem.to_buildplate) { + to_bp_data = safe_union(diff_clipped(increased, volumes.getAvoidance(radius, layer_idx - 1, settings.type, false, settings.use_min_distance))); + if (! current_elem.to_buildplate && area(to_bp_data) > tiny_area_threshold) { + // mostly happening in the tip, but with merges one should check every time, just to be sure. + current_elem.to_buildplate = true; // sometimes nodes that can reach the buildplate are marked as cant reach, tainting subtrees. This corrects it. + BOOST_LOG_TRIVIAL(debug) << "Corrected taint leading to a wrong to model value on layer " << layer_idx - 1 << " targeting " << + current_elem.target_height << " with radius " << radius; + } + } + if (config.support_rests_on_model) { + if (mergelayer || current_elem.to_model_gracious) + to_model_data = safe_union(diff_clipped(increased, volumes.getAvoidance(radius, layer_idx - 1, settings.type, true, settings.use_min_distance))); + + if (!current_elem.to_model_gracious) { + if (mergelayer && area(to_model_data) >= tiny_area_threshold) { + current_elem.to_model_gracious = true; + BOOST_LOG_TRIVIAL(debug) << "Corrected taint leading to a wrong non gracious value on layer " << layer_idx - 1 << " targeting " << + current_elem.target_height << " with radius " << radius; + } else + to_model_data = safe_union(diff_clipped(increased, volumes.getCollision(radius, layer_idx - 1, settings.use_min_distance))); + } + } + + check_layer_data = current_elem.to_buildplate ? to_bp_data : to_model_data; + + if (settings.increase_radius && area(check_layer_data) > tiny_area_threshold) { + auto validWithRadius = [&](coord_t next_radius) { + if (volumes.ceilRadius(next_radius, settings.use_min_distance) <= volumes.ceilRadius(radius, settings.use_min_distance)) + return true; + + Polygons to_bp_data_2; + if (current_elem.to_buildplate) + // regular union as output will not be used later => this area should always be a subset of the safe_union one (i think) + to_bp_data_2 = diff_clipped(increased, volumes.getAvoidance(next_radius, layer_idx - 1, settings.type, false, settings.use_min_distance)); + Polygons to_model_data_2; + if (config.support_rests_on_model && !current_elem.to_buildplate) + to_model_data_2 = diff_clipped(increased, + current_elem.to_model_gracious ? + volumes.getAvoidance(next_radius, layer_idx - 1, settings.type, true, settings.use_min_distance) : + volumes.getCollision(next_radius, layer_idx - 1, settings.use_min_distance)); + Polygons check_layer_data_2 = current_elem.to_buildplate ? to_bp_data_2 : to_model_data_2; + return area(check_layer_data_2) > tiny_area_threshold; + }; + coord_t ceil_radius_before = volumes.ceilRadius(radius, settings.use_min_distance); + + if (config.getCollisionRadius(current_elem) < config.increase_radius_until_radius && config.getCollisionRadius(current_elem) < config.getRadius(current_elem)) { + coord_t target_radius = std::min(config.getRadius(current_elem), config.increase_radius_until_radius); + coord_t current_ceil_radius = volumes.getRadiusNextCeil(radius, settings.use_min_distance); + + while (current_ceil_radius < target_radius && validWithRadius(volumes.getRadiusNextCeil(current_ceil_radius + 1, settings.use_min_distance))) + current_ceil_radius = volumes.getRadiusNextCeil(current_ceil_radius + 1, settings.use_min_distance); + size_t resulting_eff_dtt = current_elem.effective_radius_height; + while (resulting_eff_dtt + 1 < current_elem.distance_to_top && + config.getRadius(resulting_eff_dtt + 1, current_elem.elephant_foot_increases) <= current_ceil_radius && + config.getRadius(resulting_eff_dtt + 1, current_elem.elephant_foot_increases) <= config.getRadius(current_elem)) + ++ resulting_eff_dtt; + current_elem.effective_radius_height = resulting_eff_dtt; + } + radius = config.getCollisionRadius(current_elem); + + const coord_t foot_radius_increase = config.branch_radius * (std::max(config.diameter_scale_bp_radius - config.diameter_angle_scale_factor, 0.0)); + // Is nearly all of the time 1, but sometimes an increase of 1 could cause the radius to become bigger than recommendedMinRadius, + // which could cause the radius to become bigger than precalculated. + double planned_foot_increase = std::min(1.0, double(config.recommendedMinRadius(layer_idx - 1) - config.getRadius(current_elem)) / foot_radius_increase); +//FIXME + bool increase_bp_foot = planned_foot_increase > 0 && current_elem.to_buildplate; +// bool increase_bp_foot = false; + + if (increase_bp_foot && config.getRadius(current_elem) >= config.branch_radius && config.getRadius(current_elem) >= config.increase_radius_until_radius) + if (validWithRadius(config.getRadius(current_elem.effective_radius_height, current_elem.elephant_foot_increases + planned_foot_increase))) { + current_elem.elephant_foot_increases += planned_foot_increase; + radius = config.getCollisionRadius(current_elem); + } + + if (ceil_radius_before != volumes.ceilRadius(radius, settings.use_min_distance)) { + if (current_elem.to_buildplate) + to_bp_data = safe_union(diff_clipped(increased, volumes.getAvoidance(radius, layer_idx - 1, settings.type, false, settings.use_min_distance))); + if (config.support_rests_on_model && (!current_elem.to_buildplate || mergelayer)) + to_model_data = safe_union(diff_clipped(increased, + current_elem.to_model_gracious ? + volumes.getAvoidance(radius, layer_idx - 1, settings.type, true, settings.use_min_distance) : + volumes.getCollision(radius, layer_idx - 1, settings.use_min_distance) + )); + check_layer_data = current_elem.to_buildplate ? to_bp_data : to_model_data; + if (area(check_layer_data) < tiny_area_threshold) { + BOOST_LOG_TRIVIAL(error) << "Lost area by doing catch up from " << ceil_radius_before << " to radius " << + volumes.ceilRadius(config.getCollisionRadius(current_elem), settings.use_min_distance); + tree_supports_show_error("Area lost catching up radius. May not cause visible malformation."sv, true); + } + } + } + + return area(check_layer_data) > tiny_area_threshold ? std::optional(current_elem) : std::optional(); +} + +struct SupportElementInfluenceAreas { + // All influence areas: both to build plate and model. + Polygons influence_areas; + // Influence areas just to build plate. + Polygons to_bp_areas; + // Influence areas just to model. + Polygons to_model_areas; + + void clear() { + this->influence_areas.clear(); + this->to_bp_areas.clear(); + this->to_model_areas.clear(); + } +}; + +struct SupportElementMerging { + SupportElementState state; + /*! + * \brief All elements in the layer above the current one that are supported by this element + */ + SupportElement::ParentIndices parents; + + SupportElementInfluenceAreas areas; + // Bounding box of all influence areas. + Eigen::AlignedBox bbox_data; + + const Eigen::AlignedBox& bbox() const { return bbox_data;} + const Point centroid() const { return (bbox_data.min() + bbox_data.max()) / 2; } + void set_bbox(const BoundingBox& abbox) + { Point eps { coord_t(SCALED_EPSILON), coord_t(SCALED_EPSILON) }; bbox_data = { abbox.min - eps, abbox.max + eps }; } + + // Called by the AABBTree builder to get an index into the vector of source elements. + // Not needed, thus zero is returned. + static size_t idx() { return 0; } +}; + +/*! + * \brief Increases influence areas as far as required. + * + * Calculates influence areas of the layer below, based on the influence areas of the current layer. + * Increases every influence area by maximum_move_distance_slow. If this is not enough, as in it would change the gracious or to_buildplate status, the influence areas are instead increased by maximum_move_distance. + * Also ensures that increasing the radius of a branch, does not cause it to change its status (like to_buildplate ). If this were the case, the radius is not increased instead. + * + * Warning: The used format inside this is different as the SupportElement does not have a valid area member. Instead this area is saved as value of the dictionary. This was done to avoid not needed heap allocations. + * + * \param to_bp_areas[out] Influence areas that can reach the buildplate + * \param to_model_areas[out] Influence areas that do not have to reach the buildplate. This has overlap with new_layer_data, as areas that can reach the buildplate are also considered valid areas to the model. + * This redundancy is required if a to_buildplate influence area is allowed to merge with a to model influence area. + * \param influence_areas[out] Area than can reach all further up support points. No assurance is made that the buildplate or the model can be reached in accordance to the user-supplied settings. + * \param bypass_merge_areas[out] Influence areas ready to be added to the layer below that do not need merging. + * \param last_layer[in] Influence areas of the current layer. + * \param layer_idx[in] Number of the current layer. + * \param mergelayer[in] Will the merge method be called on this layer. This information is required as some calculation can be avoided if they are not required for merging. + */ +static void increase_areas_one_layer( + const TreeModelVolumes &volumes, + const TreeSupportSettings &config, + // New areas at the layer below layer_idx + std::vector &merging_areas, + // Layer above merging_areas. + const LayerIndex layer_idx, + // Layer elements above merging_areas. + SupportElements &layer_elements, + // If false, the merging_areas will not be merged for performance reasons. + const bool mergelayer, + std::function throw_on_cancel) +{ + using AvoidanceType = TreeModelVolumes::AvoidanceType; + + tbb::parallel_for(tbb::blocked_range(0, merging_areas.size()), + [&](const tbb::blocked_range &range) { + for (size_t merging_area_idx = range.begin(); merging_area_idx < range.end(); ++ merging_area_idx) { + SupportElementMerging &merging_area = merging_areas[merging_area_idx]; + assert(merging_area.parents.size() == 1); + SupportElement &parent = layer_elements[merging_area.parents.front()]; + SupportElementState elem = SupportElementState::propagate_down(parent.state); + const Polygons &wall_restriction = + // Abstract representation of the model outline. If an influence area would move through it, it could teleport through a wall. + volumes.getWallRestriction(config.getCollisionRadius(parent.state), layer_idx, parent.state.use_min_xy_dist); + +#ifdef TREESUPPORT_DEBUG_SVG + SVG::export_expolygons(debug_out_path("treesupport-increase_areas_one_layer-%d-%ld.svg", layer_idx, int(merging_area_idx)), + { { { union_ex(wall_restriction) }, { "wall_restricrictions", "gray", 0.5f } }, + { { union_ex(parent.influence_area) }, { "parent", "red", "black", "", scaled(0.1f), 0.5f } } }); +#endif // TREESUPPORT_DEBUG_SVG + + Polygons to_bp_data, to_model_data; + coord_t radius = config.getCollisionRadius(elem); + + // When the radius increases, the outer "support wall" of the branch will have been moved farther away from the center (as this is the definition of radius). + // As it is not specified that the support_tree_angle has to be one of the center of the branch, it is here seen as the smaller angle of the outer wall of the branch, to the outer wall of the same branch one layer above. + // As the branch may have become larger the distance between these 2 walls is smaller than the distance of the center points. + // These extra distance is added to the movement distance possible for this layer. + + coord_t extra_speed = 5; // The extra speed is added to both movement distances. Also move 5 microns faster than allowed to avoid rounding errors, this may cause issues at VERY VERY small layer heights. + coord_t extra_slow_speed = 0; // Only added to the slow movement distance. + const coord_t ceiled_parent_radius = volumes.ceilRadius(config.getCollisionRadius(parent.state), parent.state.use_min_xy_dist); + coord_t projected_radius_increased = config.getRadius(parent.state.effective_radius_height + 1, parent.state.elephant_foot_increases); + coord_t projected_radius_delta = projected_radius_increased - config.getCollisionRadius(parent.state); + + // When z distance is more than one layer up and down the Collision used to calculate the wall restriction will always include the wall (and not just the xy_min_distance) of the layer above and below like this (d = blocked area because of z distance): + /* + * layer z+1:dddddiiiiiioooo + * layer z+0:xxxxxdddddddddd + * layer z-1:dddddxxxxxxxxxx + * For more detailed visualisation see calculateWallRestrictions + */ + const coord_t safe_movement_distance = + (elem.use_min_xy_dist ? config.xy_min_distance : config.xy_distance) + + (std::min(config.z_distance_top_layers, config.z_distance_bottom_layers) > 0 ? config.min_feature_size : 0); + if (ceiled_parent_radius == volumes.ceilRadius(projected_radius_increased, parent.state.use_min_xy_dist) || + projected_radius_increased < config.increase_radius_until_radius) + // If it is guaranteed possible to increase the radius, the maximum movement speed can be increased, as it is assumed that the maximum movement speed is the one of the slower moving wall + extra_speed += projected_radius_delta; + else + // if a guaranteed radius increase is not possible, only increase the slow speed + // Ensure that the slow movement distance can not become larger than the fast one. + extra_slow_speed += std::min(projected_radius_delta, (config.maximum_move_distance + extra_speed) - (config.maximum_move_distance_slow + extra_slow_speed)); + + if (config.layer_start_bp_radius > layer_idx && + config.recommendedMinRadius(layer_idx - 1) < config.getRadius(elem.effective_radius_height + 1, elem.elephant_foot_increases)) { + // can guarantee elephant foot radius increase + if (ceiled_parent_radius == volumes.ceilRadius(config.getRadius(parent.state.effective_radius_height + 1, parent.state.elephant_foot_increases + 1), parent.state.use_min_xy_dist)) + extra_speed += config.branch_radius * config.diameter_scale_bp_radius; + else + extra_slow_speed += std::min(coord_t(config.branch_radius * config.diameter_scale_bp_radius), + config.maximum_move_distance - (config.maximum_move_distance_slow + extra_slow_speed)); + } + + const coord_t fast_speed = config.maximum_move_distance + extra_speed; + const coord_t slow_speed = config.maximum_move_distance_slow + extra_speed + extra_slow_speed; + + Polygons offset_slow, offset_fast; + + bool add = false; + bool bypass_merge = false; + constexpr bool increase_radius = true, no_error = true, use_min_radius = true, move = true; // aliases for better readability + + // Determine in which order configurations are checked if they result in a valid influence area. Check will stop if a valid area is found + std::vector order; + auto insertSetting = [&](AreaIncreaseSettings settings, bool back) { + if (std::find(order.begin(), order.end(), settings) == order.end()) { + if (back) + order.emplace_back(settings); + else + order.insert(order.begin(), settings); + } + }; + + const bool parent_moved_slow = elem.last_area_increase.increase_speed < config.maximum_move_distance; + const bool avoidance_speed_mismatch = parent_moved_slow && elem.last_area_increase.type != AvoidanceType::Slow; + if (elem.last_area_increase.move && elem.last_area_increase.no_error && elem.can_use_safe_radius && !mergelayer && + !avoidance_speed_mismatch && (elem.distance_to_top >= config.tip_layers || parent_moved_slow)) { + // assume that the avoidance type that was best for the parent is best for me. Makes this function about 7% faster. + insertSetting({ elem.last_area_increase.type, elem.last_area_increase.increase_speed < config.maximum_move_distance ? slow_speed : fast_speed, + increase_radius, elem.last_area_increase.no_error, !use_min_radius, elem.last_area_increase.move }, true); + insertSetting({ elem.last_area_increase.type, elem.last_area_increase.increase_speed < config.maximum_move_distance ? slow_speed : fast_speed, + !increase_radius, elem.last_area_increase.no_error, !use_min_radius, elem.last_area_increase.move }, true); + } + // branch may still go though a hole, so a check has to be done whether the hole was already passed, and the regular avoidance can be used. + if (!elem.can_use_safe_radius) { + // if the radius until which it is always increased can not be guaranteed, move fast. This is to avoid holes smaller than the real branch radius. + // This does not guarantee the avoidance of such holes, but ensures they are avoided if possible. + // order.emplace_back(AvoidanceType::Slow,!increase_radius,no_error,!use_min_radius,move); + insertSetting({ AvoidanceType::Slow, slow_speed, increase_radius, no_error, !use_min_radius, !move }, true); // did we go through the hole + // in many cases the definition of hole is overly restrictive, so to avoid unnecessary fast movement in the tip, it is ignored there for a bit. + // This CAN cause a branch to go though a hole it otherwise may have avoided. + if (elem.distance_to_top < round_up_divide(config.tip_layers, size_t(2))) + insertSetting({ AvoidanceType::Fast, slow_speed, increase_radius, no_error, !use_min_radius, !move }, true); + insertSetting({ AvoidanceType::FastSafe, fast_speed, increase_radius, no_error, !use_min_radius, !move }, true); // did we manage to avoid the hole + insertSetting({ AvoidanceType::FastSafe, fast_speed, !increase_radius, no_error, !use_min_radius, move }, true); + insertSetting({ AvoidanceType::Fast, fast_speed, !increase_radius, no_error, !use_min_radius, move }, true); + } else { + insertSetting({ AvoidanceType::Slow, slow_speed, increase_radius, no_error, !use_min_radius, move }, true); + // while moving fast to be able to increase the radius (b) may seems preferable (over a) this can cause the a sudden skip in movement, + // which looks similar to a layer shift and can reduce stability. + // as such idx have chosen to only use the user setting for radius increases as a friendly recommendation. + insertSetting({ AvoidanceType::Slow, slow_speed, !increase_radius, no_error, !use_min_radius, move }, true); // a + if (elem.distance_to_top < config.tip_layers) + insertSetting({ AvoidanceType::FastSafe, slow_speed, increase_radius, no_error, !use_min_radius, move }, true); + insertSetting({ AvoidanceType::FastSafe, fast_speed, increase_radius, no_error, !use_min_radius, move }, true); // b + insertSetting({ AvoidanceType::FastSafe, fast_speed, !increase_radius, no_error, !use_min_radius, move }, true); + } + + if (elem.use_min_xy_dist) { + std::vector new_order; + // if the branch currently has to use min_xy_dist check if the configuration would also be valid + // with the regular xy_distance before checking with use_min_radius (Only happens when Support Distance priority is z overrides xy ) + for (AreaIncreaseSettings settings : order) { + new_order.emplace_back(settings); + new_order.push_back({ settings.type, settings.increase_speed, settings.increase_radius, settings.no_error, use_min_radius, settings.move }); + } + order = new_order; + } + if (elem.to_buildplate || (elem.to_model_gracious && intersection(parent.influence_area, volumes.getPlaceableAreas(radius, layer_idx, throw_on_cancel)).empty())) { + // error case + // it is normal that we wont be able to find a new area at some point in time if we wont be able to reach layer 0 aka have to connect with the model + insertSetting({ AvoidanceType::Fast, fast_speed, !increase_radius, !no_error, elem.use_min_xy_dist, move }, true); + } + if (elem.distance_to_top < elem.dont_move_until && elem.can_use_safe_radius) // only do not move when holes would be avoided in every case. + // Only do not move when already in a no hole avoidance with the regular xy distance. + insertSetting({ AvoidanceType::Slow, 0, increase_radius, no_error, !use_min_radius, !move }, false); + + Polygons inc_wo_collision; + // Check whether it is faster to calculate the area increased with the fast speed independently from the slow area, or time could be saved by reusing the slow area to calculate the fast one. + // Calculated by comparing the steps saved when calcualting idependently with the saved steps when not. + bool offset_independant_faster = radius / safe_movement_distance - int(config.maximum_move_distance + extra_speed < radius + safe_movement_distance) > + round_up_divide((extra_speed + extra_slow_speed + config.maximum_move_distance_slow), safe_movement_distance); + for (const AreaIncreaseSettings &settings : order) { + if (settings.move) { + if (offset_slow.empty() && (settings.increase_speed == slow_speed || ! offset_independant_faster)) { + // offsetting in 2 steps makes our offsetted area rounder preventing (rounding) errors created by to pointy areas. At this point one can see that the Polygons class + // was never made for precision in the single digit micron range. + offset_slow = safe_offset_inc(parent.influence_area, extra_speed + extra_slow_speed + config.maximum_move_distance_slow, + wall_restriction, safe_movement_distance, offset_independant_faster ? safe_movement_distance + radius : 0, 2); +#ifdef TREESUPPORT_DEBUG_SVG + SVG::export_expolygons(debug_out_path("treesupport-increase_areas_one_layer-slow-%d-%ld.svg", layer_idx, int(merging_area_idx)), + { { { union_ex(wall_restriction) }, { "wall_restricrictions", "gray", 0.5f } }, + { { union_ex(offset_slow) }, { "offset_slow", "red", "black", "", scaled(0.1f), 0.5f } } }); +#endif // TREESUPPORT_DEBUG_SVG + } + if (offset_fast.empty() && settings.increase_speed != slow_speed) { + if (offset_independant_faster) + offset_fast = safe_offset_inc(parent.influence_area, extra_speed + config.maximum_move_distance, + wall_restriction, safe_movement_distance, offset_independant_faster ? safe_movement_distance + radius : 0, 1); + else { + const coord_t delta_slow_fast = config.maximum_move_distance - (config.maximum_move_distance_slow + extra_slow_speed); + offset_fast = safe_offset_inc(offset_slow, delta_slow_fast, wall_restriction, safe_movement_distance, safe_movement_distance + radius, offset_independant_faster ? 2 : 1); + } +#ifdef TREESUPPORT_DEBUG_SVG + SVG::export_expolygons(debug_out_path("treesupport-increase_areas_one_layer-fast-%d-%ld.svg", layer_idx, int(merging_area_idx)), + { { { union_ex(wall_restriction) }, { "wall_restricrictions", "gray", 0.5f } }, + { { union_ex(offset_fast) }, { "offset_fast", "red", "black", "", scaled(0.1f), 0.5f } } }); +#endif // TREESUPPORT_DEBUG_SVG + } + } + std::optional result; + inc_wo_collision.clear(); + if (!settings.no_error) { + // ERROR CASE + // if the area becomes for whatever reason something that clipper sees as a line, offset would stop working, so ensure that even if if wrongly would be a line, it still actually has an area that can be increased + Polygons lines_offset = offset(to_polylines(parent.influence_area), scaled(0.005), jtMiter, 1.2); + Polygons base_error_area = union_(parent.influence_area, lines_offset); + result = increase_single_area(volumes, config, settings, layer_idx, parent, + base_error_area, to_bp_data, to_model_data, inc_wo_collision, (config.maximum_move_distance + extra_speed) * 1.5, mergelayer); +#ifdef TREE_SUPPORT_SHOW_ERRORS + BOOST_LOG_TRIVIAL(error) +#else // TREE_SUPPORT_SHOW_ERRORS + BOOST_LOG_TRIVIAL(warning) +#endif // TREE_SUPPORT_SHOW_ERRORS + << "Influence area could not be increased! Data about the Influence area: " + "Radius: " << radius << " at layer: " << layer_idx - 1 << " NextTarget: " << elem.layer_idx << " Distance to top: " << elem.distance_to_top << + " Elephant foot increases " << elem.elephant_foot_increases << " use_min_xy_dist " << elem.use_min_xy_dist << " to buildplate " << elem.to_buildplate << + " gracious " << elem.to_model_gracious << " safe " << elem.can_use_safe_radius << " until move " << elem.dont_move_until << " \n " + "Parent " << &parent << ": Radius: " << config.getCollisionRadius(parent.state) << " at layer: " << layer_idx << " NextTarget: " << parent.state.layer_idx << + " Distance to top: " << parent.state.distance_to_top << " Elephant foot increases " << parent.state.elephant_foot_increases << " use_min_xy_dist " << parent.state.use_min_xy_dist << + " to buildplate " << parent.state.to_buildplate << " gracious " << parent.state.to_model_gracious << " safe " << parent.state.can_use_safe_radius << " until move " << parent.state.dont_move_until; + tree_supports_show_error("Potentially lost branch!"sv, true); + } else + result = increase_single_area(volumes, config, settings, layer_idx, parent, + settings.increase_speed == slow_speed ? offset_slow : offset_fast, to_bp_data, to_model_data, inc_wo_collision, 0, mergelayer); + + if (result) { + elem = *result; + radius = config.getCollisionRadius(elem); + elem.last_area_increase = settings; + add = true; + // do not merge if the branch should not move or the priority has to be to get farther away from the model. + bypass_merge = !settings.move || (settings.use_min_distance && elem.distance_to_top < config.tip_layers); + if (settings.move) + elem.dont_move_until = 0; + else + elem.result_on_layer = parent.state.result_on_layer; + + elem.can_use_safe_radius = settings.type != AvoidanceType::Fast; + + if (!settings.use_min_distance) + elem.use_min_xy_dist = false; + if (!settings.no_error) +#ifdef TREE_SUPPORT_SHOW_ERRORS + BOOST_LOG_TRIVIAL(error) +#else // TREE_SUPPORT_SHOW_ERRORS + BOOST_LOG_TRIVIAL(info) +#endif // TREE_SUPPORT_SHOW_ERRORS + << "Trying to keep area by moving faster than intended: Success"; + break; + } else if (!settings.no_error) + BOOST_LOG_TRIVIAL(error) << "Trying to keep area by moving faster than intended: FAILURE! WRONG BRANCHES LIKLY!"; + } + + if (add) { + // Union seems useless, but some rounding errors somewhere can cause to_bp_data to be slightly bigger than it should be. + assert(! inc_wo_collision.empty() || ! to_bp_data.empty() || ! to_model_data.empty()); + Polygons max_influence_area = safe_union( + diff_clipped(inc_wo_collision, volumes.getCollision(radius, layer_idx - 1, elem.use_min_xy_dist)), + safe_union(to_bp_data, to_model_data)); + merging_area.state = elem; + assert(!max_influence_area.empty()); + merging_area.set_bbox(get_extents(max_influence_area)); + merging_area.areas.influence_areas = std::move(max_influence_area); + if (! bypass_merge) { + if (elem.to_buildplate) + merging_area.areas.to_bp_areas = std::move(to_bp_data); + if (config.support_rests_on_model) + merging_area.areas.to_model_areas = std::move(to_model_data); + } + } else { + // If the bottom most point of a branch is set, later functions will assume that the position is valid, and ignore it. + // But as branches connecting with the model that are to small have to be culled, the bottom most point has to be not set. + // A point can be set on the top most tip layer (maybe more if it should not move for a few layers). + parent.state.result_on_layer_reset(); + } + throw_on_cancel(); + } + }); +} + +[[nodiscard]] static SupportElementState merge_support_element_states( + const SupportElementState &first, const SupportElementState &second, const Point &next_position, const coord_t layer_idx, + const TreeSupportSettings &config) +{ + SupportElementState out; + out.next_position = next_position; + out.layer_idx = layer_idx; + out.use_min_xy_dist = first.use_min_xy_dist || second.use_min_xy_dist; + out.supports_roof = first.supports_roof || second.supports_roof; + out.dont_move_until = std::max(first.dont_move_until, second.dont_move_until); + out.can_use_safe_radius = first.can_use_safe_radius || second.can_use_safe_radius; + out.missing_roof_layers = std::min(first.missing_roof_layers, second.missing_roof_layers); + out.skip_ovalisation = false; + if (first.target_height > second.target_height) { + out.target_height = first.target_height; + out.target_position = first.target_position; + } else { + out.target_height = second.target_height; + out.target_position = second.target_position; + } + out.effective_radius_height = std::max(first.effective_radius_height, second.effective_radius_height); + out.distance_to_top = std::max(first.distance_to_top, second.distance_to_top); + + out.to_buildplate = first.to_buildplate && second.to_buildplate; + out.to_model_gracious = first.to_model_gracious && second.to_model_gracious; // valid as we do not merge non-gracious with gracious + + out.elephant_foot_increases = 0; + if (config.diameter_scale_bp_radius > 0) { + coord_t foot_increase_radius = std::abs(std::max(config.getCollisionRadius(second), config.getCollisionRadius(first)) - config.getCollisionRadius(out)); + // elephant_foot_increases has to be recalculated, as when a smaller tree with a larger elephant_foot_increases merge with a larger branch + // the elephant_foot_increases may have to be lower as otherwise the radius suddenly increases. This results often in a non integer value. + out.elephant_foot_increases = foot_increase_radius / (config.branch_radius * (config.diameter_scale_bp_radius - config.diameter_angle_scale_factor)); + } + + // set last settings to the best out of both parents. If this is wrong, it will only cause a small performance penalty instead of weird behavior. + out.last_area_increase = { + std::min(first.last_area_increase.type, second.last_area_increase.type), + std::min(first.last_area_increase.increase_speed, second.last_area_increase.increase_speed), + first.last_area_increase.increase_radius || second.last_area_increase.increase_radius, + first.last_area_increase.no_error || second.last_area_increase.no_error, + first.last_area_increase.use_min_distance && second.last_area_increase.use_min_distance, + first.last_area_increase.move || second.last_area_increase.move }; + + return out; +} + +static bool merge_influence_areas_two_elements( + const TreeModelVolumes &volumes, const TreeSupportSettings &config, const LayerIndex layer_idx, + SupportElementMerging &dst, SupportElementMerging &src) +{ + // Don't merge gracious with a non gracious area as bad placement could negatively impact reliability of the whole subtree. + const bool merging_gracious_and_non_gracious = dst.state.to_model_gracious != src.state.to_model_gracious; + // Could cause some issues with the increase of one area, as it is assumed that if the smaller is increased + // by the delta to the larger it is engulfed by it already. But because a different collision + // may be removed from the in draw_area() generated circles, this assumption could be wrong. + const bool merging_min_and_regular_xy = dst.state.use_min_xy_dist != src.state.use_min_xy_dist; + + if (merging_gracious_and_non_gracious || merging_min_and_regular_xy) + return false; + + const bool dst_radius_bigger = config.getCollisionRadius(dst.state) > config.getCollisionRadius(src.state); + const SupportElementMerging &smaller_rad = dst_radius_bigger ? src : dst; + const SupportElementMerging &bigger_rad = dst_radius_bigger ? dst : src; + const coord_t real_radius_delta = std::abs(config.getRadius(bigger_rad.state) - config.getRadius(smaller_rad.state)); + { + // Testing intersection of bounding boxes. + // Expand the smaller radius branch bounding box to match the lambda intersect_small_with_bigger() below. + // Because the lambda intersect_small_with_bigger() applies a rounded offset, a snug offset of the bounding box + // is sufficient. On the other side, if a mitered offset was used by the lambda, + // the bounding box expansion would have to account for the mitered extension of the sharp corners. + Eigen::AlignedBox smaller_bbox = smaller_rad.bbox(); + smaller_bbox.min() -= Point{ real_radius_delta, real_radius_delta }; + smaller_bbox.max() += Point{ real_radius_delta, real_radius_delta }; + if (! smaller_bbox.intersects(bigger_rad.bbox())) + return false; + } + + // Accumulator of a radius increase of a "to model" branch by merging in a "to build plate" branch. + coord_t increased_to_model_radius = 0; + const bool merging_to_bp = dst.state.to_buildplate && src.state.to_buildplate; + if (! merging_to_bp) { + // Get the real radius increase as the user does not care for the collision model. + if (dst.state.to_buildplate != src.state.to_buildplate) { + // Merging a "to build plate" branch with a "to model" branch. + // Don't allow merging a thick "to build plate" branch into a thinner "to model" branch. + const coord_t rdst = config.getRadius(dst.state); + const coord_t rsrc = config.getRadius(src.state); + if (dst.state.to_buildplate) { + if (rsrc < rdst) + increased_to_model_radius = src.state.increased_to_model_radius + rdst - rsrc; + } else { + if (rsrc > rdst) + increased_to_model_radius = dst.state.increased_to_model_radius + rsrc - rdst; + } + if (increased_to_model_radius > config.max_to_model_radius_increase) + return false; + } + // if a merge could place a stable branch on unstable ground, would be increasing the radius further + // than allowed to when merging to model and to_bp trees or would merge to model before it is known + // they will even been drawn the merge is skipped + if (! dst.state.supports_roof && ! src.state.supports_roof && + std::max(src.state.distance_to_top, dst.state.distance_to_top) < config.min_dtt_to_model) + return false; + } + + // Area of the bigger radius is used to ensure correct placement regarding the relevant avoidance, + // so if that would change an invalid area may be created. + if (! bigger_rad.state.can_use_safe_radius && smaller_rad.state.can_use_safe_radius) + return false; + + // the bigger radius is used to verify that the area is still valid after the increase with the delta. + // If there were a point where the big influence area could be valid with can_use_safe_radius + // the element would already be can_use_safe_radius. + // the smaller radius, which gets increased by delta may reach into the area where use_min_xy_dist is no longer required. + const bool use_min_radius = bigger_rad.state.use_min_xy_dist && smaller_rad.state.use_min_xy_dist; + + // The idea is that the influence area with the smaller collision radius is increased by the radius difference. + // If this area has any intersections with the influence area of the larger collision radius, a branch (of the larger collision radius) placed in this intersection, has already engulfed the branch of the smaller collision radius. + // Because of this a merge may happen even if the influence areas (that represent possible center points of branches) do not intersect yet. + // Remember that collision radius <= real radius as otherwise this assumption would be false. + const coord_t smaller_collision_radius = config.getCollisionRadius(smaller_rad.state); + const Polygons &collision = volumes.getCollision(smaller_collision_radius, layer_idx - 1, use_min_radius); + auto intersect_small_with_bigger = [real_radius_delta, smaller_collision_radius, &collision, &config](const Polygons &small, const Polygons &bigger) { + return intersection( + safe_offset_inc( + small, real_radius_delta, collision, + // -3 avoids possible rounding errors + 2 * (config.xy_distance + smaller_collision_radius - 3), 0, 0), + bigger); + }; + Polygons intersect = intersect_small_with_bigger( + merging_to_bp ? smaller_rad.areas.to_bp_areas : smaller_rad.areas.to_model_areas, + merging_to_bp ? bigger_rad.areas.to_bp_areas : bigger_rad.areas.to_model_areas); + + // dont use empty as a line is not empty, but for this use-case it very well may be (and would be one layer down as union does not keep lines) + // check if the overlap is large enough (Small ares tend to attract rounding errors in clipper). + if (area(intersect) <= tiny_area_threshold) + return false; + + // While 0.025 was guessed as enough, i did not have reason to change it. + if (area(offset(intersect, scaled(-0.025), jtMiter, 1.2)) <= tiny_area_threshold) + return false; + + // Do the actual merge now that the branches are confirmed to be able to intersect. + // calculate which point is closest to the point of the last merge (or tip center if no merge above it has happened) + // used at the end to estimate where to best place the branch on the bottom most layer + // could be replaced with a random point inside the new area + Point new_pos = move_inside_if_outside(intersect, dst.state.next_position); + + SupportElementState new_state = merge_support_element_states(dst.state, src.state, new_pos, layer_idx - 1, config); + new_state.increased_to_model_radius = increased_to_model_radius == 0 ? + // increased_to_model_radius was not set yet. Propagate maximum. + std::max(dst.state.increased_to_model_radius, src.state.increased_to_model_radius) : + increased_to_model_radius; + + // Rather unioning with "intersect" due to some rounding errors. + Polygons influence_areas = safe_union( + intersect_small_with_bigger(smaller_rad.areas.influence_areas, bigger_rad.areas.influence_areas), + intersect); + + Polygons to_model_areas; + if (merging_to_bp && config.support_rests_on_model) + to_model_areas = new_state.to_model_gracious ? + // Rather unioning with "intersect" due to some rounding errors. + safe_union( + intersect_small_with_bigger(smaller_rad.areas.to_model_areas, bigger_rad.areas.to_model_areas), + intersect) : + influence_areas; + + dst.parents.insert(dst.parents.end(), src.parents.begin(), src.parents.end()); + dst.state = new_state; + dst.areas.influence_areas = std::move(influence_areas); + dst.areas.to_bp_areas.clear(); + dst.areas.to_model_areas.clear(); + if (merging_to_bp) { + dst.areas.to_bp_areas = std::move(intersect); + if (config.support_rests_on_model) + dst.areas.to_model_areas = std::move(to_model_areas); + } else + dst.areas.to_model_areas = std::move(intersect); + // Update the bounding box. + BoundingBox bbox(get_extents(dst.areas.influence_areas)); + bbox.merge(get_extents(dst.areas.to_bp_areas)); + bbox.merge(get_extents(dst.areas.to_model_areas)); + dst.set_bbox(bbox); + // Clear the source data. + src.areas.clear(); + src.parents.clear(); + return true; +} + +/*! + * \brief Merges Influence Areas if possible. + * + * Branches which do overlap have to be merged. This helper merges all elements in input with the elements into reduced_new_layer. + * Elements in input_aabb are merged together if possible, while elements reduced_new_layer_aabb are not checked against each other. + * + * \param reduced_aabb[in,out] The already processed elements. + * \param input_aabb[in] Not yet processed elements + * \param to_bp_areas[in] The Elements of the current Layer that will reach the buildplate. Value is the influence area where the center of a circle of support may be placed. + * \param to_model_areas[in] The Elements of the current Layer that do not have to reach the buildplate. Also contains main as every element that can reach the buildplate is not forced to. + * Value is the influence area where the center of a circle of support may be placed. + * \param influence_areas[in] The influence areas without avoidance removed. + * \param insert_bp_areas[out] Elements to be inserted into the main dictionary after the Helper terminates. + * \param insert_model_areas[out] Elements to be inserted into the secondary dictionary after the Helper terminates. + * \param insert_influence[out] Elements to be inserted into the dictionary containing the largest possibly valid influence area (ignoring if the area may not be there because of avoidance) + * \param erase[out] Elements that should be deleted from the above dictionaries. + * \param layer_idx[in] The Index of the current Layer. + */ + +static SupportElementMerging* merge_influence_areas_leaves( + const TreeModelVolumes &volumes, const TreeSupportSettings &config, const LayerIndex layer_idx, + SupportElementMerging * const dst_begin, SupportElementMerging *dst_end) +{ + // Merging at the lowest level of the AABB tree. Checking one against each other, O(n^2). + assert(dst_begin < dst_end); + for (SupportElementMerging *i = dst_begin; i + 1 < dst_end;) { + for (SupportElementMerging *j = i + 1; j != dst_end;) + if (merge_influence_areas_two_elements(volumes, config, layer_idx, *i, *j)) { + // i was merged with j, j is empty. + if (j != -- dst_end) + *j = std::move(*dst_end); + goto merged; + } else + ++ j; + // not merged + ++ i; + merged: + ; + } + return dst_end; +} + +static SupportElementMerging* merge_influence_areas_two_sets( + const TreeModelVolumes &volumes, const TreeSupportSettings &config, const LayerIndex layer_idx, + SupportElementMerging * const dst_begin, SupportElementMerging * dst_end, + SupportElementMerging * src_begin, SupportElementMerging * const src_end) +{ + // Merging src into dst. + // Areas of src should not overlap with areas of another elements of src. + // Areas of dst should not overlap with areas of another elements of dst. + // The memory from dst_begin to src_end is reserved for the merging operation, + // src follows dst. + assert(src_begin < src_end); + assert(dst_begin < dst_end); + assert(dst_end <= src_begin); + for (SupportElementMerging *src = src_begin; src != src_end; ++ src) { + SupportElementMerging *dst = dst_begin; + SupportElementMerging *merged = nullptr; + for (; dst != dst_end; ++ dst) + if (merge_influence_areas_two_elements(volumes, config, layer_idx, *dst, *src)) { + merged = dst ++; + if (src != src_begin) + // Compactify src. + *src = std::move(*src_begin); + ++ src_begin; + break; + } + for (; dst != dst_end;) + if (merge_influence_areas_two_elements(volumes, config, layer_idx, *merged, *dst)) { + // Compactify dst. + if (dst != -- dst_end) + *dst = std::move(*dst_end); + } else + ++ dst; + } + // Compactify src elements that were not merged with dst to the end of dst. + assert(dst_end <= src_begin); + if (dst_end == src_begin) + dst_end = src_end; + else + while (src_begin != src_end) + *dst_end ++ = std::move(*src_begin ++); + + return dst_end; +} + +/*! + * \brief Merges Influence Areas at one layer if possible. + * + * Branches which do overlap have to be merged. This manages the helper and uses a divide and conquer approach to parallelize this problem. This parallelization can at most accelerate the merging by a factor of 2. + * + * \param to_bp_areas[in] The Elements of the current Layer that will reach the buildplate. + * Value is the influence area where the center of a circle of support may be placed. + * \param to_model_areas[in] The Elements of the current Layer that do not have to reach the buildplate. Also contains main as every element that can reach the buildplate is not forced to. + * Value is the influence area where the center of a circle of support may be placed. + * \param influence_areas[in] The Elements of the current Layer without avoidances removed. This is the largest possible influence area for this layer. + * Value is the influence area where the center of a circle of support may be placed. + * \param layer_idx[in] The current layer. + */ +static void merge_influence_areas( + const TreeModelVolumes &volumes, + const TreeSupportSettings &config, + const LayerIndex layer_idx, + std::vector &influence_areas, + std::function throw_on_cancel) +{ + const size_t input_size = influence_areas.size(); + if (input_size == 0) + return; + + // Merging by divide & conquer. + // The majority of time is consumed by Clipper polygon operations, intersection is accelerated by bounding boxes. + // Sorting input into an AABB tree helps to perform most of the intersections at first iterations, + // thus reducing computation when merging larger subtrees. + // The actual merge logic is found in merge_influence_areas_two_sets. + + // Build an AABB tree over the influence areas. + //FIXME A full tree does not need to be built, the lowest level branches will be always bucketed. + // However the additional time consumed is negligible. + AABBTreeIndirect::Tree<2, coord_t> tree; + // Sort influence_areas in place. + tree.build_modify_input(influence_areas); + + throw_on_cancel(); + + // Prepare the initial buckets as ranges of influence areas. The initial buckets contain power of 2 influence areas to follow + // the branching of the AABB tree. + // Vectors of ranges of influence areas, following the branching of the AABB tree: + std::vector> buckets; + // Initial number of buckets for 1st round of merging. + size_t num_buckets_initial; + { + // How many buckets per first merge iteration? + const size_t num_threads = tbb::this_task_arena::max_concurrency(); + // 4 buckets per thread if possible, + const size_t num_buckets_min = (input_size + 2) / 4; + // 2 buckets per thread otherwise. + const size_t num_buckets_max = input_size / 2; + num_buckets_initial = num_buckets_min >= num_threads ? num_buckets_min : num_buckets_max; + const size_t bucket_size = num_buckets_min >= num_threads ? 4 : 2; + // Fill in the buckets. + SupportElementMerging *it = influence_areas.data(); + // Reserve one more bucket to keep a single influence area which will not be merged in the first iteration. + buckets.reserve(num_buckets_initial + 1); + for (size_t i = 0; i < num_buckets_initial; ++ i, it += bucket_size) + buckets.emplace_back(std::make_pair(it, it + bucket_size)); + SupportElementMerging *it_end = influence_areas.data() + influence_areas.size(); + if (buckets.back().second >= it_end) { + // Last bucket is less than size 4, but bigger than size 1. + buckets.back().second = std::min(buckets.back().second, it_end); + } else { + // Last bucket is size 1, it will not be merged in the first iteration. + assert(it + 1 == it_end); + buckets.emplace_back(std::make_pair(it, it_end)); + } + } + + // 1st merge iteration, merge one with each other. + tbb::parallel_for(tbb::blocked_range(0, num_buckets_initial), + [&](const tbb::blocked_range &range) { + for (size_t idx = range.begin(); idx < range.end(); ++ idx) { + const size_t bucket_pair_idx = idx * 2; + // Merge bucket_count adjacent to each other, merging uneven bucket numbers into even buckets + buckets[idx].second = merge_influence_areas_leaves(volumes, config, layer_idx, buckets[idx].first, buckets[idx].second); + throw_on_cancel(); + } + }); + + // Further merge iterations, merging one AABB subtree with another one, hopefully minimizing intersections between the elements + // of each of the subtree. + while (buckets.size() > 1) { + tbb::parallel_for(tbb::blocked_range(0, buckets.size() / 2), + [&](const tbb::blocked_range &range) { + for (size_t idx = range.begin(); idx < range.end(); ++ idx) { + const size_t bucket_pair_idx = idx * 2; + // Merge bucket_count adjacent to each other, merging uneven bucket numbers into even buckets + buckets[bucket_pair_idx].second = merge_influence_areas_two_sets(volumes, config, layer_idx, + buckets[bucket_pair_idx].first, buckets[bucket_pair_idx].second, + buckets[bucket_pair_idx + 1].first, buckets[bucket_pair_idx + 1].second); + throw_on_cancel(); + } + }); + // Remove odd buckets, which were merged into even buckets. + size_t new_size = (buckets.size() + 1) / 2; + for (size_t i = 1; i < new_size; ++ i) + buckets[i] = std::move(buckets[i * 2]); + buckets.erase(buckets.begin() + new_size, buckets.end()); + } +} + +/*! + * \brief Propagates influence downwards, and merges overlapping ones. + * + * \param move_bounds[in,out] All currently existing influence areas + */ +void create_layer_pathing(const TreeModelVolumes &volumes, const TreeSupportSettings &config, std::vector &move_bounds, std::function throw_on_cancel) +{ +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + const double data_size_inverse = 1 / double(move_bounds.size()); + double progress_total = TREE_PROGRESS_PRECALC_AVO + TREE_PROGRESS_PRECALC_COLL + TREE_PROGRESS_GENERATE_NODES; +#endif // SLIC3R_TREESUPPORTS_PROGRESS + + auto dur_inc = std::chrono::duration_values::zero(); + auto dur_total = std::chrono::duration_values::zero(); + + LayerIndex last_merge_layer_idx = move_bounds.size(); + bool new_element = false; + + // Ensures at least one merge operation per 3mm height, 50 layers, 1 mm movement of slow speed or 5mm movement of fast speed (whatever is lowest). Values were guessed. + size_t max_merge_every_x_layers = std::min(std::min(5000 / (std::max(config.maximum_move_distance, coord_t(100))), 1000 / std::max(config.maximum_move_distance_slow, coord_t(20))), 3000 / config.layer_height); + size_t merge_every_x_layers = 1; + // Calculate the influence areas for each layer below (Top down) + // This is done by first increasing the influence area by the allowed movement distance, and merging them with other influence areas if possible + for (int layer_idx = int(move_bounds.size()) - 1; layer_idx > 0; -- layer_idx) + if (SupportElements &prev_layer = move_bounds[layer_idx]; ! prev_layer.empty()) { + // merging is expensive and only parallelized to a max speedup of 2. As such it may be useful in some cases to only merge every few layers to improve performance. + bool had_new_element = new_element; + const bool merge_this_layer = had_new_element || size_t(last_merge_layer_idx - layer_idx) >= merge_every_x_layers; + if (had_new_element) + merge_every_x_layers = 1; + const auto ta = std::chrono::high_resolution_clock::now(); + + // ### Increase the influence areas by the allowed movement distance + std::vector influence_areas; + influence_areas.reserve(prev_layer.size()); + for (int32_t element_idx = 0; element_idx < int32_t(prev_layer.size()); ++ element_idx) { + SupportElement &el = prev_layer[element_idx]; + assert(!el.influence_area.empty()); + SupportElement::ParentIndices parents; + parents.emplace_back(element_idx); + influence_areas.push_back({ el.state, parents }); + } + increase_areas_one_layer(volumes, config, influence_areas, layer_idx, prev_layer, merge_this_layer, throw_on_cancel); + + // Place already fully constructed elements to the output, remove them from influence_areas. + SupportElements &this_layer = move_bounds[layer_idx - 1]; + influence_areas.erase(std::remove_if(influence_areas.begin(), influence_areas.end(), + [&this_layer, layer_idx](SupportElementMerging &elem) { + if (elem.areas.influence_areas.empty()) + // This area was removed completely due to collisions. + return true; + if (elem.areas.to_bp_areas.empty() && elem.areas.to_model_areas.empty()) { + if (area(elem.areas.influence_areas) < tiny_area_threshold) { + BOOST_LOG_TRIVIAL(error) << "Insert Error of Influence area bypass on layer " << layer_idx - 1; + tree_supports_show_error("Insert error of area after bypassing merge.\n"sv, true); + } + // Move the area to output. + this_layer.emplace_back(elem.state, std::move(elem.parents), std::move(elem.areas.influence_areas)); + return true; + } + // Keep the area. + return false; + }), + influence_areas.end()); + + dur_inc += std::chrono::high_resolution_clock::now() - ta; + new_element = ! move_bounds[layer_idx - 1].empty(); + if (merge_this_layer) { + bool reduced_by_merging = false; + if (size_t count_before_merge = influence_areas.size(); count_before_merge > 1) { + // ### Calculate which influence areas overlap, and merge them into a new influence area (simplified: an intersection of influence areas that have such an intersection) + merge_influence_areas(volumes, config, layer_idx, influence_areas, throw_on_cancel); + reduced_by_merging = count_before_merge > influence_areas.size(); + } + last_merge_layer_idx = layer_idx; + if (! reduced_by_merging && ! had_new_element) + merge_every_x_layers = std::min(max_merge_every_x_layers, merge_every_x_layers + 1); + } + + dur_total += std::chrono::high_resolution_clock::now() - ta; + + // Save calculated elements to output, and allocate Polygons on heap, as they will not be changed again. + for (SupportElementMerging &elem : influence_areas) + if (! elem.areas.influence_areas.empty()) { + Polygons new_area = safe_union(elem.areas.influence_areas); + if (area(new_area) < tiny_area_threshold) { + BOOST_LOG_TRIVIAL(error) << "Insert Error of Influence area on layer " << layer_idx - 1 << ". Origin of " << elem.parents.size() << " areas. Was to bp " << elem.state.to_buildplate; + tree_supports_show_error("Insert error of area after merge.\n"sv, true); + } + this_layer.emplace_back(elem.state, std::move(elem.parents), std::move(new_area)); + } + + #ifdef SLIC3R_TREESUPPORTS_PROGRESS + progress_total += data_size_inverse * TREE_PROGRESS_AREA_CALC; + Progress::messageProgress(Progress::Stage::SUPPORT, progress_total * m_progress_multiplier + m_progress_offset, TREE_PROGRESS_TOTAL); + #endif + throw_on_cancel(); + } + + BOOST_LOG_TRIVIAL(info) << "Time spent with creating influence areas' subtasks: Increasing areas " << dur_inc.count() / 1000000 << + " ms merging areas: " << (dur_total - dur_inc).count() / 1000000 << " ms"; +} + +/*! + * \brief Sets the result_on_layer for all parents based on the SupportElement supplied. + * + * \param elem[in] The SupportElements, which parent's position should be determined. + */ +static void set_points_on_areas(const SupportElement &elem, SupportElements *layer_above) +{ + assert(!elem.state.deleted); + assert(layer_above != nullptr || elem.parents.empty()); + + // Based on the branch center point of the current layer, the point on the next (further up) layer is calculated. + if (! elem.state.result_on_layer_is_set()) { + BOOST_LOG_TRIVIAL(error) << "Uninitialized support element"; + tree_supports_show_error("Uninitialized support element. A branch may be missing.\n"sv, true); + return; + } + + if (layer_above) + for (int32_t next_elem_idx : elem.parents) { + assert(next_elem_idx >= 0); + SupportElement &next_elem = (*layer_above)[next_elem_idx]; + assert(! next_elem.state.deleted); + // if the value was set somewhere else it it kept. This happens when a branch tries not to move after being unable to create a roof. + if (! next_elem.state.result_on_layer_is_set()) { + // Move inside has edgecases (see tests) so DONT use Polygons.inside to confirm correct move, Error with distance 0 is <= 1 + // it is not required to check if how far this move moved a point as is can be larger than maximum_movement_distance. + // While this seems like a problem it may for example occur after merges. + next_elem.state.result_on_layer = move_inside_if_outside(next_elem.influence_area, elem.state.result_on_layer); + // do not call recursive because then amount of layers would be restricted by the stack size + } + // Mark the parent element as accessed from a valid child element. + next_elem.state.marked = true; + } +} + +static void set_to_model_contact_simple(SupportElement &elem) +{ + const Point best = move_inside_if_outside(elem.influence_area, elem.state.next_position); + elem.state.result_on_layer = best; + BOOST_LOG_TRIVIAL(debug) << "Added NON gracious Support On Model Point (" << best.x() << "," << best.y() << "). The current layer is " << elem.state.layer_idx; +} + +/*! + * \brief Get the best point to connect to the model and set the result_on_layer of the relevant SupportElement accordingly. + * + * \param move_bounds[in,out] All currently existing influence areas + * \param first_elem[in,out] SupportElement that did not have its result_on_layer set meaning that it does not have a child element. + * \param layer_idx[in] The current layer. + */ +static void set_to_model_contact_to_model_gracious( + const TreeModelVolumes &volumes, + const TreeSupportSettings &config, + std::vector &move_bounds, + SupportElement &first_elem, + std::function throw_on_cancel) +{ + SupportElement *last_successfull_layer = nullptr; + + // check for every layer upwards, up to the point where this influence area was created (either by initial insert or merge) if the branch could be placed on it, and highest up layer index. + { + SupportElement *elem = &first_elem; + for (LayerIndex layer_check = elem->state.layer_idx; + ! intersection(elem->influence_area, volumes.getPlaceableAreas(config.getCollisionRadius(elem->state), layer_check, throw_on_cancel)).empty(); + elem = &move_bounds[++ layer_check][elem->parents.front()]) { + assert(elem->state.layer_idx == layer_check); + assert(! elem->state.deleted); + assert(elem->state.to_model_gracious); + last_successfull_layer = elem; + if (elem->parents.size() != 1) + // Reached merge point. + break; + } + } + + // Could not find valid placement, even though it should exist => error handling + if (last_successfull_layer == nullptr) { + BOOST_LOG_TRIVIAL(warning) << "No valid placement found for to model gracious element on layer " << first_elem.state.layer_idx; + tree_supports_show_error("Could not fine valid placement on model! Just placing it down anyway. Could cause floating branches."sv, true); + first_elem.state.to_model_gracious = false; + set_to_model_contact_simple(first_elem); + } else { + // Found a gracious area above first_elem. Remove all below last_successfull_layer. + { + LayerIndex parent_layer_idx = first_elem.state.layer_idx; + for (SupportElement *elem = &first_elem; elem != last_successfull_layer; elem = &move_bounds[++ parent_layer_idx][elem->parents.front()]) { + assert(! elem->state.deleted); + elem->state.deleted = true; + } + } + // Guess a point inside the influence area, in which the branch will be placed in. + const Point best = move_inside_if_outside(last_successfull_layer->influence_area, last_successfull_layer->state.next_position); + last_successfull_layer->state.result_on_layer = best; + BOOST_LOG_TRIVIAL(debug) << "Added gracious Support On Model Point (" << best.x() << "," << best.y() << "). The current layer is " << last_successfull_layer; + } +} + +// Remove elements marked as "deleted", update indices to parents. +static void remove_deleted_elements(std::vector &move_bounds) +{ + std::vector map_parents; + std::vector map_current; + for (LayerIndex layer_idx = LayerIndex(move_bounds.size()) - 1; layer_idx >= 0; -- layer_idx) { + SupportElements &layer = move_bounds[layer_idx]; + map_current.clear(); + for (int32_t i = 0; i < int32_t(layer.size());) { + SupportElement &element = layer[i]; + if (element.state.deleted) { + if (map_current.empty()) { + // Initialize with identity map. + map_current.assign(layer.size(), 0); + std::iota(map_current.begin(), map_current.end(), 0); + } + // Delete all "deleted" elements from the end of the layer vector. + while (i < layer.size() && layer.back().state.deleted) { + layer.pop_back(); + // Mark as deleted in the map. + map_current[layer.size()] = -1; + } + assert(i == layer.size() || i + 1 < layer.size()); + if (i + 1 < layer.size()) { + element = std::move(layer.back()); + layer.pop_back(); + // Mark the current element as deleted. + map_current[i] = -1; + // Mark the moved element as moved to index i. + map_current[layer.size()] = i; + } + } else { + // Current element is not deleted. Update its parent indices. + if (! map_parents.empty()) + for (int32_t &parent_idx : element.parents) + parent_idx = map_parents[parent_idx]; + ++ i; + } + } + std::swap(map_current, map_parents); + } +} + +/*! + * \brief Set the result_on_layer point for all influence areas + * + * \param move_bounds[in,out] All currently existing influence areas + */ +void create_nodes_from_area( + const TreeModelVolumes &volumes, + const TreeSupportSettings &config, + std::vector &move_bounds, + std::function throw_on_cancel) +{ + // Initialize points on layer 0, with a "random" point in the influence area. + // Point is chosen based on an inaccurate estimate where the branches will split into two, but every point inside the influence area would produce a valid result. + { + SupportElements *layer_above = move_bounds.size() > 1 ? &move_bounds[1] : nullptr; + for (SupportElement &elem : *layer_above) + elem.state.marked = false; + for (SupportElement &init : move_bounds.front()) { + init.state.result_on_layer = move_inside_if_outside(init.influence_area, init.state.next_position); + // Also set the parent nodes, as these will be required for the first iteration of the loop below and mark the parent nodes. + set_points_on_areas(init, layer_above); + } + } + + throw_on_cancel(); + + for (LayerIndex layer_idx = 1; layer_idx < LayerIndex(move_bounds.size()); ++ layer_idx) { + auto &layer = move_bounds[layer_idx]; + auto *layer_above = layer_idx + 1 < move_bounds.size() ? &move_bounds[layer_idx + 1] : nullptr; + if (layer_above) + for (SupportElement &elem : *layer_above) + elem.state.marked = false; + for (SupportElement &elem : layer) { + assert(! elem.state.deleted); + assert(elem.state.layer_idx == layer_idx); + // check if the resulting center point is not yet set + if (! elem.state.result_on_layer_is_set()) { + if (elem.state.to_buildplate || (elem.state.distance_to_top < config.min_dtt_to_model && ! elem.state.supports_roof)) { + if (elem.state.to_buildplate) { + BOOST_LOG_TRIVIAL(error) << "Uninitialized Influence area targeting " << elem.state.target_position.x() << "," << elem.state.target_position.y() << ") " + "at target_height: " << elem.state.target_height << " layer: " << layer_idx; + tree_supports_show_error("Uninitialized support element! A branch could be missing or exist partially."sv, true); + } + // we dont need to remove yet the parents as they will have a lower dtt and also no result_on_layer set + elem.state.deleted = true; + } else { + // set the point where the branch will be placed on the model + if (elem.state.to_model_gracious) + set_to_model_contact_to_model_gracious(volumes, config, move_bounds, elem, throw_on_cancel); + else + set_to_model_contact_simple(elem); + } + } + if (! elem.state.deleted && ! elem.state.marked && elem.state.target_height == layer_idx) + // Just a tip surface with no supporting element. + elem.state.deleted = true; + if (elem.state.deleted) { + for (int32_t parent_idx : elem.parents) + // When the roof was not able to generate downwards enough, the top elements may have not moved, and have result_on_layer already set. + // As this branch needs to be removed => all parents result_on_layer have to be invalidated. + (*layer_above)[parent_idx].state.result_on_layer_reset(); + } + if (! elem.state.deleted) { + // Element is valid now setting points in the layer above and mark the parent nodes. + set_points_on_areas(elem, layer_above); + } + } + throw_on_cancel(); + } + +#ifndef NDEBUG + // Verify the tree connectivity including the branch slopes. + for (LayerIndex layer_idx = 0; layer_idx + 1 < LayerIndex(move_bounds.size()); ++ layer_idx) { + auto &layer = move_bounds[layer_idx]; + auto &above = move_bounds[layer_idx + 1]; + for (SupportElement &elem : layer) + if (! elem.state.deleted) { + for (int32_t iparent : elem.parents) { + SupportElement &parent = above[iparent]; + assert(! parent.state.deleted); + assert(elem.state.result_on_layer_is_set() == parent.state.result_on_layer_is_set()); + if (elem.state.result_on_layer_is_set()) { + double radius_increase = config.getRadius(elem.state) - config.getRadius(parent.state); + assert(radius_increase >= 0); + double shift = (elem.state.result_on_layer - parent.state.result_on_layer).cast().norm(); + //FIXME this assert fails a lot. Is it correct? + //assert(shift < radius_increase + 2. * config.maximum_move_distance_slow); + } + } + } + } +#endif // NDEBUG + + remove_deleted_elements(move_bounds); + +#ifndef NDEBUG + // Verify the tree connectivity including the branch slopes. + for (LayerIndex layer_idx = 0; layer_idx + 1 < LayerIndex(move_bounds.size()); ++ layer_idx) { + auto &layer = move_bounds[layer_idx]; + auto &above = move_bounds[layer_idx + 1]; + for (SupportElement &elem : layer) { + assert(! elem.state.deleted); + for (int32_t iparent : elem.parents) { + SupportElement &parent = above[iparent]; + assert(! parent.state.deleted); + assert(elem.state.result_on_layer_is_set() == parent.state.result_on_layer_is_set()); + if (elem.state.result_on_layer_is_set()) { + double radius_increase = config.getRadius(elem.state) - config.getRadius(parent.state); + assert(radius_increase >= 0); + double shift = (elem.state.result_on_layer - parent.state.result_on_layer).cast().norm(); + //FIXME this assert fails a lot. Is it correct? + //assert(shift < radius_increase + 2. * config.maximum_move_distance_slow); + } + } + } + } +#endif // NDEBUG +} + +// For producing circular / elliptical areas from SupportElements (one DrawArea per one SupportElement) +// and for smoothing those areas along the tree branches. +struct DrawArea +{ + // Element to be processed. + SupportElement *element; + // Element below, if there is such an element. nullptr if element is a root of a tree. + SupportElement *child_element; + // Polygons to be extruded for this element. + Polygons polygons; +}; + +/*! + * \brief Draws circles around result_on_layer points of the influence areas + * + * \param linear_data[in] All currently existing influence areas with the layer they are on + * \param layer_tree_polygons[out] Resulting branch areas with the layerindex they appear on. layer_tree_polygons.size() has to be at least linear_data.size() as each Influence area in linear_data will save have at least one (that's why it's a vector) corresponding branch area in layer_tree_polygons. + * \param inverse_tree_order[in] A mapping that returns the child of every influence area. + */ +static void generate_branch_areas( + const TreeModelVolumes &volumes, + const TreeSupportSettings &config, + const std::vector &move_bounds, + std::vector &linear_data, + std::function throw_on_cancel) +{ +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + double progress_total = TREE_PROGRESS_PRECALC_AVO + TREE_PROGRESS_PRECALC_COLL + TREE_PROGRESS_GENERATE_NODES + TREE_PROGRESS_AREA_CALC; + constexpr int progress_report_steps = 10; + const size_t progress_inserts_check_interval = linear_data.size() / progress_report_steps; + std::mutex critical_sections; +#endif // SLIC3R_TREESUPPORTS_PROGRESS + + // Pre-generate a circle with correct diameter so that we don't have to recompute those (co)sines every time. + const Polygon branch_circle = make_circle(config.branch_radius, SUPPORT_TREE_CIRCLE_RESOLUTION); + + tbb::parallel_for(tbb::blocked_range(0, linear_data.size()), + [&volumes, &config, &move_bounds, &linear_data, &branch_circle, &throw_on_cancel](const tbb::blocked_range &range) { + for (size_t idx = range.begin(); idx < range.end(); ++ idx) { + DrawArea &draw_area = linear_data[idx]; + const LayerIndex layer_idx = draw_area.element->state.layer_idx; + const coord_t radius = config.getRadius(*draw_area.element); + bool parent_uses_min = false; + + // Calculate multiple ovalized circles, to connect with every parent and child. Also generate regular circle for the current layer. Merge all these into one area. + std::vector> movement_directions{ std::pair(Point(0, 0), radius) }; + if (! draw_area.element->state.skip_ovalisation) { + if (draw_area.child_element != nullptr) { + const Point movement = draw_area.child_element->state.result_on_layer - draw_area.element->state.result_on_layer; + movement_directions.emplace_back(movement, radius); + } + const SupportElements *layer_above = layer_idx + 1 < move_bounds.size() ? &move_bounds[layer_idx + 1] : nullptr; + for (int32_t parent_idx : draw_area.element->parents) { + const SupportElement &parent = (*layer_above)[parent_idx]; + const Point movement = parent.state.result_on_layer - draw_area.element->state.result_on_layer; + //FIXME why max(..., config.support_line_width)? + movement_directions.emplace_back(movement, std::max(config.getRadius(parent), config.support_line_width)); + parent_uses_min |= parent.state.use_min_xy_dist; + } + } + + const Polygons &collision = volumes.getCollision(0, layer_idx, parent_uses_min || draw_area.element->state.use_min_xy_dist); + auto generateArea = [&collision, &draw_area, &branch_circle, branch_radius = config.branch_radius, support_line_width = config.support_line_width, &movement_directions] + (coord_t aoffset, double &max_speed) { + Polygons poly; + max_speed = 0; + for (std::pair movement : movement_directions) { + max_speed = std::max(max_speed, movement.first.cast().norm()); + + // Visualization: https://jsfiddle.net/0zvcq39L/2/ + // Ovalizes the circle to an ellipse, that contains both old center and new target position. + double used_scale = (movement.second + aoffset) / (1.0 * branch_radius); + Point center_position = draw_area.element->state.result_on_layer + movement.first / 2; + const double moveX = movement.first.x() / (used_scale * branch_radius); + const double moveY = movement.first.y() / (used_scale * branch_radius); + const double vsize_inv = 0.5 / (0.01 + std::sqrt(moveX * moveX + moveY * moveY)); + + double matrix[] = { + used_scale * (1 + moveX * moveX * vsize_inv), + used_scale * (0 + moveX * moveY * vsize_inv), + used_scale * (0 + moveX * moveY * vsize_inv), + used_scale * (1 + moveY * moveY * vsize_inv), + }; + Polygon circle; + for (Point vertex : branch_circle) + circle.points.emplace_back(center_position + Point(matrix[0] * vertex.x() + matrix[1] * vertex.y(), matrix[2] * vertex.x() + matrix[3] * vertex.y())); + poly.emplace_back(std::move(circle)); + } + + // There seem to be some rounding errors, causing a branch to be a tiny bit further away from the model that it has to be. + // This can cause the tip to be slightly further away front the overhang (x/y wise) than optimal. This fixes it, and for every other part, 0.05mm will not be noticed. + poly = diff_clipped(offset(union_(poly), std::min(coord_t(50), support_line_width / 4), jtMiter, 1.2), collision); + return poly; + }; + + // Ensure branch area will not overlap with model/collision. This can happen because of e.g. ovalization or increase_until_radius. + double max_speed; + Polygons polygons = generateArea(0, max_speed); + const bool fast_relative_movement = max_speed > radius * 0.75; + + if (fast_relative_movement || config.getRadius(*draw_area.element) - config.getCollisionRadius(draw_area.element->state) > config.support_line_width) { + // Simulate the path the nozzle will take on the outermost wall. + // If multiple parts exist, the outer line will not go all around the support part potentially causing support material to be printed mid air. + ExPolygons nozzle_path = offset_ex(polygons, - config.support_line_width / 2); + if (nozzle_path.size() > 1) { + // Just try to make the area a tiny bit larger. + polygons = generateArea(config.support_line_width / 2, max_speed); + nozzle_path = offset_ex(polygons, -config.support_line_width / 2); + // If larger area did not fix the problem, all parts off the nozzle path that do not contain the center point are removed, hoping for the best. + if (nozzle_path.size() > 1) { + ExPolygons polygons_with_correct_center; + for (ExPolygon &part : nozzle_path) { + bool drop = false; + if (! part.contains(draw_area.element->state.result_on_layer)) { + // try a fuzzy inside as sometimes the point should be on the border, but is not because of rounding errors... + Point pt = draw_area.element->state.result_on_layer; + move_inside(to_polygons(part), pt, 0); + drop = (draw_area.element->state.result_on_layer - pt).cast().norm() >= scaled(0.025); + } + if (! drop) + polygons_with_correct_center.emplace_back(std::move(part)); + } + // Increase the area again, to ensure the nozzle path when calculated later is very similar to the one assumed above. + assert(contains(polygons, draw_area.element->state.result_on_layer)); + polygons = diff_clipped(offset(polygons_with_correct_center, config.support_line_width / 2, jtMiter, 1.2), + //FIXME Vojtech: Clipping may split the region into multiple pieces again, reversing the fixing effort. + collision); + } + } + } + + draw_area.polygons = std::move(polygons); + +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + if (idx % progress_inserts_check_interval == 0) { + std::lock_guard critical_section_progress(critical_sections); + progress_total += TREE_PROGRESS_GENERATE_BRANCH_AREAS / progress_report_steps; + Progress::messageProgress(Progress::Stage::SUPPORT, progress_total * m_progress_multiplier + m_progress_offset, TREE_PROGRESS_TOTAL); + } +#endif + throw_on_cancel(); + } + }); +} + +/*! + * \brief Applies some smoothing to the outer wall, intended to smooth out sudden jumps as they can happen when a branch moves though a hole. + * + * \param layer_tree_polygons[in,out] Resulting branch areas with the layerindex they appear on. + */ +static void smooth_branch_areas( + const TreeSupportSettings &config, + std::vector &move_bounds, + std::vector &linear_data, + const std::vector &linear_data_layers, + std::function throw_on_cancel) +{ +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + double progress_total = TREE_PROGRESS_PRECALC_AVO + TREE_PROGRESS_PRECALC_COLL + TREE_PROGRESS_GENERATE_NODES + TREE_PROGRESS_AREA_CALC + TREE_PROGRESS_GENERATE_BRANCH_AREAS; +#endif // SLIC3R_TREESUPPORTS_PROGRESS + + const coord_t max_radius_change_per_layer = 1 + config.support_line_width / 2; // this is the upper limit a radius may change per layer. +1 to avoid rounding errors + + // smooth upwards + for (LayerIndex layer_idx = 0; layer_idx < LayerIndex(move_bounds.size()) - 1; ++ layer_idx) { + const size_t processing_base = linear_data_layers[layer_idx]; + const size_t processing_base_above = linear_data_layers[layer_idx + 1]; + const SupportElements &layer_above = move_bounds[layer_idx + 1]; + tbb::parallel_for(tbb::blocked_range(0, processing_base_above - processing_base), + [&](const tbb::blocked_range &range) { + for (size_t processing_idx = range.begin(); processing_idx < range.end(); ++ processing_idx) { + DrawArea &draw_area = linear_data[processing_base + processing_idx]; + assert(draw_area.element->state.layer_idx == layer_idx); + double max_outer_wall_distance = 0; + bool do_something = false; + for (int32_t parent_idx : draw_area.element->parents) { + const SupportElement &parent = layer_above[parent_idx]; + assert(parent.state.layer_idx == layer_idx + 1); + if (config.getRadius(parent.state) != config.getCollisionRadius(parent.state)) { + do_something = true; + max_outer_wall_distance = std::max(max_outer_wall_distance, (draw_area.element->state.result_on_layer - parent.state.result_on_layer).cast().norm() - (config.getRadius(*draw_area.element) - config.getRadius(parent))); + } + } + max_outer_wall_distance += max_radius_change_per_layer; // As this change is a bit larger than what usually appears, lost radius can be slowly reclaimed over the layers. + if (do_something) { + assert(contains(draw_area.polygons, draw_area.element->state.result_on_layer)); + Polygons max_allowed_area = offset(draw_area.polygons, float(max_outer_wall_distance), jtMiter, 1.2); + for (int32_t parent_idx : draw_area.element->parents) { + const SupportElement &parent = layer_above[parent_idx]; +#ifndef NDEBUG + assert(parent.state.layer_idx == layer_idx + 1); + assert(contains(linear_data[processing_base_above + parent_idx].polygons, parent.state.result_on_layer)); + double radius_increase = config.getRadius(draw_area.element->state) - config.getRadius(parent.state); + assert(radius_increase >= 0); + double shift = (draw_area.element->state.result_on_layer - parent.state.result_on_layer).cast().norm(); + assert(shift < radius_increase + 2. * config.maximum_move_distance_slow); +#endif // NDEBUG + if (config.getRadius(parent.state) != config.getCollisionRadius(parent.state)) { + // No other element on this layer than the current one may be connected to &parent, + // thus it is safe to update parent's DrawArea directly. + Polygons &dst = linear_data[processing_base_above + parent_idx].polygons; +// Polygons orig = dst; + if (! dst.empty()) { + dst = intersection(dst, max_allowed_area); +#if 0 + if (dst.empty()) { + static int irun = 0; + SVG::export_expolygons(debug_out_path("treesupport-extrude_areas-smooth-error-%d.svg", irun ++), + { { { union_ex(max_allowed_area) }, { "max_allowed_area", "yellow", 0.5f } }, + { { union_ex(orig) }, { "orig", "red", "black", "", scaled(0.1f), 0.5f } } }); + ::MessageBoxA(nullptr, "TreeSupport smoothing bug", "Bug detected!", MB_OK | MB_SYSTEMMODAL | MB_SETFOREGROUND | MB_ICONWARNING); + } +#endif + } + } + } + } + throw_on_cancel(); + } + }); + } + +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + progress_total += TREE_PROGRESS_SMOOTH_BRANCH_AREAS / 2; + Progress::messageProgress(Progress::Stage::SUPPORT, progress_total * m_progress_multiplier + m_progress_offset, TREE_PROGRESS_TOTAL); // It is just assumed that both smoothing loops together are one third of the time spent in this function. This was guessed. As the whole function is only 10%, and the smoothing is hard to predict a progress report in the loop may be not useful. +#endif + + // smooth downwards + for (auto& element : move_bounds.back()) + element.state.marked = false; + for (int layer_idx = int(move_bounds.size()) - 2; layer_idx >= 0; -- layer_idx) { + const size_t processing_base = linear_data_layers[layer_idx]; + const size_t processing_base_above = linear_data_layers[layer_idx + 1]; + const SupportElements &layer_above = move_bounds[layer_idx + 1]; + tbb::parallel_for(tbb::blocked_range(0, processing_base_above - processing_base), + [&](const tbb::blocked_range &range) { + for (size_t processing_idx = range.begin(); processing_idx < range.end(); ++ processing_idx) { + DrawArea &draw_area = linear_data[processing_base + processing_idx]; + bool do_something = false; + Polygons max_allowed_area; + for (int32_t parent_idx : draw_area.element->parents) { + const SupportElement &parent = layer_above[parent_idx]; + coord_t max_outer_line_increase = max_radius_change_per_layer; + Polygons result = offset(linear_data[processing_base_above + parent_idx].polygons, max_outer_line_increase, jtMiter, 1.2); + Point direction = draw_area.element->state.result_on_layer - parent.state.result_on_layer; + // move the polygons object + for (auto &outer : result) + for (Point& p : outer) + p += direction; + append(max_allowed_area, std::move(result)); + do_something = do_something || parent.state.marked || config.getCollisionRadius(parent.state) != config.getRadius(parent.state); + } + if (do_something) { + // Trim the current drawing areas with max_allowed_area. + Polygons result = intersection(max_allowed_area, draw_area.polygons); + if (area(result) < area(draw_area.polygons)) { + // Mark parent as modified to propagate down. + draw_area.element->state.marked = true; + draw_area.polygons = std::move(result); + } + } + throw_on_cancel(); + } + }); + } + +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + progress_total += TREE_PROGRESS_SMOOTH_BRANCH_AREAS / 2; + Progress::messageProgress(Progress::Stage::SUPPORT, progress_total * m_progress_multiplier + m_progress_offset, TREE_PROGRESS_TOTAL); +#endif +} + +/*! + * \brief Drop down areas that do rest non-gracefully on the model to ensure the branch actually rests on something. + * + * \param layer_tree_polygons[in] Resulting branch areas with the layerindex they appear on. + * \param linear_data[in] All currently existing influence areas with the layer they are on + * \param dropped_down_areas[out] Areas that have to be added to support all non-graceful areas. + * \param inverse_tree_order[in] A mapping that returns the child of every influence area. + */ +static void drop_non_gracious_areas( + const TreeModelVolumes &volumes, + const std::vector &linear_data, + std::vector &support_layer_storage, + std::function throw_on_cancel) +{ + std::vector>> dropped_down_areas(linear_data.size()); + tbb::parallel_for(tbb::blocked_range(0, linear_data.size()), + [&](const tbb::blocked_range &range) { + for (size_t idx = range.begin(); idx < range.end(); ++ idx) { + // If a element has no child, it connects to whatever is below as no support further down for it will exist. + if (const DrawArea &draw_element = linear_data[idx]; ! draw_element.element->state.to_model_gracious && draw_element.child_element == nullptr) { + Polygons rest_support; + const LayerIndex layer_idx_first = draw_element.element->state.layer_idx - 1; + for (LayerIndex layer_idx = layer_idx_first; area(rest_support) > tiny_area_threshold && layer_idx >= 0; -- layer_idx) { + rest_support = diff_clipped(layer_idx == layer_idx_first ? draw_element.polygons : rest_support, volumes.getCollision(0, layer_idx, false)); + dropped_down_areas[idx].emplace_back(layer_idx, rest_support); + } + } + throw_on_cancel(); + } + }); + + for (coord_t i = 0; i < static_cast(dropped_down_areas.size()); i++) + for (std::pair &pair : dropped_down_areas[i]) + append(support_layer_storage[pair.first], std::move(pair.second)); +} + +/*! + * \brief Generates Support Floor, ensures Support Roof can not cut of branches, and saves the branches as support to storage + * + * \param support_layer_storage[in] Areas where support should be generated. + * \param support_roof_storage[in] Areas where support was replaced with roof. + * \param storage[in,out] The storage where the support should be stored. + */ +static void finalize_interface_and_support_areas( + const PrintObject &print_object, + const TreeModelVolumes &volumes, + const TreeSupportSettings &config, + const std::vector &overhangs, + std::vector &support_layer_storage, + std::vector &support_roof_storage, + + SupportGeneratorLayersPtr &bottom_contacts, + SupportGeneratorLayersPtr &top_contacts, + SupportGeneratorLayersPtr &intermediate_layers, + SupportGeneratorLayerStorage &layer_storage, + + std::function throw_on_cancel) +{ + InterfacePreference interface_pref = config.interface_preference; // InterfacePreference::SupportLinesOverwriteInterface; + +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + double progress_total = TREE_PROGRESS_PRECALC_AVO + TREE_PROGRESS_PRECALC_COLL + TREE_PROGRESS_GENERATE_NODES + TREE_PROGRESS_AREA_CALC + TREE_PROGRESS_GENERATE_BRANCH_AREAS + TREE_PROGRESS_SMOOTH_BRANCH_AREAS; +#endif // SLIC3R_TREESUPPORTS_PROGRESS + + // Iterate over the generated circles in parallel and clean them up. Also add support floor. + tbb::spin_mutex layer_storage_mutex; + tbb::parallel_for(tbb::blocked_range(0, support_layer_storage.size()), + [&](const tbb::blocked_range &range) { + for (size_t layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx) { + // Most of the time in this function is this union call. Can take 300+ ms when a lot of areas are to be unioned. + support_layer_storage[layer_idx] = smooth_outward(union_(support_layer_storage[layer_idx]), config.support_line_width); //FIXME was .smooth(50); + //smooth_outward(closing(std::move(bottom), closing_distance + minimum_island_radius, closing_distance, SUPPORT_SURFACES_OFFSET_PARAMETERS), smoothing_distance) : + + // simplify a bit, to ensure the output does not contain outrageous amounts of vertices. Should not be necessary, just a precaution. + support_layer_storage[layer_idx] = polygons_simplify(support_layer_storage[layer_idx], std::min(scaled(0.03), double(config.resolution))); + // Subtract support lines of the branches from the roof + SupportGeneratorLayer*& support_roof = top_contacts[layer_idx]; + if (! support_roof_storage[layer_idx].empty() || support_roof != nullptr) { + if (support_roof == nullptr) { + support_roof = &layer_allocate(layer_storage, layer_storage_mutex, SupporLayerType::sltTopContact, print_object.slicing_parameters(), layer_idx); + support_roof->polygons = union_(support_roof_storage[layer_idx]); + } else + support_roof->polygons = union_(support_roof->polygons, support_roof_storage[layer_idx]); + + if (! support_roof->polygons.empty() && + area(intersection(support_layer_storage[layer_idx], support_roof->polygons)) > tiny_area_threshold) { + switch (interface_pref) { + case InterfacePreference::InterfaceAreaOverwritesSupport: + support_layer_storage[layer_idx] = diff(support_layer_storage[layer_idx], support_roof->polygons); + break; + case InterfacePreference::SupportAreaOverwritesInterface: + support_roof->polygons = diff(support_roof->polygons, support_layer_storage[layer_idx]); + break; + //FIXME + #if 1 + case InterfacePreference::InterfaceLinesOverwriteSupport: + case InterfacePreference::SupportLinesOverwriteInterface: + assert(false); + [[fallthrough]]; + #else + case InterfacePreference::InterfaceLinesOverwriteSupport: + { + // Hatch the support roof interfaces, offset them by their line width and subtract them from support base. + Polygons interface_lines = offset(to_polylines( + generate_support_infill_lines(support_roof->polygons, true, layer_idx, config.support_roof_line_distance)), + config.support_roof_line_width / 2); + support_layer_storage[layer_idx] = diff(support_layer_storage[layer_idx], interface_lines); + break; + } + case InterfacePreference::SupportLinesOverwriteInterface: + { + // Hatch the support roof interfaces, offset them by their line width and subtract them from support base. + Polygons tree_lines = union_(offset(to_polylines( + generate_support_infill_lines(support_layer_storage[layer_idx], false, layer_idx, config.support_line_distance, true)), + config.support_line_width / 2)); + // do not draw roof where the tree is. I prefer it this way as otherwise the roof may cut of a branch from its support below. + support_roof->polygons = diff(support_roof->polygons, tree_lines); + break; + } + #endif + case InterfacePreference::Nothing: + break; + } + } + } + + // Subtract support floors from the support area and add them to the support floor instead. + if (config.support_bottom_layers > 0 && !support_layer_storage[layer_idx].empty()) { + SupportGeneratorLayer*& support_bottom = bottom_contacts[layer_idx]; + Polygons layer_outset = diff_clipped( + config.support_bottom_offset > 0 ? offset(support_layer_storage[layer_idx], config.support_bottom_offset, jtMiter, 1.2) : support_layer_storage[layer_idx], + volumes.getCollision(0, layer_idx, false)); + Polygons floor_layer; + size_t layers_below = 0; + while (layers_below <= config.support_bottom_layers) { + // one sample at 0 layers below, another at config.support_bottom_layers. In-between samples at config.performance_interface_skip_layers distance from each other. + const size_t sample_layer = static_cast(std::max(0, (static_cast(layer_idx) - static_cast(layers_below)) - static_cast(config.z_distance_bottom_layers))); + //FIXME subtract the wipe tower + append(floor_layer, intersection(layer_outset, overhangs[sample_layer])); + if (layers_below < config.support_bottom_layers) + layers_below = std::min(layers_below + config.performance_interface_skip_layers, config.support_bottom_layers); + else + break; + } + if (! floor_layer.empty()) { + if (support_bottom == nullptr) + support_bottom = &layer_allocate(layer_storage, layer_storage_mutex, SupporLayerType::sltBottomContact, print_object.slicing_parameters(), layer_idx); + support_bottom->polygons = union_(floor_layer, support_bottom->polygons); + support_layer_storage[layer_idx] = diff_clipped(support_layer_storage[layer_idx], offset(support_bottom->polygons, scaled(0.01), jtMiter, 1.2)); // Subtract the support floor from the normal support. + } + } + + if (! support_layer_storage[layer_idx].empty()) { + SupportGeneratorLayer *&l = intermediate_layers[layer_idx]; + if (l == nullptr) + l = &layer_allocate(layer_storage, layer_storage_mutex, SupporLayerType::sltBase, print_object.slicing_parameters(), layer_idx); + append(l->polygons, union_(support_layer_storage[layer_idx])); + } + +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + { + std::lock_guard critical_section_progress(critical_sections); + progress_total += TREE_PROGRESS_FINALIZE_BRANCH_AREAS / support_layer_storage.size(); + Progress::messageProgress(Progress::Stage::SUPPORT, progress_total * m_progress_multiplier + m_progress_offset, TREE_PROGRESS_TOTAL); + } +#endif +#if 0 + { + std::lock_guard lock(critical_sections); + if (!storage.support.supportLayers[layer_idx].support_infill_parts.empty() || !storage.support.supportLayers[layer_idx].support_roof.empty()) + storage.support.layer_nr_max_filled_layer = std::max(storage.support.layer_nr_max_filled_layer, static_cast(layer_idx)); + } +#endif + throw_on_cancel(); + } + }); +} + +/*! + * \brief Draws circles around result_on_layer points of the influence areas and applies some post processing. + * + * \param move_bounds[in] All currently existing influence areas + * \param storage[in,out] The storage where the support should be stored. + */ +static void draw_areas( + PrintObject &print_object, + const TreeModelVolumes &volumes, + const TreeSupportSettings &config, + const std::vector &overhangs, + std::vector &move_bounds, + + SupportGeneratorLayersPtr &bottom_contacts, + SupportGeneratorLayersPtr &top_contacts, + SupportGeneratorLayersPtr &intermediate_layers, + SupportGeneratorLayerStorage &layer_storage, + std::function throw_on_cancel) +{ + std::vector support_layer_storage(move_bounds.size()); + std::vector support_roof_storage(move_bounds.size()); + // All SupportElements are put into a layer independent storage to improve parallelization. + std::vector linear_data; + std::vector linear_data_layers; + { + std::vector> map_downwards_old; + std::vector> map_downwards_new; + for (LayerIndex layer_idx = 0; layer_idx < LayerIndex(move_bounds.size()); ++ layer_idx) { + SupportElements *layer_above = layer_idx + 1 < move_bounds.size() ? &move_bounds[layer_idx + 1] : nullptr; + map_downwards_new.clear(); + linear_data_layers.emplace_back(linear_data.size()); + std::sort(map_downwards_old.begin(), map_downwards_old.end(), [](auto &l, auto &r) { return l.first < r.first; }); + for (SupportElement &elem : move_bounds[layer_idx]) { + SupportElement *child = nullptr; + if (layer_idx > 0) { + auto it = std::lower_bound(map_downwards_old.begin(), map_downwards_old.end(), &elem, [](auto &l, const SupportElement *r) { return l.first < r; }); + if (it != map_downwards_old.end() && it->first == &elem) { + child = it->second; + // Only one link points to a node above from below. + assert(! (++ it != map_downwards_old.end() && it->first == &elem)); + } + assert(child ? child->state.result_on_layer_is_set() : elem.state.target_height > layer_idx); + } + for (int32_t parent_idx : elem.parents) { + SupportElement &parent = (*layer_above)[parent_idx]; + if (parent.state.result_on_layer_is_set()) + map_downwards_new.emplace_back(&parent, &elem); + } + linear_data.push_back({ &elem, child }); + } + std::swap(map_downwards_old, map_downwards_new); + } + linear_data_layers.emplace_back(linear_data.size()); + } + + throw_on_cancel(); + +#ifndef NDEBUG + for (size_t i = 0; i < move_bounds.size(); ++ i) { + size_t begin = linear_data_layers[i]; + size_t end = linear_data_layers[i + 1]; + for (size_t j = begin; j < end; ++ j) + assert(linear_data[j].element == &move_bounds[i][j - begin]); + } +#endif // NDEBUG + + auto t_start = std::chrono::high_resolution_clock::now(); + // Generate the circles that will be the branches. + generate_branch_areas(volumes, config, move_bounds, linear_data, throw_on_cancel); + +#if 0 + assert(linear_data_layers.size() == move_bounds.size() + 1); + for (const auto &draw_area : linear_data) + assert(contains(draw_area.polygons, draw_area.element->state.result_on_layer)); + for (size_t i = 0; i < move_bounds.size(); ++ i) { + size_t begin = linear_data_layers[i]; + size_t end = linear_data_layers[i + 1]; + for (size_t j = begin; j < end; ++ j) { + const auto &draw_area = linear_data[j]; + assert(draw_area.element == &move_bounds[i][j - begin]); + assert(contains(draw_area.polygons, draw_area.element->state.result_on_layer)); + } + } +#endif + +#if 0 + for (size_t area_layer_idx = 0; area_layer_idx + 1 < linear_data_layers.size(); ++ area_layer_idx) { + size_t begin = linear_data_layers[area_layer_idx]; + size_t end = linear_data_layers[area_layer_idx + 1]; + Polygons polygons; + for (size_t area_idx = begin; area_idx < end; ++ area_idx) { + DrawArea &area = linear_data[area_idx]; + append(polygons, area.polygons); + } + SVG::export_expolygons(debug_out_path("treesupport-extrude_areas-raw-%d.svg", area_layer_idx), + { { { union_ex(polygons) }, { "parent", "red", "black", "", scaled(0.1f), 0.5f } } }); + } +#endif + + auto t_generate = std::chrono::high_resolution_clock::now(); + // In some edgecases a branch may go though a hole, where the regular radius does not fit. This can result in an apparent jump in branch radius. As such this cases need to be caught and smoothed out. + smooth_branch_areas(config, move_bounds, linear_data, linear_data_layers, throw_on_cancel); + +#if 0 + for (size_t area_layer_idx = 0; area_layer_idx + 1 < linear_data_layers.size(); ++area_layer_idx) { + size_t begin = linear_data_layers[area_layer_idx]; + size_t end = linear_data_layers[area_layer_idx + 1]; + Polygons polygons; + for (size_t area_idx = begin; area_idx < end; ++area_idx) { + DrawArea& area = linear_data[area_idx]; + append(polygons, area.polygons); + } + SVG::export_expolygons(debug_out_path("treesupport-extrude_areas-smooth-%d.svg", area_layer_idx), + { { { union_ex(polygons) }, { "parent", "red", "black", "", scaled(0.1f), 0.5f } } }); + } +#endif + + auto t_smooth = std::chrono::high_resolution_clock::now(); + // drop down all trees that connect non gracefully with the model + drop_non_gracious_areas(volumes, linear_data, support_layer_storage, throw_on_cancel); + auto t_drop = std::chrono::high_resolution_clock::now(); + + // Single threaded combining all support areas to the right layers. + { + auto begin = linear_data.begin(); + for (LayerIndex layer_idx = 0; layer_idx < LayerIndex(move_bounds.size()); ++ layer_idx) { + size_t cnt_roofs = 0; + size_t cnt_layers = 0; + auto end = begin; + for (; end != linear_data.end() && end->element->state.layer_idx == layer_idx; ++ end) + ++ (end->element->state.missing_roof_layers > end->element->state.distance_to_top ? cnt_roofs : cnt_layers); + auto &this_roofs = support_roof_storage[layer_idx]; + auto &this_layers = support_layer_storage[layer_idx]; + this_roofs.reserve(this_roofs.size() + cnt_roofs); + this_layers.reserve(this_layers.size() + cnt_layers); + for (auto it = begin; it != end; ++ it) + std::move(std::begin(it->polygons), std::end(it->polygons), std::back_inserter(it->element->state.missing_roof_layers > it->element->state.distance_to_top ? this_roofs : this_layers)); + begin = end; + } + } + + finalize_interface_and_support_areas(print_object, volumes, config, overhangs, support_layer_storage, support_roof_storage, + bottom_contacts, top_contacts, intermediate_layers, layer_storage, throw_on_cancel); + auto t_end = std::chrono::high_resolution_clock::now(); + + auto dur_gen_tips = 0.001 * std::chrono::duration_cast(t_generate - t_start).count(); + auto dur_smooth = 0.001 * std::chrono::duration_cast(t_smooth - t_generate).count(); + auto dur_drop = 0.001 * std::chrono::duration_cast(t_drop - t_smooth).count(); + auto dur_finalize = 0.001 * std::chrono::duration_cast(t_end - t_drop).count(); + + BOOST_LOG_TRIVIAL(info) << + "Time used for drawing subfuctions: generate_branch_areas: " << dur_gen_tips << " ms " + "smooth_branch_areas: " << dur_smooth << " ms " + "drop_non_gracious_areas: " << dur_drop << " ms " + "finalize_interface_and_support_areas " << dur_finalize << " ms"; +} + +#if 1 +// Test whether two circles, each on its own plane in 3D intersect. +// Circles are considered intersecting, if the lowest point on one circle is below the other circle's plane. +// Assumption: The two planes are oriented the same way. +static bool circles_intersect( + const Vec3d &p1, const Vec3d &n1, const double r1, + const Vec3d &p2, const Vec3d &n2, const double r2) +{ + assert(n1.dot(n2) >= 0); + + const Vec3d z = n1.cross(n2); + const Vec3d dir1 = z.cross(n1); + const Vec3d lowest_point1 = p1 + dir1 * (r1 / dir1.norm()); + assert(n2.dot(p1) >= n2.dot(lowest_point1)); + if (n2.dot(lowest_point1) <= 0) + return true; + const Vec3d dir2 = z.cross(n2); + const Vec3d lowest_point2 = p2 + dir2 * (r2 / dir2.norm()); + assert(n1.dot(p2) >= n1.dot(lowest_point2)); + return n1.dot(lowest_point2) <= 0; +} + +template +void triangulate_fan(indexed_triangle_set &its, int ifan, int ibegin, int iend) +{ + // at least 3 vertices, increasing order. + assert(ibegin + 3 <= iend); + assert(ibegin >= 0 && iend <= its.vertices.size()); + assert(ifan >= 0 && ifan < its.vertices.size()); + int num_faces = iend - ibegin; + its.indices.reserve(its.indices.size() + num_faces * 3); + for (int v = ibegin, u = iend - 1; v < iend; u = v ++) { + if (flip_normals) + its.indices.push_back({ ifan, u, v }); + else + its.indices.push_back({ ifan, v, u }); + } +} + +static void triangulate_strip(indexed_triangle_set &its, int ibegin1, int iend1, int ibegin2, int iend2) +{ + // at least 3 vertices, increasing order. + assert(ibegin1 + 3 <= iend1); + assert(ibegin1 >= 0 && iend1 <= its.vertices.size()); + assert(ibegin2 + 3 <= iend2); + assert(ibegin2 >= 0 && iend2 <= its.vertices.size()); + int n1 = iend1 - ibegin1; + int n2 = iend2 - ibegin2; + its.indices.reserve(its.indices.size() + (n1 + n2) * 3); + + // For the first vertex of 1st strip, find the closest vertex on the 2nd strip. + int istart2 = ibegin2; + { + const Vec3f &p1 = its.vertices[ibegin1]; + auto d2min = std::numeric_limits::max(); + for (int i = ibegin2; i < iend2; ++ i) { + const Vec3f &p2 = its.vertices[i]; + const float d2 = (p2 - p1).squaredNorm(); + if (d2 < d2min) { + d2min = d2; + istart2 = i; + } + } + } + + // Now triangulate the strip zig-zag fashion taking always the shortest connection if possible. + for (int u = ibegin1, v = istart2; n1 > 0 || n2 > 0;) { + bool take_first; + int u2, v2; + auto update_u2 = [&u2, u, ibegin1, iend1]() { + u2 = u; + if (++ u2 == iend1) + u2 = ibegin1; + }; + auto update_v2 = [&v2, v, ibegin2, iend2]() { + v2 = v; + if (++ v2 == iend2) + v2 = ibegin2; + }; + if (n1 == 0) { + take_first = false; + update_v2(); + } else if (n2 == 0) { + take_first = true; + update_u2(); + } else { + update_u2(); + update_v2(); + float l1 = (its.vertices[u2] - its.vertices[v]).squaredNorm(); + float l2 = (its.vertices[v2] - its.vertices[u]).squaredNorm(); + take_first = l1 < l2; + } + if (take_first) { + its.indices.push_back({ u, u2, v }); + -- n1; + u = u2; + } else { + its.indices.push_back({ u, v2, v }); + -- n2; + v = v2; + } + } +} + +// Discretize 3D circle, append to output vector, return ranges of indices of the points added. +static std::pair discretize_circle(const Vec3f ¢er, const Vec3f &normal, const float radius, const float eps, std::vector &pts) +{ + // Calculate discretization step and number of steps. + float angle_step = 2. * acos(1. - eps / radius); + auto nsteps = int(ceil(2 * M_PI / angle_step)); + angle_step = 2 * M_PI / nsteps; + + // Prepare coordinate system for the circle plane. + Vec3f x = normal.cross(Vec3f(0.f, -1.f, 0.f)).normalized(); + Vec3f y = normal.cross(x).normalized(); + assert(std::abs(x.cross(y).dot(normal) - 1.f) < EPSILON); + + // Discretize the circle. + int begin = int(pts.size()); + pts.reserve(pts.size() + nsteps); + float angle = 0; + x *= radius; + y *= radius; + for (int i = 0; i < nsteps; ++ i) { + pts.emplace_back(center + x * cos(angle) + y * sin(angle)); + angle += angle_step; + } + return { begin, int(pts.size()) }; +} + +// Discretize polygon, append to output vector, return ranges of indices of the points added. +static std::pair discretize_polygon(const Vec3f& center, const Polygons& polys, std::vector& pts) +{ + const Polygon& poly = polys.front(); + size_t nsteps = poly.size(); + // Discretize the circle. + int begin = int(pts.size()); + pts.reserve(pts.size() + nsteps); + for (int i = 0; i < nsteps; ++i) { + Vec3f pt(poly.points[i].x(), poly.points[i].y(), center.z()); + pts.emplace_back(pt); + } + return { begin, int(pts.size()) }; +} + +static void extrude_branch( + const std::vector &path, + const TreeSupportSettings &config, + const SlicingParameters &slicing_params, + const std::vector &move_bounds, + indexed_triangle_set &result) +{ + Vec3d p1, p2, p3; + Vec3d v1, v2; + Vec3d nprev; + Vec3d ncurrent; + assert(path.size() >= 2); + static constexpr const float eps = 0.015f; + std::pair prev_strip; + +// char fname[2048]; +// static int irun = 0; + + for (size_t ipath = 1; ipath < path.size(); ++ ipath) { + const SupportElement &prev = *path[ipath - 1]; + const SupportElement ¤t = *path[ipath]; + assert(prev.state.layer_idx + 1 == current.state.layer_idx); + p1 = to_3d(unscaled(prev .state.result_on_layer), layer_z(slicing_params, prev .state.layer_idx)); + p2 = to_3d(unscaled(current.state.result_on_layer), layer_z(slicing_params, current.state.layer_idx)); + v1 = (p2 - p1).normalized(); + if (ipath == 1) { + nprev = v1; + // Extrude the bottom half sphere. + float radius = unscaled(config.getRadius(prev.state)); + float angle_step = 2. * acos(1. - eps / radius); + auto nsteps = int(ceil(M_PI / (2. * angle_step))); + angle_step = M_PI / (2. * nsteps); + int ifan = int(result.vertices.size()); + result.vertices.emplace_back((p1 - nprev * radius).cast()); + float angle = angle_step; + std::pair strip; + if (current.state.type == TreeSupport::NodeType::ePolygon) { + strip = discretize_polygon(p1.cast(), current.influence_area, result.vertices); + prev_strip = strip; + strip = discretize_polygon(p2.cast(), current.influence_area, result.vertices); + } + else { + for (int i = 1; i < nsteps; ++i, angle += angle_step) { + if (current.state.type != TreeSupport::NodeType::ePolygon) { + strip = discretize_circle((p1 - nprev * radius * cos(angle)).cast(), nprev.cast(), radius * sin(angle), eps, result.vertices); + } + if (i == 1) + triangulate_fan(result, ifan, strip.first, strip.second); + else + triangulate_strip(result, prev_strip.first, prev_strip.second, strip.first, strip.second); + // sprintf(fname, "d:\\temp\\meshes\\tree-partial-%d.obj", ++ irun); + // its_write_obj(result, fname); + prev_strip = strip; + } + } + } + if (ipath + 1 == path.size()) { + // End of the tube. + ncurrent = v1; + // Extrude the top half sphere. + float radius = unscaled(config.getRadius(current.state)); + float angle_step = 2. * acos(1. - eps / radius); + auto nsteps = int(ceil(M_PI / (2. * angle_step))); + angle_step = M_PI / (2. * nsteps); + auto angle = float(M_PI / 2.); + std::pair strip; + if (current.state.type == TreeSupport::NodeType::ePolygon) { + strip = discretize_polygon(p2.cast(), current.influence_area, result.vertices); + } + else { + for (int i = 0; i < nsteps; ++i, angle -= angle_step) { + strip = discretize_circle((p2 + ncurrent * radius * cos(angle)).cast(), ncurrent.cast(), radius * sin(angle), eps, result.vertices); + triangulate_strip(result, prev_strip.first, prev_strip.second, strip.first, strip.second); + // sprintf(fname, "d:\\temp\\meshes\\tree-partial-%d.obj", ++ irun); + // its_write_obj(result, fname); + prev_strip = strip; + } + int ifan = int(result.vertices.size()); + result.vertices.emplace_back((p2 + ncurrent * radius).cast()); + triangulate_fan(result, ifan, prev_strip.first, prev_strip.second); + // sprintf(fname, "d:\\temp\\meshes\\tree-partial-%d.obj", ++ irun); + // its_write_obj(result, fname); + } + } else { + const SupportElement &next = *path[ipath + 1]; + assert(current.state.layer_idx + 1 == next.state.layer_idx); + p3 = to_3d(unscaled(next.state.result_on_layer), layer_z(slicing_params, next.state.layer_idx)); + v2 = (p3 - p2).normalized(); + ncurrent = (v1 + v2).normalized(); + float radius = unscaled(config.getRadius(current.state)); + std::pair strip; + if (current.state.type == TreeSupport::NodeType::ePolygon) { + strip = discretize_polygon(p2.cast(), current.influence_area, result.vertices); + } + else { + strip = discretize_circle(p2.cast(), ncurrent.cast(), radius, eps, result.vertices); + } + triangulate_strip(result, prev_strip.first, prev_strip.second, strip.first, strip.second); + prev_strip = strip; +// sprintf(fname, "d:\\temp\\meshes\\tree-partial-%d.obj", ++irun); +// its_write_obj(result, fname); + } +#if 0 + if (circles_intersect(p1, nprev, settings.getRadius(prev), p2, ncurrent, settings.getRadius(current))) { + // Cannot connect previous and current slice using a simple zig-zag triangulation, + // because the two circles intersect. + + } else { + // Continue with chaining. + + } +#endif + } +} +#endif + +#ifdef TREE_SUPPORT_ORGANIC_NUDGE_NEW +// New version using per layer AABB trees of lines for nudging spheres away from an object. +static void organic_smooth_branches_avoid_collisions( + const PrintObject &print_object, + const TreeModelVolumes &volumes, + const TreeSupportSettings &config, + std::vector &move_bounds, + const std::vector> &elements_with_link_down, + const std::vector &linear_data_layers, + std::function throw_on_cancel) +{ + struct LayerCollisionCache { + coord_t min_element_radius{ std::numeric_limits::max() }; + bool min_element_radius_known() const { return this->min_element_radius != std::numeric_limits::max(); } + coord_t collision_radius{ 0 }; + std::vector lines; + AABBTreeIndirect::Tree<2, double> aabbtree_lines; + bool empty() const { return this->lines.empty(); } + }; + std::vector layer_collision_cache; + layer_collision_cache.reserve(1024); + const SlicingParameters &slicing_params = print_object.slicing_parameters(); + for (const std::pair& element : elements_with_link_down) { + LayerIndex layer_idx = element.first->state.layer_idx; + if (size_t num_layers = layer_idx + 1; num_layers > layer_collision_cache.size()) { + if (num_layers > layer_collision_cache.capacity()) + layer_collision_cache.reserve(next_highest_power_of_2(num_layers)); + layer_collision_cache.resize(num_layers, {}); + } + auto& l = layer_collision_cache[layer_idx]; + l.min_element_radius = std::min(l.min_element_radius, config.getRadius(element.first->state)); + } + + throw_on_cancel(); + + for (LayerIndex layer_idx = 0; layer_idx < LayerIndex(layer_collision_cache.size()); ++layer_idx) + if (LayerCollisionCache& l = layer_collision_cache[layer_idx]; !l.min_element_radius_known()) + l.min_element_radius = 0; + else { + //FIXME + l.min_element_radius = 0; + std::optional>> res = volumes.get_collision_lower_bound_area(layer_idx, l.min_element_radius); + assert(res.has_value()); + l.collision_radius = res->first; + Lines alines = to_lines(res->second.get()); + l.lines.reserve(alines.size()); + for (const Line &line : alines) + l.lines.push_back({ unscaled(line.a), unscaled(line.b) }); + l.aabbtree_lines = AABBTreeLines::build_aabb_tree_over_indexed_lines(l.lines); + throw_on_cancel(); + } + + struct CollisionSphere { + const SupportElement& element; + int element_below_id; + const bool locked; + float radius; + // Current position, when nudged away from the collision. + Vec3f position; + // Previous position, for Laplacian smoothing. + Vec3f prev_position; + // + Vec3f last_collision; + double last_collision_depth; + // Minimum Z for which the sphere collision will be evaluated. + // Limited by the minimum sloping angle and by the bottom of the tree. + float min_z{ -std::numeric_limits::max() }; + // Maximum Z for which the sphere collision will be evaluated. + // Limited by the minimum sloping angle and by the tip of the current branch. + float max_z{ std::numeric_limits::max() }; + uint32_t layer_begin; + uint32_t layer_end; + }; + + std::vector collision_spheres; + collision_spheres.reserve(elements_with_link_down.size()); + for (const std::pair &element_with_link : elements_with_link_down) { + const SupportElement &element = *element_with_link.first; + const int link_down = element_with_link.second; + collision_spheres.push_back({ + element, + link_down, + // locked + element.parents.empty() || (link_down == -1 && element.state.layer_idx > 0), + unscaled(config.getRadius(element.state)), + // 3D position + to_3d(unscaled(element.state.result_on_layer), float(layer_z(slicing_params, element.state.layer_idx))) + }); + // Update min_z coordinate to min_z of the tree below. + CollisionSphere &collision_sphere = collision_spheres.back(); + if (link_down != -1) { + const size_t offset_below = linear_data_layers[element.state.layer_idx - 1]; + collision_sphere.min_z = collision_spheres[offset_below + link_down].min_z; + } else + collision_sphere.min_z = collision_sphere.position.z(); + } + // Update max_z by propagating max_z from the tips of the branches. + for (int collision_sphere_id = int(collision_spheres.size()) - 1; collision_sphere_id >= 0; -- collision_sphere_id) { + CollisionSphere &collision_sphere = collision_spheres[collision_sphere_id]; + if (collision_sphere.element.parents.empty()) + // Tip + collision_sphere.max_z = collision_sphere.position.z(); + else { + // Below tip + const size_t offset_above = linear_data_layers[collision_sphere.element.state.layer_idx + 1]; + for (auto iparent : collision_sphere.element.parents) { + float parent_z = collision_spheres[offset_above + iparent].max_z; +// collision_sphere.max_z = collision_sphere.max_z == std::numeric_limits::max() ? parent_z : std::max(collision_sphere.max_z, parent_z); + collision_sphere.max_z = std::min(collision_sphere.max_z, parent_z); + } + } + } + // Update min_z / max_z to limit the search Z span of a given sphere for collision detection. + for (CollisionSphere &collision_sphere : collision_spheres) { + //FIXME limit the collision span by the tree slope. + collision_sphere.min_z = std::max(collision_sphere.min_z, collision_sphere.position.z() - collision_sphere.radius); + collision_sphere.max_z = std::min(collision_sphere.max_z, collision_sphere.position.z() + collision_sphere.radius); + collision_sphere.layer_begin = std::min(collision_sphere.element.state.layer_idx, layer_idx_ceil(slicing_params, collision_sphere.min_z)); + collision_sphere.layer_end = std::max(collision_sphere.element.state.layer_idx, layer_idx_floor(slicing_params, collision_sphere.max_z)) + 1; + } + + throw_on_cancel(); + + static constexpr const double collision_extra_gap = 0.1; + static constexpr const double max_nudge_collision_avoidance = 0.5; + static constexpr const double max_nudge_smoothing = 0.2; + static constexpr const size_t num_iter = 100; // 1000; + for (size_t iter = 0; iter < num_iter; ++ iter) { + // Back up prev position before Laplacian smoothing. + for (CollisionSphere &collision_sphere : collision_spheres) + collision_sphere.prev_position = collision_sphere.position; + std::atomic num_moved{ 0 }; + tbb::parallel_for(tbb::blocked_range(0, collision_spheres.size()), + [&collision_spheres, &layer_collision_cache, &slicing_params, &move_bounds, &linear_data_layers, &num_moved, &throw_on_cancel](const tbb::blocked_range range) { + for (size_t collision_sphere_id = range.begin(); collision_sphere_id < range.end(); ++ collision_sphere_id) + if (CollisionSphere &collision_sphere = collision_spheres[collision_sphere_id]; ! collision_sphere.locked) { + // Calculate collision of multiple 2D layers against a collision sphere. + collision_sphere.last_collision_depth = - std::numeric_limits::max(); + for (uint32_t layer_id = collision_sphere.layer_begin; layer_id != collision_sphere.layer_end; ++ layer_id) { + double dz = (layer_id - collision_sphere.element.state.layer_idx) * slicing_params.layer_height; + if (double r2 = sqr(collision_sphere.radius) - sqr(dz); r2 > 0) { + if (const LayerCollisionCache &layer_collision_cache_item = layer_collision_cache[layer_id]; ! layer_collision_cache_item.empty()) { + size_t hit_idx_out; + Vec2d hit_point_out; + if (double dist = sqrt(AABBTreeLines::squared_distance_to_indexed_lines( + layer_collision_cache_item.lines, layer_collision_cache_item.aabbtree_lines, Vec2d(to_2d(collision_sphere.position).cast()), + hit_idx_out, hit_point_out, r2)); dist >= 0.) { + double collision_depth = sqrt(r2) - dist; + if (collision_depth > collision_sphere.last_collision_depth) { + collision_sphere.last_collision_depth = collision_depth; + collision_sphere.last_collision = to_3d(hit_point_out.cast(), float(layer_z(slicing_params, layer_id))); + } + } + } + } + } + if (collision_sphere.last_collision_depth > 0) { + // Collision detected to be removed. + // Nudge the circle center away from the collision. + if (collision_sphere.last_collision_depth > EPSILON) + // a little bit of hysteresis to detect end of + ++ num_moved; + // Shift by maximum 2mm. + double nudge_dist = std::min(std::max(0., collision_sphere.last_collision_depth + collision_extra_gap), max_nudge_collision_avoidance); + Vec2d nudge_vector = (to_2d(collision_sphere.position) - to_2d(collision_sphere.last_collision)).cast().normalized() * nudge_dist; + collision_sphere.position.head<2>() += (nudge_vector * nudge_dist).cast(); + } + // Laplacian smoothing + Vec2d avg{ 0, 0 }; + const SupportElements &above = move_bounds[collision_sphere.element.state.layer_idx + 1]; + const size_t offset_above = linear_data_layers[collision_sphere.element.state.layer_idx + 1]; + double weight = 0.; + for (auto iparent : collision_sphere.element.parents) { + double w = collision_sphere.radius; + avg += w * to_2d(collision_spheres[offset_above + iparent].prev_position.cast()); + weight += w; + } + if (collision_sphere.element_below_id != -1) { + const size_t offset_below = linear_data_layers[collision_sphere.element.state.layer_idx - 1]; + const double w = weight; // config.getRadius(move_bounds[element.state.layer_idx - 1][below].state); + avg += w * to_2d(collision_spheres[offset_below + collision_sphere.element_below_id].prev_position.cast()); + weight += w; + } + avg /= weight; + static constexpr const double smoothing_factor = 0.5; + Vec2d old_pos = to_2d(collision_sphere.position).cast(); + Vec2d new_pos = (1. - smoothing_factor) * old_pos + smoothing_factor * avg; + Vec2d shift = new_pos - old_pos; + double nudge_dist_max = shift.norm(); + // Shift by maximum 1mm, less than the collision avoidance factor. + double nudge_dist = std::min(std::max(0., nudge_dist_max), max_nudge_smoothing); + collision_sphere.position.head<2>() += (shift.normalized() * nudge_dist).cast(); + + throw_on_cancel(); + } + }); +#if 0 + std::vector stat; + for (CollisionSphere& collision_sphere : collision_spheres) + if (!collision_sphere.locked) + stat.emplace_back(collision_sphere.last_collision_depth); + std::sort(stat.begin(), stat.end()); + printf("iteration: %d, moved: %d, collision depth: min %lf, max %lf, median %lf\n", int(iter), int(num_moved), stat.front(), stat.back(), stat[stat.size() / 2]); +#endif + if (num_moved == 0) + break; + } + + for (size_t i = 0; i < collision_spheres.size(); ++ i) + elements_with_link_down[i].first->state.result_on_layer = scaled(to_2d(collision_spheres[i].position)); +} +#else // TREE_SUPPORT_ORGANIC_NUDGE_NEW +// Old version using OpenVDB, works but it is extremely slow for complex meshes. +static void organic_smooth_branches_avoid_collisions( + const PrintObject &print_object, + const TreeModelVolumes &volumes, + const TreeSupportSettings &config, + std::vector &move_bounds, + const std::vector> &elements_with_link_down, + const std::vector &linear_data_layers, + std::function throw_on_cancel) +{ + TriangleMesh mesh = print_object.model_object()->raw_mesh(); + mesh.transform(print_object.trafo_centered()); + double scale = 10.; + openvdb::FloatGrid::Ptr grid = mesh_to_grid(mesh.its, openvdb::math::Transform{}, scale, 0., 0.); + std::unique_ptr> closest_surface_point = openvdb::tools::ClosestSurfacePoint::create(*grid); + std::vector pts, prev, projections; + std::vector distances; + for (const std::pair& element : elements_with_link_down) { + Vec3d pt = to_3d(unscaled(element.first->state.result_on_layer), layer_z(print_object.slicing_parameters(), element.first->state.layer_idx)) * scale; + pts.push_back({ pt.x(), pt.y(), pt.z() }); + } + + const double collision_extra_gap = 1. * scale; + const double max_nudge_collision_avoidance = 2. * scale; + const double max_nudge_smoothing = 1. * scale; + + static constexpr const size_t num_iter = 100; // 1000; + for (size_t iter = 0; iter < num_iter; ++ iter) { + prev = pts; + projections = pts; + distances.assign(pts.size(), std::numeric_limits::max()); + closest_surface_point->searchAndReplace(projections, distances); + size_t num_moved = 0; + for (size_t i = 0; i < projections.size(); ++ i) { + const SupportElement &element = *elements_with_link_down[i].first; + const int below = elements_with_link_down[i].second; + const bool locked = below == -1 && element.state.layer_idx > 0; + if (! locked && pts[i] != projections[i]) { + // Nudge the circle center away from the collision. + Vec3d v{ projections[i].x() - pts[i].x(), projections[i].y() - pts[i].y(), projections[i].z() - pts[i].z() }; + double depth = v.norm(); + assert(std::abs(distances[i] - depth) < EPSILON); + double radius = unscaled(config.getRadius(element.state)) * scale; + if (depth < radius) { + // Collision detected to be removed. + ++ num_moved; + double dxy = sqrt(sqr(radius) - sqr(v.z())); + double nudge_dist_max = dxy - std::hypot(v.x(), v.y()) + //FIXME 1mm gap + + collision_extra_gap; + // Shift by maximum 2mm. + double nudge_dist = std::min(std::max(0., nudge_dist_max), max_nudge_collision_avoidance); + Vec2d nudge_v = to_2d(v).normalized() * (- nudge_dist); + pts[i].x() += nudge_v.x(); + pts[i].y() += nudge_v.y(); + } + } + // Laplacian smoothing + if (! locked && ! element.parents.empty()) { + Vec2d avg{ 0, 0 }; + const SupportElements &above = move_bounds[element.state.layer_idx + 1]; + const size_t offset_above = linear_data_layers[element.state.layer_idx + 1]; + double weight = 0.; + for (auto iparent : element.parents) { + double w = config.getRadius(above[iparent].state); + avg.x() += w * prev[offset_above + iparent].x(); + avg.y() += w * prev[offset_above + iparent].y(); + weight += w; + } + size_t cnt = element.parents.size(); + if (below != -1) { + const size_t offset_below = linear_data_layers[element.state.layer_idx - 1]; + const double w = weight; // config.getRadius(move_bounds[element.state.layer_idx - 1][below].state); + avg.x() += w * prev[offset_below + below].x(); + avg.y() += w * prev[offset_below + below].y(); + ++ cnt; + weight += w; + } + //avg /= double(cnt); + avg /= weight; + static constexpr const double smoothing_factor = 0.5; + Vec2d old_pos{ pts[i].x(), pts[i].y() }; + Vec2d new_pos = (1. - smoothing_factor) * old_pos + smoothing_factor * avg; + Vec2d shift = new_pos - old_pos; + double nudge_dist_max = shift.norm(); + // Shift by maximum 1mm, less than the collision avoidance factor. + double nudge_dist = std::min(std::max(0., nudge_dist_max), max_nudge_smoothing); + Vec2d nudge_v = shift.normalized() * nudge_dist; + pts[i].x() += nudge_v.x(); + pts[i].y() += nudge_v.y(); + } + } +// printf("iteration: %d, moved: %d\n", int(iter), int(num_moved)); + if (num_moved == 0) + break; + } + + for (size_t i = 0; i < projections.size(); ++ i) { + elements_with_link_down[i].first->state.result_on_layer.x() = scaled(pts[i].x()) / scale; + elements_with_link_down[i].first->state.result_on_layer.y() = scaled(pts[i].y()) / scale; + } +} +#endif // TREE_SUPPORT_ORGANIC_NUDGE_NEW + +// Organic specific: Smooth branches and produce one cummulative mesh to be sliced. +indexed_triangle_set draw_branches( + PrintObject &print_object, + const TreeModelVolumes &volumes, + const TreeSupportSettings &config, + std::vector &move_bounds, + std::function throw_on_cancel) +{ + static int irun = 0; + + // All SupportElements are put into a layer independent storage to improve parallelization. + std::vector> elements_with_link_down; + std::vector linear_data_layers; + { + std::vector> map_downwards_old; + std::vector> map_downwards_new; + linear_data_layers.emplace_back(0); + for (LayerIndex layer_idx = 0; layer_idx < LayerIndex(move_bounds.size()); ++ layer_idx) { + SupportElements *layer_above = layer_idx + 1 < move_bounds.size() ? &move_bounds[layer_idx + 1] : nullptr; + map_downwards_new.clear(); + std::sort(map_downwards_old.begin(), map_downwards_old.end(), [](auto& l, auto& r) { return l.first < r.first; }); + SupportElements &layer = move_bounds[layer_idx]; + for (size_t elem_idx = 0; elem_idx < layer.size(); ++ elem_idx) { + SupportElement &elem = layer[elem_idx]; + int child = -1; + if (layer_idx > 0) { + auto it = std::lower_bound(map_downwards_old.begin(), map_downwards_old.end(), &elem, [](auto& l, const SupportElement* r) { return l.first < r; }); + if (it != map_downwards_old.end() && it->first == &elem) { + child = it->second; + // Only one link points to a node above from below. + assert(!(++it != map_downwards_old.end() && it->first == &elem)); + } + const SupportElement *pchild = child == -1 ? nullptr : &move_bounds[layer_idx - 1][child]; + assert(pchild ? pchild->state.result_on_layer_is_set() : elem.state.target_height > layer_idx); + } + for (int32_t parent_idx : elem.parents) { + SupportElement &parent = (*layer_above)[parent_idx]; + if (parent.state.result_on_layer_is_set()) + map_downwards_new.emplace_back(&parent, elem_idx); + } + + elements_with_link_down.push_back({ &elem, int(child) }); + } + std::swap(map_downwards_old, map_downwards_new); + linear_data_layers.emplace_back(elements_with_link_down.size()); + } + } + + throw_on_cancel(); + + organic_smooth_branches_avoid_collisions(print_object, volumes, config, move_bounds, elements_with_link_down, linear_data_layers, throw_on_cancel); + + // Unmark all nodes. + for (SupportElements &elements : move_bounds) + for (SupportElement &element : elements) + element.state.marked = false; + + // Traverse all nodes, generate tubes. + // Traversal stack with nodes and thier current parent + const SlicingParameters &slicing_params = print_object.slicing_parameters(); + std::vector path; + indexed_triangle_set cummulative_mesh; + indexed_triangle_set partial_mesh; + indexed_triangle_set temp_mesh; + for (LayerIndex layer_idx = 0; layer_idx + 1 < LayerIndex(move_bounds.size()); ++ layer_idx) { + SupportElements &layer = move_bounds[layer_idx]; + SupportElements &layer_above = move_bounds[layer_idx + 1]; + + for (SupportElement &start_element : layer) + if (! start_element.state.marked && ! start_element.parents.empty()) { + // Collect elements up to a bifurcation above. + start_element.state.marked = true; + for (size_t parent_idx = 0; parent_idx < start_element.parents.size(); ++ parent_idx) { + path.clear(); + path.emplace_back(&start_element); + // Traverse each branch until it branches again. + SupportElement &first_parent = layer_above[start_element.parents[parent_idx]]; + assert(path.back()->state.layer_idx + 1 == first_parent.state.layer_idx); + path.emplace_back(&first_parent); + if (first_parent.parents.size() < 2) + first_parent.state.marked = true; + if (first_parent.parents.size() == 1) { + for (SupportElement *parent = &first_parent;;) { + SupportElement &next_parent = move_bounds[parent->state.layer_idx + 1][parent->parents.front()]; + assert(path.back()->state.layer_idx + 1 == next_parent.state.layer_idx); + path.emplace_back(&next_parent); + if (next_parent.parents.size() > 1) + break; + next_parent.state.marked = true; + if (next_parent.parents.size() == 0) + break; + parent = &next_parent; + } + } + // Triangulate the tube. + partial_mesh.clear(); + extrude_branch(path, config, slicing_params, move_bounds, partial_mesh); +#if 1 + char fname[2048]; + sprintf(fname, "SVG\\tree-raw-%d.obj", ++ irun); + its_write_obj(partial_mesh, fname); +#if 0 + temp_mesh.clear(); + cut_mesh(partial_mesh, layer_z(slicing_params, path.back()->state.layer_idx) + EPSILON, nullptr, &temp_mesh, false); + sprintf(fname, "d:\\temp\\meshes\\tree-trimmed1-%d.obj", irun); + its_write_obj(temp_mesh, fname); + partial_mesh.clear(); + cut_mesh(temp_mesh, layer_z(slicing_params, path.front()->state.layer_idx) - EPSILON, &partial_mesh, nullptr, false); + sprintf(fname, "d:\\temp\\meshes\\tree-trimmed2-%d.obj", irun); + its_write_obj(partial_mesh, fname); +#endif +#endif + its_merge(cummulative_mesh, partial_mesh); + } + throw_on_cancel(); + } + } + return cummulative_mesh; +} + +// Organic specific: Slice the cummulative mesh produced by draw_branches(). +void slice_branches( + PrintObject &print_object, + const TreeModelVolumes &volumes, + const TreeSupportSettings &config, + const std::vector &overhangs, + std::vector &move_bounds, + const indexed_triangle_set &cummulative_mesh, + + SupportGeneratorLayersPtr &bottom_contacts, + SupportGeneratorLayersPtr &top_contacts, + SupportGeneratorLayersPtr &intermediate_layers, + SupportGeneratorLayerStorage &layer_storage, + + std::function throw_on_cancel) +{ + const SlicingParameters &slicing_params = print_object.slicing_parameters(); + std::vector slice_z; + for (size_t layer_idx = 0; layer_idx < move_bounds.size(); ++ layer_idx) { + double print_z = slicing_params.object_print_z_min + slicing_params.first_object_layer_height + layer_idx * slicing_params.layer_height; + double layer_height = layer_idx == 0 ? slicing_params.first_object_layer_height : slicing_params.layer_height; + slice_z.emplace_back(float(print_z - layer_height * 0.5)); + } + // Remove the trailing slices. + while (! slice_z.empty()) + if (move_bounds[slice_z.size() - 1].empty()) + slice_z.pop_back(); + else + break; + +#if 0 + its_write_obj(cummulative_mesh, "d:\\temp\\meshes\\tree.obj"); +#endif + + MeshSlicingParamsEx params; + params.closing_radius = float(print_object.config().slice_closing_radius.value); + params.mode = MeshSlicingParams::SlicingMode::Positive; + std::vector slices = slice_mesh_ex(cummulative_mesh, slice_z, params, throw_on_cancel); + for (size_t layer_idx = 0; layer_idx < slice_z.size(); ++ layer_idx) + if (! slices[layer_idx].empty()) { + SupportGeneratorLayer *&l = intermediate_layers[layer_idx]; + if (l == nullptr) + l = &layer_allocate(layer_storage, SupporLayerType::sltBase, slicing_params, layer_idx); + append(l->polygons, to_polygons(std::move(slices[layer_idx]))); + } + + // Trim the slices. + tbb::parallel_for(tbb::blocked_range(0, intermediate_layers.size()), + [&](const tbb::blocked_range &range) { + for (size_t layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx) + if (SupportGeneratorLayer *layer = intermediate_layers[layer_idx]; layer) { + Polygons &poly = intermediate_layers[layer_idx]->polygons; + poly = diff_clipped(poly, volumes.getCollision(0, layer_idx, true)); + poly = intersection(poly, volumes.m_bed_area); + } + }); + + std::vector support_layer_storage(move_bounds.size()); + std::vector support_roof_storage(move_bounds.size()); + finalize_interface_and_support_areas(print_object, volumes, config, overhangs, support_layer_storage, support_roof_storage, + bottom_contacts, top_contacts, intermediate_layers, layer_storage, throw_on_cancel); +} + +/*! + * \brief Create the areas that need support. + * + * These areas are stored inside the given SliceDataStorage object. + * \param storage The data storage where the mesh data is gotten from and + * where the resulting support areas are stored. + */ +static void generate_support_areas(Print &print, TreeSupport* tree_support, const BuildVolume &build_volume, const std::vector &print_object_ids, std::function throw_on_cancel) +{ + g_showed_critical_error = false; + g_showed_performance_warning = false; + + // Settings with the indexes of meshes that use these settings. + std::vector>> grouped_meshes = group_meshes(print, print_object_ids); + if (grouped_meshes.empty()) + return; + + size_t counter = 0; + + // Process every mesh group. These groups can not be processed parallel, as the processing in each group is parallelized, and nested parallelization is disables and slow. + for (std::pair> &processing : grouped_meshes) + { + // process each combination of meshes + // this struct is used to easy retrieve setting. No other function except those in TreeModelVolumes and generate_initial_areas() have knowledge of the existence of multiple meshes being processed. + //FIXME this is a copy + // Contains config settings to avoid loading them in every function. This was done to improve readability of the code. + const TreeSupportSettings &config = processing.first; + BOOST_LOG_TRIVIAL(info) << "Processing support tree mesh group " << counter + 1 << " of " << grouped_meshes.size() << " containing " << grouped_meshes[counter].second.size() << " meshes."; + auto t_start = std::chrono::high_resolution_clock::now(); + +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + m_progress_multiplier = 1.0 / double(grouped_meshes.size()); + m_progress_offset = counter == 0 ? 0 : TREE_PROGRESS_TOTAL * (double(counter) * m_progress_multiplier); +#endif // SLIC3R_TREESUPPORT_PROGRESS + PrintObject &print_object = *print.get_object(processing.second.front()); + // Generator for model collision, avoidance and internal guide volumes. + TreeModelVolumes volumes{ print_object, build_volume, config.maximum_move_distance, config.maximum_move_distance_slow, processing.second.front(), +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + m_progress_multiplier, m_progress_offset, +#endif // SLIC3R_TREESUPPORTS_PROGRESS + /* additional_excluded_areas */{} }; + + //FIXME generating overhangs just for the first mesh of the group. + assert(processing.second.size() == 1); + + print.set_status(55, _L("Support: detect overhangs")); + std::vector overhangs; + tree_support->detect_overhangs(); + overhangs.resize(print_object.support_layer_count()); + for (size_t i = 0; i < overhangs.size(); i++) + { + overhangs[i] = to_polygons(print_object.get_support_layer(i)->overhang_areas); + } + print_object.clear_support_layers(); + + // ### Precalculate avoidances, collision etc. + size_t num_support_layers = precalculate(print, overhangs, processing.first, processing.second, volumes, throw_on_cancel); + if (num_support_layers == 0) + continue; + + auto t_precalc = std::chrono::high_resolution_clock::now(); + + // value is the area where support may be placed. As this is calculated in CreateLayerPathing it is saved and reused in draw_areas + std::vector move_bounds(num_support_layers); + + // ### Place tips of the support tree + SupportGeneratorLayersPtr bottom_contacts(num_support_layers, nullptr); + SupportGeneratorLayersPtr top_contacts(num_support_layers, nullptr); + SupportGeneratorLayersPtr intermediate_layers(num_support_layers, nullptr); + SupportGeneratorLayerStorage layer_storage; + + for (size_t mesh_idx : processing.second) + generate_initial_areas(*print.get_object(mesh_idx), volumes, config, overhangs, move_bounds, top_contacts, layer_storage, throw_on_cancel); + auto t_gen = std::chrono::high_resolution_clock::now(); + +#ifdef TREESUPPORT_DEBUG_SVG + for (size_t layer_idx = 0; layer_idx < move_bounds.size(); ++layer_idx) { + Polygons polys; + for (auto& area : move_bounds[layer_idx]) + append(polys, area.influence_area); + if (auto begin = move_bounds[layer_idx].begin(); begin != move_bounds[layer_idx].end()) + SVG::export_expolygons(debug_out_path("treesupport-initial_areas-%d.svg", layer_idx), + { { { union_ex(volumes.getWallRestriction(config.getCollisionRadius(begin->state), layer_idx, begin->state.use_min_xy_dist)) }, + { "wall_restricrictions", "gray", 0.5f } }, + { { union_ex(polys) }, { "parent", "red", "black", "", scaled(0.1f), 0.5f } } }); + } +#endif // TREESUPPORT_DEBUG_SVG + + // ### Propagate the influence areas downwards. This is an inherently serial operation. + print.set_status(60, _L("Support: propagate branches")); + create_layer_pathing(volumes, config, move_bounds, throw_on_cancel); + auto t_path = std::chrono::high_resolution_clock::now(); + + // ### Set a point in each influence area + create_nodes_from_area(volumes, config, move_bounds, throw_on_cancel); + auto t_place = std::chrono::high_resolution_clock::now(); + + // ### draw these points as circles + + if (0 && is_tree(print_object.config().support_type.value)) + draw_areas(*print.get_object(processing.second.front()), volumes, config, overhangs, move_bounds, + bottom_contacts, top_contacts, intermediate_layers, layer_storage, throw_on_cancel); + else { + indexed_triangle_set branches = draw_branches(*print.get_object(processing.second.front()), volumes, config, move_bounds, throw_on_cancel); + // Reduce memory footprint. After this point only slice_branches() will use volumes and from that only collisions with zero radius will be used. + volumes.clear_all_but_object_collision(); + slice_branches(*print.get_object(processing.second.front()), volumes, config, overhangs, move_bounds, branches, + bottom_contacts, top_contacts, intermediate_layers, layer_storage, throw_on_cancel); + } + + auto t_draw = std::chrono::high_resolution_clock::now(); + auto dur_pre_gen = 0.001 * std::chrono::duration_cast(t_precalc - t_start).count(); + auto dur_gen = 0.001 * std::chrono::duration_cast(t_gen - t_precalc).count(); + auto dur_path = 0.001 * std::chrono::duration_cast(t_path - t_gen).count(); + auto dur_place = 0.001 * std::chrono::duration_cast(t_place - t_path).count(); + auto dur_draw = 0.001 * std::chrono::duration_cast(t_draw - t_place).count(); + auto dur_total = 0.001 * std::chrono::duration_cast(t_draw - t_start).count(); + BOOST_LOG_TRIVIAL(info) << + "Total time used creating Tree support for the currently grouped meshes: " << dur_total << " ms. " + "Different subtasks:\nCalculating Avoidance: " << dur_pre_gen << " ms " + "Creating inital influence areas: " << dur_gen << " ms " + "Influence area creation: " << dur_path << "ms " + "Placement of Points in InfluenceAreas: " << dur_place << "ms " + "Drawing result as support " << dur_draw << " ms"; + + move_bounds.clear(); + + auto remove_undefined_layers = [](SupportGeneratorLayersPtr &layers) { + layers.erase(std::remove_if(layers.begin(), layers.end(), [](const SupportGeneratorLayer* ptr) { return ptr == nullptr; }), layers.end()); + }; + remove_undefined_layers(bottom_contacts); + remove_undefined_layers(top_contacts); + remove_undefined_layers(intermediate_layers); + + // Produce the support G-code. + // Used by both classic and tree supports. + SupportParameters support_params(print_object); + support_params.with_sheath = true; + support_params.support_density = 0; + SupportGeneratorLayersPtr interface_layers, base_interface_layers; + SupportGeneratorLayersPtr raft_layers = generate_raft_base(print_object, support_params, print_object.slicing_parameters(), top_contacts, interface_layers, base_interface_layers, intermediate_layers, layer_storage); +#if 1 //#ifdef SLIC3R_DEBUG + SupportGeneratorLayersPtr layers_sorted = +#endif // SLIC3R_DEBUG + generate_support_layers(print_object, raft_layers, bottom_contacts, top_contacts, intermediate_layers, interface_layers, base_interface_layers); + + // Don't fill in the tree supports, make them hollow with just a single sheath line. + print.set_status(69, _L("Support: generate toolpath")); + generate_support_toolpaths(print_object, print_object.support_layers(), print_object.config(), support_params, print_object.slicing_parameters(), + raft_layers, bottom_contacts, top_contacts, intermediate_layers, interface_layers, base_interface_layers); + + #if 0 +//#ifdef SLIC3R_DEBUG + { + static int iRun = 0; + ++ iRun; + size_t layer_id = 0; + for (int i = 0; i < int(layers_sorted.size());) { + // Find the last layer with roughly the same print_z, find the minimum layer height of all. + // Due to the floating point inaccuracies, the print_z may not be the same even if in theory they should. + int j = i + 1; + coordf_t zmax = layers_sorted[i]->print_z + EPSILON; + bool empty = layers_sorted[i]->polygons.empty(); + for (; j < layers_sorted.size() && layers_sorted[j]->print_z <= zmax; ++j) + if (!layers_sorted[j]->polygons.empty()) + empty = false; + if (!empty) { + export_print_z_polygons_to_svg( + debug_out_path("support-%d-%lf.svg", iRun, layers_sorted[i]->print_z).c_str(), + layers_sorted.data() + i, j - i); + export_print_z_polygons_and_extrusions_to_svg( + debug_out_path("support-w-fills-%d-%lf.svg", iRun, layers_sorted[i]->print_z).c_str(), + layers_sorted.data() + i, j - i, + *print_object.support_layers()[layer_id]); + ++layer_id; + } + i = j; + } + } +#endif /* SLIC3R_DEBUG */ + + ++ counter; + } + +// storage.support.generated = true; +} + +} // namespace TreeSupport3D + +void generate_tree_support_3D(PrintObject &print_object, TreeSupport* tree_support, std::function throw_on_cancel) +{ + size_t idx = 0; + for (const PrintObject *po : print_object.print()->objects()) { + if (po == &print_object) + break; + ++idx; + } + + Points bedpts = tree_support->m_machine_border.contour.points; + BuildVolume build_volume{ Pointfs{ unscaled(bedpts[0]), unscaled(bedpts[1]),unscaled(bedpts[2]),unscaled(bedpts[3])}, tree_support->m_print_config->printable_height }; + + TreeSupport3D::generate_support_areas(*print_object.print(), tree_support, build_volume, { idx }, throw_on_cancel); +} + +} // namespace Slic3r diff --git a/src/libslic3r/TreeSupport3D.hpp b/src/libslic3r/TreeSupport3D.hpp new file mode 100644 index 000000000..307af3e8b --- /dev/null +++ b/src/libslic3r/TreeSupport3D.hpp @@ -0,0 +1,599 @@ +// Tree supports by Thomas Rahm, losely based on Tree Supports by CuraEngine. +// Original source of Thomas Rahm's tree supports: +// https://github.com/ThomasRahm/CuraEngine +// +// Original CuraEngine copyright: +// Copyright (c) 2021 Ultimaker B.V. +// CuraEngine is released under the terms of the AGPLv3 or higher. + +#ifndef slic3r_TreeSupport_hpp +#define slic3r_TreeSupport_hpp + +#include "TreeModelVolumes.hpp" +#include "Point.hpp" + +#include + +#include "BoundingBox.hpp" +#include "Utils.hpp" + +// #define TREE_SUPPORT_SHOW_ERRORS + +#ifdef SLIC3R_TREESUPPORTS_PROGRESS + // The various stages of the process can be weighted differently in the progress bar. + // These weights are obtained experimentally using a small sample size. Sensible weights can differ drastically based on the assumed default settings and model. + #define TREE_PROGRESS_TOTAL 10000 + #define TREE_PROGRESS_PRECALC_COLL TREE_PROGRESS_TOTAL * 0.1 + #define TREE_PROGRESS_PRECALC_AVO TREE_PROGRESS_TOTAL * 0.4 + #define TREE_PROGRESS_GENERATE_NODES TREE_PROGRESS_TOTAL * 0.1 + #define TREE_PROGRESS_AREA_CALC TREE_PROGRESS_TOTAL * 0.3 + #define TREE_PROGRESS_DRAW_AREAS TREE_PROGRESS_TOTAL * 0.1 + #define TREE_PROGRESS_GENERATE_BRANCH_AREAS TREE_PROGRESS_DRAW_AREAS / 3 + #define TREE_PROGRESS_SMOOTH_BRANCH_AREAS TREE_PROGRESS_DRAW_AREAS / 3 + #define TREE_PROGRESS_FINALIZE_BRANCH_AREAS TREE_PROGRESS_DRAW_AREAS / 3 +#endif // SLIC3R_TREESUPPORTS_PROGRESS + +namespace Slic3r +{ + +// Forward declarations +class TreeSupport; +class Print; +class PrintObject; +class SupportGeneratorLayer; +using SupportGeneratorLayerStorage = std::deque; +using SupportGeneratorLayersPtr = std::vector; + +namespace TreeSupport3D +{ + +using LayerIndex = int; + +static constexpr const double SUPPORT_TREE_EXPONENTIAL_FACTOR = 1.5; +static constexpr const coord_t SUPPORT_TREE_EXPONENTIAL_THRESHOLD = scaled(1. * SUPPORT_TREE_EXPONENTIAL_FACTOR); +static constexpr const coord_t SUPPORT_TREE_COLLISION_RESOLUTION = scaled(0.5); + +// The number of vertices in each circle. +static constexpr const size_t SUPPORT_TREE_CIRCLE_RESOLUTION = 25; +static constexpr const bool SUPPORT_TREE_AVOID_SUPPORT_BLOCKER = true; + +enum class InterfacePreference +{ + InterfaceAreaOverwritesSupport, + SupportAreaOverwritesInterface, + InterfaceLinesOverwriteSupport, + SupportLinesOverwriteInterface, + Nothing +}; + +struct AreaIncreaseSettings +{ + AreaIncreaseSettings( + TreeModelVolumes::AvoidanceType type = TreeModelVolumes::AvoidanceType::Fast, coord_t increase_speed = 0, + bool increase_radius = false, bool no_error = false, bool use_min_distance = false, bool move = false) : + increase_speed{ increase_speed }, type{ type }, increase_radius{ increase_radius }, no_error{ no_error }, use_min_distance{ use_min_distance }, move{ move } {} + + coord_t increase_speed; + // Packing for smaller memory footprint of SupportElementState && SupportElementMerging + TreeModelVolumes::AvoidanceType type; + bool increase_radius : 1; + bool no_error : 1; + bool use_min_distance : 1; + bool move : 1; + bool operator==(const AreaIncreaseSettings& other) const + { + return type == other.type && + increase_speed == other.increase_speed && + increase_radius == other.increase_radius && + no_error == other.no_error && + use_min_distance == other.use_min_distance && + move == other.move; + } +}; + +struct TreeSupportSettings; + +// C++17 does not support in place initializers of bit values, thus a constructor zeroing the bits is provided. +struct SupportElementStateBits { + SupportElementStateBits() : + to_buildplate(false), + to_model_gracious(false), + use_min_xy_dist(false), + supports_roof(false), + can_use_safe_radius(false), + skip_ovalisation(false), + deleted(false), + marked(false) + {} + + /*! + * \brief The element trys to reach the buildplate + */ + bool to_buildplate : 1; + + /*! + * \brief Will the branch be able to rest completely on a flat surface, be it buildplate or model ? + */ + bool to_model_gracious : 1; + + /*! + * \brief Whether the min_xy_distance can be used to get avoidance or similar. Will only be true if support_xy_overrides_z=Z overrides X/Y. + */ + bool use_min_xy_dist : 1; + + /*! + * \brief True if this Element or any parent provides support to a support roof. + */ + bool supports_roof : 1; + + /*! + * \brief An influence area is considered safe when it can use the holefree avoidance <=> It will not have to encounter holes on its way downward. + */ + bool can_use_safe_radius : 1; + + /*! + * \brief Skip the ovalisation to parent and children when generating the final circles. + */ + bool skip_ovalisation : 1; + + // Not valid anymore, to be deleted. + bool deleted : 1; + + // General purpose flag marking a visited element. + bool marked : 1; +}; + +struct SupportElementState : public SupportElementStateBits +{ + int type; + + /*! + * \brief The layer this support elements wants reach + */ + LayerIndex target_height; + + /*! + * \brief The position this support elements wants to support on layer=target_height + */ + Point target_position; + + /*! + * \brief The next position this support elements wants to reach. NOTE: This is mainly a suggestion regarding direction inside the influence area. + */ + Point next_position; + + /*! + * \brief The next height this support elements wants to reach + */ + LayerIndex layer_idx; + + /*! + * \brief The Effective distance to top of this element regarding radius increases and collision calculations. + */ + uint32_t effective_radius_height; + + /*! + * \brief The amount of layers this element is below the topmost layer of this branch. + */ + uint32_t distance_to_top; + + /*! + * \brief The resulting center point around which a circle will be drawn later. + * Will be set by setPointsOnAreas + */ + Point result_on_layer { std::numeric_limits::max(), std::numeric_limits::max() }; + bool result_on_layer_is_set() const { return this->result_on_layer != Point{ std::numeric_limits::max(), std::numeric_limits::max() }; } + void result_on_layer_reset() { this->result_on_layer = Point{ std::numeric_limits::max(), std::numeric_limits::max() }; } + /*! + * \brief The amount of extra radius we got from merging branches that could have reached the buildplate, but merged with ones that can not. + */ + coord_t increased_to_model_radius; // how much to model we increased only relevant for merging + + /*! + * \brief Counter about the times the elephant foot was increased. Can be fractions for merge reasons. + */ + double elephant_foot_increases; + + /*! + * \brief The element trys not to move until this dtt is reached, is set to 0 if the element had to move. + */ + uint32_t dont_move_until; + + /*! + * \brief Settings used to increase the influence area to its current state. + */ + AreaIncreaseSettings last_area_increase; + + /*! + * \brief Amount of roof layers that were not yet added, because the branch needed to move. + */ + uint32_t missing_roof_layers; + + // called by increase_single_area() and increaseAreas() + [[nodiscard]] static SupportElementState propagate_down(const SupportElementState &src) + { + SupportElementState dst{ src }; + ++ dst.distance_to_top; + -- dst.layer_idx; + // set to invalid as we are a new node on a new layer + dst.result_on_layer_reset(); + dst.skip_ovalisation = false; + return dst; + } +}; + +struct SupportElement +{ + using ParentIndices = +#ifdef NDEBUG + // To reduce memory allocation in release mode. + boost::container::small_vector; +#else // NDEBUG + // To ease debugging. + std::vector; +#endif // NDEBUG + +// SupportElement(const SupportElementState &state) : SupportElementState(state) {} + SupportElement(const SupportElementState &state, Polygons &&influence_area) : state(state), influence_area(std::move(influence_area)) {} + SupportElement(const SupportElementState &state, ParentIndices &&parents, Polygons &&influence_area) : + state(state), parents(std::move(parents)), influence_area(std::move(influence_area)) {} + + SupportElementState state; + + /*! + * \brief All elements in the layer above the current one that are supported by this element + */ + ParentIndices parents; + + /*! + * \brief The resulting influence area. + * Will only be set in the results of createLayerPathing, and will be nullptr inside! + */ + Polygons influence_area; +}; + +/*! + * \brief This struct contains settings used in the tree support. Thanks to this most functions do not need to know of meshes etc. Also makes the code shorter. + */ +struct TreeSupportSettings +{ + TreeSupportSettings() = default; // required for the definition of the config variable in the TreeSupportGenerator class. + + explicit TreeSupportSettings(const TreeSupportMeshGroupSettings& mesh_group_settings) + : angle(mesh_group_settings.support_tree_angle), + angle_slow(mesh_group_settings.support_tree_angle_slow), + support_line_width(mesh_group_settings.support_line_width), + layer_height(mesh_group_settings.layer_height), + branch_radius(mesh_group_settings.support_tree_branch_diameter / 2), + min_radius(mesh_group_settings.support_tree_tip_diameter / 2), // The actual radius is 50 microns larger as the resulting branches will be increased by 50 microns to avoid rounding errors effectively increasing the xydistance + maximum_move_distance((angle < M_PI / 2.) ? (coord_t)(tan(angle) * layer_height) : std::numeric_limits::max()), + maximum_move_distance_slow((angle_slow < M_PI / 2.) ? (coord_t)(tan(angle_slow) * layer_height) : std::numeric_limits::max()), + support_bottom_layers(mesh_group_settings.support_bottom_enable ? (mesh_group_settings.support_bottom_height + layer_height / 2) / layer_height : 0), + tip_layers(std::max((branch_radius - min_radius) / (support_line_width / 3), branch_radius / layer_height)), // Ensure lines always stack nicely even if layer height is large + diameter_angle_scale_factor(sin(mesh_group_settings.support_tree_branch_diameter_angle) * layer_height / branch_radius), + max_to_model_radius_increase(mesh_group_settings.support_tree_max_diameter_increase_by_merges_when_support_to_model / 2), + min_dtt_to_model(round_up_divide(mesh_group_settings.support_tree_min_height_to_model, layer_height)), + increase_radius_until_radius(mesh_group_settings.support_tree_branch_diameter / 2), + increase_radius_until_layer(increase_radius_until_radius <= branch_radius ? tip_layers * (increase_radius_until_radius / branch_radius) : (increase_radius_until_radius - branch_radius) / (branch_radius * diameter_angle_scale_factor)), + support_rests_on_model(! mesh_group_settings.support_material_buildplate_only), + xy_distance(mesh_group_settings.support_xy_distance), + xy_min_distance(std::min(mesh_group_settings.support_xy_distance, mesh_group_settings.support_xy_distance_overhang)), + bp_radius(mesh_group_settings.support_tree_bp_diameter / 2), + diameter_scale_bp_radius(std::min(sin(0.7) * layer_height / branch_radius, 1.0 / (branch_radius / (support_line_width / 2.0)))), // Either 40? or as much as possible so that 2 lines will overlap by at least 50%, whichever is smaller. + z_distance_top_layers(round_up_divide(mesh_group_settings.support_top_distance, layer_height)), + z_distance_bottom_layers(round_up_divide(mesh_group_settings.support_bottom_distance, layer_height)), + performance_interface_skip_layers(round_up_divide(mesh_group_settings.support_interface_skip_height, layer_height)), +// support_infill_angles(mesh_group_settings.support_infill_angles), + support_roof_angles(mesh_group_settings.support_roof_angles), + roof_pattern(mesh_group_settings.support_roof_pattern), + support_pattern(mesh_group_settings.support_pattern), + support_roof_line_width(mesh_group_settings.support_roof_line_width), + support_line_spacing(mesh_group_settings.support_line_spacing), + support_bottom_offset(mesh_group_settings.support_bottom_offset), + support_wall_count(mesh_group_settings.support_wall_count), + resolution(mesh_group_settings.resolution), + support_roof_line_distance(mesh_group_settings.support_roof_line_distance), // in the end the actual infill has to be calculated to subtract interface from support areas according to interface_preference. + settings(mesh_group_settings), + min_feature_size(mesh_group_settings.min_feature_size) + + + { + layer_start_bp_radius = (bp_radius - branch_radius) / (branch_radius * diameter_scale_bp_radius); + + if (TreeSupportSettings::soluble) { + // safeOffsetInc can only work in steps of the size xy_min_distance in the worst case => xy_min_distance has to be a bit larger than 0 in this worst case and should be large enough for performance to not suffer extremely + // When for all meshes the z bottom and top distance is more than one layer though the worst case is xy_min_distance + min_feature_size + // This is not the best solution, but the only one to ensure areas can not lag though walls at high maximum_move_distance. + xy_min_distance = std::max(xy_min_distance, scaled(0.1)); + xy_distance = std::max(xy_distance, xy_min_distance); + } + + +// const std::unordered_map interface_map = { { "support_area_overwrite_interface_area", InterfacePreference::SupportAreaOverwritesInterface }, { "interface_area_overwrite_support_area", InterfacePreference::InterfaceAreaOverwritesSupport }, { "support_lines_overwrite_interface_area", InterfacePreference::SupportLinesOverwriteInterface }, { "interface_lines_overwrite_support_area", InterfacePreference::InterfaceLinesOverwriteSupport }, { "nothing", InterfacePreference::Nothing } }; +// interface_preference = interface_map.at(mesh_group_settings.get("support_interface_priority")); +//FIXME this was the default +// interface_preference = InterfacePreference::SupportLinesOverwriteInterface; + interface_preference = InterfacePreference::SupportAreaOverwritesInterface; + } + +private: + double angle; + double angle_slow; + std::vector known_z; + +public: + // some static variables dependent on other meshes that are not currently processed. + // Has to be static because TreeSupportConfig will be used in TreeModelVolumes as this reduces redundancy. + inline static bool soluble = false; + /*! + * \brief Width of a single line of support. + */ + coord_t support_line_width; + /*! + * \brief Height of a single layer + */ + coord_t layer_height; + /*! + * \brief Radius of a branch when it has left the tip. + */ + coord_t branch_radius; + /*! + * \brief smallest allowed radius, required to ensure that even at DTT 0 every circle will still be printed + */ + coord_t min_radius; + /*! + * \brief How far an influence area may move outward every layer at most. + */ + coord_t maximum_move_distance; + /*! + * \brief How far every influence area will move outward every layer if possible. + */ + coord_t maximum_move_distance_slow; + /*! + * \brief Amount of bottom layers. 0 if disabled. + */ + size_t support_bottom_layers; + /*! + * \brief Amount of effectiveDTT increases are required to reach branch radius. + */ + size_t tip_layers; + /*! + * \brief Factor by which to increase the branch radius. + */ + double diameter_angle_scale_factor; + /*! + * \brief How much a branch resting on the model may grow in radius by merging with branches that can reach the buildplate. + */ + coord_t max_to_model_radius_increase; + /*! + * \brief If smaller (in layers) than that, all branches to model will be deleted + */ + size_t min_dtt_to_model; + /*! + * \brief Increase radius in the resulting drawn branches, even if the avoidance does not allow it. Will be cut later to still fit. + */ + coord_t increase_radius_until_radius; + /*! + * \brief Same as increase_radius_until_radius, but contains the DTT at which the radius will be reached. + */ + size_t increase_radius_until_layer; + /*! + * \brief True if the branches may connect to the model. + */ + bool support_rests_on_model; + /*! + * \brief How far should support be from the model. + */ + coord_t xy_distance; + /*! + * \brief Radius a branch should have when reaching the buildplate. + */ + coord_t bp_radius; + /*! + * \brief The layer index at which an increase in radius may be required to reach the bp_radius. + */ + coord_t layer_start_bp_radius; + /*! + * \brief Factor by which to increase the branch radius to reach the required bp_radius at layer 0. Note that this radius increase will not happen in the tip, to ensure the tip is structurally sound. + */ + double diameter_scale_bp_radius; + /*! + * \brief minimum xy_distance. Only relevant when Z overrides XY, otherwise equal to xy_distance- + */ + coord_t xy_min_distance; + /*! + * \brief Amount of layers distance required the top of the support to the model + */ + size_t z_distance_top_layers; + /*! + * \brief Amount of layers distance required from the top of the model to the bottom of a support structure. + */ + size_t z_distance_bottom_layers; + /*! + * \brief used for performance optimization at the support floor. Should have no impact on the resulting tree. + */ + size_t performance_interface_skip_layers; + /*! + * \brief User specified angles for the support infill. + */ +// std::vector support_infill_angles; + /*! + * \brief User specified angles for the support roof infill. + */ + std::vector support_roof_angles; + /*! + * \brief Pattern used in the support roof. May contain non relevant data if support roof is disabled. + */ + SupportMaterialInterfacePattern roof_pattern; + /*! + * \brief Pattern used in the support infill. + */ + SupportMaterialPattern support_pattern; + /*! + * \brief Line width of the support roof. + */ + coord_t support_roof_line_width; + /*! + * \brief Distance between support infill lines. + */ + coord_t support_line_spacing; + /*! + * \brief Offset applied to the support floor area. + */ + coord_t support_bottom_offset; + /* + * \brief Amount of walls the support area will have. + */ + int support_wall_count; + /* + * \brief Maximum allowed deviation when simplifying. + */ + coord_t resolution; + /* + * \brief Distance between the lines of the roof. + */ + coord_t support_roof_line_distance; + /* + * \brief How overlaps of an interface area with a support area should be handled. + */ + InterfacePreference interface_preference; + + /* + * \brief The infill class wants a settings object. This one will be the correct one for all settings it uses. + */ + TreeSupportMeshGroupSettings settings; + + /* + * \brief Minimum thickness of any model features. + */ + coord_t min_feature_size; + + public: + bool operator==(const TreeSupportSettings& other) const + { + return branch_radius == other.branch_radius && tip_layers == other.tip_layers && diameter_angle_scale_factor == other.diameter_angle_scale_factor && layer_start_bp_radius == other.layer_start_bp_radius && bp_radius == other.bp_radius && diameter_scale_bp_radius == other.diameter_scale_bp_radius && min_radius == other.min_radius && xy_min_distance == other.xy_min_distance && // as a recalculation of the collision areas is required to set a new min_radius. + xy_distance - xy_min_distance == other.xy_distance - other.xy_min_distance && // if the delta of xy_min_distance and xy_distance is different the collision areas have to be recalculated. + support_rests_on_model == other.support_rests_on_model && increase_radius_until_layer == other.increase_radius_until_layer && min_dtt_to_model == other.min_dtt_to_model && max_to_model_radius_increase == other.max_to_model_radius_increase && maximum_move_distance == other.maximum_move_distance && maximum_move_distance_slow == other.maximum_move_distance_slow && z_distance_bottom_layers == other.z_distance_bottom_layers && support_line_width == other.support_line_width && + support_line_spacing == other.support_line_spacing && support_roof_line_width == other.support_roof_line_width && // can not be set on a per-mesh basis currently, so code to enable processing different roof line width in the same iteration seems useless. + support_bottom_offset == other.support_bottom_offset && support_wall_count == other.support_wall_count && support_pattern == other.support_pattern && roof_pattern == other.roof_pattern && // can not be set on a per-mesh basis currently, so code to enable processing different roof patterns in the same iteration seems useless. + support_roof_angles == other.support_roof_angles && + //support_infill_angles == other.support_infill_angles && + increase_radius_until_radius == other.increase_radius_until_radius && support_bottom_layers == other.support_bottom_layers && layer_height == other.layer_height && z_distance_top_layers == other.z_distance_top_layers && resolution == other.resolution && // Infill generation depends on deviation and resolution. + support_roof_line_distance == other.support_roof_line_distance && interface_preference == other.interface_preference + && min_feature_size == other.min_feature_size // interface_preference should be identical to ensure the tree will correctly interact with the roof. + // The infill class now wants the settings object and reads a lot of settings, and as the infill class is used to calculate support roof lines for interface-preference. Not all of these may be required to be identical, but as I am not sure, better safe than sorry +#if 0 + && (interface_preference == InterfacePreference::InterfaceAreaOverwritesSupport || interface_preference == InterfacePreference::SupportAreaOverwritesInterface + // Perimeter generator parameters + || + (settings.get("fill_outline_gaps") == other.settings.get("fill_outline_gaps") && + settings.get("min_bead_width") == other.settings.get("min_bead_width") && + settings.get("wall_transition_angle") == other.settings.get("wall_transition_angle") && + settings.get("wall_transition_length") == other.settings.get("wall_transition_length") && + settings.get("wall_split_middle_threshold") == other.settings.get("wall_split_middle_threshold") && + settings.get("wall_add_middle_threshold") == other.settings.get("wall_add_middle_threshold") && + settings.get("wall_distribution_count") == other.settings.get("wall_distribution_count") && + settings.get("wall_transition_filter_distance") == other.settings.get("wall_transition_filter_distance") && + settings.get("wall_transition_filter_deviation") == other.settings.get("wall_transition_filter_deviation") && + settings.get("wall_line_width_x") == other.settings.get("wall_line_width_x") && + settings.get("meshfix_maximum_extrusion_area_deviation") == other.settings.get("meshfix_maximum_extrusion_area_deviation")) + ) +#endif + ; + } + + /*! + * \brief Get the Distance to top regarding the real radius this part will have. This is different from distance_to_top, which is can be used to calculate the top most layer of the branch. + * \param elem[in] The SupportElement one wants to know the effectiveDTT + * \return The Effective DTT. + */ + [[nodiscard]] inline size_t getEffectiveDTT(const SupportElementState &elem) const + { + return elem.effective_radius_height < increase_radius_until_layer ? (elem.distance_to_top < increase_radius_until_layer ? elem.distance_to_top : increase_radius_until_layer) : elem.effective_radius_height; + } + + /*! + * \brief Get the Radius part will have based on numeric values. + * \param distance_to_top[in] The effective distance_to_top of the element + * \param elephant_foot_increases[in] The elephant_foot_increases of the element. + * \return The radius an element with these attributes would have. + */ + [[nodiscard]] inline coord_t getRadius(size_t distance_to_top, const double elephant_foot_increases = 0) const + { + return (distance_to_top <= tip_layers ? min_radius + (branch_radius - min_radius) * distance_to_top / tip_layers : // tip + branch_radius + // base + branch_radius * (distance_to_top - tip_layers) * diameter_angle_scale_factor) + + // gradual increase + branch_radius * elephant_foot_increases * (std::max(diameter_scale_bp_radius - diameter_angle_scale_factor, 0.0)); + } + + /*! + * \brief Get the Radius, that this element will have. + * \param elem[in] The Element. + * \return The radius the element has. + */ + [[nodiscard]] inline coord_t getRadius(const SupportElementState &elem) const + { return getRadius(getEffectiveDTT(elem), elem.elephant_foot_increases); } + [[nodiscard]] inline coord_t getRadius(const SupportElement &elem) const + { return this->getRadius(elem.state); } + + /*! + * \brief Get the collision Radius of this Element. This can be smaller then the actual radius, as the drawAreas will cut off areas that may collide with the model. + * \param elem[in] The Element. + * \return The collision radius the element has. + */ + [[nodiscard]] inline coord_t getCollisionRadius(const SupportElementState &elem) const + { + return getRadius(elem.effective_radius_height, elem.elephant_foot_increases); + } + + /*! + * \brief Get the Radius an element should at least have at a given layer. + * \param layer_idx[in] The layer. + * \return The radius every element should aim to achieve. + */ + [[nodiscard]] inline coord_t recommendedMinRadius(LayerIndex layer_idx) const + { + double scale = (layer_start_bp_radius - int(layer_idx)) * diameter_scale_bp_radius; + return scale > 0 ? branch_radius + branch_radius * scale : 0; + } + + /*! + * \brief Return on which z in microns the layer will be printed. Used only for support infill line generation. + * \param layer_idx[in] The layer. + * \return The radius every element should aim to achieve. + */ + [[nodiscard]] inline coord_t getActualZ(LayerIndex layer_idx) + { + return layer_idx < coord_t(known_z.size()) ? known_z[layer_idx] : (layer_idx - known_z.size()) * layer_height + known_z.size() ? known_z.back() : 0; + } + + /*! + * \brief Set the z every Layer is printed at. Required for getActualZ to work + * \param z[in] The z every LayerIndex is printed. Vector is used as a map with the index of each element being the corresponding LayerIndex + * \return The radius every element should aim to achieve. + */ + void setActualZ(std::vector& z) + { + known_z = z; + } +}; + +void tree_supports_show_error(std::string_view message, bool critical); + +using SupportElements = std::deque; +void create_layer_pathing(const TreeModelVolumes& volumes, const TreeSupportSettings& config, std::vector& move_bounds, std::function throw_on_cancel); + +void create_nodes_from_area(const TreeModelVolumes& volumes, const TreeSupportSettings& config, std::vector& move_bounds, std::function throw_on_cancel); + +indexed_triangle_set draw_branches(PrintObject& print_object, const TreeModelVolumes& volumes, const TreeSupportSettings& config, std::vector& move_bounds, std::function throw_on_cancel); + +void slice_branches(PrintObject& print_object, const TreeModelVolumes& volumes, const TreeSupportSettings& config, const std::vector& overhangs, std::vector& move_bounds, const indexed_triangle_set& cummulative_mesh, SupportGeneratorLayersPtr& bottom_contacts, SupportGeneratorLayersPtr& top_contacts, SupportGeneratorLayersPtr& intermediate_layers, SupportGeneratorLayerStorage& layer_storage, std::function throw_on_cancel); + +} // namespace TreeSupport3D + +void generate_tree_support_3D(PrintObject &print_object, TreeSupport* tree_support, std::function throw_on_cancel = []{}); + +} // namespace Slic3r + +#endif /* slic3r_TreeSupport_hpp */ diff --git a/src/libslic3r/Utils.hpp b/src/libslic3r/Utils.hpp index 3ad5836d3..2e77a9b57 100644 --- a/src/libslic3r/Utils.hpp +++ b/src/libslic3r/Utils.hpp @@ -114,7 +114,16 @@ inline std::string convert_to_full_version(std::string short_version) } return result; } - +template +inline DataType round_divide(DataType dividend, DataType divisor) //!< Return dividend divided by divisor rounded to the nearest integer +{ + return (dividend + divisor / 2) / divisor; +} +template +inline DataType round_up_divide(DataType dividend, DataType divisor) //!< Return dividend divided by divisor rounded to the nearest integer +{ + return (dividend + divisor - 1) / divisor; +} // Set a path with GUI localization files. diff --git a/src/slic3r/GUI/ConfigManipulation.cpp b/src/slic3r/GUI/ConfigManipulation.cpp index c2c5045d1..1df058880 100644 --- a/src/slic3r/GUI/ConfigManipulation.cpp +++ b/src/slic3r/GUI/ConfigManipulation.cpp @@ -428,8 +428,8 @@ void ConfigManipulation::update_print_fff_config(DynamicPrintConfig* config, con if (config->opt_bool("enable_support")) { auto support_type = config->opt_enum("support_type"); auto support_style = config->opt_enum("support_style"); - std::set enum_set_normal = {0, 1, 2}; - std::set enum_set_tree = {0, 3, 4, 5}; + std::set enum_set_normal = { smsDefault, smsGrid, smsSnug }; + std::set enum_set_tree = { smsDefault, smsTreeSlim, smsTreeStrong, smsTreeHybrid, smsTreeOrganic }; auto & set = is_tree(support_type) ? enum_set_tree : enum_set_normal; if (set.find(support_style) == set.end()) { DynamicPrintConfig new_conf = *config; diff --git a/src/slic3r/GUI/Tab.cpp b/src/slic3r/GUI/Tab.cpp index 2ace223c4..ebfdb5e41 100644 --- a/src/slic3r/GUI/Tab.cpp +++ b/src/slic3r/GUI/Tab.cpp @@ -2125,8 +2125,8 @@ void TabPrint::toggle_options() auto support_type = m_config->opt_enum("support_type"); if (auto choice = dynamic_cast(field)) { auto def = print_config_def.get("support_style"); - std::vector enum_set_normal = {0, 1, 2}; - std::vector enum_set_tree = {0, 3, 4, 5}; + std::vector enum_set_normal = {smsDefault, smsGrid, smsSnug }; + std::vector enum_set_tree = { smsDefault, smsTreeSlim, smsTreeStrong, smsTreeHybrid, smsTreeOrganic }; auto & set = is_tree(support_type) ? enum_set_tree : enum_set_normal; auto & opt = const_cast(field->m_opt); auto cb = dynamic_cast(choice->window);