GCodeViewer -> Fixed visualization of travel paths

This commit is contained in:
enricoturri1966 2020-05-04 09:37:06 +02:00
parent 9f2f798ea2
commit c7806dd021
3 changed files with 107 additions and 22 deletions

View file

@ -66,6 +66,30 @@ void GCodeViewer::VBuffer::reset()
vertices_count = 0; vertices_count = 0;
} }
bool GCodeViewer::Path::matches(const GCodeProcessor::MoveVertex& move) const
{
switch (move.type)
{
case GCodeProcessor::EMoveType::Tool_change:
case GCodeProcessor::EMoveType::Color_change:
case GCodeProcessor::EMoveType::Pause_Print:
case GCodeProcessor::EMoveType::Custom_GCode:
case GCodeProcessor::EMoveType::Retract:
case GCodeProcessor::EMoveType::Unretract:
case GCodeProcessor::EMoveType::Extrude:
{
return type == move.type && role == move.extrusion_role && height == move.height && width == move.width &&
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;
}
case GCodeProcessor::EMoveType::Travel:
{
return type == move.type && feedrate == move.feedrate && extruder_id == move.extruder_id && cp_color_id == move.cp_color_id;
}
default: { return false; }
}
}
void GCodeViewer::IBuffer::reset() void GCodeViewer::IBuffer::reset()
{ {
// release gpu memory // release gpu memory
@ -92,7 +116,7 @@ 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)
{ {
Path::Endpoint endpoint = { static_cast<unsigned int>(data.size()), static_cast<double>(move.position[2]) }; Path::Endpoint endpoint = { static_cast<unsigned int>(data.size()), move.position };
paths.push_back({ move.type, move.extrusion_role, endpoint, endpoint, move.delta_extruder, move.height, move.width, move.feedrate, move.fan_speed, move.volumetric_rate(), move.extruder_id, move.cp_color_id }); paths.push_back({ move.type, move.extrusion_role, endpoint, endpoint, move.delta_extruder, move.height, move.width, move.feedrate, move.fan_speed, move.volumetric_rate(), move.extruder_id, move.cp_color_id });
} }
@ -410,10 +434,11 @@ void GCodeViewer::load_toolpaths(const GCodeProcessor::Result& gcode_result)
{ {
if (prev.type != curr.type || !buffer.paths.back().matches(curr)) { if (prev.type != curr.type || !buffer.paths.back().matches(curr)) {
buffer.add_path(curr); buffer.add_path(curr);
buffer.paths.back().first.position = prev.position;
buffer.data.push_back(static_cast<unsigned int>(i - 1)); buffer.data.push_back(static_cast<unsigned int>(i - 1));
} }
buffer.paths.back().last.id = static_cast<unsigned int>(buffer.data.size()); buffer.paths.back().last = { static_cast<unsigned int>(buffer.data.size()), curr.position };
buffer.data.push_back(static_cast<unsigned int>(i)); buffer.data.push_back(static_cast<unsigned int>(i));
break; break;
} }
@ -424,6 +449,13 @@ void GCodeViewer::load_toolpaths(const GCodeProcessor::Result& gcode_result)
} }
} }
#if ENABLE_GCODE_VIEWER_STATISTICS
for (IBuffer& buffer : m_buffers)
{
m_statistics.paths_size += SLIC3R_STDVEC_MEMSIZE(buffer.paths, Path);
}
#endif // ENABLE_GCODE_VIEWER_STATISTICS
// indices data -> send data to gpu // indices data -> send data to gpu
for (IBuffer& buffer : m_buffers) for (IBuffer& buffer : m_buffers)
{ {
@ -445,12 +477,16 @@ 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 (size_t i = 0; i < m_vertices.vertices_count; ++i)
{
const GCodeProcessor::MoveVertex& move = gcode_result.moves[i];
if (move.type == GCodeProcessor::EMoveType::Extrude) if (move.type == GCodeProcessor::EMoveType::Extrude)
m_layers_zs.emplace_back(static_cast<double>(move.position[2])); m_layers_zs.emplace_back(static_cast<double>(move.position[2]));
m_roles.emplace_back(move.extrusion_role);
m_extruder_ids.emplace_back(move.extruder_id); m_extruder_ids.emplace_back(move.extruder_id);
if (i > 0)
m_roles.emplace_back(move.extrusion_role);
} }
// layers zs -> replace intervals of layers with similar top positions with their average value. // layers zs -> replace intervals of layers with similar top positions with their average value.
@ -560,9 +596,13 @@ void GCodeViewer::refresh_render_paths() const
for (IBuffer& buffer : m_buffers) { for (IBuffer& buffer : m_buffers) {
buffer.render_paths = std::vector<RenderPath>(); buffer.render_paths = std::vector<RenderPath>();
for (const Path& path : buffer.paths) for (size_t i = 0; i < buffer.paths.size(); ++i) {
{ const Path& path = buffer.paths[i];
if (!is_in_z_range(path)) if (path.type == GCodeProcessor::EMoveType::Travel) {
if (!is_travel_in_z_range(i))
continue;
}
else if (!is_in_z_range(path))
continue; continue;
if (path.type == GCodeProcessor::EMoveType::Extrude && !is_visible(path)) if (path.type == GCodeProcessor::EMoveType::Extrude && !is_visible(path))
@ -576,8 +616,7 @@ void GCodeViewer::refresh_render_paths() const
} }
auto it = std::find_if(buffer.render_paths.begin(), buffer.render_paths.end(), [color](const RenderPath& path) { return path.color == color; }); auto it = std::find_if(buffer.render_paths.begin(), buffer.render_paths.end(), [color](const RenderPath& path) { return path.color == color; });
if (it == buffer.render_paths.end()) if (it == buffer.render_paths.end()) {
{
it = buffer.render_paths.insert(buffer.render_paths.end(), RenderPath()); it = buffer.render_paths.insert(buffer.render_paths.end(), RenderPath());
it->color = color; it->color = color;
} }
@ -1053,34 +1092,79 @@ void GCodeViewer::render_statistics() const
ImGui::SameLine(offset); ImGui::SameLine(offset);
imgui.text(std::to_string(m_statistics.results_size) + " bytes"); imgui.text(std::to_string(m_statistics.results_size) + " bytes");
ImGui::Separator();
ImGui::PushStyleColor(ImGuiCol_Text, ORANGE); ImGui::PushStyleColor(ImGuiCol_Text, ORANGE);
imgui.text(std::string("Vertices CPU:")); imgui.text(std::string("Vertices CPU:"));
ImGui::PopStyleColor(); ImGui::PopStyleColor();
ImGui::SameLine(offset); ImGui::SameLine(offset);
imgui.text(std::to_string(m_statistics.vertices_size) + " bytes"); imgui.text(std::to_string(m_statistics.vertices_size) + " bytes");
ImGui::PushStyleColor(ImGuiCol_Text, ORANGE);
imgui.text(std::string("Vertices GPU:"));
ImGui::PopStyleColor();
ImGui::SameLine(offset);
imgui.text(std::to_string(m_statistics.vertices_gpu_size) + " bytes");
ImGui::PushStyleColor(ImGuiCol_Text, ORANGE); ImGui::PushStyleColor(ImGuiCol_Text, ORANGE);
imgui.text(std::string("Indices CPU:")); imgui.text(std::string("Indices CPU:"));
ImGui::PopStyleColor(); ImGui::PopStyleColor();
ImGui::SameLine(offset); ImGui::SameLine(offset);
imgui.text(std::to_string(m_statistics.indices_size) + " bytes"); imgui.text(std::to_string(m_statistics.indices_size) + " bytes");
ImGui::PushStyleColor(ImGuiCol_Text, ORANGE);
imgui.text(std::string("Paths CPU:"));
ImGui::PopStyleColor();
ImGui::SameLine(offset);
imgui.text(std::to_string(m_statistics.paths_size) + " bytes");
ImGui::PushStyleColor(ImGuiCol_Text, ORANGE);
imgui.text(std::string("TOTAL CPU:"));
ImGui::PopStyleColor();
ImGui::SameLine(offset);
imgui.text(std::to_string(m_statistics.vertices_size + m_statistics.indices_size + m_statistics.paths_size) + " bytes");
ImGui::Separator();
ImGui::PushStyleColor(ImGuiCol_Text, ORANGE);
imgui.text(std::string("Vertices GPU:"));
ImGui::PopStyleColor();
ImGui::SameLine(offset);
imgui.text(std::to_string(m_statistics.vertices_gpu_size) + " bytes");
ImGui::PushStyleColor(ImGuiCol_Text, ORANGE); ImGui::PushStyleColor(ImGuiCol_Text, ORANGE);
imgui.text(std::string("Indices GPU:")); imgui.text(std::string("Indices GPU:"));
ImGui::PopStyleColor(); ImGui::PopStyleColor();
ImGui::SameLine(offset); ImGui::SameLine(offset);
imgui.text(std::to_string(m_statistics.indices_gpu_size) + " bytes"); imgui.text(std::to_string(m_statistics.indices_gpu_size) + " bytes");
ImGui::PushStyleColor(ImGuiCol_Text, ORANGE);
imgui.text(std::string("TOTAL GPU:"));
ImGui::PopStyleColor();
ImGui::SameLine(offset);
imgui.text(std::to_string(m_statistics.vertices_gpu_size + m_statistics.indices_gpu_size) + " bytes");
imgui.end(); imgui.end();
} }
#endif // ENABLE_GCODE_VIEWER_STATISTICS #endif // ENABLE_GCODE_VIEWER_STATISTICS
bool GCodeViewer::is_travel_in_z_range(size_t id) const
{
const IBuffer& buffer = m_buffers[buffer_id(GCodeProcessor::EMoveType::Travel)];
if (id >= buffer.paths.size())
return false;
Path path = buffer.paths[id];
int first = static_cast<int>(id);
unsigned int last = static_cast<unsigned int>(id);
// check adjacent paths
while (first > 0 && path.first.position.isApprox(buffer.paths[first - 1].last.position)) {
--first;
path.first = buffer.paths[first].first;
}
while (last < static_cast<unsigned int>(buffer.paths.size() - 1) && path.last.position.isApprox(buffer.paths[last + 1].first.position)) {
++last;
path.last = buffer.paths[last].last;
}
return is_in_z_range(path);
}
} // namespace GUI } // namespace GUI
} // namespace Slic3r } // namespace Slic3r

View file

@ -38,7 +38,7 @@ class GCodeViewer
struct Endpoint struct Endpoint
{ {
unsigned int id{ 0u }; unsigned int id{ 0u };
double z{ 0.0 }; Vec3f position{ Vec3f::Zero() };
}; };
GCodeProcessor::EMoveType type{ GCodeProcessor::EMoveType::Noop }; GCodeProcessor::EMoveType type{ GCodeProcessor::EMoveType::Noop };
@ -54,11 +54,7 @@ class GCodeViewer
unsigned char extruder_id{ 0 }; unsigned char extruder_id{ 0 };
unsigned char cp_color_id{ 0 }; unsigned char cp_color_id{ 0 };
bool matches(const GCodeProcessor::MoveVertex& move) const { bool matches(const GCodeProcessor::MoveVertex& move) const;
return type == move.type && role == move.extrusion_role && height == move.height && width == move.width &&
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;
}
}; };
// Used to batch the indices needed to render paths // Used to batch the indices needed to render paths
@ -157,6 +153,7 @@ class GCodeViewer
long long vertices_gpu_size{ 0 }; long long vertices_gpu_size{ 0 };
long long indices_size{ 0 }; long long indices_size{ 0 };
long long indices_gpu_size{ 0 }; long long indices_gpu_size{ 0 };
long long paths_size{ 0 };
void reset_all() { void reset_all() {
reset_times(); reset_times();
@ -180,6 +177,7 @@ class GCodeViewer
vertices_gpu_size = 0; vertices_gpu_size = 0;
indices_size = 0; indices_size = 0;
indices_gpu_size = 0; indices_gpu_size = 0;
paths_size = 0;
} }
}; };
#endif // ENABLE_GCODE_VIEWER_STATISTICS #endif // ENABLE_GCODE_VIEWER_STATISTICS
@ -279,8 +277,9 @@ private:
return z > m_layers_z_range[0] - EPSILON && z < m_layers_z_range[1] + EPSILON; 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); return in_z_range(path.first.position[2]) || in_z_range(path.last.position[2]);
} }
bool is_travel_in_z_range(size_t id) const;
}; };
} // namespace GUI } // namespace GUI

View file

@ -405,6 +405,7 @@ bool Preview::init(wxWindow* parent, Bed3D& bed, Camera& camera, GLToolbar& view
bind_event_handlers(); bind_event_handlers();
#if !ENABLE_GCODE_VIEWER
// sets colors for gcode preview extrusion roles // sets colors for gcode preview extrusion roles
std::vector<std::string> extrusion_roles_colors = { std::vector<std::string> extrusion_roles_colors = {
"Perimeter", "FFFF66", "Perimeter", "FFFF66",
@ -422,6 +423,7 @@ bool Preview::init(wxWindow* parent, Bed3D& bed, Camera& camera, GLToolbar& view
"Custom", "28CC94" "Custom", "28CC94"
}; };
m_gcode_preview_data->set_extrusion_paths_colors(extrusion_roles_colors); m_gcode_preview_data->set_extrusion_paths_colors(extrusion_roles_colors);
#endif // !ENABLE_GCODE_VIEWER
return true; return true;
} }