-
-
Notifications
You must be signed in to change notification settings - Fork 486
/
convert_shape_list_to_polygon.cpp
1301 lines (1063 loc) · 44 KB
/
convert_shape_list_to_polygon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2015 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
*
* 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
*/
#include <unordered_set>
#include <trigo.h>
#include <macros.h>
#include <math/vector2d.h>
#include <pcb_shape.h>
#include <footprint.h>
#include <pad.h>
#include <base_units.h>
#include <convert_basic_shapes_to_polygon.h>
#include <geometry/shape_poly_set.h>
#include <geometry/geometry_utils.h>
#include <convert_shape_list_to_polygon.h>
#include <board.h>
#include <collectors.h>
#include <wx/log.h>
/**
* Flag to enable debug tracing for the board outline creation
*
* Use "KICAD_BOARD_OUTLINE" to enable.
*
* @ingroup trace_env_vars
*/
const wxChar* traceBoardOutline = wxT( "KICAD_BOARD_OUTLINE" );
class SCOPED_FLAGS_CLEANER : public std::unordered_set<EDA_ITEM*>
{
EDA_ITEM_FLAGS m_flagsToClear;
public:
SCOPED_FLAGS_CLEANER( const EDA_ITEM_FLAGS& aFlagsToClear ) : m_flagsToClear( aFlagsToClear ) {}
~SCOPED_FLAGS_CLEANER()
{
for( EDA_ITEM* item : *this )
item->ClearFlags( m_flagsToClear );
}
};
/**
* Local and tunable method of qualifying the proximity of two points.
*
* @param aLeft is the first point.
* @param aRight is the second point.
* @param aLimit is a measure of proximity that the caller knows about.
* @return true if the two points are close enough, else false.
*/
static bool close_enough( VECTOR2I aLeft, VECTOR2I aRight, unsigned aLimit )
{
return ( aLeft - aRight ).SquaredEuclideanNorm() <= SEG::Square( aLimit );
}
/**
* Local method which qualifies whether the start or end point of a segment is closest to a point.
*
* @param aRef is the reference point
* @param aFirst is the first point
* @param aSecond is the second point
* @return true if the first point is closest to the reference, otherwise false.
*/
static bool closer_to_first( VECTOR2I aRef, VECTOR2I aFirst, VECTOR2I aSecond )
{
return ( aRef - aFirst ).SquaredEuclideanNorm() < ( aRef - aSecond ).SquaredEuclideanNorm();
}
/**
* Search for a #PCB_SHAPE matching a given end point or start point in a list.
*
* @param aShape The starting shape.
* @param aPoint The starting or ending point to search for.
* @param aList The list to remove from.
* @param aLimit is the distance from \a aPoint that still constitutes a valid find.
* @return The first #PCB_SHAPE that has a start or end point matching aPoint, otherwise nullptr.
*/
static PCB_SHAPE* findNext( PCB_SHAPE* aShape, const VECTOR2I& aPoint,
const std::vector<PCB_SHAPE*>& aList, unsigned aLimit )
{
// Look for an unused, exact hit
for( PCB_SHAPE* graphic : aList )
{
if( graphic == aShape || ( graphic->GetFlags() & SKIP_STRUCT ) != 0 )
continue;
if( aPoint == graphic->GetStart() || aPoint == graphic->GetEnd() )
return graphic;
}
// Search again for anything that's close, even something already used. (The latter is
// important for error reporting.)
VECTOR2I pt( aPoint );
SEG::ecoord closest_dist_sq = SEG::Square( aLimit );
PCB_SHAPE* closest_graphic = nullptr;
SEG::ecoord d_sq;
for( PCB_SHAPE* graphic : aList )
{
if( graphic == aShape )
continue;
d_sq = ( pt - graphic->GetStart() ).SquaredEuclideanNorm();
if( d_sq < closest_dist_sq )
{
closest_dist_sq = d_sq;
closest_graphic = graphic;
}
d_sq = ( pt - graphic->GetEnd() ).SquaredEuclideanNorm();
if( d_sq < closest_dist_sq )
{
closest_dist_sq = d_sq;
closest_graphic = graphic;
}
}
return closest_graphic; // Note: will be nullptr if nothing within aLimit
}
static bool isCopperOutside( const FOOTPRINT* aFootprint, SHAPE_POLY_SET& aShape )
{
bool padOutside = false;
for( PAD* pad : aFootprint->Pads() )
{
pad->Padstack().ForEachUniqueLayer(
[&]( PCB_LAYER_ID aLayer )
{
SHAPE_POLY_SET poly = aShape.CloneDropTriangulation();
poly.ClearArcs();
poly.BooleanIntersection( *pad->GetEffectivePolygon( aLayer, ERROR_INSIDE ),
SHAPE_POLY_SET::PM_FAST );
if( poly.OutlineCount() == 0 )
{
VECTOR2I padPos = pad->GetPosition();
wxLogTrace( traceBoardOutline, wxT( "Tested pad (%d, %d): outside" ),
padPos.x, padPos.y );
padOutside = true;
}
} );
if( padOutside )
break;
VECTOR2I padPos = pad->GetPosition();
wxLogTrace( traceBoardOutline, wxT( "Tested pad (%d, %d): not outside" ),
padPos.x, padPos.y );
}
return padOutside;
}
bool doConvertOutlineToPolygon( std::vector<PCB_SHAPE*>& aShapeList, SHAPE_POLY_SET& aPolygons,
int aErrorMax, int aChainingEpsilon, bool aAllowDisjoint,
OUTLINE_ERROR_HANDLER* aErrorHandler, bool aAllowUseArcsInPolygons,
SCOPED_FLAGS_CLEANER& aCleaner )
{
if( aShapeList.size() == 0 )
return true;
bool selfIntersecting = false;
wxString msg;
PCB_SHAPE* graphic = nullptr;
std::set<PCB_SHAPE*> startCandidates( aShapeList.begin(), aShapeList.end() );
// Keep a list of where the various shapes came from so after doing our combined-polygon
// tests we can still report errors against the individual graphic items.
std::map<std::pair<VECTOR2I, VECTOR2I>, PCB_SHAPE*> shapeOwners;
auto fetchOwner =
[&]( const SEG& seg ) -> PCB_SHAPE*
{
auto it = shapeOwners.find( std::make_pair( seg.A, seg.B ) );
return it == shapeOwners.end() ? nullptr : it->second;
};
PCB_SHAPE* prevGraphic = nullptr;
VECTOR2I prevPt;
std::vector<SHAPE_LINE_CHAIN> contours;
for( PCB_SHAPE* shape : startCandidates )
shape->ClearFlags( SKIP_STRUCT );
while( startCandidates.size() )
{
graphic = (PCB_SHAPE*) *startCandidates.begin();
graphic->SetFlags( SKIP_STRUCT );
aCleaner.insert( graphic );
startCandidates.erase( startCandidates.begin() );
contours.emplace_back();
SHAPE_LINE_CHAIN& currContour = contours.back();
currContour.SetWidth( graphic->GetWidth() );
bool firstPt = true;
// Circles, rects and polygons are closed shapes unto themselves (and do not combine
// with other shapes), so process them separately.
if( graphic->GetShape() == SHAPE_T::POLY )
{
for( auto it = graphic->GetPolyShape().CIterate(); it; it++ )
{
VECTOR2I pt = *it;
currContour.Append( pt );
if( firstPt )
firstPt = false;
else
shapeOwners[ std::make_pair( prevPt, pt ) ] = graphic;
prevPt = pt;
}
currContour.SetClosed( true );
}
else if( graphic->GetShape() == SHAPE_T::CIRCLE )
{
VECTOR2I center = graphic->GetCenter();
int radius = graphic->GetRadius();
VECTOR2I start = center;
start.x += radius;
// Add 360 deg Arc in currContour
SHAPE_ARC arc360( center, start, ANGLE_360, 0 );
currContour.Append( arc360, aErrorMax );
currContour.SetClosed( true );
// set shapeOwners for currContour points created by appending the arc360:
for( int ii = 1; ii < currContour.PointCount(); ++ii )
{
shapeOwners[ std::make_pair( currContour.CPoint( ii-1 ),
currContour.CPoint( ii ) ) ] = graphic;
}
if( !aAllowUseArcsInPolygons )
currContour.ClearArcs();
}
else if( graphic->GetShape() == SHAPE_T::RECTANGLE )
{
std::vector<VECTOR2I> pts = graphic->GetRectCorners();
for( const VECTOR2I& pt : pts )
{
currContour.Append( pt );
if( firstPt )
firstPt = false;
else
shapeOwners[ std::make_pair( prevPt, pt ) ] = graphic;
prevPt = pt;
}
currContour.SetClosed( true );
}
else
{
// Polygon start point. Arbitrarily chosen end of the segment and build the poly
// from here.
VECTOR2I startPt = graphic->GetEnd();
prevPt = startPt;
currContour.Append( prevPt );
// do not append the other end point yet, this first 'graphic' might be an arc
for(;;)
{
switch( graphic->GetShape() )
{
case SHAPE_T::RECTANGLE:
case SHAPE_T::CIRCLE:
{
// As a non-first item, closed shapes can't be anything but self-intersecting
if( aErrorHandler )
{
wxASSERT( prevGraphic );
(*aErrorHandler)( _( "(self-intersecting)" ), prevGraphic, graphic,
prevPt );
}
selfIntersecting = true;
// A closed shape will finish where it started, so no point in updating prevPt
break;
}
case SHAPE_T::SEGMENT:
{
VECTOR2I nextPt;
// Use the line segment end point furthest away from prevPt as we assume
// the other end to be ON prevPt or very close to it.
if( closer_to_first( prevPt, graphic->GetStart(), graphic->GetEnd()) )
nextPt = graphic->GetEnd();
else
nextPt = graphic->GetStart();
currContour.Append( nextPt );
shapeOwners[ std::make_pair( prevPt, nextPt ) ] = graphic;
prevPt = nextPt;
}
break;
case SHAPE_T::ARC:
{
VECTOR2I pstart = graphic->GetStart();
VECTOR2I pmid = graphic->GetArcMid();
VECTOR2I pend = graphic->GetEnd();
if( !close_enough( prevPt, pstart, aChainingEpsilon ) )
{
wxASSERT( close_enough( prevPt, graphic->GetEnd(), aChainingEpsilon ) );
std::swap( pstart, pend );
}
SHAPE_ARC sarc( pstart, pmid, pend, 0 );
SHAPE_LINE_CHAIN arcChain;
arcChain.Append( sarc, aErrorMax );
// if this arc is after another object, pop off the first point
// the previous point from the last object should be already close enough as part of chaining
if( prevGraphic != nullptr )
arcChain.Remove( 0 );
if( !aAllowUseArcsInPolygons )
arcChain.ClearArcs();
// set shapeOwners for arcChain points created by appending the sarc:
for( int ii = 1; ii < arcChain.PointCount(); ++ii )
{
shapeOwners[std::make_pair( arcChain.CPoint( ii - 1 ),
arcChain.CPoint( ii ) )] = graphic;
}
currContour.Append( arcChain );
prevPt = pend;
}
break;
case SHAPE_T::BEZIER:
{
// We do not support Bezier curves in polygons, so approximate with a series
// of short lines and put those line segments into the !same! PATH.
VECTOR2I nextPt;
bool reverse = false;
// Use the end point furthest away from prevPt as we assume the other
// end to be ON prevPt or very close to it.
if( closer_to_first( prevPt, graphic->GetStart(), graphic->GetEnd()) )
{
nextPt = graphic->GetEnd();
}
else
{
nextPt = graphic->GetStart();
reverse = true;
}
// Ensure the approximated Bezier shape is built
graphic->RebuildBezierToSegmentsPointsList( ARC_HIGH_DEF );
if( reverse )
{
for( int jj = graphic->GetBezierPoints().size()-1; jj >= 0; jj-- )
{
const VECTOR2I& pt = graphic->GetBezierPoints()[jj];
if( prevPt == pt )
continue;
currContour.Append( pt );
shapeOwners[ std::make_pair( prevPt, pt ) ] = graphic;
prevPt = pt;
}
}
else
{
for( const VECTOR2I& pt : graphic->GetBezierPoints() )
{
if( prevPt == pt )
continue;
currContour.Append( pt );
shapeOwners[ std::make_pair( prevPt, pt ) ] = graphic;
prevPt = pt;
}
}
prevPt = nextPt;
}
break;
default:
UNIMPLEMENTED_FOR( graphic->SHAPE_T_asString() );
return false;
}
// Get next closest segment.
PCB_SHAPE* nextGraphic = findNext( graphic, prevPt, aShapeList, aChainingEpsilon );
if( nextGraphic && !( nextGraphic->GetFlags() & SKIP_STRUCT ) )
{
prevGraphic = graphic;
graphic = nextGraphic;
graphic->SetFlags( SKIP_STRUCT );
aCleaner.insert( graphic );
startCandidates.erase( graphic );
continue;
}
// Finished, or ran into trouble...
if( close_enough( startPt, prevPt, aChainingEpsilon ) )
{
currContour.SetClosed( true );
break;
}
else if( nextGraphic ) // encountered already-used segment, but not at the start
{
if( aErrorHandler )
(*aErrorHandler)( _( "(self-intersecting)" ), graphic, nextGraphic,
prevPt );
break;
}
else // encountered discontinuity
{
if( aErrorHandler )
(*aErrorHandler)( _( "(not a closed shape)" ), graphic, nullptr, prevPt );
break;
}
}
}
}
for( const SHAPE_LINE_CHAIN& contour : contours )
{
if( !contour.IsClosed() )
return false;
}
// First, collect the parents of each contour
std::map<int, std::vector<int>> contourToParentIndexesMap;
for( size_t ii = 0; ii < contours.size(); ++ii )
{
VECTOR2I firstPt = contours[ii].GetPoint( 0 );
std::vector<int> parents;
for( size_t jj = 0; jj < contours.size(); ++jj )
{
if( jj == ii )
continue;
const SHAPE_LINE_CHAIN& parentCandidate = contours[jj];
if( parentCandidate.PointInside( firstPt ) )
parents.push_back( jj );
}
contourToParentIndexesMap[ii] = std::move( parents );
}
// Next add those that are top-level outlines to the SHAPE_POLY_SET
std::map<int, int> contourToOutlineIdxMap;
for( const auto& [ contourIndex, parentIndexes ] : contourToParentIndexesMap )
{
if( parentIndexes.size() %2 == 0 )
{
// Even number of parents; top-level outline
if( !aAllowDisjoint && !aPolygons.IsEmpty() )
{
if( aErrorHandler )
{
BOARD_ITEM* a = fetchOwner( aPolygons.Outline( 0 ).GetSegment( 0 ) );
BOARD_ITEM* b = fetchOwner( contours[ contourIndex ].GetSegment( 0 ) );
if( a && b )
{
(*aErrorHandler)( _( "(multiple board outlines not supported)" ), a, b,
contours[ contourIndex ].GetPoint( 0 ) );
return false;
}
}
}
aPolygons.AddOutline( contours[ contourIndex ] );
contourToOutlineIdxMap[ contourIndex ] = aPolygons.OutlineCount() - 1;
}
}
// And finally add the holes
for( const auto& [ contourIndex, parentIndexes ] : contourToParentIndexesMap )
{
if( parentIndexes.size() %2 == 1 )
{
// Odd number of parents; we're a hole in the parent which has one fewer parents
// than we have.
const SHAPE_LINE_CHAIN& hole = contours[ contourIndex ];
for( int parentContourIdx : parentIndexes )
{
if( contourToParentIndexesMap[ parentContourIdx ].size() == parentIndexes.size() - 1 )
{
int outlineIdx = contourToOutlineIdxMap[ parentContourIdx ];
aPolygons.AddHole( hole, outlineIdx );
break;
}
}
}
}
// All of the silliness that follows is to work around the segment iterator while checking
// for collisions.
// TODO: Implement proper segment and point iterators that follow std
for( auto seg1 = aPolygons.IterateSegmentsWithHoles(); seg1; seg1++ )
{
auto seg2 = seg1;
for( ++seg2; seg2; seg2++ )
{
// Check for exact overlapping segments.
if( *seg1 == *seg2 || ( ( *seg1 ).A == ( *seg2 ).B && ( *seg1 ).B == ( *seg2 ).A ) )
{
if( aErrorHandler )
{
BOARD_ITEM* a = fetchOwner( *seg1 );
BOARD_ITEM* b = fetchOwner( *seg2 );
(*aErrorHandler)( _( "(self-intersecting)" ), a, b, ( *seg1 ).A );
}
selfIntersecting = true;
}
if( OPT_VECTOR2I pt = seg1.Get().Intersect( seg2.Get(), true ) )
{
if( aErrorHandler )
{
BOARD_ITEM* a = fetchOwner( *seg1 );
BOARD_ITEM* b = fetchOwner( *seg2 );
(*aErrorHandler)( _( "(self-intersecting)" ), a, b, *pt );
}
selfIntersecting = true;
}
}
}
return !selfIntersecting;
}
bool ConvertOutlineToPolygon( std::vector<PCB_SHAPE*>& aShapeList, SHAPE_POLY_SET& aPolygons,
int aErrorMax, int aChainingEpsilon, bool aAllowDisjoint,
OUTLINE_ERROR_HANDLER* aErrorHandler, bool aAllowUseArcsInPolygons )
{
SCOPED_FLAGS_CLEANER cleaner( SKIP_STRUCT );
return doConvertOutlineToPolygon( aShapeList, aPolygons, aErrorMax, aChainingEpsilon,
aAllowDisjoint, aErrorHandler, aAllowUseArcsInPolygons,
cleaner );
}
bool TestBoardOutlinesGraphicItems( BOARD* aBoard, int aMinDist,
OUTLINE_ERROR_HANDLER* aErrorHandler )
{
bool success = true;
PCB_TYPE_COLLECTOR items;
int min_dist = std::max( 0, aMinDist );
// Get all the shapes into 'items', then keep only those on layer == Edge_Cuts.
items.Collect( aBoard, { PCB_SHAPE_T } );
std::vector<PCB_SHAPE*> shapeList;
for( int ii = 0; ii < items.GetCount(); ii++ )
{
PCB_SHAPE* seg = static_cast<PCB_SHAPE*>( items[ii] );
if( seg->GetLayer() == Edge_Cuts )
shapeList.push_back( seg );
}
// Now Test validity of collected items
for( PCB_SHAPE* shape : shapeList )
{
switch( shape->GetShape() )
{
case SHAPE_T::RECTANGLE:
{
VECTOR2I seg = shape->GetEnd() - shape->GetStart();
int dim = seg.EuclideanNorm();
if( dim <= min_dist )
{
success = false;
if( aErrorHandler )
{
(*aErrorHandler)( wxString::Format( _( "(rectangle has null or very small "
"size: %d nm)" ), dim ),
shape, nullptr, shape->GetStart() );
}
}
break;
}
case SHAPE_T::CIRCLE:
{
int r = shape->GetRadius();
if( r <= min_dist )
{
success = false;
if( aErrorHandler )
{
(*aErrorHandler)( wxString::Format( _( "(circle has null or very small "
"radius: %d nm)" ), r ),
shape, nullptr, shape->GetStart() );
}
}
break;
}
case SHAPE_T::SEGMENT:
{
VECTOR2I seg = shape->GetEnd() - shape->GetStart();
int dim = seg.EuclideanNorm();
if( dim <= min_dist )
{
success = false;
if( aErrorHandler )
{
(*aErrorHandler)( wxString::Format( _( "(segment has null or very small "
"length: %d nm)" ), dim ),
shape, nullptr, shape->GetStart() );
}
}
break;
}
case SHAPE_T::ARC:
{
// Arc size can be evaluated from the distance between arc middle point and arc ends
// We do not need a precise value, just an idea of its size
VECTOR2I arcMiddle = shape->GetArcMid();
VECTOR2I seg1 = arcMiddle - shape->GetStart();
VECTOR2I seg2 = shape->GetEnd() - arcMiddle;
int dim = seg1.EuclideanNorm() + seg2.EuclideanNorm();
if( dim <= min_dist )
{
success = false;
if( aErrorHandler )
{
(*aErrorHandler)( wxString::Format( _( "(arc has null or very small size: "
"%d nm)" ), dim ),
shape, nullptr, shape->GetStart() );
}
}
break;
}
case SHAPE_T::POLY:
break;
case SHAPE_T::BEZIER:
break;
default:
UNIMPLEMENTED_FOR( shape->SHAPE_T_asString() );
return false;
}
}
return success;
}
bool BuildBoardPolygonOutlines( BOARD* aBoard, SHAPE_POLY_SET& aOutlines, int aErrorMax,
int aChainingEpsilon, OUTLINE_ERROR_HANDLER* aErrorHandler,
bool aAllowUseArcsInPolygons )
{
PCB_TYPE_COLLECTOR items;
SHAPE_POLY_SET fpHoles;
bool success = false;
SCOPED_FLAGS_CLEANER cleaner( SKIP_STRUCT );
// Get all the shapes into 'items', then keep only those on layer == Edge_Cuts.
items.Collect( aBoard, { PCB_SHAPE_T } );
for( int ii = 0; ii < items.GetCount(); ++ii )
items[ii]->ClearFlags( SKIP_STRUCT );
for( FOOTPRINT* fp : aBoard->Footprints() )
{
PCB_TYPE_COLLECTOR fpItems;
fpItems.Collect( fp, { PCB_SHAPE_T } );
std::vector<PCB_SHAPE*> fpSegList;
for( int ii = 0; ii < fpItems.GetCount(); ii++ )
{
PCB_SHAPE* fpSeg = static_cast<PCB_SHAPE*>( fpItems[ii] );
if( fpSeg->GetLayer() == Edge_Cuts )
fpSegList.push_back( fpSeg );
}
if( !fpSegList.empty() )
{
SHAPE_POLY_SET fpOutlines;
success = doConvertOutlineToPolygon( fpSegList, fpOutlines, aErrorMax, aChainingEpsilon,
false,
// don't report errors here; the second pass also
// gets an opportunity to use these segments
nullptr, aAllowUseArcsInPolygons, cleaner );
// Test to see if we should make holes or outlines. Holes are made if the footprint
// has copper outside of a single, closed outline. If there are multiple outlines,
// we assume that the footprint edges represent holes as we do not support multiple
// boards. Similarly, if any of the footprint pads are located outside of the edges,
// then the edges are holes
if( success && ( isCopperOutside( fp, fpOutlines ) || fpOutlines.OutlineCount() > 1 ) )
{
fpHoles.Append( fpOutlines );
}
else
{
// If it wasn't a closed area, or wasn't a hole, the we want to keep the fpSegs
// in contention for the board outline builds.
for( int ii = 0; ii < fpItems.GetCount(); ++ii )
fpItems[ii]->ClearFlags( SKIP_STRUCT );
}
}
}
// Make a working copy of aSegList, because the list is modified during calculations
std::vector<PCB_SHAPE*> segList;
for( int ii = 0; ii < items.GetCount(); ii++ )
{
PCB_SHAPE* seg = static_cast<PCB_SHAPE*>( items[ii] );
// Skip anything already used to generate footprint holes (above)
if( seg->GetFlags() & SKIP_STRUCT )
continue;
if( seg->GetLayer() == Edge_Cuts )
segList.push_back( seg );
}
if( segList.size() )
{
success = doConvertOutlineToPolygon( segList, aOutlines, aErrorMax, aChainingEpsilon, true,
aErrorHandler, aAllowUseArcsInPolygons, cleaner );
}
if( !success || !aOutlines.OutlineCount() )
{
// Couldn't create a valid polygon outline. Use the board edge cuts bounding box to
// create a rectangular outline, or, failing that, the bounding box of the items on
// the board.
BOX2I bbbox = aBoard->GetBoardEdgesBoundingBox();
// If null area, uses the global bounding box.
if( ( bbbox.GetWidth() ) == 0 || ( bbbox.GetHeight() == 0 ) )
bbbox = aBoard->ComputeBoundingBox( false );
// Ensure non null area. If happen, gives a minimal size.
if( ( bbbox.GetWidth() ) == 0 || ( bbbox.GetHeight() == 0 ) )
bbbox.Inflate( pcbIUScale.mmToIU( 1.0 ) );
aOutlines.RemoveAllContours();
aOutlines.NewOutline();
VECTOR2I corner;
aOutlines.Append( bbbox.GetOrigin() );
corner.x = bbbox.GetOrigin().x;
corner.y = bbbox.GetEnd().y;
aOutlines.Append( corner );
aOutlines.Append( bbbox.GetEnd() );
corner.x = bbbox.GetEnd().x;
corner.y = bbbox.GetOrigin().y;
aOutlines.Append( corner );
}
for( int ii = 0; ii < fpHoles.OutlineCount(); ++ii )
{
const VECTOR2I holePt = fpHoles.Outline( ii ).CPoint( 0 );
for( int jj = 0; jj < aOutlines.OutlineCount(); ++jj )
{
if( aOutlines.Outline( jj ).PointInside( holePt ) )
{
aOutlines.AddHole( fpHoles.Outline( ii ), jj );
break;
}
}
}
return success;
}
/**
* Get the complete bounding box of the board (including all items).
*
* The vertex numbers and segment numbers of the rectangle returned.
* 1
* *---------------*
* |1 2|
* 0| |2
* |0 3|
* *---------------*
* 3
*/
void buildBoardBoundingBoxPoly( const BOARD* aBoard, SHAPE_POLY_SET& aOutline )
{
BOX2I bbbox = aBoard->GetBoundingBox();
SHAPE_LINE_CHAIN chain;
// If null area, uses the global bounding box.
if( ( bbbox.GetWidth() ) == 0 || ( bbbox.GetHeight() == 0 ) )
bbbox = aBoard->ComputeBoundingBox( false );
// Ensure non null area. If happen, gives a minimal size.
if( ( bbbox.GetWidth() ) == 0 || ( bbbox.GetHeight() == 0 ) )
bbbox.Inflate( pcbIUScale.mmToIU( 1.0 ) );
// Inflate slightly (by 1/10th the size of the box)
bbbox.Inflate( bbbox.GetWidth() / 10, bbbox.GetHeight() / 10 );
chain.Append( bbbox.GetOrigin() );
chain.Append( bbbox.GetOrigin().x, bbbox.GetEnd().y );
chain.Append( bbbox.GetEnd() );
chain.Append( bbbox.GetEnd().x, bbbox.GetOrigin().y );
chain.SetClosed( true );
aOutline.RemoveAllContours();
aOutline.AddOutline( chain );
}
VECTOR2I projectPointOnSegment( const VECTOR2I& aEndPoint, const SHAPE_POLY_SET& aOutline,
int aOutlineNum = 0 )
{
int minDistance = -1;
VECTOR2I projPoint;
for( auto it = aOutline.CIterateSegments( aOutlineNum ); it; it++ )
{
auto seg = it.Get();
int dis = seg.Distance( aEndPoint );
if( minDistance < 0 || ( dis < minDistance ) )
{
minDistance = dis;
projPoint = seg.NearestPoint( aEndPoint );
}
}
return projPoint;
}
int findEndSegments( SHAPE_LINE_CHAIN& aChain, SEG& aStartSeg, SEG& aEndSeg )
{
int foundSegs = 0;
for( int i = 0; i < aChain.SegmentCount(); i++ )
{
SEG seg = aChain.Segment( i );
bool foundA = false;
bool foundB = false;
for( int j = 0; j < aChain.SegmentCount(); j++ )
{
// Don't test the segment against itself
if( i == j )
continue;
SEG testSeg = aChain.Segment( j );
if( testSeg.Contains( seg.A ) )
foundA = true;
if( testSeg.Contains( seg.B ) )
foundB = true;
}
// This segment isn't a start or end
if( foundA && foundB )
continue;
if( foundSegs == 0 )
{
// The first segment we encounter is the "start" segment
wxLogTrace( traceBoardOutline, wxT( "Found start segment: (%d, %d)-(%d, %d)" ),
seg.A.x, seg.A.y, seg.B.x, seg.B.y );
aStartSeg = seg;
foundSegs++;
}
else
{
// Once we find both start and end, we can stop
wxLogTrace( traceBoardOutline, wxT( "Found end segment: (%d, %d)-(%d, %d)" ),
seg.A.x, seg.A.y, seg.B.x, seg.B.y );
aEndSeg = seg;
foundSegs++;
break;
}
}
return foundSegs;
}
bool BuildFootprintPolygonOutlines( BOARD* aBoard, SHAPE_POLY_SET& aOutlines, int aErrorMax,
int aChainingEpsilon, OUTLINE_ERROR_HANDLER* aErrorHandler )
{
FOOTPRINT* footprint = aBoard->GetFirstFootprint();
// No footprint loaded
if( !footprint )
{
wxLogTrace( traceBoardOutline, wxT( "No footprint found on board" ) );
return false;
}
PCB_TYPE_COLLECTOR items;
SHAPE_POLY_SET outlines;
bool success = false;
SCOPED_FLAGS_CLEANER cleaner( SKIP_STRUCT );
// Get all the SHAPEs into 'items', then keep only those on layer == Edge_Cuts.
items.Collect( aBoard, { PCB_SHAPE_T } );
// Make a working copy of aSegList, because the list is modified during calculations
std::vector<PCB_SHAPE*> segList;
for( int ii = 0; ii < items.GetCount(); ii++ )