KiCad PCB EDA Suite
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 (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 <geometry/eda_angle.h>
33#include <math/vector2d.h>
34#include <math/box2.h>
35#include <wx/string.h>
36
38
44{
45 SH_RECT = 0,
55};
56
57static inline wxString SHAPE_TYPE_asString( SHAPE_TYPE a )
58{
59 switch( a )
60 {
61 case SH_RECT: return wxT( "SH_RECT" );
62 case SH_SEGMENT: return wxT( "SH_SEGMENT" );
63 case SH_LINE_CHAIN: return wxT( "SH_LINE_CHAIN" );
64 case SH_CIRCLE: return wxT( "SH_CIRCLE" );
65 case SH_SIMPLE: return wxT( "SH_SIMPLE" );
66 case SH_POLY_SET: return wxT( "SH_POLY_SET" );
67 case SH_COMPOUND: return wxT( "SH_COMPOUND" );
68 case SH_ARC: return wxT( "SH_ARC" );
69 case SH_NULL: return wxT( "SH_NULL" );
70 case SH_POLY_SET_TRIANGLE: return wxT( "SH_POLY_SET_TRIANGLE" );
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
239 virtual void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) = 0;
240
241 virtual void Move( const VECTOR2I& aVector ) = 0;
242
243 virtual bool IsSolid() const = 0;
244
245 virtual bool Parse( std::stringstream& aStream );
246
247 virtual const std::string Format( bool aCplusPlus = true ) const;
248
249protected:
251};
252
253
255{
256public:
258 SHAPE( aType )
259 {
260 }
261
263 {
264 }
265
275 virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
276 VECTOR2I* aLocation = nullptr ) const override;
277
288 virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
289 VECTOR2I* aLocation = nullptr ) const override;
290
291 SEG::ecoord SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
292
302 bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const;
303
310 bool PointOnEdge( const VECTOR2I& aP, int aAccuracy = 0 ) const;
311
318 int EdgeContainingPoint( const VECTOR2I& aP, int aAccuracy = 0 ) const;
319
320 virtual const VECTOR2I GetPoint( int aIndex ) const = 0;
321 virtual const SEG GetSegment( int aIndex ) const = 0;
322 virtual size_t GetPointCount() const = 0;
323 virtual size_t GetSegmentCount() const = 0;
324 virtual bool IsClosed() const = 0;
325
326 virtual BOX2I* GetCachedBBox() const { return nullptr; }
327};
328
329#endif // __SHAPE_H
Vec Centre() const
Definition: box2.h:70
Definition: seg.h:42
VECTOR2I::extended_type ecoord
Definition: seg.h:44
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
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Check if point aP lies inside a polygon (any type) defined by the line chain.
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.
SEG::ecoord SquaredDistance(const VECTOR2I &aP, bool aOutlineOnly=false) const
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:257
virtual size_t GetSegmentCount() const =0
virtual const VECTOR2I GetPoint(int aIndex) const =0
virtual BOX2I * GetCachedBBox() const
Definition: shape.h:326
virtual bool IsClosed() const =0
virtual const SEG GetSegment(int aIndex) const =0
virtual ~SHAPE_LINE_CHAIN_BASE()
Definition: shape.h:262
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
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:49
VECTOR2I::extended_type ecoord
Definition: shape.h:250
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:34
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition: shape.h:134
virtual const std::string Format(bool aCplusPlus=true) const
Definition: shape.cpp:41
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 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.
VECTOR2_TRAITS< int >::extended_type extended_type
Definition: vector2d.h:72
SHAPE_TYPE
Lists all supported shapes.
Definition: shape.h:44
@ SH_POLY_SET
set of polygons (with holes, etc.)
Definition: shape.h:50
@ SH_RECT
axis-aligned rectangle
Definition: shape.h:45
@ SH_CIRCLE
circle
Definition: shape.h:48
@ SH_SIMPLE
simple polygon
Definition: shape.h:49
@ SH_NULL
empty shape (no shape...),
Definition: shape.h:53
@ SH_SEGMENT
line segment
Definition: shape.h:46
@ SH_ARC
circular arc
Definition: shape.h:52
@ SH_POLY_SET_TRIANGLE
a single triangle belonging to a POLY_SET triangulation
Definition: shape.h:54
@ SH_LINE_CHAIN
line chain (polyline)
Definition: shape.h:47
@ SH_COMPOUND
compound shape, consisting of multiple simple shapes
Definition: shape.h:51
static wxString SHAPE_TYPE_asString(SHAPE_TYPE a)
Definition: shape.h:57