From 3594a064752063461053c009d1d9e9c2f10fd483 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Mon, 1 Aug 2022 09:48:29 -0700 Subject: [PATCH] Fix simplify routine The change in c9c31fcbc2 missed a number of cases. This reverts back to the system as it existed before 2be352b9f9ba67d649bca5ccfeef8c2ecd11f975 but using the revised method of determining A-B-C distance and avoiding resizing the arrays (cherry picked from commit 675a5a6e7babccecc5073d57840c69df17529ae7) --- libs/kimath/src/geometry/shape_line_chain.cpp | 52 ++++++++++++------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/libs/kimath/src/geometry/shape_line_chain.cpp b/libs/kimath/src/geometry/shape_line_chain.cpp index d26c977d3f..2966c25ef9 100644 --- a/libs/kimath/src/geometry/shape_line_chain.cpp +++ b/libs/kimath/src/geometry/shape_line_chain.cpp @@ -1739,33 +1739,45 @@ SHAPE_LINE_CHAIN& SHAPE_LINE_CHAIN::Simplify( bool aRemoveColinear ) i = 0; - m_points.push_back( pts_unique[0] ); - m_shapes.push_back( shapes_unique[0] ); - // stage 2: eliminate colinear segments - for( i = 1 ; i < np - 1 ; i ++ ) + while( i < np - 2 ) { - const VECTOR2I p_prev = pts_unique[i - 1]; - const VECTOR2I midpoint = pts_unique[i]; - const VECTOR2I p_next = pts_unique[i + 1]; + const VECTOR2I p0 = pts_unique[i]; + int n = i; - - if( aRemoveColinear && shapes_unique[i - 1] == SHAPES_ARE_PT - && shapes_unique[i] == SHAPES_ARE_PT ) + if( aRemoveColinear && shapes_unique[i] == SHAPES_ARE_PT + && shapes_unique[i + 1] == SHAPES_ARE_PT ) { - const auto distToMidpoint = SEG( p_prev, p_next ).LineDistance( midpoint ); - const auto isMidpointColinear = SEG( p_prev, p_next ).Collinear( SEG( p_prev, midpoint ) ); - - if( distToMidpoint > 1 && !isMidpointColinear ) - { - m_points.push_back( pts_unique[i] ); - m_shapes.push_back( shapes_unique[i] ); - } + while( n < np - 2 + && ( SEG( p0, pts_unique[n + 2] ).LineDistance( pts_unique[n + 1] ) <= 1 + || SEG( p0, pts_unique[n + 2] ).Collinear( SEG( p0, pts_unique[n + 1] ) ) ) ) + n++; } + + m_points.push_back( p0 ); + m_shapes.push_back( shapes_unique[i] ); + + if( n > i ) + i = n; + + if( n == np - 2 ) + { + m_points.push_back( pts_unique[np - 1] ); + m_shapes.push_back( shapes_unique[np - 1] ); + return *this; + } + + i++; } - m_points.push_back( pts_unique.back() ); - m_shapes.push_back( shapes_unique.back() ); + if( np > 1 ) + { + m_points.push_back( pts_unique[np - 2] ); + m_shapes.push_back( shapes_unique[np - 2] ); + } + + m_points.push_back( pts_unique[np - 1] ); + m_shapes.push_back( shapes_unique[np - 1] ); assert( m_points.size() == m_shapes.size() );