KiCad PCB EDA Suite
Loading...
Searching...
No Matches
shape.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2013 CERN
5 * Copyright The 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, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifndef __SHAPE_H
23#define __SHAPE_H
24
25#include <sstream>
26#include <vector>
27#include <geometry/seg.h>
28#include <geometry/eda_angle.h>
30#include <math/vector2d.h>
31#include <math/box2.h>
32#include <wx/string.h>
33
35class SHAPE_POLY_SET;
36
40
55
56static inline wxString SHAPE_TYPE_asString( SHAPE_TYPE a )
57{
58 switch( a )
59 {
60 case SH_RECT: return wxT( "SH_RECT" );
61 case SH_SEGMENT: return wxT( "SH_SEGMENT" );
62 case SH_LINE_CHAIN: return wxT( "SH_LINE_CHAIN" );
63 case SH_CIRCLE: return wxT( "SH_CIRCLE" );
64 case SH_SIMPLE: return wxT( "SH_SIMPLE" );
65 case SH_POLY_SET: return wxT( "SH_POLY_SET" );
66 case SH_COMPOUND: return wxT( "SH_COMPOUND" );
67 case SH_ARC: return wxT( "SH_ARC" );
68 case SH_NULL: return wxT( "SH_NULL" );
69 case SH_POLY_SET_TRIANGLE: return wxT( "SH_POLY_SET_TRIANGLE" );
70 case SH_ELLIPSE: return wxT( "SH_ELLIPSE" );
71 }
72
73 return wxEmptyString; // Just to quiet GCC.
74}
75
76class SHAPE;
77
79{
80public:
85 m_type( aType )
86 {}
87
88 virtual ~SHAPE_BASE()
89 {}
90
97 {
98 return m_type;
99 }
100
101 wxString TypeName() const
102 {
103 return SHAPE_TYPE_asString( m_type );
104 }
105
106 virtual bool HasIndexableSubshapes() const
107 {
108 return false;
109 }
110
111 virtual size_t GetIndexableSubshapeCount() const { return 0; }
112
113 virtual void GetIndexableSubshapes( std::vector<const SHAPE*>& aSubshapes ) const { }
114
115protected:
118};
119
123class SHAPE : public SHAPE_BASE
124{
125public:
129 static const int MIN_PRECISION_IU = 4;
130
134 SHAPE( SHAPE_TYPE aType ) :
135 SHAPE_BASE( aType )
136 {}
137
138 virtual ~SHAPE()
139 {}
140
146 virtual SHAPE* Clone() const
147 {
148 assert( false );
149 return nullptr;
150 };
151
157 int GetClearance( const SHAPE* aOther ) const;
158
164 bool IsNull() const
165 {
166 return m_type == SH_NULL;
167 }
168
179 virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
180 VECTOR2I* aLocation = nullptr ) const
181 {
182 return Collide( SEG( aP, aP ), aClearance, aActual, aLocation );
183 }
184
198 virtual bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const;
199
200 virtual bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
201 VECTOR2I* aLocation = nullptr ) const;
202
213 virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
214 VECTOR2I* aLocation = nullptr ) const = 0;
215
223 virtual const BOX2I BBox( int aClearance = 0 ) const = 0;
224
230 virtual VECTOR2I Centre() const
231 {
232 return BBox( 0 ).Centre(); // if nothing better is available....
233 }
234
242 virtual int Distance( const VECTOR2I& aP ) const;
243
247 virtual SEG::ecoord SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
248
258 bool NearestPoints( const SHAPE* aOther, VECTOR2I& aPtThis, VECTOR2I& aPtOther ) const;
259
268 virtual bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const;
269
276 virtual void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError, ERROR_LOC aErrorLoc ) const = 0;
277
282 virtual void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) = 0;
283
284 virtual VECTOR2I GetStart() const { return {}; }
285 virtual VECTOR2I GetEnd() const { return {}; }
286
287 virtual int GetWidth() const { return 0; }
288 virtual void SetWidth( int aWidth ) {}
289
290 virtual void Move( const VECTOR2I& aVector ) = 0;
291
292 virtual bool IsSolid() const = 0;
293
294 virtual bool Parse( std::stringstream& aStream );
295
296 virtual const std::string Format( bool aCplusPlus = true ) const;
297
298protected:
300};
301
302
304{
305public:
307 SHAPE( aType )
308 {
309 }
310
312 {
313 }
314
324 virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
325 VECTOR2I* aLocation = nullptr ) const override;
326
336
337 virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
338 VECTOR2I* aLocation = nullptr ) const override;
339
340 SEG::ecoord SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly = false ) const override;
341
342 bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const override;
343
350 bool PointOnEdge( const VECTOR2I& aP, int aAccuracy = 0 ) const;
351
358 int EdgeContainingPoint( const VECTOR2I& aP, int aAccuracy = 0 ) const;
359
360 virtual const VECTOR2I GetPoint( int aIndex ) const = 0;
361 virtual const SEG GetSegment( int aIndex ) const = 0;
362 virtual size_t GetPointCount() const = 0;
363 virtual size_t GetSegmentCount() const = 0;
364 virtual bool IsClosed() const = 0;
365
366 virtual BOX2I* GetCachedBBox() const { return nullptr; }
367
368 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError,
369 ERROR_LOC aErrorLoc ) const override
370 {}
371};
372
373#endif // __SHAPE_H
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr Vec Centre() const
Definition box2.h:93
Definition seg.h:38
VECTOR2I::extended_type ecoord
Definition seg.h:40
virtual size_t GetIndexableSubshapeCount() const
Definition shape.h:111
SHAPE_BASE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition shape.h:84
wxString TypeName() const
Definition shape.h:101
SHAPE_TYPE m_type
< type of our shape
Definition shape.h:117
virtual void GetIndexableSubshapes(std::vector< const SHAPE * > &aSubshapes) const
Definition shape.h:113
virtual ~SHAPE_BASE()
Definition shape.h:88
virtual bool HasIndexableSubshapes() const
Definition shape.h:106
SHAPE_TYPE Type() const
Return the type of the shape.
Definition shape.h:96
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.
bool PointOnEdge(const VECTOR2I &aP, int aAccuracy=0) const
Check if point aP lies on an edge or vertex of the line chain.
int EdgeContainingPoint(const VECTOR2I &aP, int aAccuracy=0) const
Check if point aP lies on an edge or vertex of the line chain.
virtual size_t GetPointCount() const =0
SHAPE_LINE_CHAIN_BASE(SHAPE_TYPE aType)
Definition shape.h:306
virtual size_t GetSegmentCount() const =0
virtual const VECTOR2I GetPoint(int aIndex) const =0
virtual BOX2I * GetCachedBBox() const
Definition shape.h:366
void TransformToPolygon(SHAPE_POLY_SET &aBuffer, int aError, ERROR_LOC aErrorLoc) const override
Fills a SHAPE_POLY_SET with a polygon representation of this shape.
Definition shape.h:368
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
SEG::ecoord SquaredDistance(const VECTOR2I &aP, bool aOutlineOnly=false) const override
virtual ~SHAPE_LINE_CHAIN_BASE()
Definition shape.h:311
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
Represent a set of closed polygons.
An abstract shape on 2D plane.
Definition shape.h:124
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,...
Definition shape.h:179
int GetClearance(const SHAPE *aOther) const
Return the actual minimum distance between two shapes.
Definition shape.cpp:51
virtual void TransformToPolygon(SHAPE_POLY_SET &aBuffer, int aError, ERROR_LOC aErrorLoc) const =0
Fills a SHAPE_POLY_SET with a polygon representation of this shape.
VECTOR2I::extended_type ecoord
Definition shape.h:299
virtual VECTOR2I GetStart() const
Definition shape.h:284
virtual VECTOR2I Centre() const
Compute a center-of-mass of the shape.
Definition shape.h:230
virtual void Move(const VECTOR2I &aVector)=0
virtual bool Parse(std::stringstream &aStream)
Definition shape.cpp:36
virtual VECTOR2I GetEnd() const
Definition shape.h:285
virtual bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Check if point aP lies inside a closed shape.
Definition shape.cpp:123
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition shape.h:134
virtual int GetWidth() const
Definition shape.h:287
virtual const std::string Format(bool aCplusPlus=true) const
Definition shape.cpp:43
bool NearestPoints(const SHAPE *aOther, VECTOR2I &aPtThis, VECTOR2I &aPtOther) const
Return the two points that mark the closest distance between this shape and aOther.
virtual int Distance(const VECTOR2I &aP) const
Returns the minimum distance from a given point to this shape.
Definition shape.cpp:105
static const int MIN_PRECISION_IU
This is the minimum precision for all the points in a shape.
Definition shape.h:129
bool IsNull() const
Return true if the shape is a null shape.
Definition shape.h:164
virtual SHAPE * Clone() const
Return a dynamically allocated copy of the shape.
Definition shape.h:146
virtual void SetWidth(int aWidth)
Definition shape.h:288
virtual void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 })=0
virtual ~SHAPE()
Definition shape.h:138
virtual bool IsSolid() const =0
virtual bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const =0
Check if the boundary of shape (this) lies closer to the segment aSeg than aClearance,...
virtual const BOX2I BBox(int aClearance=0) const =0
Compute a bounding box of the shape, with a margin of aClearance a collision.
virtual SEG::ecoord SquaredDistance(const VECTOR2I &aP, bool aOutlineOnly=false) const
Definition shape.cpp:111
VECTOR2_TRAITS< int32_t >::extended_type extended_type
Definition vector2d.h:69
SHAPE_TYPE
Lists all supported shapes.
Definition shape.h:42
@ SH_POLY_SET
set of polygons (with holes, etc.)
Definition shape.h:48
@ SH_RECT
axis-aligned rectangle
Definition shape.h:43
@ SH_CIRCLE
circle
Definition shape.h:46
@ SH_SIMPLE
simple polygon
Definition shape.h:47
@ SH_ELLIPSE
ellipse or elliptical arc
Definition shape.h:53
@ SH_NULL
empty shape (no shape...),
Definition shape.h:51
@ SH_SEGMENT
line segment
Definition shape.h:44
@ SH_ARC
circular arc
Definition shape.h:50
@ SH_POLY_SET_TRIANGLE
a single triangle belonging to a POLY_SET triangulation
Definition shape.h:52
@ SH_LINE_CHAIN
line chain (polyline)
Definition shape.h:45
@ SH_COMPOUND
compound shape, consisting of multiple simple shapes
Definition shape.h:49
static wxString SHAPE_TYPE_asString(SHAPE_TYPE a)
Definition shape.h:56
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683