2013-09-10 11:43:09 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
2022-01-20 20:54:22 +00:00
|
|
|
* Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-09-10 11:43:09 +00: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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __SHAPE_H
|
|
|
|
#define __SHAPE_H
|
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
#include <sstream>
|
2020-09-18 14:22:30 +00:00
|
|
|
#include <vector>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <geometry/seg.h>
|
2022-01-20 20:54:22 +00:00
|
|
|
#include <geometry/eda_angle.h>
|
2013-09-10 11:43:09 +00:00
|
|
|
#include <math/vector2d.h>
|
|
|
|
#include <math/box2.h>
|
2023-02-19 03:40:07 +00:00
|
|
|
#include <wx/string.h>
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2020-09-04 23:08:18 +00:00
|
|
|
class SHAPE_LINE_CHAIN;
|
2013-09-10 11:43:09 +00:00
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Lists all supported shapes.
|
2013-09-10 11:43:09 +00:00
|
|
|
*/
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
enum SHAPE_TYPE
|
2013-10-14 14:13:35 +00:00
|
|
|
{
|
2021-01-25 12:42:36 +00:00
|
|
|
SH_RECT = 0, ///< axis-aligned rectangle
|
|
|
|
SH_SEGMENT, ///< line segment
|
|
|
|
SH_LINE_CHAIN, ///< line chain (polyline)
|
|
|
|
SH_CIRCLE, ///< circle
|
|
|
|
SH_SIMPLE, ///< simple polygon
|
|
|
|
SH_POLY_SET, ///< set of polygons (with holes, etc.)
|
|
|
|
SH_COMPOUND, ///< compound shape, consisting of multiple simple shapes
|
|
|
|
SH_ARC, ///< circular arc
|
|
|
|
SH_NULL, ///< empty shape (no shape...),
|
|
|
|
SH_POLY_SET_TRIANGLE ///< a single triangle belonging to a POLY_SET triangulation
|
2013-09-10 11:43:09 +00:00
|
|
|
};
|
|
|
|
|
2020-06-30 12:41:59 +00:00
|
|
|
static inline wxString SHAPE_TYPE_asString( SHAPE_TYPE a )
|
|
|
|
{
|
|
|
|
switch( a )
|
|
|
|
{
|
2022-02-09 14:01:30 +00:00
|
|
|
case SH_RECT: return wxT( "SH_RECT" );
|
|
|
|
case SH_SEGMENT: return wxT( "SH_SEGMENT" );
|
|
|
|
case SH_LINE_CHAIN: return wxT( "SH_LINE_CHAIN" );
|
|
|
|
case SH_CIRCLE: return wxT( "SH_CIRCLE" );
|
|
|
|
case SH_SIMPLE: return wxT( "SH_SIMPLE" );
|
|
|
|
case SH_POLY_SET: return wxT( "SH_POLY_SET" );
|
|
|
|
case SH_COMPOUND: return wxT( "SH_COMPOUND" );
|
|
|
|
case SH_ARC: return wxT( "SH_ARC" );
|
|
|
|
case SH_NULL: return wxT( "SH_NULL" );
|
|
|
|
case SH_POLY_SET_TRIANGLE: return wxT( "SH_POLY_SET_TRIANGLE" );
|
2020-06-30 12:41:59 +00:00
|
|
|
}
|
2020-06-30 18:15:11 +00:00
|
|
|
|
|
|
|
return wxEmptyString; // Just to quiet GCC.
|
2020-06-30 12:41:59 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 14:22:30 +00:00
|
|
|
class SHAPE;
|
2020-06-30 12:41:59 +00:00
|
|
|
|
2020-09-04 23:08:18 +00:00
|
|
|
class SHAPE_BASE
|
2013-10-14 14:13:35 +00:00
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
public:
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Create an empty shape of type aType
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
2020-12-12 03:43:16 +00:00
|
|
|
SHAPE_BASE( SHAPE_TYPE aType ) :
|
|
|
|
m_type( aType )
|
2013-10-14 18:40:36 +00:00
|
|
|
{}
|
|
|
|
|
2020-09-04 23:08:18 +00:00
|
|
|
virtual ~SHAPE_BASE()
|
2013-10-14 18:40:36 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Return the type of the shape.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
|
|
|
* @retval the type
|
|
|
|
*/
|
|
|
|
SHAPE_TYPE Type() const
|
|
|
|
{
|
|
|
|
return m_type;
|
|
|
|
}
|
|
|
|
|
2022-07-28 16:36:31 +00:00
|
|
|
wxString TypeName() const
|
|
|
|
{
|
|
|
|
return SHAPE_TYPE_asString( m_type );
|
|
|
|
}
|
|
|
|
|
2020-09-04 23:08:18 +00:00
|
|
|
virtual bool HasIndexableSubshapes() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-18 14:22:30 +00:00
|
|
|
virtual size_t GetIndexableSubshapeCount() const { return 0; }
|
2020-09-04 23:08:18 +00:00
|
|
|
|
2022-07-21 22:01:50 +00:00
|
|
|
virtual void GetIndexableSubshapes( std::vector<const SHAPE*>& aSubshapes ) const { }
|
2020-09-04 23:08:18 +00:00
|
|
|
|
|
|
|
protected:
|
2021-01-25 12:42:36 +00:00
|
|
|
///< type of our shape
|
2020-09-04 23:08:18 +00:00
|
|
|
SHAPE_TYPE m_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* An abstract shape on 2D plane.
|
2020-09-04 23:08:18 +00:00
|
|
|
*/
|
|
|
|
class SHAPE : public SHAPE_BASE
|
|
|
|
{
|
|
|
|
public:
|
2021-01-06 01:35:52 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* This is the minimum precision for all the points in a shape.
|
2021-01-06 01:35:52 +00:00
|
|
|
*/
|
|
|
|
static const int MIN_PRECISION_IU = 4;
|
|
|
|
|
2020-09-04 23:08:18 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Create an empty shape of type \a aType.
|
2020-09-04 23:08:18 +00:00
|
|
|
*/
|
2020-12-12 03:43:16 +00:00
|
|
|
SHAPE( SHAPE_TYPE aType ) :
|
|
|
|
SHAPE_BASE( aType )
|
2020-09-04 23:08:18 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~SHAPE()
|
|
|
|
{}
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Return a dynamically allocated copy of the shape.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
|
|
|
* @retval copy of the shape
|
|
|
|
*/
|
|
|
|
virtual SHAPE* Clone() const
|
|
|
|
{
|
|
|
|
assert( false );
|
2021-07-18 14:06:48 +00:00
|
|
|
return nullptr;
|
2013-10-14 18:40:36 +00:00
|
|
|
};
|
|
|
|
|
2022-07-21 22:01:50 +00:00
|
|
|
/**
|
|
|
|
* Return the actual minimum distance between two shapes
|
|
|
|
*
|
|
|
|
* @retval distance in IU
|
|
|
|
*/
|
|
|
|
int GetClearance( const SHAPE* aOther ) const;
|
|
|
|
|
2020-08-26 21:59:17 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Return true if the shape is a null shape.
|
2020-08-26 21:59:17 +00:00
|
|
|
*
|
|
|
|
* @retval true if null :-)
|
|
|
|
*/
|
|
|
|
bool IsNull() const
|
|
|
|
{
|
|
|
|
return m_type == SH_NULL;
|
|
|
|
}
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Check if the boundary of shape (this) lies closer to the point \a aP than \a aClearance,
|
2013-10-14 18:40:36 +00:00
|
|
|
* indicating a collision.
|
2021-01-25 12:42:36 +00:00
|
|
|
*
|
2020-09-28 22:27:33 +00:00
|
|
|
* @param aActual [out] an optional pointer to an int to store the actual distance in the
|
|
|
|
* event of a collision.
|
|
|
|
* @param aLocation [out] an option pointer to a point to store a nearby location in the
|
|
|
|
* event of a collision.
|
2013-10-14 18:40:36 +00:00
|
|
|
* @return true, if there is a collision.
|
|
|
|
*/
|
2020-12-12 03:43:16 +00:00
|
|
|
virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
|
|
|
|
VECTOR2I* aLocation = nullptr ) const
|
2013-10-14 18:40:36 +00:00
|
|
|
{
|
2020-09-28 22:27:33 +00:00
|
|
|
return Collide( SEG( aP, aP ), aClearance, aActual, aLocation );
|
2013-10-14 18:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Check if the boundary of shape (this) lies closer to the shape \a aShape than \a aClearance,
|
2013-10-14 18:40:36 +00:00
|
|
|
* indicating a collision.
|
2021-01-25 12:42:36 +00:00
|
|
|
*
|
2013-10-14 18:40:36 +00:00
|
|
|
* @param aShape shape to check collision against
|
|
|
|
* @param aClearance minimum clearance
|
|
|
|
* @param aMTV minimum translation vector
|
2020-09-28 22:27:33 +00:00
|
|
|
* @param aActual [out] an optional pointer to an int to store the actual distance in the
|
|
|
|
* event of a collision.
|
|
|
|
* @param aLocation [out] an option pointer to a point to store a nearby location in the
|
|
|
|
* event of a collision.
|
2013-10-14 18:40:36 +00:00
|
|
|
* @return true, if there is a collision.
|
|
|
|
*/
|
2020-07-02 16:06:09 +00:00
|
|
|
virtual bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const;
|
2020-09-28 22:27:33 +00:00
|
|
|
|
|
|
|
virtual bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
|
|
|
|
VECTOR2I* aLocation = nullptr ) const;
|
2013-10-14 18:40:36 +00:00
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Check if the boundary of shape (this) lies closer to the segment \a aSeg than \a aClearance,
|
2013-10-14 18:40:36 +00:00
|
|
|
* indicating a collision.
|
2021-01-25 12:42:36 +00:00
|
|
|
*
|
2020-09-28 22:27:33 +00:00
|
|
|
* @param aActual [out] an optional pointer to an int to be updated with the actual distance
|
|
|
|
* int the event of a collision.
|
|
|
|
* @param aLocation [out] an option pointer to a point to store a nearby location in the
|
|
|
|
* event of a collision.
|
2013-10-14 18:40:36 +00:00
|
|
|
* @return true, if there is a collision.
|
|
|
|
*/
|
2020-09-28 22:27:33 +00:00
|
|
|
virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
|
|
|
|
VECTOR2I* aLocation = nullptr ) const = 0;
|
2013-10-14 18:40:36 +00:00
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Compute a bounding box of the shape, with a margin of \a aClearance a collision.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-01-25 12:42:36 +00:00
|
|
|
* @param aClearance how much the bounding box is expanded wrs to the minimum enclosing
|
|
|
|
* rectangle for the shape.
|
2013-10-14 18:40:36 +00:00
|
|
|
* @return the bounding box.
|
|
|
|
*/
|
|
|
|
virtual const BOX2I BBox( int aClearance = 0 ) const = 0;
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Compute a center-of-mass of the shape.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
|
|
|
* @return the center-of-mass point
|
|
|
|
*/
|
|
|
|
virtual VECTOR2I Centre() const
|
|
|
|
{
|
|
|
|
return BBox( 0 ).Centre(); // if nothing better is available....
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:06:09 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* @param aCenter is the rotation center.
|
2022-01-20 20:54:22 +00:00
|
|
|
* @param aAngle rotation angle.
|
2020-07-02 16:06:09 +00:00
|
|
|
*/
|
2022-01-20 20:54:22 +00:00
|
|
|
virtual void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) = 0;
|
2020-07-02 16:06:09 +00:00
|
|
|
|
|
|
|
virtual void Move( const VECTOR2I& aVector ) = 0;
|
2014-11-14 18:18:31 +00:00
|
|
|
|
|
|
|
virtual bool IsSolid() const = 0;
|
2014-11-14 19:19:00 +00:00
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
virtual bool Parse( std::stringstream& aStream );
|
|
|
|
|
2022-10-23 22:20:02 +00:00
|
|
|
virtual const std::string Format( bool aCplusPlus = true ) const;
|
2020-12-12 03:43:16 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
typedef VECTOR2I::extended_type ecoord;
|
2013-09-10 11:43:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-09-04 23:08:18 +00:00
|
|
|
class SHAPE_LINE_CHAIN_BASE : public SHAPE
|
|
|
|
{
|
|
|
|
public:
|
2020-12-12 03:43:16 +00:00
|
|
|
SHAPE_LINE_CHAIN_BASE( SHAPE_TYPE aType ) :
|
|
|
|
SHAPE( aType )
|
2020-09-04 23:08:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~SHAPE_LINE_CHAIN_BASE()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Check if point \a aP lies closer to us than \a aClearance.
|
2020-09-04 23:08:18 +00:00
|
|
|
*
|
|
|
|
* @param aP the point to check for collisions with
|
|
|
|
* @param aClearance minimum distance that does not qualify as a collision.
|
|
|
|
* @param aActual an optional pointer to an int to store the actual distance in the event
|
|
|
|
* of a collision.
|
|
|
|
* @return true, when a collision has been found
|
|
|
|
*/
|
2020-12-12 03:43:16 +00:00
|
|
|
virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
|
|
|
|
VECTOR2I* aLocation = nullptr ) const override;
|
2020-09-04 23:08:18 +00:00
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Check if segment \a aSeg lies closer to us than \a aClearance.
|
2020-09-04 23:08:18 +00:00
|
|
|
*
|
|
|
|
* @param aSeg the segment to check for collisions with
|
|
|
|
* @param aClearance minimum distance that does not qualify as a collision.
|
|
|
|
* @param aActual an optional pointer to an int to store the actual distance in the event
|
|
|
|
* of a collision.
|
|
|
|
* @return true, when a collision has been found
|
|
|
|
*/
|
|
|
|
|
2020-12-12 03:43:16 +00:00
|
|
|
virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
|
|
|
|
VECTOR2I* aLocation = nullptr ) const override;
|
2020-09-28 22:27:33 +00:00
|
|
|
|
2020-09-04 23:08:18 +00:00
|
|
|
SEG::ecoord SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Check if point \a aP lies inside a polygon (any type) defined by the line chain.
|
2020-09-04 23:08:18 +00:00
|
|
|
* For closed shapes only.
|
2021-01-25 12:42:36 +00:00
|
|
|
*
|
2020-09-04 23:08:18 +00:00
|
|
|
* @param aPt point to check
|
2021-06-09 19:32:58 +00:00
|
|
|
* @param aUseBBoxCache gives better performance if the bounding box caches have been
|
2020-09-04 23:08:18 +00:00
|
|
|
* generated.
|
|
|
|
* @return true if the point is inside the shape (edge is not treated as being inside).
|
|
|
|
*/
|
|
|
|
bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const;
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Check if point \a aP lies on an edge or vertex of the line chain.
|
2020-09-04 23:08:18 +00:00
|
|
|
*
|
|
|
|
* @param aP point to check
|
|
|
|
* @return true if the point lies on the edge.
|
|
|
|
*/
|
|
|
|
bool PointOnEdge( const VECTOR2I& aP, int aAccuracy = 0 ) const;
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Check if point \a aP lies on an edge or vertex of the line chain.
|
2020-09-04 23:08:18 +00:00
|
|
|
*
|
|
|
|
* @param aP point to check
|
|
|
|
* @return index of the first edge containing the point, otherwise negative
|
|
|
|
*/
|
|
|
|
int EdgeContainingPoint( const VECTOR2I& aP, int aAccuracy = 0 ) const;
|
|
|
|
|
|
|
|
virtual const VECTOR2I GetPoint( int aIndex ) const = 0;
|
|
|
|
virtual const SEG GetSegment( int aIndex ) const = 0;
|
|
|
|
virtual size_t GetPointCount() const = 0;
|
|
|
|
virtual size_t GetSegmentCount() const = 0;
|
|
|
|
virtual bool IsClosed() const = 0;
|
2021-09-09 14:35:09 +00:00
|
|
|
|
|
|
|
virtual BOX2I* GetCachedBBox() const { return nullptr; }
|
2020-09-04 23:08:18 +00:00
|
|
|
};
|
|
|
|
|
2013-09-10 11:43:09 +00:00
|
|
|
#endif // __SHAPE_H
|