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 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
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 
42 {
43  SH_RECT = 0,
53 };
54 
55 static inline wxString SHAPE_TYPE_asString( SHAPE_TYPE a )
56 {
57  switch( a )
58  {
59  case SH_RECT: return "SH_RECT";
60  case SH_SEGMENT: return "SH_SEGMENT";
61  case SH_LINE_CHAIN: return "SH_LINE_CHAIN";
62  case SH_CIRCLE: return "SH_CIRCLE";
63  case SH_SIMPLE: return "SH_SIMPLE";
64  case SH_POLY_SET: return "SH_POLY_SET";
65  case SH_COMPOUND: return "SH_COMPOUND";
66  case SH_ARC: return "SH_ARC";
67  case SH_NULL: return "SH_NULL";
68  case SH_POLY_SET_TRIANGLE: return "SH_POLY_SET_TRIANGLE";
69  }
70 
71  return wxEmptyString; // Just to quiet GCC.
72 }
73 
74 class SHAPE;
75 
77 {
78 public:
83  m_type( aType )
84  {}
85 
86  virtual ~SHAPE_BASE()
87  {}
88 
94  SHAPE_TYPE Type() const
95  {
96  return m_type;
97  }
98 
99  virtual bool HasIndexableSubshapes() const
100  {
101  return false;
102  }
103 
104  virtual size_t GetIndexableSubshapeCount() const { return 0; }
105 
106  virtual void GetIndexableSubshapes( std::vector<SHAPE*>& aSubshapes ) { }
107 
108 protected:
111 };
112 
116 class SHAPE : public SHAPE_BASE
117 {
118 public:
122  static const int MIN_PRECISION_IU = 4;
123 
127  SHAPE( SHAPE_TYPE aType ) :
128  SHAPE_BASE( aType )
129  {}
130 
131  virtual ~SHAPE()
132  {}
133 
139  virtual SHAPE* Clone() const
140  {
141  assert( false );
142  return nullptr;
143  };
144 
150  bool IsNull() const
151  {
152  return m_type == SH_NULL;
153  }
154 
165  virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
166  VECTOR2I* aLocation = nullptr ) const
167  {
168  return Collide( SEG( aP, aP ), aClearance, aActual, aLocation );
169  }
170 
184  virtual bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const;
185 
186  virtual bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
187  VECTOR2I* aLocation = nullptr ) const;
188 
199  virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
200  VECTOR2I* aLocation = nullptr ) const = 0;
201 
209  virtual const BOX2I BBox( int aClearance = 0 ) const = 0;
210 
216  virtual VECTOR2I Centre() const
217  {
218  return BBox( 0 ).Centre(); // if nothing better is available....
219  }
220 
225  virtual void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) = 0;
226 
227  virtual void Move( const VECTOR2I& aVector ) = 0;
228 
229  virtual bool IsSolid() const = 0;
230 
231  virtual bool Parse( std::stringstream& aStream );
232 
233  virtual const std::string Format( ) const;
234 
235 protected:
237 };
238 
239 
241 {
242 public:
244  SHAPE( aType )
245  {
246  }
247 
249  {
250  }
251 
261  virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
262  VECTOR2I* aLocation = nullptr ) const override;
263 
274  virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
275  VECTOR2I* aLocation = nullptr ) const override;
276 
277  SEG::ecoord SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
278 
288  bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const;
289 
296  bool PointOnEdge( const VECTOR2I& aP, int aAccuracy = 0 ) const;
297 
304  int EdgeContainingPoint( const VECTOR2I& aP, int aAccuracy = 0 ) const;
305 
306  virtual const VECTOR2I GetPoint( int aIndex ) const = 0;
307  virtual const SEG GetSegment( int aIndex ) const = 0;
308  virtual size_t GetPointCount() const = 0;
309  virtual size_t GetSegmentCount() const = 0;
310  virtual bool IsClosed() const = 0;
311 };
312 
313 #endif // __SHAPE_H
SHAPE_LINE_CHAIN_BASE(SHAPE_TYPE aType)
Definition: shape.h:243
virtual size_t GetIndexableSubshapeCount() const
Definition: shape.h:104
VECTOR2_TRAITS< int >::extended_type extended_type
Definition: vector2d.h:76
compound shape, consisting of multiple simple shapes
Definition: shape.h:49
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition: shape.h:127
SEG::ecoord SquaredDistance(const VECTOR2I &aP, bool aOutlineOnly=false) const
virtual bool IsClosed() const =0
VECTOR2I::extended_type ecoord
Definition: seg.h:43
virtual const std::string Format() const
Definition: shape.cpp:35
virtual size_t GetPointCount() const =0
virtual ~SHAPE_BASE()
Definition: shape.h:86
bool PointOnEdge(const VECTOR2I &aP, int aAccuracy=0) const
Check if point aP lies on an edge or vertex of the line chain.
virtual VECTOR2I Centre() const
Compute a center-of-mass of the shape.
Definition: shape.h:216
virtual size_t GetSegmentCount() const =0
virtual ~SHAPE_LINE_CHAIN_BASE()
Definition: shape.h:248
A 2D bounding box built on top of an origin point and size vector.
Definition: box2.h:41
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:165
virtual const BOX2I BBox(int aClearance=0) const =0
Compute a bounding box of the shape, with a margin of aClearance a collision.
virtual ~SHAPE()
Definition: shape.h:131
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.
SHAPE_TYPE m_type
< type of our shape
Definition: shape.h:110
bool IsNull() const
Return true if the shape is a null shape.
Definition: shape.h:150
static const int MIN_PRECISION_IU
This is the minimum precision for all the points in a shape.
Definition: shape.h:122
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 SHAPE * Clone() const
Return a dynamically allocated copy of the shape.
Definition: shape.h:139
static wxString SHAPE_TYPE_asString(SHAPE_TYPE a)
Definition: shape.h:55
circular arc
Definition: shape.h:50
An abstract shape on 2D plane.
Definition: shape.h:116
virtual bool Parse(std::stringstream &aStream)
Definition: shape.cpp:28
circle
Definition: shape.h:46
Definition: seg.h:40
virtual const SEG GetSegment(int aIndex) const =0
set of polygons (with holes, etc.)
Definition: shape.h:48
virtual bool IsSolid() const =0
a single triangle belonging to a POLY_SET triangulation
Definition: shape.h:52
virtual void GetIndexableSubshapes(std::vector< SHAPE * > &aSubshapes)
Definition: shape.h:106
Vec Centre() const
Definition: box2.h:63
VECTOR2I::extended_type ecoord
Definition: shape.h:236
Represent a polyline (an zero-thickness chain of connected line segments).
empty shape (no shape...),
Definition: shape.h:51
line chain (polyline)
Definition: shape.h:45
virtual bool HasIndexableSubshapes() const
Definition: shape.h:99
int EdgeContainingPoint(const VECTOR2I &aP, int aAccuracy=0) const
Check if point aP lies on an edge or vertex of the line chain.
axis-aligned rectangle
Definition: shape.h:43
virtual const VECTOR2I GetPoint(int aIndex) const =0
virtual void Move(const VECTOR2I &aVector)=0
SHAPE_TYPE
Lists all supported shapes.
Definition: shape.h:41
simple polygon
Definition: shape.h:47
SHAPE_TYPE Type() const
Return the type of the shape.
Definition: shape.h:94
line segment
Definition: shape.h:44
SHAPE_BASE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition: shape.h:82
virtual void Rotate(double aAngle, const VECTOR2I &aCenter={ 0, 0 })=0