KiCad PCB EDA Suite
shape_simple.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) 2015-2021 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #ifndef __SHAPE_SIMPLE_H
25 #define __SHAPE_SIMPLE_H
26 
27 #include <geometry/seg.h>
28 #include <geometry/shape.h>
30 #include <math/box2.h>
31 #include <math/vector2d.h>
32 
42 {
43 public:
49  {
50  m_points.SetClosed( true );
51  }
52 
53  SHAPE_SIMPLE( const SHAPE_LINE_CHAIN& aPoly ) :
55  m_points( aPoly )
56  {
57  m_points.SetClosed( true );
58  }
59 
60  SHAPE_SIMPLE( const SHAPE_SIMPLE& aOther ) :
62  {}
63 
64  SHAPE* Clone() const override
65  {
66  return new SHAPE_SIMPLE( *this );
67  }
68 
72  void Clear()
73  {
74  m_points.Clear();
75  }
76 
78  const BOX2I BBox( int aClearance = 0 ) const override
79  {
80  return m_points.BBox( aClearance );
81  }
82 
88  int PointCount() const
89  {
90  return m_points.PointCount();
91  }
92 
102  const VECTOR2I& CPoint( int aIndex ) const
103  {
104  return m_points.CPoint( aIndex );
105  }
106 
113  const VECTOR2D CDPoint( int aIndex ) const
114  {
115  const VECTOR2I& v = CPoint( aIndex );
116  return VECTOR2D( v.x, v.y );
117  }
118 
124  const SHAPE_LINE_CHAIN& Vertices() const
125  {
126  return m_points;
127  }
128 
135  void Append( int aX, int aY )
136  {
137  VECTOR2I v( aX, aY );
138  Append( v );
139  }
140 
146  void Append( const VECTOR2I& aP )
147  {
148  m_points.Append( aP );
149  }
150 
152  bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
153  VECTOR2I* aLocation = nullptr ) const override
154  {
155  return m_points.Collide( aSeg, aClearance, aActual, aLocation );
156  }
157 
158  void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
159  {
160  m_points.Rotate( aAngle, aCenter );
161  }
162 
163  void Move( const VECTOR2I& aVector ) override
164  {
165  m_points.Move( aVector );
166  }
167 
168  bool IsSolid() const override
169  {
170  return true;
171  }
172 
173  virtual const VECTOR2I GetPoint( int aIndex ) const override { return m_points.CPoint(aIndex); }
174  virtual const SEG GetSegment( int aIndex ) const override { return m_points.CSegment(aIndex); }
175  virtual size_t GetPointCount() const override { return m_points.PointCount(); }
176  virtual size_t GetSegmentCount() const override { return m_points.SegmentCount(); }
177 
178  bool IsClosed() const override
179  {
180  return true;
181  }
182 
183 private:
184  // vertices
186 };
187 
188 #endif // __SHAPE_SIMPLE_H
virtual const SEG GetSegment(int aIndex) const override
Definition: shape_simple.h:174
bool IsClosed() const override
Definition: shape_simple.h:178
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
Definition: shape_simple.h:152
Represent a simple polygon consisting of a zero-thickness closed chain of connected line segments.
Definition: shape_simple.h:41
const VECTOR2I & CPoint(int aIndex) const
Return a const reference to a given point in the polygon.
Definition: shape_simple.h:102
virtual const VECTOR2I GetPoint(int aIndex) const override
Definition: shape_simple.h:173
void Move(const VECTOR2I &aVector) override
virtual size_t GetPointCount() const override
Definition: shape_simple.h:175
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Definition: shape_simple.h:78
int PointCount() const
Return the number of points (vertices) in this line chain.
void Append(const VECTOR2I &aP)
Append a new point at the end of the polygon.
Definition: shape_simple.h:146
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
Definition: shape_simple.h:64
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of 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.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
const VECTOR2D CDPoint(int aIndex) const
Return a given point as a vector with elements of type double.
Definition: shape_simple.h:113
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:622
SHAPE_SIMPLE(const SHAPE_SIMPLE &aOther)
Definition: shape_simple.h:60
void Rotate(double aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
Definition: shape_simple.h:158
const SHAPE_LINE_CHAIN & Vertices() const
Return the list of vertices defining this simple polygon.
Definition: shape_simple.h:124
SHAPE_SIMPLE()
Create an empty polygon.
Definition: shape_simple.h:47
An abstract shape on 2D plane.
Definition: shape.h:116
int SegmentCount() const
Return the number of segments in this line chain.
bool IsSolid() const override
Definition: shape_simple.h:168
void Clear()
Remove all points from the polygon.
Definition: shape_simple.h:72
void Rotate(double aAngle, const VECTOR2I &aCenter=VECTOR2I(0, 0)) override
Rotate all vertices by a given angle.
Definition: seg.h:40
virtual size_t GetSegmentCount() const override
Definition: shape_simple.h:176
SHAPE_LINE_CHAIN m_points
Definition: shape_simple.h:185
const SEG CSegment(int aIndex) const
Return a constant copy of the aIndex segment in the line chain.
Represent a polyline (an zero-thickness chain of connected line segments).
int PointCount() const
Return the number of points (vertices) in this polygon.
Definition: shape_simple.h:88
void Clear()
Remove all points from the line chain.
SHAPE_SIMPLE(const SHAPE_LINE_CHAIN &aPoly)
Definition: shape_simple.h:53
simple polygon
Definition: shape.h:47
void Append(int aX, int aY)
Append a new point at the end of the polygon.
Definition: shape_simple.h:135
void Move(const VECTOR2I &aVector) override
Definition: shape_simple.h:163