2013-09-10 13:43:09 +02:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
2025-01-01 13:30:11 -08:00
|
|
|
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
|
2013-09-10 13:43:09 +02:00
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2019-12-05 06:03:15 -08:00
|
|
|
#include <cmath>
|
2023-03-10 13:45:11 +00:00
|
|
|
#include <limits>
|
2013-09-10 13:43:09 +02:00
|
|
|
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <geometry/seg.h> // for SEG
|
2013-09-10 13:43:09 +02:00
|
|
|
#include <geometry/shape.h>
|
2018-05-16 13:06:44 -07:00
|
|
|
#include <geometry/shape_arc.h>
|
2013-09-10 13:43:09 +02:00
|
|
|
#include <geometry/shape_line_chain.h>
|
|
|
|
#include <geometry/shape_circle.h>
|
|
|
|
#include <geometry/shape_rect.h>
|
2014-05-14 11:45:01 +02:00
|
|
|
#include <geometry/shape_segment.h>
|
2020-07-21 11:42:18 +02:00
|
|
|
#include <geometry/shape_compound.h>
|
2023-06-06 13:39:53 +01:00
|
|
|
#include <geometry/shape_poly_set.h>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <math/vector2d.h>
|
2013-09-10 13:43:09 +02:00
|
|
|
|
2013-09-23 17:02:25 +02:00
|
|
|
typedef VECTOR2I::extended_type ecoord;
|
2013-09-10 13:43:09 +02:00
|
|
|
|
2020-12-12 12:36:09 +00:00
|
|
|
|
2013-09-27 18:51:21 +02:00
|
|
|
static inline bool Collide( const SHAPE_CIRCLE& aA, const SHAPE_CIRCLE& aB, int aClearance,
|
2020-09-28 23:27:33 +01:00
|
|
|
int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2013-09-10 13:43:09 +02:00
|
|
|
{
|
2013-10-14 13:43:57 +02:00
|
|
|
ecoord min_dist = aClearance + aA.GetRadius() + aB.GetRadius();
|
|
|
|
ecoord min_dist_sq = min_dist * min_dist;
|
2013-09-10 13:43:09 +02:00
|
|
|
|
2013-10-14 13:43:57 +02:00
|
|
|
const VECTOR2I delta = aB.GetCenter() - aA.GetCenter();
|
|
|
|
ecoord dist_sq = delta.SquaredEuclideanNorm();
|
2013-09-10 13:43:09 +02:00
|
|
|
|
2020-10-04 10:46:18 +01:00
|
|
|
if( dist_sq == 0 || dist_sq < min_dist_sq )
|
2020-09-28 23:27:33 +01:00
|
|
|
{
|
|
|
|
if( aActual )
|
|
|
|
*aActual = std::max( 0, (int) sqrt( dist_sq ) - aA.GetRadius() - aB.GetRadius() );
|
2013-10-14 13:43:57 +02:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
if( aLocation )
|
|
|
|
*aLocation = ( aA.GetCenter() + aB.GetCenter() ) / 2;
|
2020-07-02 17:06:09 +01:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
if( aMTV )
|
|
|
|
*aMTV = delta.Resize( min_dist - sqrt( dist_sq ) + 3 ); // fixme: apparent rounding error
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-14 13:43:57 +02:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
return false;
|
2013-09-10 13:43:09 +02:00
|
|
|
}
|
|
|
|
|
2013-10-14 20:40:36 +02:00
|
|
|
|
2014-05-14 11:45:01 +02:00
|
|
|
static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_CIRCLE& aB, int aClearance,
|
2020-09-28 23:27:33 +01:00
|
|
|
int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2013-09-10 13:43:09 +02:00
|
|
|
{
|
2013-10-14 13:43:57 +02:00
|
|
|
const VECTOR2I c = aB.GetCenter();
|
|
|
|
const VECTOR2I p0 = aA.GetPosition();
|
|
|
|
const VECTOR2I size = aA.GetSize();
|
2020-12-12 12:36:09 +00:00
|
|
|
const int r = aB.GetRadius();
|
|
|
|
const int min_dist = aClearance + r;
|
|
|
|
const ecoord min_dist_sq = SEG::Square( min_dist );
|
2014-09-09 18:46:12 +02:00
|
|
|
|
2013-10-14 16:13:35 +02:00
|
|
|
const VECTOR2I vts[] =
|
|
|
|
{
|
|
|
|
VECTOR2I( p0.x, p0.y ),
|
|
|
|
VECTOR2I( p0.x, p0.y + size.y ),
|
|
|
|
VECTOR2I( p0.x + size.x, p0.y + size.y ),
|
|
|
|
VECTOR2I( p0.x + size.x, p0.y ),
|
|
|
|
VECTOR2I( p0.x, p0.y )
|
|
|
|
};
|
2013-10-14 13:43:57 +02:00
|
|
|
|
2020-12-12 12:36:09 +00:00
|
|
|
ecoord nearest_side_dist_sq = VECTOR2I::ECOORD_MAX;
|
2013-10-14 13:43:57 +02:00
|
|
|
VECTOR2I nearest;
|
|
|
|
|
2013-10-14 16:13:35 +02:00
|
|
|
bool inside = c.x >= p0.x && c.x <= ( p0.x + size.x )
|
|
|
|
&& c.y >= p0.y && c.y <= ( p0.y + size.y );
|
2013-10-14 13:43:57 +02:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
// If we're not looking for MTV or actual, short-circuit once we find a hard collision
|
|
|
|
if( inside && !aActual && !aLocation && !aMTV )
|
2014-05-14 11:45:01 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
for( int i = 0; i < 4; i++ )
|
2013-10-14 13:43:57 +02:00
|
|
|
{
|
2020-07-02 17:06:09 +01:00
|
|
|
const SEG side( vts[i], vts[ i + 1] );
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
VECTOR2I pn = side.NearestPoint( c );
|
2020-07-02 22:52:37 +01:00
|
|
|
ecoord side_dist_sq = ( pn - c ).SquaredEuclideanNorm();
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
if( side_dist_sq < nearest_side_dist_sq )
|
2013-10-14 13:43:57 +02:00
|
|
|
{
|
2014-05-14 11:45:01 +02:00
|
|
|
nearest = pn;
|
2020-07-02 17:06:09 +01:00
|
|
|
nearest_side_dist_sq = side_dist_sq;
|
2020-09-28 23:27:33 +01:00
|
|
|
|
2020-10-05 22:22:01 +01:00
|
|
|
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 )
|
2020-09-28 23:27:33 +01:00
|
|
|
break;
|
2013-10-14 13:43:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-04 10:46:18 +01:00
|
|
|
if( inside || nearest_side_dist_sq == 0 || nearest_side_dist_sq < min_dist_sq )
|
2020-09-28 23:27:33 +01:00
|
|
|
{
|
|
|
|
if( aLocation )
|
|
|
|
*aLocation = nearest;
|
2013-10-14 13:43:57 +02:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
if( aActual )
|
|
|
|
*aActual = std::max( 0, (int) sqrt( nearest_side_dist_sq ) - r );
|
2013-10-14 13:43:57 +02:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
if( aMTV )
|
|
|
|
{
|
|
|
|
VECTOR2I delta = c - nearest;
|
2014-09-09 18:46:12 +02:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
if( inside )
|
|
|
|
*aMTV = -delta.Resize( abs( min_dist + 1 + sqrt( nearest_side_dist_sq ) ) + 1 );
|
|
|
|
else
|
|
|
|
*aMTV = delta.Resize( abs( min_dist + 1 - sqrt( nearest_side_dist_sq ) ) + 1 );
|
|
|
|
}
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
return true;
|
|
|
|
}
|
2013-10-14 13:43:57 +02:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
return false;
|
2013-09-10 13:43:09 +02:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:23:43 +02:00
|
|
|
|
2014-05-14 11:45:01 +02:00
|
|
|
static VECTOR2I pushoutForce( const SHAPE_CIRCLE& aA, const SEG& aB, int aClearance )
|
|
|
|
{
|
2016-03-01 14:26:08 +01:00
|
|
|
VECTOR2I f( 0, 0 );
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2016-03-01 14:26:08 +01:00
|
|
|
const VECTOR2I c = aA.GetCenter();
|
|
|
|
const VECTOR2I nearest = aB.NearestPoint( c );
|
|
|
|
|
|
|
|
const int r = aA.GetRadius();
|
|
|
|
|
|
|
|
int dist = ( nearest - c ).EuclideanNorm();
|
|
|
|
int min_dist = aClearance + r;
|
2014-05-14 11:45:01 +02:00
|
|
|
|
|
|
|
if( dist < min_dist )
|
2016-03-01 14:26:08 +01:00
|
|
|
{
|
|
|
|
for( int corr = 0; corr < 5; corr++ )
|
|
|
|
{
|
|
|
|
f = ( aA.GetCenter() - nearest ).Resize( min_dist - dist + corr );
|
|
|
|
|
|
|
|
if( aB.Distance( c + f ) >= min_dist )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-05-14 11:45:01 +02:00
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
|
|
|
|
static inline bool Collide( const SHAPE_CIRCLE& aA, const SHAPE_LINE_CHAIN_BASE& aB,
|
|
|
|
int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2013-09-10 13:43:09 +02:00
|
|
|
{
|
2023-03-10 13:45:11 +00:00
|
|
|
int closest_dist = std::numeric_limits<int>::max();
|
|
|
|
int closest_mtv_dist = std::numeric_limits<int>::max();
|
2020-09-28 23:27:33 +01:00
|
|
|
VECTOR2I nearest;
|
2020-10-19 21:41:26 +02:00
|
|
|
int closest_mtv_seg = -1;
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-10-05 22:22:01 +01:00
|
|
|
if( aB.IsClosed() && aB.PointInside( aA.GetCenter() ) )
|
2013-10-14 13:43:57 +02:00
|
|
|
{
|
2020-10-05 22:22:01 +01:00
|
|
|
nearest = aA.GetCenter();
|
2020-10-19 21:41:26 +02:00
|
|
|
closest_dist = 0;
|
|
|
|
|
|
|
|
if( aMTV )
|
|
|
|
{
|
2023-03-10 16:14:27 +00:00
|
|
|
for( size_t s = 0; s < aB.GetSegmentCount(); s++ )
|
2020-10-19 21:41:26 +02:00
|
|
|
{
|
|
|
|
int dist = aB.GetSegment(s).Distance( aA.GetCenter() );
|
|
|
|
|
|
|
|
if( dist < closest_mtv_dist )
|
|
|
|
{
|
|
|
|
closest_mtv_dist = dist;
|
|
|
|
closest_mtv_seg = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 22:22:01 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-09 23:12:44 +00:00
|
|
|
for( size_t s = 0; s < aB.GetSegmentCount(); s++ )
|
2020-07-02 17:06:09 +01:00
|
|
|
{
|
2020-10-05 22:22:01 +01:00
|
|
|
int collision_dist = 0;
|
|
|
|
VECTOR2I pn;
|
|
|
|
|
|
|
|
if( aA.Collide( aB.GetSegment( s ), aClearance,
|
|
|
|
aActual || aLocation ? &collision_dist : nullptr,
|
|
|
|
aLocation ? &pn : nullptr ) )
|
2020-09-28 23:27:33 +01:00
|
|
|
{
|
2020-10-05 22:22:01 +01:00
|
|
|
if( collision_dist < closest_dist )
|
|
|
|
{
|
|
|
|
nearest = pn;
|
|
|
|
closest_dist = collision_dist;
|
|
|
|
}
|
2020-07-02 17:06:09 +01:00
|
|
|
|
2020-10-05 22:22:01 +01:00
|
|
|
if( closest_dist == 0 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If we're not looking for aActual then any collision will do
|
|
|
|
if( !aActual )
|
2020-09-28 23:27:33 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-05-14 11:45:01 +02:00
|
|
|
}
|
2014-09-09 18:46:12 +02:00
|
|
|
|
2020-10-04 10:46:18 +01:00
|
|
|
if( closest_dist == 0 || closest_dist < aClearance )
|
2020-09-28 23:27:33 +01:00
|
|
|
{
|
|
|
|
if( aLocation )
|
|
|
|
*aLocation = nearest;
|
2015-07-02 16:09:43 +02:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
if( aActual )
|
|
|
|
*aActual = closest_dist;
|
2015-07-02 16:09:43 +02:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
if( aMTV )
|
|
|
|
{
|
|
|
|
SHAPE_CIRCLE cmoved( aA );
|
|
|
|
VECTOR2I f_total( 0, 0 );
|
2015-07-02 16:09:43 +02:00
|
|
|
|
2020-10-19 21:41:26 +02:00
|
|
|
VECTOR2I f;
|
|
|
|
|
|
|
|
if (closest_mtv_seg >= 0)
|
|
|
|
{
|
|
|
|
SEG cs = aB.GetSegment( closest_mtv_seg );
|
|
|
|
VECTOR2I np = cs.NearestPoint( aA.GetCenter() );
|
|
|
|
f = ( np - aA.GetCenter() ) + ( np - aA.GetCenter() ).Resize( aA.GetRadius() );
|
|
|
|
}
|
|
|
|
|
|
|
|
cmoved.SetCenter( cmoved.GetCenter() + f );
|
|
|
|
f_total += f;
|
|
|
|
|
2023-03-10 16:14:27 +00:00
|
|
|
for( size_t s = 0; s < aB.GetSegmentCount(); s++ )
|
2020-09-28 23:27:33 +01:00
|
|
|
{
|
2023-03-10 17:42:36 +00:00
|
|
|
f = pushoutForce( cmoved, aB.GetSegment( s ), aClearance );
|
2020-09-28 23:27:33 +01:00
|
|
|
cmoved.SetCenter( cmoved.GetCenter() + f );
|
|
|
|
f_total += f;
|
|
|
|
}
|
2020-07-02 17:06:09 +01:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
*aMTV = f_total;
|
2020-07-02 17:06:09 +01:00
|
|
|
}
|
2015-07-02 16:09:43 +02:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
return true;
|
2020-07-02 17:06:09 +01:00
|
|
|
}
|
2020-09-28 23:27:33 +01:00
|
|
|
|
|
|
|
return false;
|
2015-07-02 16:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-14 11:45:01 +02:00
|
|
|
static inline bool Collide( const SHAPE_CIRCLE& aA, const SHAPE_SEGMENT& aSeg, int aClearance,
|
2020-09-28 23:27:33 +01:00
|
|
|
int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2014-05-14 11:45:01 +02:00
|
|
|
{
|
2020-09-28 23:27:33 +01:00
|
|
|
if( aA.Collide( aSeg.GetSeg(), aClearance + aSeg.GetWidth() / 2, aActual, aLocation ) )
|
|
|
|
{
|
|
|
|
if( aMTV )
|
|
|
|
*aMTV = -pushoutForce( aA, aSeg.GetSeg(), aClearance + aSeg.GetWidth() / 2);
|
2020-07-02 17:06:09 +01:00
|
|
|
|
2020-10-14 11:05:23 +01:00
|
|
|
if( aActual )
|
|
|
|
*aActual = std::max( 0, *aActual - aSeg.GetWidth() / 2 );
|
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
return false;
|
2013-09-10 13:43:09 +02:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:23:43 +02:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
static inline bool Collide( const SHAPE_LINE_CHAIN_BASE& aA, const SHAPE_LINE_CHAIN_BASE& aB,
|
|
|
|
int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2013-09-10 13:43:09 +02:00
|
|
|
{
|
2022-02-09 09:01:30 -05:00
|
|
|
wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
|
2022-07-28 09:36:31 -07:00
|
|
|
aA.TypeName(),
|
|
|
|
aB.TypeName() ) );
|
2020-07-03 16:12:28 +01:00
|
|
|
|
2023-03-10 13:45:11 +00:00
|
|
|
int closest_dist = std::numeric_limits<int>::max();
|
2020-09-28 23:27:33 +01:00
|
|
|
VECTOR2I nearest;
|
|
|
|
|
2020-10-05 22:22:01 +01:00
|
|
|
if( aB.IsClosed() && aA.GetPointCount() > 0 && aB.PointInside( aA.GetPoint( 0 ) ) )
|
2020-07-02 17:06:09 +01:00
|
|
|
{
|
2020-10-05 22:22:01 +01:00
|
|
|
closest_dist = 0;
|
|
|
|
nearest = aA.GetPoint( 0 );
|
|
|
|
}
|
2024-03-18 15:51:04 -07:00
|
|
|
else if( aA.IsClosed() && aB.GetPointCount() > 0 && aA.PointInside( aB.GetPoint( 0 ) ) )
|
|
|
|
{
|
|
|
|
closest_dist = 0;
|
|
|
|
nearest = aB.GetPoint( 0 );
|
|
|
|
}
|
2020-10-05 22:22:01 +01:00
|
|
|
else
|
|
|
|
{
|
2024-03-11 16:28:51 -07:00
|
|
|
std::vector<SEG> a_segs;
|
|
|
|
std::vector<SEG> b_segs;
|
2020-10-05 22:22:01 +01:00
|
|
|
|
2024-03-11 16:28:51 -07:00
|
|
|
for( size_t ii = 0; ii < aA.GetSegmentCount(); ii++ )
|
|
|
|
{
|
|
|
|
if( aA.Type() != SH_LINE_CHAIN
|
|
|
|
|| !static_cast<const SHAPE_LINE_CHAIN*>( &aA )->IsArcSegment( ii ) )
|
2021-11-05 18:06:22 +00:00
|
|
|
{
|
2024-03-11 16:28:51 -07:00
|
|
|
a_segs.push_back( aA.GetSegment( ii ) );
|
|
|
|
}
|
|
|
|
}
|
2021-11-05 18:06:22 +00:00
|
|
|
|
2024-03-11 16:28:51 -07:00
|
|
|
for( size_t ii = 0; ii < aB.GetSegmentCount(); ii++ )
|
|
|
|
{
|
|
|
|
if( aB.Type() != SH_LINE_CHAIN
|
|
|
|
|| !static_cast<const SHAPE_LINE_CHAIN*>( &aB )->IsArcSegment( ii ) )
|
|
|
|
{
|
|
|
|
b_segs.push_back( aB.GetSegment( ii ) );
|
2021-11-05 18:06:22 +00:00
|
|
|
}
|
2024-03-11 16:28:51 -07:00
|
|
|
}
|
2021-11-05 18:06:22 +00:00
|
|
|
|
2024-03-11 16:28:51 -07:00
|
|
|
auto seg_sort = []( const SEG& a, const SEG& b )
|
|
|
|
{
|
|
|
|
return a.A.x < b.A.x || ( a.A.x == b.A.x && a.A.y < b.A.y );
|
|
|
|
};
|
|
|
|
|
|
|
|
std::sort( a_segs.begin(), a_segs.end(), seg_sort );
|
|
|
|
std::sort( b_segs.begin(), b_segs.end(), seg_sort );
|
|
|
|
|
|
|
|
for( const SEG& a_seg : a_segs )
|
|
|
|
{
|
|
|
|
for( const SEG& b_seg : b_segs )
|
2020-09-28 23:27:33 +01:00
|
|
|
{
|
2024-03-11 16:28:51 -07:00
|
|
|
int dist = 0;
|
|
|
|
|
|
|
|
if( a_seg.Collide( b_seg, aClearance, aActual || aLocation ? &dist : nullptr ) )
|
2020-10-05 22:22:01 +01:00
|
|
|
{
|
2024-03-11 16:28:51 -07:00
|
|
|
if( dist < closest_dist )
|
|
|
|
{
|
|
|
|
nearest = a_seg.NearestPoint( b_seg );
|
|
|
|
closest_dist = dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( closest_dist == 0 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If we're not looking for aActual then any collision will do
|
|
|
|
if( !aActual )
|
|
|
|
break;
|
2020-10-05 22:22:01 +01:00
|
|
|
}
|
2020-09-28 23:27:33 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-22 10:41:42 -07:00
|
|
|
}
|
2021-11-05 18:06:22 +00:00
|
|
|
|
2024-03-22 10:41:42 -07:00
|
|
|
if( (!aActual && !aLocation ) || closest_dist > 0 )
|
|
|
|
{
|
|
|
|
std::vector<const SHAPE_LINE_CHAIN*> chains = {
|
|
|
|
dynamic_cast<const SHAPE_LINE_CHAIN*>( &aA ),
|
|
|
|
dynamic_cast<const SHAPE_LINE_CHAIN*>( &aB )
|
|
|
|
};
|
2021-11-05 18:06:22 +00:00
|
|
|
|
2024-03-22 10:41:42 -07:00
|
|
|
std::vector<const SHAPE*> shapes = { &aA, &aB };
|
2024-03-11 17:19:30 -07:00
|
|
|
|
2024-03-22 10:41:42 -07:00
|
|
|
for( int ii = 0; ii < 2; ii++ )
|
|
|
|
{
|
|
|
|
const SHAPE_LINE_CHAIN* chain = chains[ii];
|
|
|
|
const SHAPE* other = shapes[( ii + 1 ) % 2];
|
2024-03-11 17:19:30 -07:00
|
|
|
|
2024-03-22 10:41:42 -07:00
|
|
|
if( !chain )
|
|
|
|
continue;
|
2021-11-05 18:06:22 +00:00
|
|
|
|
2024-03-22 10:41:42 -07:00
|
|
|
for( size_t jj = 0; jj < chain->ArcCount(); jj++ )
|
|
|
|
{
|
|
|
|
const SHAPE_ARC& arc = chain->Arc( jj );
|
2021-11-05 18:06:22 +00:00
|
|
|
|
2024-03-22 10:41:42 -07:00
|
|
|
if( arc.Collide( other, aClearance, aActual, aLocation ) )
|
|
|
|
return true;
|
2021-11-05 18:06:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 23:27:33 +01:00
|
|
|
}
|
|
|
|
|
2020-10-04 10:46:18 +01:00
|
|
|
if( closest_dist == 0 || closest_dist < aClearance )
|
2020-09-28 23:27:33 +01:00
|
|
|
{
|
|
|
|
if( aLocation )
|
|
|
|
*aLocation = nearest;
|
|
|
|
|
|
|
|
if( aActual )
|
|
|
|
*aActual = closest_dist;
|
|
|
|
|
|
|
|
return true;
|
2020-07-02 17:06:09 +01:00
|
|
|
}
|
2013-10-14 16:13:35 +02:00
|
|
|
|
2013-10-14 13:43:57 +02:00
|
|
|
return false;
|
2013-09-10 13:43:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-05 01:08:18 +02:00
|
|
|
static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_LINE_CHAIN_BASE& aB, int aClearance,
|
2020-09-28 23:27:33 +01:00
|
|
|
int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2013-09-10 13:43:09 +02:00
|
|
|
{
|
2022-02-09 09:01:30 -05:00
|
|
|
wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
|
2022-07-28 09:36:31 -07:00
|
|
|
aA.TypeName(),
|
|
|
|
aB.TypeName() ) );
|
2020-09-28 23:27:33 +01:00
|
|
|
|
2023-03-10 13:45:11 +00:00
|
|
|
int closest_dist = std::numeric_limits<int>::max();
|
2020-09-28 23:27:33 +01:00
|
|
|
VECTOR2I nearest;
|
2020-07-02 17:06:09 +01:00
|
|
|
|
2020-10-05 22:22:01 +01:00
|
|
|
if( aB.IsClosed() && aB.PointInside( aA.Centre() ) )
|
2013-10-14 13:43:57 +02:00
|
|
|
{
|
2020-10-05 22:22:01 +01:00
|
|
|
nearest = aA.Centre();
|
|
|
|
closest_dist = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-09 23:12:44 +00:00
|
|
|
for( size_t s = 0; s < aB.GetSegmentCount(); s++ )
|
2020-07-02 17:06:09 +01:00
|
|
|
{
|
2020-10-05 22:22:01 +01:00
|
|
|
int collision_dist = 0;
|
|
|
|
VECTOR2I pn;
|
|
|
|
|
|
|
|
if( aA.Collide( aB.GetSegment( s ), aClearance,
|
|
|
|
aActual || aLocation ? &collision_dist : nullptr,
|
|
|
|
aLocation ? &pn : nullptr ) )
|
2020-09-28 23:27:33 +01:00
|
|
|
{
|
2020-10-05 22:22:01 +01:00
|
|
|
if( collision_dist < closest_dist )
|
|
|
|
{
|
|
|
|
nearest = pn;
|
|
|
|
closest_dist = collision_dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( closest_dist == 0 )
|
|
|
|
break;
|
2020-07-02 17:06:09 +01:00
|
|
|
|
2020-10-05 22:22:01 +01:00
|
|
|
// If we're not looking for aActual then any collision will do
|
|
|
|
if( !aActual )
|
2020-09-28 23:27:33 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-07-02 17:06:09 +01:00
|
|
|
}
|
2013-10-14 13:43:57 +02:00
|
|
|
}
|
|
|
|
|
2020-10-04 10:46:18 +01:00
|
|
|
if( closest_dist == 0 || closest_dist < aClearance )
|
2020-09-28 23:27:33 +01:00
|
|
|
{
|
|
|
|
if( aLocation )
|
|
|
|
*aLocation = nearest;
|
2020-07-02 17:06:09 +01:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
if( aActual )
|
|
|
|
*aActual = closest_dist;
|
2020-07-03 16:12:28 +01:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-09-10 13:43:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-12 12:36:09 +00:00
|
|
|
static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_SEGMENT& aB, int aClearance,
|
2020-09-28 23:27:33 +01:00
|
|
|
int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2013-09-10 13:43:09 +02:00
|
|
|
{
|
2022-02-09 09:01:30 -05:00
|
|
|
wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
|
2022-07-28 09:36:31 -07:00
|
|
|
aA.TypeName(),
|
|
|
|
aB.TypeName() ) );
|
2020-12-12 12:36:09 +00:00
|
|
|
|
2021-06-03 16:50:25 +01:00
|
|
|
bool rv = aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation );
|
2020-07-03 16:12:28 +01:00
|
|
|
|
2021-06-03 16:50:25 +01:00
|
|
|
if( aActual )
|
|
|
|
*aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
|
2020-07-02 17:06:09 +01:00
|
|
|
|
2021-06-03 16:50:25 +01:00
|
|
|
return rv;
|
2014-05-14 11:45:01 +02:00
|
|
|
}
|
2013-10-14 13:43:57 +02:00
|
|
|
|
|
|
|
|
2014-05-14 11:45:01 +02:00
|
|
|
static inline bool Collide( const SHAPE_SEGMENT& aA, const SHAPE_SEGMENT& aB, int aClearance,
|
2020-09-28 23:27:33 +01:00
|
|
|
int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2014-05-14 11:45:01 +02:00
|
|
|
{
|
2022-02-09 09:01:30 -05:00
|
|
|
wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
|
2022-07-28 09:36:31 -07:00
|
|
|
aA.TypeName(),
|
|
|
|
aB.TypeName() ) );
|
2020-12-12 12:36:09 +00:00
|
|
|
|
2021-06-03 16:50:25 +01:00
|
|
|
bool rv = aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation );
|
2020-07-02 17:06:09 +01:00
|
|
|
|
2021-06-03 16:50:25 +01:00
|
|
|
if( aActual )
|
|
|
|
*aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
|
2020-07-02 17:06:09 +01:00
|
|
|
|
2021-06-03 16:50:25 +01:00
|
|
|
return rv;
|
2014-05-14 11:45:01 +02:00
|
|
|
}
|
2013-10-14 13:43:57 +02:00
|
|
|
|
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
static inline bool Collide( const SHAPE_LINE_CHAIN_BASE& aA, const SHAPE_SEGMENT& aB,
|
|
|
|
int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2014-05-14 11:45:01 +02:00
|
|
|
{
|
2022-02-09 09:01:30 -05:00
|
|
|
wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
|
2022-07-28 09:36:31 -07:00
|
|
|
aA.TypeName(),
|
|
|
|
aB.TypeName() ) );
|
2020-12-12 12:36:09 +00:00
|
|
|
|
2021-06-03 16:50:25 +01:00
|
|
|
bool rv = aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation );
|
2020-07-02 17:06:09 +01:00
|
|
|
|
2021-06-03 16:50:25 +01:00
|
|
|
if( aActual )
|
|
|
|
*aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
|
2013-10-14 13:43:57 +02:00
|
|
|
|
2021-06-03 16:50:25 +01:00
|
|
|
return rv;
|
2014-05-14 11:45:01 +02:00
|
|
|
}
|
2013-10-14 13:43:57 +02:00
|
|
|
|
|
|
|
|
2015-08-19 18:07:16 +02:00
|
|
|
static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_RECT& aB, int aClearance,
|
2020-09-28 23:27:33 +01:00
|
|
|
int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2015-08-19 18:07:16 +02:00
|
|
|
{
|
2022-06-18 19:47:11 +01:00
|
|
|
if( aClearance || aActual || aLocation || aMTV )
|
|
|
|
{
|
|
|
|
return Collide( aA.Outline(), aB.Outline(), aClearance, aActual, aLocation, aMTV );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BOX2I bboxa = aA.BBox();
|
|
|
|
BOX2I bboxb = aB.BBox();
|
|
|
|
|
|
|
|
return bboxa.Intersects( bboxb );
|
|
|
|
}
|
2015-08-19 18:07:16 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 12:36:09 +00:00
|
|
|
|
2018-05-16 13:06:44 -07:00
|
|
|
static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_RECT& aB, int aClearance,
|
2020-09-28 23:27:33 +01:00
|
|
|
int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2018-05-16 13:06:44 -07:00
|
|
|
{
|
2022-02-09 09:01:30 -05:00
|
|
|
wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
|
2022-07-28 09:36:31 -07:00
|
|
|
aA.TypeName(),
|
|
|
|
aB.TypeName() ) );
|
2021-06-03 16:50:25 +01:00
|
|
|
|
2021-11-05 18:06:22 +00:00
|
|
|
const SHAPE_LINE_CHAIN lc( aA );
|
2020-12-12 12:36:09 +00:00
|
|
|
|
2021-06-03 16:50:25 +01:00
|
|
|
bool rv = Collide( lc, aB.Outline(), aClearance + aA.GetWidth() / 2, aActual, aLocation, aMTV );
|
|
|
|
|
|
|
|
if( rv && aActual )
|
|
|
|
*aActual = std::max( 0, *aActual - aA.GetWidth() / 2 );
|
|
|
|
|
|
|
|
return rv;
|
2018-05-16 13:06:44 -07:00
|
|
|
}
|
|
|
|
|
2020-12-12 12:36:09 +00:00
|
|
|
|
2018-05-16 13:06:44 -07:00
|
|
|
static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_CIRCLE& aB, int aClearance,
|
2020-09-28 23:27:33 +01:00
|
|
|
int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2018-05-16 13:06:44 -07:00
|
|
|
{
|
2025-01-14 12:16:13 -08:00
|
|
|
VECTOR2I ptA, ptB;
|
|
|
|
int64_t dist_sq = std::numeric_limits<int64_t>::max();
|
|
|
|
aA.NearestPoints( aB, ptA, ptB, dist_sq );
|
|
|
|
int half_width = ( aA.GetWidth() + 1 ) / 2;
|
|
|
|
int min_dist = aClearance + half_width;
|
2018-05-16 13:06:44 -07:00
|
|
|
|
2025-01-14 12:16:13 -08:00
|
|
|
if( dist_sq < SEG::Square( min_dist ) )
|
|
|
|
{
|
|
|
|
if( aLocation )
|
|
|
|
*aLocation = ( ptA + ptB ) / 2;
|
2021-06-03 16:50:25 +01:00
|
|
|
|
2025-01-14 12:16:13 -08:00
|
|
|
if( aActual )
|
|
|
|
*aActual = std::max( 0, KiROUND( std::sqrt( dist_sq ) - half_width ) );
|
2018-05-16 13:06:44 -07:00
|
|
|
|
2025-01-14 12:16:13 -08:00
|
|
|
if( aMTV )
|
|
|
|
{
|
|
|
|
const VECTOR2I delta = ptB - ptA;
|
|
|
|
*aMTV = delta.Resize( min_dist - std::sqrt( dist_sq ) + 3 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-05-16 13:06:44 -07:00
|
|
|
}
|
|
|
|
|
2020-12-12 12:36:09 +00:00
|
|
|
|
2018-05-16 13:06:44 -07:00
|
|
|
static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_LINE_CHAIN& aB, int aClearance,
|
2020-09-28 23:27:33 +01:00
|
|
|
int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2018-05-16 13:06:44 -07:00
|
|
|
{
|
2022-02-09 09:01:30 -05:00
|
|
|
wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
|
2022-07-28 09:36:31 -07:00
|
|
|
aA.TypeName(),
|
|
|
|
aB.TypeName() ) );
|
2021-06-03 16:50:25 +01:00
|
|
|
|
2023-03-10 13:45:11 +00:00
|
|
|
int closest_dist = std::numeric_limits<int>::max();
|
2021-11-05 18:06:22 +00:00
|
|
|
VECTOR2I nearest;
|
2020-12-12 12:36:09 +00:00
|
|
|
|
2021-11-05 18:06:22 +00:00
|
|
|
if( aB.IsClosed() && aB.PointInside( aA.GetP0() ) )
|
|
|
|
{
|
|
|
|
closest_dist = 0;
|
|
|
|
nearest = aA.GetP0();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-11 16:28:51 -07:00
|
|
|
int collision_dist = 0;
|
|
|
|
VECTOR2I pn;
|
|
|
|
|
2021-11-05 18:06:22 +00:00
|
|
|
for( size_t i = 0; i < aB.GetSegmentCount(); i++ )
|
|
|
|
{
|
|
|
|
// ignore arcs - we will collide these separately
|
|
|
|
if( aB.IsArcSegment( i ) )
|
|
|
|
continue;
|
2021-06-03 16:50:25 +01:00
|
|
|
|
2021-11-05 18:06:22 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( size_t i = 0; i < aB.ArcCount(); i++ )
|
|
|
|
{
|
|
|
|
const SHAPE_ARC& arc = aB.Arc( i );
|
|
|
|
|
|
|
|
// The arcs in the chain should have zero width
|
2022-02-09 09:01:30 -05:00
|
|
|
wxASSERT_MSG( arc.GetWidth() == 0, wxT( "Invalid arc width - should be zero" ) );
|
2021-11-05 18:06:22 +00:00
|
|
|
|
2024-03-11 16:28:51 -07:00
|
|
|
if( aA.Collide( &arc, 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( !aActual )
|
|
|
|
break;
|
|
|
|
}
|
2021-11-05 18:06:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( closest_dist == 0 || closest_dist < aClearance )
|
|
|
|
{
|
|
|
|
if( aLocation )
|
|
|
|
*aLocation = nearest;
|
|
|
|
|
|
|
|
if( aActual )
|
|
|
|
*aActual = closest_dist;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-05-16 13:06:44 -07:00
|
|
|
}
|
|
|
|
|
2020-12-12 12:36:09 +00:00
|
|
|
|
2018-05-16 13:06:44 -07:00
|
|
|
static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_SEGMENT& aB, int aClearance,
|
2020-09-28 23:27:33 +01:00
|
|
|
int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2018-05-16 13:06:44 -07:00
|
|
|
{
|
2022-02-09 09:01:30 -05:00
|
|
|
wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
|
2022-07-28 09:36:31 -07:00
|
|
|
aA.TypeName(),
|
|
|
|
aB.TypeName() ) );
|
2021-06-03 16:50:25 +01:00
|
|
|
|
2024-12-30 12:55:51 -08:00
|
|
|
// If the arc radius is too large, it is effectively a line segment
|
|
|
|
if( aA.IsEffectiveLine() )
|
|
|
|
{
|
|
|
|
SHAPE_SEGMENT tmp( aA.GetP0(), aA.GetP1(), aA.GetWidth() );
|
|
|
|
return Collide( tmp, aB, aClearance, aActual, aLocation, aMTV );
|
|
|
|
}
|
|
|
|
|
2024-05-23 15:21:40 +00:00
|
|
|
bool rv = aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation );
|
2021-06-03 16:50:25 +01:00
|
|
|
|
|
|
|
if( rv && aActual )
|
2024-05-23 15:21:40 +00:00
|
|
|
*aActual = std::max( 0, *aActual - aB.GetWidth() / 2 );
|
2021-06-03 16:50:25 +01:00
|
|
|
|
|
|
|
return rv;
|
2018-05-16 13:06:44 -07:00
|
|
|
}
|
|
|
|
|
2020-12-12 12:36:09 +00:00
|
|
|
|
2020-09-05 01:08:18 +02:00
|
|
|
static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_LINE_CHAIN_BASE& aB, int aClearance,
|
2020-09-28 23:27:33 +01:00
|
|
|
int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2018-05-16 13:06:44 -07:00
|
|
|
{
|
2024-12-30 12:55:51 -08:00
|
|
|
// If the arc radius is too large, it is effectively a line segment
|
|
|
|
if( aA.IsEffectiveLine() )
|
|
|
|
{
|
|
|
|
SHAPE_SEGMENT tmp( aA.GetP0(), aA.GetP1(), aA.GetWidth() );
|
|
|
|
return Collide( aB, tmp, aClearance, aActual, aLocation, aMTV );
|
|
|
|
}
|
|
|
|
|
2022-02-09 09:01:30 -05:00
|
|
|
wxASSERT_MSG( !aMTV, wxString::Format( wxT( "MTV not implemented for %s : %s collisions" ),
|
2022-07-28 09:36:31 -07:00
|
|
|
aA.TypeName(),
|
|
|
|
aB.TypeName() ) );
|
2021-06-03 16:50:25 +01:00
|
|
|
|
2023-03-10 13:45:11 +00:00
|
|
|
int closest_dist = std::numeric_limits<int>::max();
|
2021-07-26 17:14:05 +01:00
|
|
|
VECTOR2I nearest;
|
2020-07-02 17:06:09 +01:00
|
|
|
|
2021-07-26 17:14:05 +01:00
|
|
|
if( aB.IsClosed() && aB.PointInside( aA.GetP0() ) )
|
|
|
|
{
|
|
|
|
closest_dist = 0;
|
|
|
|
nearest = aA.GetP0();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( size_t i = 0; i < aB.GetSegmentCount(); i++ )
|
|
|
|
{
|
|
|
|
int collision_dist = 0;
|
|
|
|
VECTOR2I pn;
|
2021-06-03 16:50:25 +01:00
|
|
|
|
2021-07-26 17:14:05 +01:00
|
|
|
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;
|
|
|
|
}
|
2021-06-03 16:50:25 +01:00
|
|
|
|
2021-07-26 17:14:05 +01:00
|
|
|
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 )
|
|
|
|
{
|
|
|
|
if( aLocation )
|
|
|
|
*aLocation = nearest;
|
|
|
|
|
|
|
|
if( aActual )
|
|
|
|
*aActual = closest_dist;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-05-16 13:06:44 -07:00
|
|
|
}
|
|
|
|
|
2020-12-12 12:36:09 +00:00
|
|
|
|
2018-05-16 13:06:44 -07:00
|
|
|
static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_ARC& aB, int aClearance,
|
2020-09-28 23:27:33 +01:00
|
|
|
int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2018-05-16 13:06:44 -07:00
|
|
|
{
|
2025-01-13 18:29:30 -08:00
|
|
|
VECTOR2I ptA, ptB;
|
|
|
|
int64_t dist_sq = std::numeric_limits<int64_t>::max();
|
|
|
|
aA.NearestPoints( aB, ptA, ptB, dist_sq );
|
|
|
|
int dual_width = ( aA.GetWidth() + aB.GetWidth() ) / 2;
|
2025-01-14 10:47:55 -08:00
|
|
|
int min_dist = aClearance + dual_width;
|
2020-12-12 12:36:09 +00:00
|
|
|
|
2025-01-14 10:47:55 -08:00
|
|
|
if( dist_sq < SEG::Square( min_dist ) )
|
2021-07-25 20:46:00 +01:00
|
|
|
{
|
|
|
|
if( aLocation )
|
2025-01-13 18:29:30 -08:00
|
|
|
*aLocation = ( ptA + ptB ) / 2;
|
2021-07-25 20:46:00 +01:00
|
|
|
|
2025-01-13 18:29:30 -08:00
|
|
|
if( aActual )
|
|
|
|
*aActual = std::max( 0, KiROUND( std::sqrt( dist_sq ) - dual_width ) );
|
2021-07-25 20:46:00 +01:00
|
|
|
|
2025-01-14 10:47:55 -08:00
|
|
|
if( aMTV )
|
|
|
|
{
|
|
|
|
const VECTOR2I delta = ptB - ptA;
|
|
|
|
*aMTV = delta.Resize( min_dist - std::sqrt( dist_sq ) + 3 );
|
|
|
|
}
|
|
|
|
|
2025-01-13 18:29:30 -08:00
|
|
|
return true;
|
2021-07-25 20:46:00 +01:00
|
|
|
}
|
2021-06-03 16:50:25 +01:00
|
|
|
|
2025-01-13 18:29:30 -08:00
|
|
|
return false;
|
2018-05-16 13:06:44 -07:00
|
|
|
}
|
2015-07-02 16:09:43 +02:00
|
|
|
|
2020-12-12 12:36:09 +00:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
template<class T_a, class T_b>
|
|
|
|
inline bool CollCase( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
|
2020-09-28 23:27:33 +01:00
|
|
|
VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2020-07-02 17:06:09 +01:00
|
|
|
|
2014-05-14 11:45:01 +02:00
|
|
|
{
|
2020-07-02 17:06:09 +01:00
|
|
|
return Collide( *static_cast<const T_a*>( aA ), *static_cast<const T_b*>( aB ),
|
2020-09-28 23:27:33 +01:00
|
|
|
aClearance, aActual, aLocation, aMTV);
|
2014-05-14 11:45:01 +02:00
|
|
|
}
|
2013-10-14 13:43:57 +02:00
|
|
|
|
2020-12-12 12:36:09 +00:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
template<class T_a, class T_b>
|
|
|
|
inline bool CollCaseReversed ( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
|
2020-09-28 23:27:33 +01:00
|
|
|
VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2014-05-14 11:45:01 +02:00
|
|
|
{
|
2020-07-02 17:06:09 +01:00
|
|
|
bool rv = Collide( *static_cast<const T_b*>( aB ), *static_cast<const T_a*>( aA ),
|
2020-09-28 23:27:33 +01:00
|
|
|
aClearance, aActual, aLocation, aMTV);
|
2020-07-02 17:06:09 +01:00
|
|
|
|
|
|
|
if( rv && aMTV)
|
|
|
|
*aMTV = - *aMTV;
|
|
|
|
|
2014-11-14 19:17:52 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
static bool collideSingleShapes( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
|
|
|
|
VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2015-01-16 09:10:23 +01:00
|
|
|
{
|
2023-06-19 18:05:24 +01:00
|
|
|
if( aA->Type() == SH_POLY_SET )
|
|
|
|
{
|
|
|
|
const SHAPE_POLY_SET* polySetA = static_cast<const SHAPE_POLY_SET*>( aA );
|
|
|
|
|
|
|
|
wxASSERT( !aMTV );
|
|
|
|
return polySetA->Collide( aB, aClearance, aActual, aLocation );
|
|
|
|
}
|
|
|
|
else if( aB->Type() == SH_POLY_SET )
|
|
|
|
{
|
|
|
|
const SHAPE_POLY_SET* polySetB = static_cast<const SHAPE_POLY_SET*>( aB );
|
|
|
|
|
|
|
|
wxASSERT( !aMTV );
|
|
|
|
return polySetB->Collide( aA, aClearance, aActual, aLocation );
|
|
|
|
}
|
|
|
|
|
2015-01-16 09:10:23 +01:00
|
|
|
switch( aA->Type() )
|
|
|
|
{
|
2021-02-25 17:18:13 +01:00
|
|
|
case SH_NULL:
|
2020-09-01 22:05:18 +02:00
|
|
|
return false;
|
2020-09-05 01:08:18 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_RECT:
|
|
|
|
switch( aB->Type() )
|
|
|
|
{
|
2015-01-16 09:10:23 +01:00
|
|
|
case SH_RECT:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_RECT, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2015-07-07 19:49:03 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_CIRCLE:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_RECT, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2015-01-16 09:10:23 +01:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_LINE_CHAIN:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2015-01-16 09:10:23 +01:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_SEGMENT:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_RECT, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2015-01-16 09:10:23 +01:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_SIMPLE:
|
2020-09-05 01:08:18 +02:00
|
|
|
case SH_POLY_SET_TRIANGLE:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2015-07-02 16:09:43 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_ARC:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCaseReversed<SHAPE_RECT, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2018-05-16 13:06:44 -07:00
|
|
|
|
2020-09-01 22:05:18 +02:00
|
|
|
case SH_NULL:
|
|
|
|
return false;
|
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
default:
|
2015-07-07 18:37:03 +02:00
|
|
|
break;
|
2020-07-02 17:06:09 +01:00
|
|
|
}
|
|
|
|
break;
|
2015-01-16 09:10:23 +01:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_CIRCLE:
|
|
|
|
switch( aB->Type() )
|
|
|
|
{
|
|
|
|
case SH_RECT:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCaseReversed<SHAPE_CIRCLE, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_CIRCLE:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_CIRCLE, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2015-01-16 09:10:23 +01:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_LINE_CHAIN:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_SEGMENT:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_CIRCLE, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2015-01-16 09:10:23 +01:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_SIMPLE:
|
2020-09-05 01:08:18 +02:00
|
|
|
case SH_POLY_SET_TRIANGLE:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2015-07-02 16:09:43 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_ARC:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCaseReversed<SHAPE_CIRCLE, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2021-02-25 17:18:13 +01:00
|
|
|
|
2020-09-01 22:05:18 +02:00
|
|
|
case SH_NULL:
|
|
|
|
return false;
|
2018-05-16 13:06:44 -07:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
default:
|
2015-07-07 18:37:03 +02:00
|
|
|
break;
|
2020-07-02 17:06:09 +01:00
|
|
|
}
|
|
|
|
break;
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_LINE_CHAIN:
|
|
|
|
switch( aB->Type() )
|
|
|
|
{
|
|
|
|
case SH_RECT:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN>( aB, aA, aClearance, aActual, aLocation, aMTV );
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_CIRCLE:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN>( aB, aA, aClearance, aActual, aLocation, aMTV );
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_LINE_CHAIN:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_SEGMENT:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_LINE_CHAIN, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_SIMPLE:
|
2020-09-05 01:08:18 +02:00
|
|
|
case SH_POLY_SET_TRIANGLE:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2015-07-02 16:09:43 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_ARC:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCaseReversed<SHAPE_LINE_CHAIN, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2018-05-16 13:06:44 -07:00
|
|
|
|
2020-09-01 22:05:18 +02:00
|
|
|
case SH_NULL:
|
|
|
|
return false;
|
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
default:
|
2015-07-07 18:37:03 +02:00
|
|
|
break;
|
2020-07-02 17:06:09 +01:00
|
|
|
}
|
|
|
|
break;
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_SEGMENT:
|
|
|
|
switch( aB->Type() )
|
|
|
|
{
|
|
|
|
case SH_RECT:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_RECT, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_CIRCLE:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCaseReversed<SHAPE_SEGMENT, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_LINE_CHAIN:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_LINE_CHAIN, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_SEGMENT:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_SEGMENT, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_SIMPLE:
|
2020-09-05 01:08:18 +02:00
|
|
|
case SH_POLY_SET_TRIANGLE:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_SEGMENT>( aB, aA, aClearance, aActual, aLocation, aMTV );
|
2015-07-02 16:09:43 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_ARC:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCaseReversed<SHAPE_SEGMENT, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2018-05-16 13:06:44 -07:00
|
|
|
|
2020-09-01 22:05:18 +02:00
|
|
|
case SH_NULL:
|
|
|
|
return false;
|
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
default:
|
2015-07-07 18:37:03 +02:00
|
|
|
break;
|
2020-07-02 17:06:09 +01:00
|
|
|
}
|
|
|
|
break;
|
2015-07-02 16:09:43 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_SIMPLE:
|
2020-09-05 01:08:18 +02:00
|
|
|
case SH_POLY_SET_TRIANGLE:
|
2020-07-02 17:06:09 +01:00
|
|
|
switch( aB->Type() )
|
|
|
|
{
|
|
|
|
case SH_RECT:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_RECT, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
|
2015-07-02 16:09:43 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_CIRCLE:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_CIRCLE, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
|
2015-07-02 16:09:43 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_LINE_CHAIN:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_LINE_CHAIN, SHAPE_LINE_CHAIN_BASE>( aB, aA, aClearance, aActual, aLocation, aMTV );
|
2015-07-02 16:09:43 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_SEGMENT:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2015-07-02 16:09:43 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_SIMPLE:
|
2020-09-05 01:08:18 +02:00
|
|
|
case SH_POLY_SET_TRIANGLE:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_LINE_CHAIN_BASE, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2015-07-02 16:09:43 +02:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_ARC:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCaseReversed<SHAPE_LINE_CHAIN_BASE, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2018-05-16 13:06:44 -07:00
|
|
|
|
2020-09-01 22:05:18 +02:00
|
|
|
case SH_NULL:
|
|
|
|
return false;
|
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
default:
|
2018-05-16 13:06:44 -07:00
|
|
|
break;
|
2020-07-02 17:06:09 +01:00
|
|
|
}
|
|
|
|
break;
|
2018-05-16 13:06:44 -07:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_ARC:
|
|
|
|
switch( aB->Type() )
|
|
|
|
{
|
|
|
|
case SH_RECT:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_ARC, SHAPE_RECT>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2018-05-16 13:06:44 -07:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_CIRCLE:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_ARC, SHAPE_CIRCLE>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2018-05-16 13:06:44 -07:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_LINE_CHAIN:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_ARC, SHAPE_LINE_CHAIN>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2018-05-16 13:06:44 -07:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_SEGMENT:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_ARC, SHAPE_SEGMENT>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2018-05-16 13:06:44 -07:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_SIMPLE:
|
2020-09-05 01:08:18 +02:00
|
|
|
case SH_POLY_SET_TRIANGLE:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_ARC, SHAPE_LINE_CHAIN_BASE>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2018-05-16 13:06:44 -07:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
case SH_ARC:
|
2020-09-28 23:27:33 +01:00
|
|
|
return CollCase<SHAPE_ARC, SHAPE_ARC>( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2020-09-01 22:05:18 +02:00
|
|
|
case SH_NULL:
|
|
|
|
return false;
|
|
|
|
|
2015-01-16 09:10:23 +01:00
|
|
|
default:
|
|
|
|
break;
|
2020-07-02 17:06:09 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2015-01-16 09:10:23 +01:00
|
|
|
}
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2022-02-09 09:01:30 -05:00
|
|
|
wxFAIL_MSG( wxString::Format( wxT( "Unsupported collision: %s with %s" ),
|
2020-12-12 12:22:52 +00:00
|
|
|
SHAPE_TYPE_asString( aA->Type() ),
|
|
|
|
SHAPE_TYPE_asString( aB->Type() ) ) );
|
2014-05-14 11:45:01 +02:00
|
|
|
|
2015-01-16 09:10:23 +01:00
|
|
|
return false;
|
2013-09-10 13:43:09 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
static bool collideShapes( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual,
|
|
|
|
VECTOR2I* aLocation, VECTOR2I* aMTV )
|
2020-07-21 11:42:18 +02:00
|
|
|
{
|
|
|
|
int currentActual = std::numeric_limits<int>::max();
|
2020-09-28 23:27:33 +01:00
|
|
|
VECTOR2I currentLocation;
|
2020-07-22 22:48:50 +02:00
|
|
|
VECTOR2I currentMTV(0, 0);
|
2020-07-21 11:42:18 +02:00
|
|
|
bool colliding = false;
|
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
auto canExit =
|
|
|
|
[&]()
|
|
|
|
{
|
|
|
|
if( !colliding )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( aActual && currentActual > 0 )
|
|
|
|
return false;
|
2020-07-21 11:42:18 +02:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
if( aMTV )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto collideCompoundSubshapes =
|
|
|
|
[&]( const SHAPE* elemA, const SHAPE* elemB, int clearance ) -> bool
|
|
|
|
{
|
|
|
|
int actual = 0;
|
|
|
|
VECTOR2I location;
|
2020-07-22 22:48:50 +02:00
|
|
|
VECTOR2I mtv;
|
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
if( collideSingleShapes( elemA, elemB, clearance,
|
|
|
|
aActual || aLocation ? &actual : nullptr,
|
|
|
|
aLocation ? &location : nullptr,
|
|
|
|
aMTV ? &mtv : nullptr ) )
|
2020-07-21 11:42:18 +02:00
|
|
|
{
|
2020-09-28 23:27:33 +01:00
|
|
|
if( actual < currentActual )
|
2020-07-21 11:42:18 +02:00
|
|
|
{
|
2020-09-28 23:27:33 +01:00
|
|
|
currentActual = actual;
|
|
|
|
currentLocation = location;
|
2020-07-21 11:42:18 +02:00
|
|
|
}
|
2020-09-28 23:27:33 +01:00
|
|
|
|
|
|
|
if( aMTV && mtv.SquaredEuclideanNorm() > currentMTV.SquaredEuclideanNorm() )
|
2020-07-21 11:42:18 +02:00
|
|
|
{
|
2020-09-28 23:27:33 +01:00
|
|
|
currentMTV = mtv;
|
2020-07-21 11:42:18 +02:00
|
|
|
}
|
2020-09-28 23:27:33 +01:00
|
|
|
|
|
|
|
return true;
|
2020-07-21 11:42:18 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
return false;
|
|
|
|
};
|
2020-07-21 11:42:18 +02:00
|
|
|
|
2020-09-19 12:47:43 +01:00
|
|
|
if( aA->Type() == SH_COMPOUND && aB->Type() == SH_COMPOUND )
|
2020-07-21 11:42:18 +02:00
|
|
|
{
|
2020-09-28 23:27:33 +01:00
|
|
|
const SHAPE_COMPOUND* cmpA = static_cast<const SHAPE_COMPOUND*>( aA );
|
|
|
|
const SHAPE_COMPOUND* cmpB = static_cast<const SHAPE_COMPOUND*>( aB );
|
2020-07-22 22:48:50 +02:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
for( const SHAPE* elemA : cmpA->Shapes() )
|
2020-07-21 11:42:18 +02:00
|
|
|
{
|
2020-09-28 23:27:33 +01:00
|
|
|
for( const SHAPE* elemB : cmpB->Shapes() )
|
2020-07-21 11:42:18 +02:00
|
|
|
{
|
2020-07-22 22:48:50 +02:00
|
|
|
if( collideCompoundSubshapes( elemA, elemB, aClearance ) )
|
2020-07-21 11:42:18 +02:00
|
|
|
{
|
|
|
|
colliding = true;
|
2020-09-28 23:27:33 +01:00
|
|
|
|
|
|
|
if( canExit() )
|
2020-07-22 22:48:50 +02:00
|
|
|
break;
|
2020-07-21 11:42:18 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 23:27:33 +01:00
|
|
|
|
|
|
|
if( canExit() )
|
2020-07-22 22:48:50 +02:00
|
|
|
break;
|
2020-07-21 11:42:18 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-19 12:47:43 +01:00
|
|
|
else if( aA->Type() == SH_COMPOUND )
|
2020-07-21 11:42:18 +02:00
|
|
|
{
|
2020-09-28 23:27:33 +01:00
|
|
|
const SHAPE_COMPOUND* cmpA = static_cast<const SHAPE_COMPOUND*>( aA );
|
|
|
|
|
|
|
|
for( const SHAPE* elemA : cmpA->Shapes() )
|
2020-07-21 11:42:18 +02:00
|
|
|
{
|
2020-07-22 22:48:50 +02:00
|
|
|
if( collideCompoundSubshapes( elemA, aB, aClearance ) )
|
2020-07-21 11:42:18 +02:00
|
|
|
{
|
|
|
|
colliding = true;
|
2020-09-28 23:27:33 +01:00
|
|
|
|
|
|
|
if( canExit() )
|
2020-07-22 22:48:50 +02:00
|
|
|
break;
|
2020-07-21 11:42:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-28 23:27:33 +01:00
|
|
|
else if( aB->Type() == SH_COMPOUND )
|
2020-07-21 11:42:18 +02:00
|
|
|
{
|
2020-09-28 23:27:33 +01:00
|
|
|
const SHAPE_COMPOUND* cmpB = static_cast<const SHAPE_COMPOUND*>( aB );
|
|
|
|
|
|
|
|
for( const SHAPE* elemB : cmpB->Shapes() )
|
2020-07-21 11:42:18 +02:00
|
|
|
{
|
2020-07-22 22:48:50 +02:00
|
|
|
if( collideCompoundSubshapes( aA, elemB, aClearance ) )
|
2020-07-21 11:42:18 +02:00
|
|
|
{
|
|
|
|
colliding = true;
|
2020-09-28 23:27:33 +01:00
|
|
|
|
|
|
|
if( canExit() )
|
2020-07-22 22:48:50 +02:00
|
|
|
break;
|
2020-07-21 11:42:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-28 23:27:33 +01:00
|
|
|
return collideSingleShapes( aA, aB, aClearance, aActual, aLocation, aMTV );
|
2020-07-21 11:42:18 +02:00
|
|
|
}
|
2021-02-25 17:18:13 +01:00
|
|
|
|
2020-07-22 22:48:50 +02:00
|
|
|
if( colliding )
|
|
|
|
{
|
2020-09-28 23:27:33 +01:00
|
|
|
if( aLocation )
|
|
|
|
*aLocation = currentLocation;
|
|
|
|
|
2020-07-22 22:48:50 +02:00
|
|
|
if( aActual )
|
|
|
|
*aActual = currentActual;
|
2020-09-28 23:27:33 +01:00
|
|
|
|
2020-07-22 22:48:50 +02:00
|
|
|
if( aMTV )
|
|
|
|
*aMTV = currentMTV;
|
|
|
|
}
|
2020-07-21 11:42:18 +02:00
|
|
|
|
2020-07-22 22:48:50 +02:00
|
|
|
return colliding;
|
2020-07-21 11:42:18 +02:00
|
|
|
}
|
2013-09-10 13:43:09 +02:00
|
|
|
|
2020-12-12 12:36:09 +00:00
|
|
|
|
2020-07-02 17:06:09 +01:00
|
|
|
bool SHAPE::Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const
|
2013-09-10 13:43:09 +02:00
|
|
|
{
|
2020-09-28 23:27:33 +01:00
|
|
|
return collideShapes( this, aShape, aClearance, nullptr, nullptr, aMTV );
|
2013-09-10 13:43:09 +02:00
|
|
|
}
|
|
|
|
|
2013-09-27 16:23:43 +02:00
|
|
|
|
2020-09-28 23:27:33 +01:00
|
|
|
bool SHAPE::Collide( const SHAPE* aShape, int aClearance, int* aActual, VECTOR2I* aLocation ) const
|
2013-09-10 13:43:09 +02:00
|
|
|
{
|
2020-09-28 23:27:33 +01:00
|
|
|
return collideShapes( this, aShape, aClearance, aActual, aLocation, nullptr );
|
2013-09-10 13:43:09 +02:00
|
|
|
}
|
2015-06-12 17:11:50 +02:00
|
|
|
|
|
|
|
|