KiCad PCB EDA Suite
Loading...
Searching...
No Matches
tracks_cleaner.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 (C) 2004-2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2011 Wayne Stambaugh <[email protected]>
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include "tracks_cleaner.h"
23
24#include <atomic>
25#include <bit>
26
27#include <board_commit.h>
28#include <cleanup_item.h>
31#include <drc/drc_rtree.h>
32#include <reporter.h>
33#include <thread_pool.h>
34#include <lset.h>
35#include <pad.h>
36#include <pcb_generator.h>
38#include <pcb_track.h>
39#include <tool/tool_manager.h>
40#include <tools/pcb_actions.h>
42
43
45 m_brd( aPcb ),
46 m_commit( aCommit ),
47 m_dryRun( true ),
48 m_itemsList( nullptr ),
49 m_reporter( nullptr ),
50 m_filter( nullptr )
51{
52}
53
54
55/* Main cleaning function.
56 * Delete
57 * - Redundant points on tracks (merge aligned segments)
58 * - vias on pad
59 * - null length segments
60 */
62 std::vector<std::shared_ptr<CLEANUP_ITEM> >* aItemsList,
63 bool aRemoveMisConnected, bool aCleanVias, bool aMergeSegments,
64 bool aDeleteUnconnected, bool aDeleteTracksinPad,
65 bool aDeleteDanglingVias, REPORTER* aReporter )
66{
67 m_reporter = aReporter;
68 bool has_deleted = false;
69
70 m_dryRun = aDryRun;
71 m_itemsList = aItemsList;
72
73 if( m_reporter )
74 {
75 if( aDryRun )
76 m_reporter->Report( _( "Checking null tracks and vias..." ) );
77 else
78 m_reporter->Report( _( "Removing null tracks and vias..." ) );
79
80 wxSafeYield(); // Timeslice to update UI
81 }
82
83 bool removeNullSegments = aMergeSegments || aRemoveMisConnected;
84 cleanup( aCleanVias, removeNullSegments, aMergeSegments /* dup segments*/, aMergeSegments );
85
86 if( m_reporter )
87 {
88 if( aDryRun )
89 m_reporter->Report( _( "Checking redundant tracks..." ) );
90 else
91 m_reporter->Report( _( "Removing redundant tracks..." ) );
92
93 wxSafeYield(); // Timeslice to update UI
94 }
95
96 // If we didn't remove duplicates above, do it now
97 if( !aMergeSegments )
98 cleanup( false, false, true, false );
99
100 if( aRemoveMisConnected )
101 {
102 if( m_reporter )
103 {
104 if( aDryRun )
105 m_reporter->Report( _( "Checking shorting tracks..." ) );
106 else
107 m_reporter->Report( _( "Removing shorting tracks..." ) );
108
109 wxSafeYield(); // Timeslice to update UI
110 }
111
113 }
114
115 if( aDeleteTracksinPad )
116 {
117 if( m_reporter )
118 {
119 if( aDryRun )
120 m_reporter->Report( _( "Checking tracks in pads..." ) );
121 else
122 m_reporter->Report( _( "Removing tracks in pads..." ) );
123
124 wxSafeYield(); // Timeslice to update UI
125 }
126
128 }
129
130 if( aDeleteUnconnected || aDeleteDanglingVias )
131 {
132 if( m_reporter )
133 {
134 if( aDryRun )
135 {
136 m_reporter->Report( _( "Checking dangling tracks and vias..." ) );
137 }
138 else
139 {
140 if( aDeleteUnconnected )
141 m_reporter->Report( _( "Removing dangling tracks..." ) );
142
143 if( aDeleteDanglingVias )
144 m_reporter->Report( _( "Removing dangling vias..." ) );
145 }
146
147 wxSafeYield(); // Timeslice to update UI
148 }
149
150 has_deleted = deleteDanglingTracks( aDeleteUnconnected, aDeleteDanglingVias );
151 }
152
153 if( has_deleted && aMergeSegments )
154 {
155 if( m_reporter )
156 {
157 if( aDryRun )
158 m_reporter->Report( _( "Checking collinear tracks..." ) );
159 else
160 m_reporter->Report( _( "Merging collinear tracks..." ) );
161
162 wxSafeYield(); // Timeslice to update UI
163 }
164
165 cleanup( false, false, false, true );
166 }
167}
168
169
171{
172 // A via stack owns its vias and connecting traces, and rebuilds them as a unit, so cleanup
173 // must not delete one out from under it. Other generators are left to the cleaner as before.
174 if( PCB_GENERATOR* generator = dynamic_cast<PCB_GENERATOR*>( aItem->GetParentGroup() ) )
175 {
176 if( generator->GetGeneratorType() == PCB_VIA_STACK::GENERATOR_TYPE )
177 return true;
178 }
179
180 if( !m_filter )
181 return false;
182
183 return (m_filter)( aItem );
184}
185
186
188{
189 std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_brd->GetConnectivity();
190
191 std::set<BOARD_ITEM *> toRemove;
192
193 for( PCB_TRACK* segment : m_brd->Tracks() )
194 {
195 if( segment->IsLocked() || filterItem( segment ) )
196 continue;
197
198 for( PAD* testedPad : connectivity->GetConnectedPads( segment ) )
199 {
200 if( segment->GetNetCode() != testedPad->GetNetCode() )
201 {
202 std::shared_ptr<CLEANUP_ITEM> item;
203
204 if( segment->Type() == PCB_VIA_T )
205 item = std::make_shared<CLEANUP_ITEM>( CLEANUP_SHORTING_VIA );
206 else
207 item = std::make_shared<CLEANUP_ITEM>( CLEANUP_SHORTING_TRACK );
208
209 item->SetItems( segment );
210 m_itemsList->push_back( std::move( item ) );
211
212 toRemove.insert( segment );
213 }
214 }
215
216 for( PCB_TRACK* testedTrack : connectivity->GetConnectedTracks( segment ) )
217 {
218 if( segment->GetNetCode() != testedTrack->GetNetCode() )
219 {
220 std::shared_ptr<CLEANUP_ITEM> item;
221
222 if( segment->Type() == PCB_VIA_T )
223 item = std::make_shared<CLEANUP_ITEM>( CLEANUP_SHORTING_VIA );
224 else
225 item = std::make_shared<CLEANUP_ITEM>( CLEANUP_SHORTING_TRACK );
226
227 item->SetItems( segment );
228 m_itemsList->push_back( std::move( item ) );
229
230 toRemove.insert( segment );
231 }
232 }
233 }
234
235 if( !m_dryRun )
236 removeItems( toRemove );
237}
238
239
240bool TRACKS_CLEANER::testTrackEndpointIsNode( PCB_TRACK* aTrack, bool aTstStart, bool aTstEnd )
241{
242 if( !( aTstStart && aTstEnd ) )
243 return false;
244
245 // A node is a point where more than 2 items are connected. However, we elide tracks that are
246 // collinear with the track being tested.
247 const std::list<CN_ITEM*>& items =
248 m_brd->GetConnectivity()->GetConnectivityAlgo()->ItemEntry( aTrack ).GetItems();
249
250 if( items.empty() )
251 return false;
252
253 int itemcount = 0;
254
255 for( CN_ITEM* item : items )
256 {
257 if( !item->Valid() || item->Parent() == aTrack || item->Parent()->HasFlag( IS_DELETED ) )
258 continue;
259
260 if( item->Parent()->Type() == PCB_TRACE_T &&
261 static_cast<PCB_TRACK*>( item->Parent() )->ApproxCollinear( aTrack ) )
262 {
263 continue;
264 }
265
266 for( const std::shared_ptr<CN_ANCHOR>& anchor : item->Anchors() )
267 {
268 if( ( aTstStart && anchor->Pos() == aTrack->GetStart() )
269 && ( aTstEnd && anchor->Pos() == aTrack->GetEnd() ) )
270 {
271 itemcount++;
272 break;
273 }
274 }
275 }
276
277 return itemcount > 1;
278}
279
280
281bool TRACKS_CLEANER::deleteDanglingTracks( bool aTrack, bool aVia )
282{
283 bool item_erased = false;
284 bool modified = false;
285
286 if( !aTrack && !aVia )
287 return false;
288
289 do // Iterate when at least one track is deleted
290 {
291 item_erased = false;
292 // Ensure the connectivity is up to date, especially after removing a dangling segment
293 m_brd->BuildConnectivity();
294
295 // Keep a duplicate deque to all deleting in the primary
296 std::deque<PCB_TRACK*> temp_tracks( m_brd->Tracks() );
297
298 for( PCB_TRACK* track : temp_tracks )
299 {
300 if( track->HasFlag( IS_DELETED ) || track->IsLocked() || filterItem( track ) )
301 continue;
302
303 if( !aVia && track->Type() == PCB_VIA_T )
304 continue;
305
306 if( !aTrack && ( track->Type() == PCB_TRACE_T || track->Type() == PCB_ARC_T ) )
307 continue;
308
309 // Test if a track (or a via) endpoint is not connected to another track or zone.
310 if( m_brd->GetConnectivity()->TestTrackEndpointDangling( track, false ) )
311 {
312 std::shared_ptr<CLEANUP_ITEM> item;
313
314 if( track->Type() == PCB_VIA_T )
315 item = std::make_shared<CLEANUP_ITEM>( CLEANUP_DANGLING_VIA );
316 else
317 item = std::make_shared<CLEANUP_ITEM>( CLEANUP_DANGLING_TRACK );
318
319 item->SetItems( track );
320 m_itemsList->push_back( std::move( item ) );
321 track->SetFlags( IS_DELETED );
322
323 // keep iterating, because a track connected to the deleted track
324 // now perhaps is not connected and should be deleted
325 item_erased = true;
326
327 if( !m_dryRun )
328 {
329 m_brd->Remove( track );
330 m_commit.Removed( track );
331 modified = true;
332 }
333 }
334 }
335 } while( item_erased ); // A segment was erased: test for some new dangling segments
336
337 return modified;
338}
339
340
342{
343 std::set<BOARD_ITEM*> toRemove;
344
345 // Delete tracks that start and end on the same pad
346 std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_brd->GetConnectivity();
347
348 for( PCB_TRACK* track : m_brd->Tracks() )
349 {
350 if( track->IsLocked() || filterItem( track ) )
351 continue;
352
353 if( track->Type() == PCB_VIA_T )
354 continue;
355
356 // Mark track if connected to pads
357 for( PAD* pad : connectivity->GetConnectedPads( track ) )
358 {
359 if( pad->HitTest( track->GetStart() ) && pad->HitTest( track->GetEnd() ) )
360 {
361 SHAPE_POLY_SET poly;
362 track->TransformShapeToPolygon( poly, track->GetLayer(), 0, track->GetMaxError(),
363 ERROR_INSIDE );
364
365 poly.BooleanSubtract( *pad->GetEffectivePolygon( track->GetLayer(), ERROR_INSIDE ) );
366
367 if( poly.IsEmpty() )
368 {
369 auto item = std::make_shared<CLEANUP_ITEM>( CLEANUP_TRACK_IN_PAD );
370 item->SetItems( track );
371 m_itemsList->push_back( std::move( item ) );
372
373 toRemove.insert( track );
374 track->SetFlags( IS_DELETED );
375 }
376 }
377 }
378 }
379
380 if( !m_dryRun )
381 removeItems( toRemove );
382}
383
384
388void TRACKS_CLEANER::cleanup( bool aDeleteDuplicateVias, bool aDeleteNullSegments,
389 bool aDeleteDuplicateSegments, bool aMergeSegments )
390{
391 DRC_RTREE rtree;
392
393 for( PCB_TRACK* track : m_brd->Tracks() )
394 {
395 track->ClearFlags( IS_DELETED | SKIP_STRUCT );
396 rtree.Insert( track, track->GetLayer(), NULL_CONSTRAINT );
397 }
398
399 rtree.Build();
400
401 std::set<BOARD_ITEM*> toRemove;
402
403 for( PCB_TRACK* track : m_brd->Tracks() )
404 {
405 if( track->HasFlag( IS_DELETED ) || track->IsLocked() || filterItem( track ) )
406 continue;
407
408 if( aDeleteDuplicateVias && track->Type() == PCB_VIA_T )
409 {
410 PCB_VIA* via = static_cast<PCB_VIA*>( track );
411
412 if( via->GetStart() != via->GetEnd() )
413 via->SetEnd( via->GetStart() );
414
415 rtree.QueryColliding( via, via->GetLayer(), via->GetLayer(),
416 // Filter:
417 [&]( BOARD_ITEM* aItem ) -> bool
418 {
419 return aItem->Type() == PCB_VIA_T
420 && !aItem->HasFlag( SKIP_STRUCT )
421 && !aItem->HasFlag( IS_DELETED );
422 },
423 // Visitor:
424 [&]( BOARD_ITEM* aItem ) -> bool
425 {
426 PCB_VIA* other = static_cast<PCB_VIA*>( aItem );
427
428 if( via->GetPosition() == other->GetPosition()
429 && via->GetViaType() == other->GetViaType()
430 && via->GetLayerSet() == other->GetLayerSet() )
431 {
432 auto item = std::make_shared<CLEANUP_ITEM>( CLEANUP_REDUNDANT_VIA );
433 item->SetItems( via );
434 m_itemsList->push_back( std::move( item ) );
435
436 via->SetFlags( IS_DELETED );
437 toRemove.insert( via );
438 }
439
440 return true;
441 } );
442
443 // To delete through Via on THT pads at same location
444 // Examine the list of connected pads: if a through pad is found, the via is redundant
445 for( PAD* pad : m_brd->GetConnectivity()->GetConnectedPads( via ) )
446 {
447 const LSET all_cu = LSET::AllCuMask( m_brd->GetCopperLayerCount() );
448
449 if( ( pad->GetLayerSet() & all_cu ) == all_cu )
450 {
451 auto item = std::make_shared<CLEANUP_ITEM>( CLEANUP_REDUNDANT_VIA );
452 item->SetItems( via, pad );
453 m_itemsList->push_back( std::move( item ) );
454
455 via->SetFlags( IS_DELETED );
456 toRemove.insert( via );
457 break;
458 }
459 }
460
461 via->SetFlags( SKIP_STRUCT );
462 }
463
464 if( aDeleteNullSegments && track->Type() != PCB_VIA_T )
465 {
466 if( track->IsNull() )
467 {
468 auto item = std::make_shared<CLEANUP_ITEM>( CLEANUP_ZERO_LENGTH_TRACK );
469 item->SetItems( track );
470 m_itemsList->push_back( std::move( item ) );
471
472 track->SetFlags( IS_DELETED );
473 toRemove.insert( track );
474 }
475 }
476
477 if( aDeleteDuplicateSegments && track->Type() == PCB_TRACE_T && !track->IsNull() )
478 {
479 rtree.QueryColliding( track, track->GetLayer(), track->GetLayer(),
480 // Filter:
481 [&]( BOARD_ITEM* aItem ) -> bool
482 {
483 return aItem->Type() == PCB_TRACE_T
484 && !aItem->HasFlag( SKIP_STRUCT )
485 && !aItem->HasFlag( IS_DELETED )
486 && !static_cast<PCB_TRACK*>( aItem )->IsNull();
487 },
488 // Visitor:
489 [&]( BOARD_ITEM* aItem ) -> bool
490 {
491 PCB_TRACK* other = static_cast<PCB_TRACK*>( aItem );
492
493 if( track->IsPointOnEnds( other->GetStart() )
494 && track->IsPointOnEnds( other->GetEnd() )
495 && track->GetWidth() == other->GetWidth()
496 && track->GetLayer() == other->GetLayer() )
497 {
498 auto item = std::make_shared<CLEANUP_ITEM>( CLEANUP_DUPLICATE_TRACK );
499 item->SetItems( track );
500 m_itemsList->push_back( std::move( item ) );
501
502 track->SetFlags( IS_DELETED );
503 toRemove.insert( track );
504 }
505
506 return true;
507 } );
508
509 track->SetFlags( SKIP_STRUCT );
510 }
511 }
512
513 if( !m_dryRun )
514 removeItems( toRemove );
515
516
517
518
519 auto mergeSegments = [&]( std::shared_ptr<CN_CONNECTIVITY_ALGO> connectivity ) -> bool
520 {
521 auto track_loop = [&]( int aStart, int aEnd ) -> std::vector<std::pair<PCB_TRACK*, PCB_TRACK*>>
522 {
523 std::vector<std::pair<PCB_TRACK*, PCB_TRACK*>> tracks;
524
525 for( int ii = aStart; ii < aEnd; ++ii )
526 {
527 PCB_TRACK* segment = m_brd->Tracks()[ii];
528
529 // one can merge only collinear segments, not vias or arcs.
530 if( segment->Type() != PCB_TRACE_T )
531 continue;
532
533 if( segment->HasFlag( IS_DELETED ) ) // already taken into account
534 continue;
535
536 if( filterItem( segment ) )
537 continue;
538
539 // for each end of the segment:
540 auto& cnItems = connectivity->ItemEntry( segment ).GetItems();
541
542 for( CN_ITEM* citem : cnItems )
543 {
544 // Do not merge an end which has different width tracks attached -- it's a
545 // common use-case for necking-down a track between pads.
546 std::vector<PCB_TRACK*> sameWidthCandidates;
547 std::vector<PCB_TRACK*> differentWidthCandidates;
548
549 for( CN_ITEM* connected : citem->ConnectedItems() )
550 {
551 if( !connected->Valid() )
552 continue;
553
554 BOARD_CONNECTED_ITEM* candidate = connected->Parent();
555
556 if( candidate->Type() == PCB_TRACE_T && !candidate->HasFlag( IS_DELETED )
557 && !filterItem( candidate ) )
558 {
559 PCB_TRACK* candidateSegment = static_cast<PCB_TRACK*>( candidate );
560
561 if( candidateSegment->GetWidth() == segment->GetWidth() )
562 {
563 sameWidthCandidates.push_back( candidateSegment );
564 }
565 else
566 {
567 differentWidthCandidates.push_back( candidateSegment );
568 break;
569 }
570 }
571 }
572
573 if( !differentWidthCandidates.empty() )
574 continue;
575
576 for( PCB_TRACK* candidate : sameWidthCandidates )
577 {
578 if( candidate < segment ) // avoid duplicate merges
579 continue;
580
581 if( segment->ApproxCollinear( *candidate )
582 && testMergeCollinearSegments( segment, candidate ) )
583 {
584 tracks.emplace_back( segment, candidate );
585 break;
586 }
587 }
588 }
589 }
590
591 return tracks;
592 };
593
594 // The idea here is to parallelize the loop that does not modify the connectivity
595 // and extract all of the pairs of segments that might be merged. Then, perform
596 // the actual merge in the main loop.
598 auto merge_returns = tp.submit_blocks( 0, m_brd->Tracks().size(), track_loop );
599 bool retval = false;
600
601 // Drain every worker before mutating any track flags. mergeCollinearSegments writes
602 // IS_DELETED on aSeg2, and worker threads still in track_loop read the same flags via
603 // HasFlag and via the PCB_TRACK copy constructor in testMergeCollinearSegments.
604 std::vector<std::pair<PCB_TRACK*, PCB_TRACK*>> mergePairs;
605
606 for( auto& ret : merge_returns )
607 {
608 if( ret.valid() )
609 {
610 std::vector<std::pair<PCB_TRACK*, PCB_TRACK*>> pairs = ret.get();
611 mergePairs.insert( mergePairs.end(), pairs.begin(), pairs.end() );
612 }
613 }
614
615 for( auto& [seg1, seg2] : mergePairs )
616 {
617 retval = true;
618
619 if( seg1->HasFlag( IS_DELETED ) || seg2->HasFlag( IS_DELETED ) )
620 continue;
621
622 mergeCollinearSegments( seg1, seg2 );
623 }
624
625 return retval;
626 };
627
628 if( aMergeSegments )
629 {
630 do
631 {
632 while( !m_brd->BuildConnectivity() )
633 wxSafeYield();
634
635 // BuildConnectivity adds items but doesn't establish connections between them.
636 // RecalculateRatsnest triggers searchConnections which actually finds and links
637 // connected items in the connectivity graph.
638 m_brd->GetConnectivity()->RecalculateRatsnest();
639
640 std::lock_guard lock( m_mutex );
641 m_connectedItemsCache.clear();
642 } while( mergeSegments( m_brd->GetConnectivity()->GetConnectivityAlgo() ) );
643 }
644
645 for( PCB_TRACK* track : m_brd->Tracks() )
646 track->ClearFlags( IS_DELETED | SKIP_STRUCT );
647}
648
649
650const std::vector<BOARD_CONNECTED_ITEM*>& TRACKS_CLEANER::getConnectedItems( PCB_TRACK* aTrack )
651{
652 const std::shared_ptr<CONNECTIVITY_DATA>& connectivity = m_brd->GetConnectivity();
653 std::lock_guard lock( m_mutex );
654
655 if( !m_connectedItemsCache.contains( aTrack ) )
656 m_connectedItemsCache[aTrack] = connectivity->GetConnectedItems( aTrack );
657
658 return m_connectedItemsCache.at( aTrack );
659}
660
661
663{
664 if( aSeg1->IsLocked() || aSeg2->IsLocked() )
665 return false;
666
667 // Collect the unique points where the two tracks are connected to other items
668 const unsigned p1s = 1 << 0;
669 const unsigned p1e = 1 << 1;
670 const unsigned p2s = 1 << 2;
671 const unsigned p2e = 1 << 3;
672 std::vector<VECTOR2I> pts = { aSeg1->GetStart(), aSeg1->GetEnd(), aSeg2->GetStart(), aSeg2->GetEnd() };
673 std::atomic<unsigned> flags = 0;
674
675 auto collectPtsSeg1 =
676 [&]( BOARD_CONNECTED_ITEM* citem )
677 {
678 if( std::popcount( flags.load() ) > 2 )
679 return;
680
681 if( citem->Type() == PCB_TRACE_T || citem->Type() == PCB_ARC_T
682 || citem->Type() == PCB_VIA_T )
683 {
684 PCB_TRACK* track = static_cast<PCB_TRACK*>( citem );
685
686 if( track->IsPointOnEnds( aSeg1->GetStart() ) )
687 flags |= p1s;
688
689 if( track->IsPointOnEnds( aSeg1->GetEnd() ) )
690 flags |= p1e;
691 }
692 else
693 {
694 if( !( flags & p1s ) && citem->HitTest( aSeg1->GetStart(), ( aSeg1->GetWidth() + 1 ) / 2 ) )
695 flags |= p1s;
696
697 if( !( flags & p1e ) && citem->HitTest( aSeg1->GetEnd(), ( aSeg1->GetWidth() + 1 ) / 2 ) )
698 flags |= p1e;
699 }
700 };
701
702 auto collectPtsSeg2 =
703 [&]( BOARD_CONNECTED_ITEM* citem )
704 {
705 if( std::popcount( flags.load() ) > 2 )
706 return;
707
708 if( citem->Type() == PCB_TRACE_T || citem->Type() == PCB_ARC_T
709 || citem->Type() == PCB_VIA_T )
710 {
711 PCB_TRACK* track = static_cast<PCB_TRACK*>( citem );
712
713 if( track->IsPointOnEnds( aSeg2->GetStart() ) )
714 flags |= p2s;
715
716 if( track->IsPointOnEnds( aSeg2->GetEnd() ) )
717 flags |= p2e;
718 }
719 else
720 {
721 if( !( flags & p2s ) && citem->HitTest( aSeg2->GetStart(), ( aSeg2->GetWidth() + 1 ) / 2 ) )
722 flags |= p2s;
723
724 if( !( flags & p2e ) && citem->HitTest( aSeg2->GetEnd(), ( aSeg2->GetWidth() + 1 ) / 2 ) )
725 flags |= p2e;
726 }
727 };
728
729 for( BOARD_CONNECTED_ITEM* item : getConnectedItems( aSeg1 ) )
730 {
731 if( item->HasFlag( IS_DELETED ) )
732 continue;
733
734 if( item != aSeg1 && item != aSeg2 )
735 collectPtsSeg1( item );
736 }
737
738 for( BOARD_CONNECTED_ITEM* item : getConnectedItems( aSeg2 ) )
739 {
740 if( item->HasFlag( IS_DELETED ) )
741 continue;
742
743 if( item != aSeg1 && item != aSeg2 )
744 collectPtsSeg2( item );
745 }
746
747 // This means there is a node in the center
748 if( std::popcount( flags.load() ) > 2 )
749 return false;
750
751 // Verify the removed point after merging is not a node.
752 // If it is a node (i.e. if more than one other item is connected, the segments cannot be merged
753
754 PCB_TRACK dummy_seg( *aSeg1 );
755
756 if( !aDummySeg )
757 aDummySeg = &dummy_seg;
758
759 // Calculate the new ends of the segment to merge, and store them to dummy_seg:
760 int min_x = std::min( aSeg1->GetStart().x,
761 std::min( aSeg1->GetEnd().x, std::min( aSeg2->GetStart().x, aSeg2->GetEnd().x ) ) );
762 int min_y = std::min( aSeg1->GetStart().y,
763 std::min( aSeg1->GetEnd().y, std::min( aSeg2->GetStart().y, aSeg2->GetEnd().y ) ) );
764 int max_x = std::max( aSeg1->GetStart().x,
765 std::max( aSeg1->GetEnd().x, std::max( aSeg2->GetStart().x, aSeg2->GetEnd().x ) ) );
766 int max_y = std::max( aSeg1->GetStart().y,
767 std::max( aSeg1->GetEnd().y, std::max( aSeg2->GetStart().y, aSeg2->GetEnd().y ) ) );
768
769 if( ( aSeg1->GetStart().x > aSeg1->GetEnd().x )
770 == ( aSeg1->GetStart().y > aSeg1->GetEnd().y ) )
771 {
772 aDummySeg->SetStart( VECTOR2I( min_x, min_y ) );
773 aDummySeg->SetEnd( VECTOR2I( max_x, max_y ) );
774 }
775 else
776 {
777 aDummySeg->SetStart( VECTOR2I( min_x, max_y ) );
778 aDummySeg->SetEnd( VECTOR2I( max_x, min_y ) );
779 }
780
781 // The new ends of the segment must be connected to all of the same points as the original
782 // segments. If not, the segments cannot be merged.
783 for( unsigned i = 0; i < 4; ++i )
784 {
785 if( ( flags & ( 1 << i ) ) && !aDummySeg->IsPointOnEnds( pts[i] ) )
786 return false;
787 }
788
789 // Now find the removed end(s) and stop merging if it is a node:
790 return !testTrackEndpointIsNode( aSeg1, aDummySeg->IsPointOnEnds( aSeg1->GetStart() ),
791 aDummySeg->IsPointOnEnds( aSeg1->GetEnd() ) );
792}
793
795{
796 PCB_TRACK dummy_seg( *aSeg1 );
797
798 if( !testMergeCollinearSegments( aSeg1, aSeg2, &dummy_seg ) )
799 return false;
800
801 std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_MERGE_TRACKS );
802 item->SetItems( aSeg1, aSeg2 );
803 m_itemsList->push_back( std::move( item ) );
804
805 aSeg2->SetFlags( IS_DELETED );
806
807 if( !m_dryRun )
808 {
809 m_commit.Modify( aSeg1 );
810
811 *aSeg1 = dummy_seg;
812
813 m_brd->GetConnectivity()->Update( aSeg1 );
814
815 // Merge successful, seg2 has to go away
816 m_brd->Remove( aSeg2 );
817 m_commit.Removed( aSeg2 );
818 }
819
820 return true;
821}
822
823
824void TRACKS_CLEANER::removeItems( std::set<BOARD_ITEM*>& aItems )
825{
826 for( BOARD_ITEM* item : aItems )
827 {
828 m_brd->Remove( item );
829 m_commit.Removed( item );
830 }
831}
@ ERROR_INSIDE
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
bool IsLocked() const override
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:409
CN_ITEM represents a BOARD_CONNETED_ITEM in the connectivity system (ie: a pad, track/arc/via,...
const std::vector< CN_ITEM * > & ConnectedItems() const
Implement an R-tree for fast spatial and layer indexing of connectable items.
Definition drc_rtree.h:45
void Insert(BOARD_ITEM *aItem, PCB_LAYER_ID aLayer, DRC_CONSTRAINT_T aConstraintType, int aWorstClearance=0, bool aAtomicTables=false)
Insert an item into the tree on a particular layer with an optional worst clearance.
Definition drc_rtree.h:91
int QueryColliding(BOARD_ITEM *aRefItem, PCB_LAYER_ID aRefLayer, PCB_LAYER_ID aTargetLayer, std::function< bool(BOARD_ITEM *)> aFilter=nullptr, std::function< bool(BOARD_ITEM *)> aVisitor=nullptr, int aClearance=0) const
This is a fast test which essentially does bounding-box overlap given a worst-case clearance.
Definition drc_rtree.h:277
void Build()
Finalize all pending inserts by bulk-building packed R-trees from the staged items.
Definition drc_rtree.h:164
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition eda_item.h:158
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:116
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition eda_item.h:168
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:604
Definition pad.h:61
void SetEnd(const VECTOR2I &aEnd)
Definition pcb_track.h:89
void SetStart(const VECTOR2I &aStart)
Definition pcb_track.h:92
bool ApproxCollinear(const PCB_TRACK &aTrack)
const VECTOR2I & GetStart() const
Definition pcb_track.h:93
const VECTOR2I & GetEnd() const
Definition pcb_track.h:90
EDA_ITEM_FLAGS IsPointOnEnds(const VECTOR2I &point, int min_dist=0) const
Return STARTPOINT if point if near (dist = min_dist) start point, ENDPOINT if point if near (dist = m...
virtual int GetWidth() const
Definition pcb_track.h:87
static const wxString GENERATOR_TYPE
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
Represent a set of closed polygons.
bool IsEmpty() const
Return true if the set is empty (no polygons at all)
void BooleanSubtract(const SHAPE_POLY_SET &b)
Perform boolean polyset difference.
const std::vector< BOARD_CONNECTED_ITEM * > & getConnectedItems(PCB_TRACK *aTrack)
bool testMergeCollinearSegments(PCB_TRACK *aSeg1, PCB_TRACK *aSeg2, PCB_TRACK *aDummySeg=nullptr)
helper function test if 2 segments are colinear.
bool filterItem(BOARD_CONNECTED_ITEM *aItem)
void removeItems(std::set< BOARD_ITEM * > &aItems)
void cleanup(bool aDeleteDuplicateVias, bool aDeleteNullSegments, bool aDeleteDuplicateSegments, bool aMergeSegments)
Geometry-based cleanup: duplicate items, null items, colinear items.
void CleanupBoard(bool aDryRun, std::vector< std::shared_ptr< CLEANUP_ITEM > > *aItemsList, bool aCleanVias, bool aRemoveMisConnected, bool aMergeSegments, bool aDeleteUnconnected, bool aDeleteTracksinPad, bool aDeleteDanglingVias, REPORTER *aReporter=nullptr)
the cleanup function.
std::mutex m_mutex
REPORTER * m_reporter
bool mergeCollinearSegments(PCB_TRACK *aSeg1, PCB_TRACK *aSeg2)
helper function merge aTrackRef and aCandidate, when possible, i.e.
bool deleteDanglingTracks(bool aTrack, bool aVia)
Removes tracks or vias only connected on one end.
bool testTrackEndpointIsNode(PCB_TRACK *aTrack, bool aTstStart, bool aTstEnd)
std::map< PCB_TRACK *, std::vector< BOARD_CONNECTED_ITEM * > > m_connectedItemsCache
std::function< bool(BOARD_CONNECTED_ITEM *aItem)> m_filter
std::vector< std::shared_ptr< CLEANUP_ITEM > > * m_itemsList
TRACKS_CLEANER(BOARD *aPcb, BOARD_COMMIT &aCommit)
void removeShortingTrackSegments()
BOARD_COMMIT & m_commit
@ CLEANUP_TRACK_IN_PAD
@ CLEANUP_DANGLING_VIA
@ CLEANUP_MERGE_TRACKS
@ CLEANUP_DANGLING_TRACK
@ CLEANUP_ZERO_LENGTH_TRACK
@ CLEANUP_SHORTING_VIA
@ CLEANUP_REDUNDANT_VIA
@ CLEANUP_SHORTING_TRACK
@ NULL_CONSTRAINT
Definition drc_rule.h:50
#define _(s)
#define IS_DELETED
#define SKIP_STRUCT
flag indicating that the structure should be ignored
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
static thread_pool * tp
BS::priority_thread_pool thread_pool
Definition thread_pool.h:27
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:90
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:88
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683