KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2019 CERN
5 * Copyright (C) 2021-2024 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 * @author Alejandro García Montoro <[email protected]>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, you may find one here:
22 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23 * or you may search the http://www.gnu.org website for the version 2 license,
24 * or you may write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 */
27
28#ifndef __SHAPE_POLY_SET_H
29#define __SHAPE_POLY_SET_H
30
31#include <atomic>
32#include <cstdio>
33#include <deque> // for deque
34#include <iosfwd> // for string, stringstream
35#include <memory>
36#include <mutex>
37#include <set> // for set
38#include <stdexcept> // for out_of_range
39#include <stdlib.h> // for abs
40#include <vector>
41
42#include <clipper.hpp> // for ClipType, PolyTree (ptr only)
43#include <clipper2/clipper.h>
44#include <core/mirror.h> // for FLIP_DIRECTION
46#include <geometry/seg.h> // for SEG
47#include <geometry/shape.h>
49#include <math/box2.h> // for BOX2I
50#include <math/vector2d.h> // for VECTOR2I
51#include <hash_128.h>
52
53
68class SHAPE_POLY_SET : public SHAPE
69{
70public:
74 typedef std::vector<SHAPE_LINE_CHAIN> POLYGON;
75
77 {
78 public:
79 struct TRI : public SHAPE_LINE_CHAIN_BASE
80 {
81 TRI( int _a = 0, int _b = 0, int _c = 0, TRIANGULATED_POLYGON* aParent = nullptr ) :
83 a( _a ),
84 b( _b ),
85 c( _c ),
86 parent( aParent )
87 {
88 }
89
90 virtual void Rotate( const EDA_ANGLE& aAngle,
91 const VECTOR2I& aCenter = { 0, 0 } ) override {};
92
93 virtual void Move( const VECTOR2I& aVector ) override {};
94
95 virtual bool IsSolid() const override { return true; }
96
97 virtual bool IsClosed() const override { return true; }
98
99 virtual const BOX2I BBox( int aClearance = 0 ) const override;
100
101 virtual const VECTOR2I GetPoint( int aIndex ) const override
102 {
103 switch(aIndex)
104 {
105 case 0: return parent->m_vertices[a];
106 case 1: return parent->m_vertices[b];
107 case 2: return parent->m_vertices[c];
108 default: wxCHECK( false, VECTOR2I() );
109 }
110 }
111
112 virtual const SEG GetSegment( int aIndex ) const override
113 {
114 switch(aIndex)
115 {
116 case 0: return SEG( parent->m_vertices[a], parent->m_vertices[b] );
117 case 1: return SEG( parent->m_vertices[b], parent->m_vertices[c] );
118 case 2: return SEG( parent->m_vertices[c], parent->m_vertices[a] );
119 default: wxCHECK( false, SEG() );
120 }
121 }
122
123 virtual size_t GetPointCount() const override { return 3; }
124 virtual size_t GetSegmentCount() const override { return 3; }
125
126 double Area() const
127 {
128 VECTOR2I& aa = parent->m_vertices[a];
129 VECTOR2I& bb = parent->m_vertices[b];
130 VECTOR2I& cc = parent->m_vertices[c];
131
132 VECTOR2D ba = bb - aa;
133 VECTOR2D cb = cc - bb;
134
135 return std::abs( cb.Cross( ba ) * 0.5 );
136 }
137
138 int a;
139 int b;
140 int c;
142 };
143
144 TRIANGULATED_POLYGON( int aSourceOutline );
147
148 void Clear()
149 {
150 m_vertices.clear();
151 m_triangles.clear();
152 }
153
154 void GetTriangle( int index, VECTOR2I& a, VECTOR2I& b, VECTOR2I& c ) const
155 {
156 auto& tri = m_triangles[ index ];
157 a = m_vertices[ tri.a ];
158 b = m_vertices[ tri.b ];
159 c = m_vertices[ tri.c ];
160 }
161
163
164 void AddTriangle( int a, int b, int c );
165
166 void AddVertex( const VECTOR2I& aP )
167 {
168 m_vertices.push_back( aP );
169 }
170
171 size_t GetTriangleCount() const { return m_triangles.size(); }
172
174 void SetSourceOutlineIndex( int aIndex ) { m_sourceOutline = aIndex; }
175
176 const std::deque<TRI>& Triangles() const { return m_triangles; }
177 void SetTriangles( const std::deque<TRI>& aTriangles )
178 {
179 m_triangles.resize( aTriangles.size() );
180
181 for( size_t ii = 0; ii < aTriangles.size(); ii++ )
182 {
183 m_triangles[ii] = aTriangles[ii];
184 m_triangles[ii].parent = this;
185 }
186 }
187
188 const std::deque<VECTOR2I>& Vertices() const { return m_vertices; }
189 void SetVertices( const std::deque<VECTOR2I>& aVertices )
190 {
191 m_vertices = aVertices;
192 }
193
194 size_t GetVertexCount() const
195 {
196 return m_vertices.size();
197 }
198
199 void Move( const VECTOR2I& aVec )
200 {
201 for( VECTOR2I& vertex : m_vertices )
202 vertex += aVec;
203 }
204
205 private:
207 std::deque<TRI> m_triangles;
208 std::deque<VECTOR2I> m_vertices;
209 };
210
217 {
223 m_polygon(-1),
224 m_contour(-1),
225 m_vertex(-1)
226 {
227 }
228 };
229
233 template <class T>
235 {
236 public:
237
242 bool IsEndContour() const
243 {
244 return m_currentVertex + 1 ==
246 }
247
251 bool IsLastPolygon() const
252 {
254 }
255
256 operator bool() const
257 {
259 return true;
260
261 if( m_currentPolygon != m_poly->OutlineCount() - 1 )
262 return false;
263
264 const auto& currentPolygon = m_poly->CPolygon( m_currentPolygon );
265
266 return m_currentContour < (int) currentPolygon.size() - 1
267 || m_currentVertex < currentPolygon[m_currentContour].PointCount();
268 }
269
274 void Advance()
275 {
276 // Advance vertex index
278
279 // Check whether the user wants to iterate through the vertices of the holes
280 // and behave accordingly
281 if( m_iterateHoles )
282 {
283 // If the last vertex of the contour was reached, advance the contour index
284 if( m_currentVertex >=
286 {
287 m_currentVertex = 0;
289
290 // If the last contour of the current polygon was reached, advance the
291 // outline index
292 int totalContours = m_poly->CPolygon( m_currentPolygon ).size();
293
294 if( m_currentContour >= totalContours )
295 {
298 }
299 }
300 }
301 else
302 {
303 // If the last vertex of the outline was reached, advance to the following polygon
304 if( m_currentVertex >= m_poly->CPolygon( m_currentPolygon )[0].PointCount() )
305 {
306 m_currentVertex = 0;
308 }
309 }
310 }
311
312 void operator++( int dummy )
313 {
314 Advance();
315 }
316
318 {
319 Advance();
320 }
321
322 const T& Get()
323 {
325 }
326
327 const T& operator*()
328 {
329 return Get();
330 }
331
332 const T* operator->()
333 {
334 return &Get();
335 }
336
341 {
342 VERTEX_INDEX index;
343
347
348 return index;
349 }
350
351 private:
352 friend class SHAPE_POLY_SET;
353
360 };
361
365 template <class T>
367 {
368 public:
372 bool IsLastPolygon() const
373 {
375 }
376
377 operator bool() const
378 {
380 }
381
386 void Advance()
387 {
388 // Advance vertex index
390 int last;
391
392 // Check whether the user wants to iterate through the vertices of the holes
393 // and behave accordingly.
394 if( m_iterateHoles )
395 {
396 last = m_poly->CPolygon( m_currentPolygon )[m_currentContour].SegmentCount();
397
398 // If the last vertex of the contour was reached, advance the contour index.
399 if( m_currentSegment >= last )
400 {
403
404 // If the last contour of the current polygon was reached, advance the
405 // outline index.
406 int totalContours = m_poly->CPolygon( m_currentPolygon ).size();
407
408 if( m_currentContour >= totalContours )
409 {
412 }
413 }
414 }
415 else
416 {
417 last = m_poly->CPolygon( m_currentPolygon )[0].SegmentCount();
418 // If the last vertex of the outline was reached, advance to the following
419 // polygon
420 if( m_currentSegment >= last )
421 {
424 }
425 }
426 }
427
428 void operator++( int dummy )
429 {
430 Advance();
431 }
432
434 {
435 Advance();
436 }
437
438 T Get()
439 {
441 }
442
444 {
445 return Get();
446 }
447
452 {
453 VERTEX_INDEX index;
454
458
459 return index;
460 }
461
468 {
469 // Check that both iterators point to the same contour of the same polygon of the
470 // same polygon set.
471 if( m_poly == aOther.m_poly && m_currentPolygon == aOther.m_currentPolygon &&
473 {
474 // Compute the total number of segments.
475 int numSeg;
476 numSeg = m_poly->CPolygon( m_currentPolygon )[m_currentContour].SegmentCount();
477
478 // Compute the difference of the segment indices. If it is exactly one, they
479 // are adjacent. The only missing case where they also are adjacent is when
480 // the segments are the first and last one, in which case the difference
481 // always equals the total number of segments minus one.
482 int indexDiff = std::abs( m_currentSegment - aOther.m_currentSegment );
483
484 return ( indexDiff == 1 ) || ( indexDiff == (numSeg - 1) );
485 }
486
487 return false;
488 }
489
490 private:
491 friend class SHAPE_POLY_SET;
492
499 };
500
501 // Iterator and const iterator types to visit polygon's points.
504
505 // Iterator and const iterator types to visit polygon's edges.
508
510
511 SHAPE_POLY_SET( const BOX2D& aRect );
512
518 SHAPE_POLY_SET( const SHAPE_LINE_CHAIN& aOutline );
519
525 SHAPE_POLY_SET( const POLYGON& aPolygon );
526
533 SHAPE_POLY_SET( const SHAPE_POLY_SET& aOther );
534
536
537 SHAPE_POLY_SET& operator=( const SHAPE_POLY_SET& aOther );
538
550 virtual void CacheTriangulation( bool aPartition = true, bool aSimplify = false )
551 {
552 cacheTriangulation( aPartition, aSimplify, nullptr );
553 }
554 bool IsTriangulationUpToDate() const;
555
556 HASH_128 GetHash() const;
557
558 virtual bool HasIndexableSubshapes() const override;
559
560 virtual size_t GetIndexableSubshapeCount() const override;
561
562 virtual void GetIndexableSubshapes( std::vector<const SHAPE*>& aSubshapes ) const override;
563
575 bool GetRelativeIndices( int aGlobalIdx, VERTEX_INDEX* aRelativeIndices ) const;
576
586 bool GetGlobalIndex( VERTEX_INDEX aRelativeIndices, int& aGlobalIdx ) const;
587
589 SHAPE* Clone() const override;
590
592
594 int NewOutline();
595
597 int NewHole( int aOutline = -1 );
598
600 int AddOutline( const SHAPE_LINE_CHAIN& aOutline );
601
603 int AddHole( const SHAPE_LINE_CHAIN& aHole, int aOutline = -1 );
604
606 int AddPolygon( const POLYGON& apolygon );
607
609 double Area();
610
612 int ArcCount() const;
613
615 void GetArcs( std::vector<SHAPE_ARC>& aArcBuffer ) const;
616
618 void ClearArcs();
619
621
633 int Append( int x, int y, int aOutline = -1, int aHole = -1, bool aAllowDuplication = false );
634
636 void Append( const SHAPE_POLY_SET& aSet );
637
639 void Append( const VECTOR2I& aP, int aOutline = -1, int aHole = -1 );
640
650 int Append( const SHAPE_ARC& aArc, int aOutline = -1, int aHole = -1,
651 double aAccuracy = SHAPE_ARC::DefaultAccuracyForPCB() );
652
660 void InsertVertex( int aGlobalIndex, const VECTOR2I& aNewVertex );
661
663 const VECTOR2I& CVertex( int aIndex, int aOutline, int aHole ) const;
664
666 const VECTOR2I& CVertex( int aGlobalIndex ) const;
667
669 const VECTOR2I& CVertex( VERTEX_INDEX aIndex ) const;
670
684 bool GetNeighbourIndexes( int aGlobalIndex, int* aPrevious, int* aNext ) const;
685
692 bool IsPolygonSelfIntersecting( int aPolygonIndex ) const;
693
699 bool IsSelfIntersecting() const;
700
702 unsigned int TriangulatedPolyCount() const { return m_triangulatedPolys.size(); }
703
705 int OutlineCount() const { return m_polys.size(); }
706
708 int VertexCount( int aOutline = -1, int aHole = -1 ) const;
709
712 int FullPointCount() const;
713
715 int HoleCount( int aOutline ) const
716 {
717 if( ( aOutline < 0 ) || ( aOutline >= (int) m_polys.size() )
718 || ( m_polys[aOutline].size() < 2 ) )
719 return 0;
720
721 // the first polygon in m_polys[aOutline] is the main contour,
722 // only others are holes:
723 return m_polys[aOutline].size() - 1;
724 }
725
728 {
729 return m_polys[aIndex][0];
730 }
731
732 const SHAPE_LINE_CHAIN& Outline( int aIndex ) const
733 {
734 return m_polys[aIndex][0];
735 }
736
746 SHAPE_POLY_SET Subset( int aFirstPolygon, int aLastPolygon );
747
748 SHAPE_POLY_SET UnitSet( int aPolygonIndex )
749 {
750 return Subset( aPolygonIndex, aPolygonIndex + 1 );
751 }
752
754 SHAPE_LINE_CHAIN& Hole( int aOutline, int aHole )
755 {
756 return m_polys[aOutline][aHole + 1];
757 }
758
760 POLYGON& Polygon( int aIndex )
761 {
762 return m_polys[aIndex];
763 }
764
765 const POLYGON& Polygon( int aIndex ) const
766 {
767 return m_polys[aIndex];
768 }
769
770 const TRIANGULATED_POLYGON* TriangulatedPolygon( int aIndex ) const
771 {
772 return m_triangulatedPolys[aIndex].get();
773 }
774
775 const SHAPE_LINE_CHAIN& COutline( int aIndex ) const
776 {
777 return m_polys[aIndex][0];
778 }
779
780 const SHAPE_LINE_CHAIN& CHole( int aOutline, int aHole ) const
781 {
782 return m_polys[aOutline][aHole + 1];
783 }
784
785 const POLYGON& CPolygon( int aIndex ) const
786 {
787 return m_polys[aIndex];
788 }
789
790 const std::vector<POLYGON>& CPolygons() const { return m_polys; }
791
802 ITERATOR Iterate( int aFirst, int aLast, bool aIterateHoles = false )
803 {
804 ITERATOR iter;
805
806 iter.m_poly = this;
807 iter.m_currentPolygon = aFirst;
808 iter.m_lastPolygon = aLast < 0 ? OutlineCount() - 1 : aLast;
809 iter.m_currentContour = 0;
810 iter.m_currentVertex = 0;
811 iter.m_iterateHoles = aIterateHoles;
812
813 return iter;
814 }
815
821 ITERATOR Iterate( int aOutline )
822 {
823 return Iterate( aOutline, aOutline );
824 }
825
832 {
833 return Iterate( aOutline, aOutline, true );
834 }
835
841 {
842 return Iterate( 0, OutlineCount() - 1 );
843 }
844
850 {
851 return Iterate( 0, OutlineCount() - 1, true );
852 }
853
854
855 CONST_ITERATOR CIterate( int aFirst, int aLast, bool aIterateHoles = false ) const
856 {
857 CONST_ITERATOR iter;
858
859 iter.m_poly = const_cast<SHAPE_POLY_SET*>( this );
860 iter.m_currentPolygon = aFirst;
861 iter.m_lastPolygon = aLast < 0 ? OutlineCount() - 1 : aLast;
862 iter.m_currentContour = 0;
863 iter.m_currentVertex = 0;
864 iter.m_iterateHoles = aIterateHoles;
865
866 return iter;
867 }
868
869 CONST_ITERATOR CIterate( int aOutline ) const
870 {
871 return CIterate( aOutline, aOutline );
872 }
873
874 CONST_ITERATOR CIterateWithHoles( int aOutline ) const
875 {
876 return CIterate( aOutline, aOutline, true );
877 }
878
880 {
881 return CIterate( 0, OutlineCount() - 1 );
882 }
883
885 {
886 return CIterate( 0, OutlineCount() - 1, true );
887 }
888
890 {
891 // Build iterator
892 ITERATOR iter = IterateWithHoles();
893
894 // Get the relative indices of the globally indexed vertex
895 VERTEX_INDEX indices;
896
897 if( !GetRelativeIndices( aGlobalIdx, &indices ) )
898 throw( std::out_of_range( "aGlobalIndex-th vertex does not exist" ) );
899
900 // Adjust where the iterator is pointing
901 iter.m_currentPolygon = indices.m_polygon;
902 iter.m_currentContour = indices.m_contour;
903 iter.m_currentVertex = indices.m_vertex;
904
905 return iter;
906 }
907
910 SEGMENT_ITERATOR IterateSegments( int aFirst, int aLast, bool aIterateHoles = false )
911 {
912 SEGMENT_ITERATOR iter;
913
914 iter.m_poly = this;
915 iter.m_currentPolygon = aFirst;
916 iter.m_lastPolygon = aLast < 0 ? OutlineCount() - 1 : aLast;
917 iter.m_currentContour = 0;
918 iter.m_currentSegment = 0;
919 iter.m_iterateHoles = aIterateHoles;
920
921 return iter;
922 }
923
927 bool aIterateHoles = false ) const
928 {
930
931 iter.m_poly = const_cast<SHAPE_POLY_SET*>( this );
932 iter.m_currentPolygon = aFirst;
933 iter.m_lastPolygon = aLast < 0 ? OutlineCount() - 1 : aLast;
934 iter.m_currentContour = 0;
935 iter.m_currentSegment = 0;
936 iter.m_iterateHoles = aIterateHoles;
937
938 return iter;
939 }
940
943 {
944 return IterateSegments( aPolygonIdx, aPolygonIdx );
945 }
946
949 {
950 return CIterateSegments( aPolygonIdx, aPolygonIdx );
951 }
952
955 {
956 return IterateSegments( 0, OutlineCount() - 1 );
957 }
958
961 {
962 return CIterateSegments( 0, OutlineCount() - 1 );
963 }
964
967 {
968 return IterateSegments( 0, OutlineCount() - 1, true );
969 }
970
973 {
974 return IterateSegments( aOutline, aOutline, true );
975 }
976
979 {
980 return CIterateSegments( 0, OutlineCount() - 1, true );
981 }
982
985 {
986 return CIterateSegments( aOutline, aOutline, true );
987 }
988
998 {
999 PM_FAST = true,
1000 PM_STRICTLY_SIMPLE = false
1002
1005 void BooleanAdd( const SHAPE_POLY_SET& b, POLYGON_MODE aFastMode );
1006
1009 void BooleanSubtract( const SHAPE_POLY_SET& b, POLYGON_MODE aFastMode );
1010
1013 void BooleanIntersection( const SHAPE_POLY_SET& b, POLYGON_MODE aFastMode );
1014
1017 void BooleanXor( const SHAPE_POLY_SET& b, POLYGON_MODE aFastMode );
1018
1021 void BooleanAdd( const SHAPE_POLY_SET& a, const SHAPE_POLY_SET& b,
1022 POLYGON_MODE aFastMode );
1023
1026 void BooleanSubtract( const SHAPE_POLY_SET& a, const SHAPE_POLY_SET& b,
1027 POLYGON_MODE aFastMode );
1028
1031 void BooleanIntersection( const SHAPE_POLY_SET& a, const SHAPE_POLY_SET& b,
1032 POLYGON_MODE aFastMode );
1033
1036 void BooleanXor( const SHAPE_POLY_SET& a, const SHAPE_POLY_SET& b,
1037 POLYGON_MODE aFastMode );
1038
1044
1060 void Inflate( int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError,
1061 bool aSimplify = false );
1062
1063 void Deflate( int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError )
1064 {
1065 Inflate( -aAmount, aCornerStrategy, aMaxError );
1066 }
1067
1081 void OffsetLineChain( const SHAPE_LINE_CHAIN& aLine, int aAmount,
1082 CORNER_STRATEGY aCornerStrategy, int aMaxError, bool aSimplify );
1083
1091 void InflateWithLinkedHoles( int aFactor, CORNER_STRATEGY aCornerStrategy, int aMaxError,
1092 POLYGON_MODE aFastMode );
1093
1097 void Fracture( POLYGON_MODE aFastMode );
1098
1101 void Unfracture( POLYGON_MODE aFastMode );
1102
1104 bool HasHoles() const;
1105
1107 bool HasTouchingHoles() const;
1108
1109
1112 void Simplify( POLYGON_MODE aFastMode );
1113
1120 void SimplifyOutlines( int aMaxError = 0 );
1121
1131
1133 const std::string Format( bool aCplusPlus = true ) const override;
1134
1136 bool Parse( std::stringstream& aStream ) override;
1137
1139 void Move( const VECTOR2I& aVector ) override;
1140
1147 void Mirror( const VECTOR2I& aRef, FLIP_DIRECTION aFlipDirection );
1148
1155 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override;
1156
1158 bool IsSolid() const override
1159 {
1160 return true;
1161 }
1162
1163 const BOX2I BBox( int aClearance = 0 ) const override;
1164
1171 bool PointOnEdge( const VECTOR2I& aP, int aAccuracy = 0 ) const;
1172
1185 bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
1186 VECTOR2I* aLocation = nullptr ) const override;
1187
1206 bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
1207 VECTOR2I* aLocation = nullptr ) const override;
1208
1227 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
1228 VECTOR2I* aLocation = nullptr ) const override;
1229
1240 bool CollideVertex( const VECTOR2I& aPoint, VERTEX_INDEX* aClosestVertex = nullptr,
1241 int aClearance = 0 ) const;
1242
1253 bool CollideEdge( const VECTOR2I& aPoint, VERTEX_INDEX* aClosestVertex = nullptr,
1254 int aClearance = 0 ) const;
1255
1256 bool PointInside( const VECTOR2I& aPt, int aAccuracy = 0,
1257 bool aUseBBoxCache = false ) const override;
1258
1265 void BuildBBoxCaches() const;
1266
1267 const BOX2I BBoxFromCaches() const;
1268
1279 bool Contains( const VECTOR2I& aP, int aSubpolyIndex = -1, int aAccuracy = 0,
1280 bool aUseBBoxCaches = false ) const;
1281
1283 bool IsEmpty() const
1284 {
1285 return m_polys.empty();
1286 }
1287
1293 void RemoveVertex( int aGlobalIndex );
1294
1300 void RemoveVertex( VERTEX_INDEX aRelativeIndices );
1301
1303 void RemoveAllContours();
1304
1313 void RemoveContour( int aContourIdx, int aPolygonIdx = -1 );
1314
1315
1321 void RemoveOutline( int aOutlineIdx );
1322
1328 int RemoveNullSegments();
1329
1336 void SetVertex( const VERTEX_INDEX& aIndex, const VECTOR2I& aPos );
1337
1346 void SetVertex( int aGlobalIndex, const VECTOR2I& aPos );
1347
1349 int TotalVertices() const;
1350
1352 void DeletePolygon( int aIdx );
1353
1356 void DeletePolygonAndTriangulationData( int aIdx, bool aUpdateHash = true );
1357
1359
1367 POLYGON ChamferPolygon( unsigned int aDistance, int aIndex );
1368
1377 POLYGON FilletPolygon( unsigned int aRadius, int aErrorMax, int aIndex );
1378
1385 SHAPE_POLY_SET Chamfer( int aDistance );
1386
1394 SHAPE_POLY_SET Fillet( int aRadius, int aErrorMax );
1395
1406 SEG::ecoord SquaredDistanceToPolygon( VECTOR2I aPoint, int aIndex,
1407 VECTOR2I* aNearest ) const;
1408
1421 SEG::ecoord SquaredDistanceToPolygon( const SEG& aSegment, int aIndex,
1422 VECTOR2I* aNearest) const;
1423
1434 SEG::ecoord SquaredDistance( const VECTOR2I& aPoint, bool aOutlineOnly,
1435 VECTOR2I* aNearest ) const;
1436
1437 SEG::ecoord SquaredDistance( const VECTOR2I& aPoint, bool aOutlineOnly = false ) const override
1438 {
1439 return SquaredDistance( aPoint, aOutlineOnly, nullptr );
1440 }
1441
1453 SEG::ecoord SquaredDistanceToSeg( const SEG& aSegment, VECTOR2I* aNearest = nullptr ) const;
1454
1461 bool IsVertexInHole( int aGlobalIdx );
1462
1471 static const SHAPE_POLY_SET BuildPolysetFromOrientedPaths( const std::vector<SHAPE_LINE_CHAIN>& aPaths, bool aReverseOrientation = false, bool aEvenOdd = false );
1472
1473 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError,
1474 ERROR_LOC aErrorLoc ) const override
1475 {
1476 aBuffer.Append( *this );
1477 }
1478
1479protected:
1480 void cacheTriangulation( bool aPartition, bool aSimplify,
1481 std::vector<std::unique_ptr<TRIANGULATED_POLYGON>>* aHintData );
1482
1483private:
1485
1487
1488 void fractureSingle( POLYGON& paths );
1489 void unfractureSingle ( POLYGON& path );
1490 void importTree( ClipperLib::PolyTree* tree,
1491 const std::vector<CLIPPER_Z_VALUE>& aZValueBuffer,
1492 const std::vector<SHAPE_ARC>& aArcBuffe );
1493 void importTree( Clipper2Lib::PolyTree64& tree,
1494 const std::vector<CLIPPER_Z_VALUE>& aZValueBuffer,
1495 const std::vector<SHAPE_ARC>& aArcBuffe );
1496 void importPaths( Clipper2Lib::Paths64& paths,
1497 const std::vector<CLIPPER_Z_VALUE>& aZValueBuffer,
1498 const std::vector<SHAPE_ARC>& aArcBuffe );
1499 void importPolyPath( const std::unique_ptr<Clipper2Lib::PolyPath64>& aPolyPath,
1500 const std::vector<CLIPPER_Z_VALUE>& aZValueBuffer,
1501 const std::vector<SHAPE_ARC>& aArcBuffer );
1502
1503 void inflate1( int aAmount, int aCircleSegCount, CORNER_STRATEGY aCornerStrategy );
1504 void inflate2( int aAmount, int aCircleSegCount, CORNER_STRATEGY aCornerStrategy, bool aSimplify = false );
1505
1506 void inflateLine2( const SHAPE_LINE_CHAIN& aLine, int aAmount, int aCircleSegCount,
1507 CORNER_STRATEGY aCornerStrategy, bool aSimplify = false );
1508
1521 void booleanOp( ClipperLib::ClipType aType, const SHAPE_POLY_SET& aOtherShape,
1522 POLYGON_MODE aFastMode );
1523
1524 void booleanOp( ClipperLib::ClipType aType, const SHAPE_POLY_SET& aShape,
1525 const SHAPE_POLY_SET& aOtherShape, POLYGON_MODE aFastMode );
1526
1527 void booleanOp( Clipper2Lib::ClipType aType, const SHAPE_POLY_SET& aOtherShape );
1528
1529 void booleanOp( Clipper2Lib::ClipType aType, const SHAPE_POLY_SET& aShape,
1530 const SHAPE_POLY_SET& aOtherShape );
1531
1546 bool containsSingle( const VECTOR2I& aP, int aSubpolyIndex, int aAccuracy,
1547 bool aUseBBoxCaches = false ) const;
1548
1555 {
1557 FILLETED
1559
1573 POLYGON chamferFilletPolygon( CORNER_MODE aMode, unsigned int aDistance,
1574 int aIndex, int aErrorMax );
1575
1577 bool hasTouchingHoles( const POLYGON& aPoly ) const;
1578
1579 HASH_128 checksum() const;
1580
1581protected:
1582 std::vector<POLYGON> m_polys;
1583 std::vector<std::unique_ptr<TRIANGULATED_POLYGON>> m_triangulatedPolys;
1584
1585 std::atomic<bool> m_triangulationValid = false;
1587
1588private:
1590 bool m_hashValid = false;
1591};
1592
1593#endif // __SHAPE_POLY_SET_H
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
Definition: approximation.h:32
Definition: seg.h:42
VECTOR2I::extended_type ecoord
Definition: seg.h:44
static double DefaultAccuracyForPCB()
Definition: shape_arc.h:232
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
Base class for iterating over all vertices in a given SHAPE_POLY_SET.
void Advance()
Advance the indices of the current vertex/outline/contour, checking whether the vertices in the holes...
Base class for iterating over all segments in a given SHAPE_POLY_SET.
bool IsAdjacent(SEGMENT_ITERATOR_TEMPLATE< T > aOther) const
void Advance()
Advance the indices of the current vertex/outline/contour, checking whether the vertices in the holes...
void AddVertex(const VECTOR2I &aP)
void GetTriangle(int index, VECTOR2I &a, VECTOR2I &b, VECTOR2I &c) const
const std::deque< VECTOR2I > & Vertices() const
void SetVertices(const std::deque< VECTOR2I > &aVertices)
const std::deque< TRI > & Triangles() const
void AddTriangle(int a, int b, int c)
void SetTriangles(const std::deque< TRI > &aTriangles)
TRIANGULATED_POLYGON & operator=(const TRIANGULATED_POLYGON &aOther)
void Move(const VECTOR2I &aVec)
Represent a set of closed polygons.
std::mutex m_triangulationMutex
virtual bool HasIndexableSubshapes() const override
static const SHAPE_POLY_SET BuildPolysetFromOrientedPaths(const std::vector< SHAPE_LINE_CHAIN > &aPaths, bool aReverseOrientation=false, bool aEvenOdd=false)
Build a SHAPE_POLY_SET from a bunch of outlines in provided in random order.
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
Rotate all vertices by a given angle.
void RemoveAllContours()
Remove all outlines & holes (clears) the polygon set.
SHAPE_POLY_SET Chamfer(int aDistance)
Return a chamfered version of the polygon set.
void RemoveOutline(int aOutlineIdx)
Delete the aOutlineIdx-th outline of the set including its contours and holes.
bool CollideEdge(const VECTOR2I &aPoint, VERTEX_INDEX *aClosestVertex=nullptr, int aClearance=0) const
Check whether aPoint collides with any edge of any of the contours of the polygon.
HASH_128 GetHash() const
virtual void GetIndexableSubshapes(std::vector< const SHAPE * > &aSubshapes) const override
void BooleanSubtract(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Perform boolean polyset difference For aFastMode meaning, see function booleanOp.
ITERATOR_TEMPLATE< VECTOR2I > ITERATOR
void fractureSingle(POLYGON &paths)
bool HasHoles() const
Return true if the polygon set has any holes.
void InflateWithLinkedHoles(int aFactor, CORNER_STRATEGY aCornerStrategy, int aMaxError, POLYGON_MODE aFastMode)
Perform outline inflation/deflation, using round corners.
CONST_ITERATOR CIterateWithHoles() const
ITERATOR IterateWithHoles(int aOutline)
void BooleanXor(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Perform boolean polyset exclusive or For aFastMode meaning, see function booleanOp.
void Fracture(POLYGON_MODE aFastMode)
Convert a set of polygons with holes to a single outline with "slits"/"fractures" connecting the oute...
ITERATOR IterateWithHoles()
void ClearArcs()
Removes all arc references from all the outlines and holes in the polyset.
bool IsTriangulationUpToDate() const
void importPaths(Clipper2Lib::Paths64 &paths, const std::vector< CLIPPER_Z_VALUE > &aZValueBuffer, const std::vector< SHAPE_ARC > &aArcBuffe)
POLYGON_MODE
Operations on polygons use a aFastMode param if aFastMode is PM_FAST (true) the result can be a weak ...
void InsertVertex(int aGlobalIndex, const VECTOR2I &aNewVertex)
Adds a vertex in the globally indexed position aGlobalIndex.
CONST_ITERATOR CIterate() const
CORNER_MODE
Operation ChamferPolygon and FilletPolygon are computed under the private chamferFillet method; this ...
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index.
virtual void CacheTriangulation(bool aPartition=true, bool aSimplify=false)
Build a polygon triangulation, needed to draw a polygon on OpenGL and in some other calculations.
int VertexCount(int aOutline=-1, int aHole=-1) const
Return the number of vertices in a given outline/hole.
void DeletePolygon(int aIdx)
Delete aIdx-th polygon from the set.
double Area()
Return the area of this poly set.
void SetVertex(const VERTEX_INDEX &aIndex, const VECTOR2I &aPos)
Accessor function to set the position of a specific point.
bool IsEmpty() const
Return true if the set is empty (no polygons at all)
void BooleanAdd(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Perform boolean polyset union For aFastMode meaning, see function booleanOp.
CONST_ITERATOR CIterate(int aFirst, int aLast, bool aIterateHoles=false) const
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
ITERATOR Iterate(int aOutline)
bool Parse(std::stringstream &aStream) override
int TotalVertices() const
Return total number of vertices stored in the set.
CONST_ITERATOR CIterate(int aOutline) const
POLYGON & Polygon(int aIndex)
Return the aIndex-th subpolygon in the set.
int FullPointCount() const
Return the number of points in the shape poly set.
void GetArcs(std::vector< SHAPE_ARC > &aArcBuffer) const
Appends all the arcs in this polyset to aArcBuffer.
bool IsVertexInHole(int aGlobalIdx)
Check whether the aGlobalIndex-th vertex belongs to a hole.
int NormalizeAreaOutlines()
Convert a self-intersecting polygon to one (or more) non self-intersecting polygon(s).
void RemoveVertex(int aGlobalIndex)
Delete the aGlobalIndex-th vertex.
void unfractureSingle(POLYGON &path)
void inflateLine2(const SHAPE_LINE_CHAIN &aLine, int aAmount, int aCircleSegCount, CORNER_STRATEGY aCornerStrategy, bool aSimplify=false)
SEGMENT_ITERATOR IterateSegments(int aPolygonIdx)
Return an iterator object, for iterating aPolygonIdx-th polygon edges.
void BooleanIntersection(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Perform boolean polyset intersection For aFastMode meaning, see function booleanOp.
SEGMENT_ITERATOR IterateSegmentsWithHoles(int aOutline)
Return an iterator object, for the aOutline-th outline in the set (with holes).
bool GetRelativeIndices(int aGlobalIdx, VERTEX_INDEX *aRelativeIndices) const
Convert a global vertex index —i.e., a number that globally identifies a vertex in a concatenated lis...
bool IsPolygonSelfIntersecting(int aPolygonIndex) const
Check whether the aPolygonIndex-th polygon in the set is self intersecting.
SHAPE_POLY_SET Subset(int aFirstPolygon, int aLastPolygon)
Return a subset of the polygons in this set, the ones between aFirstPolygon and aLastPolygon.
SHAPE_POLY_SET UnitSet(int aPolygonIndex)
void inflate1(int aAmount, int aCircleSegCount, CORNER_STRATEGY aCornerStrategy)
const POLYGON & Polygon(int aIndex) const
int RemoveNullSegments()
Look for null segments; ie, segments whose ends are exactly the same and deletes them.
HASH_128 checksum() const
void Inflate(int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError, bool aSimplify=false)
Perform outline inflation/deflation.
int HoleCount(int aOutline) const
Returns the number of holes in a given outline.
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)
int AddPolygon(const POLYGON &apolygon)
Adds a polygon to the set.
const std::string Format(bool aCplusPlus=true) const override
std::vector< SHAPE_LINE_CHAIN > POLYGON
represents a single polygon outline with holes.
std::vector< std::unique_ptr< TRIANGULATED_POLYGON > > m_triangulatedPolys
SEGMENT_ITERATOR IterateSegments()
Return an iterator object, for all outlines in the set (no holes).
void booleanOp(ClipperLib::ClipType aType, const SHAPE_POLY_SET &aOtherShape, POLYGON_MODE aFastMode)
This is the engine to execute all polygon boolean transforms (AND, OR, ... and polygon simplification...
void Simplify(POLYGON_MODE aFastMode)
Simplify the polyset (merges overlapping polys, eliminates degeneracy/self-intersections) For aFastMo...
ITERATOR Iterate()
const SHAPE_LINE_CHAIN & Outline(int aIndex) const
CONST_SEGMENT_ITERATOR CIterateSegments() const
Returns an iterator object, for all outlines in the set (no holes)
ITERATOR_TEMPLATE< const VECTOR2I > CONST_ITERATOR
void inflate2(int aAmount, int aCircleSegCount, CORNER_STRATEGY aCornerStrategy, bool aSimplify=false)
int AddHole(const SHAPE_LINE_CHAIN &aHole, int aOutline=-1)
Adds a new hole to the given outline (default: last) and returns its index.
SEG::ecoord SquaredDistance(const VECTOR2I &aPoint, bool aOutlineOnly, VECTOR2I *aNearest) const
Compute the minimum distance squared between aPoint and all the polygons in the set.
void RemoveContour(int aContourIdx, int aPolygonIdx=-1)
Delete the aContourIdx-th contour of the aPolygonIdx-th polygon in the set.
int ArcCount() const
Count the number of arc shapes present.
void Unfracture(POLYGON_MODE aFastMode)
Convert a single outline slitted ("fractured") polygon into a set ouf outlines with holes.
bool GetGlobalIndex(VERTEX_INDEX aRelativeIndices, int &aGlobalIdx) const
Compute the global index of a vertex from the relative indices of polygon, contour and vertex.
bool GetNeighbourIndexes(int aGlobalIndex, int *aPrevious, int *aNext) const
Return the global indexes of the previous and the next corner of the aGlobalIndex-th corner of a cont...
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
SHAPE_LINE_CHAIN & Hole(int aOutline, int aHole)
Return the reference to aHole-th hole in the aIndex-th outline.
bool IsSolid() const override
int NewOutline()
Creates a new empty polygon in the set and returns its index.
void SimplifyOutlines(int aMaxError=0)
Simplifies the lines in the polyset.
const TRIANGULATED_POLYGON * TriangulatedPolygon(int aIndex) const
bool hasTouchingHoles(const POLYGON &aPoly) const
Return true if the polygon set has any holes that touch share a vertex.
CONST_SEGMENT_ITERATOR CIterateSegments(int aPolygonIdx) const
Return an iterator object, for iterating aPolygonIdx-th polygon edges.
SEG::ecoord SquaredDistance(const VECTOR2I &aPoint, bool aOutlineOnly=false) const override
bool PointOnEdge(const VECTOR2I &aP, int aAccuracy=0) const
Check if point aP lies on an edge or vertex of some of the outlines or holes.
bool CollideVertex(const VECTOR2I &aPoint, VERTEX_INDEX *aClosestVertex=nullptr, int aClearance=0) const
Check whether aPoint collides with any vertex of any of the contours of the polygon.
void DeletePolygonAndTriangulationData(int aIdx, bool aUpdateHash=true)
Delete aIdx-th polygon and its triangulation data from the set.
unsigned int TriangulatedPolyCount() const
Return the number of triangulated polygons.
std::atomic< bool > m_triangulationValid
void Deflate(int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError)
void UpdateTriangulationDataHash()
int NewHole(int aOutline=-1)
Creates a new hole in a given outline.
SEG::ecoord SquaredDistanceToPolygon(VECTOR2I aPoint, int aIndex, VECTOR2I *aNearest) const
Compute the minimum distance between the aIndex-th polygon and aPoint.
CONST_SEGMENT_ITERATOR CIterateSegmentsWithHoles() const
Return an iterator object, for the aOutline-th outline in the set (with holes).
void cacheTriangulation(bool aPartition, bool aSimplify, std::vector< std::unique_ptr< TRIANGULATED_POLYGON > > *aHintData)
virtual size_t GetIndexableSubshapeCount() const override
SEGMENT_ITERATOR IterateSegments(int aFirst, int aLast, bool aIterateHoles=false)
Return an iterator object, for iterating between aFirst and aLast outline, with or without holes (def...
ITERATOR Iterate(int aFirst, int aLast, bool aIterateHoles=false)
Return an object to iterate through the points of the polygons between aFirst and aLast.
CONST_SEGMENT_ITERATOR CIterateSegments(int aFirst, int aLast, bool aIterateHoles=false) const
Return an iterator object, for iterating between aFirst and aLast outline, with or without holes (def...
ITERATOR IterateFromVertexWithHoles(int aGlobalIdx)
SEG::ecoord SquaredDistanceToSeg(const SEG &aSegment, VECTOR2I *aNearest=nullptr) const
Compute the minimum distance squared between aSegment and all the polygons in the set.
void importPolyPath(const std::unique_ptr< Clipper2Lib::PolyPath64 > &aPolyPath, const std::vector< CLIPPER_Z_VALUE > &aZValueBuffer, const std::vector< SHAPE_ARC > &aArcBuffer)
void RebuildHolesFromContours()
Extract all contours from this polygon set, then recreate polygons with holes.
void Mirror(const VECTOR2I &aRef, FLIP_DIRECTION aFlipDirection)
Mirror the line points about y or x (or both)
void OffsetLineChain(const SHAPE_LINE_CHAIN &aLine, int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError, bool aSimplify)
Perform offsetting of a line chain.
void BuildBBoxCaches() const
Construct BBoxCaches for Contains(), below.
std::vector< POLYGON > m_polys
const SHAPE_LINE_CHAIN & CHole(int aOutline, int aHole) const
POLYGON FilletPolygon(unsigned int aRadius, int aErrorMax, int aIndex)
Return a filleted version of the aIndex-th polygon.
bool containsSingle(const VECTOR2I &aP, int aSubpolyIndex, int aAccuracy, bool aUseBBoxCaches=false) const
Check whether the point aP is inside the aSubpolyIndex-th polygon of the polyset.
const VECTOR2I & CVertex(int aIndex, int aOutline, int aHole) const
Return the index-th vertex in a given hole outline within a given outline.
int OutlineCount() const
Return the number of outlines in the set.
SEGMENT_ITERATOR_TEMPLATE< SEG > SEGMENT_ITERATOR
POLYGON chamferFilletPolygon(CORNER_MODE aMode, unsigned int aDistance, int aIndex, int aErrorMax)
Return the chamfered or filleted version of the aIndex-th polygon in the set, depending on the aMode ...
SHAPE_POLY_SET Fillet(int aRadius, int aErrorMax)
Return a filleted version of the polygon set.
void Move(const VECTOR2I &aVector) override
bool HasTouchingHoles() const
Return true if the polygon set has any holes that share a vertex.
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
SHAPE_POLY_SET & operator=(const SHAPE_POLY_SET &aOther)
bool Contains(const VECTOR2I &aP, int aSubpolyIndex=-1, int aAccuracy=0, bool aUseBBoxCaches=false) const
Return true if a given subpolygon contains the point aP.
SHAPE_POLY_SET CloneDropTriangulation() const
void importTree(ClipperLib::PolyTree *tree, const std::vector< CLIPPER_Z_VALUE > &aZValueBuffer, const std::vector< SHAPE_ARC > &aArcBuffe)
void TransformToPolygon(SHAPE_POLY_SET &aBuffer, int aError, ERROR_LOC aErrorLoc) const override
Fills a SHAPE_POLY_SET with a polygon representation of this shape.
const POLYGON & CPolygon(int aIndex) const
CONST_ITERATOR CIterateWithHoles(int aOutline) const
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
POLYGON ChamferPolygon(unsigned int aDistance, int aIndex)
Return a chamfered version of the aIndex-th polygon.
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const override
Check if point aP lies inside a closed shape.
const std::vector< POLYGON > & CPolygons() const
SEGMENT_ITERATOR IterateSegmentsWithHoles()
Returns an iterator object, for all outlines in the set (with holes)
const BOX2I BBoxFromCaches() const
CONST_SEGMENT_ITERATOR CIterateSegmentsWithHoles(int aOutline) const
Return an iterator object, for the aOutline-th outline in the set (with holes).
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
SEGMENT_ITERATOR_TEMPLATE< const SEG > CONST_SEGMENT_ITERATOR
bool IsSelfIntersecting() const
Check whether any of the polygons in the set is self intersecting.
An abstract shape on 2D plane.
Definition: shape.h:126
constexpr extended_type Cross(const VECTOR2< T > &aVector) const
Compute cross product of self with aVector.
Definition: vector2d.h:542
CORNER_STRATEGY
define how inflate transform build inflated polygon
FLIP_DIRECTION
Definition: mirror.h:27
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:390
@ SH_POLY_SET_TRIANGLE
a single triangle belonging to a POLY_SET triangulation
Definition: shape.h:56
std::vector< FAB_LAYER_COLOR > dummy
A storage class for 128-bit hash value.
Definition: hash_128.h:36
virtual bool IsClosed() const override
TRI(int _a=0, int _b=0, int _c=0, TRIANGULATED_POLYGON *aParent=nullptr)
virtual bool IsSolid() const override
virtual void Move(const VECTOR2I &aVector) override
virtual void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
virtual const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
virtual size_t GetPointCount() const override
virtual const VECTOR2I GetPoint(int aIndex) const override
virtual size_t GetSegmentCount() const override
virtual const SEG GetSegment(int aIndex) const override
Structure to hold the necessary information in order to index a vertex on a SHAPE_POLY_SET object: th...
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691