2015-07-02 14:11:02 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2022-01-20 20:54:22 +00:00
|
|
|
* Copyright (C) 2015-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2015-07-02 14:11:02 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2018-05-03 17:52:42 +00:00
|
|
|
#ifndef __SHAPE_SIMPLE_H
|
|
|
|
#define __SHAPE_SIMPLE_H
|
2015-07-02 14:11:02 +00:00
|
|
|
|
|
|
|
#include <geometry/seg.h>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <geometry/shape.h>
|
2015-07-02 14:11:02 +00:00
|
|
|
#include <geometry/shape_line_chain.h>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <math/box2.h>
|
|
|
|
#include <math/vector2d.h>
|
2015-07-02 14:11:02 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Represent a simple polygon consisting of a zero-thickness closed chain of
|
2015-07-02 14:11:02 +00:00
|
|
|
* connected line segments.
|
|
|
|
*
|
|
|
|
* Internally the vertices are held in a SHAPE_LINE_CHAIN, please note that
|
|
|
|
* there is a "virtual" line segment between the last and first vertex.
|
|
|
|
*/
|
2020-09-04 23:08:18 +00:00
|
|
|
|
|
|
|
class SHAPE_SIMPLE : public SHAPE_LINE_CHAIN_BASE
|
2015-07-02 14:11:02 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Create an empty polygon.
|
2015-07-02 14:11:02 +00:00
|
|
|
*/
|
2018-05-03 17:52:42 +00:00
|
|
|
SHAPE_SIMPLE() :
|
2020-09-04 23:08:18 +00:00
|
|
|
SHAPE_LINE_CHAIN_BASE( SH_SIMPLE )
|
2015-07-02 14:11:02 +00:00
|
|
|
{
|
|
|
|
m_points.SetClosed( true );
|
|
|
|
}
|
|
|
|
|
2020-06-21 14:58:44 +00:00
|
|
|
SHAPE_SIMPLE( const SHAPE_LINE_CHAIN& aPoly ) :
|
2020-09-04 23:08:18 +00:00
|
|
|
SHAPE_LINE_CHAIN_BASE( SH_SIMPLE ),
|
2020-06-21 14:58:44 +00:00
|
|
|
m_points( aPoly )
|
|
|
|
{
|
|
|
|
m_points.SetClosed( true );
|
|
|
|
}
|
|
|
|
|
2018-05-03 17:52:42 +00:00
|
|
|
SHAPE_SIMPLE( const SHAPE_SIMPLE& aOther ) :
|
2020-09-04 23:08:18 +00:00
|
|
|
SHAPE_LINE_CHAIN_BASE( SH_SIMPLE ), m_points( aOther.m_points )
|
2015-07-02 14:11:02 +00:00
|
|
|
{}
|
|
|
|
|
2016-09-25 17:06:49 +00:00
|
|
|
SHAPE* Clone() const override
|
2015-07-02 14:11:02 +00:00
|
|
|
{
|
2018-05-03 17:52:42 +00:00
|
|
|
return new SHAPE_SIMPLE( *this );
|
2015-07-02 14:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Remove all points from the polygon.
|
2015-07-02 14:11:02 +00:00
|
|
|
*/
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
m_points.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @copydoc SHAPE::BBox()
|
2016-09-25 17:06:49 +00:00
|
|
|
const BOX2I BBox( int aClearance = 0 ) const override
|
2015-07-02 14:11:02 +00:00
|
|
|
{
|
|
|
|
return m_points.BBox( aClearance );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Return the number of points (vertices) in this polygon.
|
2015-07-02 14:11:02 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @return number of points.
|
2015-07-02 14:11:02 +00:00
|
|
|
*/
|
|
|
|
int PointCount() const
|
|
|
|
{
|
|
|
|
return m_points.PointCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Return a const reference to a given point in the polygon.
|
|
|
|
*
|
|
|
|
* Negative indices count from the end of the point list, e.g. -1 means "last point", -2 means
|
|
|
|
* "second to last point" and so on.
|
2015-07-02 14:11:02 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aIndex is the index of the point.
|
|
|
|
* @return the const reference to the point.
|
2015-07-02 14:11:02 +00:00
|
|
|
*/
|
|
|
|
const VECTOR2I& CPoint( int aIndex ) const
|
|
|
|
{
|
|
|
|
return m_points.CPoint( aIndex );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Return a given point as a vector with elements of type double.
|
2015-07-02 14:11:02 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aIndex is the index of the point.
|
|
|
|
* @return the point with elements of type double.
|
2015-07-02 14:11:02 +00:00
|
|
|
*/
|
|
|
|
const VECTOR2D CDPoint( int aIndex ) const
|
|
|
|
{
|
|
|
|
const VECTOR2I& v = CPoint( aIndex );
|
|
|
|
return VECTOR2D( v.x, v.y );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Return the list of vertices defining this simple polygon.
|
2015-07-02 14:11:02 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @return the list of vertices defining this simple polygon.
|
2015-07-02 14:11:02 +00:00
|
|
|
*/
|
|
|
|
const SHAPE_LINE_CHAIN& Vertices() const
|
|
|
|
{
|
|
|
|
return m_points;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Append a new point at the end of the polygon.
|
2015-07-02 14:11:02 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aX is X coordinate of the new point.
|
|
|
|
* @param aY is Y coordinate of the new point.
|
2015-07-02 14:11:02 +00:00
|
|
|
*/
|
|
|
|
void Append( int aX, int aY )
|
|
|
|
{
|
|
|
|
VECTOR2I v( aX, aY );
|
|
|
|
Append( v );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Append a new point at the end of the polygon.
|
2015-07-02 14:11:02 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aP is the new point.
|
2015-07-02 14:11:02 +00:00
|
|
|
*/
|
|
|
|
void Append( const VECTOR2I& aP )
|
|
|
|
{
|
|
|
|
m_points.Append( aP );
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @copydoc SHAPE::Collide()
|
2020-09-28 22:27:33 +00:00
|
|
|
bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
|
|
|
|
VECTOR2I* aLocation = nullptr ) const override
|
2015-07-02 14:11:02 +00:00
|
|
|
{
|
2020-09-28 22:27:33 +00:00
|
|
|
return m_points.Collide( aSeg, aClearance, aActual, aLocation );
|
2020-07-02 16:06:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 20:54:22 +00:00
|
|
|
void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
|
2020-07-02 16:06:09 +00:00
|
|
|
{
|
|
|
|
m_points.Rotate( aAngle, aCenter );
|
2015-07-02 14:11:02 +00:00
|
|
|
}
|
|
|
|
|
2016-09-25 17:06:49 +00:00
|
|
|
void Move( const VECTOR2I& aVector ) override
|
2015-07-02 14:11:02 +00:00
|
|
|
{
|
|
|
|
m_points.Move( aVector );
|
|
|
|
}
|
|
|
|
|
2016-09-25 17:06:49 +00:00
|
|
|
bool IsSolid() const override
|
2015-07-02 14:11:02 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-04 23:08:18 +00:00
|
|
|
virtual const VECTOR2I GetPoint( int aIndex ) const override { return m_points.CPoint(aIndex); }
|
|
|
|
virtual const SEG GetSegment( int aIndex ) const override { return m_points.CSegment(aIndex); }
|
|
|
|
virtual size_t GetPointCount() const override { return m_points.PointCount(); }
|
|
|
|
virtual size_t GetSegmentCount() const override { return m_points.SegmentCount(); }
|
|
|
|
|
|
|
|
bool IsClosed() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-16 03:49:22 +00:00
|
|
|
void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError,
|
|
|
|
ERROR_LOC aErrorLoc ) const override;
|
|
|
|
|
2015-07-02 14:11:02 +00:00
|
|
|
private:
|
|
|
|
// vertices
|
|
|
|
SHAPE_LINE_CHAIN m_points;
|
|
|
|
};
|
|
|
|
|
2018-05-03 17:52:42 +00:00
|
|
|
#endif // __SHAPE_SIMPLE_H
|