Skip to content

Commit

Permalink
Detection of curled edges to enhance slowdown for overhangs algorithm (
Browse files Browse the repository at this point in the history
…#2056)

* Overhang perimeter handling

Updated code to handle overhang perimeters as an overhang and not as a bridge.

* Preparing to add curled extrusions identification

* Porting curling calculations from Prusa Slier 2.6.1

* Prototype 1 - slowdown extended to detect curled edges and further reduce speed

First prototype of the code submitted.

* Working prototype - 2

Code is now finally working - external perimeters are slowed down as needed when there is likelyhood of curling up.

ToDo:
1. Reslicing the model causes the algorithm not to run - need to find where this fails to trigger the call for this.
2. Slowdown of internal perimeters not working yet.

* Updated to use overhang wall speed instead of bridging speed for this algorithm

* Fixed bug in speed calculation and tweaked parameters for high speed printer

Fixed bug in speed calculation and tweaked parameters for high speed printer

* Attempting to fix "set started" not being set

* Parameter tweak after print tests

* Fixed estimation not running when model is re-sliced.

* Removing debug printf statements and fixed threading flag.

* Fixed threading

* Parameter tweaks following print tests

* Made this as an option in the GUI

* Reintroduced handling of bridges as per original design

* UI line toggling when option makes sense to be visible.

* Fixed bug in field visibility & made it default to off

* Code optimisation

---------

Co-authored-by: SoftFever <[email protected]>
  • Loading branch information
igiannakas and SoftFever authored Sep 16, 2023
1 parent bcedb43 commit 61437b2
Show file tree
Hide file tree
Showing 16 changed files with 325 additions and 146 deletions.
9 changes: 5 additions & 4 deletions src/libslic3r/AABBTreeLines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ namespace AABBTreeLines {
AABBTreeIndirect::detail::indexed_primitives_within_distance_squared_recurisve(distancer, size_t(0), max_distance_squared, found_lines);
return found_lines;
}


// return 1 if true, -1 if false, 0 for point on contour (or if cannot be determined)
template <typename LineType, typename TreeType, typename VectorType>
Expand Down Expand Up @@ -350,10 +351,10 @@ namespace AABBTreeLines {
return dist;
}

std::vector<size_t> all_lines_in_radius(const Vec<2, typename LineType::Scalar>& point, Floating radius)
{
return all_lines_in_radius(this->lines, this->tree, point, radius * radius);
}
std::vector<size_t> all_lines_in_radius(const Vec<2, Scalar> &point, Floating radius)
{
return AABBTreeLines::all_lines_in_radius(this->lines, this->tree, point.template cast<Floating>(), radius * radius);
}

template <bool sorted>
std::vector<std::pair<Vec<2, Scalar>, size_t>> intersections_with_line(const LineType& line) const
Expand Down
8 changes: 8 additions & 0 deletions src/libslic3r/ExtrusionEntityCollection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class ExtrusionEntityCollection : public ExtrusionEntity
~ExtrusionEntityCollection() { clear(); }
explicit operator ExtrusionPaths() const;

ExtrusionEntitiesPtr::const_iterator cbegin() const { return this->entities.cbegin(); }
ExtrusionEntitiesPtr::const_iterator cend() const { return this->entities.cend(); }
ExtrusionEntitiesPtr::const_iterator begin() const { return this->entities.cbegin(); }
ExtrusionEntitiesPtr::const_iterator end() const { return this->entities.cend(); }
ExtrusionEntitiesPtr::iterator begin() { return this->entities.begin(); }
ExtrusionEntitiesPtr::iterator end() { return this->entities.end(); }

bool is_collection() const override { return true; }
ExtrusionRole role() const override {
ExtrusionRole out = erNone;
Expand Down Expand Up @@ -112,6 +119,7 @@ class ExtrusionEntityCollection : public ExtrusionEntity
Polygons polygons_covered_by_spacing(const float scaled_epsilon = 0.f) const
{ Polygons out; this->polygons_covered_by_spacing(out, scaled_epsilon); return out; }
size_t items_count() const;
size_t size() const { return entities.size(); }
/// Returns a flattened copy of this ExtrusionEntityCollection. That is, all of the items in its entities vector are not collections.
/// You should be iterating over flatten().entities if you are interested in the underlying ExtrusionEntities (and don't care about hierarchy).
/// \param preserve_ordering Flag to method that will flatten if and only if the underlying collection is sortable when True (default: False).
Expand Down
89 changes: 61 additions & 28 deletions src/libslic3r/GCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4530,35 +4530,68 @@ std::string GCode::_extrude(const ExtrusionPath &path, std::string description,

if (m_config.enable_overhang_speed && !m_config.overhang_speed_classic && !this->on_first_layer() &&
(is_bridge(path.role()) || is_perimeter(path.role()))) {
double out_wall_ref_speed = m_config.get_abs_value("outer_wall_speed");
ConfigOptionPercents overhang_overlap_levels({75, 50, 25, 13, 12.99, 0});
ConfigOptionFloatsOrPercents dynamic_overhang_speeds(
{(m_config.get_abs_value("overhang_1_4_speed") < 0.5) ?
FloatOrPercent{100, true} :
FloatOrPercent{m_config.get_abs_value("overhang_1_4_speed") * 100 / out_wall_ref_speed, true},
(m_config.get_abs_value("overhang_2_4_speed") < 0.5) ?
FloatOrPercent{100, true} :
FloatOrPercent{m_config.get_abs_value("overhang_2_4_speed") * 100 / out_wall_ref_speed, true},
(m_config.get_abs_value("overhang_3_4_speed") < 0.5) ?
FloatOrPercent{100, true} :
FloatOrPercent{m_config.get_abs_value("overhang_3_4_speed") * 100 / out_wall_ref_speed, true},
(m_config.get_abs_value("overhang_4_4_speed") < 0.5) ?
FloatOrPercent{100, true} :
FloatOrPercent{m_config.get_abs_value("overhang_4_4_speed") * 100 / out_wall_ref_speed, true},
FloatOrPercent{m_config.get_abs_value("bridge_speed") * 100 / out_wall_ref_speed, true},
FloatOrPercent{m_config.get_abs_value("bridge_speed") * 100 / out_wall_ref_speed, true}});

if (out_wall_ref_speed == 0)
out_wall_ref_speed = EXTRUDER_CONFIG(filament_max_volumetric_speed) / _mm3_per_mm;

if (EXTRUDER_CONFIG(filament_max_volumetric_speed) > 0) {
out_wall_ref_speed = std::min(out_wall_ref_speed, EXTRUDER_CONFIG(filament_max_volumetric_speed) / path.mm3_per_mm);
}

new_points = m_extrusion_quality_estimator.estimate_extrusion_quality(path, overhang_overlap_levels, dynamic_overhang_speeds,
out_wall_ref_speed, speed);

double out_wall_ref_speed = m_config.get_abs_value("outer_wall_speed");
ConfigOptionPercents overhang_overlap_levels({75, 50, 25, 13, 12.99, 0});

if (m_config.slowdown_for_curled_perimeters){
ConfigOptionFloatsOrPercents dynamic_overhang_speeds(
{(m_config.get_abs_value("overhang_1_4_speed") < 0.5) ?
FloatOrPercent{100, true} :
FloatOrPercent{m_config.get_abs_value("overhang_1_4_speed") * 100 / out_wall_ref_speed, true},
(m_config.get_abs_value("overhang_2_4_speed") < 0.5) ?
FloatOrPercent{100, true} :
FloatOrPercent{m_config.get_abs_value("overhang_2_4_speed") * 100 / out_wall_ref_speed, true},
(m_config.get_abs_value("overhang_3_4_speed") < 0.5) ?
FloatOrPercent{100, true} :
FloatOrPercent{m_config.get_abs_value("overhang_3_4_speed") * 100 / out_wall_ref_speed, true},
(m_config.get_abs_value("overhang_4_4_speed") < 0.5) ?
FloatOrPercent{100, true} :
FloatOrPercent{m_config.get_abs_value("overhang_4_4_speed") * 100 / out_wall_ref_speed, true},
(m_config.get_abs_value("overhang_4_4_speed") < 0.5) ?
FloatOrPercent{100, true} :
FloatOrPercent{m_config.get_abs_value("overhang_4_4_speed") * 100 / out_wall_ref_speed, true},
(m_config.get_abs_value("overhang_4_4_speed") < 0.5) ?
FloatOrPercent{100, true} :
FloatOrPercent{m_config.get_abs_value("overhang_4_4_speed") * 100 / out_wall_ref_speed, true}});
if (out_wall_ref_speed == 0)
out_wall_ref_speed = EXTRUDER_CONFIG(filament_max_volumetric_speed) / _mm3_per_mm;

if (EXTRUDER_CONFIG(filament_max_volumetric_speed) > 0) {
out_wall_ref_speed = std::min(out_wall_ref_speed, EXTRUDER_CONFIG(filament_max_volumetric_speed) / path.mm3_per_mm);
}

new_points = m_extrusion_quality_estimator.estimate_extrusion_quality(path, overhang_overlap_levels, dynamic_overhang_speeds,
out_wall_ref_speed, speed, m_config.slowdown_for_curled_perimeters);
}else{
ConfigOptionFloatsOrPercents dynamic_overhang_speeds(
{(m_config.get_abs_value("overhang_1_4_speed") < 0.5) ?
FloatOrPercent{100, true} :
FloatOrPercent{m_config.get_abs_value("overhang_1_4_speed") * 100 / out_wall_ref_speed, true},
(m_config.get_abs_value("overhang_2_4_speed") < 0.5) ?
FloatOrPercent{100, true} :
FloatOrPercent{m_config.get_abs_value("overhang_2_4_speed") * 100 / out_wall_ref_speed, true},
(m_config.get_abs_value("overhang_3_4_speed") < 0.5) ?
FloatOrPercent{100, true} :
FloatOrPercent{m_config.get_abs_value("overhang_3_4_speed") * 100 / out_wall_ref_speed, true},
(m_config.get_abs_value("overhang_4_4_speed") < 0.5) ?
FloatOrPercent{100, true} :
FloatOrPercent{m_config.get_abs_value("overhang_4_4_speed") * 100 / out_wall_ref_speed, true},
FloatOrPercent{m_config.get_abs_value("bridge_speed") * 100 / out_wall_ref_speed, true},
FloatOrPercent{m_config.get_abs_value("bridge_speed") * 100 / out_wall_ref_speed, true}});

if (out_wall_ref_speed == 0)
out_wall_ref_speed = EXTRUDER_CONFIG(filament_max_volumetric_speed) / _mm3_per_mm;

if (EXTRUDER_CONFIG(filament_max_volumetric_speed) > 0) {
out_wall_ref_speed = std::min(out_wall_ref_speed, EXTRUDER_CONFIG(filament_max_volumetric_speed) / path.mm3_per_mm);
}

new_points = m_extrusion_quality_estimator.estimate_extrusion_quality(path, overhang_overlap_levels, dynamic_overhang_speeds,
out_wall_ref_speed, speed, m_config.slowdown_for_curled_perimeters);
}
variable_speed = std::any_of(new_points.begin(), new_points.end(), [speed](const ProcessedPoint &p) { return p.speed != speed; });

variable_speed = std::any_of(new_points.begin(), new_points.end(), [speed](const ProcessedPoint &p) { return p.speed != speed; });
}

double F = speed * 60; // convert mm/sec to mm/min
Expand Down
69 changes: 66 additions & 3 deletions src/libslic3r/GCode/ExtrusionProcessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ class ExtrusionQualityEstimator
{
std::unordered_map<const PrintObject *, AABBTreeLines::LinesDistancer<Linef>> prev_layer_boundaries;
std::unordered_map<const PrintObject *, AABBTreeLines::LinesDistancer<Linef>> next_layer_boundaries;
std::unordered_map<const PrintObject *, AABBTreeLines::LinesDistancer<CurledLine>> prev_curled_extrusions;
std::unordered_map<const PrintObject *, AABBTreeLines::LinesDistancer<CurledLine>> next_curled_extrusions;
const PrintObject *current_object;

public:
Expand All @@ -258,16 +260,22 @@ class ExtrusionQualityEstimator
const PrintObject *object = obj;
prev_layer_boundaries[object] = next_layer_boundaries[object];
next_layer_boundaries[object] = AABBTreeLines::LinesDistancer<Linef>{to_unscaled_linesf(layer->lslices)};
prev_curled_extrusions[object] = next_curled_extrusions[object];
next_curled_extrusions[object] = AABBTreeLines::LinesDistancer<CurledLine>{layer->curled_lines};
}

std::vector<ProcessedPoint> estimate_extrusion_quality(const ExtrusionPath &path,
const ConfigOptionPercents &overlaps,
const ConfigOptionFloatsOrPercents &speeds,
float ext_perimeter_speed,
float original_speed)
float original_speed,
bool slowdown_for_curled_edges)
{
size_t speed_sections_count = std::min(overlaps.values.size(), speeds.values.size());
std::vector<std::pair<float, float>> speed_sections;



for (size_t i = 0; i < speed_sections_count; i++) {
float distance = path.width * (1.0 - (overlaps.get_at(i) / 100.0));
float speed = speeds.get_at(i).percent ? (ext_perimeter_speed * speeds.get_at(i).value / 100.0) : speeds.get_at(i).value;
Expand Down Expand Up @@ -297,6 +305,56 @@ class ExtrusionQualityEstimator
for (size_t i = 0; i < extended_points.size(); i++) {
const ExtendedPoint &curr = extended_points[i];
const ExtendedPoint &next = extended_points[i + 1 < extended_points.size() ? i + 1 : i];

float artificial_distance_to_curled_lines = 0.0;
if(slowdown_for_curled_edges) {
// The following code artifically increases the distance to provide slowdown for extrusions that are over curled lines
const double dist_limit = 10.0 * path.width;
{
Vec2d middle = 0.5 * (curr.position + next.position);
auto line_indices = prev_curled_extrusions[current_object].all_lines_in_radius(Point::new_scale(middle), scale_(dist_limit));
if (!line_indices.empty()) {
double len = (next.position - curr.position).norm();
// For long lines, there is a problem with the additional slowdown. If by accident, there is small curled line near the middle of this long line
// The whole segment gets slower unnecesarily. For these long lines, we do additional check whether it is worth slowing down.
// NOTE that this is still quite rough approximation, e.g. we are still checking lines only near the middle point
// TODO maybe split the lines into smaller segments before running this alg? but can be demanding, and GCode will be huge
if (len > 8) {
Vec2d dir = Vec2d(next.position - curr.position) / len;
Vec2d right = Vec2d(-dir.y(), dir.x());

Polygon box_of_influence = {
scaled(Vec2d(curr.position + right * dist_limit)),
scaled(Vec2d(next.position + right * dist_limit)),
scaled(Vec2d(next.position - right * dist_limit)),
scaled(Vec2d(curr.position - right * dist_limit)),
};

double projected_lengths_sum = 0;
for (size_t idx : line_indices) {
const CurledLine &line = prev_curled_extrusions[current_object].get_line(idx);
Lines inside = intersection_ln({{line.a, line.b}}, {box_of_influence});
if (inside.empty())
continue;
double projected_length = abs(dir.dot(unscaled(Vec2d((inside.back().b - inside.back().a).cast<double>()))));
projected_lengths_sum += projected_length;
}
if (projected_lengths_sum < 0.4 * len) {
line_indices.clear();
}
}

for (size_t idx : line_indices) {
const CurledLine &line = prev_curled_extrusions[current_object].get_line(idx);
float distance_from_curled = unscaled(line_alg::distance_to(line, Point::new_scale(middle)));
float dist = path.width * (1.0 - (distance_from_curled / dist_limit)) *
(1.0 - (distance_from_curled / dist_limit)) *
(line.curled_height / (path.height * 10.0f)); // max_curled_height_factor from SupportSpotGenerator
artificial_distance_to_curled_lines = std::max(artificial_distance_to_curled_lines, dist);
}
}
}
}

auto calculate_speed = [&speed_sections, &original_speed](float distance) {
float final_speed;
Expand All @@ -316,10 +374,15 @@ class ExtrusionQualityEstimator
}
return final_speed;
};

float extrusion_speed = std::min(calculate_speed(curr.distance), calculate_speed(next.distance));
if(slowdown_for_curled_edges) {
float curled_speed = calculate_speed(artificial_distance_to_curled_lines);
extrusion_speed = std::min(curled_speed, extrusion_speed); // adjust extrusion speed based on what is smallest - the calculated overhang speed or the artificial curled speed
}

float overlap = std::min(1 - curr.distance * width_inv, 1 - next.distance * width_inv);

processed_points.push_back({ scaled(curr.position), extrusion_speed, overlap });
}
return processed_points;
Expand Down
3 changes: 3 additions & 0 deletions src/libslic3r/Layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ class Layer
coordf_t height; // layer height in unscaled coordinates
coordf_t bottom_z() const { return this->print_z - this->height; }

//Extrusions estimated to be seriously malformed, estimated during "Estimating curled extrusions" step. These lines should be avoided during fast travels.
CurledLines curled_lines;

// BBS
mutable ExPolygons sharp_tails;
mutable ExPolygons cantilevers;
Expand Down
12 changes: 12 additions & 0 deletions src/libslic3r/Line.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ class ThickLine : public Line
double a_width, b_width;
};

class CurledLine : public Line
{
public:
CurledLine() : curled_height(0.0f) {}
CurledLine(const Point& a, const Point& b) : Line(a, b), curled_height(0.0f) {}
CurledLine(const Point& a, const Point& b, float curled_height) : Line(a, b), curled_height(curled_height) {}

float curled_height;
};

using CurledLines = std::vector<CurledLine>;

class Line3
{
public:
Expand Down
2 changes: 1 addition & 1 deletion src/libslic3r/Preset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ static std::vector<std::string> s_Preset_print_options {
"tree_support_branch_diameter", "tree_support_branch_diameter_angle", "tree_support_branch_diameter_double_wall",
"detect_narrow_internal_solid_infill",
"gcode_add_line_number", "enable_arc_fitting", "infill_combination", /*"adaptive_layer_height",*/
"support_bottom_interface_spacing", "enable_overhang_speed", "overhang_1_4_speed", "overhang_2_4_speed", "overhang_3_4_speed", "overhang_4_4_speed",
"support_bottom_interface_spacing", "enable_overhang_speed", "slowdown_for_curled_perimeters", "overhang_1_4_speed", "overhang_2_4_speed", "overhang_3_4_speed", "overhang_4_4_speed",
"initial_layer_infill_speed", "only_one_wall_top",
"timelapse_type", "internal_bridge_support_thickness",
"wall_generator", "wall_transition_length", "wall_transition_filter_deviation", "wall_transition_angle",
Expand Down
11 changes: 11 additions & 0 deletions src/libslic3r/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ bool Print::invalidate_state_by_config_options(const ConfigOptionResolver & /* n
|| opt_key == "enable_arc_fitting"
|| opt_key == "wall_infill_order") {
osteps.emplace_back(posPerimeters);
osteps.emplace_back(posEstimateCurledExtrusions);
osteps.emplace_back(posInfill);
osteps.emplace_back(posSupportMaterial);
osteps.emplace_back(posSimplifyPath);
Expand Down Expand Up @@ -1666,6 +1667,15 @@ void Print::process(bool use_cache)
obj->set_done(posPerimeters);
}
}
for (PrintObject *obj : m_objects) {
if (need_slicing_objects.count(obj) != 0) {
obj->estimate_curled_extrusions();
}
else {
if (obj->set_started(posEstimateCurledExtrusions))
obj->set_done(posEstimateCurledExtrusions);
}
}
for (PrintObject *obj : m_objects) {
if (need_slicing_objects.count(obj) != 0) {
obj->infill();
Expand Down Expand Up @@ -1723,6 +1733,7 @@ void Print::process(bool use_cache)
obj->infill();
obj->ironing();
obj->generate_support_material();
obj->estimate_curled_extrusions();
}
}
}
Expand Down
Loading

0 comments on commit 61437b2

Please sign in to comment.