GCodeViewer -> Use rounded values for toolpaths height, width and volumetric rate to reduce the number of generated paths

This commit is contained in:
enricoturri1966 2020-08-17 13:07:13 +02:00
parent 5b579aee9a
commit b156153405
2 changed files with 43 additions and 37 deletions

View file

@ -85,19 +85,6 @@ static float acceleration_time_from_distance(float initial_feedrate, float dista
return (acceleration != 0.0f) ? (speed_from_distance(initial_feedrate, distance, acceleration) - initial_feedrate) / acceleration : 0.0f;
}
float round_to_nearest(float value, unsigned int decimals)
{
float res = 0.0f;
if (decimals == 0)
res = std::round(value);
else {
char buf[64];
sprintf(buf, "%.*g", decimals, value);
res = std::stof(buf);
}
return res;
}
void GCodeProcessor::CachedPosition::reset()
{
std::fill(position.begin(), position.end(), FLT_MAX);
@ -1392,13 +1379,13 @@ void GCodeProcessor::process_G1(const GCodeReader::GCodeLine& line)
float area_toolpath_cross_section = volume_extruded_filament / d_xyz;
// volume extruded filament / tool displacement = area toolpath cross section
m_mm3_per_mm = round_to_nearest(area_toolpath_cross_section, 3);
m_mm3_per_mm = area_toolpath_cross_section;
#if ENABLE_GCODE_VIEWER_DATA_CHECKING
m_mm3_per_mm_compare.update(area_toolpath_cross_section, m_extrusion_role);
#endif // ENABLE_GCODE_VIEWER_DATA_CHECKING
if (m_end_position[Z] > m_extruded_last_z + EPSILON) {
m_height = round_to_nearest(m_end_position[Z] - m_extruded_last_z, 4);
m_height = m_end_position[Z] - m_extruded_last_z;
#if ENABLE_GCODE_VIEWER_DATA_CHECKING
m_height_compare.update(m_height, m_extrusion_role);
#endif // ENABLE_GCODE_VIEWER_DATA_CHECKING
@ -1407,13 +1394,13 @@ void GCodeProcessor::process_G1(const GCodeReader::GCodeLine& line)
if (m_extrusion_role == erExternalPerimeter)
// cross section: rectangle
m_width = round_to_nearest(delta_pos[E] * static_cast<float>(M_PI * sqr(1.05 * filament_radius)) / (d_xyz * m_height), 3);
m_width = delta_pos[E] * static_cast<float>(M_PI * sqr(1.05 * filament_radius)) / (d_xyz * m_height);
else if (m_extrusion_role == erBridgeInfill || m_extrusion_role == erNone)
// cross section: circle
m_width = round_to_nearest(static_cast<float>(m_filament_diameters[m_extruder_id]) * std::sqrt(delta_pos[E] / d_xyz), 3);
m_width = static_cast<float>(m_filament_diameters[m_extruder_id]) * std::sqrt(delta_pos[E] / d_xyz);
else
// cross section: rectangle + 2 semicircles
m_width = round_to_nearest(delta_pos[E] * static_cast<float>(M_PI * sqr(filament_radius)) / (d_xyz * m_height) + static_cast<float>(1.0 - 0.25 * M_PI) * m_height, 3);
m_width = delta_pos[E] * static_cast<float>(M_PI * sqr(filament_radius)) / (d_xyz * m_height) + static_cast<float>(1.0 - 0.25 * M_PI) * m_height;
#if ENABLE_GCODE_VIEWER_DATA_CHECKING
m_width_compare.update(m_width, m_extrusion_role);
@ -1983,19 +1970,20 @@ void GCodeProcessor::process_T(const std::string& command)
void GCodeProcessor::store_move_vertex(EMoveType type)
{
MoveVertex vertex;
vertex.type = type;
vertex.extrusion_role = m_extrusion_role;
vertex.position = Vec3f(m_end_position[X], m_end_position[Y], m_end_position[Z]) + m_extruder_offsets[m_extruder_id];
vertex.delta_extruder = m_end_position[E] - m_start_position[E];
vertex.feedrate = m_feedrate;
vertex.width = m_width;
vertex.height = m_height;
vertex.mm3_per_mm = m_mm3_per_mm;
vertex.fan_speed = m_fan_speed;
vertex.extruder_id = m_extruder_id;
vertex.cp_color_id = m_cp_color.current;
vertex.time = static_cast<float>(m_result.moves.size());
MoveVertex vertex = {
type,
m_extrusion_role,
m_extruder_id,
m_cp_color.current,
Vec3f(m_end_position[X], m_end_position[Y], m_end_position[Z]) + m_extruder_offsets[m_extruder_id],
m_end_position[E] - m_start_position[E],
m_feedrate,
m_width,
m_height,
m_mm3_per_mm,
m_fan_speed,
static_cast<float>(m_result.moves.size())
};
m_result.moves.emplace_back(vertex);
}

View file

@ -38,7 +38,7 @@ static EMoveType buffer_type(unsigned char id) {
return static_cast<EMoveType>(static_cast<unsigned char>(EMoveType::Retract) + id);
}
std::array<float, 3> decode_color(const std::string& color) {
static std::array<float, 3> decode_color(const std::string& color) {
static const float INV_255 = 1.0f / 255.0f;
std::array<float, 3> ret = { 0.0f, 0.0f, 0.0f };
@ -56,7 +56,7 @@ std::array<float, 3> decode_color(const std::string& color) {
return ret;
}
std::vector<std::array<float, 3>> decode_colors(const std::vector<std::string>& colors) {
static std::vector<std::array<float, 3>> decode_colors(const std::vector<std::string>& colors) {
std::vector<std::array<float, 3>> output(colors.size(), { 0.0f, 0.0f, 0.0f });
for (size_t i = 0; i < colors.size(); ++i) {
output[i] = decode_color(colors[i]);
@ -64,6 +64,19 @@ std::vector<std::array<float, 3>> decode_colors(const std::vector<std::string>&
return output;
}
static float round_to_nearest(float value, unsigned int decimals)
{
float res = 0.0f;
if (decimals == 0)
res = std::round(value);
else {
char buf[64];
sprintf(buf, "%.*g", decimals, value);
res = std::stof(buf);
}
return res;
}
void GCodeViewer::VBuffer::reset()
{
// release gpu memory
@ -98,9 +111,11 @@ bool GCodeViewer::Path::matches(const GCodeProcessor::MoveVertex& move) const
case EMoveType::Unretract:
case 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;
// use rounding to reduce the number of generated paths
return type == move.type && role == move.extrusion_role && height == round_to_nearest(move.height, 2) &&
width == round_to_nearest(move.width, 2) && feedrate == move.feedrate && fan_speed == move.fan_speed &&
volumetric_rate == round_to_nearest(move.volumetric_rate(), 2) && extruder_id == move.extruder_id &&
cp_color_id == move.cp_color_id;
}
case EMoveType::Travel:
{
@ -124,7 +139,10 @@ void GCodeViewer::TBuffer::reset()
void GCodeViewer::TBuffer::add_path(const GCodeProcessor::MoveVertex& move, unsigned int i_id, unsigned int s_id)
{
Path::Endpoint endpoint = { i_id, s_id, 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 });
// use rounding to reduce the number of generated paths
paths.push_back({ move.type, move.extrusion_role, endpoint, endpoint, move.delta_extruder,
round_to_nearest(move.height, 2), round_to_nearest(move.width, 2), move.feedrate, move.fan_speed,
round_to_nearest(move.volumetric_rate(), 2), move.extruder_id, move.cp_color_id });
}
GCodeViewer::Color GCodeViewer::Extrusions::Range::get_color_at(float value) const