From c09781d61d809143b13cdf8567cafdb6d1bc25b4 Mon Sep 17 00:00:00 2001 From: PavelMikus Date: Mon, 6 Jun 2022 17:30:02 +0200 Subject: [PATCH] optimize embedding computation fix seed of random generators set high angle importance for nearest mode --- src/libslic3r/GCode/SeamPlacer.cpp | 23 +++++++++++++++-------- src/libslic3r/GCode/SeamPlacer.hpp | 3 ++- src/libslic3r/ShortEdgeCollapse.cpp | 2 +- src/libslic3r/TriangleSetSampling.cpp | 2 +- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/libslic3r/GCode/SeamPlacer.cpp b/src/libslic3r/GCode/SeamPlacer.cpp index 2a7448d8f..f2d386c05 100644 --- a/src/libslic3r/GCode/SeamPlacer.cpp +++ b/src/libslic3r/GCode/SeamPlacer.cpp @@ -687,8 +687,10 @@ void gather_enforcers_blockers(GlobalModelInfo &result, const PrintObject *po) { struct SeamComparator { SeamPosition setup; + float angle_importance; SeamComparator(SeamPosition setup) : setup(setup) { + angle_importance = setup == spNearest ? SeamPlacer::angle_importance_nearest : SeamPlacer::angle_importance_aligned; } // Standard comparator, must respect the requirements of comparators (e.g. give same result on same inputs) for sorting usage @@ -734,10 +736,10 @@ struct SeamComparator { // the penalites are kept close to range [0-1.x] however, it should not be relied upon float penalty_a = a.visibility + - SeamPlacer::angle_importance * compute_angle_penalty(a.local_ccw_angle) + angle_importance * compute_angle_penalty(a.local_ccw_angle) + distance_penalty_a; float penalty_b = b.visibility + - SeamPlacer::angle_importance * compute_angle_penalty(b.local_ccw_angle) + angle_importance * compute_angle_penalty(b.local_ccw_angle) + distance_penalty_b; return penalty_a < penalty_b; @@ -791,9 +793,9 @@ struct SeamComparator { } float penalty_a = a.visibility - + SeamPlacer::angle_importance * compute_angle_penalty(a.local_ccw_angle); + + angle_importance * compute_angle_penalty(a.local_ccw_angle); float penalty_b = b.visibility + - SeamPlacer::angle_importance * compute_angle_penalty(b.local_ccw_angle); + angle_importance * compute_angle_penalty(b.local_ccw_angle); return penalty_a <= penalty_b || penalty_a - penalty_b < SeamPlacer::seam_align_score_tolerance; } @@ -1071,9 +1073,14 @@ void SeamPlacer::calculate_overhangs_and_layer_embedding(const PrintObject *po) } for (size_t layer_idx = r.begin(); layer_idx < r.end(); ++layer_idx) { - bool layer_has_multiple_loops = - layers[layer_idx].points[0].perimeter.end_index - < layers[layer_idx].points.size() - 1; + size_t regions_with_perimeter = 0; + for (const LayerRegion *region : po->layers()[layer_idx]->regions()) { + if (region->perimeters.entities.size() > 0) { + regions_with_perimeter++; + } + }; + bool should_compute_layer_embedding = regions_with_perimeter > 1; + layers[layer_idx].points[0].perimeter.end_index < layers[layer_idx].points.size() - 1; std::unique_ptr current_layer_distancer = std::make_unique(po->layers()[layer_idx]); for (SeamCandidate &perimeter_point : layers[layer_idx].points) { @@ -1082,7 +1089,7 @@ void SeamPlacer::calculate_overhangs_and_layer_embedding(const PrintObject *po) perimeter_point.overhang = prev_layer_distancer->distance_from_perimeter(point); } - if (layer_has_multiple_loops) { // search for embedded perimeter points (points hidden inside the print ,e.g. multimaterial join, best position for seam) + if (should_compute_layer_embedding) { // search for embedded perimeter points (points hidden inside the print ,e.g. multimaterial join, best position for seam) perimeter_point.embedded_distance = current_layer_distancer->distance_from_perimeter(point); } } diff --git a/src/libslic3r/GCode/SeamPlacer.hpp b/src/libslic3r/GCode/SeamPlacer.hpp index 08974bf5f..e297497d1 100644 --- a/src/libslic3r/GCode/SeamPlacer.hpp +++ b/src/libslic3r/GCode/SeamPlacer.hpp @@ -133,7 +133,8 @@ public: 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.6f; + static constexpr float angle_importance_aligned = 0.6f; + static constexpr float angle_importance_nearest = 2.0f; // use much higher angle importance for nearest mode, to combat the visiblity info noise // 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; diff --git a/src/libslic3r/ShortEdgeCollapse.cpp b/src/libslic3r/ShortEdgeCollapse.cpp index ac55e2605..b36278c37 100644 --- a/src/libslic3r/ShortEdgeCollapse.cpp +++ b/src/libslic3r/ShortEdgeCollapse.cpp @@ -79,7 +79,7 @@ void its_short_edge_collpase(indexed_triangle_set &mesh, size_t target_triangle_ } }; - std::mt19937_64 generator { }; // default constant seed! so that results are deterministic + std::mt19937_64 generator { 27644437 };// default constant seed! so that results are deterministic std::vector face_indices(mesh.indices.size()); for (size_t idx = 0; idx < face_indices.size(); ++idx) { face_indices[idx] = idx; diff --git a/src/libslic3r/TriangleSetSampling.cpp b/src/libslic3r/TriangleSetSampling.cpp index 30f9a9b40..bb03ff6d7 100644 --- a/src/libslic3r/TriangleSetSampling.cpp +++ b/src/libslic3r/TriangleSetSampling.cpp @@ -28,7 +28,7 @@ TriangleSetSamples sample_its_uniform_parallel(size_t samples_count, const index area_sum_to_triangle_idx[area_sum] = t_idx; } - std::mt19937_64 mersenne_engine { }; + std::mt19937_64 mersenne_engine { 27644437 }; // random numbers on interval [0, 1) std::uniform_real_distribution fdistribution;