Skip to content

Commit eca94e5

Browse files
committed
Fix simplify to not introduce artifacts
When the intersection point (I) is generated, it may end up being far away from the previous (P) and next (N) points, even though it satisfies the condition of having a small vertical distance from the previous(P)-current(C) segment. This commit fixes that by also checking whether the distance I-P and I-C are also smaller than the smallest_line_segment provided by the user. If these distances are bigger than that, then the intersection point (I) should not replace the current point (C) and C should remain inside the path. CURA-7709
1 parent 2bd38f9 commit eca94e5

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/utils/polygon.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ void PolygonRef::simplify(const coord_t smallest_line_segment_squared, const coo
350350
{
351351
next = path->at((point_idx + 1) % size());
352352
}
353-
354353
const coord_t removed_area_next = current.X * next.Y - current.Y * next.X; // Twice the Shoelace formula for area of polygon per line segment.
355354
const coord_t negative_area_closing = next.X * previous.Y - next.Y * previous.X; // area between the origin and the short-cutting segment
356355
accumulated_area_removed += removed_area_next;
@@ -386,11 +385,14 @@ void PolygonRef::simplify(const coord_t smallest_line_segment_squared, const coo
386385
{
387386
// Special case; The next line is long. If we were to remove this, it could happen that we get quite noticeable artifacts.
388387
// We should instead move this point to a location where both edges are kept and then remove the previous point that we wanted to keep.
389-
// By taking the intersection of these two lines, we get a point that perseves the direction (so it makes the corner a bit more pointy)
388+
// By taking the intersection of these two lines, we get a point that preserves the direction (so it makes the corner a bit more pointy).
389+
// We just need to be sure that the intersection point does not introduce an artifact itself.
390390
Point intersection_point;
391391
bool has_intersection = LinearAlg2D::lineLineIntersection(previous_previous, previous, current, next, intersection_point);
392-
393-
if (!has_intersection || LinearAlg2D::getDist2FromLine(intersection_point, previous, current) > allowed_error_distance_squared)
392+
if (!has_intersection
393+
|| LinearAlg2D::getDist2FromLine(intersection_point, previous, current) > allowed_error_distance_squared
394+
|| vSize2(intersection_point - previous) > smallest_line_segment_squared // The intersection point is way too far from the 'previous'
395+
|| vSize2(intersection_point - next) > smallest_line_segment_squared) // and 'next' points, so it shouldn't replace 'current'
394396
{
395397
if(length2 < 25)
396398
{
@@ -404,7 +406,6 @@ void PolygonRef::simplify(const coord_t smallest_line_segment_squared, const coo
404406
{
405407
// New point seems like a valid one.
406408
current = intersection_point;
407-
408409
// If there was a previous point added, remove it.
409410
if(new_path.size() > 0)
410411
{
@@ -417,7 +418,6 @@ void PolygonRef::simplify(const coord_t smallest_line_segment_squared, const coo
417418
continue; //Remove the vertex.
418419
}
419420
}
420-
421421
//Don't remove the vertex.
422422
accumulated_area_removed = removed_area_next; // so that in the next iteration it's the area between the origin, [previous] and [current]
423423
previous_previous = previous;

0 commit comments

Comments
 (0)