Add rounding epsilon to convert tool polygon search

Fixes https://gitlab.com/kicad/code/kicad/-/issues/8927
This commit is contained in:
Jon Evans 2021-08-12 20:17:04 -04:00
parent fbb3f4b58d
commit 5bfafa5e4c
1 changed files with 25 additions and 2 deletions

View File

@ -249,6 +249,11 @@ int CONVERT_TOOL::CreatePolys( const TOOL_EVENT& aEvent )
SHAPE_POLY_SET CONVERT_TOOL::makePolysFromSegs( const std::deque<EDA_ITEM*>& aItems ) SHAPE_POLY_SET CONVERT_TOOL::makePolysFromSegs( const std::deque<EDA_ITEM*>& aItems )
{ {
// TODO: This code has a somewhat-similar purpose to ConvertOutlineToPolygon but is slightly
// different, so this remains a separate algorithm. It might be nice to analyze the dfiferences
// in requirements and refactor this.
const int chainingEpsilon = Millimeter2iu( 0.02 );
SHAPE_POLY_SET poly; SHAPE_POLY_SET poly;
// Stores pairs of (anchor, item) where anchor == 0 -> SEG.A, anchor == 1 -> SEG.B // Stores pairs of (anchor, item) where anchor == 0 -> SEG.A, anchor == 1 -> SEG.B
@ -256,13 +261,31 @@ SHAPE_POLY_SET CONVERT_TOOL::makePolysFromSegs( const std::deque<EDA_ITEM*>& aIt
std::set<EDA_ITEM*> used; std::set<EDA_ITEM*> used;
std::deque<EDA_ITEM*> toCheck; std::deque<EDA_ITEM*> toCheck;
auto closeEnough =
[]( VECTOR2I aLeft, VECTOR2I aRight, unsigned aLimit )
{
return ( aLeft - aRight ).SquaredEuclideanNorm() <= SEG::Square( aLimit );
};
auto findInsertionPoint =
[&]( VECTOR2I aPoint ) -> VECTOR2I
{
for( const auto& candidatePair : connections )
{
if( closeEnough( aPoint, candidatePair.first, chainingEpsilon ) )
return candidatePair.first;
}
return aPoint;
};
for( EDA_ITEM* item : aItems ) for( EDA_ITEM* item : aItems )
{ {
if( OPT<SEG> seg = getStartEndPoints( item, nullptr ) ) if( OPT<SEG> seg = getStartEndPoints( item, nullptr ) )
{ {
toCheck.push_back( item ); toCheck.push_back( item );
connections[seg->A].emplace_back( std::make_pair( 0, item ) ); connections[findInsertionPoint( seg->A )].emplace_back( std::make_pair( 0, item ) );
connections[seg->B].emplace_back( std::make_pair( 1, item ) ); connections[findInsertionPoint( seg->B )].emplace_back( std::make_pair( 1, item ) );
} }
} }