KiCad PCB EDA Suite
shape_poly_set.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2017 CERN
5  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  * @author Alejandro GarcĂ­a Montoro <alejandro.garciamontoro@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #ifndef __SHAPE_POLY_SET_H
27 #define __SHAPE_POLY_SET_H
28 
29 #include <vector>
30 #include <cstdio>
31 #include <memory>
32 #include <geometry/shape.h>
34 
35 #include "clipper.hpp"
36 
37 #include <md5_hash.h>
38 
39 
56 class SHAPE_POLY_SET : public SHAPE
57 {
58  public:
61  typedef std::vector<SHAPE_LINE_CHAIN> POLYGON;
62 
64 
66  {
67  public:
68  struct TRI
69  {
70  TRI() : a(0), b(0), c(0)
71  {
72  }
73 
74  int a, b, c;
75  };
76 
78 
79  void Clear();
80 
81  void AllocateVertices( int aSize );
82  void AllocateTriangles ( int aSize );
83 
84  void GetTriangle( int index, VECTOR2I& a, VECTOR2I& b, VECTOR2I& c ) const
85  {
86  auto tri = &m_triangles[ index ];
87  a = m_vertices[ tri->a ];
88  b = m_vertices[ tri->b ];
89  c = m_vertices[ tri->c ];
90  }
91 
92  void SetTriangle( int aIndex, const TRI& aTri )
93  {
94  m_triangles[aIndex] = aTri;
95  }
96 
97  int AddVertex( const VECTOR2I& aP )
98  {
99  m_vertices[ m_vertexCount ] = aP;
100  return (m_vertexCount++);
101  }
102 
103  int GetTriangleCount() const
104  {
105  return m_triangleCount;
106  }
107 
108  int GetVertexCount() const
109  {
110  return m_vertexCount;
111  }
112 
113  private:
114 
115  TRI* m_triangles = nullptr;
116  VECTOR2I* m_vertices = nullptr;
117  int m_vertexCount = 0;
119  };
120 
128  typedef struct VERTEX_INDEX
129  {
130  int m_polygon;
131  int m_contour;
132  int m_vertex;
134  VERTEX_INDEX() : m_polygon(-1), m_contour(-1), m_vertex(-1)
135  {
136  }
137  } VERTEX_INDEX;
138 
144  template <class T>
146  {
147  public:
148 
154  bool IsEndContour() const
155  {
156  return m_currentVertex + 1 == m_poly->CPolygon( m_currentPolygon )[m_currentContour].PointCount();
157  }
158 
163  bool IsLastPolygon() const
164  {
165  return m_currentPolygon == m_lastPolygon;
166  }
167 
168  operator bool() const
169  {
170  return m_currentPolygon <= m_lastPolygon;
171  }
172 
178  void Advance()
179  {
180  // Advance vertex index
181  m_currentVertex ++;
182 
183  // Check whether the user wants to iterate through the vertices of the holes
184  // and behave accordingly
185  if( m_iterateHoles )
186  {
187  // If the last vertex of the contour was reached, advance the contour index
188  if( m_currentVertex >= m_poly->CPolygon( m_currentPolygon )[m_currentContour].PointCount() )
189  {
190  m_currentVertex = 0;
191  m_currentContour++;
192 
193  // If the last contour of the current polygon was reached, advance the
194  // outline index
195  int totalContours = m_poly->CPolygon( m_currentPolygon ).size();
196 
197  if( m_currentContour >= totalContours )
198  {
199  m_currentContour = 0;
200  m_currentPolygon++;
201  }
202  }
203  }
204  else
205  {
206  // If the last vertex of the outline was reached, advance to the following polygon
207  if( m_currentVertex >= m_poly->CPolygon( m_currentPolygon )[0].PointCount() )
208  {
209  m_currentVertex = 0;
210  m_currentPolygon++;
211  }
212  }
213  }
214 
215  void operator++( int dummy )
216  {
217  Advance();
218  }
219 
220  void operator++()
221  {
222  Advance();
223  }
224 
225  T& Get()
226  {
227  return m_poly->Polygon( m_currentPolygon )[m_currentContour].Point( m_currentVertex );
228  }
229 
231  {
232  return Get();
233  }
234 
236  {
237  return &Get();
238  }
239 
245  {
246  VERTEX_INDEX index;
247 
248  index.m_polygon = m_currentPolygon;
249  index.m_contour = m_currentContour;
250  index.m_vertex = m_currentVertex;
251 
252  return index;
253  }
254 
255 
256  private:
257  friend class SHAPE_POLY_SET;
258 
265  };
266 
272  template <class T>
274  {
275  public:
280  bool IsLastPolygon() const
281  {
282  return m_currentPolygon == m_lastPolygon;
283  }
284 
285  operator bool() const
286  {
287  return m_currentPolygon <= m_lastPolygon;
288  }
289 
295  void Advance()
296  {
297  // Advance vertex index
298  m_currentSegment++;
299  int last;
300 
301  // Check whether the user wants to iterate through the vertices of the holes
302  // and behave accordingly
303  if( m_iterateHoles )
304  {
305  last = m_poly->CPolygon( m_currentPolygon )[m_currentContour].SegmentCount();
306 
307  // If the last vertex of the contour was reached, advance the contour index
308  if( m_currentSegment >= last )
309  {
310  m_currentSegment = 0;
311  m_currentContour++;
312 
313  // If the last contour of the current polygon was reached, advance the
314  // outline index
315  int totalContours = m_poly->CPolygon( m_currentPolygon ).size();
316 
317  if( m_currentContour >= totalContours )
318  {
319  m_currentContour = 0;
320  m_currentPolygon++;
321  }
322  }
323  }
324  else
325  {
326  last = m_poly->CPolygon( m_currentPolygon )[0].SegmentCount();
327  // If the last vertex of the outline was reached, advance to the following
328  // polygon
329  if( m_currentSegment >= last )
330  {
331  m_currentSegment = 0;
332  m_currentPolygon++;
333  }
334  }
335  }
336 
337  void operator++( int dummy )
338  {
339  Advance();
340  }
341 
342  void operator++()
343  {
344  Advance();
345  }
346 
347  T Get()
348  {
349  return m_poly->Polygon( m_currentPolygon )[m_currentContour].Segment( m_currentSegment );
350  }
351 
353  {
354  return Get();
355  }
356 
362  {
363  VERTEX_INDEX index;
364 
365  index.m_polygon = m_currentPolygon;
366  index.m_contour = m_currentContour;
367  index.m_vertex = m_currentSegment;
368 
369  return index;
370  }
371 
380  {
381  // Check that both iterators point to the same contour of the same polygon of the
382  // same polygon set
383  if( m_poly == aOther.m_poly && m_currentPolygon == aOther.m_currentPolygon &&
384  m_currentContour == aOther.m_currentContour )
385  {
386  // Compute the total number of segments
387  int numSeg;
388  numSeg = m_poly->CPolygon( m_currentPolygon )[m_currentContour].SegmentCount();
389 
390  // Compute the difference of the segment indices. If it is exactly one, they
391  // are adjacent. The only missing case where they also are adjacent is when
392  // the segments are the first and last one, in which case the difference
393  // always equals the total number of segments minus one.
394  int indexDiff = abs( m_currentSegment - aOther.m_currentSegment );
395 
396  return ( indexDiff == 1 ) || ( indexDiff == (numSeg - 1) );
397  }
398 
399  return false;
400  }
401 
402  private:
403  friend class SHAPE_POLY_SET;
404 
411  };
412 
413  // Iterator and const iterator types to visit polygon's points.
416 
417  // Iterator and const iterator types to visit polygon's edges.
420 
421  SHAPE_POLY_SET();
422 
428  SHAPE_POLY_SET( const SHAPE_POLY_SET& aOther );
429 
430  ~SHAPE_POLY_SET();
431 
444  bool GetRelativeIndices( int aGlobalIdx, VERTEX_INDEX* aRelativeIndices) const;
445 
455  bool GetGlobalIndex( VERTEX_INDEX aRelativeIndices, int& aGlobalIdx );
456 
458  SHAPE* Clone() const override;
459 
461  int NewOutline();
462 
464  int NewHole( int aOutline = -1 );
465 
467  int AddOutline( const SHAPE_LINE_CHAIN& aOutline );
468 
470  int AddHole( const SHAPE_LINE_CHAIN& aHole, int aOutline = -1 );
471 
473 
485  int Append( int x, int y, int aOutline = -1, int aHole = -1,
486  bool aAllowDuplication = false );
487 
489  void Append( const SHAPE_POLY_SET& aSet );
490 
492  void Append( const VECTOR2I& aP, int aOutline = -1, int aHole = -1 );
493 
501  void InsertVertex( int aGlobalIndex, VECTOR2I aNewVertex );
502 
504  VECTOR2I& Vertex( int aIndex, int aOutline, int aHole );
505 
507  const VECTOR2I& CVertex( int aIndex, int aOutline, int aHole ) const;
508 
510  VECTOR2I& Vertex( int aGlobalIndex );
511 
513  const VECTOR2I& CVertex( int aGlobalIndex ) const;
514 
516  VECTOR2I& Vertex( VERTEX_INDEX aIndex );
517 
519  const VECTOR2I& CVertex( VERTEX_INDEX aIndex ) const;
520 
532  bool GetNeighbourIndexes( int aGlobalIndex, int* aPrevious, int* aNext );
533 
534 
542  bool IsPolygonSelfIntersecting( int aPolygonIndex );
543 
549  bool IsSelfIntersecting();
550 
552  int OutlineCount() const { return m_polys.size(); }
553 
555  int VertexCount( int aOutline = -1, int aHole = -1 ) const;
556 
558  int HoleCount( int aOutline ) const
559  {
560  if( ( aOutline < 0 ) || (aOutline >= (int)m_polys.size()) || (m_polys[aOutline].size() < 2) )
561  return 0;
562 
563  // the first polygon in m_polys[aOutline] is the main contour,
564  // only others are holes:
565  return m_polys[aOutline].size() - 1;
566  }
567 
569  SHAPE_LINE_CHAIN& Outline( int aIndex )
570  {
571  return m_polys[aIndex][0];
572  }
573 
583  SHAPE_POLY_SET Subset( int aFirstPolygon, int aLastPolygon );
584 
585  SHAPE_POLY_SET UnitSet( int aPolygonIndex )
586  {
587  return Subset( aPolygonIndex, aPolygonIndex + 1 );
588  }
589 
591  SHAPE_LINE_CHAIN& Hole( int aOutline, int aHole )
592  {
593  return m_polys[aOutline][aHole + 1];
594  }
595 
597  POLYGON& Polygon( int aIndex )
598  {
599  return m_polys[aIndex];
600  }
601 
602  const POLYGON& Polygon( int aIndex ) const
603  {
604  return m_polys[aIndex];
605  }
606 
607  const TRIANGULATED_POLYGON* TriangulatedPolygon( int aIndex ) const
608  {
609  return m_triangulatedPolys[aIndex].get();
610  }
611 
612 
613  const SHAPE_LINE_CHAIN& COutline( int aIndex ) const
614  {
615  return m_polys[aIndex][0];
616  }
617 
618  const SHAPE_LINE_CHAIN& CHole( int aOutline, int aHole ) const
619  {
620  return m_polys[aOutline][aHole + 1];
621  }
622 
623  const POLYGON& CPolygon( int aIndex ) const
624  {
625  return m_polys[aIndex];
626  }
627 
638  ITERATOR Iterate( int aFirst, int aLast, bool aIterateHoles = false )
639  {
640  ITERATOR iter;
641 
642  iter.m_poly = this;
643  iter.m_currentPolygon = aFirst;
644  iter.m_lastPolygon = aLast < 0 ? OutlineCount() - 1 : aLast;
645  iter.m_currentContour = 0;
646  iter.m_currentVertex = 0;
647  iter.m_iterateHoles = aIterateHoles;
648 
649  return iter;
650  }
651 
658  ITERATOR Iterate( int aOutline )
659  {
660  return Iterate( aOutline, aOutline );
661  }
662 
669  ITERATOR IterateWithHoles( int aOutline )
670  {
671  return Iterate( aOutline, aOutline, true );
672  }
673 
679  ITERATOR Iterate()
680  {
681  return Iterate( 0, OutlineCount() - 1 );
682  }
683 
689  ITERATOR IterateWithHoles()
690  {
691  return Iterate( 0, OutlineCount() - 1, true );
692  }
693 
694 
695  CONST_ITERATOR CIterate( int aFirst, int aLast, bool aIterateHoles = false ) const
696  {
697  CONST_ITERATOR iter;
698 
699  iter.m_poly = const_cast<SHAPE_POLY_SET*>( this );
700  iter.m_currentPolygon = aFirst;
701  iter.m_lastPolygon = aLast < 0 ? OutlineCount() - 1 : aLast;
702  iter.m_currentContour = 0;
703  iter.m_currentVertex = 0;
704  iter.m_iterateHoles = aIterateHoles;
705 
706  return iter;
707  }
708 
709  CONST_ITERATOR CIterate( int aOutline ) const
710  {
711  return CIterate( aOutline, aOutline );
712  }
713 
714  CONST_ITERATOR CIterateWithHoles( int aOutline ) const
715  {
716  return CIterate( aOutline, aOutline, true );
717  }
718 
719  CONST_ITERATOR CIterate() const
720  {
721  return CIterate( 0, OutlineCount() - 1 );
722  }
723 
724  CONST_ITERATOR CIterateWithHoles() const
725  {
726  return CIterate( 0, OutlineCount() - 1, true );
727  }
728 
729  ITERATOR IterateFromVertexWithHoles( int aGlobalIdx )
730  {
731  // Build iterator
732  ITERATOR iter = IterateWithHoles();
733 
734  // Get the relative indices of the globally indexed vertex
735  VERTEX_INDEX indices;
736 
737  if( !GetRelativeIndices( aGlobalIdx, &indices ) )
738  throw( std::out_of_range( "aGlobalIndex-th vertex does not exist" ) );
739 
740  // Adjust where the iterator is pointing
741  iter.m_currentPolygon = indices.m_polygon;
742  iter.m_currentContour = indices.m_contour;
743  iter.m_currentVertex = indices.m_vertex;
744 
745  return iter;
746  }
747 
750  SEGMENT_ITERATOR IterateSegments( int aFirst, int aLast, bool aIterateHoles = false )
751  {
752  SEGMENT_ITERATOR iter;
753 
754  iter.m_poly = this;
755  iter.m_currentPolygon = aFirst;
756  iter.m_lastPolygon = aLast < 0 ? OutlineCount() - 1 : aLast;
757  iter.m_currentContour = 0;
758  iter.m_currentSegment = 0;
759  iter.m_iterateHoles = aIterateHoles;
760 
761  return iter;
762  }
763 
765  SEGMENT_ITERATOR IterateSegments( int aPolygonIdx )
766  {
767  return IterateSegments( aPolygonIdx, aPolygonIdx );
768  }
769 
771  SEGMENT_ITERATOR IterateSegments()
772  {
773  return IterateSegments( 0, OutlineCount() - 1 );
774  }
775 
777  SEGMENT_ITERATOR IterateSegmentsWithHoles()
778  {
779  return IterateSegments( 0, OutlineCount() - 1, true );
780  }
781 
783  SEGMENT_ITERATOR IterateSegmentsWithHoles( int aOutline )
784  {
785  return IterateSegments( aOutline, aOutline, true );
786  }
787 
796  {
797  PM_FAST = true,
799  };
800 
803  void BooleanAdd( const SHAPE_POLY_SET& b, POLYGON_MODE aFastMode );
804 
807  void BooleanSubtract( const SHAPE_POLY_SET& b, POLYGON_MODE aFastMode );
808 
811  void BooleanIntersection( const SHAPE_POLY_SET& b, POLYGON_MODE aFastMode );
812 
815  void BooleanAdd( const SHAPE_POLY_SET& a, const SHAPE_POLY_SET& b,
816  POLYGON_MODE aFastMode );
817 
820  void BooleanSubtract( const SHAPE_POLY_SET& a, const SHAPE_POLY_SET& b,
821  POLYGON_MODE aFastMode );
822 
825  void BooleanIntersection( const SHAPE_POLY_SET& a, const SHAPE_POLY_SET& b,
826  POLYGON_MODE aFastMode );
827 
829  void Inflate( int aFactor, int aCircleSegmentsCount );
830 
834  void Fracture( POLYGON_MODE aFastMode );
835 
838  void Unfracture( POLYGON_MODE aFastMode );
839 
841  bool HasHoles() const;
842 
844  bool HasTouchingHoles() const;
845 
846 
849  void Simplify( POLYGON_MODE aFastMode );
850 
858  int NormalizeAreaOutlines();
859 
861  const std::string Format() const override;
862 
864  bool Parse( std::stringstream& aStream ) override;
865 
867  void Move( const VECTOR2I& aVector ) override;
868 
875  void Rotate( double aAngle, const VECTOR2I& aCenter );
876 
878  bool IsSolid() const override
879  {
880  return true;
881  }
882 
883  const BOX2I BBox( int aClearance = 0 ) const override;
884 
892  bool PointOnEdge( const VECTOR2I& aP ) const;
893 
905  bool Collide( const VECTOR2I& aP, int aClearance = 0 ) const override;
906 
919  bool Collide( const SEG& aSeg, int aClearance = 0 ) const override;
920 
931  bool CollideVertex( const VECTOR2I& aPoint, VERTEX_INDEX& aClosestVertex,
932  int aClearance = 0 );
933 
944  bool CollideEdge( const VECTOR2I& aPoint, VERTEX_INDEX& aClosestVertex,
945  int aClearance = 0 );
946 
955  bool Contains( const VECTOR2I& aP, int aSubpolyIndex = -1, bool aIgnoreHoles = false ) const;
956 
958  bool IsEmpty() const
959  {
960  return m_polys.size() == 0;
961  }
962 
968  void RemoveVertex( int aGlobalIndex );
969 
975  void RemoveVertex( VERTEX_INDEX aRelativeIndices );
976 
978  void RemoveAllContours();
979 
988  void RemoveContour( int aContourIdx, int aPolygonIdx = -1 );
989 
995  int RemoveNullSegments();
996 
998  int TotalVertices() const;
999 
1001  void DeletePolygon( int aIdx );
1002 
1010  POLYGON ChamferPolygon( unsigned int aDistance, int aIndex = 0 );
1011 
1020  POLYGON FilletPolygon( unsigned int aRadius, int aErrorMax, int aIndex = 0 );
1021 
1028  SHAPE_POLY_SET Chamfer( int aDistance );
1029 
1037  SHAPE_POLY_SET Fillet( int aRadius, int aErrorMax );
1038 
1047  int DistanceToPolygon( VECTOR2I aPoint, int aIndex );
1048 
1061  int DistanceToPolygon( SEG aSegment, int aIndex, int aSegmentWidth = 0 );
1062 
1070  int Distance( VECTOR2I aPoint );
1071 
1080  int Distance( const SEG& aSegment, int aSegmentWidth = 0 );
1081 
1088  bool IsVertexInHole( int aGlobalIdx );
1089 
1090  private:
1091 
1092  SHAPE_LINE_CHAIN& getContourForCorner( int aCornerId, int& aIndexWithinContour );
1093  VECTOR2I& vertex( int aCornerId );
1094  const VECTOR2I& cvertex( int aCornerId ) const;
1095 
1096 
1097  void fractureSingle( POLYGON& paths );
1098  void unfractureSingle ( POLYGON& path );
1099  void importTree( ClipperLib::PolyTree* tree );
1100 
1112  void booleanOp( ClipperLib::ClipType aType,
1113  const SHAPE_POLY_SET& aOtherShape, POLYGON_MODE aFastMode );
1114 
1115  void booleanOp( ClipperLib::ClipType aType,
1116  const SHAPE_POLY_SET& aShape,
1117  const SHAPE_POLY_SET& aOtherShape, POLYGON_MODE aFastMode );
1118 
1119  bool pointInPolygon( const VECTOR2I& aP, const SHAPE_LINE_CHAIN& aPath ) const;
1120 
1121  const ClipperLib::Path convertToClipper( const SHAPE_LINE_CHAIN& aPath, bool aRequiredOrientation );
1122  const SHAPE_LINE_CHAIN convertFromClipper( const ClipperLib::Path& aPath );
1123 
1136  bool containsSingle( const VECTOR2I& aP, int aSubpolyIndex, bool aIgnoreHoles = false ) const;
1137 
1144  {
1147  };
1148 
1149 
1150 
1165  POLYGON chamferFilletPolygon( CORNER_MODE aMode, unsigned int aDistance,
1166  int aIndex, int aErrorMax = -1 );
1167 
1169  bool hasTouchingHoles( const POLYGON& aPoly ) const;
1170 
1171  typedef std::vector<POLYGON> POLYSET;
1172 
1173  POLYSET m_polys;
1174 
1175  public:
1176 
1178 
1179  void CacheTriangulation();
1180  bool IsTriangulationUpToDate() const;
1181 
1182  MD5_HASH GetHash() const;
1183 
1184  private:
1185  void triangulateSingle( const POLYGON& aPoly, SHAPE_POLY_SET::TRIANGULATED_POLYGON& aResult );
1186 
1187  MD5_HASH checksum() const;
1188 
1189  std::vector<std::unique_ptr<TRIANGULATED_POLYGON>> m_triangulatedPolys;
1190  bool m_triangulationValid = false;
1192 
1193 };
1194 
1195 #endif
std::vector< SHAPE_LINE_CHAIN > POLYGON
represents a single polygon outline with holes.
void booleanOp(ClipperLib::ClipType aType, const SHAPE_POLY_SET &aOtherShape, POLYGON_MODE aFastMode)
Function booleanOp this is the engine to execute all polygon boolean transforms (AND, OR, ...
int NewHole(int aOutline=-1)
Creates a new hole in a given outline
ITERATOR IterateFromVertexWithHoles(int aGlobalIdx)
POLYGON ChamferPolygon(unsigned int aDistance, int aIndex=0)
Function Chamfer returns a chamfered version of the aIndex-th polygon.
bool HasHoles() const
Returns true if the polygon set has any holes.
void SetTriangle(int aIndex, const TRI &aTri)
const VECTOR2I & CVertex(int aIndex, int aOutline, int aHole) const
Returns the index-th vertex in a given hole outline within a given outline
void fractureSingle(POLYGON &paths)
SEGMENT_ITERATOR IterateSegmentsWithHoles()
Returns an iterator object, for all outlines in the set (with holes)
void unfractureSingle(POLYGON &path)
void BooleanAdd(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Performs boolean polyset union For aFastMode meaning, see function booleanOp
ITERATOR Iterate(int aOutline)
Function Iterate.
CONST_ITERATOR CIterate() const
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...
std::vector< POLYGON > POLYSET
bool IsLastPolygon() const
Function IsLastOutline.
const ClipperLib::Path convertToClipper(const SHAPE_LINE_CHAIN &aPath, bool aRequiredOrientation)
MD5_HASH checksum() const
MD5_HASH GetHash() const
SEGMENT_ITERATOR_TEMPLATE< const SEG > CONST_SEGMENT_ITERATOR
CONST_ITERATOR CIterateWithHoles(int aOutline) const
bool IsPolygonSelfIntersecting(int aPolygonIndex)
Function IsPolygonSelfIntersecting.
SEGMENT_ITERATOR_TEMPLATE< SEG > SEGMENT_ITERATOR
int NormalizeAreaOutlines()
Function NormalizeAreaOutlines Convert a self-intersecting polygon to one (or more) non self-intersec...
bool IsSolid() const override
CONST_ITERATOR CIterate(int aFirst, int aLast, bool aIterateHoles=false) const
int HoleCount(int aOutline) const
Returns the number of holes in a given outline
bool Parse(std::stringstream &aStream) override
int TotalVertices() const
Returns total number of vertices stored in the set.
bool Contains(const VECTOR2I &aP, int aSubpolyIndex=-1, bool aIgnoreHoles=false) const
Returns true if a given subpolygon contains the point aP.
void Advance()
Function Advance advances the indices of the current vertex/outline/contour, checking whether the ver...
Struct VERTEX_INDEX.
SHAPE_LINE_CHAIN & Hole(int aOutline, int aHole)
Returns the reference to aHole-th hole in the aIndex-th outline
const VECTOR2I & cvertex(int aCornerId) const
Class SEGMENT_ITERATOR_TEMPLATE.
const SHAPE_LINE_CHAIN & CHole(int aOutline, int aHole) const
bool IsTriangulationUpToDate() const
SHAPE_POLY_SET Fillet(int aRadius, int aErrorMax)
Function Fillet returns a filleted version of the polygon set.
ITERATOR Iterate()
Function Iterate.
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...
int OutlineCount() const
Returns the number of outlines in the set
std::vector< std::unique_ptr< TRIANGULATED_POLYGON > > m_triangulatedPolys
bool HasTouchingHoles() const
Returns true if the polygon set has any holes tha share a vertex.
#define abs(a)
Definition: auxiliary.h:84
VERTEX_INDEX GetIndex()
Function GetIndex.
struct SHAPE_POLY_SET::VERTEX_INDEX VERTEX_INDEX
Struct VERTEX_INDEX.
POLYGON FilletPolygon(unsigned int aRadius, int aErrorMax, int aIndex=0)
Function Fillet returns a filleted version of the aIndex-th polygon.
bool IsEndContour() const
Function IsEndContour.
bool IsVertexInHole(int aGlobalIdx)
Function IsVertexInHole.
VECTOR2I & Vertex(int aIndex, int aOutline, int aHole)
Returns the index-th vertex in a given hole outline within a given outline
void DeletePolygon(int aIdx)
Deletes aIdx-th polygon from the set
POLYGON chamferFilletPolygon(CORNER_MODE aMode, unsigned int aDistance, int aIndex, int aErrorMax=-1)
Function chamferFilletPolygon Returns the camfered or filleted version of the aIndex-th polygon in th...
CONST_ITERATOR CIterate(int aOutline) const
SHAPE_LINE_CHAIN & getContourForCorner(int aCornerId, int &aIndexWithinContour)
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...
const POLYGON & Polygon(int aIndex) const
SHAPE_POLY_SET & operator=(const SHAPE_POLY_SET &)
void Inflate(int aFactor, int aCircleSegmentsCount)
Performs outline inflation/deflation, using round corners.
void Move(const VECTOR2I &aVector) override
ITERATOR IterateWithHoles(int aOutline)
Function IterateWithHoles.
Class SHAPE_POLY_SET.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Returns the reference to aIndex-th outline in the set
SEGMENT_ITERATOR IterateSegments(int aPolygonIdx)
Returns an iterator object, for iterating aPolygonIdx-th polygon edges
void Advance()
Function Advance advances the indices of the current vertex/outline/contour, checking whether the ver...
bool GetRelativeIndices(int aGlobalIdx, VERTEX_INDEX *aRelativeIndices) const
Function GetRelativeIndices.
bool IsAdjacent(SEGMENT_ITERATOR_TEMPLATE< T > aOther)
Function IsAdjacent.
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...
const TRIANGULATED_POLYGON * TriangulatedPolygon(int aIndex) const
bool PointOnEdge(const VECTOR2I &aP) const
Function PointOnEdge()
SEGMENT_ITERATOR IterateSegmentsWithHoles(int aOutline)
Returns an iterator object, for the aOutline-th outline in the set (with holes)
bool hasTouchingHoles(const POLYGON &aPoly) const
Returns true if the polygon set has any holes that touch share a vertex.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
int AddVertex(const VECTOR2I &aP)
void Simplify(POLYGON_MODE aFastMode)
Simplifies the polyset (merges overlapping polys, eliminates degeneracy/self-intersections) For aFast...
Class SHAPE.
Definition: shape.h:58
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...
const std::string Format() const override
int RemoveNullSegments()
Function RemoveNullSegments looks for null segments; ie, segments whose ends are exactly the same and...
void BooleanIntersection(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Performs boolean polyset intersection For aFastMode meaning, see function booleanOp ...
void triangulateSingle(const POLYGON &aPoly, SHAPE_POLY_SET::TRIANGULATED_POLYGON &aResult)
int NewOutline()
Creates a new empty polygon in the set and returns its index
void Fracture(POLYGON_MODE aFastMode)
Converts a set of polygons with holes to a singe outline with "slits"/"fractures" connecting the oute...
int AddHole(const SHAPE_LINE_CHAIN &aHole, int aOutline=-1)
Adds a new hole to the given outline (default: last) and returns its index
VECTOR2I & vertex(int aCornerId)
ITERATOR_TEMPLATE< VECTOR2I > ITERATOR
bool pointInPolygon(const VECTOR2I &aP, const SHAPE_LINE_CHAIN &aPath) const
bool GetGlobalIndex(VERTEX_INDEX aRelativeIndices, int &aGlobalIdx)
Function GetGlobalIndex computes the global index of a vertex from the relative indices of polygon...
Definition: seg.h:36
int DistanceToPolygon(VECTOR2I aPoint, int aIndex)
Function DistanceToPolygon computes the minimum distance between the aIndex-th polygon and aPoint...
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index
CORNER_MODE
Operations ChamferPolygon and FilletPolygon are computed under the private chamferFillet method; this...
SHAPE_POLY_SET Chamfer(int aDistance)
Function Chamfer returns a chamfered version of the polygon set.
SHAPE_POLY_SET UnitSet(int aPolygonIndex)
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
const SHAPE_LINE_CHAIN convertFromClipper(const ClipperLib::Path &aPath)
ITERATOR IterateWithHoles()
Function IterateWithHoles.
Class SHAPE_LINE_CHAIN.
void RemoveVertex(int aGlobalIndex)
Function RemoveVertex deletes the aGlobalIndex-th vertex.
int Distance(VECTOR2I aPoint)
Function DistanceToPolygon computes the minimum distance between aPoint and all the polygons in the s...
int VertexCount(int aOutline=-1, int aHole=-1) const
Returns the number of vertices in a given outline/hole
Class ITERATOR_TEMPLATE.
void RemoveAllContours()
Removes all outlines & holes (clears) the polygon set.
ITERATOR_TEMPLATE< const VECTOR2I > CONST_ITERATOR
bool IsLastPolygon() const
Function IsLastOutline.
const POLYGON & CPolygon(int aIndex) const
void InsertVertex(int aGlobalIndex, VECTOR2I aNewVertex)
Function InsertVertex Adds a vertex in the globally indexed position aGlobalIndex.
void GetTriangle(int index, VECTOR2I &a, VECTOR2I &b, VECTOR2I &c) const
POLYGON_MODE
operations on polygons use a aFastMode param if aFastMode is PM_FAST (true) the result can be a weak ...
void BooleanSubtract(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Performs boolean polyset difference For aFastMode meaning, see function booleanOp ...
bool IsEmpty() const
Returns true if the set is empty (no polygons at all)
POLYGON & Polygon(int aIndex)
Returns the aIndex-th subpolygon in the set
bool containsSingle(const VECTOR2I &aP, int aSubpolyIndex, bool aIgnoreHoles=false) const
containsSingle function Checks whether the point aP is inside the aSubpolyIndex-th polygon of the pol...
SHAPE * Clone() const override
Function Clone()
bool IsSelfIntersecting()
Function IsSelfIntersecting Checks whether any of the polygons in the set is self intersecting...
CONST_ITERATOR CIterateWithHoles() const
SEGMENT_ITERATOR IterateSegments()
Returns an iterator object, for all outlines in the set (no holes)
void Rotate(double aAngle, const VECTOR2I &aCenter)
Function Rotate rotates all vertices by a given angle.
const BOX2I BBox(int aClearance=0) const override
Function BBox()
VERTEX_INDEX GetIndex()
Function GetIndex.
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...
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...
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) ...
void RemoveContour(int aContourIdx, int aPolygonIdx=-1)
Function RemoveContour deletes the aContourIdx-th contour of the aPolygonIdx-th polygon in the set...
void importTree(ClipperLib::PolyTree *tree)
void Unfracture(POLYGON_MODE aFastMode)
Converts a single outline slitted ("fractured") polygon into a set ouf outlines with holes...