47 ecoord min_dist_sq = min_dist * min_dist;
52 if( dist_sq == 0 || dist_sq < min_dist_sq )
61 *aMTV =
delta.Resize( min_dist - sqrt( dist_sq ) + 3 );
77 const int min_dist = aClearance + r;
92 bool inside = c.
x >= p0.
x && c.
x <= ( p0.
x + size.
x )
93 && c.
y >= p0.
y && c.
y <= ( p0.
y + size.
y );
96 if( inside && !aActual && !aLocation && !aMTV )
99 for(
int i = 0; i < 4; i++ )
101 const SEG side( vts[i], vts[ i + 1] );
104 ecoord side_dist_sq = ( pn - c ).SquaredEuclideanNorm();
106 if( side_dist_sq < nearest_side_dist_sq )
109 nearest_side_dist_sq = side_dist_sq;
114 if( nearest_side_dist_sq == 0 )
118 if( nearest_side_dist_sq < min_dist_sq && !aActual )
123 if( inside || nearest_side_dist_sq == 0 || nearest_side_dist_sq < min_dist_sq )
126 *aLocation = nearest;
129 *aActual = std::max( 0, (
int) sqrt( nearest_side_dist_sq ) - r );
136 *aMTV = -
delta.Resize( abs( min_dist + 1 + sqrt( nearest_side_dist_sq ) ) + 1 );
138 *aMTV =
delta.Resize( abs( min_dist + 1 - sqrt( nearest_side_dist_sq ) ) + 1 );
157 int dist = ( nearest - c ).EuclideanNorm();
158 int min_dist = aClearance + r;
160 if( dist < min_dist )
162 for(
int corr = 0; corr < 5; corr++ )
164 f = ( aA.
GetCenter() - nearest ).Resize( min_dist - dist + corr );
166 if( aB.
Distance( c + f ) >= min_dist )
178 int closest_dist = std::numeric_limits<int>::max();
179 int closest_mtv_dist = std::numeric_limits<int>::max();
181 int closest_mtv_seg = -1;
194 if( dist < closest_mtv_dist )
196 closest_mtv_dist = dist;
207 int collision_dist = 0;
211 aActual || aLocation ? &collision_dist :
nullptr,
212 aLocation ? &pn :
nullptr ) )
214 if( collision_dist < closest_dist )
217 closest_dist = collision_dist;
220 if( closest_dist == 0 )
230 if( closest_dist == 0 || closest_dist < aClearance )
233 *aLocation = nearest;
236 *aActual = closest_dist;
245 if (closest_mtv_seg >= 0)
281 *aActual = std::max( 0, *aActual - aSeg.
GetWidth() / 2 );
293 wxASSERT_MSG( !aMTV, wxString::Format( wxT(
"MTV not implemented for %s : %s collisions" ),
297 int closest_dist = std::numeric_limits<int>::max();
312 std::vector<SEG> a_segs;
313 std::vector<SEG> b_segs;
333 auto seg_sort = [](
const SEG& a,
const SEG& b )
335 return a.
A.
x < b.A.x || ( a.
A.
x == b.A.x && a.
A.
y < b.A.y );
338 std::sort( a_segs.begin(), a_segs.end(), seg_sort );
339 std::sort( b_segs.begin(), b_segs.end(), seg_sort );
341 for(
const SEG& a_seg : a_segs )
343 for(
const SEG& b_seg : b_segs )
347 if( a_seg.Collide( b_seg, aClearance, aActual || aLocation ? &dist :
nullptr ) )
349 if( dist < closest_dist )
351 nearest = a_seg.NearestPoint( b_seg );
355 if( closest_dist == 0 )
366 if( (!aActual && !aLocation ) || closest_dist > 0 )
368 std::vector<const SHAPE_LINE_CHAIN*> chains = {
373 std::vector<const SHAPE*> shapes = { &aA, &aB };
375 for(
int ii = 0; ii < 2; ii++ )
378 const SHAPE* other = shapes[( ii + 1 ) % 2];
383 for(
size_t jj = 0; jj < chain->
ArcCount(); jj++ )
387 if( arc.
Collide( other, aClearance, aActual, aLocation ) )
393 if( closest_dist == 0 || closest_dist < aClearance )
396 *aLocation = nearest;
399 *aActual = closest_dist;
411 wxASSERT_MSG( !aMTV, wxString::Format( wxT(
"MTV not implemented for %s : %s collisions" ),
415 int closest_dist = std::numeric_limits<int>::max();
427 int collision_dist = 0;
431 aActual || aLocation ? &collision_dist :
nullptr,
432 aLocation ? &pn :
nullptr ) )
434 if( collision_dist < closest_dist )
437 closest_dist = collision_dist;
440 if( closest_dist == 0 )
450 if( closest_dist == 0 || closest_dist < aClearance )
453 *aLocation = nearest;
456 *aActual = closest_dist;
468 wxASSERT_MSG( !aMTV, wxString::Format( wxT(
"MTV not implemented for %s : %s collisions" ),
475 *aActual = std::max( 0, *aActual - aB.
GetWidth() / 2 );
484 wxASSERT_MSG( !aMTV, wxString::Format( wxT(
"MTV not implemented for %s : %s collisions" ),
491 *aActual = std::max( 0, *aActual - aB.
GetWidth() / 2 );
500 wxASSERT_MSG( !aMTV, wxString::Format( wxT(
"MTV not implemented for %s : %s collisions" ),
507 *aActual = std::max( 0, *aActual - aB.
GetWidth() / 2 );
516 if( aClearance || aActual || aLocation || aMTV )
536 bool retval =
Collide( aB, tmp, aClearance, aActual, aLocation, aMTV );
545 int64_t dist_sq = std::numeric_limits<int64_t>::max();
547 int half_width = ( aA.
GetWidth() + 1 ) / 2;
548 int min_dist = aClearance + half_width;
553 *aLocation = ( ptA + ptB ) / 2;
556 *aActual = std::max( 0,
KiROUND( std::sqrt( dist_sq ) - half_width ) );
561 *aMTV =
delta.Resize( min_dist - std::sqrt( dist_sq ) + 3 );
577 bool retval =
Collide( aB, tmp, aClearance, aActual, aLocation, aMTV );
586 int64_t dist_sq = std::numeric_limits<int64_t>::max();
588 int half_width = ( aA.
GetWidth() + 1 ) / 2;
589 int min_dist = aClearance + half_width;
594 *aLocation = ( ptA + ptB ) / 2;
597 *aActual = std::max( 0,
KiROUND( std::sqrt( dist_sq ) - half_width ) );
602 *aMTV =
delta.Resize( min_dist - std::sqrt( dist_sq ) + 3 );
615 wxASSERT_MSG( !aMTV, wxString::Format( wxT(
"MTV not implemented for %s : %s collisions" ),
619 int closest_dist = std::numeric_limits<int>::max();
625 nearest = aA.
GetP0();
629 int collision_dist = 0;
639 aActual || aLocation ? &collision_dist :
nullptr,
640 aLocation ? &pn :
nullptr ) )
642 if( collision_dist < closest_dist )
645 closest_dist = collision_dist;
648 if( closest_dist == 0 )
657 for(
size_t i = 0; i < aB.
ArcCount(); i++ )
662 wxASSERT_MSG( arc.
GetWidth() == 0, wxT(
"Invalid arc width - should be zero" ) );
664 if( aA.
Collide( &arc, aClearance, aActual || aLocation ? &collision_dist :
nullptr,
665 aLocation ? &pn :
nullptr ) )
667 if( collision_dist < closest_dist )
670 closest_dist = collision_dist;
673 if( closest_dist == 0 )
682 if( closest_dist == 0 || closest_dist < aClearance )
685 *aLocation = nearest;
688 *aActual = closest_dist;
700 wxASSERT_MSG( !aMTV, wxString::Format( wxT(
"MTV not implemented for %s : %s collisions" ),
708 return Collide( tmp, aB, aClearance, aActual, aLocation, aMTV );
714 *aActual = std::max( 0, *aActual - aB.
GetWidth() / 2 );
727 return Collide( aB, tmp, aClearance, aActual, aLocation, aMTV );
730 wxASSERT_MSG( !aMTV, wxString::Format( wxT(
"MTV not implemented for %s : %s collisions" ),
734 int closest_dist = std::numeric_limits<int>::max();
740 nearest = aA.
GetP0();
746 int collision_dist = 0;
750 aActual || aLocation ? &collision_dist :
nullptr,
751 aLocation ? &pn :
nullptr ) )
753 if( collision_dist < closest_dist )
756 closest_dist = collision_dist;
759 if( closest_dist == 0 )
769 if( closest_dist == 0 || closest_dist < aClearance )
772 *aLocation = nearest;
775 *aActual = closest_dist;
790 bool retval =
Collide( aB, tmp, aClearance, aActual, aLocation, aMTV );
801 return Collide( aA, tmp, aClearance, aActual, aLocation, aMTV );
805 int64_t dist_sq = std::numeric_limits<int64_t>::max();
808 int min_dist = aClearance + dual_width;
813 *aLocation = ( ptA + ptB ) / 2;
816 *aActual = std::max( 0,
KiROUND( std::sqrt( dist_sq ) - dual_width ) );
821 *aMTV =
delta.Resize( min_dist - std::sqrt( dist_sq ) + 3 );
831template<
class T_a,
class T_b>
836 return Collide( *
static_cast<const T_a*
>( aA ), *
static_cast<const T_b*
>( aB ),
837 aClearance, aActual, aLocation, aMTV);
841template<
class T_a,
class T_b>
845 bool rv =
Collide( *
static_cast<const T_b*
>( aB ), *
static_cast<const T_a*
>( aA ),
846 aClearance, aActual, aLocation, aMTV);
863 return polySetA->
Collide( aB, aClearance, aActual, aLocation );
870 return polySetB->
Collide( aA, aClearance, aActual, aLocation );
882 return CollCase<SHAPE_RECT, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
885 return CollCase<SHAPE_RECT, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
888 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
891 return CollCase<SHAPE_RECT, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
895 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
898 return CollCaseReversed<SHAPE_RECT, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
912 return CollCaseReversed<SHAPE_CIRCLE, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
915 return CollCase<SHAPE_CIRCLE, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
918 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
921 return CollCase<SHAPE_CIRCLE, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
925 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
928 return CollCaseReversed<SHAPE_CIRCLE, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
942 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN>( aB, aA, aClearance, aActual, aLocation, aMTV );
945 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN>( aB, aA, aClearance, aActual, aLocation, aMTV );
948 return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
951 return CollCase<SHAPE_LINE_CHAIN, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
955 return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
958 return CollCaseReversed<SHAPE_LINE_CHAIN, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
972 return CollCase<SHAPE_RECT, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
975 return CollCaseReversed<SHAPE_SEGMENT, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
978 return CollCase<SHAPE_LINE_CHAIN, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
981 return CollCase<SHAPE_SEGMENT, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
985 return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
988 return CollCaseReversed<SHAPE_SEGMENT, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
1000 switch( aB->
Type() )
1003 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
1006 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
1009 return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
1012 return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
1016 return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
1019 return CollCaseReversed<SHAPE_LINE_CHAIN_BASE, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
1030 switch( aB->
Type() )
1033 return CollCase<SHAPE_ARC, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
1036 return CollCase<SHAPE_ARC, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
1039 return CollCase<SHAPE_ARC, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
1042 return CollCase<SHAPE_ARC, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
1046 return CollCase<SHAPE_ARC, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
1049 return CollCase<SHAPE_ARC, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
1063 wxFAIL_MSG( wxString::Format( wxT(
"Unsupported collision: %s with %s" ),
1073 int currentActual = std::numeric_limits<int>::max();
1076 bool colliding =
false;
1084 if( aActual && currentActual > 0 )
1093 auto collideCompoundSubshapes =
1094 [&](
const SHAPE* elemA,
const SHAPE* elemB,
int clearance ) ->
bool
1101 aActual || aLocation ? &actual :
nullptr,
1102 aLocation ? &location :
nullptr,
1103 aMTV ? &mtv :
nullptr ) )
1105 if( actual < currentActual )
1107 currentActual = actual;
1108 currentLocation = location;
1131 if( collideCompoundSubshapes( elemA, elemB, aClearance ) )
1150 if( collideCompoundSubshapes( elemA, aB, aClearance ) )
1165 if( collideCompoundSubshapes( aA, elemB, aClearance ) )
1182 *aLocation = currentLocation;
1185 *aActual = currentActual;
1197 return collideShapes(
this, aShape, aClearance,
nullptr,
nullptr, aMTV );
1203 return collideShapes(
this, aShape, aClearance, aActual, aLocation,
nullptr );
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
constexpr bool Intersects(const BOX2< Vec > &aRect) const
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
static SEG::ecoord Square(int a)
int Distance(const SEG &aSeg) const
Compute minimum Euclidean distance to segment aSeg.
const VECTOR2I & GetP1() const
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the segment aSeg than aClearance,...
bool IsEffectiveLine() const
bool NearestPoints(const SHAPE_ARC &aArc, VECTOR2I &aPtA, VECTOR2I &aPtB, int64_t &aDistSq) const
Compute closest points between this arc and aArc.
const VECTOR2I & GetP0() const
wxString TypeName() const
SHAPE_TYPE Type() const
Return the type of the shape.
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the segment aSeg than aClearance,...
const VECTOR2I GetCenter() const
void SetCenter(const VECTOR2I &aCenter)
const std::vector< SHAPE * > & Shapes() const
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if point aP lies closer to us than aClearance.
virtual size_t GetPointCount() const =0
virtual size_t GetSegmentCount() const =0
virtual const VECTOR2I GetPoint(int aIndex) const =0
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const override
Check if point aP lies inside a closed shape.
virtual bool IsClosed() 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
bool IsClosed() const override
virtual const SEG GetSegment(int aIndex) const override
virtual size_t GetSegmentCount() const override
bool IsArcSegment(size_t aSegment) const
Represent a set of closed polygons.
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
bool Collide(const SHAPE *aShape, int aClearance, VECTOR2I *aMTV) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
const SHAPE_LINE_CHAIN Outline() const
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
const VECTOR2I & GetPosition() const
const VECTOR2I GetSize() const
const SEG & GetSeg() const
bool Collide(const SHAPE *aShape, int aClearance, VECTOR2I *aMTV) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
An abstract shape on 2D plane.
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
virtual VECTOR2I Centre() const
Compute a center-of-mass of the shape.
constexpr extended_type SquaredEuclideanNorm() const
Compute the squared euclidean norm of the vector, which is defined as (x ** 2 + y ** 2).
static constexpr extended_type ECOORD_MAX
VECTOR2_TRAITS< int32_t >::extended_type extended_type
@ SH_POLY_SET
set of polygons (with holes, etc.)
@ SH_RECT
axis-aligned rectangle
@ SH_SIMPLE
simple polygon
@ SH_NULL
empty shape (no shape...),
@ 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 wxString SHAPE_TYPE_asString(SHAPE_TYPE a)
static bool collideSingleShapes(const SHAPE *aA, const SHAPE *aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
static bool Collide(const SHAPE_CIRCLE &aA, const SHAPE_CIRCLE &aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
bool CollCaseReversed(const SHAPE *aA, const SHAPE *aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
static VECTOR2I pushoutForce(const SHAPE_CIRCLE &aA, const SEG &aB, int aClearance)
bool CollCase(const SHAPE *aA, const SHAPE *aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
static bool collideShapes(const SHAPE *aA, const SHAPE *aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
VECTOR2I::extended_type ecoord
VECTOR2< int32_t > VECTOR2I