From b72c6e5cb0455be633da3f44415ed5848237f58e Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Wed, 3 May 2023 11:48:36 -0700 Subject: [PATCH] Avoid NaNs when applying fillets Segments that are inline with each other will create NaNs when filleting. This double check prevents NaN points from being added to the polygon --- libs/kimath/src/geometry/shape_poly_set.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/libs/kimath/src/geometry/shape_poly_set.cpp b/libs/kimath/src/geometry/shape_poly_set.cpp index e37693e053..8c521f3493 100644 --- a/libs/kimath/src/geometry/shape_poly_set.cpp +++ b/libs/kimath/src/geometry/shape_poly_set.cpp @@ -2491,6 +2491,13 @@ SHAPE_POLY_SET::POLYGON SHAPE_POLY_SET::chamferFilletPolygon( CORNER_MODE aMode, double xb = currContour.CPoint( nextVertex ).x - x1; double yb = currContour.CPoint( nextVertex ).y - y1; + // Avoid segments that will generate nans below + if( std::abs( xa + xb ) < std::numeric_limits::epsilon() + && std::abs( ya + yb ) < std::numeric_limits::epsilon() ) + { + continue; + } + // Compute the new distances double lena = hypot( xa, ya ); double lenb = hypot( xb, yb ); @@ -2573,6 +2580,9 @@ SHAPE_POLY_SET::POLYGON SHAPE_POLY_SET::chamferFilletPolygon( CORNER_MODE aMode, double nx = xc + xs; double ny = yc + ys; + if( std::isnan( nx ) || std::isnan( ny ) ) + continue; + newContour.Append( KiROUND( nx ), KiROUND( ny ) ); // Store the previous added corner to make a sanity check @@ -2584,6 +2594,9 @@ SHAPE_POLY_SET::POLYGON SHAPE_POLY_SET::chamferFilletPolygon( CORNER_MODE aMode, nx = xc + cos( startAngle + ( j + 1 ) * deltaAngle ) * radius; ny = yc - sin( startAngle + ( j + 1 ) * deltaAngle ) * radius; + if( std::isnan( nx ) || std::isnan( ny ) ) + continue; + // Sanity check: the rounding can produce repeated corners; do not add them. if( KiROUND( nx ) != prevX || KiROUND( ny ) != prevY ) {