26 #ifndef __SHAPE_POLY_SET_H 27 #define __SHAPE_POLY_SET_H 31 #include <geometry/shape.h> 32 #include <geometry/shape_line_chain.h> 34 #include "clipper.hpp" 58 typedef std::vector<SHAPE_LINE_CHAIN>
POLYGON;
73 VERTEX_INDEX() : m_polygon(-1), m_contour(-1), m_vertex(-1)
95 return m_currentVertex + 1 == m_poly->CPolygon( m_currentPolygon )[m_currentContour].PointCount();
104 return m_currentPolygon == m_lastPolygon;
107 operator bool()
const 109 return m_currentPolygon <= m_lastPolygon;
127 if( m_currentVertex >= m_poly->CPolygon( m_currentPolygon )[m_currentContour].PointCount() )
134 int totalContours = m_poly->CPolygon( m_currentPolygon ).size();
136 if( m_currentContour >= totalContours )
138 m_currentContour = 0;
146 if( m_currentVertex >= m_poly->CPolygon( m_currentPolygon )[0].PointCount() )
154 void operator++(
int dummy )
166 return m_poly->Polygon( m_currentPolygon )[m_currentContour].Point( m_currentVertex );
199 int m_currentPolygon;
200 int m_currentContour;
221 return m_currentPolygon == m_lastPolygon;
224 operator bool()
const 226 return m_currentPolygon <= m_lastPolygon;
244 last = m_poly->CPolygon( m_currentPolygon )[m_currentContour].SegmentCount();
247 if( m_currentSegment >= last )
249 m_currentSegment = 0;
254 int totalContours = m_poly->CPolygon( m_currentPolygon ).size();
256 if( m_currentContour >= totalContours )
258 m_currentContour = 0;
265 last = m_poly->CPolygon( m_currentPolygon )[0].SegmentCount();
268 if( m_currentSegment >= last )
270 m_currentSegment = 0;
276 void operator++(
int dummy )
288 return m_poly->Polygon( m_currentPolygon )[m_currentContour].Segment( m_currentSegment );
322 if( m_poly == aOther.m_poly && m_currentPolygon == aOther.m_currentPolygon &&
323 m_currentContour == aOther.m_currentContour )
327 numSeg = m_poly->CPolygon( m_currentPolygon )[m_currentContour].SegmentCount();
333 int indexDiff = abs( m_currentSegment - aOther.m_currentSegment );
335 return ( indexDiff == 1 ) || ( indexDiff == (numSeg - 1) );
345 int m_currentPolygon;
346 int m_currentContour;
347 int m_currentSegment;
403 int NewHole(
int aOutline = -1 );
424 int Append(
int x,
int y,
int aOutline = -1,
int aHole = -1,
425 bool aAllowDuplication =
false );
431 void Append(
const VECTOR2I& aP,
int aOutline = -1,
int aHole = -1 );
494 int VertexCount(
int aOutline = -1,
int aHole = -1 )
const;
499 if( (aOutline > (
int)m_polys.size()) || (m_polys[aOutline].size() < 2) )
501 return m_polys[aOutline].size() - 1;
507 return m_polys[aIndex][0];
523 return Subset( aPolygonIndex, aPolygonIndex + 1 );
529 return m_polys[aOutline][aHole + 1];
535 return m_polys[aIndex];
540 return m_polys[aIndex][0];
545 return m_polys[aOutline][aHole + 1];
548 const POLYGON& CPolygon(
int aIndex )
const 550 return m_polys[aIndex];
563 ITERATOR
Iterate(
int aFirst,
int aLast,
bool aIterateHoles =
false )
568 iter.m_currentPolygon = aFirst;
569 iter.m_lastPolygon = aLast < 0 ?
OutlineCount() - 1 : aLast;
570 iter.m_currentContour = 0;
571 iter.m_currentVertex = 0;
572 iter.m_iterateHoles = aIterateHoles;
585 return Iterate( aOutline, aOutline );
596 return Iterate( aOutline, aOutline,
true );
620 CONST_ITERATOR CIterate(
int aFirst,
int aLast,
bool aIterateHoles =
false )
const 625 iter.m_currentPolygon = aFirst;
626 iter.m_lastPolygon = aLast < 0 ?
OutlineCount() - 1 : aLast;
627 iter.m_currentContour = 0;
628 iter.m_currentVertex = 0;
629 iter.m_iterateHoles = aIterateHoles;
634 CONST_ITERATOR CIterate(
int aOutline )
const 636 return CIterate( aOutline, aOutline );
639 CONST_ITERATOR CIterateWithHoles(
int aOutline )
const 641 return CIterate( aOutline, aOutline,
true );
644 CONST_ITERATOR CIterate()
const 649 CONST_ITERATOR CIterateWithHoles()
const 654 ITERATOR IterateFromVertexWithHoles(
int aGlobalIdx )
663 throw( std::out_of_range(
"aGlobalIndex-th vertex does not exist" ) );
666 iter.m_currentPolygon = indices.
m_polygon;
667 iter.m_currentContour = indices.
m_contour;
668 iter.m_currentVertex = indices.
m_vertex;
677 SEGMENT_ITERATOR iter;
680 iter.m_currentPolygon = aFirst;
681 iter.m_lastPolygon = aLast < 0 ?
OutlineCount() - 1 : aLast;
682 iter.m_currentContour = 0;
683 iter.m_currentSegment = 0;
684 iter.m_iterateHoles = aIterateHoles;
723 PM_STRICTLY_SIMPLE =
false 754 void Inflate(
int aFactor,
int aCircleSegmentsCount );
778 const std::string
Format()
const override;
781 bool Parse( std::stringstream& aStream )
override;
792 const BOX2I BBox(
int aClearance = 0 )
const override;
817 bool Collide(
const SEG& aSeg,
int aClearance = 0 )
const override {
return false; }
830 int aClearance = 0 );
843 int aClearance = 0 );
852 return m_polys.size() == 0;
912 POLYGON
FilletPolygon(
unsigned int aRadius,
unsigned int aSegments,
int aIndex = 0 );
972 int Distance(
SEG aSegment,
int aSegmentWidth = 0 );
984 SHAPE_LINE_CHAIN& getContourForCorner(
int aCornerId,
int& aIndexWithinContour );
986 const VECTOR2I& cvertex(
int aCornerId )
const;
989 void fractureSingle( POLYGON& paths );
1003 void booleanOp( ClipperLib::ClipType aType,
1006 void booleanOp( ClipperLib::ClipType aType,
1012 const ClipperLib::Path convertToClipper(
const SHAPE_LINE_CHAIN& aPath,
bool aRequiredOrientation );
1013 const SHAPE_LINE_CHAIN convertFromClipper(
const ClipperLib::Path& aPath );
1026 bool containsSingle(
const VECTOR2I& aP,
int aSubpolyIndex )
const;
1053 POLYGON chamferFilletPolygon( CORNER_MODE aMode,
unsigned int aDistance,
1054 int aIndex,
int aSegments = -1 );
1056 typedef std::vector<POLYGON> Polyset;
int TotalVertices() const
Returns total number of vertices stored in the set.
Definition: shape_poly_set.cpp:1340
std::vector< SHAPE_LINE_CHAIN > POLYGON
represents a single polygon outline with holes.
Definition: shape_poly_set.h:58
int NewHole(int aOutline=-1)
Creates a new hole in a given outline
Definition: shape_poly_set.cpp:161
POLYGON ChamferPolygon(unsigned int aDistance, int aIndex=0)
Function Chamfer returns a chamfered version of the aIndex-th polygon.
Definition: shape_poly_set.cpp:1356
int OutlineCount() const
Returns the number of outlines in the set
Definition: shape_poly_set.h:491
bool IsEndContour() const
Function IsEndContour.
Definition: shape_poly_set.h:93
SEGMENT_ITERATOR IterateSegmentsWithHoles()
Returns an iterator object, for all outlines in the set (with holes)
Definition: shape_poly_set.h:702
void BooleanAdd(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Performs boolean polyset union For aFastMode meaning, see function booleanOp
Definition: shape_poly_set.cpp:536
ITERATOR Iterate(int aOutline)
Function Iterate.
Definition: shape_poly_set.h:583
bool CollideEdge(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 polyg...
Definition: shape_poly_set.cpp:1172
bool Collide(const SEG &aSeg, int aClearance=0) const override
Function Collide()
Definition: shape_poly_set.h:817
const VECTOR2I & CVertex(int aIndex, int aOutline, int aHole) const
Returns the index-th vertex in a given hole outline within a given outline
Definition: shape_poly_set.cpp:274
bool IsEmpty() const
Returns true if the set is empty (no polygons at all)
Definition: shape_poly_set.h:850
bool IsPolygonSelfIntersecting(int aPolygonIndex)
Function IsPolygonSelfIntersecting.
Definition: shape_poly_set.cpp:375
int NormalizeAreaOutlines()
Function NormalizeAreaOutlines Convert a self-intersecting polygon to one (or more) non self-intersec...
Definition: shape_poly_set.cpp:885
int VertexCount(int aOutline=-1, int aHole=-1) const
Returns the number of vertices in a given outline/hole
Definition: shape_poly_set.cpp:221
bool IsSolid() const override
Definition: shape_poly_set.h:787
bool Parse(std::stringstream &aStream) override
Definition: shape_poly_set.cpp:940
bool HasHoles() const
Returns true if the polygon set has any holes.
Definition: shape_poly_set.cpp:862
void Advance()
Function Advance advances the indices of the current vertex/outline/contour, checking whether the ver...
Definition: shape_poly_set.h:117
Struct VERTEX_INDEX.
Definition: shape_poly_set.h:67
SHAPE_LINE_CHAIN & Hole(int aOutline, int aHole)
Returns the reference to aHole-th hole in the aIndex-th outline
Definition: shape_poly_set.h:527
Class SEGMENT_ITERATOR_TEMPLATE.
Definition: shape_poly_set.h:212
ITERATOR Iterate()
Function Iterate.
Definition: shape_poly_set.h:604
ITERATOR Iterate(int aFirst, int aLast, bool aIterateHoles=false)
Function Iterate returns an object to iterate through the points of the polygons between aFirst and a...
Definition: shape_poly_set.h:563
bool PointOnEdge(const VECTOR2I &aP) const
Function PointOnEdge()
Definition: shape_poly_set.cpp:1014
VERTEX_INDEX GetIndex()
Function GetIndex.
Definition: shape_poly_set.h:183
bool IsLastPolygon() const
Function IsLastOutline.
Definition: shape_poly_set.h:219
bool IsVertexInHole(int aGlobalIdx)
Function IsVertexInHole.
Definition: shape_poly_set.cpp:1467
VECTOR2I & Vertex(int aIndex, int aOutline, int aHole)
Returns the index-th vertex in a given hole outline within a given outline
Definition: shape_poly_set.cpp:255
void DeletePolygon(int aIdx)
Deletes aIdx-th polygon from the set
Definition: shape_poly_set.cpp:1116
bool GetNeighbourIndexes(int aGlobalIndex, int *aPrevious, int *aNext)
Returns the global indexes of the previous and the next corner of the aGlobalIndex-th corner of a con...
Definition: shape_poly_set.cpp:328
bool Contains(const VECTOR2I &aP, int aSubpolyIndex=-1) const
Returns true if a given subpolygon contains the point aP.
Definition: shape_poly_set.cpp:1202
void Inflate(int aFactor, int aCircleSegmentsCount)
Performs outline inflation/deflation, using round corners.
Definition: shape_poly_set.cpp:572
SHAPE_POLY_SET Fillet(int aRadius, int aSegments)
Function Fillet returns a filleted version of the polygon set.
Definition: shape_poly_set.cpp:1491
void Move(const VECTOR2I &aVector) override
Definition: shape_poly_set.cpp:1328
Definition: clipper.hpp:159
ITERATOR IterateWithHoles(int aOutline)
Function IterateWithHoles.
Definition: shape_poly_set.h:594
Class SHAPE_POLY_SET.
Definition: shape_poly_set.h:53
SHAPE_LINE_CHAIN & Outline(int aIndex)
Returns the reference to aIndex-th outline in the set
Definition: shape_poly_set.h:505
SEGMENT_ITERATOR IterateSegments(int aPolygonIdx)
Returns an iterator object, for iterating aPolygonIdx-th polygon edges
Definition: shape_poly_set.h:690
void Advance()
Function Advance advances the indices of the current vertex/outline/contour, checking whether the ver...
Definition: shape_poly_set.h:234
bool IsAdjacent(SEGMENT_ITERATOR_TEMPLATE< T > aOther)
Function IsAdjacent.
Definition: shape_poly_set.h:318
bool CollideVertex(const VECTOR2I &aPoint, VERTEX_INDEX &aClosestVertex, int aClearance=0)
Function CollideVertex Checks whether aPoint collides with any vertex of any of the contours of the p...
Definition: shape_poly_set.cpp:1134
SEGMENT_ITERATOR IterateSegmentsWithHoles(int aOutline)
Returns an iterator object, for the aOutline-th outline in the set (with holes)
Definition: shape_poly_set.h:708
void Simplify(POLYGON_MODE aFastMode)
Simplifies the polyset (merges overlapping polys, eliminates degeneracy/self-intersections) For aFast...
Definition: shape_poly_set.cpp:877
Class SHAPE.
Definition: shape.h:57
bool Collide(const VECTOR2I &aP, int aClearance=0) const override
Function Collide Checks whether the point aP collides with the inside of the polygon set; if the poin...
Definition: shape_poly_set.cpp:1031
const std::string Format() const override
Definition: shape_poly_set.cpp:918
int RemoveNullSegments()
Function RemoveNullSegments looks for null segments; ie, segments whose ends are exactly the same and...
Definition: shape_poly_set.cpp:1063
void BooleanIntersection(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Performs boolean polyset intersection For aFastMode meaning, see function booleanOp ...
Definition: shape_poly_set.cpp:548
bool GetRelativeIndices(int aGlobalIdx, VERTEX_INDEX *aRelativeIndices) const
Function GetRelativeIndices.
Definition: shape_poly_set.cpp:67
int NewOutline()
Creates a new empty polygon in the set and returns its index
Definition: shape_poly_set.cpp:150
int HoleCount(int aOutline) const
Returns the number of holes in a given outline
Definition: shape_poly_set.h:497
int m_contour
Definition: shape_poly_set.h:70
void Fracture(POLYGON_MODE aFastMode)
Converts a set of polygons with holes to a singe outline with "slits"/"fractures" connecting the oute...
Definition: shape_poly_set.cpp:851
int AddHole(const SHAPE_LINE_CHAIN &aHole, int aOutline=-1)
Adds a new hole to the given outline (default: last) and returns its index
Definition: shape_poly_set.cpp:431
bool GetGlobalIndex(VERTEX_INDEX aRelativeIndices, int &aGlobalIdx)
Function GetGlobalIndex computes the global index of a vertex from the relative indices of polygon...
Definition: shape_poly_set.cpp:107
int DistanceToPolygon(VECTOR2I aPoint, int aIndex)
Function DistanceToPolygon computes the minimum distance between the aIndex-th polygon and aPoint...
Definition: shape_poly_set.cpp:1370
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index
Definition: shape_poly_set.cpp:417
SHAPE_POLY_SET Chamfer(int aDistance)
Function Chamfer returns a chamfered version of the polygon set.
Definition: shape_poly_set.cpp:1480
ITERATOR IterateWithHoles()
Function IterateWithHoles.
Definition: shape_poly_set.h:614
Class SHAPE_LINE_CHAIN.
Definition: shape_line_chain.h:47
void RemoveVertex(int aGlobalIndex)
Function RemoveVertex deletes the aGlobalIndex-th vertex.
Definition: shape_poly_set.cpp:1223
int Distance(VECTOR2I aPoint)
Function DistanceToPolygon computes the minimum distance between aPoint and all the polygons in the s...
Definition: shape_poly_set.cpp:1431
Class ITERATOR_TEMPLATE.
Definition: shape_poly_set.h:84
void RemoveAllContours()
Removes all outlines & holes (clears) the polygon set.
Definition: shape_poly_set.cpp:1047
int m_polygon
Definition: shape_poly_set.h:69
void InsertVertex(int aGlobalIndex, VECTOR2I aNewVertex)
Function InsertVertex Adds a vertex in the globally indexed position aGlobalIndex.
Definition: shape_poly_set.cpp:198
int m_vertex
Definition: shape_poly_set.h:71
POLYGON_MODE
operations on polygons use a aFastMode param if aFastMode is PM_FAST (true) the result can be a weak ...
Definition: shape_poly_set.h:720
void BooleanSubtract(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Performs boolean polyset difference For aFastMode meaning, see function booleanOp ...
Definition: shape_poly_set.cpp:542
POLYGON & Polygon(int aIndex)
Returns the aIndex-th subpolygon in the set
Definition: shape_poly_set.h:533
POLYGON FilletPolygon(unsigned int aRadius, unsigned int aSegments, int aIndex=0)
Function Fillet returns a filleted version of the aIndex-th polygon.
Definition: shape_poly_set.cpp:1362
SHAPE * Clone() const override
Function Clone()
Definition: shape_poly_set.cpp:61
bool IsSelfIntersecting()
Function IsSelfIntersecting Checks whether any of the polygons in the set is self intersecting...
Definition: shape_poly_set.cpp:405
SEGMENT_ITERATOR IterateSegments()
Returns an iterator object, for all outlines in the set (no holes)
Definition: shape_poly_set.h:696
const BOX2I BBox(int aClearance=0) const override
Function BBox()
Definition: shape_poly_set.cpp:997
VERTEX_INDEX GetIndex()
Function GetIndex.
Definition: shape_poly_set.h:300
SEGMENT_ITERATOR IterateSegments(int aFirst, int aLast, bool aIterateHoles=false)
Returns an iterator object, for iterating between aFirst and aLast outline, with or without holes (de...
Definition: shape_poly_set.h:675
SHAPE_POLY_SET Subset(int aFirstPolygon, int aLastPolygon)
Function Subset returns a subset of the polygons in this set, the ones between aFirstPolygon and aLas...
Definition: shape_poly_set.cpp:240
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline) ...
Definition: shape_poly_set.cpp:177
void RemoveContour(int aContourIdx, int aPolygonIdx=-1)
Function RemoveContour deletes the aContourIdx-th contour of the aPolygonIdx-th polygon in the set...
Definition: shape_poly_set.cpp:1053
bool IsLastPolygon() const
Function IsLastOutline.
Definition: shape_poly_set.h:102