From f92d25e00be523bcdeeba42680df28354c17fa9a Mon Sep 17 00:00:00 2001 From: Roberto Fernandez Bautista Date: Fri, 19 Aug 2022 20:49:10 +0100 Subject: [PATCH] Fix SHAPE_LINE_CHAIN::Simplify() when there are only 3 points in the chain We weren't updating m_shapes properly. Fixes https://gitlab.com/kicad/code/kicad/-/issues/11695 (cherry picked from commit 1fa1b44d0233b7cba3c39a305be19b21a9eaacf2) --- libs/kimath/src/geometry/shape_line_chain.cpp | 2 +- .../kimath/geometry/test_shape_line_chain.cpp | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/libs/kimath/src/geometry/shape_line_chain.cpp b/libs/kimath/src/geometry/shape_line_chain.cpp index 9a22b4b752..723ee2391a 100644 --- a/libs/kimath/src/geometry/shape_line_chain.cpp +++ b/libs/kimath/src/geometry/shape_line_chain.cpp @@ -1699,7 +1699,7 @@ SHAPE_LINE_CHAIN& SHAPE_LINE_CHAIN::Simplify( bool aRemoveColinear ) else if( PointCount() == 2 ) { if( m_points[0] == m_points[1] ) - m_points.pop_back(); + Remove( 1 ); return *this; } diff --git a/qa/libs/kimath/geometry/test_shape_line_chain.cpp b/qa/libs/kimath/geometry/test_shape_line_chain.cpp index 8b224037dd..5b0077e6f9 100644 --- a/qa/libs/kimath/geometry/test_shape_line_chain.cpp +++ b/qa/libs/kimath/geometry/test_shape_line_chain.cpp @@ -106,6 +106,7 @@ BOOST_AUTO_TEST_CASE( ArcToPolylineLargeCoords ) base_chain.ClearArcs(); BOOST_CHECK( GEOM_TEST::IsOutlineValid( base_chain ) ); + BOOST_CHECK_EQUAL( base_chain.CPoints().size(), base_chain.CShapes().size() ); BOOST_CHECK_EQUAL( base_chain.PointCount(), 11 ); // We should have the same number of points BOOST_CHECK_EQUAL( base_chain.ArcCount(), 0 ); // All arcs should have been removed BOOST_CHECK_EQUAL( base_chain.Area(), areaPriorToArcRemoval ); // Area should not have changed @@ -130,10 +131,30 @@ BOOST_AUTO_TEST_CASE( SetClosedDuplicatePoint ) // CLOSED CHAIN chain.SetClosed( true ); + BOOST_CHECK_EQUAL( chain.CPoints().size(), chain.CShapes().size() ); BOOST_CHECK_EQUAL( chain.PointCount(), 30 ); // (-1) should have removed coincident points BOOST_CHECK( GEOM_TEST::IsOutlineValid( chain ) ); } +// Test that duplicate point gets removed when we call simplify +BOOST_AUTO_TEST_CASE( SimplifyDuplicatePoint ) +{ + SHAPE_LINE_CHAIN chain; + + chain.Append( { 100, 100 } ); + chain.Append( { 100, 100 }, true ); //duplicate point to simplify + chain.Append( { 200, 100 } ); + + BOOST_CHECK( GEOM_TEST::IsOutlineValid( chain ) ); + BOOST_CHECK_EQUAL( chain.PointCount(), 3 ); + + chain.Simplify(); + + BOOST_CHECK_EQUAL( chain.CPoints().size(), chain.CShapes().size() ); + BOOST_CHECK_EQUAL( chain.PointCount(), 2 ); // (-1) should have removed coincident points + BOOST_CHECK( GEOM_TEST::IsOutlineValid( chain ) ); +} + // Test special case where the last arc in the chain has a shared point with the first arc BOOST_AUTO_TEST_CASE( ArcWrappingToStartSharedPoints )