GCodeViewer -> Refactoring

This commit is contained in:
enricoturri1966 2020-04-27 12:43:51 +02:00
parent a6ed1d817a
commit c1246f86eb
3 changed files with 35 additions and 42 deletions

View file

@ -65,19 +65,6 @@ void GCodeViewer::VBuffer::reset()
vertices_count = 0; vertices_count = 0;
} }
bool GCodeViewer::Path::is_path_visible(const Path& path, unsigned int flags) {
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
@ -186,6 +173,8 @@ void GCodeViewer::load(const GCodeProcessor::Result& gcode_result, const Print&
void GCodeViewer::refresh(const GCodeProcessor::Result& gcode_result, const std::vector<std::string>& str_tool_colors) void GCodeViewer::refresh(const GCodeProcessor::Result& gcode_result, const std::vector<std::string>& str_tool_colors)
{ {
auto start_time = std::chrono::high_resolution_clock::now();
if (m_vertices.vertices_count == 0) if (m_vertices.vertices_count == 0)
return; return;
@ -222,6 +211,9 @@ void GCodeViewer::refresh(const GCodeProcessor::Result& gcode_result, const std:
default: { break; } default: { break; }
} }
} }
auto end_time = std::chrono::high_resolution_clock::now();
std::cout << "refresh: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count() << "ms \n";
} }
void GCodeViewer::reset() void GCodeViewer::reset()
@ -560,7 +552,7 @@ 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) { for (const Path& path : buffer.paths) {
if (!Path::is_path_in_z_range(path, m_layers_z_range)) if (!is_in_z_range(path))
continue; continue;
glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); glsafe(::glEnable(GL_PROGRAM_POINT_SIZE));
@ -574,7 +566,7 @@ void GCodeViewer::render_toolpaths() const
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) { for (const Path& path : buffer.paths) {
if (!Path::is_path_in_z_range(path, m_layers_z_range)) if (!is_in_z_range(path))
continue; continue;
glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); glsafe(::glEnable(GL_PROGRAM_POINT_SIZE));
@ -588,7 +580,7 @@ void GCodeViewer::render_toolpaths() const
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) { for (const Path& path : buffer.paths) {
if (!Path::is_path_in_z_range(path, m_layers_z_range)) if (!is_in_z_range(path))
continue; continue;
glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); glsafe(::glEnable(GL_PROGRAM_POINT_SIZE));
@ -602,7 +594,7 @@ void GCodeViewer::render_toolpaths() const
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) { for (const Path& path : buffer.paths) {
if (!Path::is_path_in_z_range(path, m_layers_z_range)) if (!is_in_z_range(path))
continue; continue;
glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); glsafe(::glEnable(GL_PROGRAM_POINT_SIZE));
@ -616,7 +608,7 @@ void GCodeViewer::render_toolpaths() const
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) { for (const Path& path : buffer.paths) {
if (!Path::is_path_in_z_range(path, m_layers_z_range)) if (!is_in_z_range(path))
continue; continue;
glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); glsafe(::glEnable(GL_PROGRAM_POINT_SIZE));
@ -630,7 +622,7 @@ void GCodeViewer::render_toolpaths() const
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) { for (const Path& path : buffer.paths) {
if (!Path::is_path_in_z_range(path, m_layers_z_range)) if (!is_in_z_range(path))
continue; continue;
glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); glsafe(::glEnable(GL_PROGRAM_POINT_SIZE));
@ -642,7 +634,7 @@ void GCodeViewer::render_toolpaths() const
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(path, m_extrusions.role_visibility_flags) || !Path::is_path_in_z_range(path, m_layers_z_range)) if (!is_visible(path) || !is_in_z_range(path))
continue; continue;
set_color(current_program_id, extrusion_color(path)); set_color(current_program_id, extrusion_color(path));
@ -653,7 +645,7 @@ 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)) if (!is_in_z_range(path))
continue; 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));
@ -689,9 +681,7 @@ void GCodeViewer::render_shells() const
void GCodeViewer::render_overlay() const void GCodeViewer::render_overlay() const
{ {
static const ImVec4 ORANGE(1.0f, 0.49f, 0.22f, 1.0f); static const ImVec4 ORANGE(1.0f, 0.49f, 0.22f, 1.0f);
static const float ICON_BORDER_SIZE = 25.0f;
static const ImU32 ICON_BORDER_COLOR = ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, 1.0f)); static const ImU32 ICON_BORDER_COLOR = ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, 1.0f));
static const float GAP_ICON_TEXT = 7.5f;
if (!m_legend_enabled || m_roles.empty()) if (!m_legend_enabled || m_roles.empty())
return; return;
@ -709,14 +699,15 @@ void GCodeViewer::render_overlay() const
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) {
// draw icon float icon_size = ImGui::GetTextLineHeight();
ImVec2 pos(ImGui::GetCursorPosX() + 2.0f, ImGui::GetCursorPosY() + 2.0f); ImVec2 pos = ImGui::GetCursorPos();
draw_list->AddRect({ pos.x, pos.y }, { pos.x + ICON_BORDER_SIZE, pos.y + ICON_BORDER_SIZE }, ICON_BORDER_COLOR, 0.0f, 0); draw_list->AddRect({ pos.x, pos.y }, { pos.x + icon_size, pos.y + icon_size }, ICON_BORDER_COLOR, 0.0f, 0);
draw_list->AddRectFilled({ pos.x + 1.0f, pos.y + 1.0f }, draw_list->AddRectFilled({ pos.x + 1.0f, pos.y + 1.0f }, { pos.x + icon_size - 1.0f, pos.y + icon_size - 1.0f },
{ pos.x + ICON_BORDER_SIZE - 1.0f, pos.y + ICON_BORDER_SIZE - 1.0f },
ImGui::GetColorU32({ color[0], color[1], color[2], 1.0f })); ImGui::GetColorU32({ color[0], color[1], color[2], 1.0f }));
// draw text // draw text
ImGui::SetCursorPos({ pos.x + ICON_BORDER_SIZE + GAP_ICON_TEXT, pos.y + 0.5f * (ICON_BORDER_SIZE - ImGui::GetTextLineHeight()) }); ImGui::Dummy({ icon_size, icon_size });
ImGui::SameLine();
imgui.text(label); imgui.text(label);
}; };
@ -762,7 +753,7 @@ void GCodeViewer::render_overlay() const
case EViewType::FeatureType: case EViewType::FeatureType:
{ {
for (ExtrusionRole role : m_roles) { for (ExtrusionRole role : m_roles) {
bool visible = m_extrusions.is_role_visible(role); bool visible = is_visible(role);
if (!visible) if (!visible)
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0.3333f); ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0.3333f);

View file

@ -56,9 +56,6 @@ class GCodeViewer
feedrate == move.feedrate && fan_speed == move.fan_speed && volumetric_rate == move.volumetric_rate() && feedrate == move.feedrate && fan_speed == move.fan_speed && volumetric_rate == move.volumetric_rate() &&
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(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
@ -134,14 +131,6 @@ class GCodeViewer
} }
void reset_ranges() { ranges.reset(); } void reset_ranges() { ranges.reset(); }
bool is_role_visible(ExtrusionRole role) const {
return role < erCount && (role_visibility_flags & (1 << role)) != 0;
}
static bool is_role_visible(unsigned int flags, ExtrusionRole role) {
return role < erCount && (flags & (1 << role)) != 0;
}
}; };
public: public:
@ -217,6 +206,17 @@ private:
void render_toolpaths() const; void render_toolpaths() const;
void render_shells() const; void render_shells() const;
void render_overlay() const; void render_overlay() const;
bool is_visible(ExtrusionRole role) const {
return role < erCount && (m_extrusions.role_visibility_flags & (1 << role)) != 0;
}
bool is_visible(const Path& path) const { return is_visible(path.role); }
bool is_in_z_range(const Path& path) const {
auto in_z_range = [this](double z) {
return z > m_layers_z_range[0] - EPSILON && z < m_layers_z_range[1] + EPSILON;
};
return in_z_range(path.first_z) || in_z_range(path.last_z);
}
}; };
} // namespace GUI } // namespace GUI

View file

@ -2858,6 +2858,8 @@ void GLCanvas3D::load_gcode_preview(const GCodeProcessor::Result& gcode_result)
void GLCanvas3D::refresh_gcode_preview(const GCodeProcessor::Result& gcode_result, const std::vector<std::string>& str_tool_colors) void GLCanvas3D::refresh_gcode_preview(const GCodeProcessor::Result& gcode_result, const std::vector<std::string>& str_tool_colors)
{ {
m_gcode_viewer.refresh(gcode_result, str_tool_colors); m_gcode_viewer.refresh(gcode_result, str_tool_colors);
set_as_dirty();
request_extra_frame();
} }
#endif // ENABLE_GCODE_VIEWER #endif // ENABLE_GCODE_VIEWER