2015-06-12 15:12:02 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-03-07 12:06:00 +00:00
|
|
|
* Copyright (C) 2015-2017 CERN
|
2015-06-12 15:12:02 +00:00
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
2017-03-07 12:06:00 +00:00
|
|
|
* @author Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
|
2015-06-12 15:12: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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __SHAPE_POLY_SET_H
|
|
|
|
#define __SHAPE_POLY_SET_H
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <cstdio>
|
2017-12-08 11:20:02 +00:00
|
|
|
#include <memory>
|
2015-06-12 15:12:02 +00:00
|
|
|
#include <geometry/shape.h>
|
|
|
|
#include <geometry/shape_line_chain.h>
|
|
|
|
|
|
|
|
#include "clipper.hpp"
|
|
|
|
|
2017-11-23 16:20:27 +00:00
|
|
|
#include <md5_hash.h>
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
/**
|
|
|
|
* Class SHAPE_POLY_SET
|
|
|
|
*
|
|
|
|
* Represents a set of closed polygons. Polygons may be nonconvex, self-intersecting
|
|
|
|
* and have holes. Provides boolean operations (using Clipper library as the backend).
|
|
|
|
*
|
2017-03-07 12:06:00 +00:00
|
|
|
* Let us define the terms used on this class to clarify methods names and comments:
|
|
|
|
* - Polygon: each polygon in the set.
|
|
|
|
* - Outline: first polyline in each polygon; represents its outer contour.
|
|
|
|
* - Hole: second and following polylines in the polygon.
|
|
|
|
* - Contour: each polyline of each polygon in the set, whether or not it is an
|
|
|
|
* outline or a hole.
|
|
|
|
* - Vertex (or corner): each one of the points that define a contour.
|
|
|
|
*
|
2015-07-27 19:45:57 +00:00
|
|
|
* TODO: add convex partitioning & spatial index
|
2015-06-12 15:12:02 +00:00
|
|
|
*/
|
|
|
|
class SHAPE_POLY_SET : public SHAPE
|
|
|
|
{
|
|
|
|
public:
|
2015-07-27 19:45:57 +00:00
|
|
|
///> represents a single polygon outline with holes. The first entry is the outline,
|
|
|
|
///> the remaining (if any), are the holes
|
|
|
|
typedef std::vector<SHAPE_LINE_CHAIN> POLYGON;
|
|
|
|
|
2017-12-13 23:33:20 +00:00
|
|
|
class TRIANGULATION_CONTEXT;
|
2017-11-23 16:20:27 +00:00
|
|
|
|
2017-12-13 23:33:20 +00:00
|
|
|
class TRIANGULATED_POLYGON
|
|
|
|
{
|
|
|
|
public:
|
2017-11-23 16:20:27 +00:00
|
|
|
struct TRI
|
|
|
|
{
|
2018-02-15 09:16:05 +00:00
|
|
|
TRI() : a(0), b(0), c(0)
|
2017-12-01 15:53:58 +00:00
|
|
|
{
|
|
|
|
}
|
2017-11-23 16:20:27 +00:00
|
|
|
|
|
|
|
int a, b, c;
|
|
|
|
};
|
|
|
|
|
2017-12-13 23:33:20 +00:00
|
|
|
~TRIANGULATED_POLYGON();
|
|
|
|
|
2017-11-23 16:20:27 +00:00
|
|
|
void Clear();
|
|
|
|
|
|
|
|
void AllocateVertices( int aSize );
|
|
|
|
void AllocateTriangles ( int aSize );
|
|
|
|
|
|
|
|
void GetTriangle( int index, VECTOR2I& a, VECTOR2I& b, VECTOR2I& c ) const
|
|
|
|
{
|
|
|
|
auto tri = &m_triangles[ index ];
|
|
|
|
a = m_vertices[ tri->a ];
|
|
|
|
b = m_vertices[ tri->b ];
|
|
|
|
c = m_vertices[ tri->c ];
|
|
|
|
}
|
|
|
|
|
2017-12-13 23:33:20 +00:00
|
|
|
void SetTriangle( int aIndex, const TRI& aTri )
|
|
|
|
{
|
|
|
|
m_triangles[aIndex] = aTri;
|
|
|
|
}
|
|
|
|
|
2017-11-23 16:20:27 +00:00
|
|
|
int AddVertex( const VECTOR2I& aP )
|
|
|
|
{
|
|
|
|
m_vertices[ m_vertexCount ] = aP;
|
|
|
|
return (m_vertexCount++);
|
|
|
|
}
|
|
|
|
|
2018-02-03 09:39:46 +00:00
|
|
|
int GetTriangleCount() const
|
2017-12-13 23:33:20 +00:00
|
|
|
{
|
2018-02-03 09:39:46 +00:00
|
|
|
return m_triangleCount;
|
2017-12-13 23:33:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-03 09:39:46 +00:00
|
|
|
int GetVertexCount() const
|
2017-12-13 23:33:20 +00:00
|
|
|
{
|
2018-02-03 09:39:46 +00:00
|
|
|
return m_vertexCount;
|
2017-12-13 23:33:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2017-11-23 16:20:27 +00:00
|
|
|
TRI* m_triangles = nullptr;
|
|
|
|
VECTOR2I* m_vertices = nullptr;
|
|
|
|
int m_vertexCount = 0;
|
|
|
|
int m_triangleCount = 0;
|
|
|
|
};
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Struct VERTEX_INDEX
|
|
|
|
*
|
|
|
|
* Structure to hold the necessary information in order to index a vertex on a
|
|
|
|
* SHAPE_POLY_SET object: the polygon index, the contour index relative to the polygon and
|
|
|
|
* the vertex index relative the contour.
|
|
|
|
*/
|
|
|
|
typedef struct VERTEX_INDEX
|
|
|
|
{
|
|
|
|
int m_polygon; /*!< m_polygon is the index of the polygon. */
|
|
|
|
int m_contour; /*!< m_contour is the index of the contour relative to the polygon. */
|
|
|
|
int m_vertex; /*!< m_vertex is the index of the vertex relative to the contour. */
|
|
|
|
|
|
|
|
VERTEX_INDEX() : m_polygon(-1), m_contour(-1), m_vertex(-1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
} VERTEX_INDEX;
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
/**
|
|
|
|
* Class ITERATOR_TEMPLATE
|
|
|
|
*
|
2017-03-07 12:06:00 +00:00
|
|
|
* Base class for iterating over all vertices in a given SHAPE_POLY_SET.
|
2015-07-27 19:45:57 +00:00
|
|
|
*/
|
|
|
|
template <class T>
|
2015-08-16 12:07:58 +00:00
|
|
|
class ITERATOR_TEMPLATE
|
|
|
|
{
|
|
|
|
public:
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function IsEndContour.
|
|
|
|
* @return bool - true if the current vertex is the last one of the current contour
|
|
|
|
* (outline or hole); false otherwise.
|
|
|
|
*/
|
2015-08-16 12:07:58 +00:00
|
|
|
bool IsEndContour() const
|
|
|
|
{
|
2017-03-07 12:06:00 +00:00
|
|
|
return m_currentVertex + 1 == m_poly->CPolygon( m_currentPolygon )[m_currentContour].PointCount();
|
2015-08-16 12:07:58 +00:00
|
|
|
}
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function IsLastOutline.
|
|
|
|
* @return bool - true if the current outline is the last one; false otherwise.
|
|
|
|
*/
|
|
|
|
bool IsLastPolygon() const
|
2015-08-16 12:07:58 +00:00
|
|
|
{
|
2017-03-07 12:06:00 +00:00
|
|
|
return m_currentPolygon == m_lastPolygon;
|
2015-08-16 12:07:58 +00:00
|
|
|
}
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2015-08-16 12:07:58 +00:00
|
|
|
operator bool() const
|
|
|
|
{
|
2017-03-07 12:06:00 +00:00
|
|
|
return m_currentPolygon <= m_lastPolygon;
|
2015-08-16 12:07:58 +00:00
|
|
|
}
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function Advance
|
|
|
|
* advances the indices of the current vertex/outline/contour, checking whether the
|
|
|
|
* vertices in the holes have to be iterated through
|
|
|
|
*/
|
2015-08-16 12:07:58 +00:00
|
|
|
void Advance()
|
|
|
|
{
|
2017-03-07 12:06:00 +00:00
|
|
|
// Advance vertex index
|
2015-08-16 12:07:58 +00:00
|
|
|
m_currentVertex ++;
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
// Check whether the user wants to iterate through the vertices of the holes
|
|
|
|
// and behave accordingly
|
|
|
|
if( m_iterateHoles )
|
|
|
|
{
|
|
|
|
// If the last vertex of the contour was reached, advance the contour index
|
|
|
|
if( m_currentVertex >= m_poly->CPolygon( m_currentPolygon )[m_currentContour].PointCount() )
|
|
|
|
{
|
|
|
|
m_currentVertex = 0;
|
|
|
|
m_currentContour++;
|
|
|
|
|
|
|
|
// If the last contour of the current polygon was reached, advance the
|
|
|
|
// outline index
|
|
|
|
int totalContours = m_poly->CPolygon( m_currentPolygon ).size();
|
|
|
|
|
|
|
|
if( m_currentContour >= totalContours )
|
|
|
|
{
|
|
|
|
m_currentContour = 0;
|
|
|
|
m_currentPolygon++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2015-07-27 19:45:57 +00:00
|
|
|
{
|
2017-03-07 12:06:00 +00:00
|
|
|
// If the last vertex of the outline was reached, advance to the following polygon
|
|
|
|
if( m_currentVertex >= m_poly->CPolygon( m_currentPolygon )[0].PointCount() )
|
|
|
|
{
|
|
|
|
m_currentVertex = 0;
|
|
|
|
m_currentPolygon++;
|
|
|
|
}
|
2015-07-27 19:45:57 +00:00
|
|
|
}
|
2015-08-16 12:07:58 +00:00
|
|
|
}
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2015-08-16 12:07:58 +00:00
|
|
|
void operator++( int dummy )
|
|
|
|
{
|
|
|
|
Advance();
|
|
|
|
}
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2015-08-16 12:07:58 +00:00
|
|
|
void operator++()
|
|
|
|
{
|
|
|
|
Advance();
|
|
|
|
}
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2015-08-16 12:07:58 +00:00
|
|
|
T& Get()
|
|
|
|
{
|
2017-03-07 12:06:00 +00:00
|
|
|
return m_poly->Polygon( m_currentPolygon )[m_currentContour].Point( m_currentVertex );
|
2015-08-16 12:07:58 +00:00
|
|
|
}
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2015-08-16 12:07:58 +00:00
|
|
|
T& operator*()
|
|
|
|
{
|
|
|
|
return Get();
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator->()
|
|
|
|
{
|
|
|
|
return &Get();
|
|
|
|
}
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function GetIndex
|
|
|
|
* @return VERTEX_INDEX - indices of the current polygon, contour and vertex.
|
|
|
|
*/
|
|
|
|
VERTEX_INDEX GetIndex()
|
|
|
|
{
|
|
|
|
VERTEX_INDEX index;
|
|
|
|
|
|
|
|
index.m_polygon = m_currentPolygon;
|
|
|
|
index.m_contour = m_currentContour;
|
|
|
|
index.m_vertex = m_currentVertex;
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2015-08-16 12:07:58 +00:00
|
|
|
private:
|
|
|
|
friend class SHAPE_POLY_SET;
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2015-08-16 12:07:58 +00:00
|
|
|
SHAPE_POLY_SET* m_poly;
|
2017-03-07 12:06:00 +00:00
|
|
|
int m_currentPolygon;
|
|
|
|
int m_currentContour;
|
2015-08-16 12:07:58 +00:00
|
|
|
int m_currentVertex;
|
2017-03-07 12:06:00 +00:00
|
|
|
int m_lastPolygon;
|
|
|
|
bool m_iterateHoles;
|
2015-07-27 19:45:57 +00:00
|
|
|
};
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Class SEGMENT_ITERATOR_TEMPLATE
|
|
|
|
*
|
|
|
|
* Base class for iterating over all segments in a given SHAPE_POLY_SET.
|
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
class SEGMENT_ITERATOR_TEMPLATE
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Function IsLastOutline.
|
|
|
|
* @return bool - true if the current outline is the last one.
|
|
|
|
*/
|
|
|
|
bool IsLastPolygon() const
|
|
|
|
{
|
|
|
|
return m_currentPolygon == m_lastPolygon;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator bool() const
|
|
|
|
{
|
|
|
|
return m_currentPolygon <= m_lastPolygon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Advance
|
|
|
|
* advances the indices of the current vertex/outline/contour, checking whether the
|
|
|
|
* vertices in the holes have to be iterated through
|
|
|
|
*/
|
|
|
|
void Advance()
|
|
|
|
{
|
|
|
|
// Advance vertex index
|
|
|
|
m_currentSegment++;
|
|
|
|
int last;
|
|
|
|
|
|
|
|
// Check whether the user wants to iterate through the vertices of the holes
|
|
|
|
// and behave accordingly
|
|
|
|
if( m_iterateHoles )
|
|
|
|
{
|
|
|
|
last = m_poly->CPolygon( m_currentPolygon )[m_currentContour].SegmentCount();
|
|
|
|
|
|
|
|
// If the last vertex of the contour was reached, advance the contour index
|
|
|
|
if( m_currentSegment >= last )
|
|
|
|
{
|
|
|
|
m_currentSegment = 0;
|
|
|
|
m_currentContour++;
|
|
|
|
|
|
|
|
// If the last contour of the current polygon was reached, advance the
|
|
|
|
// outline index
|
|
|
|
int totalContours = m_poly->CPolygon( m_currentPolygon ).size();
|
|
|
|
|
|
|
|
if( m_currentContour >= totalContours )
|
|
|
|
{
|
|
|
|
m_currentContour = 0;
|
|
|
|
m_currentPolygon++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
last = m_poly->CPolygon( m_currentPolygon )[0].SegmentCount();
|
|
|
|
// If the last vertex of the outline was reached, advance to the following
|
|
|
|
// polygon
|
|
|
|
if( m_currentSegment >= last )
|
|
|
|
{
|
|
|
|
m_currentSegment = 0;
|
|
|
|
m_currentPolygon++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator++( int dummy )
|
|
|
|
{
|
|
|
|
Advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator++()
|
|
|
|
{
|
|
|
|
Advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
T Get()
|
|
|
|
{
|
|
|
|
return m_poly->Polygon( m_currentPolygon )[m_currentContour].Segment( m_currentSegment );
|
|
|
|
}
|
|
|
|
|
|
|
|
T operator*()
|
|
|
|
{
|
|
|
|
return Get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function GetIndex
|
|
|
|
* @return VERTEX_INDEX - indices of the current polygon, contour and vertex.
|
|
|
|
*/
|
|
|
|
VERTEX_INDEX GetIndex()
|
|
|
|
{
|
|
|
|
VERTEX_INDEX index;
|
|
|
|
|
|
|
|
index.m_polygon = m_currentPolygon;
|
|
|
|
index.m_contour = m_currentContour;
|
|
|
|
index.m_vertex = m_currentSegment;
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function IsAdjacent
|
|
|
|
* @param aOther is an iterator pointing to another segment.
|
|
|
|
* @return bool - true if both iterators point to the same segment of the same
|
|
|
|
* contour of the same polygon of the same polygon set; false
|
|
|
|
* otherwise.
|
|
|
|
*/
|
|
|
|
bool IsAdjacent( SEGMENT_ITERATOR_TEMPLATE<T> aOther )
|
|
|
|
{
|
|
|
|
// Check that both iterators point to the same contour of the same polygon of the
|
|
|
|
// same polygon set
|
|
|
|
if( m_poly == aOther.m_poly && m_currentPolygon == aOther.m_currentPolygon &&
|
|
|
|
m_currentContour == aOther.m_currentContour )
|
|
|
|
{
|
|
|
|
// Compute the total number of segments
|
|
|
|
int numSeg;
|
|
|
|
numSeg = m_poly->CPolygon( m_currentPolygon )[m_currentContour].SegmentCount();
|
|
|
|
|
|
|
|
// Compute the difference of the segment indices. If it is exactly one, they
|
|
|
|
// are adjacent. The only missing case where they also are adjacent is when
|
|
|
|
// the segments are the first and last one, in which case the difference
|
|
|
|
// always equals the total number of segments minus one.
|
|
|
|
int indexDiff = abs( m_currentSegment - aOther.m_currentSegment );
|
|
|
|
|
|
|
|
return ( indexDiff == 1 ) || ( indexDiff == (numSeg - 1) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class SHAPE_POLY_SET;
|
|
|
|
|
|
|
|
SHAPE_POLY_SET* m_poly;
|
|
|
|
int m_currentPolygon;
|
|
|
|
int m_currentContour;
|
|
|
|
int m_currentSegment;
|
|
|
|
int m_lastPolygon;
|
|
|
|
bool m_iterateHoles;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Iterator and const iterator types to visit polygon's points.
|
2015-07-27 19:45:57 +00:00
|
|
|
typedef ITERATOR_TEMPLATE<VECTOR2I> ITERATOR;
|
|
|
|
typedef ITERATOR_TEMPLATE<const VECTOR2I> CONST_ITERATOR;
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
// Iterator and const iterator types to visit polygon's edges.
|
|
|
|
typedef SEGMENT_ITERATOR_TEMPLATE<SEG> SEGMENT_ITERATOR;
|
|
|
|
typedef SEGMENT_ITERATOR_TEMPLATE<const SEG> CONST_SEGMENT_ITERATOR;
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
SHAPE_POLY_SET();
|
2017-03-07 12:06:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor SHAPE_POLY_SET
|
|
|
|
* Performs a deep copy of \p aOther into \p this.
|
|
|
|
* @param aOther is the SHAPE_POLY_SET object that will be copied.
|
|
|
|
*/
|
|
|
|
SHAPE_POLY_SET( const SHAPE_POLY_SET& aOther );
|
|
|
|
|
2018-02-03 09:39:46 +00:00
|
|
|
~SHAPE_POLY_SET();
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function GetRelativeIndices
|
|
|
|
*
|
|
|
|
* Converts a global vertex index ---i.e., a number that globally identifies a vertex in a
|
|
|
|
* concatenated list of all vertices in all contours--- and get the index of the vertex
|
|
|
|
* relative to the contour relative to the polygon in which it is.
|
|
|
|
* @param aGlobalIdx is the global index of the corner whose structured index wants to
|
|
|
|
* be found
|
2017-06-18 07:04:42 +00:00
|
|
|
* @param aRelativeIndices is a pointer to the set of relative indices to store.
|
2017-03-07 12:06:00 +00:00
|
|
|
* @return bool - true if the global index is correct and the information in
|
|
|
|
* aRelativeIndices is valid; false otherwise.
|
|
|
|
*/
|
|
|
|
bool GetRelativeIndices( int aGlobalIdx, VERTEX_INDEX* aRelativeIndices) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function GetGlobalIndex
|
|
|
|
* computes the global index of a vertex from the relative indices of polygon, contour and
|
|
|
|
* vertex.
|
|
|
|
* @param aRelativeIndices is the set of relative indices.
|
|
|
|
* @param aGlobalIdx [out] is the computed global index.
|
2017-04-16 16:32:47 +00:00
|
|
|
* @return bool - true if the relative indices are correct; false otherwise. The computed
|
2017-03-07 12:06:00 +00:00
|
|
|
* global index is returned in the \p aGlobalIdx reference.
|
|
|
|
*/
|
|
|
|
bool GetGlobalIndex( VERTEX_INDEX aRelativeIndices, int& aGlobalIdx );
|
|
|
|
|
|
|
|
/// @copydoc SHAPE::Clone()
|
|
|
|
SHAPE* Clone() const override;
|
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
///> Creates a new empty polygon in the set and returns its index
|
2015-07-06 13:15:48 +00:00
|
|
|
int NewOutline();
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
///> Creates a new hole in a given outline
|
|
|
|
int NewHole( int aOutline = -1 );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
|
|
|
///> Adds a new outline to the set and returns its index
|
2015-07-06 13:15:48 +00:00
|
|
|
int AddOutline( const SHAPE_LINE_CHAIN& aOutline );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
|
|
|
///> Adds a new hole to the given outline (default: last) and returns its index
|
2015-07-06 13:15:48 +00:00
|
|
|
int AddHole( const SHAPE_LINE_CHAIN& aHole, int aOutline = -1 );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
///> Appends a vertex at the end of the given outline/hole (default: the last outline)
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function Append
|
|
|
|
* adds a new vertex to the contour indexed by \p aOutline and \p aHole (defaults to the
|
|
|
|
* outline of the last polygon).
|
|
|
|
* @param x is the x coordinate of the new vertex.
|
|
|
|
* @param y is the y coordinate of the new vertex.
|
|
|
|
* @param aOutline is the index of the polygon.
|
|
|
|
* @param aHole is the index of the hole (-1 for the main outline),
|
|
|
|
* @param aAllowDuplication is a flag to indicate whether it is allowed to add this
|
|
|
|
* corner even if it is duplicated.
|
|
|
|
* @return int - the number of corners of the selected contour after the addition.
|
|
|
|
*/
|
|
|
|
int Append( int x, int y, int aOutline = -1, int aHole = -1,
|
|
|
|
bool aAllowDuplication = false );
|
2015-07-27 19:45:57 +00:00
|
|
|
|
|
|
|
///> Merges polygons from two sets.
|
|
|
|
void Append( const SHAPE_POLY_SET& aSet );
|
|
|
|
|
|
|
|
///> Appends a vertex at the end of the given outline/hole (default: the last outline)
|
|
|
|
void Append( const VECTOR2I& aP, int aOutline = -1, int aHole = -1 );
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function InsertVertex
|
|
|
|
* Adds a vertex in the globally indexed position aGlobalIndex.
|
|
|
|
* @param aGlobalIndex is the global index of the position in which teh new vertex will be
|
|
|
|
* inserted.
|
|
|
|
* @param aNewVertex is the new inserted vertex.
|
|
|
|
*/
|
|
|
|
void InsertVertex( int aGlobalIndex, VECTOR2I aNewVertex );
|
|
|
|
|
|
|
|
///> Returns the index-th vertex in a given hole outline within a given outline
|
|
|
|
VECTOR2I& Vertex( int aIndex, int aOutline, int aHole );
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
///> Returns the index-th vertex in a given hole outline within a given outline
|
2017-03-07 12:06:00 +00:00
|
|
|
const VECTOR2I& CVertex( int aIndex, int aOutline, int aHole ) const;
|
|
|
|
|
|
|
|
///> Returns the aGlobalIndex-th vertex in the poly set
|
|
|
|
VECTOR2I& Vertex( int aGlobalIndex );
|
|
|
|
|
|
|
|
///> Returns the aGlobalIndex-th vertex in the poly set
|
|
|
|
const VECTOR2I& CVertex( int aGlobalIndex ) const;
|
|
|
|
|
|
|
|
///> Returns the index-th vertex in a given hole outline within a given outline
|
|
|
|
VECTOR2I& Vertex( VERTEX_INDEX aIndex );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
|
|
|
///> Returns the index-th vertex in a given hole outline within a given outline
|
2017-03-07 12:06:00 +00:00
|
|
|
const VECTOR2I& CVertex( VERTEX_INDEX aIndex ) const;
|
|
|
|
|
|
|
|
/**
|
2017-04-16 16:32:47 +00:00
|
|
|
* Returns the global indexes of the previous and the next corner
|
|
|
|
* of the aGlobalIndex-th corner of a contour in the polygon set.
|
|
|
|
* they are often aGlobalIndex-1 and aGlobalIndex+1, but not for the first and last
|
|
|
|
* corner of the contour.
|
|
|
|
* @param aGlobalIndex is index of the corner, globally indexed between all edges in all
|
2017-03-07 12:06:00 +00:00
|
|
|
* contours
|
2017-04-16 16:32:47 +00:00
|
|
|
* @param aPrevious - the globalIndex of the previous corner of the same contour.
|
|
|
|
* @param aNext - the globalIndex of the next corner of the same contour.
|
|
|
|
* @return true if OK, false if aGlobalIndex is out of range
|
2017-03-07 12:06:00 +00:00
|
|
|
*/
|
2017-04-16 16:32:47 +00:00
|
|
|
bool GetNeighbourIndexes( int aGlobalIndex, int* aPrevious, int* aNext );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function IsPolygonSelfIntersecting.
|
|
|
|
* Checks whether the aPolygonIndex-th polygon in the set is self intersecting.
|
|
|
|
* @param aPolygonIndex index of the polygon that wants to be checked.
|
|
|
|
* @return bool - true if the aPolygonIndex-th polygon is self intersecting, false
|
|
|
|
* otherwise.
|
|
|
|
*/
|
|
|
|
bool IsPolygonSelfIntersecting( int aPolygonIndex );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function IsSelfIntersecting
|
|
|
|
* Checks whether any of the polygons in the set is self intersecting.
|
|
|
|
* @return bool - true if any of the polygons is self intersecting, false otherwise.
|
|
|
|
*/
|
2015-06-12 15:12:02 +00:00
|
|
|
bool IsSelfIntersecting();
|
|
|
|
|
|
|
|
///> Returns the number of outlines in the set
|
|
|
|
int OutlineCount() const { return m_polys.size(); }
|
|
|
|
|
|
|
|
///> Returns the number of vertices in a given outline/hole
|
2015-07-06 13:15:48 +00:00
|
|
|
int VertexCount( int aOutline = -1, int aHole = -1 ) const;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
///> Returns the number of holes in a given outline
|
2015-08-24 13:55:22 +00:00
|
|
|
int HoleCount( int aOutline ) const
|
|
|
|
{
|
2017-10-25 07:39:26 +00:00
|
|
|
if( ( aOutline < 0 ) || (aOutline >= (int)m_polys.size()) || (m_polys[aOutline].size() < 2) )
|
2015-08-24 13:55:22 +00:00
|
|
|
return 0;
|
2017-10-25 07:39:26 +00:00
|
|
|
|
|
|
|
// the first polygon in m_polys[aOutline] is the main contour,
|
|
|
|
// only others are holes:
|
2015-08-24 13:55:22 +00:00
|
|
|
return m_polys[aOutline].size() - 1;
|
|
|
|
}
|
2015-07-27 19:45:57 +00:00
|
|
|
|
|
|
|
///> Returns the reference to aIndex-th outline in the set
|
|
|
|
SHAPE_LINE_CHAIN& Outline( int aIndex )
|
|
|
|
{
|
|
|
|
return m_polys[aIndex][0];
|
|
|
|
}
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function Subset
|
|
|
|
* returns a subset of the polygons in this set, the ones between aFirstPolygon and
|
|
|
|
* aLastPolygon.
|
|
|
|
* @param aFirstPolygon is the first polygon to be included in the returned set.
|
|
|
|
* @param aLastPolygon is the first polygon to be excluded of the returned set.
|
|
|
|
* @return SHAPE_POLY_SET - a set containing the polygons between aFirstPolygon (included)
|
|
|
|
* and aLastPolygon (excluded).
|
|
|
|
*/
|
|
|
|
SHAPE_POLY_SET Subset( int aFirstPolygon, int aLastPolygon );
|
|
|
|
|
|
|
|
SHAPE_POLY_SET UnitSet( int aPolygonIndex )
|
|
|
|
{
|
|
|
|
return Subset( aPolygonIndex, aPolygonIndex + 1 );
|
|
|
|
}
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
///> Returns the reference to aHole-th hole in the aIndex-th outline
|
|
|
|
SHAPE_LINE_CHAIN& Hole( int aOutline, int aHole )
|
|
|
|
{
|
|
|
|
return m_polys[aOutline][aHole + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
///> Returns the aIndex-th subpolygon in the set
|
|
|
|
POLYGON& Polygon( int aIndex )
|
2015-07-14 11:36:24 +00:00
|
|
|
{
|
|
|
|
return m_polys[aIndex];
|
|
|
|
}
|
|
|
|
|
2017-11-23 16:20:27 +00:00
|
|
|
const POLYGON& Polygon( int aIndex ) const
|
|
|
|
{
|
|
|
|
return m_polys[aIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
const TRIANGULATED_POLYGON* TriangulatedPolygon( int aIndex ) const
|
|
|
|
{
|
2017-12-08 11:20:02 +00:00
|
|
|
return m_triangulatedPolys[aIndex].get();
|
2017-11-23 16:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
const SHAPE_LINE_CHAIN& COutline( int aIndex ) const
|
|
|
|
{
|
|
|
|
return m_polys[aIndex][0];
|
|
|
|
}
|
|
|
|
|
|
|
|
const SHAPE_LINE_CHAIN& CHole( int aOutline, int aHole ) const
|
|
|
|
{
|
|
|
|
return m_polys[aOutline][aHole + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
const POLYGON& CPolygon( int aIndex ) const
|
|
|
|
{
|
|
|
|
return m_polys[aIndex];
|
|
|
|
}
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function Iterate
|
|
|
|
* returns an object to iterate through the points of the polygons between \p aFirst and
|
|
|
|
* \p aLast.
|
|
|
|
* @param aFirst is the first polygon whose points will be iterated.
|
|
|
|
* @param aLast is the last polygon whose points will be iterated.
|
|
|
|
* @param aIterateHoles is a flag to indicate whether the points of the holes should be
|
|
|
|
* iterated.
|
|
|
|
* @return ITERATOR - the iterator object.
|
|
|
|
*/
|
|
|
|
ITERATOR Iterate( int aFirst, int aLast, bool aIterateHoles = false )
|
2015-07-27 19:45:57 +00:00
|
|
|
{
|
|
|
|
ITERATOR iter;
|
|
|
|
|
|
|
|
iter.m_poly = this;
|
2017-03-07 12:06:00 +00:00
|
|
|
iter.m_currentPolygon = aFirst;
|
|
|
|
iter.m_lastPolygon = aLast < 0 ? OutlineCount() - 1 : aLast;
|
|
|
|
iter.m_currentContour = 0;
|
2015-07-27 19:45:57 +00:00
|
|
|
iter.m_currentVertex = 0;
|
2017-03-07 12:06:00 +00:00
|
|
|
iter.m_iterateHoles = aIterateHoles;
|
2015-07-27 19:45:57 +00:00
|
|
|
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function Iterate
|
|
|
|
* @param aOutline the index of the polygon to be iterated.
|
|
|
|
* @return ITERATOR - an iterator object to visit all points in the main outline of the
|
|
|
|
* aOutline-th polygon, without visiting the points in the holes.
|
|
|
|
*/
|
2015-07-27 19:45:57 +00:00
|
|
|
ITERATOR Iterate( int aOutline )
|
|
|
|
{
|
|
|
|
return Iterate( aOutline, aOutline );
|
|
|
|
}
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function IterateWithHoles
|
|
|
|
* @param aOutline the index of the polygon to be iterated.
|
|
|
|
* @return ITERATOR - an iterator object to visit all points in the main outline of the
|
|
|
|
* aOutline-th polygon, visiting also the points in the holes.
|
|
|
|
*/
|
|
|
|
ITERATOR IterateWithHoles( int aOutline )
|
|
|
|
{
|
|
|
|
return Iterate( aOutline, aOutline, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Iterate
|
|
|
|
* @return ITERATOR - an iterator object to visit all points in all outlines of the set,
|
|
|
|
* without visiting the points in the holes.
|
|
|
|
*/
|
2015-07-27 19:45:57 +00:00
|
|
|
ITERATOR Iterate()
|
|
|
|
{
|
|
|
|
return Iterate( 0, OutlineCount() - 1 );
|
|
|
|
}
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function IterateWithHoles
|
|
|
|
* @return ITERATOR - an iterator object to visit all points in all outlines of the set,
|
|
|
|
* visiting also the points in the holes.
|
|
|
|
*/
|
|
|
|
ITERATOR IterateWithHoles()
|
|
|
|
{
|
|
|
|
return Iterate( 0, OutlineCount() - 1, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CONST_ITERATOR CIterate( int aFirst, int aLast, bool aIterateHoles = false ) const
|
2015-07-27 19:45:57 +00:00
|
|
|
{
|
|
|
|
CONST_ITERATOR iter;
|
|
|
|
|
|
|
|
iter.m_poly = const_cast<SHAPE_POLY_SET*>( this );
|
2017-03-07 12:06:00 +00:00
|
|
|
iter.m_currentPolygon = aFirst;
|
|
|
|
iter.m_lastPolygon = aLast < 0 ? OutlineCount() - 1 : aLast;
|
|
|
|
iter.m_currentContour = 0;
|
2015-07-27 19:45:57 +00:00
|
|
|
iter.m_currentVertex = 0;
|
2017-03-07 12:06:00 +00:00
|
|
|
iter.m_iterateHoles = aIterateHoles;
|
2015-07-27 19:45:57 +00:00
|
|
|
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
CONST_ITERATOR CIterate( int aOutline ) const
|
|
|
|
{
|
|
|
|
return CIterate( aOutline, aOutline );
|
|
|
|
}
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
CONST_ITERATOR CIterateWithHoles( int aOutline ) const
|
|
|
|
{
|
|
|
|
return CIterate( aOutline, aOutline, true );
|
|
|
|
}
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
CONST_ITERATOR CIterate() const
|
|
|
|
{
|
|
|
|
return CIterate( 0, OutlineCount() - 1 );
|
|
|
|
}
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
CONST_ITERATOR CIterateWithHoles() const
|
|
|
|
{
|
|
|
|
return CIterate( 0, OutlineCount() - 1, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
ITERATOR IterateFromVertexWithHoles( int aGlobalIdx )
|
|
|
|
{
|
|
|
|
// Build iterator
|
|
|
|
ITERATOR iter = IterateWithHoles();
|
|
|
|
|
|
|
|
// Get the relative indices of the globally indexed vertex
|
|
|
|
VERTEX_INDEX indices;
|
|
|
|
|
|
|
|
if( !GetRelativeIndices( aGlobalIdx, &indices ) )
|
|
|
|
throw( std::out_of_range( "aGlobalIndex-th vertex does not exist" ) );
|
|
|
|
|
|
|
|
// Adjust where the iterator is pointing
|
|
|
|
iter.m_currentPolygon = indices.m_polygon;
|
|
|
|
iter.m_currentContour = indices.m_contour;
|
|
|
|
iter.m_currentVertex = indices.m_vertex;
|
|
|
|
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
///> Returns an iterator object, for iterating between aFirst and aLast outline, with or
|
|
|
|
/// without holes (default: without)
|
|
|
|
SEGMENT_ITERATOR IterateSegments( int aFirst, int aLast, bool aIterateHoles = false )
|
|
|
|
{
|
|
|
|
SEGMENT_ITERATOR iter;
|
|
|
|
|
|
|
|
iter.m_poly = this;
|
|
|
|
iter.m_currentPolygon = aFirst;
|
|
|
|
iter.m_lastPolygon = aLast < 0 ? OutlineCount() - 1 : aLast;
|
|
|
|
iter.m_currentContour = 0;
|
|
|
|
iter.m_currentSegment = 0;
|
|
|
|
iter.m_iterateHoles = aIterateHoles;
|
|
|
|
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
///> Returns an iterator object, for iterating aPolygonIdx-th polygon edges
|
|
|
|
SEGMENT_ITERATOR IterateSegments( int aPolygonIdx )
|
|
|
|
{
|
|
|
|
return IterateSegments( aPolygonIdx, aPolygonIdx );
|
|
|
|
}
|
|
|
|
|
|
|
|
///> Returns an iterator object, for all outlines in the set (no holes)
|
|
|
|
SEGMENT_ITERATOR IterateSegments()
|
|
|
|
{
|
|
|
|
return IterateSegments( 0, OutlineCount() - 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
///> Returns an iterator object, for all outlines in the set (with holes)
|
|
|
|
SEGMENT_ITERATOR IterateSegmentsWithHoles()
|
|
|
|
{
|
|
|
|
return IterateSegments( 0, OutlineCount() - 1, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
///> Returns an iterator object, for the aOutline-th outline in the set (with holes)
|
|
|
|
SEGMENT_ITERATOR IterateSegmentsWithHoles( int aOutline )
|
|
|
|
{
|
|
|
|
return IterateSegments( aOutline, aOutline, true );
|
|
|
|
}
|
|
|
|
|
2015-12-15 20:21:25 +00:00
|
|
|
/** operations on polygons use a aFastMode param
|
|
|
|
* if aFastMode is PM_FAST (true) the result can be a weak polygon
|
|
|
|
* if aFastMode is PM_STRICTLY_SIMPLE (false) (default) the result is (theorically) a strictly
|
|
|
|
* simple polygon, but calculations can be really significantly time consuming
|
|
|
|
* Most of time PM_FAST is preferable.
|
|
|
|
* PM_STRICTLY_SIMPLE can be used in critical cases (Gerber output for instance)
|
|
|
|
*/
|
|
|
|
enum POLYGON_MODE
|
|
|
|
{
|
|
|
|
PM_FAST = true,
|
|
|
|
PM_STRICTLY_SIMPLE = false
|
|
|
|
};
|
2015-06-12 15:12:02 +00:00
|
|
|
|
|
|
|
///> Performs boolean polyset union
|
2015-08-16 12:07:58 +00:00
|
|
|
///> For aFastMode meaning, see function booleanOp
|
2015-12-15 20:21:25 +00:00
|
|
|
void BooleanAdd( const SHAPE_POLY_SET& b, POLYGON_MODE aFastMode );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
///> Performs boolean polyset difference
|
2015-08-16 12:07:58 +00:00
|
|
|
///> For aFastMode meaning, see function booleanOp
|
2015-12-15 20:21:25 +00:00
|
|
|
void BooleanSubtract( const SHAPE_POLY_SET& b, POLYGON_MODE aFastMode );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-08-24 13:55:22 +00:00
|
|
|
///> Performs boolean polyset intersection
|
|
|
|
///> For aFastMode meaning, see function booleanOp
|
2015-12-15 20:21:25 +00:00
|
|
|
void BooleanIntersection( const SHAPE_POLY_SET& b, POLYGON_MODE aFastMode );
|
2015-08-24 13:55:22 +00:00
|
|
|
|
|
|
|
///> Performs boolean polyset union between a and b, store the result in it self
|
|
|
|
///> For aFastMode meaning, see function booleanOp
|
2015-12-15 20:21:25 +00:00
|
|
|
void BooleanAdd( const SHAPE_POLY_SET& a, const SHAPE_POLY_SET& b,
|
|
|
|
POLYGON_MODE aFastMode );
|
2015-08-24 13:55:22 +00:00
|
|
|
|
|
|
|
///> Performs boolean polyset difference between a and b, store the result in it self
|
|
|
|
///> For aFastMode meaning, see function booleanOp
|
2015-12-15 20:21:25 +00:00
|
|
|
void BooleanSubtract( const SHAPE_POLY_SET& a, const SHAPE_POLY_SET& b,
|
|
|
|
POLYGON_MODE aFastMode );
|
2015-08-24 13:55:22 +00:00
|
|
|
|
|
|
|
///> Performs boolean polyset intersection between a and b, store the result in it self
|
|
|
|
///> For aFastMode meaning, see function booleanOp
|
2015-12-15 20:21:25 +00:00
|
|
|
void BooleanIntersection( const SHAPE_POLY_SET& a, const SHAPE_POLY_SET& b,
|
|
|
|
POLYGON_MODE aFastMode );
|
2015-08-24 13:55:22 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
///> Performs outline inflation/deflation, using round corners.
|
2015-07-27 19:46:47 +00:00
|
|
|
void Inflate( int aFactor, int aCircleSegmentsCount );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
///> Converts a set of polygons with holes to a singe outline with "slits"/"fractures" connecting the outer ring
|
2015-06-12 15:12:02 +00:00
|
|
|
///> to the inner holes
|
2015-08-16 12:07:58 +00:00
|
|
|
///> For aFastMode meaning, see function booleanOp
|
2015-12-15 20:21:25 +00:00
|
|
|
void Fracture( POLYGON_MODE aFastMode );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2017-12-01 15:53:58 +00:00
|
|
|
///> Converts a single outline slitted ("fractured") polygon into a set ouf outlines
|
|
|
|
///> with holes.
|
2017-11-23 16:20:27 +00:00
|
|
|
void Unfracture( POLYGON_MODE aFastMode );
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
///> Returns true if the polygon set has any holes.
|
|
|
|
bool HasHoles() const;
|
|
|
|
|
2017-12-13 23:33:20 +00:00
|
|
|
///> Returns true if the polygon set has any holes tha share a vertex.
|
|
|
|
bool HasTouchingHoles() const;
|
|
|
|
|
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
///> Simplifies the polyset (merges overlapping polys, eliminates degeneracy/self-intersections)
|
2015-08-16 12:07:58 +00:00
|
|
|
///> For aFastMode meaning, see function booleanOp
|
2015-12-15 20:21:25 +00:00
|
|
|
void Simplify( POLYGON_MODE aFastMode );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function NormalizeAreaOutlines
|
|
|
|
* Convert a self-intersecting polygon to one (or more) non self-intersecting polygon(s)
|
|
|
|
* Removes null segments.
|
|
|
|
* @return int - the polygon count (always >= 1, because there is at least one polygon)
|
|
|
|
* There are new polygons only if the polygon count is > 1.
|
|
|
|
*/
|
|
|
|
int NormalizeAreaOutlines();
|
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
/// @copydoc SHAPE::Format()
|
2016-09-24 18:53:15 +00:00
|
|
|
const std::string Format() const override;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
|
|
|
/// @copydoc SHAPE::Parse()
|
2016-09-24 18:53:15 +00:00
|
|
|
bool Parse( std::stringstream& aStream ) override;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
/// @copydoc SHAPE::Move()
|
2016-09-24 18:53:15 +00:00
|
|
|
void Move( const VECTOR2I& aVector ) override;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2017-12-22 09:54:47 +00:00
|
|
|
/**
|
|
|
|
* Function Rotate
|
|
|
|
* rotates all vertices by a given angle
|
|
|
|
* @param aCenter is the rotation center
|
|
|
|
* @param aAngle rotation angle in radians
|
|
|
|
*/
|
|
|
|
void Rotate( double aAngle, const VECTOR2I& aCenter );
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
/// @copydoc SHAPE::IsSolid()
|
2016-09-24 18:53:15 +00:00
|
|
|
bool IsSolid() const override
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-24 18:53:15 +00:00
|
|
|
const BOX2I BBox( int aClearance = 0 ) const override;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function PointOnEdge()
|
|
|
|
*
|
|
|
|
* Checks if point aP lies on an edge or vertex of some of the outlines or holes.
|
|
|
|
* @param aP is the point to check.
|
|
|
|
* @return bool - true if the point lies on the edge of any polygon.
|
|
|
|
*/
|
|
|
|
bool PointOnEdge( const VECTOR2I& aP ) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Collide
|
|
|
|
* Checks whether the point aP collides with the inside of the polygon set; if the point
|
|
|
|
* lies on an edge or on a corner of any of the polygons, there is no collision: the edges
|
|
|
|
* does not belong to the polygon itself.
|
|
|
|
* @param aP is the VECTOR2I point whose collision with respect to the poly set
|
|
|
|
* will be tested.
|
|
|
|
* @param aClearance is the security distance; if the point lies closer to the polygon
|
|
|
|
* than aClearance distance, then there is a collision.
|
|
|
|
* @return bool - true if the point aP collides with the polygon; false in any other case.
|
|
|
|
*/
|
|
|
|
bool Collide( const VECTOR2I& aP, int aClearance = 0 ) const override;
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
// fixme: add collision support
|
2016-09-24 18:53:15 +00:00
|
|
|
bool Collide( const SEG& aSeg, int aClearance = 0 ) const override { return false; }
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function CollideVertex
|
|
|
|
* Checks whether aPoint collides with any vertex of any of the contours of the polygon.
|
|
|
|
* @param aPoint is the VECTOR2I point whose collision with respect to the polygon
|
|
|
|
* will be tested.
|
|
|
|
* @param aClearance is the security distance; if \p aPoint lies closer to a vertex than
|
|
|
|
* aClearance distance, then there is a collision.
|
|
|
|
* @param aClosestVertex is the index of the closes vertex to \p aPoint.
|
|
|
|
* @return bool - true if there is a collision, false in any other case.
|
|
|
|
*/
|
|
|
|
bool CollideVertex( const VECTOR2I& aPoint, VERTEX_INDEX& aClosestVertex,
|
|
|
|
int aClearance = 0 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function CollideEdge
|
|
|
|
* Checks whether aPoint collides with any edge of any of the contours of the polygon.
|
|
|
|
* @param aPoint is the VECTOR2I point whose collision with respect to the polygon
|
|
|
|
* will be tested.
|
|
|
|
* @param aClearance is the security distance; if \p aPoint lies closer to a vertex than
|
|
|
|
* aClearance distance, then there is a collision.
|
|
|
|
* @param aClosestVertex is the index of the closes vertex to \p aPoint.
|
|
|
|
* @return bool - true if there is a collision, false in any other case.
|
|
|
|
*/
|
|
|
|
bool CollideEdge( const VECTOR2I& aPoint, VERTEX_INDEX& aClosestVertex,
|
|
|
|
int aClearance = 0 );
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2018-02-19 00:00:29 +00:00
|
|
|
/**
|
|
|
|
* Returns true if a given subpolygon contains the point aP
|
|
|
|
*
|
|
|
|
* @param aP is the point to check
|
|
|
|
* @param aSubpolyIndex is the subpolygon to check, or -1 to check all
|
|
|
|
* @param aIgnoreHoles controls whether or not internal holes are considered
|
|
|
|
* @return true if the polygon contains the point
|
|
|
|
*/
|
|
|
|
bool Contains( const VECTOR2I& aP, int aSubpolyIndex = -1, bool aIgnoreHoles = false ) const;
|
2015-07-27 19:45:57 +00:00
|
|
|
|
|
|
|
///> Returns true if the set is empty (no polygons at all)
|
|
|
|
bool IsEmpty() const
|
|
|
|
{
|
|
|
|
return m_polys.size() == 0;
|
|
|
|
}
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function RemoveVertex
|
|
|
|
* deletes the aGlobalIndex-th vertex.
|
|
|
|
* @param aGlobalIndex is the global index of the to-be-removed vertex.
|
|
|
|
*/
|
|
|
|
void RemoveVertex( int aGlobalIndex );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function RemoveVertex
|
|
|
|
* deletes the vertex indexed by aIndex (index of polygon, contour and vertex).
|
2017-06-18 07:04:42 +00:00
|
|
|
* @param aRelativeIndices is the set of relative indices of the to-be-removed vertex.
|
2017-03-07 12:06:00 +00:00
|
|
|
*/
|
|
|
|
void RemoveVertex( VERTEX_INDEX aRelativeIndices );
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
///> Removes all outlines & holes (clears) the polygon set.
|
|
|
|
void RemoveAllContours();
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function RemoveContour
|
|
|
|
* deletes the aContourIdx-th contour of the aPolygonIdx-th polygon in the set.
|
|
|
|
* @param aContourIdx is the index of the contour in the aPolygonIdx-th polygon to be
|
|
|
|
* removed.
|
|
|
|
* @param aPolygonIdx is the index of the polygon in which the to-be-removed contour is.
|
|
|
|
* Defaults to the last polygon in the set.
|
|
|
|
*/
|
|
|
|
void RemoveContour( int aContourIdx, int aPolygonIdx = -1 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function RemoveNullSegments
|
|
|
|
* looks for null segments; ie, segments whose ends are exactly the same and deletes them.
|
|
|
|
* @return int - the number of deleted segments.
|
|
|
|
*/
|
|
|
|
int RemoveNullSegments();
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
///> Returns total number of vertices stored in the set.
|
|
|
|
int TotalVertices() const;
|
|
|
|
|
|
|
|
///> Deletes aIdx-th polygon from the set
|
|
|
|
void DeletePolygon( int aIdx );
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function Chamfer
|
|
|
|
* returns a chamfered version of the aIndex-th polygon.
|
|
|
|
* @param aDistance is the chamfering distance.
|
|
|
|
* @param aIndex is the index of the polygon to be chamfered.
|
|
|
|
* @return POLYGON - A polygon containing the chamfered version of the aIndex-th polygon.
|
|
|
|
*/
|
|
|
|
POLYGON ChamferPolygon( unsigned int aDistance, int aIndex = 0 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Fillet
|
|
|
|
* returns a filleted version of the aIndex-th polygon.
|
|
|
|
* @param aRadius is the fillet radius.
|
2018-03-22 18:02:45 +00:00
|
|
|
* @param aErrorMax is the maximum allowable deviation of the polygon from the circle
|
2017-03-07 12:06:00 +00:00
|
|
|
* @param aIndex is the index of the polygon to be filleted
|
|
|
|
* @return POLYGON - A polygon containing the filleted version of the aIndex-th polygon.
|
|
|
|
*/
|
2018-03-22 18:02:45 +00:00
|
|
|
POLYGON FilletPolygon( unsigned int aRadius, int aErrorMax, int aIndex = 0 );
|
2017-03-07 12:06:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Chamfer
|
|
|
|
* returns a chamfered version of the polygon set.
|
|
|
|
* @param aDistance is the chamfering distance.
|
|
|
|
* @return SHAPE_POLY_SET - A set containing the chamfered version of this set.
|
|
|
|
*/
|
|
|
|
SHAPE_POLY_SET Chamfer( int aDistance );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Fillet
|
|
|
|
* returns a filleted version of the polygon set.
|
|
|
|
* @param aRadius is the fillet radius.
|
2018-03-22 18:02:45 +00:00
|
|
|
* @param aErrorMax is the maximum allowable deviation of the polygon from the circle
|
2017-03-07 12:06:00 +00:00
|
|
|
* @return SHAPE_POLY_SET - A set containing the filleted version of this set.
|
|
|
|
*/
|
2018-03-22 18:02:45 +00:00
|
|
|
SHAPE_POLY_SET Fillet( int aRadius, int aErrorMax );
|
2017-03-07 12:06:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function DistanceToPolygon
|
|
|
|
* computes the minimum distance between the aIndex-th polygon and aPoint.
|
|
|
|
* @param aPoint is the point whose distance to the aIndex-th polygon has to be measured.
|
|
|
|
* @param aIndex is the index of the polygon whose distace to aPoint has to be measured.
|
|
|
|
* @return int - The minimum distance between aPoint and all the segments of the aIndex-th
|
|
|
|
* polygon. If the point is contained in the polygon, the distance is zero.
|
|
|
|
*/
|
|
|
|
int DistanceToPolygon( VECTOR2I aPoint, int aIndex );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function DistanceToPolygon
|
|
|
|
* computes the minimum distance between the aIndex-th polygon and aSegment with a
|
|
|
|
* possible width.
|
|
|
|
* @param aSegment is the segment whose distance to the aIndex-th polygon has to be
|
|
|
|
* measured.
|
|
|
|
* @param aIndex is the index of the polygon whose distace to aPoint has to be measured.
|
|
|
|
* @param aSegmentWidth is the width of the segment; defaults to zero.
|
|
|
|
* @return int - The minimum distance between aSegment and all the segments of the
|
|
|
|
* aIndex-th polygon. If the point is contained in the polygon, the
|
|
|
|
* distance is zero.
|
|
|
|
*/
|
|
|
|
int DistanceToPolygon( SEG aSegment, int aIndex, int aSegmentWidth = 0 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function DistanceToPolygon
|
|
|
|
* computes the minimum distance between aPoint and all the polygons in the set
|
|
|
|
* @param aPoint is the point whose distance to the set has to be measured.
|
|
|
|
* @return int - The minimum distance between aPoint and all the polygons in the set. If
|
|
|
|
* the point is contained in any of the polygons, the distance is zero.
|
|
|
|
*/
|
2017-06-18 07:04:42 +00:00
|
|
|
int Distance( VECTOR2I aPoint );
|
2017-03-07 12:06:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function DistanceToPolygon
|
|
|
|
* computes the minimum distance between aSegment and all the polygons in the set.
|
|
|
|
* @param aSegment is the segment whose distance to the polygon set has to be measured.
|
|
|
|
* @param aSegmentWidth is the width of the segment; defaults to zero.
|
|
|
|
* @return int - The minimum distance between aSegment and all the polygons in the set.
|
|
|
|
* If the point is contained in the polygon, the distance is zero.
|
|
|
|
*/
|
2017-09-23 09:20:10 +00:00
|
|
|
int Distance( const SEG& aSegment, int aSegmentWidth = 0 );
|
2017-03-07 12:06:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function IsVertexInHole.
|
|
|
|
* checks whether the aGlobalIndex-th vertex belongs to a hole.
|
|
|
|
* @param aGlobalIdx is the index of the vertex.
|
|
|
|
* @return bool - true if the globally indexed aGlobalIdx-th vertex belongs to a hole.
|
|
|
|
*/
|
|
|
|
bool IsVertexInHole( int aGlobalIdx );
|
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
private:
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
SHAPE_LINE_CHAIN& getContourForCorner( int aCornerId, int& aIndexWithinContour );
|
|
|
|
VECTOR2I& vertex( int aCornerId );
|
|
|
|
const VECTOR2I& cvertex( int aCornerId ) const;
|
|
|
|
|
|
|
|
|
|
|
|
void fractureSingle( POLYGON& paths );
|
2017-11-23 16:20:27 +00:00
|
|
|
void unfractureSingle ( POLYGON& path );
|
2015-07-27 19:45:57 +00:00
|
|
|
void importTree( ClipperLib::PolyTree* tree );
|
2015-08-16 12:07:58 +00:00
|
|
|
|
|
|
|
/** Function booleanOp
|
|
|
|
* this is the engine to execute all polygon boolean transforms
|
|
|
|
* (AND, OR, ... and polygon simplification (merging overlaping polygons)
|
|
|
|
* @param aType is the transform type ( see ClipperLib::ClipType )
|
|
|
|
* @param aOtherShape is the SHAPE_LINE_CHAIN to combine with me.
|
2015-12-15 20:21:25 +00:00
|
|
|
* @param aFastMode is an option to choose if the result can be a weak polygon
|
2015-08-16 12:07:58 +00:00
|
|
|
* or a stricty simple polygon.
|
2015-12-15 20:21:25 +00:00
|
|
|
* if aFastMode is PM_FAST the result can be a weak polygon
|
|
|
|
* if aFastMode is PM_STRICTLY_SIMPLE (default) the result is (theorically) a strictly
|
2015-08-24 13:55:22 +00:00
|
|
|
* simple polygon, but calculations can be really significantly time consuming
|
2015-08-16 12:07:58 +00:00
|
|
|
*/
|
|
|
|
void booleanOp( ClipperLib::ClipType aType,
|
2015-12-15 20:21:25 +00:00
|
|
|
const SHAPE_POLY_SET& aOtherShape, POLYGON_MODE aFastMode );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-08-24 13:55:22 +00:00
|
|
|
void booleanOp( ClipperLib::ClipType aType,
|
|
|
|
const SHAPE_POLY_SET& aShape,
|
2015-12-15 20:21:25 +00:00
|
|
|
const SHAPE_POLY_SET& aOtherShape, POLYGON_MODE aFastMode );
|
2015-08-24 13:55:22 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
bool pointInPolygon( const VECTOR2I& aP, const SHAPE_LINE_CHAIN& aPath ) const;
|
|
|
|
|
|
|
|
const ClipperLib::Path convertToClipper( const SHAPE_LINE_CHAIN& aPath, bool aRequiredOrientation );
|
|
|
|
const SHAPE_LINE_CHAIN convertFromClipper( const ClipperLib::Path& aPath );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* containsSingle function
|
|
|
|
* Checks whether the point aP is inside the aSubpolyIndex-th polygon of the polyset. If
|
|
|
|
* the points lies on an edge, the polygon is considered to contain it.
|
|
|
|
* @param aP is the VECTOR2I point whose position with respect to the inside of
|
|
|
|
* the aSubpolyIndex-th polygon will be tested.
|
|
|
|
* @param aSubpolyIndex is an integer specifying which polygon in the set has to be
|
|
|
|
* checked.
|
2018-02-19 00:00:29 +00:00
|
|
|
* @param aIgnoreHoles can be set to true to ignore internal holes in the polygon
|
2017-03-07 12:06:00 +00:00
|
|
|
* @return bool - true if aP is inside aSubpolyIndex-th polygon; false in any other
|
|
|
|
* case.
|
|
|
|
*/
|
2018-02-19 00:00:29 +00:00
|
|
|
bool containsSingle( const VECTOR2I& aP, int aSubpolyIndex, bool aIgnoreHoles = false ) const;
|
2017-03-07 12:06:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Operations ChamferPolygon and FilletPolygon are computed under the private chamferFillet
|
|
|
|
* method; this enum is defined to make the necessary distinction when calling this method
|
|
|
|
* from the public ChamferPolygon and FilletPolygon methods.
|
|
|
|
*/
|
|
|
|
enum CORNER_MODE
|
|
|
|
{
|
|
|
|
CHAMFERED,
|
|
|
|
FILLETED
|
|
|
|
};
|
|
|
|
|
2017-11-23 16:20:27 +00:00
|
|
|
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
|
|
|
* Function chamferFilletPolygon
|
|
|
|
* Returns the camfered or filleted version of the aIndex-th polygon in the set, depending
|
|
|
|
* on the aMode selected
|
|
|
|
* @param aMode represent which action will be taken: CORNER_MODE::CHAMFERED will
|
|
|
|
* return a chamfered version of the polygon, CORNER_MODE::FILLETED will
|
|
|
|
* return a filleted version of the polygon.
|
|
|
|
* @param aDistance is the chamfering distance if aMode = CHAMFERED; if aMode = FILLETED,
|
|
|
|
* is the filleting radius.
|
|
|
|
* @param aIndex is the index of the polygon that will be chamfered/filleted.
|
2018-03-22 18:02:45 +00:00
|
|
|
* @param aErrorMax is the maximum allowable deviation of the polygon from the circle
|
|
|
|
* if aMode = FILLETED. If aMode = CHAMFERED, it is unused.
|
2017-03-07 12:06:00 +00:00
|
|
|
* @return POLYGON - the chamfered/filleted version of the polygon.
|
|
|
|
*/
|
|
|
|
POLYGON chamferFilletPolygon( CORNER_MODE aMode, unsigned int aDistance,
|
2018-03-22 18:02:45 +00:00
|
|
|
int aIndex, int aErrorMax = -1 );
|
2017-03-07 12:06:00 +00:00
|
|
|
|
2017-12-13 23:33:20 +00:00
|
|
|
///> Returns true if the polygon set has any holes that touch share a vertex.
|
|
|
|
bool hasTouchingHoles( const POLYGON& aPoly ) const;
|
|
|
|
|
2017-10-25 07:39:26 +00:00
|
|
|
typedef std::vector<POLYGON> POLYSET;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2017-10-25 07:39:26 +00:00
|
|
|
POLYSET m_polys;
|
2017-11-23 16:20:27 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2017-12-08 11:20:02 +00:00
|
|
|
SHAPE_POLY_SET& operator=( const SHAPE_POLY_SET& );
|
|
|
|
|
2017-11-23 16:20:27 +00:00
|
|
|
void CacheTriangulation();
|
|
|
|
bool IsTriangulationUpToDate() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void triangulateSingle( const POLYGON& aPoly, SHAPE_POLY_SET::TRIANGULATED_POLYGON& aResult );
|
|
|
|
|
|
|
|
MD5_HASH checksum() const;
|
|
|
|
|
2017-12-08 11:20:02 +00:00
|
|
|
std::vector<std::unique_ptr<TRIANGULATED_POLYGON>> m_triangulatedPolys;
|
2017-11-23 16:20:27 +00:00
|
|
|
bool m_triangulationValid = false;
|
|
|
|
MD5_HASH m_hash;
|
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|