From 60e8163900c3a94ea9fc62f49b8998554af5c716 Mon Sep 17 00:00:00 2001 From: Filip Sykala - NTB T15p Date: Fri, 21 Feb 2025 09:16:00 +0100 Subject: [PATCH] Hide factory include into cpp file + remove unused minimal island area --- src/libslic3r/SLA/SupportPointGenerator.cpp | 138 +++++++++++++------- src/libslic3r/SLA/SupportPointGenerator.hpp | 16 +-- 2 files changed, 96 insertions(+), 58 deletions(-) diff --git a/src/libslic3r/SLA/SupportPointGenerator.cpp b/src/libslic3r/SLA/SupportPointGenerator.cpp index bb343216a0..356172b932 100644 --- a/src/libslic3r/SLA/SupportPointGenerator.cpp +++ b/src/libslic3r/SLA/SupportPointGenerator.cpp @@ -10,8 +10,10 @@ #include "libslic3r/KDTreeIndirect.hpp" #include "libslic3r/ClipperUtils.hpp" #include "libslic3r/AABBTreeLines.hpp" // closest point to layer part +#include "libslic3r/AABBMesh.hpp" // move_on_mesh_surface Should be in another file // SupportIslands #include "libslic3r/SLA/SupportIslands/UniformSupportIsland.hpp" +#include "libslic3r/SLA/SupportIslands/SampleConfigFactory.hpp" using namespace Slic3r; using namespace Slic3r::sla; @@ -798,6 +800,7 @@ SupportPointGeneratorData Slic3r::sla::prepare_generator_data( return result; } +#ifdef USE_ISLAND_GUI_FOR_SETTINGS #include "libslic3r/NSVGUtils.hpp" #include "libslic3r/Utils.hpp" std::vector load_curve_from_file() { @@ -837,6 +840,7 @@ std::vector load_curve_from_file() { assert(false); return {}; } +#endif // USE_ISLAND_GUI_FOR_SETTINGS // Processing permanent support points // Permanent are manualy edited points by user @@ -1165,21 +1169,42 @@ Points get_permanents(const PermanentSupports &supports, size_t support_index, } // namespace -LayerSupportPoints Slic3r::sla::generate_support_points( +namespace Slic3r::sla { +using namespace Slic3r; + +std::vector create_default_support_curve(){ +#ifdef USE_ISLAND_GUI_FOR_SETTINGS + return {}; +#else // USE_ISLAND_GUI_FOR_SETTINGS + return std::vector{ + Vec2f{3.2f, 0.f}, + Vec2f{4.f, 3.9f}, + Vec2f{5.f, 15.f}, + Vec2f{6.f, 40.f}, + }; +#endif // USE_ISLAND_GUI_FOR_SETTINGS +} + +SampleConfig create_default_island_configuration(float head_diameter_in_mm) { + return SampleConfigFactory::create(head_diameter_in_mm); +} + +LayerSupportPoints generate_support_points( const SupportPointGeneratorData &data, const SupportPointGeneratorConfig &config, ThrowOnCancel throw_on_cancel, StatusFunction statusfn -){ +) { const Layers &layers = data.layers; double increment = 100.0 / static_cast(layers.size()); double status = 0; // current progress - int status_int = 0; - + int status_int = 0; +#ifdef USE_ISLAND_GUI_FOR_SETTINGS // Hack to set curve for testing if (config.support_curve.empty()) const_cast(config).support_curve = load_curve_from_file(); - +#endif // USE_ISLAND_GUI_FOR_SETTINGS + // Maximal radius of supported area of one support point double max_support_radius = config.support_curve.back().x(); // check distance to nearest support points from grid @@ -1205,11 +1230,15 @@ LayerSupportPoints Slic3r::sla::generate_support_points( for (const LayerPart &part : layer.parts) { size_t part_id = &part - &layer.parts.front(); - if (part.prev_parts.empty()) { // Island ? + if (part.prev_parts.empty()) { // Island ? grids.emplace_back(&result); // only island add new grid - Points permanent = get_permanents(permanent_supports, permanent_index, layer_id, part_id); + Points permanent = + get_permanents(permanent_supports, permanent_index, layer_id, part_id); support_island(part, grids.back(), layer.print_z, permanent, config); - copy_permanent_supports(grids.back(), permanent_supports, permanent_index, layer.print_z, layer_id, part_id, config); + copy_permanent_supports( + grids.back(), permanent_supports, permanent_index, layer.print_z, layer_id, + part_id, config + ); continue; } @@ -1220,12 +1249,16 @@ LayerSupportPoints Slic3r::sla::generate_support_points( remove_supports_out_of_part(near_points, part, layer.print_z); if (!part.peninsulas.empty()) { // only get copy of points do not modify permanent_index - Points permanent = get_permanents(permanent_supports, permanent_index, layer_id, part_id); + Points permanent = + get_permanents(permanent_supports, permanent_index, layer_id, part_id); support_peninsulas(part.peninsulas, near_points, layer.print_z, permanent, config); } - copy_permanent_supports(near_points, permanent_supports, permanent_index, layer.print_z, layer_id, part_id, config); + copy_permanent_supports( + near_points, permanent_supports, permanent_index, layer.print_z, layer_id, part_id, + config + ); support_part_overhangs(part, config, near_points, layer.print_z, maximal_radius); - grids.push_back(std::move(near_points)); + grids.push_back(std::move(near_points)); } prev_grids = std::move(grids); @@ -1239,14 +1272,16 @@ LayerSupportPoints Slic3r::sla::generate_support_points( } // Remove permanent supports from result // To preserve permanent 3d position it is necessary to append points after move_on_mesh_surface - result.erase(std::remove_if(result.begin(), result.end(), - [](const LayerSupportPoint &p) { return p.is_permanent; }), result.end()); + result.erase( + std::remove_if( + result.begin(), result.end(), [](const LayerSupportPoint &p) { return p.is_permanent; } + ), + result.end() + ); return result; } -// TODO: Should be in another file -#include "libslic3r/AABBMesh.hpp" -SupportPoints Slic3r::sla::move_on_mesh_surface( +SupportPoints move_on_mesh_surface( const LayerSupportPoints &points, const AABBMesh &mesh, double allowed_move, @@ -1258,38 +1293,47 @@ SupportPoints Slic3r::sla::move_on_mesh_surface( pts.push_back(static_cast(p)); // The function makes sure that all the points are really exactly placed on the mesh. - execution::for_each(ex_tbb, size_t(0), pts.size(), [&pts, &mesh, &throw_on_cancel, allowed_move](size_t idx) - { - if ((idx % 16) == 0) - // Don't call the following function too often as it flushes CPU write caches due to synchronization primitves. - throw_on_cancel(); + execution::for_each( + ex_tbb, size_t(0), pts.size(), + [&pts, &mesh, &throw_on_cancel, allowed_move](size_t idx) { + if ((idx % 16) == 0) + // Don't call the following function too often as it flushes CPU write caches due to + // synchronization primitves. + throw_on_cancel(); - Vec3f& p = pts[idx].pos; - Vec3d p_double = p.cast(); - const Vec3d up_vec(0., 0., 1.); - const Vec3d down_vec(0., 0., -1.); - // Project the point upward and downward and choose the closer intersection with the mesh. - AABBMesh::hit_result hit_up = mesh.query_ray_hit(p_double, up_vec); - AABBMesh::hit_result hit_down = mesh.query_ray_hit(p_double, down_vec); + Vec3f &p = pts[idx].pos; + Vec3d p_double = p.cast(); + const Vec3d up_vec(0., 0., 1.); + const Vec3d down_vec(0., 0., -1.); + // Project the point upward and downward and choose the closer intersection with the mesh. + AABBMesh::hit_result hit_up = mesh.query_ray_hit(p_double, up_vec); + AABBMesh::hit_result hit_down = mesh.query_ray_hit(p_double, down_vec); - bool up = hit_up.is_hit(); - bool down = hit_down.is_hit(); - // no hit means support points lay exactly on triangle surface - if (!up && !down) return; - - AABBMesh::hit_result &hit = (!down || hit_up.distance() < hit_down.distance()) ? hit_up : hit_down; - if (hit.distance() <= allowed_move) { - p[2] += static_cast(hit.distance() * - hit.direction()[2]); - return; - } - - // big distance means that ray fly over triangle side (space between triangles) - int triangle_index; - Vec3d closest_point; - double distance = mesh.squared_distance(p_double, triangle_index, closest_point); - if (distance <= std::numeric_limits::epsilon()) return; // correct coordinate - p = closest_point.cast(); - }, 64 /* gransize */); + bool up = hit_up.is_hit(); + bool down = hit_down.is_hit(); + // no hit means support points lay exactly on triangle surface + if (!up && !down) + return; + + AABBMesh::hit_result &hit = (!down || hit_up.distance() < hit_down.distance()) ? + hit_up : + hit_down; + if (hit.distance() <= allowed_move) { + p[2] += static_cast(hit.distance() * hit.direction()[2]); + return; + } + + // big distance means that ray fly over triangle side (space between triangles) + int triangle_index; + Vec3d closest_point; + double distance = mesh.squared_distance(p_double, triangle_index, closest_point); + if (distance <= std::numeric_limits::epsilon()) + return; // correct coordinate + p = closest_point.cast(); + }, + 64 /* gransize */ + ); return pts; } + +} // namespace Slic3r::sla diff --git a/src/libslic3r/SLA/SupportPointGenerator.hpp b/src/libslic3r/SLA/SupportPointGenerator.hpp index e685a5dec0..4b74445415 100644 --- a/src/libslic3r/SLA/SupportPointGenerator.hpp +++ b/src/libslic3r/SLA/SupportPointGenerator.hpp @@ -14,10 +14,12 @@ #include "libslic3r/ExPolygon.hpp" #include "libslic3r/SLA/SupportPoint.hpp" #include "libslic3r/SLA/SupportIslands/SampleConfig.hpp" -#include "libslic3r/SLA/SupportIslands/SampleConfigFactory.hpp" namespace Slic3r::sla { +std::vector create_default_support_curve(); +SampleConfig create_default_island_configuration(float head_diameter_in_mm); + /// /// Configuration for automatic support placement /// @@ -35,22 +37,14 @@ struct SupportPointGeneratorConfig{ /// float head_diameter = 0.4f; // [in mm] - // FIXME: calculate actual pixel area from printer config: - // const float pixel_area = - // pow(wxGetApp().preset_bundle->project_config.option("display_width") / - // wxGetApp().preset_bundle->project_config.option("display_pixels_x"), 2.f); // - // Minimal island Area to print - TODO: Should be modifiable from UI - // !! Filter should be out of sampling algorithm !! - float minimal_island_area = pow(0.047f, 2.f); // [in mm^2] pixel_area - // maximal distance to nearest support point(define radiuses per layer) // x axis .. mean distance on layer(XY) // y axis .. mean difference of height(Z) // Points of lines [in mm] - std::vector support_curve; + std::vector support_curve = create_default_support_curve(); // Configuration for sampling island - SampleConfig island_configuration = SampleConfigFactory::create(head_diameter); + SampleConfig island_configuration = create_default_island_configuration(head_diameter); // maximal allowed distance to layer part for permanent(manual edited) support // helps to identify not wanted support points during automatic support generation.