55 const int dist =
delta.EuclideanNorm();
92 aPtB.
x = std::max( p0.
x, std::min( c.
x, p0.
x + size.
x ) );
93 aPtB.
y = std::max( p0.
y, std::min( c.
y, p0.
y + size.
y ) );
99 int distToLeft = c.
x - p0.
x;
100 int distToRight = p0.
x + size.
x - c.
x;
101 int distToTop = c.
y - p0.
y;
102 int distToBottom = p0.
y + size.
y - c.
y;
104 int minDist = std::min( { distToLeft, distToRight, distToTop, distToBottom } );
106 if( minDist == distToLeft )
111 else if( minDist == distToRight )
116 else if( minDist == distToTop )
173 int64_t minDistSq = std::numeric_limits<int64_t>::max();
180 int64_t distSq = ( ptB - ptA ).SquaredEuclideanNorm();
181 if( distSq < minDistSq )
190 return minDistSq < std::numeric_limits<int64_t>::max();
204 int64_t minDistSq = std::numeric_limits<int64_t>::max();
216 int64_t distSq = ( ptB - ptA ).SquaredEuclideanNorm();
217 if( distSq < minDistSq )
236 int64_t minDistSq = std::numeric_limits<int64_t>::max();
244 int64_t distSq = ( ptB - ptA ).SquaredEuclideanNorm();
245 if( distSq < minDistSq )
264 int64_t minDistSq = std::numeric_limits<int64_t>::max();
276 int64_t distSq = ( ptB - ptA ).SquaredEuclideanNorm();
277 if( distSq < minDistSq )
309 int64_t minDistSq = std::numeric_limits<int64_t>::max();
318 int64_t distSq = ( ptB - ptA ).SquaredEuclideanNorm();
319 if( distSq < minDistSq )
328 return minDistSq < std::numeric_limits<int64_t>::max();
338 int64_t minDistSq = std::numeric_limits<int64_t>::max();
348 int64_t distSq = ( ptB - ptA ).SquaredEuclideanNorm();
349 if( distSq < minDistSq )
365 for(
size_t i = 0; i < chainA->
ArcCount(); i++ )
372 for(
size_t j = 0; j < chainB->
ArcCount(); j++ )
378 if( distSq < minDistSq )
395 if( distSq < minDistSq )
406 if( chainB && !chainA )
409 for(
size_t j = 0; j < chainB->
ArcCount(); j++ )
419 if( distSq < minDistSq )
430 return minDistSq < std::numeric_limits<int64_t>::max();
470 int64_t minDistSq = std::numeric_limits<int64_t>::max();
477 if( distSq < minDistSq )
532 dir = ( aPtA - aPtB ).Resize( aSegB.
GetWidth() / 2 );
558template<
class T_a,
class T_b>
563 *
static_cast<const T_b*
>( aB ),
567template<
class T_a,
class T_b>
572 *
static_cast<const T_a*
>( aA ),
589 return NearestPointsCase<SHAPE_RECT, SHAPE_RECT>( aA, aB, aPtA, aPtB );
591 return NearestPointsCaseReversed<SHAPE_RECT, SHAPE_CIRCLE>( aA, aB, aPtA, aPtB );
593 return NearestPointsCase<SHAPE_RECT, SHAPE_LINE_CHAIN>( aA, aB, aPtA, aPtB );
595 return NearestPointsCaseReversed<SHAPE_RECT, SHAPE_SEGMENT>( aA, aB, aPtA, aPtB );
598 return NearestPointsCase<SHAPE_RECT, SHAPE_LINE_CHAIN_BASE>( aA, aB, aPtA, aPtB );
600 return NearestPointsCaseReversed<SHAPE_RECT, SHAPE_ARC>( aA, aB, aPtA, aPtB );
610 return NearestPointsCase<SHAPE_CIRCLE, SHAPE_RECT>( aA, aB, aPtA, aPtB );
612 return NearestPointsCase<SHAPE_CIRCLE, SHAPE_CIRCLE>( aA, aB, aPtA, aPtB );
614 return NearestPointsCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN>( aA, aB, aPtA, aPtB );
616 return NearestPointsCaseReversed<SHAPE_CIRCLE, SHAPE_SEGMENT>( aA, aB, aPtA, aPtB );
619 return NearestPointsCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN_BASE>( aA, aB, aPtA, aPtB );
621 return NearestPointsCaseReversed<SHAPE_CIRCLE, SHAPE_ARC>( aA, aB, aPtA, aPtB );
631 return NearestPointsCaseReversed<SHAPE_LINE_CHAIN, SHAPE_RECT>( aA, aB, aPtA, aPtB );
633 return NearestPointsCaseReversed<SHAPE_LINE_CHAIN, SHAPE_CIRCLE>( aA, aB, aPtA, aPtB );
635 return NearestPointsCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN>( aA, aB, aPtA, aPtB );
637 return NearestPointsCaseReversed<SHAPE_LINE_CHAIN, SHAPE_SEGMENT>( aA, aB, aPtA, aPtB );
640 return NearestPointsCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN_BASE>( aA, aB, aPtA, aPtB );
646 int64_t minDistSq = std::numeric_limits<int64_t>::max();
655 if( distSq < minDistSq )
671 if( distSq < minDistSq )
680 return minDistSq < std::numeric_limits<int64_t>::max();
691 return NearestPointsCase<SHAPE_SEGMENT, SHAPE_RECT>( aA, aB, aPtA, aPtB );
693 return NearestPointsCase<SHAPE_SEGMENT, SHAPE_CIRCLE>( aA, aB, aPtA, aPtB );
695 return NearestPointsCase<SHAPE_SEGMENT, SHAPE_LINE_CHAIN>( aA, aB, aPtA, aPtB );
697 return NearestPointsCase<SHAPE_SEGMENT, SHAPE_SEGMENT>( aA, aB, aPtA, aPtB );
700 return NearestPointsCase<SHAPE_SEGMENT, SHAPE_LINE_CHAIN_BASE>( aA, aB, aPtA, aPtB );
702 return NearestPointsCaseReversed<SHAPE_SEGMENT, SHAPE_ARC>( aA, aB, aPtA, aPtB );
713 return NearestPointsCaseReversed<SHAPE_LINE_CHAIN_BASE, SHAPE_RECT>( aA, aB, aPtA, aPtB );
715 return NearestPointsCaseReversed<SHAPE_LINE_CHAIN_BASE, SHAPE_CIRCLE>( aA, aB, aPtA, aPtB );
717 return NearestPointsCaseReversed<SHAPE_LINE_CHAIN_BASE, SHAPE_LINE_CHAIN>( aA, aB, aPtA, aPtB );
719 return NearestPointsCaseReversed<SHAPE_LINE_CHAIN_BASE, SHAPE_SEGMENT>( aA, aB, aPtA, aPtB );
722 return NearestPointsCase<SHAPE_LINE_CHAIN_BASE, SHAPE_LINE_CHAIN_BASE>( aA, aB, aPtA, aPtB );
728 int64_t minDistSq = std::numeric_limits<int64_t>::max();
736 if( distSq < minDistSq )
745 return minDistSq < std::numeric_limits<int64_t>::max();
756 return NearestPointsCase<SHAPE_ARC, SHAPE_RECT>( aA, aB, aPtA, aPtB );
758 return NearestPointsCase<SHAPE_ARC, SHAPE_CIRCLE>( aA, aB, aPtA, aPtB );
760 return NearestPointsCase<SHAPE_ARC, SHAPE_LINE_CHAIN>( aA, aB, aPtA, aPtB );
762 return NearestPointsCase<SHAPE_ARC, SHAPE_SEGMENT>( aA, aB, aPtA, aPtB );
765 return NearestPointsCase<SHAPE_ARC, SHAPE_LINE_CHAIN_BASE>( aA, aB, aPtA, aPtB );
767 return NearestPointsCase<SHAPE_ARC, SHAPE_ARC>( aA, aB, aPtA, aPtB );
777 int64_t minDistSq = std::numeric_limits<int64_t>::max();
786 int64_t distSq = ( ptB - ptA ).SquaredEuclideanNorm();
787 if( distSq < minDistSq )
796 return minDistSq < std::numeric_limits<int64_t>::max();
808 int64_t minDistSq = std::numeric_limits<int64_t>::max();
817 int64_t distSq = ( ptB - ptA ).SquaredEuclideanNorm();
818 if( distSq < minDistSq )
827 return minDistSq < std::numeric_limits<int64_t>::max();
840 int64_t minDistSq = std::numeric_limits<int64_t>::max();
843 auto checkNearestPoints = [&](
const SHAPE* shapeA,
const SHAPE* shapeB ) ->
bool
849 int64_t distSq = ( ptB - ptA ).SquaredEuclideanNorm();
850 if( distSq < minDistSq )
871 checkNearestPoints( elemA, elemB );
881 checkNearestPoints( elemA, aB );
890 checkNearestPoints( aA, elemB );
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
bool NearestPoints(const SHAPE_ARC &aArc, VECTOR2I &aPtA, VECTOR2I &aPtB, int64_t &aDistSq) const
Compute closest points between this arc and aArc.
SHAPE_TYPE Type() const
Return the type of the shape.
const VECTOR2I GetCenter() const
const std::vector< SHAPE * > & Shapes() const
virtual size_t GetSegmentCount() const =0
virtual const SEG GetSegment(int aIndex) const =0
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
const SHAPE_ARC & Arc(size_t aArc) const
virtual const SEG GetSegment(int aIndex) const override
int SegmentCount() const
Return the number of segments in this line chain.
virtual size_t GetSegmentCount() const override
const SEG CSegment(int aIndex) const
Return a constant copy of the aIndex segment in the line chain.
Represent a set of closed polygons.
CONST_SEGMENT_ITERATOR CIterateSegments(int aFirst, int aLast, bool aIterateHoles=false) const
Return an iterator object, for iterating between aFirst and aLast outline, with or without holes (def...
const SHAPE_LINE_CHAIN Outline() const
const VECTOR2I & GetPosition() const
const VECTOR2I GetSize() const
const SEG & GetSeg() const
An abstract shape on 2D plane.
bool NearestPoints(const SHAPE *aOther, VECTOR2I &aPtThis, VECTOR2I &aPtOther) const
Return the two points that mark the closest distance between this shape and aOther.
VECTOR2_TRAITS< int32_t >::extended_type extended_type
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
@ SH_POLY_SET
set of polygons (with holes, etc.)
@ SH_RECT
axis-aligned rectangle
@ SH_SIMPLE
simple polygon
@ SH_POLY_SET_TRIANGLE
a single triangle belonging to a POLY_SET triangulation
@ SH_LINE_CHAIN
line chain (polyline)
@ SH_COMPOUND
compound shape, consisting of multiple simple shapes
static bool NearestPoints(const SHAPE_CIRCLE &aA, const SHAPE_CIRCLE &aB, VECTOR2I &aPtA, VECTOR2I &aPtB)
Find the nearest points between two circles.
bool NearestPointsCase(const SHAPE *aA, const SHAPE *aB, VECTOR2I &aPtA, VECTOR2I &aPtB)
Template functions to handle shape conversions and reversals.
static bool nearestPointsSingleShapes(const SHAPE *aA, const SHAPE *aB, VECTOR2I &aPtA, VECTOR2I &aPtB)
Main dispatcher for finding nearest points between arbitrary shapes.
VECTOR2I::extended_type ecoord
static bool nearestPoints(const SHAPE *aA, const SHAPE *aB, VECTOR2I &aPtA, VECTOR2I &aPtB)
Handle compound shapes by finding nearest points between all sub-shape pairs.
bool NearestPointsCaseReversed(const SHAPE *aA, const SHAPE *aB, VECTOR2I &aPtA, VECTOR2I &aPtB)
const SHAPE_LINE_CHAIN chain
VECTOR2< int32_t > VECTOR2I