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 (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>
34#include <math/vector2d.h>
35#include <math/box2.h>
36#include <wx/string.h>
37
39class SHAPE_POLY_SET;
40
46{
47 SH_RECT = 0,
57};
58
59static inline wxString SHAPE_TYPE_asString( SHAPE_TYPE a )
60{
61 switch( a )
62 {
63 case SH_RECT: return wxT( "SH_RECT" );
64 case SH_SEGMENT: return wxT( "SH_SEGMENT" );
65 case SH_LINE_CHAIN: return wxT( "SH_LINE_CHAIN" );
66 case SH_CIRCLE: return wxT( "SH_CIRCLE" );
67 case SH_SIMPLE: return wxT( "SH_SIMPLE" );
68 case SH_POLY_SET: return wxT( "SH_POLY_SET" );
69 case SH_COMPOUND: return wxT( "SH_COMPOUND" );
70 case SH_ARC: return wxT( "SH_ARC" );
71 case SH_NULL: return wxT( "SH_NULL" );
72 case SH_POLY_SET_TRIANGLE: return wxT( "SH_POLY_SET_TRIANGLE" );
73 }
74
75 return wxEmptyString; // Just to quiet GCC.
76}
77
78class SHAPE;
79
81{
82public:
87 m_type( aType )
88 {}
89
90 virtual ~SHAPE_BASE()
91 {}
92
99 {
100 return m_type;
101 }
102
103 wxString TypeName() const
104 {
105 return SHAPE_TYPE_asString( m_type );
106 }
107
108 virtual bool HasIndexableSubshapes() const
109 {
110 return false;
111 }
112
113 virtual size_t GetIndexableSubshapeCount() const { return 0; }
114
115 virtual void GetIndexableSubshapes( std::vector<const SHAPE*>& aSubshapes ) const { }
116
117protected:
120};
121
125class SHAPE : public SHAPE_BASE
126{
127public:
131 static const int MIN_PRECISION_IU = 4;
132
136 SHAPE( SHAPE_TYPE aType ) :
137 SHAPE_BASE( aType )
138 {}
139
140 virtual ~SHAPE()
141 {}
142
148 virtual SHAPE* Clone() const
149 {
150 assert( false );
151 return nullptr;
152 };
153
159 int GetClearance( const SHAPE* aOther ) const;
160
166 bool IsNull() const
167 {
168 return m_type == SH_NULL;
169 }
170
181 virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
182 VECTOR2I* aLocation = nullptr ) const
183 {
184 return Collide( SEG( aP, aP ), aClearance, aActual, aLocation );
185 }
186
200 virtual bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const;
201
202 virtual bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
203 VECTOR2I* aLocation = nullptr ) const;
204
215 virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
216 VECTOR2I* aLocation = nullptr ) const = 0;
217
225 virtual const BOX2I BBox( int aClearance = 0 ) const = 0;
226
232 virtual VECTOR2I Centre() const
233 {
234 return BBox( 0 ).Centre(); // if nothing better is available....
235 }
236
244 virtual int Distance( const VECTOR2I& aP ) const;
245
249 virtual SEG::ecoord SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
250
259 virtual bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const;
260
267 virtual void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError, ERROR_LOC aErrorLoc ) const = 0;
268
273 virtual void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) = 0;
274
275 virtual void Move( const VECTOR2I& aVector ) = 0;
276
277 virtual bool IsSolid() const = 0;
278
279 virtual bool Parse( std::stringstream& aStream );
280
281 virtual const std::string Format( bool aCplusPlus = true ) const;
282
283protected:
285};
286
287
289{
290public:
292 SHAPE( aType )
293 {
294 }
295
297 {
298 }
299
309 virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
310 VECTOR2I* aLocation = nullptr ) const override;
311
322 virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
323 VECTOR2I* aLocation = nullptr ) const override;
324
325 SEG::ecoord SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly = false ) const override;
326
327 bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const override;
328
335 bool PointOnEdge( const VECTOR2I& aP, int aAccuracy = 0 ) const;
336
343 int EdgeContainingPoint( const VECTOR2I& aP, int aAccuracy = 0 ) const;
344
345 virtual const VECTOR2I GetPoint( int aIndex ) const = 0;
346 virtual const SEG GetSegment( int aIndex ) const = 0;
347 virtual size_t GetPointCount() const = 0;
348 virtual size_t GetSegmentCount() const = 0;
349 virtual bool IsClosed() const = 0;
350
351 virtual BOX2I* GetCachedBBox() const { return nullptr; }
352
353 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError,
354 ERROR_LOC aErrorLoc ) const override
355 {}
356};
357
358#endif // __SHAPE_H
Vec Centre() const
Definition: box2.h:71
Definition: seg.h:42
VECTOR2I::extended_type ecoord
Definition: seg.h:44
virtual size_t GetIndexableSubshapeCount() const
Definition: shape.h:113
SHAPE_BASE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition: shape.h:86
wxString TypeName() const
Definition: shape.h:103
SHAPE_TYPE m_type
< type of our shape
Definition: shape.h:119
virtual void GetIndexableSubshapes(std::vector< const SHAPE * > &aSubshapes) const
Definition: shape.h:115
virtual ~SHAPE_BASE()
Definition: shape.h:90
virtual bool HasIndexableSubshapes() const
Definition: shape.h:108
SHAPE_TYPE Type() const
Return the type of the shape.
Definition: shape.h:98
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:291
virtual size_t GetSegmentCount() const =0
virtual const VECTOR2I GetPoint(int aIndex) const =0
virtual BOX2I * GetCachedBBox() const
Definition: shape.h:351
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:353
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:296
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:126
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:181
int GetClearance(const SHAPE *aOther) const
Return the actual minimum distance between two shapes.
Definition: shape.cpp:54
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:284
virtual VECTOR2I Centre() const
Compute a center-of-mass of the shape.
Definition: shape.h:232
virtual void Move(const VECTOR2I &aVector)=0
virtual bool Parse(std::stringstream &aStream)
Definition: shape.cpp:39
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:114
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition: shape.h:136
virtual const std::string Format(bool aCplusPlus=true) const
Definition: shape.cpp:46
virtual int Distance(const VECTOR2I &aP) const
Returns the minimum distance from a given point to this shape.
Definition: shape.cpp:96
static const int MIN_PRECISION_IU
This is the minimum precision for all the points in a shape.
Definition: shape.h:131
bool IsNull() const
Return true if the shape is a null shape.
Definition: shape.h:166
virtual SHAPE * Clone() const
Return a dynamically allocated copy of the shape.
Definition: shape.h:148
virtual void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 })=0
virtual ~SHAPE()
Definition: shape.h:140
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:102
VECTOR2_TRAITS< int >::extended_type extended_type
Definition: vector2d.h:72
a few functions useful in geometry calculations.
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
SHAPE_TYPE
Lists all supported shapes.
Definition: shape.h:46
@ SH_POLY_SET
set of polygons (with holes, etc.)
Definition: shape.h:52
@ SH_RECT
axis-aligned rectangle
Definition: shape.h:47
@ SH_CIRCLE
circle
Definition: shape.h:50
@ SH_SIMPLE
simple polygon
Definition: shape.h:51
@ SH_NULL
empty shape (no shape...),
Definition: shape.h:55
@ SH_SEGMENT
line segment
Definition: shape.h:48
@ SH_ARC
circular arc
Definition: shape.h:54
@ SH_POLY_SET_TRIANGLE
a single triangle belonging to a POLY_SET triangulation
Definition: shape.h:56
@ SH_LINE_CHAIN
line chain (polyline)
Definition: shape.h:49
@ SH_COMPOUND
compound shape, consisting of multiple simple shapes
Definition: shape.h:53
static wxString SHAPE_TYPE_asString(SHAPE_TYPE a)
Definition: shape.h:59