diff --git a/src/libslic3r/Arachne/utils/ExtrusionLine.cpp b/src/libslic3r/Arachne/utils/ExtrusionLine.cpp index ac7b88af83..35233f1075 100644 --- a/src/libslic3r/Arachne/utils/ExtrusionLine.cpp +++ b/src/libslic3r/Arachne/utils/ExtrusionLine.cpp @@ -153,10 +153,19 @@ void ExtrusionLine::simplify(const int64_t smallest_line_segment_squared, const // ^ current Point intersection_point; bool has_intersection = Line(previous_previous.p, previous.p).intersection_infinite(Line(current.p, next.p), &intersection_point); + const auto dist_greater = [](const Point& p1, const Point& p2, const int64_t threshold) { + const auto vec = (p1 - p2).cwiseAbs().cast(); + if(vec.x() > threshold || vec.y() > threshold) { + // If this condition is true, the distance is definitely greater than the threshold. + // We don't need to calculate the squared norm at all, which avoid potential arithmetic overflow. + return true; + } + return vec.squaredNorm() > threshold; + }; if (!has_intersection || Line::distance_to_infinite_squared(intersection_point, previous.p, current.p) > double(allowed_error_distance_squared) - || (intersection_point - previous.p).cast().squaredNorm() > smallest_line_segment_squared // The intersection point is way too far from the 'previous' - || (intersection_point - current.p).cast().squaredNorm() > smallest_line_segment_squared) // and 'current' points, so it shouldn't replace 'current' + || dist_greater(intersection_point, previous.p, smallest_line_segment_squared) // The intersection point is way too far from the 'previous' + || dist_greater(intersection_point, current.p, smallest_line_segment_squared)) // and 'current' points, so it shouldn't replace 'current' { // We can't find a better spot for it, but the size of the line is more than 5 micron. // So the only thing we can do here is leave it in...