From d67821d7714f13f72b0a507d656a31d5b553dda0 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Tue, 8 May 2018 13:03:52 -0700 Subject: [PATCH] Add a clearance check for closed shape line chains Also improves speed of multiple point checks by first eliminating points outside of the BBox. --- common/geometry/shape_line_chain.cpp | 10 ++++++++-- include/geometry/shape_line_chain.h | 12 +++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/common/geometry/shape_line_chain.cpp b/common/geometry/shape_line_chain.cpp index 58faf46b8e..294f77be6b 100644 --- a/common/geometry/shape_line_chain.cpp +++ b/common/geometry/shape_line_chain.cpp @@ -331,7 +331,7 @@ int SHAPE_LINE_CHAIN::PathLength( const VECTOR2I& aP ) const bool SHAPE_LINE_CHAIN::PointInside( const VECTOR2I& aP ) const { - if( !m_closed || SegmentCount() < 3 ) + if( !m_closed || SegmentCount() < 3 || !BBox().Contains( aP ) ) return false; bool inside = false; @@ -358,6 +358,12 @@ bool SHAPE_LINE_CHAIN::PointInside( const VECTOR2I& aP ) const bool SHAPE_LINE_CHAIN::PointOnEdge( const VECTOR2I& aP ) const +{ + return CheckClearance( aP, 1 ); +} + + +bool SHAPE_LINE_CHAIN::CheckClearance( const VECTOR2I& aP, const int aDist) const { if( !PointCount() ) return false; @@ -372,7 +378,7 @@ bool SHAPE_LINE_CHAIN::PointOnEdge( const VECTOR2I& aP ) const if( s.A == aP || s.B == aP ) return true; - if( s.Distance( aP ) <= 1 ) + if( s.Distance( aP ) <= aDist ) return true; } diff --git a/include/geometry/shape_line_chain.h b/include/geometry/shape_line_chain.h index 6631356db9..094488c9c8 100644 --- a/include/geometry/shape_line_chain.h +++ b/include/geometry/shape_line_chain.h @@ -532,7 +532,7 @@ public: /** * Function PointInside() * - * Checks if point aP lies inside a convex polygon defined by the line chain. For closed + * Checks if point aP lies inside a polygon (any type) defined by the line chain. For closed * shapes only. * @param aP point to check * @return true if the point is inside the shape (edge is not treated as being inside). @@ -548,6 +548,16 @@ public: */ bool PointOnEdge( const VECTOR2I& aP ) const; + /** + * Function CheckClearance() + * + * Checks if point aP is closer to (or on) an edge or vertex of the line chain. + * @param aP point to check + * @param aDist distance in internal units + * @return true if the point is equal to or closer than aDist to the line chain. + */ + bool CheckClearance( const VECTOR2I& aP, const int aDist) const; + /** * Function SelfIntersecting() *