KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_zone_filler.cpp
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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25#include <boost/test/data/test_case.hpp>
26
27#include <chrono>
28
30#include <board.h>
31#include <board_commit.h>
33#include <drc/drc_engine.h>
34#include <pad.h>
35#include <pcb_track.h>
36#include <footprint.h>
37#include <zone.h>
38#include <drc/drc_engine.h>
39#include <drc/drc_item.h>
42#include <advanced_config.h>
44#include <teardrop/teardrop.h>
45
46
49static void CheckAllOutlineAreasAtLeast( const std::shared_ptr<SHAPE_POLY_SET>& aFill,
50 double aMinArea, const wxString& aLabel )
51{
52 for( int ii = 0; ii < aFill->OutlineCount(); ++ii )
53 {
54 const double area = std::abs( aFill->Outline( ii ).Area() );
55
56 BOOST_CHECK_MESSAGE( area >= aMinArea,
57 wxString::Format( "%s %d area %.0f IU^2 below %.0f IU^2; partial "
58 "stamps should not survive.",
59 aLabel, ii, area, aMinArea ) );
60 }
61}
62
63
72
73
74int delta = KiROUND( 0.006 * pcbIUScale.IU_PER_MM );
75
76
78{
79 KI_TEST::LoadBoard( m_settingsManager, "zone_filler", m_board );
80
81 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
82
83 KI_TEST::FillZones( m_board.get() );
84
85 // Now that the zones are filled we're going to increase the size of -some- pads and
86 // tracks so that they generate DRC errors. The test then makes sure that those errors
87 // are generated, and that the other pads and tracks do -not- generate errors.
88
89 for( PAD* pad : m_board->Footprints()[0]->Pads() )
90 {
91 if( pad->GetNumber() == "2" || pad->GetNumber() == "4" || pad->GetNumber() == "6" )
92 {
93 pad->SetSize( PADSTACK::ALL_LAYERS,
94 pad->GetSize( PADSTACK::ALL_LAYERS ) + VECTOR2I( delta, delta ) );
95 }
96 }
97
98 int ii = 0;
99 KIID arc8;
100 KIID arc12;
101
102 for( PCB_TRACK* track : m_board->Tracks() )
103 {
104 if( track->Type() == PCB_ARC_T )
105 {
106 ii++;
107
108 if( ii == 8 )
109 {
110 arc8 = track->m_Uuid;
111 track->SetWidth( track->GetWidth() + delta + delta );
112 }
113 else if( ii == 12 )
114 {
115 arc12 = track->m_Uuid;
116 track->Move( VECTOR2I( -delta, -delta ) );
117 }
118 }
119 }
120
121 bool foundPad2Error = false;
122 bool foundPad4Error = false;
123 bool foundPad6Error = false;
124 bool foundArc8Error = false;
125 bool foundArc12Error = false;
126 bool foundOtherError = false;
127
128 bds.m_DRCEngine->InitEngine( wxFileName() ); // Just to be sure to be sure
129
131 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
132 const std::function<void( PCB_MARKER* )>& aPathGenerator )
133 {
134 if( aItem->GetErrorCode() == DRCE_CLEARANCE )
135 {
136 BOARD_ITEM* item_a = m_board->ResolveItem( aItem->GetMainItemID() );
137 PAD* pad_a = dynamic_cast<PAD*>( item_a );
138 PCB_TRACK* trk_a = dynamic_cast<PCB_TRACK*>( item_a );
139
140 BOARD_ITEM* item_b = m_board->ResolveItem( aItem->GetAuxItemID() );
141 PAD* pad_b = dynamic_cast<PAD*>( item_b );
142 PCB_TRACK* trk_b = dynamic_cast<PCB_TRACK*>( item_b );
143
144 if( pad_a && pad_a->GetNumber() == "2" ) foundPad2Error = true;
145 else if( pad_a && pad_a->GetNumber() == "4" ) foundPad4Error = true;
146 else if( pad_a && pad_a->GetNumber() == "6" ) foundPad6Error = true;
147 else if( pad_b && pad_b->GetNumber() == "2" ) foundPad2Error = true;
148 else if( pad_b && pad_b->GetNumber() == "4" ) foundPad4Error = true;
149 else if( pad_b && pad_b->GetNumber() == "6" ) foundPad6Error = true;
150 else if( trk_a && trk_a->m_Uuid == arc8 ) foundArc8Error = true;
151 else if( trk_a && trk_a->m_Uuid == arc12 ) foundArc12Error = true;
152 else if( trk_b && trk_b->m_Uuid == arc8 ) foundArc8Error = true;
153 else if( trk_b && trk_b->m_Uuid == arc12 ) foundArc12Error = true;
154 else foundOtherError = true;
155
156 }
157 } );
158
159 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
160
161 BOOST_CHECK_EQUAL( foundPad2Error, true );
162 BOOST_CHECK_EQUAL( foundPad4Error, true );
163 BOOST_CHECK_EQUAL( foundPad6Error, true );
164 BOOST_CHECK_EQUAL( foundArc8Error, true );
165 BOOST_CHECK_EQUAL( foundArc12Error, true );
166 BOOST_CHECK_EQUAL( foundOtherError, false );
167}
168
169
171{
172 KI_TEST::LoadBoard( m_settingsManager, "notched_zones", m_board );
173
174 // Older algorithms had trouble where the filleted zones intersected and left notches.
175 // See:
176 // https://gitlab.com/kicad/code/kicad/-/issues/2737
177 // https://gitlab.com/kicad/code/kicad/-/issues/2752
178 SHAPE_POLY_SET frontCopper;
179
180 KI_TEST::FillZones( m_board.get() );
181
182 frontCopper = SHAPE_POLY_SET();
183
184 for( ZONE* zone : m_board->Zones() )
185 {
186 if( zone->GetLayerSet().Contains( F_Cu ) )
187 {
188 frontCopper.BooleanAdd( *zone->GetFilledPolysList( F_Cu ) );
189 }
190 }
191
192 BOOST_CHECK_EQUAL( frontCopper.OutlineCount(), 2 );
193}
194
195
196static const std::vector<wxString> RegressionZoneFillTests_tests = {
197 "issue18",
198 "issue2568",
199 "issue3812",
200 "issue5102",
201 "issue5313",
202 "issue5320",
203 "issue5567",
204 "issue5830",
205 "issue6039",
206 "issue6260",
207 "issue6284",
208 "issue7086",
209 "issue14294", // Bad Clipper2 fill
210 "fill_bad" // Missing zone clearance expansion
211};
212
213
215 boost::unit_test::data::make( RegressionZoneFillTests_tests ), relPath )
216{
217 KI_TEST::LoadBoard( m_settingsManager, relPath, m_board );
218
219 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
220
221 KI_TEST::FillZones( m_board.get() );
222
223 std::vector<DRC_ITEM> violations;
224
226 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
227 const std::function<void( PCB_MARKER* )>& aPathGenerator )
228 {
229 if( aItem->GetErrorCode() == DRCE_CLEARANCE )
230 violations.push_back( *aItem );
231 } );
232
233 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
234
235 if( violations.empty() )
236 {
237 BOOST_CHECK_EQUAL( 1, 1 ); // quiet "did not check any assertions" warning
238 BOOST_TEST_MESSAGE( wxString::Format( "Zone fill regression: %s passed", relPath ) );
239 }
240 else
241 {
242 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::INCH );
243
244 std::map<KIID, EDA_ITEM*> itemMap;
245 m_board->FillItemMap( itemMap );
246
247 for( const DRC_ITEM& item : violations )
248 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR, itemMap ) );
249
250 BOOST_ERROR( wxString::Format( "Zone fill regression: %s failed", relPath ) );
251 }
252}
253
254
264BOOST_FIXTURE_TEST_CASE( RegressionZoneClearanceWithIterativeRefill, ZONE_FILL_TEST_FIXTURE )
265{
266 ADVANCED_CFG& cfg = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() );
267 bool originalIterativeRefill = cfg.m_ZoneFillIterativeRefill;
268
269 struct ScopeGuard { bool& ref; bool orig; ~ScopeGuard() { ref = orig; } }
270 guard{ cfg.m_ZoneFillIterativeRefill, originalIterativeRefill };
271
272 auto runDrcClearanceCheck =
273 [this]( bool aIterative ) -> int
274 {
275 ADVANCED_CFG& innerCfg = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() );
276 innerCfg.m_ZoneFillIterativeRefill = aIterative;
277
278 KI_TEST::LoadBoard( m_settingsManager, "issue23053/issue23053", m_board );
279
280 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
281
282 KI_TEST::FillZones( m_board.get() );
283
284 std::vector<DRC_ITEM> violations;
285
286 std::map<KIID, EDA_ITEM*> itemMap;
287 m_board->FillItemMap( itemMap );
288 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::MM );
289
291 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos,
292 int aLayer,
293 const std::function<void( PCB_MARKER* )>& aPathGenerator )
294 {
295 if( aItem->GetErrorCode() == DRCE_CLEARANCE )
296 {
297 BOARD_ITEM* itemA = m_board->ResolveItem( aItem->GetMainItemID() );
298 BOARD_ITEM* itemB = m_board->ResolveItem( aItem->GetAuxItemID() );
299
300 if( dynamic_cast<ZONE*>( itemA ) && dynamic_cast<ZONE*>( itemB ) )
301 {
302 violations.push_back( *aItem );
303
305 aItem->ShowReport( &unitsProvider,
306 RPT_SEVERITY_ERROR, itemMap ) );
307 }
308 }
309 } );
310
311 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
312
313 return static_cast<int>( violations.size() );
314 };
315
316 int iterativeViolations = runDrcClearanceCheck( true );
317
318 BOOST_CHECK_MESSAGE( iterativeViolations == 0,
319 wxString::Format( "Iterative refill produced %d zone-to-zone clearance "
320 "violations (expected 0)", iterativeViolations ) );
321
322 int nonIterativeViolations = runDrcClearanceCheck( false );
323
324 BOOST_CHECK_MESSAGE( nonIterativeViolations == 0,
325 wxString::Format( "Non-iterative refill produced %d zone-to-zone clearance "
326 "violations (expected 0)", nonIterativeViolations ) );
327}
328
329
330static const std::vector<wxString> RegressionSliverZoneFillTests_tests = {
331 "issue16182" // Slivers
332};
333
334
335BOOST_DATA_TEST_CASE_F( ZONE_FILL_TEST_FIXTURE, RegressionSliverZoneFillTests,
336 boost::unit_test::data::make( RegressionSliverZoneFillTests_tests ),
337 relPath )
338{
339 KI_TEST::LoadBoard( m_settingsManager, relPath, m_board );
340
341 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
342
343 KI_TEST::FillZones( m_board.get() );
344
345 std::vector<DRC_ITEM> violations;
346
348 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
349 const std::function<void( PCB_MARKER* )>& aPathGenerator )
350 {
351 if( aItem->GetErrorCode() == DRCE_COPPER_SLIVER )
352 violations.push_back( *aItem );
353 } );
354
355 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
356
357 if( violations.empty() )
358 {
359 BOOST_CHECK_EQUAL( 1, 1 ); // quiet "did not check any assertions" warning
360 BOOST_TEST_MESSAGE( wxString::Format( "Zone fill copper sliver regression: %s passed", relPath ) );
361 }
362 else
363 {
364 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::INCH );
365
366 std::map<KIID, EDA_ITEM*> itemMap;
367 m_board->FillItemMap( itemMap );
368
369 for( const DRC_ITEM& item : violations )
370 BOOST_TEST_MESSAGE( item.ShowReport( &unitsProvider, RPT_SEVERITY_ERROR, itemMap ) );
371
372 BOOST_ERROR( wxString::Format( "Zone fill copper sliver regression: %s failed", relPath ) );
373 }
374}
375
376
377static const std::vector<std::pair<wxString,int>> RegressionTeardropFill_tests = {
378 { "teardrop_issue_JPC2", 5 }, // Arcs with teardrops connecting to pads
379};
380
381
383 boost::unit_test::data::make( RegressionTeardropFill_tests ), test )
384{
385 const wxString& relPath = test.first;
386 const int count = test.second;
387
388 KI_TEST::LoadBoard( m_settingsManager, relPath, m_board );
389
390 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
391
392 KI_TEST::FillZones( m_board.get() );
393
394 int zoneCount = 0;
395
396 for( ZONE* zone : m_board->Zones() )
397 {
398 if( zone->IsTeardropArea() )
399 zoneCount++;
400 }
401
402 BOOST_CHECK_MESSAGE( zoneCount == count, "Expected " << count << " teardrop zones in "
403 << relPath << ", found "
404 << zoneCount );
405}
406
407
409{
410
411 std::vector<wxString> tests = { { "issue19956/issue19956" } // Arcs with teardrops connecting to pads
412 };
413
414 for( const wxString& relPath : tests )
415 {
416 KI_TEST::LoadBoard( m_settingsManager, relPath, m_board );
417 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
418 KI_TEST::FillZones( m_board.get() );
419
420 for( ZONE* zone : m_board->Zones() )
421 {
422 for( PCB_LAYER_ID layer : zone->GetLayerSet() )
423 {
424 std::shared_ptr<SHAPE> a_shape( zone->GetEffectiveShape( layer ) );
425
426 for( PAD* pad : m_board->GetPads() )
427 {
428 std::shared_ptr<SHAPE> pad_shape( pad->GetEffectiveShape( layer ) );
429 int clearance = pad_shape->GetClearance( a_shape.get() );
430 BOOST_CHECK_MESSAGE( pad->GetNetCode() == zone->GetNetCode() || clearance != 0,
431 wxString::Format( "Pad %s from Footprint %s has net code %s and "
432 "is connected to zone with net code %s",
433 pad->GetNumber(),
434 pad->GetParentFootprint()->GetReferenceAsString(),
435 pad->GetNetname(),
436 zone->GetNetname() ) );
437 }
438 }
439 }
440 }
441}
442
443
458BOOST_FIXTURE_TEST_CASE( RegressionZonePriorityIsolatedIslands, ZONE_FILL_TEST_FIXTURE )
459{
460 // Enable iterative refill to fix issue 21746
461 ADVANCED_CFG& cfg = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() );
462 bool originalIterativeRefill = cfg.m_ZoneFillIterativeRefill;
463 cfg.m_ZoneFillIterativeRefill = true;
464
465 // Restore config at end of scope to avoid polluting other tests
466 struct ScopeGuard { bool& ref; bool orig; ~ScopeGuard() { ref = orig; } } guard{ cfg.m_ZoneFillIterativeRefill, originalIterativeRefill };
467
468 KI_TEST::LoadBoard( m_settingsManager, "issue21746/issue21746", m_board );
469
470 KI_TEST::FillZones( m_board.get() );
471
472 // Find the GND zone
473 ZONE* gndZone = nullptr;
474
475 for( ZONE* zone : m_board->Zones() )
476 {
477 if( zone->GetNetname() == "GND" )
478 {
479 gndZone = zone;
480 break;
481 }
482 }
483
484 BOOST_REQUIRE_MESSAGE( gndZone != nullptr, "GND zone not found in test board" );
485
486 // Calculate board outline area
487 SHAPE_POLY_SET boardOutline;
488 bool hasOutline = m_board->GetBoardPolygonOutlines( boardOutline, true );
489 BOOST_REQUIRE_MESSAGE( hasOutline, "Board outline not found" );
490
491 double boardArea = 0.0;
492
493 for( int i = 0; i < boardOutline.OutlineCount(); i++ )
494 boardArea += boardOutline.Outline( i ).Area();
495
496 // Get GND zone filled area
497 gndZone->CalculateFilledArea();
498 double gndFilledArea = gndZone->GetFilledArea();
499
500 // The GND zone should fill at least 25% of the board area
501 // With the bug, it fills almost nothing because VDD knocks it out
502 double fillRatio = gndFilledArea / boardArea;
503
504 BOOST_TEST_MESSAGE( wxString::Format( "Board area: %.2f sq mm, GND filled area: %.2f sq mm, "
505 "Fill ratio: %.1f%%",
506 boardArea / 1e6, gndFilledArea / 1e6,
507 fillRatio * 100.0 ) );
508
509 BOOST_CHECK_MESSAGE( fillRatio >= 0.25,
510 wxString::Format( "GND zone fill ratio %.1f%% is less than expected 25%%. "
511 "This indicates issue 21746 - lower priority zones not "
512 "filling areas where higher priority isolated islands "
513 "were removed.",
514 fillRatio * 100.0 ) );
515}
516
517
531BOOST_FIXTURE_TEST_CASE( RegressionViaFlashingUnreachableZone, ZONE_FILL_TEST_FIXTURE )
532{
533 KI_TEST::LoadBoard( m_settingsManager, "issue22010/issue22010", m_board );
534
535 KI_TEST::FillZones( m_board.get() );
536
537 // Find vias with zone_layer_connections set for In1.Cu or In2.Cu
538 // After filling, vias that the zone doesn't actually reach should NOT be flashed
539 int viasWithUnreachableFlashing = 0;
540 int totalConditionalVias = 0;
541
542 PCB_LAYER_ID in1Cu = m_board->GetLayerID( wxT( "In1.Cu" ) );
543 PCB_LAYER_ID in2Cu = m_board->GetLayerID( wxT( "In2.Cu" ) );
544
545 for( PCB_TRACK* track : m_board->Tracks() )
546 {
547 if( track->Type() != PCB_VIA_T )
548 continue;
549
550 PCB_VIA* via = static_cast<PCB_VIA*>( track );
551
552 if( !via->GetRemoveUnconnected() )
553 continue;
554
555 totalConditionalVias++;
556
557 // Check if via is flashed on In1.Cu or In2.Cu
558 bool flashedOnIn1 = via->FlashLayer( in1Cu );
559 bool flashedOnIn2 = via->FlashLayer( in2Cu );
560
561 if( !flashedOnIn1 && !flashedOnIn2 )
562 continue;
563
564 VECTOR2I viaCenter = via->GetPosition();
565 int holeRadius = via->GetDrillValue() / 2;
566
567 // Check if any zone fill actually reaches this via
568 bool zoneReachesVia = false;
569
570 for( ZONE* zone : m_board->Zones() )
571 {
572 if( zone->GetIsRuleArea() )
573 continue;
574
575 if( zone->GetNetCode() != via->GetNetCode() )
576 continue;
577
578 for( PCB_LAYER_ID layer : { in1Cu, in2Cu } )
579 {
580 if( !zone->IsOnLayer( layer ) )
581 continue;
582
583 if( !zone->HasFilledPolysForLayer( layer ) )
584 continue;
585
586 const std::shared_ptr<SHAPE_POLY_SET>& fill = zone->GetFilledPolysList( layer );
587
588 if( fill->Contains( viaCenter, -1, holeRadius ) )
589 {
590 zoneReachesVia = true;
591 break;
592 }
593 }
594
595 if( zoneReachesVia )
596 break;
597 }
598
599 // If via is flashed but zone doesn't reach it, that's the bug
600 if( !zoneReachesVia && ( flashedOnIn1 || flashedOnIn2 ) )
601 viasWithUnreachableFlashing++;
602 }
603
604 BOOST_TEST_MESSAGE( wxString::Format( "Total conditional vias: %d, Vias with unreachable "
605 "flashing: %d", totalConditionalVias,
606 viasWithUnreachableFlashing ) );
607
608 BOOST_CHECK_MESSAGE( viasWithUnreachableFlashing == 0,
609 wxString::Format( "Found %d vias flashed on zone layers where the zone "
610 "fill doesn't actually reach them. This indicates "
611 "issue 22010 is not fixed.",
612 viasWithUnreachableFlashing ) );
613}
614
615
629{
630 KI_TEST::LoadBoard( m_settingsManager, "issue12964/issue12964", m_board );
631
632 KI_TEST::FillZones( m_board.get() );
633
634 int viasShortingZones = 0;
635 int totalConditionalVias = 0;
636
637 for( PCB_TRACK* track : m_board->Tracks() )
638 {
639 if( track->Type() != PCB_VIA_T )
640 continue;
641
642 PCB_VIA* via = static_cast<PCB_VIA*>( track );
643
644 if( !via->GetRemoveUnconnected() )
645 continue;
646
647 totalConditionalVias++;
648
649 VECTOR2I viaCenter = via->GetPosition();
650
651 for( ZONE* zone : m_board->Zones() )
652 {
653 if( zone->GetIsRuleArea() )
654 continue;
655
656 if( zone->GetNetCode() == via->GetNetCode() )
657 continue;
658
659 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
660 {
661 if( !via->FlashLayer( layer ) )
662 continue;
663
664 if( !zone->HasFilledPolysForLayer( layer ) )
665 continue;
666
667 const std::shared_ptr<SHAPE_POLY_SET>& fill = zone->GetFilledPolysList( layer );
668 int viaRadius = via->GetWidth( layer ) / 2;
669
670 if( fill->Contains( viaCenter, -1, viaRadius ) )
671 {
672 BOOST_TEST_MESSAGE( wxString::Format(
673 "Via at (%d, %d) on net %s is flashing on layer %s where zone "
674 "net %s is filled - this creates a short!",
675 viaCenter.x, viaCenter.y, via->GetNetname(),
676 m_board->GetLayerName( layer ), zone->GetNetname() ) );
677 viasShortingZones++;
678 }
679 }
680 }
681 }
682
683 BOOST_TEST_MESSAGE( wxString::Format( "Total conditional vias: %d, Vias shorting zones: %d",
684 totalConditionalVias, viasShortingZones ) );
685
686 BOOST_CHECK_MESSAGE( viasShortingZones == 0,
687 wxString::Format( "Found %d vias flashed on layers where they short to "
688 "zones with different nets. This indicates issue 12964 "
689 "is not fixed.",
690 viasShortingZones ) );
691}
692
693
706BOOST_FIXTURE_TEST_CASE( HatchZoneThermalConnectivity, ZONE_FILL_TEST_FIXTURE )
707{
708 KI_TEST::LoadBoard( m_settingsManager, "hatch_thermal_connectivity/hatch_thermal_connectivity",
709 m_board );
710
711 KI_TEST::FillZones( m_board.get() );
712
713 m_board->BuildConnectivity();
714
715 int unconnectedCount = m_board->GetConnectivity()->GetUnconnectedCount( false );
716
717 BOOST_CHECK_MESSAGE( unconnectedCount == 0,
718 wxString::Format( "Found %d unconnected items after zone fill. "
719 "Hatch zone thermal reliefs should maintain connectivity "
720 "even with large hatch gaps.",
721 unconnectedCount ) );
722}
723
724
741BOOST_FIXTURE_TEST_CASE( RegressionShallowArcZoneFill, ZONE_FILL_TEST_FIXTURE )
742{
743 KI_TEST::LoadBoard( m_settingsManager, "issue22475/issue22475", m_board );
744
745 PCB_LAYER_ID in1Cu = m_board->GetLayerID( wxT( "In1.Cu" ) );
746
747 ZONE* gndZone = nullptr;
748
749 for( ZONE* zone : m_board->Zones() )
750 {
751 if( zone->GetNetname() == "GND" && zone->IsOnLayer( in1Cu ) )
752 {
753 gndZone = zone;
754 break;
755 }
756 }
757
758 BOOST_REQUIRE_MESSAGE( gndZone != nullptr, "GND zone on In1.Cu not found in test board" );
759
760 if( !gndZone )
761 return;
762
763 KI_TEST::FillZones( m_board.get() );
764
765 BOOST_REQUIRE_MESSAGE( gndZone->HasFilledPolysForLayer( in1Cu ),
766 "GND zone has no fill on In1.Cu" );
767
768 const std::shared_ptr<SHAPE_POLY_SET>& fill = gndZone->GetFilledPolysList( in1Cu );
769
770 // The zone fill should produce a single contiguous outline. Multiple outlines
771 // indicate disconnected fill areas caused by malformed clearance holes.
772 BOOST_CHECK_EQUAL( fill->OutlineCount(), 1 );
773
774 double zoneOutlineArea = gndZone->Outline()->Area();
775
776 BOOST_REQUIRE_MESSAGE( zoneOutlineArea > 0.0, "Zone outline area must be positive" );
777
778 double fillArea = 0.0;
779
780 for( int i = 0; i < fill->OutlineCount(); i++ )
781 fillArea += std::abs( fill->Outline( i ).Area() );
782
783 double fillRatio = fillArea / zoneOutlineArea;
784
785 // The zone should be mostly filled. A low fill ratio indicates excessive voids
786 // from malformed clearance holes around shallow arcs.
787 BOOST_CHECK_GE( fillRatio, 0.90 );
788}
789
790
803BOOST_FIXTURE_TEST_CASE( RegressionIterativeRefillRespectsKeepouts, ZONE_FILL_TEST_FIXTURE )
804{
805 // Enable iterative refill
806 ADVANCED_CFG& cfg = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() );
807 bool originalIterativeRefill = cfg.m_ZoneFillIterativeRefill;
808 cfg.m_ZoneFillIterativeRefill = true;
809
810 struct ScopeGuard { bool& ref; bool orig; ~ScopeGuard() { ref = orig; } }
811 guard{ cfg.m_ZoneFillIterativeRefill, originalIterativeRefill };
812
813 KI_TEST::LoadBoard( m_settingsManager, "issue22809/issue22809", m_board );
814
815 KI_TEST::FillZones( m_board.get() );
816
817 // Find all zone keepouts
818 std::vector<ZONE*> keepouts;
819
820 for( ZONE* zone : m_board->Zones() )
821 {
822 if( zone->GetIsRuleArea() && zone->GetDoNotAllowZoneFills() )
823 keepouts.push_back( zone );
824 }
825
826 BOOST_REQUIRE_MESSAGE( !keepouts.empty(), "No zone keepouts found in test board" );
827
828 // For each keepout, check that no zone fill exists inside it
829 int violationCount = 0;
830
831 for( ZONE* keepout : keepouts )
832 {
833 for( PCB_LAYER_ID layer : keepout->GetLayerSet().Seq() )
834 {
835 SHAPE_POLY_SET keepoutOutline( *keepout->Outline() );
836 keepoutOutline.ClearArcs();
837
838 for( ZONE* zone : m_board->Zones() )
839 {
840 if( zone->GetIsRuleArea() )
841 continue;
842
843 if( !zone->IsOnLayer( layer ) )
844 continue;
845
846 if( !zone->HasFilledPolysForLayer( layer ) )
847 continue;
848
849 const std::shared_ptr<SHAPE_POLY_SET>& fill = zone->GetFilledPolysList( layer );
850
851 // Check if any fill intersects the keepout
852 SHAPE_POLY_SET intersection = *fill;
853 intersection.BooleanIntersection( keepoutOutline );
854
855 if( intersection.OutlineCount() > 0 )
856 {
857 double intersectionArea = 0;
858
859 for( int i = 0; i < intersection.OutlineCount(); i++ )
860 intersectionArea += std::abs( intersection.Outline( i ).Area() );
861
862 // Allow for small numerical errors (less than 1 square mm)
863 if( intersectionArea > 1e6 )
864 {
865 BOOST_TEST_MESSAGE( wxString::Format(
866 "Zone %s fill on layer %s overlaps keepout by %.2f sq mm",
867 zone->GetNetname(),
868 m_board->GetLayerName( layer ),
869 intersectionArea / 1e6 ) );
870 violationCount++;
871 }
872 }
873 }
874 }
875 }
876
877 BOOST_CHECK_MESSAGE( violationCount == 0,
878 wxString::Format( "Found %d zone fills overlapping keepout areas. "
879 "This indicates issue 22809 - iterative refiller "
880 "ignores zone keepouts.", violationCount ) );
881}
882
883
897BOOST_FIXTURE_TEST_CASE( RegressionTHPadInnerLayerFlashing, ZONE_FILL_TEST_FIXTURE )
898{
899 KI_TEST::LoadBoard( m_settingsManager, "issue22826/issue22826", m_board );
900
901 KI_TEST::FillZones( m_board.get() );
902
903 PCB_LAYER_ID in2Cu = m_board->GetLayerID( wxT( "In2.Cu" ) );
904 int padsWithMissingFlashing = 0;
905 int totalConditionalPads = 0;
906
907 for( FOOTPRINT* footprint : m_board->Footprints() )
908 {
909 for( PAD* pad : footprint->Pads() )
910 {
911 if( !pad->GetRemoveUnconnected() )
912 continue;
913
914 if( !pad->HasHole() )
915 continue;
916
917 if( pad->GetNetname() != "VBUS_DUT" && pad->GetNetname() != "VBUS_DBG" )
918 continue;
919
920 totalConditionalPads++;
921
922 // Check if the pad should flash on In2.Cu
923 bool shouldFlash = false;
924
925 for( ZONE* zone : m_board->Zones() )
926 {
927 if( zone->GetIsRuleArea() )
928 continue;
929
930 if( zone->GetNetCode() != pad->GetNetCode() )
931 continue;
932
933 if( !zone->IsOnLayer( in2Cu ) )
934 continue;
935
936 if( zone->Outline()->Contains( pad->GetPosition() ) )
937 {
938 shouldFlash = true;
939 break;
940 }
941 }
942
943 if( shouldFlash && !pad->FlashLayer( in2Cu ) )
944 {
945 BOOST_TEST_MESSAGE( wxString::Format(
946 "Pad %s at (%d, %d) on net %s is inside zone but not flashing on In2.Cu",
947 pad->GetNumber(), pad->GetPosition().x, pad->GetPosition().y,
948 pad->GetNetname() ) );
949 padsWithMissingFlashing++;
950 }
951 }
952 }
953
954 BOOST_TEST_MESSAGE( wxString::Format( "Total conditional pads: %d, Pads with missing "
955 "flashing: %d", totalConditionalPads,
956 padsWithMissingFlashing ) );
957
958 BOOST_CHECK_MESSAGE( padsWithMissingFlashing == 0,
959 wxString::Format( "Found %d TH pads that should flash on inner layers "
960 "but don't. This indicates issue 22826 is not fixed.",
961 padsWithMissingFlashing ) );
962}
963
964
973BOOST_FIXTURE_TEST_CASE( RegressionRoundRectTeardropGeometry, ZONE_FILL_TEST_FIXTURE )
974{
975 KI_TEST::LoadBoard( m_settingsManager, "issue19405_roundrect_teardrop", m_board );
976
977 // Set up tool manager for teardrop generation
978 TOOL_MANAGER toolMgr;
979 toolMgr.SetEnvironment( m_board.get(), nullptr, nullptr, nullptr, nullptr );
980
981 KI_TEST::DUMMY_TOOL* dummyTool = new KI_TEST::DUMMY_TOOL();
982 toolMgr.RegisterTool( dummyTool );
983
984 // Generate teardrops
985 BOARD_COMMIT commit( dummyTool );
986 TEARDROP_MANAGER teardropMgr( m_board.get(), &toolMgr );
987 teardropMgr.UpdateTeardrops( commit, nullptr, nullptr, true );
988
989 if( !commit.Empty() )
990 commit.Push( _( "Add teardrops" ), SKIP_UNDO | SKIP_SET_DIRTY );
991
992 // Find teardrop zones
993 int teardropCount = 0;
994 bool foundBadTeardrop = false;
995
996 for( ZONE* zone : m_board->Zones() )
997 {
998 if( !zone->IsTeardropArea() )
999 continue;
1000
1001 teardropCount++;
1002
1003 // Get the teardrop outline
1004 const SHAPE_POLY_SET* outline = zone->Outline();
1005
1006 if( !outline || outline->OutlineCount() == 0 )
1007 continue;
1008
1009 const SHAPE_LINE_CHAIN& chain = outline->Outline( 0 );
1010
1011 // Check that the teardrop polygon is convex or at least doesn't have
1012 // any sharp concave angles that would indicate intersection with the pad corner.
1013 // A well-formed teardrop should have all turns in the same direction
1014 // (or very close to it) except at the pad anchor points.
1015 int concaveCount = 0;
1016
1017 for( int i = 0; i < chain.PointCount(); i++ )
1018 {
1019 int prev = ( i == 0 ) ? chain.PointCount() - 1 : i - 1;
1020 int next = ( i + 1 ) % chain.PointCount();
1021
1022 VECTOR2I v1 = chain.CPoint( i ) - chain.CPoint( prev );
1023 VECTOR2I v2 = chain.CPoint( next ) - chain.CPoint( i );
1024
1025 // Cross product gives handedness of turn
1026 int64_t cross = (int64_t) v1.x * v2.y - (int64_t) v1.y * v2.x;
1027
1028 // Count significant concave turns (negative cross product for CCW polygons)
1029 // Small values are numerical noise
1030 if( cross < -1000 )
1031 concaveCount++;
1032 }
1033
1034 // A teardrop should have at most 2-3 concave points (at the pad anchor points)
1035 // Many concave points indicate the curve is intersecting the pad corner
1036 if( concaveCount > 5 )
1037 {
1038 BOOST_TEST_MESSAGE( wxString::Format( "Teardrop has %d concave vertices, "
1039 "indicating possible corner intersection",
1040 concaveCount ) );
1041 foundBadTeardrop = true;
1042 }
1043 }
1044
1045 BOOST_CHECK_MESSAGE( teardropCount > 0, "Expected at least one teardrop zone" );
1046
1047 BOOST_CHECK_MESSAGE( !foundBadTeardrop,
1048 "Found teardrop with excessive concave vertices, indicating "
1049 "issue 19405 - teardrop curve intersecting rounded rectangle corner" );
1050}
1051
1052
1061{
1062 KI_TEST::LoadBoard( m_settingsManager, "oval_teardrop", m_board );
1063
1064 // Set up tool manager for teardrop generation
1065 TOOL_MANAGER toolMgr;
1066 toolMgr.SetEnvironment( m_board.get(), nullptr, nullptr, nullptr, nullptr );
1067
1068 KI_TEST::DUMMY_TOOL* dummyTool = new KI_TEST::DUMMY_TOOL();
1069 toolMgr.RegisterTool( dummyTool );
1070
1071 // Generate teardrops
1072 BOARD_COMMIT commit( dummyTool );
1073 TEARDROP_MANAGER teardropMgr( m_board.get(), &toolMgr );
1074 teardropMgr.UpdateTeardrops( commit, nullptr, nullptr, true );
1075
1076 if( !commit.Empty() )
1077 commit.Push( _( "Add teardrops" ), SKIP_UNDO | SKIP_SET_DIRTY );
1078
1079 // Find teardrop zones
1080 int teardropCount = 0;
1081 bool foundBadTeardrop = false;
1082
1083 for( ZONE* zone : m_board->Zones() )
1084 {
1085 if( !zone->IsTeardropArea() )
1086 continue;
1087
1088 teardropCount++;
1089
1090 const SHAPE_POLY_SET* outline = zone->Outline();
1091
1092 if( !outline || outline->OutlineCount() == 0 )
1093 continue;
1094
1095 const SHAPE_LINE_CHAIN& chain = outline->Outline( 0 );
1096
1097 // Check for excessive concave vertices that would indicate the teardrop curve
1098 // is not tangent to the oval's semicircular end
1099 int concaveCount = 0;
1100
1101 for( int i = 0; i < chain.PointCount(); i++ )
1102 {
1103 int prev = ( i == 0 ) ? chain.PointCount() - 1 : i - 1;
1104 int next = ( i + 1 ) % chain.PointCount();
1105
1106 VECTOR2I v1 = chain.CPoint( i ) - chain.CPoint( prev );
1107 VECTOR2I v2 = chain.CPoint( next ) - chain.CPoint( i );
1108
1109 int64_t cross = (int64_t) v1.x * v2.y - (int64_t) v1.y * v2.x;
1110
1111 if( cross < -1000 )
1112 concaveCount++;
1113 }
1114
1115 if( concaveCount > 5 )
1116 {
1117 BOOST_TEST_MESSAGE( wxString::Format( "Oval teardrop has %d concave vertices",
1118 concaveCount ) );
1119 foundBadTeardrop = true;
1120 }
1121 }
1122
1123 BOOST_CHECK_MESSAGE( teardropCount > 0, "Expected at least one teardrop zone" );
1124
1125 BOOST_CHECK_MESSAGE( !foundBadTeardrop,
1126 "Found teardrop with excessive concave vertices on oval pad, "
1127 "indicating curve is not tangent to semicircular end" );
1128}
1129
1130
1139{
1140 KI_TEST::LoadBoard( m_settingsManager, "large_circle_teardrop", m_board );
1141
1142 // Set up tool manager for teardrop generation
1143 TOOL_MANAGER toolMgr;
1144 toolMgr.SetEnvironment( m_board.get(), nullptr, nullptr, nullptr, nullptr );
1145
1146 KI_TEST::DUMMY_TOOL* dummyTool = new KI_TEST::DUMMY_TOOL();
1147 toolMgr.RegisterTool( dummyTool );
1148
1149 // Generate teardrops
1150 BOARD_COMMIT commit( dummyTool );
1151 TEARDROP_MANAGER teardropMgr( m_board.get(), &toolMgr );
1152 teardropMgr.UpdateTeardrops( commit, nullptr, nullptr, true );
1153
1154 if( !commit.Empty() )
1155 commit.Push( _( "Add teardrops" ), SKIP_UNDO | SKIP_SET_DIRTY );
1156
1157 // Find the pad and its teardrop
1158 PAD* largePad = nullptr;
1159
1160 for( FOOTPRINT* fp : m_board->Footprints() )
1161 {
1162 for( PAD* pad : fp->Pads() )
1163 {
1164 if( pad->GetShape( F_Cu ) == PAD_SHAPE::CIRCLE )
1165 {
1166 largePad = pad;
1167 break;
1168 }
1169 }
1170 }
1171
1172 BOOST_REQUIRE_MESSAGE( largePad != nullptr, "Expected a circular pad in test board" );
1173
1174 int padRadius = largePad->GetSize( F_Cu ).x / 2;
1175 VECTOR2I padCenter = largePad->GetPosition();
1176
1177 // Find teardrop zones
1178 int teardropCount = 0;
1179 bool foundBadTeardrop = false;
1180
1181 for( ZONE* zone : m_board->Zones() )
1182 {
1183 if( !zone->IsTeardropArea() )
1184 continue;
1185
1186 teardropCount++;
1187
1188 const SHAPE_POLY_SET* outline = zone->Outline();
1189
1190 if( !outline || outline->OutlineCount() == 0 )
1191 continue;
1192
1193 const SHAPE_LINE_CHAIN& chain = outline->Outline( 0 );
1194
1195 // Check for excessive concave vertices
1196 int concaveCount = 0;
1197
1198 for( int i = 0; i < chain.PointCount(); i++ )
1199 {
1200 int prev = ( i == 0 ) ? chain.PointCount() - 1 : i - 1;
1201 int next = ( i + 1 ) % chain.PointCount();
1202
1203 VECTOR2I v1 = chain.CPoint( i ) - chain.CPoint( prev );
1204 VECTOR2I v2 = chain.CPoint( next ) - chain.CPoint( i );
1205
1206 int64_t cross = (int64_t) v1.x * v2.y - (int64_t) v1.y * v2.x;
1207
1208 if( cross < -1000 )
1209 concaveCount++;
1210 }
1211
1212 if( concaveCount > 5 )
1213 {
1214 BOOST_TEST_MESSAGE( wxString::Format( "Large circle teardrop has %d concave vertices",
1215 concaveCount ) );
1216 foundBadTeardrop = true;
1217 }
1218
1219 // Also verify that the teardrop anchor points near the pad are approximately
1220 // on the circle edge (within tolerance)
1221 int maxError = m_board->GetDesignSettings().m_MaxError;
1222
1223 for( int i = 0; i < chain.PointCount(); i++ )
1224 {
1225 VECTOR2I pt = chain.CPoint( i );
1226 double dist = ( pt - padCenter ).EuclideanNorm();
1227
1228 // Points that are close to the circle should be approximately on it
1229 if( dist > padRadius * 0.5 && dist < padRadius * 1.5 )
1230 {
1231 double deviation = std::abs( dist - padRadius );
1232
1233 // Allow some tolerance for polygon approximation
1234 if( deviation > maxError * 5 && deviation < padRadius * 0.2 )
1235 {
1236 BOOST_TEST_MESSAGE( wxString::Format(
1237 "Teardrop point at distance %.2f from pad center (radius %.2f), "
1238 "deviation %.2f exceeds tolerance",
1239 dist / 1000.0, padRadius / 1000.0, deviation / 1000.0 ) );
1240 }
1241 }
1242 }
1243 }
1244
1245 BOOST_CHECK_MESSAGE( teardropCount > 0, "Expected at least one teardrop zone" );
1246
1247 BOOST_CHECK_MESSAGE( !foundBadTeardrop,
1248 "Found teardrop with excessive concave vertices on large circle, "
1249 "indicating anchor points may not be on circle edge" );
1250}
1251
1252
1263BOOST_FIXTURE_TEST_CASE( RegressionCoincidentPadClearance, ZONE_FILL_TEST_FIXTURE )
1264{
1265 KI_TEST::LoadBoard( m_settingsManager, "issue23123_minimal", m_board );
1266
1267 KI_TEST::FillZones( m_board.get() );
1268
1269 // After filling, every pad whose net differs from the zone must have clearance.
1270 // Check each zone/pad combination on each shared layer.
1271 int violations = 0;
1272
1273 for( ZONE* zone : m_board->Zones() )
1274 {
1275 if( zone->GetIsRuleArea() )
1276 continue;
1277
1278 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
1279 {
1280 if( !zone->HasFilledPolysForLayer( layer ) )
1281 continue;
1282
1283 const std::shared_ptr<SHAPE_POLY_SET>& fill = zone->GetFilledPolysList( layer );
1284
1285 for( PAD* pad : m_board->GetPads() )
1286 {
1287 if( !pad->IsOnLayer( layer ) )
1288 continue;
1289
1290 if( pad->GetNetCode() == zone->GetNetCode() )
1291 continue;
1292
1293 std::shared_ptr<SHAPE> padShape = pad->GetEffectiveShape( layer );
1294 int clearance = padShape->GetClearance( fill.get() );
1295
1296 if( clearance < 1 )
1297 {
1298 BOOST_TEST_MESSAGE( wxString::Format(
1299 "Pad %s (net %s) at (%d, %d) has zero clearance to zone %s "
1300 "on layer %s",
1301 pad->GetNumber(), pad->GetNetname(),
1302 pad->GetPosition().x, pad->GetPosition().y,
1303 zone->GetNetname(), m_board->GetLayerName( layer ) ) );
1304 violations++;
1305 }
1306 }
1307 }
1308 }
1309
1310 BOOST_CHECK_MESSAGE( violations == 0,
1311 wxString::Format( "Found %d pads with missing zone clearance. "
1312 "Coincident pads with different nets must not be "
1313 "deduplicated in zone fill knockout.",
1314 violations ) );
1315}
1316
1317
1328{
1329 KI_TEST::LoadBoard( m_settingsManager, "connect/connect", m_board );
1330
1331 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
1332
1333 KI_TEST::FillZones( m_board.get() );
1334
1335 std::vector<DRC_ITEM> violations;
1336
1337 bds.m_DRCEngine->InitEngine( wxFileName() );
1338
1340 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
1341 const std::function<void( PCB_MARKER* )>& aPathGenerator )
1342 {
1343 if( aItem->GetErrorCode() == DRCE_CLEARANCE )
1344 {
1345 BOARD_ITEM* item_a = m_board->ResolveItem( aItem->GetMainItemID() );
1346 BOARD_ITEM* item_b = m_board->ResolveItem( aItem->GetAuxItemID() );
1347
1348 ZONE* zone_a = dynamic_cast<ZONE*>( item_a );
1349 ZONE* zone_b = dynamic_cast<ZONE*>( item_b );
1350
1351 if( zone_a || zone_b )
1352 violations.push_back( *aItem );
1353 }
1354 } );
1355
1356 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
1357
1358 BOOST_CHECK_EQUAL( violations.size(), 0 );
1359}
1360
1361
1373{
1374 KI_TEST::LoadBoard( m_settingsManager, "issue23339_zone_layer_rules", m_board );
1375
1376 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
1377
1378 // First verify that EvalRules returns the correct clearance per layer
1379 ZONE* hvZone = nullptr;
1380 ZONE* lvZone = nullptr;
1381
1382 for( ZONE* zone : m_board->Zones() )
1383 {
1384 if( zone->GetNetname() == "HV_NET" )
1385 hvZone = zone;
1386 else if( zone->GetNetname() == "LV_NET" )
1387 lvZone = zone;
1388 }
1389
1390 BOOST_REQUIRE( hvZone );
1391 BOOST_REQUIRE( lvZone );
1392
1393 // Outer layer rule should give 4.6mm clearance on F.Cu
1395 hvZone, lvZone, F_Cu );
1396
1397 BOOST_TEST_MESSAGE( "F.Cu clearance: " << outerConstraint.GetValue().Min()
1398 << " (expected " << pcbIUScale.mmToIU( 4.6 ) << ")" );
1399 BOOST_CHECK_EQUAL( outerConstraint.GetValue().Min(), pcbIUScale.mmToIU( 4.6 ) );
1400
1401 // Inner layer rule should give 2.3mm clearance on In1.Cu
1403 hvZone, lvZone, In1_Cu );
1404
1405 BOOST_TEST_MESSAGE( "In1.Cu clearance: " << innerConstraint.GetValue().Min()
1406 << " (expected " << pcbIUScale.mmToIU( 2.3 ) << ")" );
1407 BOOST_CHECK_EQUAL( innerConstraint.GetValue().Min(), pcbIUScale.mmToIU( 2.3 ) );
1408
1409 // Now fill zones and check that fills actually respect the clearances
1410 KI_TEST::FillZones( m_board.get() );
1411
1412 // Run DRC and verify no clearance violations between zones
1413 std::vector<DRC_ITEM> violations;
1414
1415 bds.m_DRCEngine->InitEngine( wxFileName() );
1416
1418 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
1419 const std::function<void( PCB_MARKER* )>& aPathGenerator )
1420 {
1421 if( aItem->GetErrorCode() == DRCE_CLEARANCE )
1422 {
1423 BOARD_ITEM* item_a = m_board->ResolveItem( aItem->GetMainItemID() );
1424 BOARD_ITEM* item_b = m_board->ResolveItem( aItem->GetAuxItemID() );
1425
1426 ZONE* zone_a = dynamic_cast<ZONE*>( item_a );
1427 ZONE* zone_b = dynamic_cast<ZONE*>( item_b );
1428
1429 if( zone_a && zone_b )
1430 {
1431 BOOST_TEST_MESSAGE( "Zone-to-zone clearance violation on layer "
1432 << aLayer << ": " << aItem->GetErrorMessage( true ) );
1433 violations.push_back( *aItem );
1434 }
1435 }
1436 } );
1437
1438 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
1439
1440 BOOST_CHECK_EQUAL( violations.size(), 0 );
1441}
1442
1443
1444BOOST_FIXTURE_TEST_CASE( RegressionZoneFillMinWidthAfterKnockout, ZONE_FILL_TEST_FIXTURE )
1445{
1446 KI_TEST::LoadBoard( m_settingsManager, "issue23332_min_width/issue23332_min_width", m_board );
1447
1448 KI_TEST::FillZones( m_board.get() );
1449
1450 int epsilon = pcbIUScale.mmToIU( 0.001 );
1451
1452 for( ZONE* zone : m_board->Zones() )
1453 {
1454 int half_min_width = zone->GetMinThickness() / 2;
1455
1456 if( half_min_width - epsilon <= epsilon )
1457 continue;
1458
1459 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
1460 {
1461 if( !zone->HasFilledPolysForLayer( layer ) )
1462 continue;
1463
1464 std::shared_ptr<SHAPE_POLY_SET> fill = zone->GetFilledPolysList( layer );
1465
1466 if( !fill || fill->OutlineCount() == 0 )
1467 continue;
1468
1469 // Check each filled island individually so that a tiny thin sliver
1470 // isn't masked by a large zone's total area
1471 for( int ii = 0; ii < fill->OutlineCount(); ii++ )
1472 {
1473 SHAPE_POLY_SET island;
1474 island.AddOutline( fill->Outline( ii ) );
1475
1476 for( int jj = 0; jj < fill->HoleCount( ii ); jj++ )
1477 island.AddHole( fill->Hole( ii, jj ) );
1478
1479 double originalArea = island.Area();
1480
1481 if( originalArea <= 0 )
1482 continue;
1483
1485
1486 test.Deflate( half_min_width - epsilon, CORNER_STRATEGY::CHAMFER_ALL_CORNERS,
1487 ARC_HIGH_DEF );
1488
1489 test.Inflate( half_min_width - epsilon, CORNER_STRATEGY::ROUND_ALL_CORNERS,
1490 ARC_HIGH_DEF, true );
1491
1492 double prunedArea = test.Area();
1493 double areaLoss = ( originalArea - prunedArea ) / originalArea;
1494
1495 BOOST_TEST_MESSAGE( wxString::Format(
1496 "Zone %s layer %d island %d: area=%.0f, loss=%.4f%%",
1497 zone->GetNetname(), static_cast<int>( layer ), ii,
1498 originalArea, areaLoss * 100.0 ) );
1499
1500 BOOST_CHECK_MESSAGE( areaLoss < 0.01,
1501 wxString::Format(
1502 "Zone %s layer %d island %d lost %.2f%% area from "
1503 "min-width pruning (min_width=%.3fmm)",
1504 zone->GetNetname(), static_cast<int>( layer ), ii,
1505 areaLoss * 100.0,
1506 zone->GetMinThickness()
1507 / static_cast<double>( pcbIUScale.IU_PER_MM ) ) );
1508 }
1509 }
1510 }
1511}
1512
1513
1514BOOST_FIXTURE_TEST_CASE( RegressionSameNetOverlappingZones, ZONE_FILL_TEST_FIXTURE )
1515{
1516 KI_TEST::LoadBoard( m_settingsManager, "issue23418/testing", m_board );
1517
1518 KI_TEST::FillZones( m_board.get() );
1519
1520 int epsilon = pcbIUScale.mmToIU( 0.001 );
1521
1522 for( ZONE* zone : m_board->Zones() )
1523 {
1524 int half_min_width = zone->GetMinThickness() / 2;
1525
1526 if( half_min_width - epsilon <= epsilon )
1527 continue;
1528
1529 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
1530 {
1531 if( !zone->HasFilledPolysForLayer( layer ) )
1532 continue;
1533
1534 std::shared_ptr<SHAPE_POLY_SET> fill = zone->GetFilledPolysList( layer );
1535
1536 if( !fill || fill->OutlineCount() == 0 )
1537 continue;
1538
1539 for( int ii = 0; ii < fill->OutlineCount(); ii++ )
1540 {
1541 SHAPE_POLY_SET island;
1542 island.AddOutline( fill->Outline( ii ) );
1543
1544 for( int jj = 0; jj < fill->HoleCount( ii ); jj++ )
1545 island.AddHole( fill->Hole( ii, jj ) );
1546
1547 double originalArea = island.Area();
1548
1549 if( originalArea <= 0 )
1550 continue;
1551
1553
1554 test.Deflate( half_min_width - epsilon, CORNER_STRATEGY::CHAMFER_ALL_CORNERS,
1555 ARC_HIGH_DEF );
1556
1557 test.Inflate( half_min_width - epsilon, CORNER_STRATEGY::ROUND_ALL_CORNERS,
1558 ARC_HIGH_DEF, true );
1559
1560 double prunedArea = test.Area();
1561 double areaLoss = ( originalArea - prunedArea ) / originalArea;
1562
1563 BOOST_CHECK_MESSAGE( areaLoss < 0.01,
1564 wxString::Format(
1565 "Zone %s (priority %d) layer %d island %d lost "
1566 "%.2f%% area from min-width pruning, suggesting "
1567 "degenerate geometry from overlapping same-net zones",
1568 zone->GetNetname(),
1569 zone->GetAssignedPriority(),
1570 static_cast<int>( layer ), ii,
1571 areaLoss * 100.0 ) );
1572 }
1573 }
1574 }
1575}
1576
1577
1578BOOST_FIXTURE_TEST_CASE( RegressionDiffNetOverlappingZones, ZONE_FILL_TEST_FIXTURE )
1579{
1580 ADVANCED_CFG& cfg = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() );
1581 bool originalIterativeRefill = cfg.m_ZoneFillIterativeRefill;
1582
1583 struct ScopeGuard { bool& ref; bool orig; ~ScopeGuard() { ref = orig; } }
1584 guard{ cfg.m_ZoneFillIterativeRefill, originalIterativeRefill };
1585
1586 auto runAreaLossCheck =
1587 [this]( bool aIterative )
1588 {
1589 ADVANCED_CFG& innerCfg = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() );
1590 innerCfg.m_ZoneFillIterativeRefill = aIterative;
1591
1592 KI_TEST::LoadBoard( m_settingsManager, "issue23418_diffnet/testing", m_board );
1593 KI_TEST::FillZones( m_board.get() );
1594
1595 int epsilon = pcbIUScale.mmToIU( 0.001 );
1596
1597 for( ZONE* zone : m_board->Zones() )
1598 {
1599 int half_min_width = zone->GetMinThickness() / 2;
1600
1601 if( half_min_width - epsilon <= epsilon )
1602 continue;
1603
1604 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
1605 {
1606 if( !zone->HasFilledPolysForLayer( layer ) )
1607 continue;
1608
1609 std::shared_ptr<SHAPE_POLY_SET> fill = zone->GetFilledPolysList( layer );
1610
1611 if( !fill || fill->OutlineCount() == 0 )
1612 continue;
1613
1614 for( int ii = 0; ii < fill->OutlineCount(); ii++ )
1615 {
1616 SHAPE_POLY_SET island;
1617 island.AddOutline( fill->Outline( ii ) );
1618
1619 for( int jj = 0; jj < fill->HoleCount( ii ); jj++ )
1620 island.AddHole( fill->Hole( ii, jj ) );
1621
1622 double originalArea = island.Area();
1623
1624 if( originalArea <= 0 )
1625 continue;
1626
1628
1629 test.Deflate( half_min_width - epsilon,
1631
1632 test.Inflate( half_min_width - epsilon,
1634
1635 double prunedArea = test.Area();
1636 double areaLoss = ( originalArea - prunedArea ) / originalArea;
1637
1639 areaLoss < 0.01,
1640 wxString::Format(
1641 "Zone %s (priority %d) layer %d island %d lost "
1642 "%.2f%% area (iterative=%d), suggesting degenerate "
1643 "geometry from different-net zone knockouts",
1644 zone->GetNetname(), zone->GetAssignedPriority(),
1645 static_cast<int>( layer ), ii, areaLoss * 100.0,
1646 aIterative ) );
1647 }
1648 }
1649 }
1650 };
1651
1652 runAreaLossCheck( false );
1653 runAreaLossCheck( true );
1654}
1655
1656
1672BOOST_FIXTURE_TEST_CASE( RegressionThermalReliefsToNowhere, ZONE_FILL_TEST_FIXTURE )
1673{
1674 ADVANCED_CFG& cfg = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() );
1675 bool originalIterativeRefill = cfg.m_ZoneFillIterativeRefill;
1676 cfg.m_ZoneFillIterativeRefill = true;
1677
1678 struct ScopeGuard { bool& ref; bool orig; ~ScopeGuard() { ref = orig; } }
1679 guard{ cfg.m_ZoneFillIterativeRefill, originalIterativeRefill };
1680
1681 KI_TEST::LoadBoard( m_settingsManager, "issue23535_minimal/issue23535_minimal", m_board );
1682
1683 KI_TEST::FillZones( m_board.get() );
1684
1685 ZONE* gndZone = nullptr;
1686
1687 for( ZONE* zone : m_board->Zones() )
1688 {
1689 if( zone->GetNetname() == "GND" )
1690 gndZone = zone;
1691 }
1692
1693 BOOST_REQUIRE( gndZone );
1695
1696 const std::shared_ptr<SHAPE_POLY_SET>& gndFill = gndZone->GetFilledPolysList( F_Cu );
1697
1698 // The pad is at (6.5mm, 5mm) with size 1.5mm and thermal gap 0.5mm.
1699 // The right edge of the pad is at x=7.25mm, thermal gap extends to x=7.75mm.
1700 // After zone knockout, GND fill stops at roughly x=7.5mm.
1701 //
1702 // A thermal-relief-to-nowhere spoke would create copper at a point inside the
1703 // thermal gap but past the zone fill boundary. Check a point at (7.4mm, 5mm)
1704 // which is in the thermal gap (x > 7.25) and near the knockout edge.
1705 VECTOR2I spokeTestPoint( pcbIUScale.mmToIU( 7.4 ), pcbIUScale.mmToIU( 5.0 ) );
1706
1707 bool hasSpokeToNowhere = gndFill->Contains( spokeTestPoint );
1708
1709 BOOST_CHECK_MESSAGE( !hasSpokeToNowhere,
1710 "GND zone fill contains copper at the thermal gap test point (7.4, 5.0), "
1711 "indicating a thermal relief spoke to nowhere (issue 23535)." );
1712
1713 // Also verify that the left-pointing spoke still connects properly.
1714 // A point at (5.6mm, 5mm) is in the thermal gap on the left side and should have
1715 // copper from a valid left-pointing spoke.
1716 VECTOR2I validSpokePoint( pcbIUScale.mmToIU( 5.6 ), pcbIUScale.mmToIU( 5.0 ) );
1717
1718 bool hasValidSpoke = gndFill->Contains( validSpokePoint );
1719
1720 BOOST_CHECK_MESSAGE( hasValidSpoke,
1721 "GND zone fill does not contain copper at the valid spoke test point "
1722 "(5.6, 5.0). The fix may have incorrectly removed valid spokes." );
1723}
1724
1725
1736{
1737 KI_TEST::LoadBoard( m_settingsManager, "off_center_teardrop", m_board );
1738
1739 TOOL_MANAGER toolMgr;
1740 toolMgr.SetEnvironment( m_board.get(), nullptr, nullptr, nullptr, nullptr );
1741
1742 KI_TEST::DUMMY_TOOL* dummyTool = new KI_TEST::DUMMY_TOOL();
1743 toolMgr.RegisterTool( dummyTool );
1744
1745 BOARD_COMMIT commit( dummyTool );
1746 TEARDROP_MANAGER teardropMgr( m_board.get(), &toolMgr );
1747 teardropMgr.UpdateTeardrops( commit, nullptr, nullptr, true );
1748
1749 if( !commit.Empty() )
1750 commit.Push( _( "Add teardrops" ), SKIP_UNDO | SKIP_SET_DIRTY );
1751
1752 // The test board has a 3mm circle pad at (100, 100) with a 0.25mm track connecting
1753 // at (100.75, 99) heading to (115, 99). The track enters the pad off-center: 1mm above
1754 // and 0.75mm right of center. The teardrop should be approximately symmetric about the
1755 // track's axis (the line from ~(100.75, 99) toward (115, 99), i.e., horizontal).
1756
1757 int teardropCount = 0;
1758
1759 for( ZONE* zone : m_board->Zones() )
1760 {
1761 if( !zone->IsTeardropArea() )
1762 continue;
1763
1764 teardropCount++;
1765
1766 const SHAPE_POLY_SET* outline = zone->Outline();
1767
1768 if( !outline || outline->OutlineCount() == 0 )
1769 continue;
1770
1771 const SHAPE_LINE_CHAIN& chain = outline->Outline( 0 );
1772
1773 // The track axis is approximately at Y=99mm (in board coordinates = 99 * 1e6 nm).
1774 // Measure the maximum extent above and below this axis across all teardrop vertices.
1775 int trackY = pcbIUScale.mmToIU( 99 );
1776 int maxAbove = 0;
1777 int maxBelow = 0;
1778
1779 for( int i = 0; i < chain.PointCount(); i++ )
1780 {
1781 int dy = chain.CPoint( i ).y - trackY;
1782
1783 if( dy < 0 )
1784 maxAbove = std::max( maxAbove, -dy );
1785 else
1786 maxBelow = std::max( maxBelow, dy );
1787 }
1788
1789 // Both sides should have some extent (the teardrop flares out on both sides)
1790 BOOST_CHECK_MESSAGE( maxAbove > 0 && maxBelow > 0,
1791 "Teardrop should extend on both sides of the track axis" );
1792
1793 if( maxAbove > 0 && maxBelow > 0 )
1794 {
1795 // The two sides should be approximately equal. Allow 30% asymmetry tolerance
1796 // to account for polygon approximation of the circular pad and convex hull rounding.
1797 double ratio = static_cast<double>( std::min( maxAbove, maxBelow ) )
1798 / static_cast<double>( std::max( maxAbove, maxBelow ) );
1799
1800 BOOST_CHECK_MESSAGE( ratio > 0.7,
1801 wxString::Format( "Teardrop asymmetry ratio %.2f is too low "
1802 "(above=%d, below=%d). Expected roughly "
1803 "symmetric about the track axis.",
1804 ratio, maxAbove, maxBelow ) );
1805 }
1806 }
1807
1808 BOOST_CHECK_MESSAGE( teardropCount > 0, "Expected at least one teardrop zone for off-center track" );
1809}
1810
1811
1820BOOST_FIXTURE_TEST_CASE( ElongatedPadTeardropContainment, ZONE_FILL_TEST_FIXTURE )
1821{
1822 KI_TEST::LoadBoard( m_settingsManager, "teardrop_elongated_pad", m_board );
1823
1824 TOOL_MANAGER toolMgr;
1825 toolMgr.SetEnvironment( m_board.get(), nullptr, nullptr, nullptr, nullptr );
1826
1827 KI_TEST::DUMMY_TOOL* dummyTool = new KI_TEST::DUMMY_TOOL();
1828 toolMgr.RegisterTool( dummyTool );
1829
1830 BOARD_COMMIT commit( dummyTool );
1831 TEARDROP_MANAGER teardropMgr( m_board.get(), &toolMgr );
1832 teardropMgr.UpdateTeardrops( commit, nullptr, nullptr, true );
1833
1834 if( !commit.Empty() )
1835 commit.Push( _( "Add teardrops" ), SKIP_UNDO | SKIP_SET_DIRTY );
1836
1837 // Find the pad to build an expanded outline for containment checking.
1838 // The pad is at board position (136.45, 100.819) with size (3.5, 0.3) rotated 270 deg,
1839 // giving board extents X: [136.3, 136.6], Y: [99.069, 102.569].
1840 PAD* testPad = nullptr;
1841
1842 for( FOOTPRINT* fp : m_board->Footprints() )
1843 {
1844 for( PAD* pad : fp->Pads() )
1845 {
1846 if( pad->GetNumber() == "7" )
1847 {
1848 testPad = pad;
1849 break;
1850 }
1851 }
1852 }
1853
1854 BOOST_REQUIRE_MESSAGE( testPad != nullptr, "Could not find pad 7 in test board" );
1855
1856 // Build the pad outline polygon with a small tolerance for the track half-width
1857 int tolerance = std::max( m_board->GetDesignSettings().m_MaxError,
1858 pcbIUScale.mmToIU( 0.001 ) );
1859 SHAPE_POLY_SET padPoly;
1860 testPad->TransformShapeToPolygon( padPoly, B_Cu, tolerance,
1861 m_board->GetDesignSettings().m_MaxError, ERROR_OUTSIDE );
1862
1863 int teardropCount = 0;
1864
1865 for( ZONE* zone : m_board->Zones() )
1866 {
1867 if( !zone->IsTeardropArea() )
1868 continue;
1869
1870 const SHAPE_POLY_SET* outline = zone->Outline();
1871
1872 BOOST_REQUIRE_MESSAGE( outline && outline->OutlineCount() > 0,
1873 "Teardrop zone has no outline" );
1874
1875 teardropCount++;
1876
1877 const SHAPE_LINE_CHAIN& chain = outline->Outline( 0 );
1878
1879 // Check each vertex of the teardrop. Vertices on the pad side (closer to pad center
1880 // than to the track anchor) must be inside the expanded pad outline.
1881 VECTOR2I padCenter = testPad->GetPosition();
1882
1883 // The track anchor region is near (136.45, 99.16) in mm, i.e., outside the pad.
1884 // We only check vertices that are closer to the pad center than to the track anchor.
1885 VECTOR2I trackAnchor( pcbIUScale.mmToIU( 136.45 ), pcbIUScale.mmToIU( 99.16 ) );
1886
1887 for( int i = 0; i < chain.PointCount(); i++ )
1888 {
1889 VECTOR2I pt = chain.CPoint( i );
1890 double distToPad = ( VECTOR2D( pt ) - VECTOR2D( padCenter ) ).EuclideanNorm();
1891 double distToTrack = ( VECTOR2D( pt ) - VECTOR2D( trackAnchor ) ).EuclideanNorm();
1892
1893 // Only check vertices on the pad side of the teardrop
1894 if( distToPad < distToTrack )
1895 {
1897 padPoly.Contains( pt ),
1898 wxString::Format( "Teardrop vertex (%d, %d) is outside the pad "
1899 "outline with %d nm tolerance",
1900 pt.x, pt.y, tolerance ) );
1901 }
1902 }
1903 }
1904
1905 BOOST_CHECK_MESSAGE( teardropCount > 0,
1906 "Expected at least one teardrop zone for elongated pad" );
1907}
1908
1909
1918BOOST_FIXTURE_TEST_CASE( TwoSegmentAngledTeardropNoSelfIntersection, ZONE_FILL_TEST_FIXTURE )
1919{
1920 auto runVariant = [&]( bool aCurvedEdges )
1921 {
1922 KI_TEST::LoadBoard( m_settingsManager, "two_segment_teardrop", m_board );
1923
1924 for( PCB_TRACK* track : m_board->Tracks() )
1925 {
1926 if( track->Type() == PCB_VIA_T )
1927 {
1928 static_cast<PCB_VIA*>( track )->SetTeardropCurved( aCurvedEdges );
1929 break;
1930 }
1931 }
1932
1933 TOOL_MANAGER toolMgr;
1934 toolMgr.SetEnvironment( m_board.get(), nullptr, nullptr, nullptr, nullptr );
1935
1936 KI_TEST::DUMMY_TOOL* dummyTool = new KI_TEST::DUMMY_TOOL();
1937 toolMgr.RegisterTool( dummyTool );
1938
1939 BOARD_COMMIT commit( dummyTool );
1940 TEARDROP_MANAGER teardropMgr( m_board.get(), &toolMgr );
1941 teardropMgr.UpdateTeardrops( commit, nullptr, nullptr, true );
1942
1943 if( !commit.Empty() )
1944 commit.Push( _( "Add teardrops" ), SKIP_UNDO | SKIP_SET_DIRTY );
1945
1946 int teardropCount = 0;
1947 bool foundSelfIntersection = false;
1948
1949 for( ZONE* zone : m_board->Zones() )
1950 {
1951 if( !zone->IsTeardropArea() )
1952 continue;
1953
1954 teardropCount++;
1955
1956 const SHAPE_POLY_SET* outline = zone->Outline();
1957
1958 if( !outline || outline->OutlineCount() == 0 )
1959 continue;
1960
1961 const SHAPE_LINE_CHAIN& chain = outline->Outline( 0 );
1962 int n = chain.PointCount();
1963
1964 for( int i = 0; i < n && !foundSelfIntersection; i++ )
1965 {
1966 SEG segA( chain.CPoint( i ), chain.CPoint( ( i + 1 ) % n ) );
1967
1968 for( int j = i + 2; j < n; j++ )
1969 {
1970 if( i == 0 && j == n - 1 )
1971 continue;
1972
1973 SEG segB( chain.CPoint( j ), chain.CPoint( ( j + 1 ) % n ) );
1974 OPT_VECTOR2I hit = segA.Intersect( segB );
1975
1976 if( hit.has_value() )
1977 {
1978 BOOST_TEST_MESSAGE( wxString::Format(
1979 "Self-intersection at (%d, %d) between edges %d and %d "
1980 "(curved=%s)",
1981 hit->x, hit->y, i, j,
1982 aCurvedEdges ? "yes" : "no" ) );
1983
1984 for( int k = 0; k < n; k++ )
1985 {
1986 BOOST_TEST_MESSAGE( wxString::Format(
1987 " pt[%d] = (%d, %d)", k,
1988 chain.CPoint( k ).x, chain.CPoint( k ).y ) );
1989 }
1990
1991 foundSelfIntersection = true;
1992 break;
1993 }
1994 }
1995 }
1996 }
1997
1998 BOOST_CHECK_MESSAGE( teardropCount > 0,
1999 wxString::Format( "Expected at least one teardrop zone "
2000 "(curved=%s)",
2001 aCurvedEdges ? "yes" : "no" ) );
2002
2003 BOOST_CHECK_MESSAGE( !foundSelfIntersection,
2004 wxString::Format( "Teardrop polygon has self-intersecting "
2005 "edges (curved=%s)",
2006 aCurvedEdges ? "yes" : "no" ) );
2007 };
2008
2009 runVariant( true );
2010 runVariant( false );
2011}
2012
2013
2032BOOST_FIXTURE_TEST_CASE( OffCenterTwoSegmentTeardropNoSpike, ZONE_FILL_TEST_FIXTURE )
2033{
2034 auto runVariant = [&]( bool aCurvedEdges )
2035 {
2036 KI_TEST::LoadBoard( m_settingsManager, "teardrop_offcenter_two_segment", m_board );
2037
2038 VECTOR2I viaPos;
2039 int viaRadius = 0;
2040
2041 for( PCB_TRACK* track : m_board->Tracks() )
2042 {
2043 if( track->Type() == PCB_VIA_T )
2044 {
2045 PCB_VIA* via = static_cast<PCB_VIA*>( track );
2046 via->SetTeardropCurved( aCurvedEdges );
2047 viaPos = via->GetPosition();
2048 viaRadius = via->GetWidth( PADSTACK::ALL_LAYERS ) / 2;
2049 break;
2050 }
2051 }
2052
2053 BOOST_REQUIRE( viaRadius > 0 );
2054
2055 TOOL_MANAGER toolMgr;
2056 toolMgr.SetEnvironment( m_board.get(), nullptr, nullptr, nullptr, nullptr );
2057
2058 KI_TEST::DUMMY_TOOL* dummyTool = new KI_TEST::DUMMY_TOOL();
2059 toolMgr.RegisterTool( dummyTool );
2060
2061 BOARD_COMMIT commit( dummyTool );
2062 TEARDROP_MANAGER teardropMgr( m_board.get(), &toolMgr );
2063 teardropMgr.UpdateTeardrops( commit, nullptr, nullptr, true );
2064
2065 if( !commit.Empty() )
2066 commit.Push( _( "Add teardrops" ), SKIP_UNDO | SKIP_SET_DIRTY );
2067
2068 // The crafted board's first segment emerges from the via by ~10 um on a 100 um
2069 // track width; the emerging-length filter rejects it and no teardrop is built.
2070 const double maxBackSideDist = viaRadius * 1.2;
2071 int teardropCount = 0;
2072 int spikingPoints = 0;
2073 VECTOR2I worstPoint;
2074 double worstDistance = 0.0;
2075
2076 for( ZONE* zone : m_board->Zones() )
2077 {
2078 if( !zone->IsTeardropArea() )
2079 continue;
2080
2081 teardropCount++;
2082
2083 const SHAPE_POLY_SET* outline = zone->Outline();
2084
2085 if( !outline || outline->OutlineCount() == 0 )
2086 continue;
2087
2088 const SHAPE_LINE_CHAIN& chain = outline->Outline( 0 );
2089
2090 for( int i = 0; i < chain.PointCount(); i++ )
2091 {
2092 const VECTOR2I& pt = chain.CPoint( i );
2093 VECTOR2I rel = pt - viaPos;
2094
2095 // Only consider points on the back side (opposite the track entry).
2096 if( rel.x >= 0 )
2097 continue;
2098
2099 double dist = rel.EuclideanNorm();
2100
2101 if( dist > maxBackSideDist )
2102 {
2103 spikingPoints++;
2104
2105 if( dist > worstDistance )
2106 {
2107 worstDistance = dist;
2108 worstPoint = pt;
2109 }
2110 }
2111 }
2112 }
2113
2114 BOOST_CHECK_MESSAGE( teardropCount == 0,
2115 wxString::Format( "Expected no teardrop on grazing-entry "
2116 "track (emergence below track width), got "
2117 "%d (curved=%s)",
2118 teardropCount,
2119 aCurvedEdges ? "yes" : "no" ) );
2120
2121 BOOST_CHECK_MESSAGE( spikingPoints == 0,
2122 wxString::Format( "Found %d teardrop polygon vertex/vertices "
2123 "outside the expected envelope (worst at "
2124 "(%d, %d), %f mm from via center; curved=%s)",
2125 spikingPoints,
2126 worstPoint.x, worstPoint.y,
2127 worstDistance / pcbIUScale.IU_PER_MM,
2128 aCurvedEdges ? "yes" : "no" ) );
2129 };
2130
2131 runVariant( true );
2132 runVariant( false );
2133}
2134
2135
2147BOOST_FIXTURE_TEST_CASE( MultiTrackSharedInsideJunctionNoSelfIntersection,
2149{
2150 auto runVariant = [&]( bool aCurvedEdges )
2151 {
2152 KI_TEST::LoadBoard( m_settingsManager, "teardrop_multi_inside_via", m_board );
2153
2154 for( PCB_TRACK* track : m_board->Tracks() )
2155 {
2156 if( track->Type() == PCB_VIA_T )
2157 {
2158 static_cast<PCB_VIA*>( track )->SetTeardropCurved( aCurvedEdges );
2159 break;
2160 }
2161 }
2162
2163 TOOL_MANAGER toolMgr;
2164 toolMgr.SetEnvironment( m_board.get(), nullptr, nullptr, nullptr, nullptr );
2165
2166 KI_TEST::DUMMY_TOOL* dummyTool = new KI_TEST::DUMMY_TOOL();
2167 toolMgr.RegisterTool( dummyTool );
2168
2169 BOARD_COMMIT commit( dummyTool );
2170 TEARDROP_MANAGER teardropMgr( m_board.get(), &toolMgr );
2171 teardropMgr.UpdateTeardrops( commit, nullptr, nullptr, true );
2172
2173 if( !commit.Empty() )
2174 commit.Push( _( "Add teardrops" ), SKIP_UNDO | SKIP_SET_DIRTY );
2175
2176 int teardropCount = 0;
2177 int selfIntersectingCount = 0;
2178 VECTOR2I worstPoint;
2179
2180 for( ZONE* zone : m_board->Zones() )
2181 {
2182 if( !zone->IsTeardropArea() )
2183 continue;
2184
2185 teardropCount++;
2186
2187 const SHAPE_POLY_SET* outline = zone->Outline();
2188
2189 if( !outline || outline->OutlineCount() == 0 )
2190 continue;
2191
2192 const SHAPE_LINE_CHAIN& chain = outline->Outline( 0 );
2193 int n = chain.PointCount();
2194 bool intersected = false;
2195
2196 for( int i = 0; i < n && !intersected; i++ )
2197 {
2198 SEG segA( chain.CPoint( i ), chain.CPoint( ( i + 1 ) % n ) );
2199
2200 for( int j = i + 2; j < n; j++ )
2201 {
2202 if( i == 0 && j == n - 1 )
2203 continue;
2204
2205 SEG segB( chain.CPoint( j ), chain.CPoint( ( j + 1 ) % n ) );
2206 OPT_VECTOR2I hit = segA.Intersect( segB );
2207
2208 if( hit.has_value() )
2209 {
2210 BOOST_TEST_MESSAGE( wxString::Format(
2211 "Teardrop polygon self-intersection at (%d, %d) "
2212 "between edges %d and %d (curved=%s)",
2213 hit->x, hit->y, i, j, aCurvedEdges ? "yes" : "no" ) );
2214
2215 worstPoint = hit.value();
2216 intersected = true;
2217 break;
2218 }
2219 }
2220 }
2221
2222 if( intersected )
2223 selfIntersectingCount++;
2224 }
2225
2226 BOOST_CHECK_MESSAGE( teardropCount > 0,
2227 wxString::Format( "Expected at least one teardrop zone "
2228 "(curved=%s)",
2229 aCurvedEdges ? "yes" : "no" ) );
2230
2231 BOOST_CHECK_MESSAGE( selfIntersectingCount == 0,
2232 wxString::Format( "%d of %d teardrop polygon(s) self-intersect "
2233 "(worst at (%d, %d); curved=%s)",
2234 selfIntersectingCount, teardropCount,
2235 worstPoint.x, worstPoint.y,
2236 aCurvedEdges ? "yes" : "no" ) );
2237 };
2238
2239 runVariant( true );
2240 runVariant( false );
2241}
2242
2243
2251BOOST_FIXTURE_TEST_CASE( RegressionKeepoutBoundaryMissingFill, ZONE_FILL_TEST_FIXTURE )
2252{
2253 ADVANCED_CFG& cfg = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() );
2254 bool originalIterativeRefill = cfg.m_ZoneFillIterativeRefill;
2255
2256 struct ScopeGuard { bool& ref; bool orig; ~ScopeGuard() { ref = orig; } }
2257 guard{ cfg.m_ZoneFillIterativeRefill, originalIterativeRefill };
2258
2259 auto getTotalFilledArea =
2260 [this]() -> double
2261 {
2262 double totalArea = 0;
2263
2264 for( ZONE* zone : m_board->Zones() )
2265 {
2266 if( zone->GetIsRuleArea() )
2267 continue;
2268
2269 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
2270 {
2271 if( !zone->HasFilledPolysForLayer( layer ) )
2272 continue;
2273
2274 std::shared_ptr<SHAPE_POLY_SET> fill = zone->GetFilledPolysList( layer );
2275
2276 if( fill )
2277 totalArea += std::abs( fill->Area() );
2278 }
2279 }
2280
2281 return totalArea;
2282 };
2283
2284 auto refillAndMeasure =
2285 [this, &cfg, &getTotalFilledArea]( bool aIterative ) -> double
2286 {
2287 cfg.m_ZoneFillIterativeRefill = aIterative;
2288
2289 KI_TEST::LoadBoard( m_settingsManager, "issue23515/issue23515", m_board );
2290
2291 double storedArea = getTotalFilledArea();
2292
2293 BOOST_REQUIRE_MESSAGE( storedArea > 0, "Stored v9 fill has zero area" );
2294
2295 KI_TEST::FillZones( m_board.get() );
2296 return getTotalFilledArea();
2297 };
2298
2299 KI_TEST::LoadBoard( m_settingsManager, "issue23515/issue23515", m_board );
2300
2301 double storedArea = getTotalFilledArea();
2302
2303 BOOST_REQUIRE_MESSAGE( storedArea > 0, "Stored v9 fill has zero area" );
2304
2305 double nonIterativeArea = refillAndMeasure( false );
2306 double iterativeArea = refillAndMeasure( true );
2307 double nonIterativeAreaRatio = nonIterativeArea / storedArea;
2308 double iterativeAreaRatio = iterativeArea / storedArea;
2309
2311 nonIterativeAreaRatio > 0.99999,
2312 wxString::Format(
2313 "Non-iterative refill lost %.4f%% versus stored v9 fill "
2314 "(stored=%.2f mm^2, non-iterative=%.2f mm^2). "
2315 "This suggests missing pieces near keepout boundaries (issue 23515).",
2316 ( 1.0 - nonIterativeAreaRatio ) * 100.0,
2317 storedArea / 1e6, nonIterativeArea / 1e6 ) );
2318
2320 iterativeAreaRatio > 0.99999,
2321 wxString::Format(
2322 "Iterative refill lost %.4f%% versus stored v9 fill "
2323 "(stored=%.2f mm^2, iterative=%.2f mm^2). "
2324 "This suggests missing pieces near keepout boundaries (issue 23515).",
2325 ( 1.0 - iterativeAreaRatio ) * 100.0,
2326 storedArea / 1e6, iterativeArea / 1e6 ) );
2327}
2328
2329
2338BOOST_FIXTURE_TEST_CASE( HatchZoneViaConnectionRespectsSetting, ZONE_FILL_TEST_FIXTURE )
2339{
2340 m_board = std::make_unique<BOARD>();
2341
2342 // Two-layer board is sufficient for this test
2343 m_board->SetCopperLayerCount( 2 );
2344
2345 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
2346 bds.SetCopperLayerCount( 2 );
2347
2348 bds.m_MinClearance = pcbIUScale.mmToIU( 0.2 );
2349
2350 // Add a GND net
2351 NETINFO_ITEM* gndNet = new NETINFO_ITEM( m_board.get(), wxT( "GND" ) );
2352 m_board->Add( gndNet );
2353 int gndNetCode = gndNet->GetNetCode();
2354
2355 // Via dimensions: 2.0mm diameter, 1.0mm drill - large enough to span multiple hatch cells
2356 // so the via always touches webbing lines regardless of position within the hatch grid.
2357 int viaDiam = pcbIUScale.mmToIU( 2.0 );
2358 int viaDrill = pcbIUScale.mmToIU( 1.0 );
2359
2360 // Hatch zone parameters: 0.5mm gap, 0.3mm thickness. The via (radius=1.0mm) is wider
2361 // than the gap, so it will always intersect webbing in FULL mode. The thermal gap
2362 // (0.5mm) makes the knockout circle radius = 1.0+0.5 = 1.5mm.
2363 int hatchGap = pcbIUScale.mmToIU( 0.5 );
2364 int hatchThickness = pcbIUScale.mmToIU( 0.3 );
2365
2366 // Via center at 10mm,10mm (middle of the zone)
2367 VECTOR2I viaPos( pcbIUScale.mmToIU( 10 ), pcbIUScale.mmToIU( 10 ) );
2368
2369 auto makeVia =
2370 [&]() -> PCB_VIA*
2371 {
2372 PCB_VIA* via = new PCB_VIA( m_board.get() );
2373 via->SetPosition( viaPos );
2374 via->SetLayerPair( F_Cu, B_Cu );
2375 via->SetDrill( viaDrill );
2376 via->SetWidth( PADSTACK::ALL_LAYERS, viaDiam );
2377 via->SetNetCode( gndNetCode );
2378 m_board->Add( via );
2379 return via;
2380 };
2381
2382 auto makeHatchZone =
2383 [&]( ZONE_CONNECTION aConnection ) -> ZONE*
2384 {
2385 ZONE* zone = new ZONE( m_board.get() );
2386 zone->SetLayer( F_Cu );
2387 zone->SetNetCode( gndNetCode );
2389 zone->SetHatchGap( hatchGap );
2390 zone->SetHatchThickness( hatchThickness );
2391 zone->SetPadConnection( aConnection );
2392 zone->SetMinThickness( pcbIUScale.mmToIU( 0.2 ) );
2393 zone->SetThermalReliefGap( pcbIUScale.mmToIU( 0.5 ) );
2394 zone->SetThermalReliefSpokeWidth( pcbIUScale.mmToIU( 0.5 ) );
2395
2396 SHAPE_POLY_SET outline;
2397 outline.NewOutline();
2398 outline.Append( VECTOR2I( pcbIUScale.mmToIU( 1 ), pcbIUScale.mmToIU( 1 ) ) );
2399 outline.Append( VECTOR2I( pcbIUScale.mmToIU( 19 ), pcbIUScale.mmToIU( 1 ) ) );
2400 outline.Append( VECTOR2I( pcbIUScale.mmToIU( 19 ), pcbIUScale.mmToIU( 19 ) ) );
2401 outline.Append( VECTOR2I( pcbIUScale.mmToIU( 1 ), pcbIUScale.mmToIU( 19 ) ) );
2402 zone->AddPolygon( outline.COutline( 0 ) );
2403
2404 m_board->Add( zone );
2405 return zone;
2406 };
2407
2408 auto initDRC =
2409 [&]()
2410 {
2411 m_board->BuildConnectivity();
2412 auto drcEngine = std::make_shared<DRC_ENGINE>( m_board.get(), &bds );
2413 drcEngine->InitEngine( wxFileName() );
2414 bds.m_DRCEngine = drcEngine;
2415 };
2416
2417 // The thermal relief adds a circular ring around the via that covers hatch holes which
2418 // would otherwise be open. With viaRadius=1.0mm, thermalGap=0.5mm, spokeWidth=0.5mm:
2419 // ring outer radius = 1.75mm, inner radius = 1.25mm
2420 // ring area added inside hatch holes > knockout area removed from webbing
2421 // net result: THERMAL fill area > FULL fill area by ~0.4 sq mm
2422 // FULL connection skips both the knockout and the ring addition, so the THERMAL fill
2423 // should be measurably larger than the FULL fill.
2424
2425 double fullFillArea = 0.0;
2426 double thermalFillArea = 0.0;
2427
2428 // Test 1: FULL connection
2429 {
2430 PCB_VIA* via = makeVia();
2431 ZONE* zone = makeHatchZone( ZONE_CONNECTION::FULL );
2432
2433 initDRC();
2434 KI_TEST::FillZones( m_board.get() );
2435
2436 BOOST_REQUIRE_MESSAGE( zone->HasFilledPolysForLayer( F_Cu ),
2437 "Zone should have fill on F.Cu with FULL connection" );
2438
2439 const std::shared_ptr<SHAPE_POLY_SET>& fill = zone->GetFilledPolysList( F_Cu );
2440
2441 for( int i = 0; i < fill->OutlineCount(); i++ )
2442 fullFillArea += std::abs( fill->Outline( i ).Area() );
2443
2444 m_board->Remove( via );
2445 m_board->Remove( zone );
2446 delete via;
2447 delete zone;
2448 }
2449
2450 // Test 2: THERMAL connection
2451 {
2452 PCB_VIA* via = makeVia();
2453 ZONE* zone = makeHatchZone( ZONE_CONNECTION::THERMAL );
2454
2455 initDRC();
2456 KI_TEST::FillZones( m_board.get() );
2457
2458 BOOST_REQUIRE_MESSAGE( zone->HasFilledPolysForLayer( F_Cu ),
2459 "Zone should have fill on F.Cu with THERMAL connection" );
2460
2461 const std::shared_ptr<SHAPE_POLY_SET>& fill = zone->GetFilledPolysList( F_Cu );
2462
2463 for( int i = 0; i < fill->OutlineCount(); i++ )
2464 thermalFillArea += std::abs( fill->Outline( i ).Area() );
2465
2466 m_board->Remove( via );
2467 m_board->Remove( zone );
2468 delete via;
2469 delete zone;
2470 }
2471
2472 // The THERMAL fill should have more area than the FULL fill because a thermal ring was
2473 // added around the via, filling hatch holes that would otherwise be open.
2474 // Use a 0.2 sq mm threshold to avoid sensitivity to small edge effects.
2475 double iuPerMM = pcbIUScale.IU_PER_MM;
2476 double areaThreshold = 0.2 * iuPerMM * iuPerMM; // 0.2 sq mm in IU^2
2477
2478 double areaIU2toMM2 = 1.0 / ( iuPerMM * iuPerMM );
2479
2480 BOOST_CHECK_MESSAGE( thermalFillArea > fullFillArea + areaThreshold,
2481 wxString::Format(
2482 "THERMAL connection fill area (%.2f sq mm) should be larger "
2483 "than FULL fill area (%.2f sq mm) by at least 0.2 sq mm. "
2484 "If they are equal or FULL is larger, thermal ring was not "
2485 "added for THERMAL connection, or thermal ring was incorrectly "
2486 "added for FULL connection (issue 23516 regression).",
2487 thermalFillArea * areaIU2toMM2, fullFillArea * areaIU2toMM2 ) );
2488}
2489
2490
2508BOOST_FIXTURE_TEST_CASE( RegressionCascadingIslandRefill, ZONE_FILL_TEST_FIXTURE )
2509{
2510 ADVANCED_CFG& cfg = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() );
2511 bool originalIterativeRefill = cfg.m_ZoneFillIterativeRefill;
2512 cfg.m_ZoneFillIterativeRefill = true;
2513
2514 struct ScopeGuard
2515 {
2516 bool& ref;
2517 bool orig;
2518 ~ScopeGuard() { ref = orig; }
2519 } guard{ cfg.m_ZoneFillIterativeRefill, originalIterativeRefill };
2520
2521 KI_TEST::LoadBoard( m_settingsManager, "zone_refill_cascading_islands", m_board );
2522 KI_TEST::FillZones( m_board.get() );
2523
2524 const std::vector<std::string> checkedNames = { "hi1", "hi2", "hi3", "hi4", "hi5", "hi6", "hi7",
2525 "lo1", "lo2", "lo3", "lo4", "lo5", "lo6" };
2526 std::map<std::string, ZONE*> zoneByName;
2527
2528 for( ZONE* zone : m_board->Zones() )
2529 zoneByName[zone->GetZoneName().ToStdString()] = zone;
2530
2531 for( const std::string& name : checkedNames )
2532 {
2533 BOOST_REQUIRE_MESSAGE( zoneByName.count( name ), "Zone '" + name + "' not found in test board" );
2534 BOOST_REQUIRE_MESSAGE( zoneByName[name]->HasFilledPolysForLayer( F_Cu ),
2535 "Zone '" + name + "' has no fill on F.Cu" );
2536 }
2537
2538 // hi3, hi5, hi7 each split into two copper islands (one standalone, one merged with lo2/lo4/lo6).
2539 for( const std::string& name : { "hi3", "hi5", "hi7" } )
2540 {
2541 int islands = zoneByName[name]->GetFilledPolysList( F_Cu )->OutlineCount();
2542
2543 BOOST_CHECK_MESSAGE( islands == 2, wxString::Format( "Zone '%s' should have 2 filled islands but has %d. "
2544 "Cascading island removal did not converge correctly.",
2545 name, islands ) );
2546 }
2547
2548 // All lo zones and hi2/hi4/hi6 are single zones.
2549 for( const std::string& name : { "lo1", "lo2", "lo3", "lo4", "lo5", "lo6", "hi2", "hi4", "hi6" } )
2550 {
2551 int islands = zoneByName[name]->GetFilledPolysList( F_Cu )->OutlineCount();
2552
2553 BOOST_CHECK_MESSAGE( islands == 1, wxString::Format( "Zone '%s' should have 1 filled island but has %d. "
2554 "Iterative refill may have incorrectly blocked or "
2555 "expanded this zone.",
2556 name, islands ) );
2557 }
2558}
2559
2560
2567BOOST_FIXTURE_TEST_CASE( CopperThievingZone_HatchSurvivesTrackBisection, ZONE_FILL_TEST_FIXTURE )
2568{
2569 KI_TEST::LoadBoard( m_settingsManager, "zone_thieving_track_bisection", m_board );
2570 KI_TEST::FillZones( m_board.get() );
2571
2572 ZONE* thievingZone = nullptr;
2573
2574 for( ZONE* z : m_board->Zones() )
2575 {
2576 if( z->GetFillMode() == ZONE_FILL_MODE::COPPER_THIEVING )
2577 {
2578 thievingZone = z;
2579 break;
2580 }
2581 }
2582
2583 BOOST_REQUIRE( thievingZone );
2584
2585 const std::shared_ptr<SHAPE_POLY_SET>& fill = thievingZone->GetFilledPolysList( F_Cu );
2586 BOOST_REQUIRE( fill );
2587
2588 // The track splits the fill area in two; before the fix the connectivity
2589 // pass classified the narrow side as an isolated island and deleted it.
2590 // Expect at least two outlines covering both halves of the original zone.
2591 BOOST_CHECK_GE( fill->OutlineCount(), 2 );
2592
2593 // The fill must span the full zone width (left edge through right edge).
2594 BOX2I fillBox = fill->BBox();
2595 BOX2I zoneBox = thievingZone->Outline()->BBox();
2596
2597 BOOST_CHECK_LT( fillBox.GetLeft(), zoneBox.GetLeft() + pcbIUScale.mmToIU( 2.0 ) );
2598 BOOST_CHECK_GT( fillBox.GetRight(), zoneBox.GetRight() - pcbIUScale.mmToIU( 2.0 ) );
2599
2600 // The mesh must have real structure on both sides.
2601 BOOST_CHECK_GT( fill->TotalVertices(), 200 );
2602}
2603
2604
2617BOOST_FIXTURE_TEST_CASE( IterativeRefillConvergenceLimit, ZONE_FILL_TEST_FIXTURE )
2618{
2619 ADVANCED_CFG& cfg = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() );
2620 bool originalIterativeRefill = cfg.m_ZoneFillIterativeRefill;
2621 cfg.m_ZoneFillIterativeRefill = true;
2622
2623 struct ScopeGuard
2624 {
2625 bool& ref;
2626 bool orig;
2627 ~ScopeGuard() { ref = orig; }
2628 } guard{ cfg.m_ZoneFillIterativeRefill, originalIterativeRefill };
2629
2630 // Capture wxLogWarning calls so we can assert that the iteration cap fires.
2631 class WarningCapture : public wxLog
2632 {
2633 public:
2634 bool m_hadWarning = false;
2635
2636 protected:
2637 void DoLogRecord( wxLogLevel aLevel, const wxString&, const wxLogRecordInfo& ) override
2638 {
2639 if( aLevel == wxLOG_Warning )
2640 m_hadWarning = true;
2641 }
2642 };
2643
2644 auto* capture = new WarningCapture();
2645 wxLog* oldLog = wxLog::SetActiveTarget( capture );
2646
2647 struct LogGuard
2648 {
2649 wxLog* old;
2650 ~LogGuard() { wxLog::SetActiveTarget( old ); }
2651 } logGuard{ oldLog };
2652
2653 KI_TEST::LoadBoard( m_settingsManager, "zone_refill_convergence_limit", m_board );
2654 KI_TEST::FillZones( m_board.get() );
2655
2656 BOOST_CHECK_MESSAGE( capture->m_hadWarning, "Expected a wxLogWarning when iterative refill hits the iteration "
2657 "limit, but none was emitted. The convergence-limit board may no "
2658 "longer trigger the cap, or the warning path has changed." );
2659}
2660
2661
2667BOOST_FIXTURE_TEST_CASE( CopperThievingZone_NonCopperLayerStampsNotSolid, ZONE_FILL_TEST_FIXTURE )
2668{
2669 m_board = std::make_unique<BOARD>();
2670
2671 ZONE* zone = new ZONE( m_board.get() );
2672 zone->SetLayer( F_SilkS );
2673 zone->AppendCorner( VECTOR2I( 0, 0 ), -1 );
2674 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 10 ), 0 ), -1 );
2675 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 10 ), pcbIUScale.mmToIU( 10 ) ), -1 );
2676 zone->AppendCorner( VECTOR2I( 0, pcbIUScale.mmToIU( 10 ) ), -1 );
2678
2679 THIEVING_SETTINGS thieving;
2681 thieving.element_size = pcbIUScale.mmToIU( 0.5 );
2682 thieving.gap = pcbIUScale.mmToIU( 1.5 );
2683 zone->SetThievingSettings( thieving );
2685 m_board->Add( zone );
2686
2687 KI_TEST::FillZones( m_board.get() );
2688
2689 const std::shared_ptr<SHAPE_POLY_SET>& fill = zone->GetFilledPolysList( F_SilkS );
2690 BOOST_REQUIRE( fill );
2691
2692 // A solid fill would have one outline (the zone polygon); a dots grid
2693 // produces dozens. Lower bound is conservative to avoid edge-clipping flakiness.
2694 BOOST_CHECK_GT( fill->OutlineCount(), 5 );
2695}
2696
2697
2706{
2707 m_board = std::make_unique<BOARD>();
2708 m_board->SetCopperLayerCount( 2 );
2709
2710 // 10 mm x 10 mm zone outline
2711 ZONE* zone = new ZONE( m_board.get() );
2712 zone->SetLayer( F_Cu );
2713 zone->AppendCorner( VECTOR2I( 0, 0 ), -1 );
2714 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 10 ), 0 ), -1 );
2715 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 10 ), pcbIUScale.mmToIU( 10 ) ), -1 );
2716 zone->AppendCorner( VECTOR2I( 0, pcbIUScale.mmToIU( 10 ) ), -1 );
2717
2719
2720 THIEVING_SETTINGS thieving;
2722 thieving.element_size = pcbIUScale.mmToIU( 0.5 );
2723 thieving.gap = pcbIUScale.mmToIU( 2.0 );
2724 thieving.line_width = pcbIUScale.mmToIU( 0.3 );
2725 thieving.stagger = false;
2726 thieving.orientation = ANGLE_0;
2727 zone->SetThievingSettings( thieving );
2728
2730
2731 m_board->Add( zone );
2732
2733 KI_TEST::FillZones( m_board.get() );
2734
2735 const std::shared_ptr<SHAPE_POLY_SET>& fill = zone->GetFilledPolysList( F_Cu );
2736 BOOST_REQUIRE( fill );
2737 BOOST_REQUIRE_GT( fill->OutlineCount(), 0 );
2738
2739 // 2.5 mm pitch, 0.5 mm dot. The four positions whose disc touches the
2740 // zone edge (x or y at 0 or 10 mm) are dropped, leaving a 3 x 3 grid.
2741 BOOST_CHECK_GE( fill->OutlineCount(), 6 );
2742 BOOST_CHECK_LE( fill->OutlineCount(), 12 );
2743
2744 // 10% slack covers the polygonal circle approximation plus post-fill corner rounding.
2745 const double fullDotArea = M_PI * std::pow( pcbIUScale.mmToIU( 0.25 ), 2 );
2746 CheckAllOutlineAreasAtLeast( fill, 0.9 * fullDotArea, wxT( "Dot" ) );
2747}
2748
2749
2756BOOST_FIXTURE_TEST_CASE( CopperThievingZone_StaggerProducesDifferentLayout, ZONE_FILL_TEST_FIXTURE )
2757{
2758 auto countDots = []( bool stagger ) -> int
2759 {
2760 auto board = std::make_unique<BOARD>();
2761 board->SetCopperLayerCount( 2 );
2762
2763 ZONE* zone = new ZONE( board.get() );
2764 zone->SetLayer( F_Cu );
2765 zone->AppendCorner( VECTOR2I( 0, 0 ), -1 );
2766 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 20 ), 0 ), -1 );
2767 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 20 ), pcbIUScale.mmToIU( 20 ) ), -1 );
2768 zone->AppendCorner( VECTOR2I( 0, pcbIUScale.mmToIU( 20 ) ), -1 );
2770
2771 THIEVING_SETTINGS thieving;
2773 thieving.element_size = pcbIUScale.mmToIU( 0.5 );
2774 thieving.gap = pcbIUScale.mmToIU( 2.0 );
2775 thieving.stagger = stagger;
2776 zone->SetThievingSettings( thieving );
2778 board->Add( zone );
2779
2780 KI_TEST::FillZones( board.get() );
2781 return zone->GetFilledPolysList( F_Cu )->OutlineCount();
2782 };
2783
2784 int plain = countDots( false );
2785 int staggered = countDots( true );
2786
2787 BOOST_TEST_MESSAGE( "plain dots: " << plain << " staggered dots: " << staggered );
2788
2789 // Within a factor of two — catches the offset walking dots off the board
2790 // (returning ~0) without being brittle about edge-clipping rounding.
2791 BOOST_CHECK_NE( plain, staggered );
2792 BOOST_CHECK_GE( staggered, plain / 2 );
2793 BOOST_CHECK_LE( staggered, plain * 2 );
2794}
2795
2796
2802BOOST_FIXTURE_TEST_CASE( CopperThievingZone_SquaresGrid, ZONE_FILL_TEST_FIXTURE )
2803{
2804 m_board = std::make_unique<BOARD>();
2805 m_board->SetCopperLayerCount( 2 );
2806
2807 ZONE* zone = new ZONE( m_board.get() );
2808 zone->SetLayer( F_Cu );
2809 zone->AppendCorner( VECTOR2I( 0, 0 ), -1 );
2810 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 10 ), 0 ), -1 );
2811 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 10 ), pcbIUScale.mmToIU( 10 ) ), -1 );
2812 zone->AppendCorner( VECTOR2I( 0, pcbIUScale.mmToIU( 10 ) ), -1 );
2814
2815 THIEVING_SETTINGS thieving;
2817 thieving.element_size = pcbIUScale.mmToIU( 0.6 );
2818 thieving.gap = pcbIUScale.mmToIU( 2.0 );
2819 zone->SetThievingSettings( thieving );
2821 m_board->Add( zone );
2822
2823 KI_TEST::FillZones( m_board.get() );
2824
2825 const std::shared_ptr<SHAPE_POLY_SET>& fill = zone->GetFilledPolysList( F_Cu );
2826 BOOST_REQUIRE( fill );
2827 BOOST_REQUIRE_GT( fill->OutlineCount(), 0 );
2828
2829 // 2.6 mm pitch, 0.6 mm square. Same edge-drop behavior as the dots test:
2830 // strict-containment leaves a 3 x 3 grid of full squares.
2831 BOOST_CHECK_GE( fill->OutlineCount(), 6 );
2832 BOOST_CHECK_LE( fill->OutlineCount(), 12 );
2833
2834 const double fullSquareArea = std::pow( pcbIUScale.mmToIU( 0.6 ), 2 );
2835 CheckAllOutlineAreasAtLeast( fill, 0.9 * fullSquareArea, wxT( "Square" ) );
2836}
2837
2838
2847BOOST_FIXTURE_TEST_CASE( CopperThievingZone_HighDensityPerformance, ZONE_FILL_TEST_FIXTURE )
2848{
2849 m_board = std::make_unique<BOARD>();
2850 m_board->SetCopperLayerCount( 2 );
2851
2852 ZONE* zone = new ZONE( m_board.get() );
2853 zone->SetLayer( F_Cu );
2854 zone->AppendCorner( VECTOR2I( 0, 0 ), -1 );
2855 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 100 ), 0 ), -1 );
2856 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 100 ), pcbIUScale.mmToIU( 100 ) ), -1 );
2857 zone->AppendCorner( VECTOR2I( 0, pcbIUScale.mmToIU( 100 ) ), -1 );
2859
2860 THIEVING_SETTINGS thieving;
2862 thieving.element_size = pcbIUScale.mmToIU( 0.3 );
2863 thieving.gap = pcbIUScale.mmToIU( 1.0 );
2864 zone->SetThievingSettings( thieving );
2866 m_board->Add( zone );
2867
2868 auto start = std::chrono::steady_clock::now();
2869 KI_TEST::FillZones( m_board.get() );
2870 auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
2871 std::chrono::steady_clock::now() - start )
2872 .count();
2873
2874 BOOST_TEST_MESSAGE( "5.9k-dot fill elapsed: " << elapsed << " ms" );
2875
2877 BOOST_CHECK_GT( zone->GetFilledPolysList( F_Cu )->OutlineCount(), 4000 );
2878
2879 // 30 s upper bound on QABUILD with assertions on; current implementation
2880 // measures in low seconds.
2881 BOOST_CHECK_LT( elapsed, 30000 );
2882}
2883
2884
2891BOOST_FIXTURE_TEST_CASE( CopperThievingZone_HatchPattern, ZONE_FILL_TEST_FIXTURE )
2892{
2893 m_board = std::make_unique<BOARD>();
2894 m_board->SetCopperLayerCount( 2 );
2895
2896 ZONE* zone = new ZONE( m_board.get() );
2897 zone->SetLayer( F_Cu );
2898 zone->AppendCorner( VECTOR2I( 0, 0 ), -1 );
2899 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 10 ), 0 ), -1 );
2900 zone->AppendCorner( VECTOR2I( pcbIUScale.mmToIU( 10 ), pcbIUScale.mmToIU( 10 ) ), -1 );
2901 zone->AppendCorner( VECTOR2I( 0, pcbIUScale.mmToIU( 10 ) ), -1 );
2903
2904 THIEVING_SETTINGS thieving;
2906 thieving.gap = pcbIUScale.mmToIU( 2.0 );
2907 thieving.line_width = pcbIUScale.mmToIU( 0.3 );
2908 zone->SetThievingSettings( thieving );
2910 m_board->Add( zone );
2911
2912 KI_TEST::FillZones( m_board.get() );
2913
2914 const std::shared_ptr<SHAPE_POLY_SET>& fill = zone->GetFilledPolysList( F_Cu );
2915 BOOST_REQUIRE( fill );
2916 BOOST_REQUIRE_GT( fill->TotalVertices(), 0 );
2917
2918 // Subtractive hatch produces a single connected outline after fracturing
2919 // (perimeter border + interior mesh linked through bridges). A dot grid
2920 // in the same outline would have dozens of disconnected pieces.
2921 BOOST_CHECK_EQUAL( fill->OutlineCount(), 1 );
2922
2923 // The fill bounding box must reach the zone corners — the perimeter
2924 // border is what differentiates hatch from a dot grid. Solid would also
2925 // reach the corners; the high vertex count below catches that case.
2926 BOX2I fillBox = fill->BBox();
2927 BOOST_CHECK_LT( fillBox.GetLeft(), pcbIUScale.mmToIU( 0.5 ) );
2928 BOOST_CHECK_GT( fillBox.GetRight(), pcbIUScale.mmToIU( 9.5 ) );
2929 BOOST_CHECK_LT( fillBox.GetTop(), pcbIUScale.mmToIU( 0.5 ) );
2930 BOOST_CHECK_GT( fillBox.GetBottom(), pcbIUScale.mmToIU( 9.5 ) );
2931
2932 // A solid 10x10 mm rectangle would have ~4 vertices. A hatched mesh has
2933 // many vertices because each void cut adds outline segments.
2934 BOOST_CHECK_GT( fill->TotalVertices(), 30 );
2935}
const char * name
@ ERROR_OUTSIDE
constexpr int ARC_HIGH_DEF
Definition base_units.h:141
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:125
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
virtual void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
Container for design settings for a BOARD object.
std::shared_ptr< DRC_ENGINE > m_DRCEngine
void SetCopperLayerCount(int aNewLayerCount)
Set the copper layer count to aNewLayerCount.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
constexpr coord_type GetLeft() const
Definition box2.h:228
constexpr coord_type GetRight() const
Definition box2.h:217
constexpr coord_type GetTop() const
Definition box2.h:229
constexpr coord_type GetBottom() const
Definition box2.h:222
bool Empty() const
Definition commit.h:137
const MINOPTMAX< int > & GetValue() const
Definition drc_rule.h:200
void RunTests(EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints, BOARD_COMMIT *aCommit=nullptr)
Run the DRC tests.
void SetViolationHandler(DRC_VIOLATION_HANDLER aHandler)
Set an optional DRC violation handler (receives DRC_ITEMs and positions).
Definition drc_engine.h:168
DRC_CONSTRAINT EvalRules(DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
void InitEngine(const wxFileName &aRulePath)
Initialize the DRC engine.
const KIID m_Uuid
Definition eda_item.h:528
Definition kiid.h:48
T Min() const
Definition minoptmax.h:33
Handle the data for a net.
Definition netinfo.h:50
int GetNetCode() const
Definition netinfo.h:97
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition padstack.h:177
Definition pad.h:55
const wxString & GetNumber() const
Definition pad.h:137
VECTOR2I GetPosition() const override
Definition pad.h:209
void TransformShapeToPolygon(SHAPE_POLY_SET &aBuffer, PCB_LAYER_ID aLayer, int aClearance, int aMaxError, ERROR_LOC aErrorLoc=ERROR_INSIDE, bool ignoreLineWidth=false) const override
Convert the pad shape to a closed polygon.
Definition pad.cpp:2609
const VECTOR2I & GetSize(PCB_LAYER_ID aLayer) const
Definition pad.h:264
Definition seg.h:42
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Compute intersection point of segment (this) with segment aSeg.
Definition seg.cpp:446
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
double Area(bool aAbsolute=true) const
Return the area of this chain.
Represent a set of closed polygons.
void BooleanAdd(const SHAPE_POLY_SET &b)
Perform boolean polyset union.
void ClearArcs()
Removes all arc references from all the outlines and holes in the polyset.
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index.
double Area()
Return the area of this poly set.
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 AddHole(const SHAPE_LINE_CHAIN &aHole, int aOutline=-1)
Adds a new hole to the given outline (default: last) and returns its index.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int NewOutline()
Creates a new empty polygon in the set and returns its index.
void BooleanIntersection(const SHAPE_POLY_SET &b)
Perform boolean polyset intersection.
int OutlineCount() const
Return the number of outlines in the set.
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
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
TEARDROP_MANAGER manage and build teardrop areas A teardrop area is a polygonal area (a copper ZONE) ...
Definition teardrop.h:94
void UpdateTeardrops(BOARD_COMMIT &aCommit, const std::vector< BOARD_ITEM * > *dirtyPadsAndVias, const std::set< PCB_TRACK * > *dirtyTracks, bool aForceFullUpdate=false)
Update teardrops on a list of items.
Definition teardrop.cpp:229
Master controller class:
void RegisterTool(TOOL_BASE *aTool)
Add a tool to the manager set and sets it up.
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, APP_SETTINGS_BASE *aSettings, TOOLS_HOLDER *aFrame)
Set the work environment (model, view, view controls and the parent window).
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition vector2d.h:283
Handle a list of polygons defining a copper zone.
Definition zone.h:74
void SetHatchThickness(int aThickness)
Definition zone.h:330
void AddPolygon(std::vector< VECTOR2I > &aPolygon)
Add a polygon to the zone outline.
Definition zone.cpp:1263
std::shared_ptr< SHAPE_POLY_SET > GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition zone.h:688
void SetMinThickness(int aMinThickness)
Definition zone.h:320
double GetFilledArea()
This area is cached from the most recent call to CalculateFilledArea().
Definition zone.h:283
void SetThermalReliefSpokeWidth(int aThermalReliefSpokeWidth)
Definition zone.h:255
virtual void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
Definition zone.cpp:603
SHAPE_POLY_SET * Outline()
Definition zone.h:422
bool SetNetCode(int aNetCode, bool aNoAssert) override
Override that clamps the netcode to 0 when this zone is in copper-thieving fill mode.
Definition zone.cpp:585
void SetFillMode(ZONE_FILL_MODE aFillMode)
Definition zone.cpp:609
bool HasFilledPolysForLayer(PCB_LAYER_ID aLayer) const
Definition zone.h:679
void SetThievingSettings(const THIEVING_SETTINGS &aSettings)
Definition zone.h:356
void SetThermalReliefGap(int aThermalReliefGap)
Definition zone.h:244
bool AppendCorner(VECTOR2I aPosition, int aHoleIdx, bool aAllowDuplication=false)
Add a new corner to the zone outline (to the main outline or a hole)
Definition zone.cpp:1280
double CalculateFilledArea()
Compute the area currently occupied by the zone fill.
Definition zone.cpp:1690
void SetPadConnection(ZONE_CONNECTION aPadConnection)
Definition zone.h:317
void SetIslandRemovalMode(ISLAND_REMOVAL_MODE aRemove)
Definition zone.h:825
void SetHatchGap(int aStep)
Definition zone.h:333
@ CHAMFER_ALL_CORNERS
All angles are chamfered.
@ ROUND_ALL_CORNERS
All angles are rounded.
@ DRCE_CLEARANCE
Definition drc_item.h:44
@ DRCE_COPPER_SLIVER
Definition drc_item.h:93
@ CLEARANCE_CONSTRAINT
Definition drc_rule.h:55
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:411
bool m_ZoneFillIterativeRefill
Enable iterative zone filling to handle isolated islands in higher priority zones.
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ B_Cu
Definition layer_ids.h:65
@ F_SilkS
Definition layer_ids.h:100
@ In1_Cu
Definition layer_ids.h:66
@ F_Cu
Definition layer_ids.h:64
void LoadBoard(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< BOARD > &aBoard)
void FillZones(BOARD *m_board)
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
CITER next(CITER it)
Definition ptree.cpp:124
@ RPT_SEVERITY_ERROR
const double epsilon
#define SKIP_SET_DIRTY
Definition sch_commit.h:42
#define SKIP_UNDO
Definition sch_commit.h:40
std::optional< VECTOR2I > OPT_VECTOR2I
Definition seg.h:39
Parameters that drive copper-thieving fill generation.
EDA_ANGLE orientation
THIEVING_PATTERN pattern
std::unique_ptr< BOARD > m_board
SETTINGS_MANAGER m_settingsManager
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
VECTOR3I v1(5, 5, 5)
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))
const SHAPE_LINE_CHAIN chain
int clearance
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2I v2(1, 0)
static const std::vector< wxString > RegressionZoneFillTests_tests
int delta
static const std::vector< std::pair< wxString, int > > RegressionTeardropFill_tests
BOOST_DATA_TEST_CASE_F(ZONE_FILL_TEST_FIXTURE, RegressionZoneFillTests, boost::unit_test::data::make(RegressionZoneFillTests_tests), relPath)
static void CheckAllOutlineAreasAtLeast(const std::shared_ptr< SHAPE_POLY_SET > &aFill, double aMinArea, const wxString &aLabel)
Assert every outline in aFill has at least aMinArea — used to verify thieving stamps survived the fil...
static const std::vector< wxString > RegressionSliverZoneFillTests_tests
BOOST_FIXTURE_TEST_CASE(BasicZoneFills, ZONE_FILL_TEST_FIXTURE)
#define M_PI
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:94
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:95
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687
VECTOR2< double > VECTOR2D
Definition vector2d.h:686
ZONE_CONNECTION
How pads are covered by copper in zone.
Definition zones.h:47
@ THERMAL
Use thermal relief for pads.
Definition zones.h:50
@ FULL
pads are covered by copper
Definition zones.h:51