2013-09-10 11:43:09 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
2022-01-20 20:54:22 +00:00
|
|
|
* Copyright (C) 2013-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-09-10 11:43:09 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __SHAPE_LINE_CHAIN
|
|
|
|
#define __SHAPE_LINE_CHAIN
|
|
|
|
|
2020-01-07 17:12:59 +00:00
|
|
|
|
|
|
|
#include <clipper.hpp>
|
2022-10-19 23:25:45 +00:00
|
|
|
#include <clipper2/clipper.h>
|
2013-09-10 11:43:09 +00:00
|
|
|
#include <geometry/seg.h>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <geometry/shape.h>
|
2019-03-26 02:55:59 +00:00
|
|
|
#include <geometry/shape_arc.h>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <math/vector2d.h>
|
2019-01-31 11:53:01 +00:00
|
|
|
|
2021-06-19 09:56:22 +00:00
|
|
|
/**
|
|
|
|
* Holds information on each point of a SHAPE_LINE_CHAIN that is retrievable
|
|
|
|
* after an operation with ClipperLib
|
|
|
|
*/
|
|
|
|
struct CLIPPER_Z_VALUE
|
|
|
|
{
|
2021-06-01 20:48:16 +00:00
|
|
|
CLIPPER_Z_VALUE()
|
|
|
|
{
|
|
|
|
m_FirstArcIdx = -1;
|
|
|
|
m_SecondArcIdx = -1;
|
|
|
|
}
|
|
|
|
|
2021-06-19 09:56:22 +00:00
|
|
|
CLIPPER_Z_VALUE( const std::pair<ssize_t, ssize_t> aShapeIndices, ssize_t aOffset = 0 )
|
|
|
|
{
|
|
|
|
m_FirstArcIdx = aShapeIndices.first;
|
|
|
|
m_SecondArcIdx = aShapeIndices.second;
|
|
|
|
|
|
|
|
auto offsetVal = [&]( ssize_t& aVal )
|
|
|
|
{
|
|
|
|
if( aVal >= 0 )
|
|
|
|
aVal += aOffset;
|
|
|
|
};
|
|
|
|
|
|
|
|
offsetVal( m_FirstArcIdx );
|
|
|
|
offsetVal( m_SecondArcIdx );
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t m_FirstArcIdx;
|
|
|
|
ssize_t m_SecondArcIdx;
|
|
|
|
};
|
|
|
|
|
2013-09-10 11:43:09 +00:00
|
|
|
|
|
|
|
/**
|
2021-11-05 18:06:22 +00:00
|
|
|
* Represent a polyline containing arcs as well as line segments: A chain of connected line and/or
|
|
|
|
* arc segments.
|
|
|
|
*
|
|
|
|
* The arc shapes are piecewise approximated for the purpose of boolean operations but are used as
|
|
|
|
* arcs when doing collision checks.
|
2013-09-10 11:43:09 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* It is purposely not named "polyline" to avoid confusion with the existing CPolyLine
|
|
|
|
* in Pcbnew.
|
2013-09-10 11:43:09 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @note The SHAPE_LINE_CHAIN class shall not be used for polygons!
|
2013-09-10 11:43:09 +00:00
|
|
|
*/
|
2020-09-04 23:08:18 +00:00
|
|
|
class SHAPE_LINE_CHAIN : public SHAPE_LINE_CHAIN_BASE
|
2013-10-14 18:40:36 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
typedef std::vector<VECTOR2I>::iterator point_iter;
|
|
|
|
typedef std::vector<VECTOR2I>::const_iterator point_citer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Represent an intersection between two line segments
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
|
|
|
struct INTERSECTION
|
|
|
|
{
|
2023-03-24 13:02:13 +00:00
|
|
|
/// Point of intersection between our and their.
|
2013-10-14 18:40:36 +00:00
|
|
|
VECTOR2I p;
|
2021-07-01 17:46:17 +00:00
|
|
|
|
2023-03-24 13:02:13 +00:00
|
|
|
/// Index of the intersecting corner/segment in the 'our' (== this) line.
|
2021-05-27 21:36:12 +00:00
|
|
|
int index_our;
|
2021-07-01 17:46:17 +00:00
|
|
|
|
2023-03-24 13:02:13 +00:00
|
|
|
/// index of the intersecting corner/segment in the 'their' (Intersect() method
|
|
|
|
/// parameter) line.
|
2021-05-27 21:36:12 +00:00
|
|
|
int index_their;
|
2021-07-01 17:46:17 +00:00
|
|
|
|
2023-03-24 13:02:13 +00:00
|
|
|
/// When true, the corner [index_our] of the 'our' line lies exactly on 'their' line.
|
2021-05-27 21:36:12 +00:00
|
|
|
bool is_corner_our;
|
2021-07-01 17:46:17 +00:00
|
|
|
|
2023-03-24 13:02:13 +00:00
|
|
|
/// When true, the corner [index_their] of the 'their' line lies exactly on 'our' line.
|
|
|
|
/// Note that when both is_corner_our and is_corner_their are set, the line chains touch
|
|
|
|
/// with with corners.
|
2021-05-27 21:36:12 +00:00
|
|
|
bool is_corner_their;
|
2021-07-01 17:46:17 +00:00
|
|
|
|
2023-03-24 13:02:13 +00:00
|
|
|
/// Auxiliary flag to avoid copying intersection info to intersection refining code,
|
|
|
|
/// used by the refining code (e.g. hull handling stuff in the P&S) to reject false
|
|
|
|
/// intersection points.
|
2021-05-27 21:36:12 +00:00
|
|
|
bool valid;
|
2022-03-24 17:16:49 +00:00
|
|
|
|
|
|
|
INTERSECTION() :
|
|
|
|
index_our( -1 ),
|
|
|
|
index_their( -1 ),
|
|
|
|
is_corner_our( false ),
|
|
|
|
is_corner_their( false ),
|
|
|
|
valid( false )
|
|
|
|
{
|
|
|
|
}
|
2013-10-14 18:40:36 +00:00
|
|
|
};
|
|
|
|
|
2020-04-14 23:11:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A dynamic state checking if a point lies within polygon with a dynamically built outline (
|
|
|
|
* with each piece of the outline added by AddPolyline ()
|
|
|
|
*/
|
|
|
|
class POINT_INSIDE_TRACKER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
POINT_INSIDE_TRACKER( const VECTOR2I& aPoint );
|
|
|
|
|
|
|
|
void AddPolyline( const SHAPE_LINE_CHAIN& aPolyline );
|
|
|
|
bool IsInside();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
bool processVertex ( const VECTOR2I& ip, const VECTOR2I& ipNext );
|
|
|
|
|
|
|
|
VECTOR2I m_point;
|
|
|
|
VECTOR2I m_lastPoint;
|
|
|
|
VECTOR2I m_firstPoint;
|
|
|
|
bool m_finished;
|
|
|
|
int m_state;
|
|
|
|
int m_count;
|
|
|
|
};
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
typedef std::vector<INTERSECTION> INTERSECTIONS;
|
|
|
|
|
2019-03-24 13:54:56 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Initialize an empty line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
2021-07-03 19:43:40 +00:00
|
|
|
SHAPE_LINE_CHAIN() :
|
|
|
|
SHAPE_LINE_CHAIN_BASE( SH_LINE_CHAIN ),
|
|
|
|
m_closed( false ),
|
|
|
|
m_width( 0 )
|
2013-10-14 18:40:36 +00:00
|
|
|
{}
|
|
|
|
|
2021-07-03 19:43:40 +00:00
|
|
|
SHAPE_LINE_CHAIN( const SHAPE_LINE_CHAIN& aShape ) :
|
|
|
|
SHAPE_LINE_CHAIN_BASE( SH_LINE_CHAIN ),
|
|
|
|
m_points( aShape.m_points ),
|
|
|
|
m_shapes( aShape.m_shapes ),
|
|
|
|
m_arcs( aShape.m_arcs ),
|
|
|
|
m_closed( aShape.m_closed ),
|
|
|
|
m_width( aShape.m_width ),
|
|
|
|
m_bbox( aShape.m_bbox )
|
2013-10-14 18:40:36 +00:00
|
|
|
{}
|
|
|
|
|
2021-07-03 19:43:40 +00:00
|
|
|
SHAPE_LINE_CHAIN( const std::vector<int>& aV );
|
2020-02-19 17:11:07 +00:00
|
|
|
|
2021-07-03 19:43:40 +00:00
|
|
|
SHAPE_LINE_CHAIN( const std::vector<VECTOR2I>& aV, bool aClosed = false ) :
|
|
|
|
SHAPE_LINE_CHAIN_BASE( SH_LINE_CHAIN ),
|
|
|
|
m_closed( aClosed ),
|
|
|
|
m_width( 0 )
|
2019-03-23 18:26:44 +00:00
|
|
|
{
|
|
|
|
m_points = aV;
|
2021-05-06 22:10:39 +00:00
|
|
|
m_shapes = std::vector<std::pair<ssize_t, ssize_t>>( aV.size(), SHAPES_ARE_PT );
|
2019-03-23 18:26:44 +00:00
|
|
|
}
|
|
|
|
|
2021-07-03 19:43:40 +00:00
|
|
|
SHAPE_LINE_CHAIN( const SHAPE_ARC& aArc, bool aClosed = false ) :
|
|
|
|
SHAPE_LINE_CHAIN_BASE( SH_LINE_CHAIN ),
|
|
|
|
m_closed( aClosed ),
|
|
|
|
m_width( 0 )
|
2019-05-17 00:13:21 +00:00
|
|
|
{
|
|
|
|
m_points = aArc.ConvertToPolyline().CPoints();
|
|
|
|
m_arcs.emplace_back( aArc );
|
2021-11-05 18:06:22 +00:00
|
|
|
m_arcs.back().SetWidth( 0 );
|
|
|
|
m_shapes = std::vector<std::pair<ssize_t, ssize_t>>( m_points.size(), { 0, SHAPE_IS_PT } );
|
2019-05-17 00:13:21 +00:00
|
|
|
}
|
|
|
|
|
2021-07-03 19:43:40 +00:00
|
|
|
SHAPE_LINE_CHAIN( const ClipperLib::Path& aPath,
|
|
|
|
const std::vector<CLIPPER_Z_VALUE>& aZValueBuffer,
|
2021-06-19 09:56:22 +00:00
|
|
|
const std::vector<SHAPE_ARC>& aArcBuffer );
|
2018-09-12 10:19:11 +00:00
|
|
|
|
2022-10-19 23:25:45 +00:00
|
|
|
SHAPE_LINE_CHAIN( const Clipper2Lib::Path64& aPath,
|
|
|
|
const std::vector<CLIPPER_Z_VALUE>& aZValueBuffer,
|
|
|
|
const std::vector<SHAPE_ARC>& aArcBuffer );
|
|
|
|
|
2019-03-24 13:54:56 +00:00
|
|
|
virtual ~SHAPE_LINE_CHAIN()
|
2013-10-14 18:40:36 +00:00
|
|
|
{}
|
|
|
|
|
2021-11-05 18:06:22 +00:00
|
|
|
/**
|
|
|
|
* Check if point \a aP lies closer to us than \a aClearance.
|
|
|
|
*
|
|
|
|
* Note: This is overridden as we want to ensure we test collisions with the arcs in this chain
|
|
|
|
* as true arcs rather than segment approximations.
|
|
|
|
*
|
|
|
|
* @param aP the point to check for collisions with
|
|
|
|
* @param aClearance minimum distance that does not qualify as a collision.
|
|
|
|
* @param aActual an optional pointer to an int to store the actual distance in the event
|
|
|
|
* of a collision.
|
|
|
|
* @return true, when a collision has been found
|
|
|
|
*/
|
|
|
|
virtual bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
|
|
|
|
VECTOR2I* aLocation = nullptr ) const override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if segment \a aSeg lies closer to us than \a aClearance.
|
|
|
|
*
|
|
|
|
* Note: This is overridden as we want to ensure we test collisions with the arcs in this chain
|
|
|
|
* as true arcs rather than segment approximations.
|
|
|
|
*
|
|
|
|
* @param aSeg the segment to check for collisions with
|
|
|
|
* @param aClearance minimum distance that does not qualify as a collision.
|
|
|
|
* @param aActual an optional pointer to an int to store the actual distance in the event
|
|
|
|
* of a collision.
|
|
|
|
* @return true, when a collision has been found
|
|
|
|
*/
|
|
|
|
virtual bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
|
|
|
|
VECTOR2I* aLocation = nullptr ) const override;
|
|
|
|
|
2021-07-03 19:43:40 +00:00
|
|
|
SHAPE_LINE_CHAIN& operator=( const SHAPE_LINE_CHAIN& ) = default;
|
2020-01-08 01:49:11 +00:00
|
|
|
|
2016-09-24 18:53:15 +00:00
|
|
|
SHAPE* Clone() const override;
|
2014-05-14 09:45:01 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Remove all points from the line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
m_points.clear();
|
2019-03-26 02:55:59 +00:00
|
|
|
m_arcs.clear();
|
|
|
|
m_shapes.clear();
|
2013-10-14 18:40:36 +00:00
|
|
|
m_closed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Mark the line chain as closed (i.e. with a segment connecting the last point with
|
2013-10-14 18:40:36 +00:00
|
|
|
* the first point).
|
2021-07-01 17:46:17 +00:00
|
|
|
*
|
2013-10-14 18:40:36 +00:00
|
|
|
* @param aClosed: whether the line chain is to be closed or not.
|
|
|
|
*/
|
|
|
|
void SetClosed( bool aClosed )
|
|
|
|
{
|
|
|
|
m_closed = aClosed;
|
2021-11-17 21:51:21 +00:00
|
|
|
mergeFirstLastPointIfNeeded();
|
2013-10-14 18:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* @return true when our line is closed.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
2020-09-04 23:08:18 +00:00
|
|
|
bool IsClosed() const override
|
2013-10-14 18:40:36 +00:00
|
|
|
{
|
|
|
|
return m_closed;
|
|
|
|
}
|
|
|
|
|
2020-01-17 14:19:27 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Set the width of all segments in the chain.
|
|
|
|
*
|
|
|
|
* @param aWidth is the width in internal units.
|
2020-01-17 14:19:27 +00:00
|
|
|
*/
|
|
|
|
void SetWidth( int aWidth )
|
|
|
|
{
|
|
|
|
m_width = aWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Get the current width of the segments in the chain.
|
|
|
|
*
|
|
|
|
* @return the width in internal units.
|
2020-01-17 14:19:27 +00:00
|
|
|
*/
|
|
|
|
int Width() const
|
|
|
|
{
|
|
|
|
return m_width;
|
|
|
|
}
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Return the number of segments in this line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @return the number of segments.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
|
|
|
int SegmentCount() const
|
|
|
|
{
|
|
|
|
int c = m_points.size() - 1;
|
2022-07-09 03:34:49 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
if( m_closed )
|
|
|
|
c++;
|
|
|
|
|
|
|
|
return std::max( 0, c );
|
|
|
|
}
|
|
|
|
|
2021-01-04 02:37:24 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Return the number of shapes (line segments or arcs) in this line chain.
|
|
|
|
*
|
|
|
|
* This is kind of like SegmentCount() but will only count arcs as 1 segment.
|
|
|
|
*
|
|
|
|
* @return ArcCount() + the number of non-arc segments.
|
2021-01-04 02:37:24 +00:00
|
|
|
*/
|
|
|
|
int ShapeCount() const;
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Return the number of points (vertices) in this line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @return the number of points.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
|
|
|
int PointCount() const
|
|
|
|
{
|
|
|
|
return m_points.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Return a copy of the aIndex-th segment in the line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aIndex is the index of the segment in the line chain. Negative values are counted
|
|
|
|
* from the end (i.e. -1 means the last segment in the line chain).
|
|
|
|
* @return a segment at the \a aIndex in the line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
|
|
|
SEG Segment( int aIndex )
|
|
|
|
{
|
|
|
|
if( aIndex < 0 )
|
|
|
|
aIndex += SegmentCount();
|
|
|
|
|
2013-11-01 12:56:20 +00:00
|
|
|
if( aIndex == (int)( m_points.size() - 1 ) && m_closed )
|
2013-10-14 18:40:36 +00:00
|
|
|
return SEG( m_points[aIndex], m_points[0], aIndex );
|
|
|
|
else
|
|
|
|
return SEG( m_points[aIndex], m_points[aIndex + 1], aIndex );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Return a constant copy of the \a aIndex segment in the line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aIndex is the index of the segment in the line chain. Negative values are counted
|
|
|
|
* from the end (i.e. -1 means the last segment in the line chain).
|
|
|
|
* @return a segment at \a aIndex in the line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
|
|
|
const SEG CSegment( int aIndex ) const
|
|
|
|
{
|
|
|
|
if( aIndex < 0 )
|
|
|
|
aIndex += SegmentCount();
|
|
|
|
|
2013-11-01 12:56:20 +00:00
|
|
|
if( aIndex == (int)( m_points.size() - 1 ) && m_closed )
|
2020-12-20 18:59:07 +00:00
|
|
|
return SEG( m_points[aIndex], m_points[0], aIndex );
|
2013-10-14 18:40:36 +00:00
|
|
|
else
|
2020-12-20 18:59:07 +00:00
|
|
|
return SEG( m_points[aIndex], m_points[aIndex + 1], aIndex );
|
2013-10-14 18:40:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 02:37:24 +00:00
|
|
|
/**
|
2021-08-08 17:28:52 +00:00
|
|
|
* Return the vertex index of the next shape in the chain, or -1 if \a aPointIndex is the
|
|
|
|
* last shape.
|
2021-07-01 17:46:17 +00:00
|
|
|
*
|
2021-08-08 17:28:52 +00:00
|
|
|
* If \a aPointIndex is the start of a segment, this will be ( aPointIndex + 1 ). If
|
|
|
|
* \a aPointIndex is part of an arc, this will be the index of the start of the next shape after
|
|
|
|
* the arc, in other words, the last point of the arc.
|
2021-07-01 17:46:17 +00:00
|
|
|
*
|
|
|
|
* @param aPointIndex is a vertex in the chain.
|
|
|
|
* @param aForwards is true if the next shape is desired, false for previous shape.
|
2021-08-08 17:28:52 +00:00
|
|
|
* @return the vertex index of the start of the next shape after aPoint's shape or -1 if
|
|
|
|
* the end was reached.
|
2021-01-04 02:37:24 +00:00
|
|
|
*/
|
|
|
|
int NextShape( int aPointIndex, bool aForwards = true ) const;
|
|
|
|
|
|
|
|
int PrevShape( int aPointIndex ) const
|
|
|
|
{
|
|
|
|
return NextShape( aPointIndex, false );
|
|
|
|
}
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Move a point to a specific location.
|
|
|
|
*
|
|
|
|
* @param aIndex is the index of the point to move.
|
|
|
|
* @param aPos is the new absolute location of the point.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
2021-05-06 22:10:39 +00:00
|
|
|
void SetPoint( int aIndex, const VECTOR2I& aPos );
|
2013-10-14 18:40:36 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Return a reference to a given point in the line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aIndex is the index of the point.
|
|
|
|
* @return a const reference to the point.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
|
|
|
const VECTOR2I& CPoint( int aIndex ) const
|
|
|
|
{
|
|
|
|
if( aIndex < 0 )
|
|
|
|
aIndex += PointCount();
|
2015-08-01 10:20:23 +00:00
|
|
|
else if( aIndex >= PointCount() )
|
2015-07-27 19:45:57 +00:00
|
|
|
aIndex -= PointCount();
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
return m_points[aIndex];
|
|
|
|
}
|
|
|
|
|
2018-08-07 11:13:23 +00:00
|
|
|
const std::vector<VECTOR2I>& CPoints() const
|
|
|
|
{
|
|
|
|
return m_points;
|
|
|
|
}
|
|
|
|
|
2018-02-19 10:20:52 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Return the last point in the line chain.
|
2018-02-19 10:20:52 +00:00
|
|
|
*/
|
|
|
|
const VECTOR2I& CLastPoint() const
|
|
|
|
{
|
2021-05-06 22:10:39 +00:00
|
|
|
return m_points[static_cast<size_t>( PointCount() ) - 1];
|
2018-02-19 10:20:52 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* @return the vector of stored arcs.
|
2019-05-17 00:13:21 +00:00
|
|
|
*/
|
|
|
|
const std::vector<SHAPE_ARC>& CArcs() const
|
|
|
|
{
|
|
|
|
return m_arcs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* @return the vector of values indicating shape type and location.
|
2019-05-17 00:13:21 +00:00
|
|
|
*/
|
2021-05-06 22:10:39 +00:00
|
|
|
const std::vector<std::pair<ssize_t, ssize_t>>& CShapes() const
|
2019-05-17 00:13:21 +00:00
|
|
|
{
|
|
|
|
return m_shapes;
|
|
|
|
}
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/// @copydoc SHAPE::BBox()
|
2016-09-24 18:53:15 +00:00
|
|
|
const BOX2I BBox( int aClearance = 0 ) const override
|
2013-10-14 18:40:36 +00:00
|
|
|
{
|
2016-09-01 16:09:06 +00:00
|
|
|
BOX2I bbox;
|
|
|
|
bbox.Compute( m_points );
|
2013-10-14 18:40:36 +00:00
|
|
|
|
2020-01-17 14:19:27 +00:00
|
|
|
if( aClearance != 0 || m_width != 0 )
|
|
|
|
bbox.Inflate( aClearance + m_width );
|
2015-07-02 14:09:43 +00:00
|
|
|
|
2016-09-01 16:09:06 +00:00
|
|
|
return bbox;
|
2013-10-14 18:40:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-20 13:25:16 +00:00
|
|
|
void GenerateBBoxCache() const
|
2019-07-05 22:45:57 +00:00
|
|
|
{
|
|
|
|
m_bbox.Compute( m_points );
|
2020-01-17 14:19:27 +00:00
|
|
|
|
|
|
|
if( m_width != 0 )
|
|
|
|
m_bbox.Inflate( m_width );
|
2019-07-05 22:45:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 14:35:09 +00:00
|
|
|
BOX2I* GetCachedBBox() const override
|
2020-05-14 21:01:39 +00:00
|
|
|
{
|
2021-09-09 14:35:09 +00:00
|
|
|
return &m_bbox;
|
2020-05-14 21:01:39 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Compute the minimum distance between the line chain and a point \a aP.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aP the point.
|
2013-10-14 18:40:36 +00:00
|
|
|
* @return minimum distance.
|
|
|
|
*/
|
2017-10-19 21:14:36 +00:00
|
|
|
int Distance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
|
2013-10-14 18:40:36 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Reverse point order in the line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @return line chain with reversed point order (original A-B-C-D: returned D-C-B-A).
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
|
|
|
const SHAPE_LINE_CHAIN Reverse() const;
|
|
|
|
|
2021-06-19 14:53:49 +00:00
|
|
|
/**
|
|
|
|
* Remove all arc references in the line chain, resulting in a chain formed
|
|
|
|
* only of straight segments. Any arcs in the chain are removed and only the
|
|
|
|
* piecewise linear approximation remains.
|
|
|
|
*/
|
|
|
|
void ClearArcs();
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Return length of the line chain in Euclidean metric.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @return the length of the line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
2019-09-03 23:38:58 +00:00
|
|
|
long long int Length() const;
|
2013-10-14 18:40:36 +00:00
|
|
|
|
2023-05-26 22:03:14 +00:00
|
|
|
/**
|
|
|
|
* Allocate a number of points all at once (for performance).
|
|
|
|
*/
|
|
|
|
void ReservePoints( size_t aSize )
|
|
|
|
{
|
|
|
|
m_points.reserve( aSize );
|
|
|
|
}
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Append a new point at the end of the line chain.
|
2013-10-14 18:40:36 +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.
|
|
|
|
* @param aAllowDuplication set to true to append the new point even if it is the same as
|
|
|
|
* the last entered point, false (default) to skip it if it is the same as the last
|
|
|
|
* entered point.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
2015-06-12 15:11:56 +00:00
|
|
|
void Append( int aX, int aY, bool aAllowDuplication = false )
|
2013-10-14 18:40:36 +00:00
|
|
|
{
|
|
|
|
VECTOR2I v( aX, aY );
|
2015-06-12 15:11:56 +00:00
|
|
|
Append( v, aAllowDuplication );
|
2013-10-14 18:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Append a new point at the end of the line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aP is the new point.
|
|
|
|
* @param aAllowDuplication set to true to append the new point even it is the same as the
|
|
|
|
* last entered point or false (default) to skip it if it is the same as the last
|
|
|
|
* entered point.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
2015-06-12 15:11:56 +00:00
|
|
|
void Append( const VECTOR2I& aP, bool aAllowDuplication = false )
|
2013-10-14 18:40:36 +00:00
|
|
|
{
|
2018-10-09 21:50:20 +00:00
|
|
|
if( m_points.size() == 0 )
|
|
|
|
m_bbox = BOX2I( aP, VECTOR2I( 0, 0 ) );
|
|
|
|
|
2015-06-12 15:11:56 +00:00
|
|
|
if( m_points.size() == 0 || aAllowDuplication || CPoint( -1 ) != aP )
|
2018-10-09 21:50:20 +00:00
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
m_points.push_back( aP );
|
2021-05-06 22:10:39 +00:00
|
|
|
m_shapes.push_back( SHAPES_ARE_PT );
|
2018-10-09 21:50:20 +00:00
|
|
|
m_bbox.Merge( aP );
|
|
|
|
}
|
2013-10-14 18:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Append another line chain at the end.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aOtherLine is the line chain to be appended.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
2019-03-26 02:55:59 +00:00
|
|
|
void Append( const SHAPE_LINE_CHAIN& aOtherLine );
|
2013-10-14 18:40:36 +00:00
|
|
|
|
2019-03-26 02:55:59 +00:00
|
|
|
void Append( const SHAPE_ARC& aArc );
|
2023-01-10 21:22:56 +00:00
|
|
|
void Append( const SHAPE_ARC& aArc, double aAccuracy );
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2019-03-26 02:55:59 +00:00
|
|
|
void Insert( size_t aVertex, const VECTOR2I& aP );
|
2013-10-14 18:40:36 +00:00
|
|
|
|
2019-03-26 02:55:59 +00:00
|
|
|
void Insert( size_t aVertex, const SHAPE_ARC& aArc );
|
2014-05-14 09:45:01 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Replace points with indices in range [start_index, end_index] with a single point \a aP.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aStartIndex is the start of the point range to be replaced (inclusive).
|
|
|
|
* @param aEndIndex is the end of the point range to be replaced (inclusive).
|
|
|
|
* @param aP is the replacement point.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
|
|
|
void Replace( int aStartIndex, int aEndIndex, const VECTOR2I& aP );
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Replace points with indices in range [start_index, end_index] with the points from line
|
|
|
|
* chain \a aLine.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aStartIndex is the start of the point range to be replaced (inclusive).
|
|
|
|
* @param aEndIndex is the end of the point range to be replaced (inclusive).
|
|
|
|
* @param aLine is the replacement line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
|
|
|
void Replace( int aStartIndex, int aEndIndex, const SHAPE_LINE_CHAIN& aLine );
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Remove the range of points [start_index, end_index] from the line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aStartIndex is the start of the point range to be replaced (inclusive).
|
|
|
|
* @param aEndIndex is the end of the point range to be replaced (inclusive).
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
|
|
|
void Remove( int aStartIndex, int aEndIndex );
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Remove the aIndex-th point from the line chain.
|
|
|
|
*
|
2017-03-07 12:06:00 +00:00
|
|
|
* @param aIndex is the index of the point to be removed.
|
|
|
|
*/
|
|
|
|
void Remove( int aIndex )
|
|
|
|
{
|
|
|
|
Remove( aIndex, aIndex );
|
|
|
|
}
|
|
|
|
|
2021-01-04 02:37:24 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Remove the shape at the given index from the line chain.
|
|
|
|
*
|
2021-01-04 02:37:24 +00:00
|
|
|
* If the given index is inside an arc, the entire arc will be removed.
|
|
|
|
* Otherwise this is equivalent to Remove( aPointIndex ).
|
2021-07-01 17:46:17 +00:00
|
|
|
*
|
|
|
|
* @param aPointIndex is the index of the point to remove.
|
2021-01-04 02:37:24 +00:00
|
|
|
*/
|
|
|
|
void RemoveShape( int aPointIndex );
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Insert the point aP belonging to one of the our segments, splitting the adjacent segment
|
2020-07-03 15:12:28 +00:00
|
|
|
* in two.
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aP is the point to be inserted.
|
2013-10-14 18:40:36 +00:00
|
|
|
* @return index of the newly inserted point (or a negative value if aP does not lie on
|
2021-07-01 17:46:17 +00:00
|
|
|
* our line).
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
|
|
|
int Split( const VECTOR2I& aP );
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Search for point \a aP.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aP is the point to be looked for.
|
|
|
|
* @return the index of the corresponding point in the line chain or negative when not found.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
2021-05-27 21:38:41 +00:00
|
|
|
int Find( const VECTOR2I& aP, int aThreshold = 0 ) const;
|
2013-10-14 18:40:36 +00:00
|
|
|
|
2014-05-14 09:45:01 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Search for segment containing point \a aP.
|
2014-05-14 09:45:01 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aP is the point to be looked for.
|
2021-06-09 19:32:58 +00:00
|
|
|
* @return index of the corresponding segment in the line chain or negative when not found.
|
2014-05-14 09:45:01 +00:00
|
|
|
*/
|
2021-05-27 21:38:41 +00:00
|
|
|
int FindSegment( const VECTOR2I& aP, int aThreshold = 1 ) const;
|
2014-05-14 09:45:01 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Return a subset of this line chain containing the [start_index, end_index] range of points.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aStartIndex is the start of the point range to be returned (inclusive).
|
|
|
|
* @param aEndIndex is the end of the point range to be returned (inclusive).
|
|
|
|
* @return the cut line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
|
|
|
const SHAPE_LINE_CHAIN Slice( int aStartIndex, int aEndIndex = -1 ) const;
|
|
|
|
|
|
|
|
struct compareOriginDistance
|
|
|
|
{
|
2014-05-14 11:52:29 +00:00
|
|
|
compareOriginDistance( const VECTOR2I& aOrigin ):
|
2013-10-14 18:40:36 +00:00
|
|
|
m_origin( aOrigin )
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool operator()( const INTERSECTION& aA, const INTERSECTION& aB )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
return ( m_origin - aA.p ).EuclideanNorm() < ( m_origin - aB.p ).EuclideanNorm();
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
VECTOR2I m_origin;
|
|
|
|
};
|
|
|
|
|
2014-05-14 09:45:01 +00:00
|
|
|
bool Intersects( const SHAPE_LINE_CHAIN& aChain ) const;
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Find all intersection points between our line chain and the segment \a aSeg.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aSeg is the segment chain to find intersections with.
|
|
|
|
* @param aIp is the reference to a vector to store found intersections. Intersection points
|
|
|
|
* are sorted with increasing distances from point aSeg.a.
|
|
|
|
* @return the number of intersections found.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
|
|
|
int Intersect( const SEG& aSeg, INTERSECTIONS& aIp ) const;
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Find all intersection points between our line chain and the line chain \a aChain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aChain is the line chain to find intersections with.
|
|
|
|
* @param aIp is reference to a vector to store found intersections. Intersection points are
|
|
|
|
* sorted with increasing path lengths from the starting point of \a aChain.
|
|
|
|
* @return the number of intersections found.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
2021-07-01 17:46:17 +00:00
|
|
|
int Intersect( const SHAPE_LINE_CHAIN& aChain, INTERSECTIONS& aIp,
|
2022-08-03 09:10:23 +00:00
|
|
|
bool aExcludeColinearAndTouching = false,
|
|
|
|
BOX2I* aChainBBox = nullptr ) const;
|
2013-10-14 18:40:36 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Compute the walk path length from the beginning of the line chain and the point \a aP
|
2020-07-03 15:12:28 +00:00
|
|
|
* belonging to our line.
|
2021-07-01 17:46:17 +00:00
|
|
|
*
|
|
|
|
* @return the path length in Euclidean metric or -1 if aP does not belong to the line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
2021-05-27 21:40:25 +00:00
|
|
|
int PathLength( const VECTOR2I& aP, int aIndex = -1 ) const;
|
2013-10-14 18:40:36 +00:00
|
|
|
|
2018-05-08 20:03:52 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Check if point \a aP is closer to (or on) an edge or vertex of the line chain.
|
2018-05-08 20:03:52 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aP is the point to check.
|
|
|
|
* @param aDist is the distance in internal units.
|
|
|
|
* @return true if the point is equal to or closer than \a aDist to the line chain.
|
2018-05-08 20:03:52 +00:00
|
|
|
*/
|
|
|
|
bool CheckClearance( const VECTOR2I& aP, const int aDist) const;
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Check if the line chain is self-intersecting.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
|
|
|
* @return (optional) first found self-intersection point.
|
|
|
|
*/
|
2022-08-25 22:50:47 +00:00
|
|
|
const std::optional<INTERSECTION> SelfIntersecting() const;
|
2013-10-14 18:40:36 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Simplify the line chain by removing colinear adjacent segments and duplicate vertices.
|
2013-10-14 18:40:36 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aRemoveColinear controls the removal of colinear adjacent segments.
|
|
|
|
* @return reference to this line chain.
|
2013-10-14 18:40:36 +00:00
|
|
|
*/
|
2021-01-04 03:36:42 +00:00
|
|
|
SHAPE_LINE_CHAIN& Simplify( bool aRemoveColinear = true );
|
2013-10-14 18:40:36 +00:00
|
|
|
|
2020-02-15 18:44:41 +00:00
|
|
|
/**
|
|
|
|
* Find the segment nearest the given point.
|
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aP is the point to compare with.
|
|
|
|
* @return the index of the segment closest to the point.
|
2020-02-15 18:44:41 +00:00
|
|
|
*/
|
|
|
|
int NearestSegment( const VECTOR2I& aP ) const;
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Find a point on the line chain that is closest to point \a aP.
|
|
|
|
*
|
|
|
|
* @param aP is the point to find.
|
2021-01-04 13:54:29 +00:00
|
|
|
* @param aAllowInternalShapePoints if false will not return points internal to an arc (i.e.
|
|
|
|
* only the arc endpoints are possible candidates)
|
2013-10-14 18:40:36 +00:00
|
|
|
* @return the nearest point.
|
|
|
|
*/
|
2021-01-04 13:54:29 +00:00
|
|
|
const VECTOR2I NearestPoint( const VECTOR2I& aP, bool aAllowInternalShapePoints = true ) const;
|
2013-10-14 18:40:36 +00:00
|
|
|
|
2015-07-02 14:09:43 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Find a point on the line chain that is closest to the line defined by the points of
|
|
|
|
* segment \a aSeg, also returns the distance.
|
|
|
|
*
|
|
|
|
* @param aSeg is the segment defining the line.
|
|
|
|
* @param dist is the reference receiving the distance to the nearest point.
|
2015-07-02 14:09:43 +00:00
|
|
|
* @return the nearest point.
|
|
|
|
*/
|
|
|
|
const VECTOR2I NearestPoint( const SEG& aSeg, int& dist ) const;
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/// @copydoc SHAPE::Format()
|
2022-10-23 22:20:02 +00:00
|
|
|
const std::string Format( bool aCplusPlus = true ) const override;
|
2013-10-14 18:40:36 +00:00
|
|
|
|
2015-08-03 18:29:44 +00:00
|
|
|
/// @copydoc SHAPE::Parse()
|
2016-09-24 18:53:15 +00:00
|
|
|
bool Parse( std::stringstream& aStream ) override;
|
2015-08-03 18:29:44 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
bool operator!=( const SHAPE_LINE_CHAIN& aRhs ) const
|
|
|
|
{
|
|
|
|
if( PointCount() != aRhs.PointCount() )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for( int i = 0; i < PointCount(); i++ )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
if( CPoint( i ) != aRhs.CPoint( i ) )
|
2013-10-14 11:43:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-19 12:55:22 +00:00
|
|
|
bool CompareGeometry( const SHAPE_LINE_CHAIN& aOther ) const;
|
2014-05-14 09:45:01 +00:00
|
|
|
|
2016-09-24 18:53:15 +00:00
|
|
|
void Move( const VECTOR2I& aVector ) override
|
2014-11-14 18:18:31 +00:00
|
|
|
{
|
2019-03-26 02:55:59 +00:00
|
|
|
for( auto& pt : m_points )
|
|
|
|
pt += aVector;
|
|
|
|
|
|
|
|
for( auto& arc : m_arcs )
|
|
|
|
arc.Move( aVector );
|
2022-11-25 17:19:27 +00:00
|
|
|
|
|
|
|
m_bbox.Move( aVector );
|
2014-11-14 18:18:31 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 18:26:44 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Mirror the line points about y or x (or both).
|
|
|
|
*
|
|
|
|
* @param aX If true, mirror about the y axis (flip X coordinate).
|
|
|
|
* @param aY If true, mirror about the x axis (flip Y coordinate).
|
|
|
|
* @param aRef sets the reference point about which to mirror.
|
2019-03-23 18:26:44 +00:00
|
|
|
*/
|
2019-03-26 02:55:59 +00:00
|
|
|
void Mirror( bool aX = true, bool aY = false, const VECTOR2I& aRef = { 0, 0 } );
|
2019-03-23 18:26:44 +00:00
|
|
|
|
2021-04-11 12:28:32 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Mirror the line points using an given axis.
|
|
|
|
*
|
|
|
|
* @param axis is the axis on which to mirror.
|
2021-04-11 12:28:32 +00:00
|
|
|
*/
|
|
|
|
void Mirror( const SEG& axis );
|
|
|
|
|
2017-12-22 09:54:47 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Rotate all vertices by a given angle.
|
|
|
|
*
|
|
|
|
* @param aCenter is the rotation center.
|
2022-01-20 20:54:22 +00:00
|
|
|
* @param aAngle is the rotation angle.
|
2017-12-22 09:54:47 +00:00
|
|
|
*/
|
2022-01-20 20:54:22 +00:00
|
|
|
void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override;
|
2017-12-22 09:54:47 +00:00
|
|
|
|
2016-09-24 18:53:15 +00:00
|
|
|
bool IsSolid() const override
|
2014-11-14 18:18:31 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2017-01-19 12:55:22 +00:00
|
|
|
const VECTOR2I PointAlong( int aPathLength ) const;
|
|
|
|
|
2021-06-19 09:56:22 +00:00
|
|
|
/**
|
|
|
|
* Return the area of this chain
|
|
|
|
* @param aAbsolute If true, returns a positive value. Otherwise the value depends on the
|
|
|
|
* orientation of the chain
|
|
|
|
*/
|
|
|
|
double Area( bool aAbsolute = true ) const;
|
2017-10-19 21:14:36 +00:00
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
size_t ArcCount() const
|
|
|
|
{
|
|
|
|
return m_arcs.size();
|
|
|
|
}
|
|
|
|
|
2021-05-06 22:10:39 +00:00
|
|
|
/**
|
|
|
|
* Return the arc index for the given segment index.
|
|
|
|
*/
|
2019-05-17 00:13:21 +00:00
|
|
|
ssize_t ArcIndex( size_t aSegment ) const
|
|
|
|
{
|
2021-05-06 22:10:39 +00:00
|
|
|
if( IsSharedPt( aSegment ) )
|
|
|
|
return m_shapes[aSegment].second;
|
|
|
|
else
|
|
|
|
return m_shapes[aSegment].first;
|
2019-05-17 00:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const SHAPE_ARC& Arc( size_t aArc ) const
|
|
|
|
{
|
|
|
|
return m_arcs[aArc];
|
|
|
|
}
|
|
|
|
|
2021-05-06 22:10:39 +00:00
|
|
|
/**
|
|
|
|
* Test if a point is shared between multiple shapes
|
|
|
|
* @param aIndex
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
bool IsSharedPt( size_t aIndex ) const
|
|
|
|
{
|
2021-11-17 21:51:21 +00:00
|
|
|
return aIndex < m_shapes.size()
|
2021-05-06 22:10:39 +00:00
|
|
|
&& m_shapes[aIndex].first != SHAPE_IS_PT
|
|
|
|
&& m_shapes[aIndex].second != SHAPE_IS_PT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IsPtOnArc( size_t aPtIndex ) const
|
|
|
|
{
|
|
|
|
return aPtIndex < m_shapes.size() && m_shapes[aPtIndex] != SHAPES_ARE_PT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IsArcSegment( size_t aSegment ) const
|
2019-05-17 00:13:21 +00:00
|
|
|
{
|
2021-07-01 17:46:17 +00:00
|
|
|
/*
|
2021-01-04 03:36:42 +00:00
|
|
|
* A segment is part of an arc except in the special case of two arcs next to each other
|
|
|
|
* but without a shared vertex. Here there is a segment between the end of the first arc
|
|
|
|
* and the start of the second arc.
|
|
|
|
*/
|
2021-05-06 22:10:39 +00:00
|
|
|
size_t nextIdx = aSegment + 1;
|
|
|
|
|
|
|
|
if( nextIdx > m_shapes.size() - 1 )
|
2021-11-17 21:51:21 +00:00
|
|
|
{
|
|
|
|
if( nextIdx == m_shapes.size() && m_closed )
|
|
|
|
nextIdx = 0; // segment between end point and first point
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-06 22:10:39 +00:00
|
|
|
|
|
|
|
return ( IsPtOnArc( aSegment )
|
|
|
|
&& ( IsSharedPt( aSegment )
|
|
|
|
|| m_shapes[aSegment].first == m_shapes[nextIdx].first ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IsArcStart( size_t aIndex ) const
|
|
|
|
{
|
|
|
|
if( aIndex == 0 )
|
|
|
|
return IsPtOnArc( aIndex );
|
|
|
|
|
|
|
|
return ( IsSharedPt( aIndex ) || ( IsPtOnArc( aIndex ) && !IsArcSegment( aIndex - 1 ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IsArcEnd( size_t aIndex ) const
|
|
|
|
{
|
|
|
|
return ( IsSharedPt( aIndex ) || ( IsPtOnArc( aIndex ) && !IsArcSegment( aIndex ) ) );
|
2019-05-17 00:13:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 23:08:18 +00:00
|
|
|
virtual const VECTOR2I GetPoint( int aIndex ) const override { return CPoint(aIndex); }
|
|
|
|
virtual const SEG GetSegment( int aIndex ) const override { return CSegment(aIndex); }
|
|
|
|
virtual size_t GetPointCount() const override { return PointCount(); }
|
|
|
|
virtual size_t GetSegmentCount() const override { return SegmentCount(); }
|
|
|
|
|
2021-03-14 20:28:49 +00:00
|
|
|
protected:
|
|
|
|
friend class SHAPE_POLY_SET;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert an arc to only a point chain by removing the arc and references
|
|
|
|
*
|
|
|
|
* @param aArcIndex index of the arc to convert to points
|
|
|
|
*/
|
|
|
|
void convertArc( ssize_t aArcIndex );
|
|
|
|
|
2021-05-09 22:27:02 +00:00
|
|
|
/**
|
|
|
|
* Splits an arc into two arcs at aPtIndex. Parameter \p aCoincident controls whether the two
|
|
|
|
* arcs are to be coincident at aPtIndex or whether a short straight segment should be created
|
|
|
|
* instead
|
|
|
|
*
|
|
|
|
* @param aPtIndex index of the point in the chain in which to split the arc
|
|
|
|
* @param aCoincident If true, the end point of the first arc will be coincident with the start
|
|
|
|
point of the second arc at aPtIndex.
|
|
|
|
If false, the end point of the first arc will be at aPtIndex-1 and the
|
|
|
|
start point of the second arc will be at aPtIndex, resulting in a short
|
|
|
|
straight line segment between aPtIndex-1 and aPtIndex.
|
|
|
|
*/
|
|
|
|
void splitArc( ssize_t aPtIndex, bool aCoincident = false );
|
|
|
|
|
2021-07-03 19:43:40 +00:00
|
|
|
void amendArc( size_t aArcIndex, const VECTOR2I& aNewStart, const VECTOR2I& aNewEnd );
|
2021-05-09 22:27:02 +00:00
|
|
|
|
2021-07-03 19:43:40 +00:00
|
|
|
void amendArcStart( size_t aArcIndex, const VECTOR2I& aNewStart )
|
2021-06-01 20:48:16 +00:00
|
|
|
{
|
2021-07-03 19:43:40 +00:00
|
|
|
amendArc( aArcIndex, aNewStart, m_arcs[aArcIndex].GetP1() );
|
2021-06-01 20:48:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-03 19:43:40 +00:00
|
|
|
void amendArcEnd( size_t aArcIndex, const VECTOR2I& aNewEnd )
|
2021-06-01 20:48:16 +00:00
|
|
|
{
|
2021-07-03 19:43:40 +00:00
|
|
|
amendArc( aArcIndex, m_arcs[aArcIndex].GetP0(), aNewEnd );
|
2021-06-01 20:48:16 +00:00
|
|
|
}
|
2021-05-09 22:27:02 +00:00
|
|
|
|
2021-08-09 19:59:53 +00:00
|
|
|
/**
|
|
|
|
* Return the arc index for the given segment index, looking backwards
|
|
|
|
*/
|
|
|
|
ssize_t reversedArcIndex( size_t aSegment ) const
|
|
|
|
{
|
|
|
|
if( IsSharedPt( aSegment ) )
|
|
|
|
return m_shapes[aSegment].first;
|
|
|
|
else
|
|
|
|
return m_shapes[aSegment].second;
|
|
|
|
}
|
|
|
|
|
2021-03-14 20:28:49 +00:00
|
|
|
/**
|
|
|
|
* Create a new Clipper path from the SHAPE_LINE_CHAIN in a given orientation
|
|
|
|
*/
|
2021-06-19 09:56:22 +00:00
|
|
|
ClipperLib::Path convertToClipper( bool aRequiredOrientation,
|
|
|
|
std::vector<CLIPPER_Z_VALUE>& aZValueBuffer,
|
|
|
|
std::vector<SHAPE_ARC>& aArcBuffer ) const;
|
2021-03-14 20:28:49 +00:00
|
|
|
|
2022-10-19 23:25:45 +00:00
|
|
|
/**
|
|
|
|
* Create a new Clipper2 path from the SHAPE_LINE_CHAIN in a given orientation
|
|
|
|
*/
|
|
|
|
Clipper2Lib::Path64 convertToClipper2( bool aRequiredOrientation,
|
|
|
|
std::vector<CLIPPER_Z_VALUE> &aZValueBuffer,
|
|
|
|
std::vector<SHAPE_ARC> &aArcBuffer ) const;
|
|
|
|
|
2021-11-17 21:51:21 +00:00
|
|
|
/**
|
|
|
|
* Fix indices of this chain to ensure arcs are not split between the end and start indices
|
|
|
|
*/
|
|
|
|
void fixIndicesRotation();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge the first and last point if they are the same and this chain is closed.
|
|
|
|
*/
|
|
|
|
void mergeFirstLastPointIfNeeded();
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
private:
|
2019-05-17 00:13:21 +00:00
|
|
|
|
2021-05-06 22:10:39 +00:00
|
|
|
static const ssize_t SHAPE_IS_PT;
|
|
|
|
|
|
|
|
static const std::pair<ssize_t, ssize_t> SHAPES_ARE_PT;
|
2019-05-17 00:13:21 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/// array of vertices
|
|
|
|
std::vector<VECTOR2I> m_points;
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2019-03-26 02:55:59 +00:00
|
|
|
/**
|
2020-07-03 15:12:28 +00:00
|
|
|
* Array of indices that refer to the index of the shape if the point is part of a larger
|
|
|
|
* shape, e.g. arc or spline.
|
|
|
|
* If the value is -1, the point is just a point.
|
2021-05-06 22:10:39 +00:00
|
|
|
*
|
|
|
|
* There can be up to two shapes associated with a single point (e.g. the end point of
|
|
|
|
* one arc might be the start point of another).
|
|
|
|
*
|
|
|
|
* Generally speaking only the first element of the pair will be populated (i.e. with a value
|
|
|
|
* not equal to SHAPE_IS_PT), unless the point is shared between two arc shapes. If the point
|
|
|
|
* is shared, then both the first and second element of the pair should be populated.
|
|
|
|
*
|
|
|
|
* The second element must always be SHAPE_IS_PT if the first element is SHAPE_IS_PT.
|
2019-03-26 02:55:59 +00:00
|
|
|
*/
|
2021-05-06 22:10:39 +00:00
|
|
|
std::vector<std::pair<ssize_t, ssize_t>> m_shapes;
|
2019-03-26 02:55:59 +00:00
|
|
|
|
|
|
|
std::vector<SHAPE_ARC> m_arcs;
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
/// is the line chain closed?
|
|
|
|
bool m_closed;
|
2018-10-09 21:50:20 +00:00
|
|
|
|
2020-01-17 14:19:27 +00:00
|
|
|
/// Width of the segments (for BBox calculations in RTree)
|
|
|
|
/// TODO Adjust usage of SHAPE_LINE_CHAIN to account for where we need a width and where not
|
2020-07-03 15:12:28 +00:00
|
|
|
/// Alternatively, we could split the class into a LINE_CHAIN (no width) and SHAPE_LINE_CHAIN
|
|
|
|
/// that derives from SHAPE as well that does have a width. Not sure yet on the correct path.
|
|
|
|
/// TODO Note that we also have SHAPE_SIMPLE which is a closed, filled SHAPE_LINE_CHAIN.
|
2020-01-17 14:19:27 +00:00
|
|
|
int m_width;
|
|
|
|
|
2018-10-09 21:50:20 +00:00
|
|
|
/// cached bounding box
|
2021-02-20 13:25:16 +00:00
|
|
|
mutable BOX2I m_bbox;
|
2013-09-10 11:43:09 +00:00
|
|
|
};
|
|
|
|
|
2019-03-26 02:55:59 +00:00
|
|
|
|
2013-09-10 11:43:09 +00:00
|
|
|
#endif // __SHAPE_LINE_CHAIN
|