| 1 |
/* |
| 2 |
* This program source code file is part of KiCad, a free EDA CAD application. |
| 3 |
* |
| 4 |
* Copyright (C) 2013 CERN |
| 5 |
* Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors. |
| 6 |
* @author Tomasz Wlostowski <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU General Public License |
| 10 |
* as published by the Free Software Foundation; either version 2 |
| 11 |
* of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; if not, you may find one here: |
| 20 |
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 21 |
* or you may search the http://www.gnu.org website for the version 2 license, |
| 22 |
* or you may write to the Free Software Foundation, Inc., |
| 23 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifndef __SHAPE_H |
| 27 |
#define __SHAPE_H |
| 28 |
|
| 29 |
#include <sstream> |
| 30 |
#include <vector> |
| 31 |
#include <geometry/seg.h> |
| 32 |
#include <math/vector2d.h> |
| 33 |
#include <math/box2.h> |
| 34 |
|
| 35 |
class SHAPE_LINE_CHAIN; |
| 36 |
|
| 37 |
/** |
| 38 |
* Lists all supported shapes. |
| 39 |
*/ |
| 40 |
|
| 41 |
enum SHAPE_TYPE |
| 42 |
{ |
| 43 |
SH_RECT = 0, ///< axis-aligned rectangle |
| 44 |
SH_SEGMENT, ///< line segment |
| 45 |
SH_LINE_CHAIN, ///< line chain (polyline) |
| 46 |
SH_CIRCLE, ///< circle |
| 47 |
SH_SIMPLE, ///< simple polygon |
| 48 |
SH_POLY_SET, ///< set of polygons (with holes, etc.) |
| 49 |
SH_COMPOUND, ///< compound shape, consisting of multiple simple shapes |
| 50 |
SH_ARC, ///< circular arc |
| 51 |
SH_NULL, ///< empty shape (no shape...), |
| 52 |
SH_POLY_SET_TRIANGLE ///< a single triangle belonging to a POLY_SET triangulation |
| 53 |
}; |
| 54 |
|
| 55 |
static inline wxString SHAPE_TYPE_asString( SHAPE_TYPE a ) |
| 56 |
{ |
| 57 |
switch( a ) |
| 58 |
{ |
| 59 |
case SH_RECT: return wxT( "SH_RECT" ); |
| 60 |
case SH_SEGMENT: return wxT( "SH_SEGMENT" ); |
| 61 |
case SH_LINE_CHAIN: return wxT( "SH_LINE_CHAIN" ); |
| 62 |
case SH_CIRCLE: return wxT( "SH_CIRCLE" ); |
| 63 |
case SH_SIMPLE: return wxT( "SH_SIMPLE" ); |
| 64 |
case SH_POLY_SET: return wxT( "SH_POLY_SET" ); |
| 65 |
case SH_COMPOUND: return wxT( "SH_COMPOUND" ); |
| 66 |
case SH_ARC: return wxT( "SH_ARC" ); |
| 67 |
case SH_NULL: return wxT( "SH_NULL" ); |
| 68 |
case SH_POLY_SET_TRIANGLE: return wxT( "SH_POLY_SET_TRIANGLE" ); |
| 69 |
} |
| 70 |
|
| 71 |
return wxEmptyString; // Just to quiet GCC. |
| 72 |
} |
| 73 |
|
| 74 |
class SHAPE; |
| 75 |
|
| 76 |
class SHAPE_BASE |
| 77 |
{ |
| 78 |
public: |
| 79 |
/** |
| 80 |
* Create an empty shape of type aType |
| 81 |
*/ |
| 82 |
SHAPE_BASE( SHAPE_TYPE aType ) : |
| 83 |
m_type( aType ) |
| 84 |
{} |
| 85 |
|
| 86 |
virtual ~SHAPE_BASE() |
| 87 |
{} |
| 88 |
|
| 89 |
/** |
| 90 |
* Return the type of the shape. |
| 91 |
* |
| 92 |
* @retval the type |
| 93 |
*/ |
| 94 |
SHAPE_TYPE Type() const |
| 95 |
{ |
| 96 |
return m_type; |
| 97 |
} |
| 98 |
|
| 99 |
wxString TypeName() const |
| 100 |
{ |
| 101 |
return SHAPE_TYPE_asString( m_type ); |
| 102 |
} |
| 103 |
|
| 104 |
virtual bool HasIndexableSubshapes() const |
| 105 |
{ |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
virtual size_t GetIndexableSubshapeCount() const { return 0; } |
| 110 |
|
| 111 |
virtual void GetIndexableSubshapes( std::vector<SHAPE*>& aSubshapes ) { } |
| 112 |
|
| 113 |
protected: |
| 114 |
///< type of our shape |
| 115 |
SHAPE_TYPE m_type; |
| 116 |
}; |
| 117 |
|
| 118 |
/** |
| 119 |
* An abstract shape on 2D plane. |
| 120 |
*/ |
| 121 |
class SHAPE : public SHAPE_BASE |
| 122 |
{ |
| 123 |
public: |
| 124 |
/** |
| 125 |
* This is the minimum precision for all the points in a shape. |
| 126 |
*/ |
| 127 |
static const int MIN_PRECISION_IU = 4; |
| 128 |
|
| 129 |
/** |
| 130 |
* Create an empty shape of type \a aType. |
| 131 |
*/ |
| 132 |
SHAPE( SHAPE_TYPE aType ) : |
| 133 |
SHAPE_BASE( aType ) |
| 134 |
{} |
| 135 |
|
| 136 |
virtual ~SHAPE() |
| 137 |
{} |
| 138 |
|
| 139 |
/** |
| 140 |
* Return a dynamically allocated copy of the shape. |
| 141 |
* |
| 142 |
* @retval copy of the shape |
| 143 |
*/ |
| 144 |
virtual SHAPE* Clone() const |
| 145 |
{ |
| 146 |
assert( false ); |
| 147 |
return nullptr; |
| 148 |
}; |
| 149 |
|
| 150 |
/** |
| 151 |
* Return true if the shape is a null shape. |
| 152 |
* |
| 153 |
* @retval true if null :-) |
| 154 |
*/ |
| 155 |
bool IsNull() const |
| 156 |
{ |
| 157 |
return m_type == SH_NULL; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Check if the boundary of shape (this) lies closer to the point \a aP than \a aClearance, |
| 162 |
* indicating a collision. |
| 163 |
* |
| 164 |
* @param aActual [out] an optional pointer to an int to store the actual distance in the |
| 165 |
* event of a collision. |
| 166 |
* @param aLocation [out] an option pointer to a point to store a nearby location in the |
| 167 |
* event of a collision. |
| 168 |
* @return true, if there is a collision. |
| 169 |
*/ |
| 170 |
virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr, |
| 171 |
VECTOR2I* aLocation = nullptr ) const |
| 172 |
{ |
| 173 |
return Collide( SEG( aP, aP ), aClearance, aActual, aLocation ); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Check if the boundary of shape (this) lies closer to the shape \a aShape than \a aClearance, |
| 178 |
* indicating a collision. |
| 179 |
* |
| 180 |
* @param aShape shape to check collision against |
| 181 |
* @param aClearance minimum clearance |
| 182 |
* @param aMTV minimum translation vector |
| 183 |
* @param aActual [out] an optional pointer to an int to store the actual distance in the |
| 184 |
* event of a collision. |
| 185 |
* @param aLocation [out] an option pointer to a point to store a nearby location in the |
| 186 |
* event of a collision. |
| 187 |
* @return true, if there is a collision. |
| 188 |
*/ |
| 189 |
virtual bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const; |
| 190 |
|
| 191 |
virtual bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr, |
| 192 |
VECTOR2I* aLocation = nullptr ) const; |
| 193 |
|
| 194 |
/** |
| 195 |
* Check if the boundary of shape (this) lies closer to the segment \a aSeg than \a aClearance, |
| 196 |
* indicating a collision. |
| 197 |
* |
| 198 |
* @param aActual [out] an optional pointer to an int to be updated with the actual distance |
| 199 |
* int the event of a collision. |
| 200 |
* @param aLocation [out] an option pointer to a point to store a nearby location in the |
| 201 |
* event of a collision. |
| 202 |
* @return true, if there is a collision. |
| 203 |
*/ |
| 204 |
virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr, |
| 205 |
VECTOR2I* aLocation = nullptr ) const = 0; |
| 206 |
|
| 207 |
/** |
| 208 |
* Compute a bounding box of the shape, with a margin of \a aClearance a collision. |
| 209 |
* |
| 210 |
* @param aClearance how much the bounding box is expanded wrs to the minimum enclosing |
| 211 |
* rectangle for the shape. |
| 212 |
* @return the bounding box. |
| 213 |
*/ |
| 214 |
virtual const BOX2I BBox( int aClearance = 0 ) const = 0; |
| 215 |
|
| 216 |
/** |
| 217 |
* Compute a center-of-mass of the shape. |
| 218 |
* |
| 219 |
* @return the center-of-mass point |
| 220 |
*/ |
| 221 |
virtual VECTOR2I Centre() const |
| 222 |
{ |
| 223 |
return BBox( 0 ).Centre(); // if nothing better is available.... |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* @param aCenter is the rotation center. |
| 228 |
* @param aAngle rotation angle in radians. |
| 229 |
*/ |
| 230 |
virtual void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) = 0; |
| 231 |
|
| 232 |
virtual void Move( const VECTOR2I& aVector ) = 0; |
| 233 |
|
| 234 |
virtual bool IsSolid() const = 0; |
| 235 |
|
| 236 |
virtual bool Parse( std::stringstream& aStream ); |
| 237 |
|
| 238 |
virtual const std::string Format( ) const; |
| 239 |
|
| 240 |
protected: |
| 241 |
typedef VECTOR2I::extended_type ecoord; |
| 242 |
}; |
| 243 |
|
| 244 |
|
| 245 |
class SHAPE_LINE_CHAIN_BASE : public SHAPE |
| 246 |
{ |
| 247 |
public: |
| 248 |
SHAPE_LINE_CHAIN_BASE( SHAPE_TYPE aType ) : |
| 249 |
SHAPE( aType ) |
| 250 |
{ |
| 251 |
} |
| 252 |
|
| 253 |
virtual ~SHAPE_LINE_CHAIN_BASE() |
| 254 |
{ |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Check if point \a aP lies closer to us than \a aClearance. |
| 259 |
* |
| 260 |
* @param aP the point to check for collisions with |
| 261 |
* @param aClearance minimum distance that does not qualify as a collision. |
| 262 |
* @param aActual an optional pointer to an int to store the actual distance in the event |
| 263 |
* of a collision. |
| 264 |
* @return true, when a collision has been found |
| 265 |
*/ |
| 266 |
virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr, |
| 267 |
VECTOR2I* aLocation = nullptr ) const override; |
| 268 |
|
| 269 |
/** |
| 270 |
* Check if segment \a aSeg lies closer to us than \a aClearance. |
| 271 |
* |
| 272 |
* @param aSeg the segment to check for collisions with |
| 273 |
* @param aClearance minimum distance that does not qualify as a collision. |
| 274 |
* @param aActual an optional pointer to an int to store the actual distance in the event |
| 275 |
* of a collision. |
| 276 |
* @return true, when a collision has been found |
| 277 |
*/ |
| 278 |
|
| 279 |
virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr, |
| 280 |
VECTOR2I* aLocation = nullptr ) const override; |
| 281 |
|
| 282 |
SEG::ecoord SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly = false ) const; |
| 283 |
|
| 284 |
/** |
| 285 |
* Check if point \a aP lies inside a polygon (any type) defined by the line chain. |
| 286 |
* For closed shapes only. |
| 287 |
* |
| 288 |
* @param aPt point to check |
| 289 |
* @param aUseBBoxCache gives better performance if the bounding box caches have been |
| 290 |
* generated. |
| 291 |
* @return true if the point is inside the shape (edge is not treated as being inside). |
| 292 |
*/ |
| 293 |
bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const; |
| 294 |
|
| 295 |
/** |
| 296 |
* Check if point \a aP lies on an edge or vertex of the line chain. |
| 297 |
* |
| 298 |
* @param aP point to check |
| 299 |
* @return true if the point lies on the edge. |
| 300 |
*/ |
| 301 |
bool PointOnEdge( const VECTOR2I& aP, int aAccuracy = 0 ) const; |
| 302 |
|
| 303 |
/** |
| 304 |
* Check if point \a aP lies on an edge or vertex of the line chain. |
| 305 |
* |
| 306 |
* @param aP point to check |
| 307 |
* @return index of the first edge containing the point, otherwise negative |
| 308 |
*/ |
| 309 |
int EdgeContainingPoint( const VECTOR2I& aP, int aAccuracy = 0 ) const; |
| 310 |
|
| 311 |
virtual const VECTOR2I GetPoint( int aIndex ) const = 0; |
| 312 |
virtual const SEG GetSegment( int aIndex ) const = 0; |
| 313 |
virtual size_t GetPointCount() const = 0; |
| 314 |
virtual size_t GetSegmentCount() const = 0; |
| 315 |
virtual bool IsClosed() const = 0; |
| 316 |
|
| 317 |
virtual BOX2I* GetCachedBBox() const { return nullptr; } |
| 318 |
}; |
| 319 |
|
| 320 |
#endif // __SHAPE_H |