From b5b39195f4caeb468572906b859d7e3490454a7b Mon Sep 17 00:00:00 2001 From: PavelMikus Date: Fri, 27 May 2022 14:16:55 +0200 Subject: [PATCH] Added throw_if_canceled callback to all slower sections --- src/libslic3r/GCode.cpp | 3 ++- src/libslic3r/GCode/SeamPlacer.cpp | 37 +++++++++++++++++++----------- src/libslic3r/GCode/SeamPlacer.hpp | 8 +++---- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index d910781645..cb14668fb2 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -1347,7 +1347,8 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato print.throw_if_canceled(); // Collect custom seam data from all objects. - m_seam_placer.init(print); + std::function throw_if_canceled_func = [&print]() { print.throw_if_canceled();}; + m_seam_placer.init(print, throw_if_canceled_func); if (! (has_wipe_tower && print.config().single_extruder_multi_material_priming)) { // Set initial extruder only after custom start G-code. diff --git a/src/libslic3r/GCode/SeamPlacer.cpp b/src/libslic3r/GCode/SeamPlacer.cpp index 6def921dec..b28ea92ba7 100644 --- a/src/libslic3r/GCode/SeamPlacer.cpp +++ b/src/libslic3r/GCode/SeamPlacer.cpp @@ -17,6 +17,7 @@ #include "libslic3r/Layer.hpp" #include "libslic3r/Geometry/Curves.hpp" +#include "libslic3r/QuadricEdgeCollapse.hpp" #include "libslic3r/TriangleSetSampling.hpp" #include "libslic3r/Utils.hpp" @@ -584,7 +585,7 @@ std::pair find_previous_and_next_perimeter_point(const std::vect } // Computes all global model info - transforms object, performs raycasting -void compute_global_occlusion(GlobalModelInfo &result, const PrintObject *po) { +void compute_global_occlusion(GlobalModelInfo &result, const PrintObject *po, std::function throw_if_canceled) { BOOST_LOG_TRIVIAL(debug) << "SeamPlacer: gather occlusion meshes: start"; auto obj_transform = po->trafo_centered(); @@ -604,6 +605,7 @@ void compute_global_occlusion(GlobalModelInfo &result, const PrintObject *po) { } } } + throw_if_canceled(); size_t negative_volumes_start_index = triangle_set.indices.size(); its_merge(triangle_set, negative_volumes_set); @@ -613,7 +615,15 @@ void compute_global_occlusion(GlobalModelInfo &result, const PrintObject *po) { << "SeamPlacer: gather occlusion meshes: end"; BOOST_LOG_TRIVIAL(debug) - << "SeamPlacer: Compute visiblity sample points: start"; + << "SeamPlacer: decimate: start"; + float target_error = 1.0f; + its_quadric_edge_collapse(triangle_set, 0, &target_error, throw_if_canceled); + BOOST_LOG_TRIVIAL(debug) + << "SeamPlacer: decimate: end"; + + + BOOST_LOG_TRIVIAL(debug) + << "SeamPlacer: Compute visibility sample points: start"; result.mesh_samples = sample_its_uniform_parallel(SeamPlacer::raycasting_visibility_samples_count, triangle_set); @@ -625,7 +635,7 @@ void compute_global_occlusion(GlobalModelInfo &result, const PrintObject *po) { BOOST_LOG_TRIVIAL(debug) << "SeamPlacer: Compute visiblity sample points: end"; - + throw_if_canceled(); BOOST_LOG_TRIVIAL(debug) << "SeamPlacer: Mesh sample raidus: " << result.mesh_samples_radius; @@ -635,11 +645,12 @@ void compute_global_occlusion(GlobalModelInfo &result, const PrintObject *po) { auto raycasting_tree = AABBTreeIndirect::build_aabb_tree_over_indexed_triangle_set(triangle_set.vertices, triangle_set.indices); + throw_if_canceled(); BOOST_LOG_TRIVIAL(debug) << "SeamPlacer: build AABB tree: end"; result.mesh_samples_visibility = raycast_visibility(raycasting_tree, triangle_set, result.mesh_samples, negative_volumes_start_index); - + throw_if_canceled(); #ifdef DEBUG_FILES result.debug_export(triangle_set); #endif @@ -1398,29 +1409,29 @@ void SeamPlacer::align_seam_points(const PrintObject *po, const SeamPlacerImpl:: } -void SeamPlacer::init(const Print &print) { +void SeamPlacer::init(const Print &print, std::function throw_if_canceled_func) { using namespace SeamPlacerImpl; m_seam_per_object.clear(); for (const PrintObject *po : print.objects()) { - + throw_if_canceled_func(); SeamPosition configured_seam_preference = po->config().seam_position.value; SeamComparator comparator { configured_seam_preference }; { GlobalModelInfo global_model_info { }; gather_enforcers_blockers(global_model_info, po); - + throw_if_canceled_func(); if (configured_seam_preference == spAligned || configured_seam_preference == spNearest) { - compute_global_occlusion(global_model_info, po); + compute_global_occlusion(global_model_info, po, throw_if_canceled_func); } - + throw_if_canceled_func(); BOOST_LOG_TRIVIAL(debug) << "SeamPlacer: gather_seam_candidates: start"; gather_seam_candidates(po, global_model_info, configured_seam_preference); BOOST_LOG_TRIVIAL(debug) << "SeamPlacer: gather_seam_candidates: end"; - + throw_if_canceled_func(); if (configured_seam_preference == spAligned || configured_seam_preference == spNearest) { BOOST_LOG_TRIVIAL(debug) << "SeamPlacer: calculate_candidates_visibility : start"; @@ -1429,13 +1440,13 @@ void SeamPlacer::init(const Print &print) { << "SeamPlacer: calculate_candidates_visibility : end"; } } // destruction of global_model_info (large structure, no longer needed) - + throw_if_canceled_func(); BOOST_LOG_TRIVIAL(debug) << "SeamPlacer: calculate_overhangs and layer embdedding : start"; calculate_overhangs_and_layer_embedding(po); BOOST_LOG_TRIVIAL(debug) << "SeamPlacer: calculate_overhangs and layer embdedding: end"; - + throw_if_canceled_func(); if (configured_seam_preference != spNearest) { // For spNearest, the seam is picked in the place_seam method with actual nozzle position information BOOST_LOG_TRIVIAL(debug) << "SeamPlacer: pick_seam_point : start"; @@ -1456,7 +1467,7 @@ void SeamPlacer::init(const Print &print) { BOOST_LOG_TRIVIAL(debug) << "SeamPlacer: pick_seam_point : end"; } - + throw_if_canceled_func(); if (configured_seam_preference == spAligned) { BOOST_LOG_TRIVIAL(debug) << "SeamPlacer: align_seam_points : start"; diff --git a/src/libslic3r/GCode/SeamPlacer.hpp b/src/libslic3r/GCode/SeamPlacer.hpp index 0a38e9a663..de7e2f67ca 100644 --- a/src/libslic3r/GCode/SeamPlacer.hpp +++ b/src/libslic3r/GCode/SeamPlacer.hpp @@ -129,13 +129,11 @@ public: // arm length used during angles computation static constexpr float polygon_local_angles_arm_distance = 0.3f; - // max tolerable distance from the previous layer is overhang_distance_tolerance_factor * flow_width static constexpr float overhang_distance_tolerance_factor = 0.5f; - // determines angle importance compared to visibility ( neutral value is 1.0f. ) - static constexpr float angle_importance = 0.5f; + static constexpr float angle_importance = 0.6f; // If enforcer or blocker is closer to the seam candidate than this limit, the seam candidate is set to Blocker or Enforcer static constexpr float enforcer_blocker_distance_tolerance = 0.35f; @@ -144,7 +142,7 @@ public: // When searching for seam clusters for alignment: // following value describes, how much worse score can point have and still be picked into seam cluster instead of original seam point on the same layer - static constexpr float seam_align_score_tolerance = 0.3f; + static constexpr float seam_align_score_tolerance = 0.27f; // seam_align_tolerable_dist - if next layer closes point is too far away, break string static constexpr float seam_align_tolerable_dist = 1.0f; // if the seam of the current layer is too far away, and the closest seam candidate is not very good, layer is skipped. @@ -158,7 +156,7 @@ public: //The following data structures hold all perimeter points for all PrintObject. std::unordered_map m_seam_per_object; - void init(const Print &print); + void init(const Print &print, std::function throw_if_canceled_func); void place_seam(const Layer *layer, ExtrusionLoop &loop, bool external_first, const Point &last_pos) const;