From f08b7f098f9112f74c0459af3d81d5bb48408b0c Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Sat, 12 Dec 2020 12:36:09 +0000 Subject: [PATCH] Cleanup. --- libs/kimath/src/geometry/shape_collisions.cpp | 71 ++++++++++++------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/libs/kimath/src/geometry/shape_collisions.cpp b/libs/kimath/src/geometry/shape_collisions.cpp index 1341db01be..4e94675d5c 100644 --- a/libs/kimath/src/geometry/shape_collisions.cpp +++ b/libs/kimath/src/geometry/shape_collisions.cpp @@ -2,6 +2,7 @@ * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2013 CERN + * Copyright (C) 2015-2020 KiCad Developers, see AUTHORS.txt for contributors. * @author Tomasz Wlostowski * * This program is free software; you can redistribute it and/or @@ -22,8 +23,6 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ - -#include // for assert #include #include // for INT_MAX @@ -39,6 +38,7 @@ typedef VECTOR2I::extended_type ecoord; + static inline bool Collide( const SHAPE_CIRCLE& aA, const SHAPE_CIRCLE& aB, int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV ) { @@ -72,9 +72,9 @@ static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_CIRCLE& aB, int aC const VECTOR2I c = aB.GetCenter(); const VECTOR2I p0 = aA.GetPosition(); const VECTOR2I size = aA.GetSize(); - const int r = aB.GetRadius(); - const int min_dist = aClearance + r; - const ecoord min_dist_sq = SEG::Square( min_dist ); + const int r = aB.GetRadius(); + const int min_dist = aClearance + r; + const ecoord min_dist_sq = SEG::Square( min_dist ); const VECTOR2I vts[] = { @@ -85,7 +85,7 @@ static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_CIRCLE& aB, int aC VECTOR2I( p0.x, p0.y ) }; - ecoord nearest_side_dist_sq = VECTOR2I::ECOORD_MAX; + ecoord nearest_side_dist_sq = VECTOR2I::ECOORD_MAX; VECTOR2I nearest; bool inside = c.x >= p0.x && c.x <= ( p0.x + size.x ) @@ -260,7 +260,9 @@ static inline bool Collide( const SHAPE_CIRCLE& aA, const SHAPE_SEGMENT& aSeg, i static inline bool Collide( const SHAPE_LINE_CHAIN_BASE& aA, const SHAPE_LINE_CHAIN_BASE& aB, int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV ) { - // TODO: why doesn't this handle MTV? + wxASSERT_MSG( !aMTV, wxString::Format( "MTV not implemented for %s : %s collisions", + aA.Type(), + aB.Type() ) ); int closest_dist = INT_MAX; VECTOR2I nearest; @@ -315,7 +317,9 @@ static inline bool Collide( const SHAPE_LINE_CHAIN_BASE& aA, const SHAPE_LINE_CH static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_LINE_CHAIN_BASE& aB, int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV ) { - // TODO: why doesn't this handle MTV? + wxASSERT_MSG( !aMTV, wxString::Format( "MTV not implemented for %s : %s collisions", + aA.Type(), + aB.Type() ) ); int closest_dist = INT_MAX; VECTOR2I nearest; @@ -367,15 +371,17 @@ static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_LINE_CHAIN_BASE& a } -static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_SEGMENT& aSeg, int aClearance, +static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_SEGMENT& aB, int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV ) { - if( aA.Collide( aSeg.GetSeg(), aClearance + aSeg.GetWidth() / 2, aActual, aLocation ) ) + wxASSERT_MSG( !aMTV, wxString::Format( "MTV not implemented for %s : %s collisions", + aA.Type(), + aB.Type() ) ); + + if( aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation ) ) { if( aActual ) - *aActual = std::max( 0, *aActual - aSeg.GetWidth() / 2 ); - - // TODO: why doesn't this handle MTV? + *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 ); return true; } @@ -387,13 +393,15 @@ static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_SEGMENT& aSeg, int static inline bool Collide( const SHAPE_SEGMENT& aA, const SHAPE_SEGMENT& aB, int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV ) { + wxASSERT_MSG( !aMTV, wxString::Format( "MTV not implemented for %s : %s collisions", + aA.Type(), + aB.Type() ) ); + if( aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation ) ) { if( aActual ) *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 ); - // TODO: why doesn't this handle MTV? - return true; } @@ -404,13 +412,15 @@ static inline bool Collide( const SHAPE_SEGMENT& aA, const SHAPE_SEGMENT& aB, in static inline bool Collide( const SHAPE_LINE_CHAIN_BASE& aA, const SHAPE_SEGMENT& aB, int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV ) { + wxASSERT_MSG( !aMTV, wxString::Format( "MTV not implemented for %s : %s collisions", + aA.Type(), + aB.Type() ) ); + if( aA.Collide( aB.GetSeg(), aClearance + aB.GetWidth() / 2, aActual, aLocation ) ) { if( aActual ) *aActual = std::max( 0, *aActual - aB.GetWidth() / 2 ); - // TODO: why doesn't this handle MTV? - return true; } @@ -424,17 +434,20 @@ static inline bool Collide( const SHAPE_RECT& aA, const SHAPE_RECT& aB, int aCle return Collide( aA.Outline(), aB.Outline(), aClearance, aActual, aLocation, aMTV ); } + static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_RECT& aB, int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV ) { - const auto lc = aA.ConvertToPolyline(); + const SHAPE_LINE_CHAIN lc = aA.ConvertToPolyline(); + return Collide( lc, aB.Outline(), aClearance, aActual, aLocation, aMTV ); } + static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_CIRCLE& aB, int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV ) { - const auto lc = aA.ConvertToPolyline(); + const SHAPE_LINE_CHAIN lc = aA.ConvertToPolyline(); bool rv = Collide( aB, lc, aClearance, aActual, aLocation, aMTV ); if( rv && aMTV ) @@ -443,36 +456,44 @@ static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_CIRCLE& aB, int aCl return rv; } + static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_LINE_CHAIN& aB, int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV ) { - const auto lc = aA.ConvertToPolyline(); + const SHAPE_LINE_CHAIN lc = aA.ConvertToPolyline(); + return Collide( lc, aB, aClearance, aActual, aLocation, aMTV ); } + static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_SEGMENT& aB, int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV ) { - const auto lc = aA.ConvertToPolyline(); + const SHAPE_LINE_CHAIN lc = aA.ConvertToPolyline(); + return Collide( lc, aB, aClearance, aActual, aLocation, aMTV ); } + static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_LINE_CHAIN_BASE& aB, int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV ) { - const auto lc = aA.ConvertToPolyline(); + const SHAPE_LINE_CHAIN lc = aA.ConvertToPolyline(); return Collide( lc, aB, aClearance, aActual, aLocation, aMTV ); } + static inline bool Collide( const SHAPE_ARC& aA, const SHAPE_ARC& aB, int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV ) { - const auto lcA = aA.ConvertToPolyline(); - const auto lcB = aB.ConvertToPolyline(); + const SHAPE_LINE_CHAIN lcA = aA.ConvertToPolyline(); + const SHAPE_LINE_CHAIN lcB = aB.ConvertToPolyline(); + return Collide( lcA, lcB, aClearance, aActual, aLocation, aMTV ); } + template inline bool CollCase( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV ) @@ -482,6 +503,7 @@ inline bool CollCase( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aAc aClearance, aActual, aLocation, aMTV); } + template inline bool CollCaseReversed ( const SHAPE* aA, const SHAPE* aB, int aClearance, int* aActual, VECTOR2I* aLocation, VECTOR2I* aMTV ) @@ -820,6 +842,7 @@ static bool collideShapes( const SHAPE* aA, const SHAPE* aB, int aClearance, int return colliding; } + bool SHAPE::Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const { return collideShapes( this, aShape, aClearance, nullptr, nullptr, aMTV );