You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

806 lines
27 KiB

5 years ago
5 years ago
5 years ago
  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 <stambaughw@verizon.net>
  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, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <atomic>
  26. #include <bit>
  27. #include <reporter.h>
  28. #include <board_commit.h>
  29. #include <cleanup_item.h>
  30. #include <connectivity/connectivity_algo.h>
  31. #include <connectivity/connectivity_data.h>
  32. #include <thread_pool.h>
  33. #include <lset.h>
  34. #include <tool/tool_manager.h>
  35. #include <tools/pcb_actions.h>
  36. #include <tools/global_edit_tool.h>
  37. #include <drc/drc_rtree.h>
  38. #include <tracks_cleaner.h>
  39. TRACKS_CLEANER::TRACKS_CLEANER( BOARD* aPcb, BOARD_COMMIT& aCommit ) :
  40. m_brd( aPcb ),
  41. m_commit( aCommit ),
  42. m_dryRun( true ),
  43. m_itemsList( nullptr ),
  44. m_reporter( nullptr ),
  45. m_filter( nullptr )
  46. {
  47. }
  48. /* Main cleaning function.
  49. * Delete
  50. * - Redundant points on tracks (merge aligned segments)
  51. * - vias on pad
  52. * - null length segments
  53. */
  54. void TRACKS_CLEANER::CleanupBoard( bool aDryRun,
  55. std::vector<std::shared_ptr<CLEANUP_ITEM> >* aItemsList,
  56. bool aRemoveMisConnected, bool aCleanVias, bool aMergeSegments,
  57. bool aDeleteUnconnected, bool aDeleteTracksinPad,
  58. bool aDeleteDanglingVias, REPORTER* aReporter )
  59. {
  60. m_reporter = aReporter;
  61. bool has_deleted = false;
  62. m_dryRun = aDryRun;
  63. m_itemsList = aItemsList;
  64. if( m_reporter )
  65. {
  66. if( aDryRun )
  67. m_reporter->Report( _( "Checking null tracks and vias..." ) );
  68. else
  69. m_reporter->Report( _( "Removing null tracks and vias..." ) );
  70. wxSafeYield(); // Timeslice to update UI
  71. }
  72. bool removeNullSegments = aMergeSegments || aRemoveMisConnected;
  73. cleanup( aCleanVias, removeNullSegments, aMergeSegments /* dup segments*/, aMergeSegments );
  74. if( m_reporter )
  75. {
  76. if( aDryRun )
  77. m_reporter->Report( _( "Checking redundant tracks..." ) );
  78. else
  79. m_reporter->Report( _( "Removing redundant tracks..." ) );
  80. wxSafeYield(); // Timeslice to update UI
  81. }
  82. // If we didn't remove duplicates above, do it now
  83. if( !aMergeSegments )
  84. cleanup( false, false, true, false );
  85. if( aRemoveMisConnected )
  86. {
  87. if( m_reporter )
  88. {
  89. if( aDryRun )
  90. m_reporter->Report( _( "Checking shorting tracks..." ) );
  91. else
  92. m_reporter->Report( _( "Removing shorting tracks..." ) );
  93. wxSafeYield(); // Timeslice to update UI
  94. }
  95. removeShortingTrackSegments();
  96. }
  97. if( aDeleteTracksinPad )
  98. {
  99. if( m_reporter )
  100. {
  101. if( aDryRun )
  102. m_reporter->Report( _( "Checking tracks in pads..." ) );
  103. else
  104. m_reporter->Report( _( "Removing tracks in pads..." ) );
  105. wxSafeYield(); // Timeslice to update UI
  106. }
  107. deleteTracksInPads();
  108. }
  109. if( aDeleteUnconnected || aDeleteDanglingVias )
  110. {
  111. if( m_reporter )
  112. {
  113. if( aDryRun )
  114. {
  115. m_reporter->Report( _( "Checking dangling tracks and vias..." ) );
  116. }
  117. else
  118. {
  119. if( aDeleteUnconnected )
  120. m_reporter->Report( _( "Removing dangling tracks..." ) );
  121. if( aDeleteDanglingVias )
  122. m_reporter->Report( _( "Removing dangling vias..." ) );
  123. }
  124. wxSafeYield(); // Timeslice to update UI
  125. }
  126. has_deleted = deleteDanglingTracks( aDeleteUnconnected, aDeleteDanglingVias );
  127. }
  128. if( has_deleted && aMergeSegments )
  129. {
  130. if( m_reporter )
  131. {
  132. if( aDryRun )
  133. m_reporter->Report( _( "Checking collinear tracks..." ) );
  134. else
  135. m_reporter->Report( _( "Merging collinear tracks..." ) );
  136. wxSafeYield(); // Timeslice to update UI
  137. }
  138. cleanup( false, false, false, true );
  139. }
  140. }
  141. bool TRACKS_CLEANER::filterItem( BOARD_CONNECTED_ITEM* aItem )
  142. {
  143. if( !m_filter )
  144. return false;
  145. return (m_filter)( aItem );
  146. }
  147. void TRACKS_CLEANER::removeShortingTrackSegments()
  148. {
  149. std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_brd->GetConnectivity();
  150. std::set<BOARD_ITEM *> toRemove;
  151. for( PCB_TRACK* segment : m_brd->Tracks() )
  152. {
  153. if( segment->IsLocked() || filterItem( segment ) )
  154. continue;
  155. for( PAD* testedPad : connectivity->GetConnectedPads( segment ) )
  156. {
  157. if( segment->GetNetCode() != testedPad->GetNetCode() )
  158. {
  159. std::shared_ptr<CLEANUP_ITEM> item;
  160. if( segment->Type() == PCB_VIA_T )
  161. item = std::make_shared<CLEANUP_ITEM>( CLEANUP_SHORTING_VIA );
  162. else
  163. item = std::make_shared<CLEANUP_ITEM>( CLEANUP_SHORTING_TRACK );
  164. item->SetItems( segment );
  165. m_itemsList->push_back( std::move( item ) );
  166. toRemove.insert( segment );
  167. }
  168. }
  169. for( PCB_TRACK* testedTrack : connectivity->GetConnectedTracks( segment ) )
  170. {
  171. if( segment->GetNetCode() != testedTrack->GetNetCode() )
  172. {
  173. std::shared_ptr<CLEANUP_ITEM> item;
  174. if( segment->Type() == PCB_VIA_T )
  175. item = std::make_shared<CLEANUP_ITEM>( CLEANUP_SHORTING_VIA );
  176. else
  177. item = std::make_shared<CLEANUP_ITEM>( CLEANUP_SHORTING_TRACK );
  178. item->SetItems( segment );
  179. m_itemsList->push_back( std::move( item ) );
  180. toRemove.insert( segment );
  181. }
  182. }
  183. }
  184. if( !m_dryRun )
  185. removeItems( toRemove );
  186. }
  187. bool TRACKS_CLEANER::testTrackEndpointIsNode( PCB_TRACK* aTrack, bool aTstStart, bool aTstEnd )
  188. {
  189. if( !( aTstStart && aTstEnd ) )
  190. return false;
  191. // A node is a point where more than 2 items are connected. However, we elide tracks that are
  192. // collinear with the track being tested.
  193. const std::list<CN_ITEM*>& items =
  194. m_brd->GetConnectivity()->GetConnectivityAlgo()->ItemEntry( aTrack ).GetItems();
  195. if( items.empty() )
  196. return false;
  197. int itemcount = 0;
  198. for( CN_ITEM* item : items )
  199. {
  200. if( !item->Valid() || item->Parent() == aTrack || item->Parent()->HasFlag( IS_DELETED ) )
  201. continue;
  202. if( item->Parent()->Type() == PCB_TRACE_T &&
  203. static_cast<PCB_TRACK*>( item->Parent() )->ApproxCollinear( aTrack ) )
  204. {
  205. continue;
  206. }
  207. for( const std::shared_ptr<CN_ANCHOR>& anchor : item->Anchors() )
  208. {
  209. if( ( aTstStart && anchor->Pos() == aTrack->GetStart() )
  210. && ( aTstEnd && anchor->Pos() == aTrack->GetEnd() ) )
  211. {
  212. itemcount++;
  213. break;
  214. }
  215. }
  216. }
  217. return itemcount > 1;
  218. }
  219. bool TRACKS_CLEANER::deleteDanglingTracks( bool aTrack, bool aVia )
  220. {
  221. bool item_erased = false;
  222. bool modified = false;
  223. if( !aTrack && !aVia )
  224. return false;
  225. do // Iterate when at least one track is deleted
  226. {
  227. item_erased = false;
  228. // Ensure the connectivity is up to date, especially after removing a dangling segment
  229. m_brd->BuildConnectivity();
  230. // Keep a duplicate deque to all deleting in the primary
  231. std::deque<PCB_TRACK*> temp_tracks( m_brd->Tracks() );
  232. for( PCB_TRACK* track : temp_tracks )
  233. {
  234. if( track->HasFlag( IS_DELETED ) || track->IsLocked() || filterItem( track ) )
  235. continue;
  236. if( !aVia && track->Type() == PCB_VIA_T )
  237. continue;
  238. if( !aTrack && ( track->Type() == PCB_TRACE_T || track->Type() == PCB_ARC_T ) )
  239. continue;
  240. // Test if a track (or a via) endpoint is not connected to another track or zone.
  241. if( m_brd->GetConnectivity()->TestTrackEndpointDangling( track, false ) )
  242. {
  243. std::shared_ptr<CLEANUP_ITEM> item;
  244. if( track->Type() == PCB_VIA_T )
  245. item = std::make_shared<CLEANUP_ITEM>( CLEANUP_DANGLING_VIA );
  246. else
  247. item = std::make_shared<CLEANUP_ITEM>( CLEANUP_DANGLING_TRACK );
  248. item->SetItems( track );
  249. m_itemsList->push_back( std::move( item ) );
  250. track->SetFlags( IS_DELETED );
  251. // keep iterating, because a track connected to the deleted track
  252. // now perhaps is not connected and should be deleted
  253. item_erased = true;
  254. if( !m_dryRun )
  255. {
  256. m_brd->Remove( track );
  257. m_commit.Removed( track );
  258. modified = true;
  259. }
  260. }
  261. }
  262. } while( item_erased ); // A segment was erased: test for some new dangling segments
  263. return modified;
  264. }
  265. void TRACKS_CLEANER::deleteTracksInPads()
  266. {
  267. std::set<BOARD_ITEM*> toRemove;
  268. // Delete tracks that start and end on the same pad
  269. std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_brd->GetConnectivity();
  270. for( PCB_TRACK* track : m_brd->Tracks() )
  271. {
  272. if( track->IsLocked() || filterItem( track ) )
  273. continue;
  274. if( track->Type() == PCB_VIA_T )
  275. continue;
  276. // Mark track if connected to pads
  277. for( PAD* pad : connectivity->GetConnectedPads( track ) )
  278. {
  279. if( pad->HitTest( track->GetStart() ) && pad->HitTest( track->GetEnd() ) )
  280. {
  281. SHAPE_POLY_SET poly;
  282. track->TransformShapeToPolygon( poly, track->GetLayer(), 0, track->GetMaxError(),
  283. ERROR_INSIDE );
  284. poly.BooleanSubtract( *pad->GetEffectivePolygon( track->GetLayer(), ERROR_INSIDE ) );
  285. if( poly.IsEmpty() )
  286. {
  287. auto item = std::make_shared<CLEANUP_ITEM>( CLEANUP_TRACK_IN_PAD );
  288. item->SetItems( track );
  289. m_itemsList->push_back( std::move( item ) );
  290. toRemove.insert( track );
  291. track->SetFlags( IS_DELETED );
  292. }
  293. }
  294. }
  295. }
  296. if( !m_dryRun )
  297. removeItems( toRemove );
  298. }
  299. /**
  300. * Geometry-based cleanup: duplicate items, null items, colinear items.
  301. */
  302. void TRACKS_CLEANER::cleanup( bool aDeleteDuplicateVias, bool aDeleteNullSegments,
  303. bool aDeleteDuplicateSegments, bool aMergeSegments )
  304. {
  305. DRC_RTREE rtree;
  306. for( PCB_TRACK* track : m_brd->Tracks() )
  307. {
  308. track->ClearFlags( IS_DELETED | SKIP_STRUCT );
  309. rtree.Insert( track, track->GetLayer() );
  310. }
  311. std::set<BOARD_ITEM*> toRemove;
  312. for( PCB_TRACK* track : m_brd->Tracks() )
  313. {
  314. if( track->HasFlag( IS_DELETED ) || track->IsLocked() || filterItem( track ) )
  315. continue;
  316. if( aDeleteDuplicateVias && track->Type() == PCB_VIA_T )
  317. {
  318. PCB_VIA* via = static_cast<PCB_VIA*>( track );
  319. if( via->GetStart() != via->GetEnd() )
  320. via->SetEnd( via->GetStart() );
  321. rtree.QueryColliding( via, via->GetLayer(), via->GetLayer(),
  322. // Filter:
  323. [&]( BOARD_ITEM* aItem ) -> bool
  324. {
  325. return aItem->Type() == PCB_VIA_T
  326. && !aItem->HasFlag( SKIP_STRUCT )
  327. && !aItem->HasFlag( IS_DELETED );
  328. },
  329. // Visitor:
  330. [&]( BOARD_ITEM* aItem ) -> bool
  331. {
  332. PCB_VIA* other = static_cast<PCB_VIA*>( aItem );
  333. if( via->GetPosition() == other->GetPosition()
  334. && via->GetViaType() == other->GetViaType()
  335. && via->GetLayerSet() == other->GetLayerSet() )
  336. {
  337. auto item = std::make_shared<CLEANUP_ITEM>( CLEANUP_REDUNDANT_VIA );
  338. item->SetItems( via );
  339. m_itemsList->push_back( std::move( item ) );
  340. via->SetFlags( IS_DELETED );
  341. toRemove.insert( via );
  342. }
  343. return true;
  344. } );
  345. // To delete through Via on THT pads at same location
  346. // Examine the list of connected pads: if a through pad is found, the via is redundant
  347. for( PAD* pad : m_brd->GetConnectivity()->GetConnectedPads( via ) )
  348. {
  349. const LSET all_cu = LSET::AllCuMask( m_brd->GetCopperLayerCount() );
  350. if( ( pad->GetLayerSet() & all_cu ) == all_cu )
  351. {
  352. auto item = std::make_shared<CLEANUP_ITEM>( CLEANUP_REDUNDANT_VIA );
  353. item->SetItems( via, pad );
  354. m_itemsList->push_back( std::move( item ) );
  355. via->SetFlags( IS_DELETED );
  356. toRemove.insert( via );
  357. break;
  358. }
  359. }
  360. via->SetFlags( SKIP_STRUCT );
  361. }
  362. if( aDeleteNullSegments && track->Type() != PCB_VIA_T )
  363. {
  364. if( track->IsNull() )
  365. {
  366. auto item = std::make_shared<CLEANUP_ITEM>( CLEANUP_ZERO_LENGTH_TRACK );
  367. item->SetItems( track );
  368. m_itemsList->push_back( std::move( item ) );
  369. track->SetFlags( IS_DELETED );
  370. toRemove.insert( track );
  371. }
  372. }
  373. if( aDeleteDuplicateSegments && track->Type() == PCB_TRACE_T && !track->IsNull() )
  374. {
  375. rtree.QueryColliding( track, track->GetLayer(), track->GetLayer(),
  376. // Filter:
  377. [&]( BOARD_ITEM* aItem ) -> bool
  378. {
  379. return aItem->Type() == PCB_TRACE_T
  380. && !aItem->HasFlag( SKIP_STRUCT )
  381. && !aItem->HasFlag( IS_DELETED )
  382. && !static_cast<PCB_TRACK*>( aItem )->IsNull();
  383. },
  384. // Visitor:
  385. [&]( BOARD_ITEM* aItem ) -> bool
  386. {
  387. PCB_TRACK* other = static_cast<PCB_TRACK*>( aItem );
  388. if( track->IsPointOnEnds( other->GetStart() )
  389. && track->IsPointOnEnds( other->GetEnd() )
  390. && track->GetWidth() == other->GetWidth()
  391. && track->GetLayer() == other->GetLayer() )
  392. {
  393. auto item = std::make_shared<CLEANUP_ITEM>( CLEANUP_DUPLICATE_TRACK );
  394. item->SetItems( track );
  395. m_itemsList->push_back( std::move( item ) );
  396. track->SetFlags( IS_DELETED );
  397. toRemove.insert( track );
  398. }
  399. return true;
  400. } );
  401. track->SetFlags( SKIP_STRUCT );
  402. }
  403. }
  404. if( !m_dryRun )
  405. removeItems( toRemove );
  406. auto mergeSegments = [&]( std::shared_ptr<CN_CONNECTIVITY_ALGO> connectivity ) -> bool
  407. {
  408. auto track_loop = [&]( int aStart, int aEnd ) -> std::vector<std::pair<PCB_TRACK*, PCB_TRACK*>>
  409. {
  410. std::vector<std::pair<PCB_TRACK*, PCB_TRACK*>> tracks;
  411. for( int ii = aStart; ii < aEnd; ++ii )
  412. {
  413. PCB_TRACK* segment = m_brd->Tracks()[ii];
  414. // one can merge only collinear segments, not vias or arcs.
  415. if( segment->Type() != PCB_TRACE_T )
  416. continue;
  417. if( segment->HasFlag( IS_DELETED ) ) // already taken into account
  418. continue;
  419. if( filterItem( segment ) )
  420. continue;
  421. // for each end of the segment:
  422. for( CN_ITEM* citem : connectivity->ItemEntry( segment ).GetItems() )
  423. {
  424. // Do not merge an end which has different width tracks attached -- it's a
  425. // common use-case for necking-down a track between pads.
  426. std::vector<PCB_TRACK*> sameWidthCandidates;
  427. std::vector<PCB_TRACK*> differentWidthCandidates;
  428. for( CN_ITEM* connected : citem->ConnectedItems() )
  429. {
  430. if( !connected->Valid() )
  431. continue;
  432. BOARD_CONNECTED_ITEM* candidate = connected->Parent();
  433. if( candidate->Type() == PCB_TRACE_T && !candidate->HasFlag( IS_DELETED )
  434. && !filterItem( candidate ) )
  435. {
  436. PCB_TRACK* candidateSegment = static_cast<PCB_TRACK*>( candidate );
  437. if( candidateSegment->GetWidth() == segment->GetWidth() )
  438. {
  439. sameWidthCandidates.push_back( candidateSegment );
  440. }
  441. else
  442. {
  443. differentWidthCandidates.push_back( candidateSegment );
  444. break;
  445. }
  446. }
  447. }
  448. if( !differentWidthCandidates.empty() )
  449. continue;
  450. for( PCB_TRACK* candidate : sameWidthCandidates )
  451. {
  452. if( candidate < segment ) // avoid duplicate merges
  453. continue;
  454. if( segment->ApproxCollinear( *candidate )
  455. && testMergeCollinearSegments( segment, candidate ) )
  456. {
  457. tracks.emplace_back( segment, candidate );
  458. break;
  459. }
  460. }
  461. }
  462. }
  463. return tracks;
  464. };
  465. // The idea here is to parallelize the loop that does not modify the connectivity
  466. // and extract all of the pairs of segments that might be merged. Then, perform
  467. // the actual merge in the main loop.
  468. thread_pool& tp = GetKiCadThreadPool();
  469. auto merge_returns = tp.parallelize_loop( 0, m_brd->Tracks().size(), track_loop );
  470. bool retval = false;
  471. for( size_t ii = 0; ii < merge_returns.size(); ++ii )
  472. {
  473. std::future<std::vector<std::pair<PCB_TRACK*, PCB_TRACK*>>>& ret = merge_returns[ii];
  474. if( ret.valid() )
  475. {
  476. for( auto& [seg1, seg2] : ret.get() )
  477. {
  478. retval = true;
  479. if( seg1->HasFlag( IS_DELETED ) || seg2->HasFlag( IS_DELETED ) )
  480. continue;
  481. mergeCollinearSegments( seg1, seg2 );
  482. }
  483. }
  484. }
  485. return retval;
  486. };
  487. if( aMergeSegments )
  488. {
  489. do
  490. {
  491. while( !m_brd->BuildConnectivity() )
  492. wxSafeYield();
  493. std::lock_guard lock( m_mutex );
  494. m_connectedItemsCache.clear();
  495. } while( mergeSegments( m_brd->GetConnectivity()->GetConnectivityAlgo() ) );
  496. }
  497. for( PCB_TRACK* track : m_brd->Tracks() )
  498. track->ClearFlags( IS_DELETED | SKIP_STRUCT );
  499. }
  500. const std::vector<BOARD_CONNECTED_ITEM*>& TRACKS_CLEANER::getConnectedItems( PCB_TRACK* aTrack )
  501. {
  502. const std::shared_ptr<CONNECTIVITY_DATA>& connectivity = m_brd->GetConnectivity();
  503. std::lock_guard lock( m_mutex );
  504. if( !m_connectedItemsCache.contains( aTrack ) )
  505. m_connectedItemsCache[aTrack] = connectivity->GetConnectedItems( aTrack );
  506. return m_connectedItemsCache.at( aTrack );
  507. }
  508. bool TRACKS_CLEANER::testMergeCollinearSegments( PCB_TRACK* aSeg1, PCB_TRACK* aSeg2, PCB_TRACK* aDummySeg )
  509. {
  510. if( aSeg1->IsLocked() || aSeg2->IsLocked() )
  511. return false;
  512. // Collect the unique points where the two tracks are connected to other items
  513. const unsigned p1s = 1 << 0;
  514. const unsigned p1e = 1 << 1;
  515. const unsigned p2s = 1 << 2;
  516. const unsigned p2e = 1 << 3;
  517. std::vector<VECTOR2I> pts = { aSeg1->GetStart(), aSeg1->GetEnd(), aSeg2->GetStart(), aSeg2->GetEnd() };
  518. std::atomic<unsigned> flags = 0;
  519. auto collectPtsSeg1 =
  520. [&]( BOARD_CONNECTED_ITEM* citem )
  521. {
  522. if( std::popcount( flags.load() ) > 2 )
  523. return;
  524. if( citem->Type() == PCB_TRACE_T || citem->Type() == PCB_ARC_T
  525. || citem->Type() == PCB_VIA_T )
  526. {
  527. PCB_TRACK* track = static_cast<PCB_TRACK*>( citem );
  528. if( track->IsPointOnEnds( aSeg1->GetStart() ) )
  529. flags |= p1s;
  530. if( track->IsPointOnEnds( aSeg1->GetEnd() ) )
  531. flags |= p1e;
  532. }
  533. else
  534. {
  535. if( !( flags & p1s ) && citem->HitTest( aSeg1->GetStart(), ( aSeg1->GetWidth() + 1 ) / 2 ) )
  536. flags |= p1s;
  537. if( !( flags & p1e ) && citem->HitTest( aSeg1->GetEnd(), ( aSeg1->GetWidth() + 1 ) / 2 ) )
  538. flags |= p1e;
  539. }
  540. };
  541. auto collectPtsSeg2 =
  542. [&]( BOARD_CONNECTED_ITEM* citem )
  543. {
  544. if( std::popcount( flags.load() ) > 2 )
  545. return;
  546. if( citem->Type() == PCB_TRACE_T || citem->Type() == PCB_ARC_T
  547. || citem->Type() == PCB_VIA_T )
  548. {
  549. PCB_TRACK* track = static_cast<PCB_TRACK*>( citem );
  550. if( track->IsPointOnEnds( aSeg2->GetStart() ) )
  551. flags |= p2s;
  552. if( track->IsPointOnEnds( aSeg2->GetEnd() ) )
  553. flags |= p2e;
  554. }
  555. else
  556. {
  557. if( !( flags & p2s ) && citem->HitTest( aSeg2->GetStart(), ( aSeg2->GetWidth() + 1 ) / 2 ) )
  558. flags |= p2s;
  559. if( !( flags & p2e ) && citem->HitTest( aSeg2->GetEnd(), ( aSeg2->GetWidth() + 1 ) / 2 ) )
  560. flags |= p2e;
  561. }
  562. };
  563. for( BOARD_CONNECTED_ITEM* item : getConnectedItems( aSeg1 ) )
  564. {
  565. if( item->HasFlag( IS_DELETED ) )
  566. continue;
  567. if( item != aSeg1 && item != aSeg2 )
  568. collectPtsSeg1( item );
  569. }
  570. for( BOARD_CONNECTED_ITEM* item : getConnectedItems( aSeg2 ) )
  571. {
  572. if( item->HasFlag( IS_DELETED ) )
  573. continue;
  574. if( item != aSeg1 && item != aSeg2 )
  575. collectPtsSeg2( item );
  576. }
  577. // This means there is a node in the center
  578. if( std::popcount( flags.load() ) > 2 )
  579. return false;
  580. // Verify the removed point after merging is not a node.
  581. // If it is a node (i.e. if more than one other item is connected, the segments cannot be merged
  582. PCB_TRACK dummy_seg( *aSeg1 );
  583. if( !aDummySeg )
  584. aDummySeg = &dummy_seg;
  585. // Calculate the new ends of the segment to merge, and store them to dummy_seg:
  586. int min_x = std::min( aSeg1->GetStart().x,
  587. std::min( aSeg1->GetEnd().x, std::min( aSeg2->GetStart().x, aSeg2->GetEnd().x ) ) );
  588. int min_y = std::min( aSeg1->GetStart().y,
  589. std::min( aSeg1->GetEnd().y, std::min( aSeg2->GetStart().y, aSeg2->GetEnd().y ) ) );
  590. int max_x = std::max( aSeg1->GetStart().x,
  591. std::max( aSeg1->GetEnd().x, std::max( aSeg2->GetStart().x, aSeg2->GetEnd().x ) ) );
  592. int max_y = std::max( aSeg1->GetStart().y,
  593. std::max( aSeg1->GetEnd().y, std::max( aSeg2->GetStart().y, aSeg2->GetEnd().y ) ) );
  594. if( ( aSeg1->GetStart().x > aSeg1->GetEnd().x )
  595. == ( aSeg1->GetStart().y > aSeg1->GetEnd().y ) )
  596. {
  597. aDummySeg->SetStart( VECTOR2I( min_x, min_y ) );
  598. aDummySeg->SetEnd( VECTOR2I( max_x, max_y ) );
  599. }
  600. else
  601. {
  602. aDummySeg->SetStart( VECTOR2I( min_x, max_y ) );
  603. aDummySeg->SetEnd( VECTOR2I( max_x, min_y ) );
  604. }
  605. // The new ends of the segment must be connected to all of the same points as the original
  606. // segments. If not, the segments cannot be merged.
  607. for( unsigned i = 0; i < 4; ++i )
  608. {
  609. if( ( flags & ( 1 << i ) ) && !aDummySeg->IsPointOnEnds( pts[i] ) )
  610. return false;
  611. }
  612. // Now find the removed end(s) and stop merging if it is a node:
  613. return !testTrackEndpointIsNode( aSeg1, aDummySeg->IsPointOnEnds( aSeg1->GetStart() ),
  614. aDummySeg->IsPointOnEnds( aSeg1->GetEnd() ) );
  615. }
  616. bool TRACKS_CLEANER::mergeCollinearSegments( PCB_TRACK* aSeg1, PCB_TRACK* aSeg2 )
  617. {
  618. PCB_TRACK dummy_seg( *aSeg1 );
  619. if( !testMergeCollinearSegments( aSeg1, aSeg2, &dummy_seg ) )
  620. return false;
  621. std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_MERGE_TRACKS );
  622. item->SetItems( aSeg1, aSeg2 );
  623. m_itemsList->push_back( std::move( item ) );
  624. aSeg2->SetFlags( IS_DELETED );
  625. if( !m_dryRun )
  626. {
  627. m_commit.Modify( aSeg1 );
  628. *aSeg1 = dummy_seg;
  629. m_brd->GetConnectivity()->Update( aSeg1 );
  630. // Merge successful, seg2 has to go away
  631. m_brd->Remove( aSeg2 );
  632. m_commit.Removed( aSeg2 );
  633. }
  634. return true;
  635. }
  636. void TRACKS_CLEANER::removeItems( std::set<BOARD_ITEM*>& aItems )
  637. {
  638. for( BOARD_ITEM* item : aItems )
  639. {
  640. m_brd->Remove( item );
  641. m_commit.Removed( item );
  642. }
  643. }