From f790468ccaced792ffd664a76f2282b8d5be438a Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Thu, 1 Sep 2022 15:47:13 +0200 Subject: [PATCH] WIP TreeSupports: Extracting make_circle() into Polygon.cpp,hpp --- src/libslic3r/Polygon.cpp | 19 +++++++++++++++++++ src/libslic3r/Polygon.hpp | 3 +++ src/libslic3r/TreeSupport.cpp | 18 +++++------------- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/libslic3r/Polygon.cpp b/src/libslic3r/Polygon.cpp index 1cfb16eb2..bf0abd4fa 100644 --- a/src/libslic3r/Polygon.cpp +++ b/src/libslic3r/Polygon.cpp @@ -528,4 +528,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; +} + } diff --git a/src/libslic3r/Polygon.hpp b/src/libslic3r/Polygon.hpp index e754e1d5e..12d457c37 100644 --- a/src/libslic3r/Polygon.hpp +++ b/src/libslic3r/Polygon.hpp @@ -253,6 +253,9 @@ inline Polygons to_polygons(std::vector &&paths) // Returns true if inside. Returns border_result if on boundary. bool contains(const Polygons& polygons, const Point& p, bool border_result = true); +Polygon make_circle(double radius, double error); +Polygon make_circle_num_segments(double radius, size_t num_segments); + } // Slic3r // start Boost diff --git a/src/libslic3r/TreeSupport.cpp b/src/libslic3r/TreeSupport.cpp index 01aec24a1..56ccfd7d1 100644 --- a/src/libslic3r/TreeSupport.cpp +++ b/src/libslic3r/TreeSupport.cpp @@ -1066,12 +1066,8 @@ void TreeSupport::generateInitialAreas( SupportGeneratorLayersPtr &top_interface_layers, SupportGeneratorLayerStorage &layer_storage) { - Polygon base_circle; - const auto base_radius = scaled(0.01); - for (unsigned int i = 0; i < SUPPORT_TREE_CIRCLE_RESOLUTION; ++ i) { - const double angle = static_cast(i) / SUPPORT_TREE_CIRCLE_RESOLUTION * (2.0 * M_PI); - base_circle.points.emplace_back(coord_t(cos(angle) * base_radius), coord_t(sin(angle) * base_radius)); - } + 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); @@ -1117,7 +1113,7 @@ void TreeSupport::generateInitialAreas( [this, &print_object, &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, &base_radius](const tbb::blocked_range &range) { + &move_bounds](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; @@ -2405,12 +2401,8 @@ void TreeSupport::generateBranchAreas( constexpr int progress_report_steps = 10; #endif // SLIC3R_TREESUPPORTS_PROGRESS - Polygon branch_circle; // Pre-generate a circle with correct diameter so that we don't have to recompute those (co)sines every time. - for (unsigned int i = 0; i < SUPPORT_TREE_CIRCLE_RESOLUTION; ++ i) { - const double angle = static_cast(i) / SUPPORT_TREE_CIRCLE_RESOLUTION * (2.0 * M_PI); - branch_circle.points.emplace_back(coord_t(cos(angle) * m_config.branch_radius), coord_t(sin(angle) * m_config.branch_radius)); - } - + // 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(m_config.branch_radius, SUPPORT_TREE_CIRCLE_RESOLUTION); std::vector linear_inserts(linear_data.size()); #ifdef SLIC3R_TREESUPPORTS_PROGRESS