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
37
43{
44 SH_RECT = 0,
54};
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 }
71
72 return wxEmptyString; // Just to quiet GCC.
73}
74
75class SHAPE;
76
78{
79public:
84 m_type( aType )
85 {}
86
87 virtual ~SHAPE_BASE()
88 {}
89
96 {
97 return m_type;
98 }
99
100 wxString TypeName() const
101 {
102 return SHAPE_TYPE_asString( m_type );
103 }
104
105 virtual bool HasIndexableSubshapes() const
106 {
107 return false;
108 }
109
110 virtual size_t GetIndexableSubshapeCount() const { return 0; }
111
112 virtual void GetIndexableSubshapes( std::vector<const SHAPE*>& aSubshapes ) const { }
113
114protected:
117};
118
122class SHAPE : public SHAPE_BASE
123{
124public:
128 static const int MIN_PRECISION_IU = 4;
129
133 SHAPE( SHAPE_TYPE aType ) :
134 SHAPE_BASE( aType )
135 {}
136
137 virtual ~SHAPE()
138 {}
139
145 virtual SHAPE* Clone() const
146 {
147 assert( false );
148 return nullptr;
149 };
150
156 int GetClearance( const SHAPE* aOther ) const;
157
163 bool IsNull() const
164 {
165 return m_type == SH_NULL;
166 }
167
178 virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
179 VECTOR2I* aLocation = nullptr ) const
180 {
181 return Collide( SEG( aP, aP ), aClearance, aActual, aLocation );
182 }
183
197 virtual bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const;
198
199 virtual bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
200 VECTOR2I* aLocation = nullptr ) const;
201
212 virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
213 VECTOR2I* aLocation = nullptr ) const = 0;
214
222 virtual const BOX2I BBox( int aClearance = 0 ) const = 0;
223
229 virtual VECTOR2I Centre() const
230 {
231 return BBox( 0 ).Centre(); // if nothing better is available....
232 }
233
238 virtual void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) = 0;
239
240 virtual void Move( const VECTOR2I& aVector ) = 0;
241
242 virtual bool IsSolid() const = 0;
243
244 virtual bool Parse( std::stringstream& aStream );
245
246 virtual const std::string Format( bool aCplusPlus = true ) const;
247
248protected:
250};
251
252
254{
255public:
257 SHAPE( aType )
258 {
259 }
260
262 {
263 }
264
274 virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
275 VECTOR2I* aLocation = nullptr ) const override;
276
287 virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
288 VECTOR2I* aLocation = nullptr ) const override;
289
290 SEG::ecoord SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
291
301 bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const;
302
309 bool PointOnEdge( const VECTOR2I& aP, int aAccuracy = 0 ) const;
310
317 int EdgeContainingPoint( const VECTOR2I& aP, int aAccuracy = 0 ) const;
318
319 virtual const VECTOR2I GetPoint( int aIndex ) const = 0;
320 virtual const SEG GetSegment( int aIndex ) const = 0;
321 virtual size_t GetPointCount() const = 0;
322 virtual size_t GetSegmentCount() const = 0;
323 virtual bool IsClosed() const = 0;
324
325 virtual BOX2I* GetCachedBBox() const { return nullptr; }
326};
327
328#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:110
SHAPE_BASE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition: shape.h:83
wxString TypeName() const
Definition: shape.h:100
SHAPE_TYPE m_type
< type of our shape
Definition: shape.h:116
virtual void GetIndexableSubshapes(std::vector< const SHAPE * > &aSubshapes) const
Definition: shape.h:112
virtual ~SHAPE_BASE()
Definition: shape.h:87
virtual bool HasIndexableSubshapes() const
Definition: shape.h:105
SHAPE_TYPE Type() const
Return the type of the shape.
Definition: shape.h:95
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:256
virtual size_t GetSegmentCount() const =0
virtual const VECTOR2I GetPoint(int aIndex) const =0
virtual BOX2I * GetCachedBBox() const
Definition: shape.h:325
virtual bool IsClosed() const =0
virtual const SEG GetSegment(int aIndex) const =0
virtual ~SHAPE_LINE_CHAIN_BASE()
Definition: shape.h:261
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:123
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:178
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:249
virtual VECTOR2I Centre() const
Compute a center-of-mass of the shape.
Definition: shape.h:229
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:133
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:128
bool IsNull() const
Return true if the shape is a null shape.
Definition: shape.h:163
virtual SHAPE * Clone() const
Return a dynamically allocated copy of the shape.
Definition: shape.h:145
virtual void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 })=0
virtual ~SHAPE()
Definition: shape.h:137
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:76
SHAPE_TYPE
Lists all supported shapes.
Definition: shape.h:43
@ SH_POLY_SET
set of polygons (with holes, etc.)
Definition: shape.h:49
@ SH_RECT
axis-aligned rectangle
Definition: shape.h:44
@ SH_CIRCLE
circle
Definition: shape.h:47
@ SH_SIMPLE
simple polygon
Definition: shape.h:48
@ SH_NULL
empty shape (no shape...),
Definition: shape.h:52
@ SH_SEGMENT
line segment
Definition: shape.h:45
@ SH_ARC
circular arc
Definition: shape.h:51
@ SH_POLY_SET_TRIANGLE
a single triangle belonging to a POLY_SET triangulation
Definition: shape.h:53
@ SH_LINE_CHAIN
line chain (polyline)
Definition: shape.h:46
@ SH_COMPOUND
compound shape, consisting of multiple simple shapes
Definition: shape.h:50
static wxString SHAPE_TYPE_asString(SHAPE_TYPE a)
Definition: shape.h:56