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  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #ifndef __SHAPE_H
26 #define __SHAPE_H
27 
28 #include <sstream>
29 #include <vector>
30 #include <geometry/seg.h>
31 #include <math/vector2d.h>
32 #include <math/box2.h>
33 
34 class SHAPE_LINE_CHAIN;
35 
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:
85  SHAPE_BASE( SHAPE_TYPE aType ) : m_type( aType )
86  {}
87 
88  // Destructor
89  virtual ~SHAPE_BASE()
90  {}
91 
98  SHAPE_TYPE Type() const
99  {
100  return m_type;
101  }
102 
103  virtual bool HasIndexableSubshapes() const
104  {
105  return false;
106  }
107 
108  virtual size_t GetIndexableSubshapeCount() const { return 0; }
109 
110  virtual void GetIndexableSubshapes( std::vector<SHAPE*>& aSubshapes ) { }
111 
112 protected:
115 };
116 
122 class SHAPE : public SHAPE_BASE
123 {
124 protected:
126 
127 public:
134  SHAPE( SHAPE_TYPE aType ) : SHAPE_BASE( aType )
135  {}
136 
137  // Destructor
138  virtual ~SHAPE()
139  {}
140 
141 
148  virtual SHAPE* Clone() const
149  {
150  assert( false );
151  return NULL;
152  };
153 
160  bool IsNull() const
161  {
162  return m_type == SH_NULL;
163  }
164 
176  virtual bool Collide( const VECTOR2I& aP, int aClearance = 0,
177  int* aActual = nullptr, VECTOR2I* aLocation = nullptr ) const
178  {
179  return Collide( SEG( aP, aP ), aClearance, aActual, aLocation );
180  }
181 
196  virtual bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const;
197 
198  virtual bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
199  VECTOR2I* aLocation = nullptr ) const;
200 
212  virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
213  VECTOR2I* aLocation = nullptr ) const = 0;
214 
224  virtual const BOX2I BBox( int aClearance = 0 ) const = 0;
225 
232  virtual VECTOR2I Centre() const
233  {
234  return BBox( 0 ).Centre(); // if nothing better is available....
235  }
236 
242  virtual void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) = 0;
243 
244  virtual void Move( const VECTOR2I& aVector ) = 0;
245 
246  virtual bool IsSolid() const = 0;
247 
248  virtual bool Parse( std::stringstream& aStream );
249 
250  virtual const std::string Format( ) const;
251 };
252 
253 
255 {
256 public:
258  {
259  }
260 
261  // Destructor
263  {
264  }
265 
276  virtual bool Collide( const VECTOR2I& aP, int aClearance = 0,
277  int* aActual = nullptr, VECTOR2I* aLocation = nullptr ) const override;
278 
290  virtual bool Collide( const SEG& aSeg, int aClearance = 0,
291  int* aActual = nullptr, VECTOR2I* aLocation = nullptr ) const override;
292 
293  SEG::ecoord SquaredDistance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
294 
305  bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0, bool aUseBBoxCache = false ) const;
306 
314  bool PointOnEdge( const VECTOR2I& aP, int aAccuracy = 0 ) const;
315 
323  int EdgeContainingPoint( const VECTOR2I& aP, int aAccuracy = 0 ) const;
324 
325  virtual const VECTOR2I GetPoint( int aIndex ) const = 0;
326  virtual const SEG GetSegment( int aIndex ) const = 0;
327  virtual size_t GetPointCount() const = 0;
328  virtual size_t GetSegmentCount() const = 0;
329  virtual bool IsClosed() const = 0;
330 };
331 
332 #endif // __SHAPE_H
SHAPE_LINE_CHAIN_BASE(SHAPE_TYPE aType)
Definition: shape.h:257
virtual size_t GetIndexableSubshapeCount() const
Definition: shape.h:108
VECTOR2_TRAITS< int >::extended_type extended_type
Definition: vector2d.h:77
set of polygons (with holes, etc.)
Definition: shape.h:49
SHAPE(SHAPE_TYPE aType)
Constructor.
Definition: shape.h:134
SEG::ecoord SquaredDistance(const VECTOR2I &aP, bool aOutlineOnly=false) const
virtual bool IsClosed() const =0
VECTOR2I::extended_type ecoord
Definition: seg.h:42
virtual const std::string Format() const
Definition: shape.cpp:35
virtual size_t GetPointCount() const =0
virtual ~SHAPE_BASE()
Definition: shape.h:89
bool PointOnEdge(const VECTOR2I &aP, int aAccuracy=0) const
Function PointOnEdge()
virtual VECTOR2I Centre() const
Function Centre()
Definition: shape.h:232
virtual size_t GetSegmentCount() const =0
virtual ~SHAPE_LINE_CHAIN_BASE()
Definition: shape.h:262
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Function Collide()
Definition: shape.h:176
virtual const BOX2I BBox(int aClearance=0) const =0
Function BBox()
virtual ~SHAPE()
Definition: shape.h:138
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Function Collide()
SHAPE_TYPE m_type
type of our shape
Definition: shape.h:114
bool IsNull() const
Function IsNull()
Definition: shape.h:160
#define NULL
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Function PointInside()
virtual SHAPE * Clone() const
Function Clone()
Definition: shape.h:148
static wxString SHAPE_TYPE_asString(SHAPE_TYPE a)
Definition: shape.h:55
compound shape, consisting of multiple simple shapes
Definition: shape.h:50
SHAPE.
Definition: shape.h:122
virtual bool Parse(std::stringstream &aStream)
Definition: shape.cpp:28
line chain (polyline)
Definition: shape.h:46
Definition: seg.h:39
virtual const SEG GetSegment(int aIndex) const =0
simple polygon
Definition: shape.h:48
virtual bool IsSolid() const =0
empty shape (no shape...),
Definition: shape.h:52
virtual void GetIndexableSubshapes(std::vector< SHAPE * > &aSubshapes)
Definition: shape.h:110
Vec Centre() const
Definition: box2.h:79
VECTOR2I::extended_type ecoord
Definition: shape.h:125
SHAPE_LINE_CHAIN.
circular arc
Definition: shape.h:51
line segment
Definition: shape.h:45
virtual bool HasIndexableSubshapes() const
Definition: shape.h:103
int EdgeContainingPoint(const VECTOR2I &aP, int aAccuracy=0) const
Function EdgeContainingPoint()
Definition: shape.h:43
virtual const VECTOR2I GetPoint(int aIndex) const =0
virtual void Move(const VECTOR2I &aVector)=0
SHAPE_TYPE
Enum SHAPE_TYPE Lists all supported shapes.
Definition: shape.h:41
circle
Definition: shape.h:47
SHAPE_TYPE Type() const
Function Type()
Definition: shape.h:98
axis-aligned rectangle
Definition: shape.h:44
SHAPE_BASE(SHAPE_TYPE aType)
Constructor.
Definition: shape.h:85
virtual void Rotate(double aAngle, const VECTOR2I &aCenter={ 0, 0 })=0
Function Rotate.