Code optimisation

This commit is contained in:
igiannakas 2023-09-12 12:33:14 +01:00
parent 03c644f4c4
commit 3f8cfa74ea

View file

@ -306,53 +306,55 @@ public:
const ExtendedPoint &curr = extended_points[i]; const ExtendedPoint &curr = extended_points[i];
const ExtendedPoint &next = extended_points[i + 1 < extended_points.size() ? i + 1 : i]; const ExtendedPoint &next = extended_points[i + 1 < extended_points.size() ? i + 1 : i];
// The following code artifically increases the distance to provide slowdown for extrusions that are over curled lines
float artificial_distance_to_curled_lines = 0.0; float artificial_distance_to_curled_lines = 0.0;
const double dist_limit = 10.0 * path.width; 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); 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)); auto line_indices = prev_curled_extrusions[current_object].all_lines_in_radius(Point::new_scale(middle), scale_(dist_limit));
if (!line_indices.empty()) { if (!line_indices.empty()) {
double len = (next.position - curr.position).norm(); 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 // 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. // 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 // 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 // TODO maybe split the lines into smaller segments before running this alg? but can be demanding, and GCode will be huge
if (len > 8) { if (len > 8) {
Vec2d dir = Vec2d(next.position - curr.position) / len; Vec2d dir = Vec2d(next.position - curr.position) / len;
Vec2d right = Vec2d(-dir.y(), dir.x()); Vec2d right = Vec2d(-dir.y(), dir.x());
Polygon box_of_influence = { Polygon box_of_influence = {
scaled(Vec2d(curr.position + right * dist_limit)), scaled(Vec2d(curr.position + right * dist_limit)),
scaled(Vec2d(next.position + right * dist_limit)), scaled(Vec2d(next.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)), scaled(Vec2d(curr.position - right * dist_limit)),
}; };
double projected_lengths_sum = 0; double projected_lengths_sum = 0;
for (size_t idx : line_indices) { for (size_t idx : line_indices) {
const CurledLine &line = prev_curled_extrusions[current_object].get_line(idx); const CurledLine &line = prev_curled_extrusions[current_object].get_line(idx);
Lines inside = intersection_ln({{line.a, line.b}}, {box_of_influence}); Lines inside = intersection_ln({{line.a, line.b}}, {box_of_influence});
if (inside.empty()) if (inside.empty())
continue; continue;
double projected_length = abs(dir.dot(unscaled(Vec2d((inside.back().b - inside.back().a).cast<double>())))); double projected_length = abs(dir.dot(unscaled(Vec2d((inside.back().b - inside.back().a).cast<double>()))));
projected_lengths_sum += projected_length; projected_lengths_sum += projected_length;
} }
if (projected_lengths_sum < 0.4 * len) { if (projected_lengths_sum < 0.4 * len) {
line_indices.clear(); line_indices.clear();
} }
} }
for (size_t idx : line_indices) { for (size_t idx : line_indices) {
const CurledLine &line = prev_curled_extrusions[current_object].get_line(idx); 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 distance_from_curled = unscaled(line_alg::distance_to(line, Point::new_scale(middle)));
float dist = path.width * (1.0 - (distance_from_curled / dist_limit)) * float dist = path.width * (1.0 - (distance_from_curled / dist_limit)) *
(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 (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); artificial_distance_to_curled_lines = std::max(artificial_distance_to_curled_lines, dist);
} }
}
} }
} }
auto calculate_speed = [&speed_sections, &original_speed](float distance) { auto calculate_speed = [&speed_sections, &original_speed](float distance) {
float final_speed; float final_speed;