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 )
533 wxASSERT_MSG( !aMTV, wxString::Format( wxT(
"MTV not implemented for %s : %s collisions" ),
542 *aActual = std::max( 0, *aActual - aA.
GetWidth() / 2 );
553 bool rv =
Collide( aB, lc, aClearance + aA.
GetWidth() / 2, aActual, aLocation, aMTV );
556 *aActual = std::max( 0, *aActual - aA.
GetWidth() / 2 );
565 wxASSERT_MSG( !aMTV, wxString::Format( wxT(
"MTV not implemented for %s : %s collisions" ),
569 int closest_dist = std::numeric_limits<int>::max();
575 nearest = aA.
GetP0();
579 int collision_dist = 0;
589 aActual || aLocation ? &collision_dist :
nullptr,
590 aLocation ? &pn :
nullptr ) )
592 if( collision_dist < closest_dist )
595 closest_dist = collision_dist;
598 if( closest_dist == 0 )
607 for(
size_t i = 0; i < aB.
ArcCount(); i++ )
612 wxASSERT_MSG( arc.
GetWidth() == 0, wxT(
"Invalid arc width - should be zero" ) );
614 if( aA.
Collide( &arc, aClearance, aActual || aLocation ? &collision_dist :
nullptr,
615 aLocation ? &pn :
nullptr ) )
617 if( collision_dist < closest_dist )
620 closest_dist = collision_dist;
623 if( closest_dist == 0 )
632 if( closest_dist == 0 || closest_dist < aClearance )
635 *aLocation = nearest;
638 *aActual = closest_dist;
650 wxASSERT_MSG( !aMTV, wxString::Format( wxT(
"MTV not implemented for %s : %s collisions" ),
657 *aActual = std::max( 0, *aActual - aB.
GetWidth() / 2 );
666 wxASSERT_MSG( !aMTV, wxString::Format( wxT(
"MTV not implemented for %s : %s collisions" ),
670 int closest_dist = std::numeric_limits<int>::max();
676 nearest = aA.
GetP0();
682 int collision_dist = 0;
686 aActual || aLocation ? &collision_dist :
nullptr,
687 aLocation ? &pn :
nullptr ) )
689 if( collision_dist < closest_dist )
692 closest_dist = collision_dist;
695 if( closest_dist == 0 )
705 if( closest_dist == 0 || closest_dist < aClearance )
708 *aLocation = nearest;
711 *aActual = closest_dist;
723 wxASSERT_MSG( !aMTV, wxString::Format( wxT(
"MTV not implemented for %s : %s collisions" ),
729 std::vector<VECTOR2I> ips;
744 std::vector<VECTOR2I> ptsA;
745 std::vector<VECTOR2I> ptsB;
747 bool cocentered = ( mediatrix.
A == mediatrix.
B );
757 ptsA.push_back( aA.
GetP0() );
758 ptsA.push_back( aA.
GetP1() );
759 ptsB.push_back( aB.
GetP0() );
760 ptsB.push_back( aB.
GetP1() );
770 double minDist = std::numeric_limits<double>::max();
782 SEG candidateMinDist( ptA, ptB );
783 int dist = candidateMinDist.
Length() - widths;
785 if( dist < aClearance )
787 if( !rv || dist < minDist )
790 minDistSeg = candidateMinDist;
799 *aActual = std::max( 0, minDistSeg.
Length() - widths );
801 if( rv && aLocation )
802 *aLocation = minDistSeg.
Center();
808template<
class T_a,
class T_b>
813 return Collide( *
static_cast<const T_a*
>( aA ), *
static_cast<const T_b*
>( aB ),
814 aClearance, aActual, aLocation, aMTV);
818template<
class T_a,
class T_b>
822 bool rv =
Collide( *
static_cast<const T_b*
>( aB ), *
static_cast<const T_a*
>( aA ),
823 aClearance, aActual, aLocation, aMTV);
840 return polySetA->
Collide( aB, aClearance, aActual, aLocation );
847 return polySetB->
Collide( aA, aClearance, aActual, aLocation );
859 return CollCase<SHAPE_RECT, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
862 return CollCase<SHAPE_RECT, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
865 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
868 return CollCase<SHAPE_RECT, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
872 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
875 return CollCaseReversed<SHAPE_RECT, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
889 return CollCaseReversed<SHAPE_CIRCLE, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
892 return CollCase<SHAPE_CIRCLE, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
895 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
898 return CollCase<SHAPE_CIRCLE, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
902 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
905 return CollCaseReversed<SHAPE_CIRCLE, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
919 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN>( aB, aA, aClearance, aActual, aLocation, aMTV );
922 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN>( aB, aA, aClearance, aActual, aLocation, aMTV );
925 return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
928 return CollCase<SHAPE_LINE_CHAIN, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
932 return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
935 return CollCaseReversed<SHAPE_LINE_CHAIN, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
949 return CollCase<SHAPE_RECT, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
952 return CollCaseReversed<SHAPE_SEGMENT, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
955 return CollCase<SHAPE_LINE_CHAIN, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
958 return CollCase<SHAPE_SEGMENT, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
962 return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
965 return CollCaseReversed<SHAPE_SEGMENT, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
980 return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
983 return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
986 return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
989 return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
993 return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
996 return CollCaseReversed<SHAPE_LINE_CHAIN_BASE, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
1007 switch( aB->
Type() )
1010 return CollCase<SHAPE_ARC, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
1013 return CollCase<SHAPE_ARC, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
1016 return CollCase<SHAPE_ARC, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
1019 return CollCase<SHAPE_ARC, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
1023 return CollCase<SHAPE_ARC, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
1026 return CollCase<SHAPE_ARC, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
1040 wxFAIL_MSG( wxString::Format( wxT(
"Unsupported collision: %s with %s" ),
1050 int currentActual = std::numeric_limits<int>::max();
1053 bool colliding =
false;
1061 if( aActual && currentActual > 0 )
1070 auto collideCompoundSubshapes =
1071 [&](
const SHAPE* elemA,
const SHAPE* elemB,
int clearance ) ->
bool
1078 aActual || aLocation ? &actual :
nullptr,
1079 aLocation ? &location :
nullptr,
1080 aMTV ? &mtv :
nullptr ) )
1082 if( actual < currentActual )
1084 currentActual = actual;
1085 currentLocation = location;
1108 if( collideCompoundSubshapes( elemA, elemB, aClearance ) )
1127 if( collideCompoundSubshapes( elemA, aB, aClearance ) )
1142 if( collideCompoundSubshapes( aA, elemB, aClearance ) )
1159 *aLocation = currentLocation;
1162 *aActual = currentActual;
1174 return collideShapes(
this, aShape, aClearance,
nullptr,
nullptr, aMTV );
1180 return collideShapes(
this, aShape, aClearance, aActual, aLocation,
nullptr );
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.
int Length() const
Return the length (this).
static SEG::ecoord Square(int a)
int Distance(const SEG &aSeg) const
Compute minimum Euclidean distance to segment aSeg.
int Intersect(const CIRCLE &aArc, std::vector< VECTOR2I > *aIpsBuffer) const
Find intersection points between this arc and a CIRCLE.
const VECTOR2I & GetP1() const
int IntersectLine(const SEG &aSeg, std::vector< VECTOR2I > *aIpsBuffer) const
Find intersection points between this arc and aSeg, treating aSeg as an infinite line.
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 & GetP0() const
const VECTOR2I & GetCenter() 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