From 9120c93e7bb6706260082f432d6695ac50b2fb3e Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Thu, 25 Jan 2024 12:02:57 +0100 Subject: [PATCH] New gcode visualization library - Axes aligned bounding boxes calculation --- src/libvgcode/include/Types.hpp | 13 ------------- src/libvgcode/include/Viewer.hpp | 16 ++++++++++++++-- src/libvgcode/src/Viewer.cpp | 9 +++++++-- src/libvgcode/src/ViewerImpl.cpp | 29 ++++++++++++++++++++--------- src/libvgcode/src/ViewerImpl.hpp | 11 ++++++++++- src/slic3r/GUI/GCodeViewer.cpp | 16 ++++++++++++---- 6 files changed, 63 insertions(+), 31 deletions(-) diff --git a/src/libvgcode/include/Types.hpp b/src/libvgcode/include/Types.hpp index 38155cf457..6491f0429e 100644 --- a/src/libvgcode/include/Types.hpp +++ b/src/libvgcode/include/Types.hpp @@ -188,19 +188,6 @@ enum class EColorRangeType : uint8_t static constexpr size_t COLOR_RANGE_TYPES_COUNT = static_cast(EColorRangeType::COUNT); -// -// Bounding box types -// -enum class EBBoxType : uint8_t -{ - Full, - Extrusion, - ExtrusionNoCustom, - COUNT -}; - -static constexpr size_t BBOX_TYPES_COUNT = static_cast(EBBoxType::COUNT); - // // Predefined colors // diff --git a/src/libvgcode/include/Viewer.hpp b/src/libvgcode/include/Viewer.hpp index 0e3f1f1d52..4bec17b911 100644 --- a/src/libvgcode/include/Viewer.hpp +++ b/src/libvgcode/include/Viewer.hpp @@ -348,9 +348,21 @@ public: // std::vector get_layers_times() const; // - // Return the axes aligned bounding box of the toolpaths. + // Return the axes aligned bounding box containing all the given types. // - AABox get_bounding_box(EBBoxType type) const; + AABox get_bounding_box(const std::vector& types = { + EMoveType::Retract, EMoveType::Unretract, EMoveType::Seam, EMoveType::ToolChange, + EMoveType::ColorChange, EMoveType::PausePrint, EMoveType::CustomGCode, EMoveType::Travel, + EMoveType::Wipe, EMoveType::Extrude }) const; + // + // Return the axes aligned bounding box containing all the extrusions with the given roles. + // + AABox get_extrusion_bounding_box(const std::vector& roles = { + EGCodeExtrusionRole::Perimeter, EGCodeExtrusionRole::ExternalPerimeter, EGCodeExtrusionRole::OverhangPerimeter, + EGCodeExtrusionRole::InternalInfill, EGCodeExtrusionRole::SolidInfill, EGCodeExtrusionRole::TopSolidInfill, + EGCodeExtrusionRole::Ironing, EGCodeExtrusionRole::BridgeInfill, EGCodeExtrusionRole::GapFill, + EGCodeExtrusionRole::Skirt, EGCodeExtrusionRole::SupportMaterial, EGCodeExtrusionRole::SupportMaterialInterface, + EGCodeExtrusionRole::WipeTower, EGCodeExtrusionRole::Custom }) const; // // Return the size of the used cpu memory, in bytes // diff --git a/src/libvgcode/src/Viewer.cpp b/src/libvgcode/src/Viewer.cpp index 51b8646310..08f8f2e26f 100644 --- a/src/libvgcode/src/Viewer.cpp +++ b/src/libvgcode/src/Viewer.cpp @@ -332,9 +332,14 @@ std::vector Viewer::get_layers_times() const return m_impl->get_layers_times(); } -AABox Viewer::get_bounding_box(EBBoxType type) const +AABox Viewer::get_bounding_box(const std::vector& types) const { - return m_impl->get_bounding_box(type); + return m_impl->get_bounding_box(types); +} + +AABox Viewer::get_extrusion_bounding_box(const std::vector& roles) const +{ + return m_impl->get_extrusion_bounding_box(roles); } size_t Viewer::get_used_cpu_memory() const diff --git a/src/libvgcode/src/ViewerImpl.cpp b/src/libvgcode/src/ViewerImpl.cpp index 9d2f002630..b34f110e6b 100644 --- a/src/libvgcode/src/ViewerImpl.cpp +++ b/src/libvgcode/src/ViewerImpl.cpp @@ -851,20 +851,31 @@ std::vector ViewerImpl::get_color_prints(uint8_t extruder_id) const return (it == m_used_extruders.end()) ? std::vector() : it->second; } -AABox ViewerImpl::get_bounding_box(EBBoxType type) const +AABox ViewerImpl::get_bounding_box(const std::vector& types) const { - assert(type < EBBoxType::COUNT); Vec3 min = { FLT_MAX, FLT_MAX, FLT_MAX }; Vec3 max = { -FLT_MAX, -FLT_MAX, -FLT_MAX }; for (const PathVertex& v : m_vertices) { - if (type != EBBoxType::Full && (v.type != EMoveType::Extrude || v.width == 0.0f || v.height == 0.0f)) - continue; - else if (type == EBBoxType::ExtrusionNoCustom && v.role == EGCodeExtrusionRole::Custom) - continue; + if (std::find(types.begin(), types.end(), v.type) != types.end()) { + for (int j = 0; j < 3; ++j) { + min[j] = std::min(min[j], v.position[j]); + max[j] = std::max(max[j], v.position[j]); + } + } + } + return { min, max }; +} - for (int j = 0; j < 3; ++j) { - min[j] = std::min(min[j], v.position[j]); - max[j] = std::max(max[j], v.position[j]); +AABox ViewerImpl::get_extrusion_bounding_box(const std::vector& roles) const +{ + Vec3 min = { FLT_MAX, FLT_MAX, FLT_MAX }; + Vec3 max = { -FLT_MAX, -FLT_MAX, -FLT_MAX }; + for (const PathVertex& v : m_vertices) { + if (v.is_extrusion() && std::find(roles.begin(), roles.end(), v.role) != roles.end()) { + for (int j = 0; j < 3; ++j) { + min[j] = std::min(min[j], v.position[j]); + max[j] = std::max(max[j], v.position[j]); + } } } return { min, max }; diff --git a/src/libvgcode/src/ViewerImpl.hpp b/src/libvgcode/src/ViewerImpl.hpp index 51c3739a7f..cb2218296e 100644 --- a/src/libvgcode/src/ViewerImpl.hpp +++ b/src/libvgcode/src/ViewerImpl.hpp @@ -99,7 +99,16 @@ public: size_t get_color_prints_count(uint8_t extruder_id) const; std::vector get_color_prints(uint8_t extruder_id) const; - AABox get_bounding_box(EBBoxType type) const; + AABox get_bounding_box(const std::vector& types = { + EMoveType::Retract, EMoveType::Unretract, EMoveType::Seam, EMoveType::ToolChange, + EMoveType::ColorChange, EMoveType::PausePrint, EMoveType::CustomGCode, EMoveType::Travel, + EMoveType::Wipe, EMoveType::Extrude }) const; + AABox get_extrusion_bounding_box(const std::vector& roles = { + EGCodeExtrusionRole::Perimeter, EGCodeExtrusionRole::ExternalPerimeter, EGCodeExtrusionRole::OverhangPerimeter, + EGCodeExtrusionRole::InternalInfill, EGCodeExtrusionRole::SolidInfill, EGCodeExtrusionRole::TopSolidInfill, + EGCodeExtrusionRole::Ironing, EGCodeExtrusionRole::BridgeInfill, EGCodeExtrusionRole::GapFill, + EGCodeExtrusionRole::Skirt, EGCodeExtrusionRole::SupportMaterial, EGCodeExtrusionRole::SupportMaterialInterface, + EGCodeExtrusionRole::WipeTower, EGCodeExtrusionRole::Custom }) const; bool is_option_visible(EOptionType type) const; void toggle_option_visibility(EOptionType type); diff --git a/src/slic3r/GUI/GCodeViewer.cpp b/src/slic3r/GUI/GCodeViewer.cpp index 0090a872e6..63025cde2b 100644 --- a/src/slic3r/GUI/GCodeViewer.cpp +++ b/src/slic3r/GUI/GCodeViewer.cpp @@ -1168,7 +1168,15 @@ void GCodeViewer::load_as_gcode(const GCodeProcessorResult& gcode_result, const } #endif // !VGCODE_ENABLE_COG_AND_TOOL_MARKERS - const libvgcode::AABox bbox = m_viewer.get_bounding_box(wxGetApp().is_gcode_viewer() ? libvgcode::EBBoxType::Full : libvgcode::EBBoxType::ExtrusionNoCustom); + const libvgcode::AABox bbox = wxGetApp().is_gcode_viewer() ? + m_viewer.get_bounding_box() : + m_viewer.get_extrusion_bounding_box({ + libvgcode::EGCodeExtrusionRole::Perimeter, libvgcode::EGCodeExtrusionRole::ExternalPerimeter, libvgcode::EGCodeExtrusionRole::OverhangPerimeter, + libvgcode::EGCodeExtrusionRole::InternalInfill, libvgcode::EGCodeExtrusionRole::SolidInfill, libvgcode::EGCodeExtrusionRole::TopSolidInfill, + libvgcode::EGCodeExtrusionRole::Ironing, libvgcode::EGCodeExtrusionRole::BridgeInfill, libvgcode::EGCodeExtrusionRole::GapFill, + libvgcode::EGCodeExtrusionRole::Skirt, libvgcode::EGCodeExtrusionRole::SupportMaterial, libvgcode::EGCodeExtrusionRole::SupportMaterialInterface, + libvgcode::EGCodeExtrusionRole::WipeTower + }); m_paths_bounding_box.min = libvgcode::convert(bbox[0]).cast(); m_paths_bounding_box.max = libvgcode::convert(bbox[1]).cast(); @@ -1298,7 +1306,7 @@ void GCodeViewer::load_as_preview(libvgcode::GCodeInputData&& data) m_viewer.set_extrusion_role_color(libvgcode::EGCodeExtrusionRole::WipeTower, { 127, 255, 127 }); m_viewer.load(std::move(data)); - const libvgcode::AABox bbox = m_viewer.get_bounding_box(libvgcode::EBBoxType::Extrusion); + const libvgcode::AABox bbox = m_viewer.get_extrusion_bounding_box(); const BoundingBoxf3 paths_bounding_box(libvgcode::convert(bbox[0]).cast(), libvgcode::convert(bbox[1]).cast()); m_contained_in_bed = wxGetApp().plater()->build_volume().all_paths_inside(GCodeProcessorResult(), paths_bounding_box); } @@ -3837,8 +3845,8 @@ void GCodeViewer::refresh_render_paths(bool keep_sequential_current_first, bool void GCodeViewer::render_toolpaths() { const Camera& camera = wxGetApp().plater()->get_camera(); - libvgcode::Mat4x4 converted_view_matrix = libvgcode::convert(static_cast(camera.get_view_matrix().matrix().cast())); - libvgcode::Mat4x4 converted_projetion_matrix = libvgcode::convert(static_cast(camera.get_projection_matrix().matrix().cast())); + const libvgcode::Mat4x4 converted_view_matrix = libvgcode::convert(static_cast(camera.get_view_matrix().matrix().cast())); + const libvgcode::Mat4x4 converted_projetion_matrix = libvgcode::convert(static_cast(camera.get_projection_matrix().matrix().cast())); #if VGCODE_ENABLE_COG_AND_TOOL_MARKERS m_viewer.set_cog_marker_scale_factor(m_cog_marker_fixed_screen_size ? 10.0f * m_cog_marker_size * camera.get_inv_zoom() : m_cog_marker_size); m_viewer.enable_tool_marker(m_viewer.get_view_enabled_range()[1] != m_viewer.get_view_visible_range()[1]);