GCodeViewer -> Layers z slider wip

This commit is contained in:
enricoturri1966 2020-04-27 11:44:29 +02:00
parent c76bf934f7
commit a6ed1d817a
12 changed files with 104 additions and 30 deletions

View file

@ -13,5 +13,5 @@ void main()
// world_normal_z = gl_Normal.z; // world_normal_z = gl_Normal.z;
gl_Position = ftransform(); gl_Position = ftransform();
gl_PointSize = 10.0; gl_PointSize = 15.0;
} }

View file

@ -13,5 +13,5 @@ void main()
// world_normal_z = gl_Normal.z; // world_normal_z = gl_Normal.z;
gl_Position = ftransform(); gl_Position = ftransform();
gl_PointSize = 10.0; gl_PointSize = 15.0;
} }

View file

@ -13,5 +13,5 @@ void main()
// world_normal_z = gl_Normal.z; // world_normal_z = gl_Normal.z;
gl_Position = ftransform(); gl_Position = ftransform();
gl_PointSize = 5.0; gl_PointSize = 15.0;
} }

View file

@ -13,5 +13,5 @@ void main()
// world_normal_z = gl_Normal.z; // world_normal_z = gl_Normal.z;
gl_Position = ftransform(); gl_Position = ftransform();
gl_PointSize = 5.0; gl_PointSize = 15.0;
} }

View file

@ -13,5 +13,5 @@ void main()
// world_normal_z = gl_Normal.z; // world_normal_z = gl_Normal.z;
gl_Position = ftransform(); gl_Position = ftransform();
gl_PointSize = 5.0; gl_PointSize = 15.0;
} }

View file

@ -341,6 +341,7 @@ BoundingBoxf3 GLVolume::transformed_convex_hull_bounding_box(const Transform3d &
} }
#if !ENABLE_GCODE_VIEWER
void GLVolume::set_range(double min_z, double max_z) void GLVolume::set_range(double min_z, double max_z)
{ {
this->qverts_range.first = 0; this->qverts_range.first = 0;
@ -375,6 +376,7 @@ void GLVolume::set_range(double min_z, double max_z)
} }
} }
} }
#endif // !ENABLE_GCODE_VIEWER
void GLVolume::render() const void GLVolume::render() const
{ {

View file

@ -442,7 +442,9 @@ public:
bool empty() const { return this->indexed_vertex_array.empty(); } bool empty() const { return this->indexed_vertex_array.empty(); }
#if !ENABLE_GCODE_VIEWER
void set_range(double low, double high); void set_range(double low, double high);
#endif // !ENABLE_GCODE_VIEWER
void render() const; void render() const;
#if !ENABLE_SLOPE_RENDERING #if !ENABLE_SLOPE_RENDERING
@ -560,7 +562,9 @@ public:
void clear() { for (auto *v : volumes) delete v; volumes.clear(); } void clear() { for (auto *v : volumes) delete v; volumes.clear(); }
bool empty() const { return volumes.empty(); } bool empty() const { return volumes.empty(); }
#if !ENABLE_GCODE_VIEWER
void set_range(double low, double high) { for (GLVolume *vol : this->volumes) vol->set_range(low, high); } void set_range(double low, double high) { for (GLVolume *vol : this->volumes) vol->set_range(low, high); }
#endif // !ENABLE_GCODE_VIEWER
void set_print_box(float min_x, float min_y, float min_z, float max_x, float max_y, float max_z) { void set_print_box(float min_x, float min_y, float min_z, float max_x, float max_y, float max_z) {
m_print_box_min[0] = min_x; m_print_box_min[1] = min_y; m_print_box_min[2] = min_z; m_print_box_min[0] = min_x; m_print_box_min[1] = min_y; m_print_box_min[2] = min_z;

View file

@ -65,10 +65,19 @@ void GCodeViewer::VBuffer::reset()
vertices_count = 0; vertices_count = 0;
} }
bool GCodeViewer::Path::is_path_visible(unsigned int flags, const Path& path) { bool GCodeViewer::Path::is_path_visible(const Path& path, unsigned int flags) {
return Extrusions::is_role_visible(flags, path.role); return Extrusions::is_role_visible(flags, path.role);
}; };
bool GCodeViewer::Path::is_path_in_z_range(const Path& path, const std::array<double, 2>& z_range)
{
auto in_z_range = [z_range](double z) {
return z > z_range[0] - EPSILON && z < z_range[1] + EPSILON;
};
return in_z_range(path.first_z) || in_z_range(path.last_z);
}
void GCodeViewer::IBuffer::reset() void GCodeViewer::IBuffer::reset()
{ {
// release gpu memory // release gpu memory
@ -96,7 +105,8 @@ bool GCodeViewer::IBuffer::init_shader(const std::string& vertex_shader_src, con
void GCodeViewer::IBuffer::add_path(const GCodeProcessor::MoveVertex& move) void GCodeViewer::IBuffer::add_path(const GCodeProcessor::MoveVertex& move)
{ {
unsigned int id = static_cast<unsigned int>(data.size()); unsigned int id = static_cast<unsigned int>(data.size());
paths.push_back({ move.type, move.extrusion_role, id, id, move.delta_extruder, move.height, move.width, move.feedrate, move.fan_speed, move.volumetric_rate(), move.extruder_id, move.cp_color_id }); double z = static_cast<double>(move.position[2]);
paths.push_back({ move.type, move.extrusion_role, id, id, z, z, move.delta_extruder, move.height, move.width, move.feedrate, move.fan_speed, move.volumetric_rate(), move.extruder_id, move.cp_color_id });
} }
std::array<float, 3> GCodeViewer::Extrusions::Range::get_color_at(float value) const std::array<float, 3> GCodeViewer::Extrusions::Range::get_color_at(float value) const
@ -182,7 +192,7 @@ void GCodeViewer::refresh(const GCodeProcessor::Result& gcode_result, const std:
// update tool colors // update tool colors
m_tool_colors = decode_colors(str_tool_colors); m_tool_colors = decode_colors(str_tool_colors);
// update ranges // update ranges for coloring / legend
m_extrusions.reset_ranges(); m_extrusions.reset_ranges();
for (size_t i = 0; i < m_vertices.vertices_count; ++i) for (size_t i = 0; i < m_vertices.vertices_count; ++i)
{ {
@ -229,6 +239,7 @@ void GCodeViewer::reset()
m_extrusions.reset_ranges(); m_extrusions.reset_ranges();
m_shells.volumes.clear(); m_shells.volumes.clear();
m_layers_zs = std::vector<double>(); m_layers_zs = std::vector<double>();
m_layers_z_range = { 0.0, 0.0 };
m_roles = std::vector<ExtrusionRole>(); m_roles = std::vector<ExtrusionRole>();
} }
@ -394,7 +405,7 @@ void GCodeViewer::load_toolpaths(const GCodeProcessor::Result& gcode_result)
// layers zs / roles / extruder ids / cp color ids -> extract from result // layers zs / roles / extruder ids / cp color ids -> extract from result
for (const GCodeProcessor::MoveVertex& move : gcode_result.moves) { for (const GCodeProcessor::MoveVertex& move : gcode_result.moves) {
if (move.type == GCodeProcessor::EMoveType::Extrude) if (move.type == GCodeProcessor::EMoveType::Extrude)
m_layers_zs.emplace_back(move.position[2]); m_layers_zs.emplace_back(static_cast<double>(move.position[2]));
m_roles.emplace_back(move.extrusion_role); m_roles.emplace_back(move.extrusion_role);
m_extruder_ids.emplace_back(move.extruder_id); m_extruder_ids.emplace_back(move.extruder_id);
@ -414,6 +425,9 @@ void GCodeViewer::load_toolpaths(const GCodeProcessor::Result& gcode_result)
if (k < n) if (k < n)
m_layers_zs.erase(m_layers_zs.begin() + k, m_layers_zs.end()); m_layers_zs.erase(m_layers_zs.begin() + k, m_layers_zs.end());
// set layers z range
m_layers_z_range = { m_layers_zs.front(), m_layers_zs.back() };
// roles -> remove duplicates // roles -> remove duplicates
std::sort(m_roles.begin(), m_roles.end()); std::sort(m_roles.begin(), m_roles.end());
m_roles.erase(std::unique(m_roles.begin(), m_roles.end()), m_roles.end()); m_roles.erase(std::unique(m_roles.begin(), m_roles.end()), m_roles.end());
@ -545,60 +559,90 @@ void GCodeViewer::render_toolpaths() const
{ {
std::array<float, 3> color = { 1.0f, 1.0f, 1.0f }; std::array<float, 3> color = { 1.0f, 1.0f, 1.0f };
set_color(current_program_id, color); set_color(current_program_id, color);
for (const Path& path : buffer.paths) {
if (!Path::is_path_in_z_range(path, m_layers_z_range))
continue;
glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); glsafe(::glEnable(GL_PROGRAM_POINT_SIZE));
glsafe(::glDrawElements(GL_POINTS, (GLsizei)buffer.data_size, GL_UNSIGNED_INT, nullptr)); glsafe(::glDrawElements(GL_POINTS, GLsizei(path.last - path.first + 1), GL_UNSIGNED_INT, (const void*)(path.first * sizeof(GLuint))));
glsafe(::glDisable(GL_PROGRAM_POINT_SIZE)); glsafe(::glDisable(GL_PROGRAM_POINT_SIZE));
}
break; break;
} }
case GCodeProcessor::EMoveType::Color_change: case GCodeProcessor::EMoveType::Color_change:
{ {
std::array<float, 3> color = { 1.0f, 0.0f, 0.0f }; std::array<float, 3> color = { 1.0f, 0.0f, 0.0f };
set_color(current_program_id, color); set_color(current_program_id, color);
for (const Path& path : buffer.paths) {
if (!Path::is_path_in_z_range(path, m_layers_z_range))
continue;
glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); glsafe(::glEnable(GL_PROGRAM_POINT_SIZE));
glsafe(::glDrawElements(GL_POINTS, (GLsizei)buffer.data_size, GL_UNSIGNED_INT, nullptr)); glsafe(::glDrawElements(GL_POINTS, GLsizei(path.last - path.first + 1), GL_UNSIGNED_INT, (const void*)(path.first * sizeof(GLuint))));
glsafe(::glDisable(GL_PROGRAM_POINT_SIZE)); glsafe(::glDisable(GL_PROGRAM_POINT_SIZE));
}
break; break;
} }
case GCodeProcessor::EMoveType::Pause_Print: case GCodeProcessor::EMoveType::Pause_Print:
{ {
std::array<float, 3> color = { 0.0f, 1.0f, 0.0f }; std::array<float, 3> color = { 0.0f, 1.0f, 0.0f };
set_color(current_program_id, color); set_color(current_program_id, color);
for (const Path& path : buffer.paths) {
if (!Path::is_path_in_z_range(path, m_layers_z_range))
continue;
glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); glsafe(::glEnable(GL_PROGRAM_POINT_SIZE));
glsafe(::glDrawElements(GL_POINTS, (GLsizei)buffer.data_size, GL_UNSIGNED_INT, nullptr)); glsafe(::glDrawElements(GL_POINTS, GLsizei(path.last - path.first + 1), GL_UNSIGNED_INT, (const void*)(path.first * sizeof(GLuint))));
glsafe(::glDisable(GL_PROGRAM_POINT_SIZE)); glsafe(::glDisable(GL_PROGRAM_POINT_SIZE));
}
break; break;
} }
case GCodeProcessor::EMoveType::Custom_GCode: case GCodeProcessor::EMoveType::Custom_GCode:
{ {
std::array<float, 3> color = { 0.0f, 0.0f, 1.0f }; std::array<float, 3> color = { 0.0f, 0.0f, 1.0f };
set_color(current_program_id, color); set_color(current_program_id, color);
for (const Path& path : buffer.paths) {
if (!Path::is_path_in_z_range(path, m_layers_z_range))
continue;
glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); glsafe(::glEnable(GL_PROGRAM_POINT_SIZE));
glsafe(::glDrawElements(GL_POINTS, (GLsizei)buffer.data_size, GL_UNSIGNED_INT, nullptr)); glsafe(::glDrawElements(GL_POINTS, GLsizei(path.last - path.first + 1), GL_UNSIGNED_INT, (const void*)(path.first * sizeof(GLuint))));
glsafe(::glDisable(GL_PROGRAM_POINT_SIZE)); glsafe(::glDisable(GL_PROGRAM_POINT_SIZE));
}
break; break;
} }
case GCodeProcessor::EMoveType::Retract: case GCodeProcessor::EMoveType::Retract:
{ {
std::array<float, 3> color = { 1.0f, 0.0f, 1.0f }; std::array<float, 3> color = { 1.0f, 0.0f, 1.0f };
set_color(current_program_id, color); set_color(current_program_id, color);
for (const Path& path : buffer.paths) {
if (!Path::is_path_in_z_range(path, m_layers_z_range))
continue;
glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); glsafe(::glEnable(GL_PROGRAM_POINT_SIZE));
glsafe(::glDrawElements(GL_POINTS, (GLsizei)buffer.data_size, GL_UNSIGNED_INT, nullptr)); glsafe(::glDrawElements(GL_POINTS, GLsizei(path.last - path.first + 1), GL_UNSIGNED_INT, (const void*)(path.first * sizeof(GLuint))));
glsafe(::glDisable(GL_PROGRAM_POINT_SIZE)); glsafe(::glDisable(GL_PROGRAM_POINT_SIZE));
}
break; break;
} }
case GCodeProcessor::EMoveType::Unretract: case GCodeProcessor::EMoveType::Unretract:
{ {
std::array<float, 3> color = { 0.0f, 1.0f, 1.0f }; std::array<float, 3> color = { 0.0f, 1.0f, 1.0f };
set_color(current_program_id, color); set_color(current_program_id, color);
for (const Path& path : buffer.paths) {
if (!Path::is_path_in_z_range(path, m_layers_z_range))
continue;
glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); glsafe(::glEnable(GL_PROGRAM_POINT_SIZE));
glsafe(::glDrawElements(GL_POINTS, (GLsizei)buffer.data_size, GL_UNSIGNED_INT, nullptr)); glsafe(::glDrawElements(GL_POINTS, GLsizei(path.last - path.first + 1), GL_UNSIGNED_INT, (const void*)(path.first * sizeof(GLuint))));
glsafe(::glDisable(GL_PROGRAM_POINT_SIZE)); glsafe(::glDisable(GL_PROGRAM_POINT_SIZE));
}
break; break;
} }
case GCodeProcessor::EMoveType::Extrude: case GCodeProcessor::EMoveType::Extrude:
{ {
for (const Path& path : buffer.paths) { for (const Path& path : buffer.paths) {
if (!Path::is_path_visible(m_extrusions.role_visibility_flags, path)) if (!Path::is_path_visible(path, m_extrusions.role_visibility_flags) || !Path::is_path_in_z_range(path, m_layers_z_range))
continue; continue;
set_color(current_program_id, extrusion_color(path)); set_color(current_program_id, extrusion_color(path));
@ -609,6 +653,9 @@ void GCodeViewer::render_toolpaths() const
case GCodeProcessor::EMoveType::Travel: case GCodeProcessor::EMoveType::Travel:
{ {
for (const Path& path : buffer.paths) { for (const Path& path : buffer.paths) {
if (!Path::is_path_in_z_range(path, m_layers_z_range))
continue;
set_color(current_program_id, (m_view_type == EViewType::Feedrate || m_view_type == EViewType::Tool || m_view_type == EViewType::ColorPrint) ? extrusion_color(path) : travel_color(path)); set_color(current_program_id, (m_view_type == EViewType::Feedrate || m_view_type == EViewType::Tool || m_view_type == EViewType::ColorPrint) ? extrusion_color(path) : travel_color(path));
glsafe(::glDrawElements(GL_LINE_STRIP, GLsizei(path.last - path.first + 1), GL_UNSIGNED_INT, (const void*)(path.first * sizeof(GLuint)))); glsafe(::glDrawElements(GL_LINE_STRIP, GLsizei(path.last - path.first + 1), GL_UNSIGNED_INT, (const void*)(path.first * sizeof(GLuint))));
} }
@ -655,6 +702,10 @@ void GCodeViewer::render_overlay() const
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
imgui.begin(std::string("Legend"), ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove); imgui.begin(std::string("Legend"), ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove);
if (ImGui::IsWindowAppearing())
// force an extra farme
wxGetApp().plater()->get_current_canvas3D()->request_extra_frame();
ImDrawList* draw_list = ImGui::GetWindowDrawList(); ImDrawList* draw_list = ImGui::GetWindowDrawList();
auto add_item = [draw_list, &imgui](const std::array<float, 3>& color, const std::string& label) { auto add_item = [draw_list, &imgui](const std::array<float, 3>& color, const std::string& label) {

View file

@ -40,6 +40,8 @@ class GCodeViewer
ExtrusionRole role{ erNone }; ExtrusionRole role{ erNone };
unsigned int first{ 0 }; unsigned int first{ 0 };
unsigned int last{ 0 }; unsigned int last{ 0 };
double first_z{ 0.0f };
double last_z{ 0.0f };
float delta_extruder{ 0.0f }; float delta_extruder{ 0.0f };
float height{ 0.0f }; float height{ 0.0f };
float width{ 0.0f }; float width{ 0.0f };
@ -55,7 +57,8 @@ class GCodeViewer
extruder_id == move.extruder_id && cp_color_id == move.cp_color_id; extruder_id == move.extruder_id && cp_color_id == move.cp_color_id;
} }
static bool is_path_visible(unsigned int flags, const Path& path); static bool is_path_visible(const Path& path, unsigned int flags);
static bool is_path_in_z_range(const Path& path, const std::array<double, 2>& z_range);
}; };
// buffer containing indices data and shader for a specific toolpath type // buffer containing indices data and shader for a specific toolpath type
@ -162,6 +165,7 @@ private:
BoundingBoxf3 m_bounding_box; BoundingBoxf3 m_bounding_box;
std::vector<std::array<float, 3>> m_tool_colors; std::vector<std::array<float, 3>> m_tool_colors;
std::vector<double> m_layers_zs; std::vector<double> m_layers_zs;
std::array<double, 2> m_layers_z_range;
std::vector<ExtrusionRole> m_roles; std::vector<ExtrusionRole> m_roles;
std::vector<unsigned char> m_extruder_ids; std::vector<unsigned char> m_extruder_ids;
Extrusions m_extrusions; Extrusions m_extrusions;
@ -201,6 +205,7 @@ public:
void set_toolpath_move_type_visible(GCodeProcessor::EMoveType type, bool visible); void set_toolpath_move_type_visible(GCodeProcessor::EMoveType type, bool visible);
void set_toolpath_role_visibility_flags(unsigned int flags) { m_extrusions.role_visibility_flags = flags; } void set_toolpath_role_visibility_flags(unsigned int flags) { m_extrusions.role_visibility_flags = flags; }
void set_options_visibility_from_flags(unsigned int flags); void set_options_visibility_from_flags(unsigned int flags);
void set_layers_z_range(const std::array<double, 2>& layers_z_range) { m_layers_z_range = layers_z_range; }
bool is_legend_enabled() const { return m_legend_enabled; } bool is_legend_enabled() const { return m_legend_enabled; }
void enable_legend(bool enable) { m_legend_enabled = enable; } void enable_legend(bool enable) { m_legend_enabled = enable; }

View file

@ -2346,17 +2346,22 @@ void GLCanvas3D::set_toolpath_view_type(GCodeViewer::EViewType type)
{ {
m_gcode_viewer.set_view_type(type); m_gcode_viewer.set_view_type(type);
} }
void GLCanvas3D::set_toolpaths_z_range(const std::array<double, 2>& range)
{
m_gcode_viewer.set_layers_z_range(range);
}
#else #else
std::vector<double> GLCanvas3D::get_current_print_zs(bool active_only) const std::vector<double> GLCanvas3D::get_current_print_zs(bool active_only) const
{ {
return m_volumes.get_current_print_zs(active_only); return m_volumes.get_current_print_zs(active_only);
} }
#endif // ENABLE_GCODE_VIEWER
void GLCanvas3D::set_toolpaths_range(double low, double high) void GLCanvas3D::set_toolpaths_range(double low, double high)
{ {
m_volumes.set_range(low, high); m_volumes.set_range(low, high);
} }
#endif // ENABLE_GCODE_VIEWER
std::vector<int> GLCanvas3D::load_object(const ModelObject& model_object, int obj_idx, std::vector<int> instance_idxs) std::vector<int> GLCanvas3D::load_object(const ModelObject& model_object, int obj_idx, std::vector<int> instance_idxs)
{ {

View file

@ -650,8 +650,10 @@ public:
void set_gcode_options_visibility_from_flags(unsigned int flags); void set_gcode_options_visibility_from_flags(unsigned int flags);
void set_toolpath_role_visibility_flags(unsigned int flags); void set_toolpath_role_visibility_flags(unsigned int flags);
void set_toolpath_view_type(GCodeViewer::EViewType type); void set_toolpath_view_type(GCodeViewer::EViewType type);
void set_toolpaths_z_range(const std::array<double, 2>& range);
#else #else
std::vector<double> get_current_print_zs(bool active_only) const; std::vector<double> get_current_print_zs(bool active_only) const;
void set_toolpaths_range(double low, double high);
#endif // ENABLE_GCODE_VIEWER #endif // ENABLE_GCODE_VIEWER
void set_toolpaths_range(double low, double high); void set_toolpaths_range(double low, double high);

View file

@ -1113,9 +1113,14 @@ void Preview::on_sliders_scroll_changed(wxCommandEvent& event)
PrinterTechnology tech = m_process->current_printer_technology(); PrinterTechnology tech = m_process->current_printer_technology();
if (tech == ptFFF) if (tech == ptFFF)
{ {
#if ENABLE_GCODE_VIEWER
m_canvas->set_toolpaths_z_range({ m_slider->GetLowerValueD(), m_slider->GetHigherValueD() });
m_canvas->set_as_dirty();
#else
m_canvas->set_toolpaths_range(m_slider->GetLowerValueD() - 1e-6, m_slider->GetHigherValueD() + 1e-6); m_canvas->set_toolpaths_range(m_slider->GetLowerValueD() - 1e-6, m_slider->GetHigherValueD() + 1e-6);
m_canvas->render(); m_canvas->render();
m_canvas->set_use_clipping_planes(false); m_canvas->set_use_clipping_planes(false);
#endif // ENABLE_GCODE_VIEWER
} }
else if (tech == ptSLA) else if (tech == ptSLA)
{ {