diff --git a/libs/kimath/src/geometry/shape_collisions.cpp b/libs/kimath/src/geometry/shape_collisions.cpp index 45e41b5c3d..9cc7d70d01 100644 --- a/libs/kimath/src/geometry/shape_collisions.cpp +++ b/libs/kimath/src/geometry/shape_collisions.cpp @@ -107,8 +107,14 @@ static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_CIRCLE& aB, int aC nearest = pn; nearest_side_dist_sq = side_dist_sq; - // If we're not looking for actual or MTV, short-circuit once we find any collision - if( ( nearest_side_dist_sq == 0 || !aActual ) && !aMTV ) + if( aMTV ) + continue; + + if( nearest_side_dist_sq == 0 ) + break; + + // If we're not looking for aActual then any collision will do + if( nearest_side_dist_sq < min_dist_sq && !aActual ) break; } } @@ -171,32 +177,37 @@ static inline bool Collide( const SHAPE_CIRCLE& aA, const SHAPE_LINE_CHAIN_BASE& int closest_dist = INT_MAX; VECTOR2I nearest; - for( int s = 0; s < aB.GetSegmentCount(); s++ ) - { - int collision_dist = 0; - VECTOR2I pn; - - if( aA.Collide( aB.GetSegment( s ), aClearance, - aActual || aLocation ? &collision_dist : nullptr, - aLocation ? &pn : nullptr ) ) - { - if( collision_dist < closest_dist ) - { - nearest = pn; - closest_dist = collision_dist; - - if( closest_dist == 0 || !aActual ) - break; - } - } - } - if( aB.IsClosed() && aB.PointInside( aA.GetCenter() ) ) { closest_dist = 0; nearest = aA.GetCenter(); } + else + { + for( int s = 0; s < aB.GetSegmentCount(); s++ ) + { + int collision_dist = 0; + VECTOR2I pn; + if( aA.Collide( aB.GetSegment( s ), aClearance, + aActual || aLocation ? &collision_dist : nullptr, + aLocation ? &pn : nullptr ) ) + { + if( collision_dist < closest_dist ) + { + nearest = pn; + closest_dist = collision_dist; + } + + if( closest_dist == 0 ) + break; + + // If we're not looking for aActual then any collision will do + if( !aActual ) + break; + } + } + } if( closest_dist == 0 || closest_dist < aClearance ) { @@ -251,31 +262,37 @@ static inline bool Collide( const SHAPE_LINE_CHAIN_BASE& aA, const SHAPE_LINE_CH int closest_dist = INT_MAX; VECTOR2I nearest; - for( int i = 0; i < aB.GetSegmentCount(); i++ ) - { - int collision_dist = 0; - VECTOR2I pn; - - if( aA.Collide( aB.GetSegment( i ), aClearance, - aActual || aLocation ? &collision_dist : nullptr, - aLocation ? &pn : nullptr ) ) - { - if( collision_dist < closest_dist ) - { - nearest = pn; - closest_dist = collision_dist; - - if( closest_dist == 0 || !aActual ) - break; - } - } - } - if( aB.IsClosed() && aA.GetPointCount() > 0 && aB.PointInside( aA.GetPoint( 0 ) ) ) { closest_dist = 0; nearest = aA.GetPoint( 0 ); } + else + { + for( int i = 0; i < aB.GetSegmentCount(); i++ ) + { + int collision_dist = 0; + VECTOR2I pn; + + if( aA.Collide( aB.GetSegment( i ), aClearance, + aActual || aLocation ? &collision_dist : nullptr, + aLocation ? &pn : nullptr ) ) + { + if( collision_dist < closest_dist ) + { + nearest = pn; + closest_dist = collision_dist; + } + + if( closest_dist == 0 ) + break; + + // If we're not looking for aActual then any collision will do + if( !aActual ) + break; + } + } + } if( closest_dist == 0 || closest_dist < aClearance ) { @@ -300,31 +317,37 @@ static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_LINE_CHAIN_BASE& a int closest_dist = INT_MAX; VECTOR2I nearest; - for( int s = 0; s < aB.GetSegmentCount(); s++ ) - { - int collision_dist = 0; - VECTOR2I pn; - - if( aA.Collide( aB.GetSegment( s ), aClearance, - aActual || aLocation ? &collision_dist : nullptr, - aLocation ? &pn : nullptr ) ) - { - if( collision_dist < closest_dist ) - { - nearest = pn; - closest_dist = collision_dist; - - if( closest_dist == 0 || !aActual ) - break; - } - } - } - if( aB.IsClosed() && aB.PointInside( aA.Centre() ) ) { nearest = aA.Centre(); closest_dist = 0; } + else + { + for( int s = 0; s < aB.GetSegmentCount(); s++ ) + { + int collision_dist = 0; + VECTOR2I pn; + + if( aA.Collide( aB.GetSegment( s ), aClearance, + aActual || aLocation ? &collision_dist : nullptr, + aLocation ? &pn : nullptr ) ) + { + if( collision_dist < closest_dist ) + { + nearest = pn; + closest_dist = collision_dist; + } + + if( closest_dist == 0 ) + break; + + // If we're not looking for aActual then any collision will do + if( !aActual ) + break; + } + } + } if( closest_dist == 0 || closest_dist < aClearance ) { diff --git a/libs/kimath/src/geometry/shape_line_chain.cpp b/libs/kimath/src/geometry/shape_line_chain.cpp index 509a0455fe..bbed7858c4 100644 --- a/libs/kimath/src/geometry/shape_line_chain.cpp +++ b/libs/kimath/src/geometry/shape_line_chain.cpp @@ -114,7 +114,11 @@ bool SHAPE_LINE_CHAIN_BASE::Collide( const VECTOR2I& aP, int aClearance, int* aA nearest = pn; closest_dist_sq = dist_sq; - if( closest_dist_sq == 0 || !aActual ) + if( closest_dist_sq == 0 ) + break; + + // If we're not looking for aActual then any collision will do + if( closest_dist_sq < clearance_sq && !aActual ) break; } } @@ -178,7 +182,11 @@ bool SHAPE_LINE_CHAIN_BASE::Collide( const SEG& aSeg, int aClearance, int* aActu closest_dist_sq = dist_sq; - if( closest_dist_sq == 0 || !aActual ) + if( closest_dist_sq == 0) + break; + + // If we're not looking for aActual then any collision will do + if( closest_dist_sq < clearance_sq && !aActual ) break; } } diff --git a/pcbnew/router/pns_router.cpp b/pcbnew/router/pns_router.cpp index 2940cee375..38993f5a63 100644 --- a/pcbnew/router/pns_router.cpp +++ b/pcbnew/router/pns_router.cpp @@ -24,10 +24,8 @@ #include #include -#include #include #include -#include #include #include @@ -36,10 +34,6 @@ #include #include -#include -#include -#include -#include #include "pns_node.h" #include "pns_line_placer.h"