| 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> |
| 29 |
#include <geometry/shape_line_chain.h> |
| 30 |
#include <math/box2.h> |
| 31 |
#include <math/vector2d.h> |
| 32 |
|
| 33 |
/** |
| 34 |
* Represent a simple polygon consisting of a zero-thickness closed chain of |
| 35 |
* connected line segments. |
| 36 |
* |
| 37 |
* Internally the vertices are held in a SHAPE_LINE_CHAIN, please note that |
| 38 |
* there is a "virtual" line segment between the last and first vertex. |
| 39 |
*/ |
| 40 |
|
| 41 |
class SHAPE_SIMPLE : public SHAPE_LINE_CHAIN_BASE |
| 42 |
{ |
| 43 |
public: |
| 44 |
/** |
| 45 |
* Create an empty polygon. |
| 46 |
*/ |
| 47 |
SHAPE_SIMPLE() : |
| 48 |
SHAPE_LINE_CHAIN_BASE( SH_SIMPLE ) |
| 49 |
{ |
| 50 |
m_points.SetClosed( true ); |
| 51 |
} |
| 52 |
|
| 53 |
SHAPE_SIMPLE( const SHAPE_LINE_CHAIN& aPoly ) : |
| 54 |
SHAPE_LINE_CHAIN_BASE( SH_SIMPLE ), |
| 55 |
m_points( aPoly ) |
| 56 |
{ |
| 57 |
m_points.SetClosed( true ); |
| 58 |
} |
| 59 |
|
| 60 |
SHAPE_SIMPLE( const SHAPE_SIMPLE& aOther ) : |
| 61 |
SHAPE_LINE_CHAIN_BASE( SH_SIMPLE ), m_points( aOther.m_points ) |
| 62 |
{} |
| 63 |
|
| 64 |
SHAPE* Clone() const override |
| 65 |
{ |
| 66 |
return new SHAPE_SIMPLE( *this ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Remove all points from the polygon. |
| 71 |
*/ |
| 72 |
void Clear() |
| 73 |
{ |
| 74 |
m_points.Clear(); |
| 75 |
} |
| 76 |
|
| 77 |
/// @copydoc SHAPE::BBox() |
| 78 |
const BOX2I BBox( int aClearance = 0 ) const override |
| 79 |
{ |
| 80 |
return m_points.BBox( aClearance ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Return the number of points (vertices) in this polygon. |
| 85 |
* |
| 86 |
* @return number of points. |
| 87 |
*/ |
| 88 |
int PointCount() const |
| 89 |
{ |
| 90 |
return m_points.PointCount(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Return a const reference to a given point in the polygon. |
| 95 |
* |
| 96 |
* Negative indices count from the end of the point list, e.g. -1 means "last point", -2 means |
| 97 |
* "second to last point" and so on. |
| 98 |
* |
| 99 |
* @param aIndex is the index of the point. |
| 100 |
* @return the const reference to the point. |
| 101 |
*/ |
| 102 |
const VECTOR2I& CPoint( int aIndex ) const |
| 103 |
{ |
| 104 |
return m_points.CPoint( aIndex ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Return a given point as a vector with elements of type double. |
| 109 |
* |
| 110 |
* @param aIndex is the index of the point. |
| 111 |
* @return the point with elements of type double. |
| 112 |
*/ |
| 113 |
const VECTOR2D CDPoint( int aIndex ) const |
| 114 |
{ |
| 115 |
const VECTOR2I& v = CPoint( aIndex ); |
| 116 |
return VECTOR2D( v.x, v.y ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Return the list of vertices defining this simple polygon. |
| 121 |
* |
| 122 |
* @return the list of vertices defining this simple polygon. |
| 123 |
*/ |
| 124 |
const SHAPE_LINE_CHAIN& Vertices() const |
| 125 |
{ |
| 126 |
return m_points; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Append a new point at the end of the polygon. |
| 131 |
* |
| 132 |
* @param aX is X coordinate of the new point. |
| 133 |
* @param aY is Y coordinate of the new point. |
| 134 |
*/ |
| 135 |
void Append( int aX, int aY ) |
| 136 |
{ |
| 137 |
VECTOR2I v( aX, aY ); |
| 138 |
Append( v ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Append a new point at the end of the polygon. |
| 143 |
* |
| 144 |
* @param aP is the new point. |
| 145 |
*/ |
| 146 |
void Append( const VECTOR2I& aP ) |
| 147 |
{ |
| 148 |
m_points.Append( aP ); |
| 149 |
} |
| 150 |
|
| 151 |
/// @copydoc SHAPE::Collide() |
| 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 |
| 185 |
SHAPE_LINE_CHAIN m_points; |
| 186 |
}; |
| 187 |
|
| 188 |
#endif // __SHAPE_SIMPLE_H |