New gcode visualization library - Axes aligned bounding boxes calculation

This commit is contained in:
enricoturri1966 2024-01-25 12:02:57 +01:00 committed by Lukas Matena
parent 4d61f1ced8
commit 9120c93e7b
6 changed files with 63 additions and 31 deletions

View File

@ -188,19 +188,6 @@ enum class EColorRangeType : uint8_t
static constexpr size_t COLOR_RANGE_TYPES_COUNT = static_cast<size_t>(EColorRangeType::COUNT); static constexpr size_t COLOR_RANGE_TYPES_COUNT = static_cast<size_t>(EColorRangeType::COUNT);
//
// Bounding box types
//
enum class EBBoxType : uint8_t
{
Full,
Extrusion,
ExtrusionNoCustom,
COUNT
};
static constexpr size_t BBOX_TYPES_COUNT = static_cast<size_t>(EBBoxType::COUNT);
// //
// Predefined colors // Predefined colors
// //

View File

@ -348,9 +348,21 @@ public:
// //
std::vector<float> get_layers_times() const; std::vector<float> 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<EMoveType>& 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<EGCodeExtrusionRole>& 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 // Return the size of the used cpu memory, in bytes
// //

View File

@ -332,9 +332,14 @@ std::vector<float> Viewer::get_layers_times() const
return m_impl->get_layers_times(); return m_impl->get_layers_times();
} }
AABox Viewer::get_bounding_box(EBBoxType type) const AABox Viewer::get_bounding_box(const std::vector<EMoveType>& 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<EGCodeExtrusionRole>& roles) const
{
return m_impl->get_extrusion_bounding_box(roles);
} }
size_t Viewer::get_used_cpu_memory() const size_t Viewer::get_used_cpu_memory() const

View File

@ -851,20 +851,31 @@ std::vector<ColorPrint> ViewerImpl::get_color_prints(uint8_t extruder_id) const
return (it == m_used_extruders.end()) ? std::vector<ColorPrint>() : it->second; return (it == m_used_extruders.end()) ? std::vector<ColorPrint>() : it->second;
} }
AABox ViewerImpl::get_bounding_box(EBBoxType type) const AABox ViewerImpl::get_bounding_box(const std::vector<EMoveType>& types) const
{ {
assert(type < EBBoxType::COUNT);
Vec3 min = { FLT_MAX, FLT_MAX, FLT_MAX }; Vec3 min = { FLT_MAX, FLT_MAX, FLT_MAX };
Vec3 max = { -FLT_MAX, -FLT_MAX, -FLT_MAX }; Vec3 max = { -FLT_MAX, -FLT_MAX, -FLT_MAX };
for (const PathVertex& v : m_vertices) { for (const PathVertex& v : m_vertices) {
if (type != EBBoxType::Full && (v.type != EMoveType::Extrude || v.width == 0.0f || v.height == 0.0f)) if (std::find(types.begin(), types.end(), v.type) != types.end()) {
continue; for (int j = 0; j < 3; ++j) {
else if (type == EBBoxType::ExtrusionNoCustom && v.role == EGCodeExtrusionRole::Custom) min[j] = std::min(min[j], v.position[j]);
continue; max[j] = std::max(max[j], v.position[j]);
}
}
}
return { min, max };
}
for (int j = 0; j < 3; ++j) { AABox ViewerImpl::get_extrusion_bounding_box(const std::vector<EGCodeExtrusionRole>& roles) const
min[j] = std::min(min[j], v.position[j]); {
max[j] = std::max(max[j], v.position[j]); 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 }; return { min, max };

View File

@ -99,7 +99,16 @@ public:
size_t get_color_prints_count(uint8_t extruder_id) const; size_t get_color_prints_count(uint8_t extruder_id) const;
std::vector<ColorPrint> get_color_prints(uint8_t extruder_id) const; std::vector<ColorPrint> get_color_prints(uint8_t extruder_id) const;
AABox get_bounding_box(EBBoxType type) const; AABox get_bounding_box(const std::vector<EMoveType>& 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<EGCodeExtrusionRole>& 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; bool is_option_visible(EOptionType type) const;
void toggle_option_visibility(EOptionType type); void toggle_option_visibility(EOptionType type);

View File

@ -1168,7 +1168,15 @@ void GCodeViewer::load_as_gcode(const GCodeProcessorResult& gcode_result, const
} }
#endif // !VGCODE_ENABLE_COG_AND_TOOL_MARKERS #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<double>(); m_paths_bounding_box.min = libvgcode::convert(bbox[0]).cast<double>();
m_paths_bounding_box.max = libvgcode::convert(bbox[1]).cast<double>(); m_paths_bounding_box.max = libvgcode::convert(bbox[1]).cast<double>();
@ -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.set_extrusion_role_color(libvgcode::EGCodeExtrusionRole::WipeTower, { 127, 255, 127 });
m_viewer.load(std::move(data)); 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<double>(), libvgcode::convert(bbox[1]).cast<double>()); const BoundingBoxf3 paths_bounding_box(libvgcode::convert(bbox[0]).cast<double>(), libvgcode::convert(bbox[1]).cast<double>());
m_contained_in_bed = wxGetApp().plater()->build_volume().all_paths_inside(GCodeProcessorResult(), paths_bounding_box); 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() void GCodeViewer::render_toolpaths()
{ {
const Camera& camera = wxGetApp().plater()->get_camera(); const Camera& camera = wxGetApp().plater()->get_camera();
libvgcode::Mat4x4 converted_view_matrix = libvgcode::convert(static_cast<Matrix4f>(camera.get_view_matrix().matrix().cast<float>())); const libvgcode::Mat4x4 converted_view_matrix = libvgcode::convert(static_cast<Matrix4f>(camera.get_view_matrix().matrix().cast<float>()));
libvgcode::Mat4x4 converted_projetion_matrix = libvgcode::convert(static_cast<Matrix4f>(camera.get_projection_matrix().matrix().cast<float>())); const libvgcode::Mat4x4 converted_projetion_matrix = libvgcode::convert(static_cast<Matrix4f>(camera.get_projection_matrix().matrix().cast<float>()));
#if VGCODE_ENABLE_COG_AND_TOOL_MARKERS #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.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]); m_viewer.enable_tool_marker(m_viewer.get_view_enabled_range()[1] != m_viewer.get_view_visible_range()[1]);