Horizon
shape.h
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 <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 
41 enum SHAPE_TYPE
42 {
43  SH_RECT = 0,
44  SH_SEGMENT,
45  SH_LINE_CHAIN,
46  SH_CIRCLE,
47  SH_SIMPLE,
48  SH_POLY_SET,
49  SH_COMPOUND,
50  SH_ARC,
51  SH_NULL,
52  SH_POLY_SET_TRIANGLE
53 };
54 
55 static inline wxString SHAPE_TYPE_asString( SHAPE_TYPE a )
56 {
57  switch( a )
58  {
59  case SH_RECT: return wxT( "SH_RECT" );
60  case SH_SEGMENT: return wxT( "SH_SEGMENT" );
61  case SH_LINE_CHAIN: return wxT( "SH_LINE_CHAIN" );
62  case SH_CIRCLE: return wxT( "SH_CIRCLE" );
63  case SH_SIMPLE: return wxT( "SH_SIMPLE" );
64  case SH_POLY_SET: return wxT( "SH_POLY_SET" );
65  case SH_COMPOUND: return wxT( "SH_COMPOUND" );
66  case SH_ARC: return wxT( "SH_ARC" );
67  case SH_NULL: return wxT( "SH_NULL" );
68  case SH_POLY_SET_TRIANGLE: return wxT( "SH_POLY_SET_TRIANGLE" );
69  }
70 
71  return wxEmptyString; // Just to quiet GCC.
72 }
73 
74 class SHAPE;
75 
77 {
78 public:
82  SHAPE_BASE( SHAPE_TYPE aType ) :
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:
110  SHAPE_TYPE m_type;
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:
236  typedef VECTOR2I::extended_type ecoord;
237 };
238 
239 
241 {
242 public:
243  SHAPE_LINE_CHAIN_BASE( SHAPE_TYPE aType ) :
244  SHAPE( aType )
245  {
246  }
247 
248  virtual ~SHAPE_LINE_CHAIN_BASE()
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  virtual BOX2I* GetCachedBBox() const { return nullptr; }
313 };
314 
315 #endif // __SHAPE_H
Definition: seg.h:41
Definition: shape.h:77
SHAPE_BASE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition: shape.h:82
SHAPE_TYPE m_type
< type of our shape
Definition: shape.h:110
SHAPE_TYPE Type() const
Return the type of the shape.
Definition: shape.h:94
Definition: shape.h:241
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.
Definition: shape_line_chain.cpp:1535
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.
Definition: shape_line_chain.cpp:301
bool PointOnEdge(const VECTOR2I &aP, int aAccuracy=0) const
Check if point aP lies on an edge or vertex of the line chain.
Definition: shape_line_chain.cpp:1587
int EdgeContainingPoint(const VECTOR2I &aP, int aAccuracy=0) const
Check if point aP lies on an edge or vertex of the line chain.
Definition: shape_line_chain.cpp:1593
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
Definition: shape_line_chain.h:81
An abstract shape on 2D plane.
Definition: shape.h:117
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 VECTOR2I Centre() const
Compute a center-of-mass of the shape.
Definition: shape.h:216
virtual SHAPE * Clone() const
Return a dynamically allocated copy of the shape.
Definition: shape.h:139
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition: shape.h:127
static const int MIN_PRECISION_IU
This is the minimum precision for all the points in a shape.
Definition: shape.h:122
bool IsNull() const
Return true if the shape is a null shape.
Definition: shape.h:150
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 void Rotate(double aAngle, const VECTOR2I &aCenter={ 0, 0 })=0
Definition: wx_compat.h:13