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.

1127 lines
29 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016-2018 CERN
  5. * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <connectivity_algo.h>
  25. #include <widgets/progress_reporter.h>
  26. #include <thread>
  27. #include <mutex>
  28. #ifdef PROFILE
  29. #include <profile.h>
  30. #endif
  31. #ifdef USE_OPENMP
  32. #include <omp.h>
  33. #endif /* USE_OPENMP */
  34. using namespace std::placeholders;
  35. bool operator<( const CN_ANCHOR_PTR& a, const CN_ANCHOR_PTR& b )
  36. {
  37. if( a->Pos().x == b->Pos().x )
  38. return a->Pos().y < b->Pos().y;
  39. else
  40. return a->Pos().x < b->Pos().x;
  41. }
  42. bool CN_ANCHOR::IsDirty() const
  43. {
  44. return m_item->Dirty();
  45. }
  46. CN_CLUSTER::CN_CLUSTER()
  47. {
  48. m_items.reserve( 64 );
  49. m_originPad = nullptr;
  50. m_originNet = -1;
  51. m_conflicting = false;
  52. }
  53. CN_CLUSTER::~CN_CLUSTER()
  54. {
  55. }
  56. wxString CN_CLUSTER::OriginNetName() const
  57. {
  58. if( !m_originPad || !m_originPad->Valid() )
  59. return "<none>";
  60. else
  61. return m_originPad->Parent()->GetNetname();
  62. }
  63. bool CN_CLUSTER::Contains( const CN_ITEM* aItem )
  64. {
  65. return std::find( m_items.begin(), m_items.end(), aItem ) != m_items.end();
  66. }
  67. bool CN_CLUSTER::Contains( const BOARD_CONNECTED_ITEM* aItem )
  68. {
  69. for( auto item : m_items )
  70. {
  71. if( item->Valid() && item->Parent() == aItem )
  72. return true;
  73. }
  74. return false;
  75. }
  76. void CN_ITEM::Dump()
  77. {
  78. printf(" valid: %d, connected: \n", !!Valid());
  79. for( auto i : m_connected )
  80. {
  81. TRACK* t = static_cast<TRACK*>( i->Parent() );
  82. printf( " - %p %d\n", t, t->Type() );
  83. }
  84. }
  85. void CN_CLUSTER::Dump()
  86. {
  87. for( auto item : m_items )
  88. {
  89. wxLogTrace( "CN", " - item : %p bitem : %p type : %d inet %s\n", item, item->Parent(),
  90. item->Parent()->Type(), (const char*) item->Parent()->GetNetname().c_str() );
  91. printf( "- item : %p bitem : %p type : %d inet %s\n", item, item->Parent(),
  92. item->Parent()->Type(), (const char*) item->Parent()->GetNetname().c_str() );
  93. item->Dump();
  94. }
  95. }
  96. void CN_CLUSTER::Add( CN_ITEM* item )
  97. {
  98. m_items.push_back( item );
  99. if( m_originNet < 0 )
  100. {
  101. m_originNet = item->Net();
  102. }
  103. if( item->Parent()->Type() == PCB_PAD_T )
  104. {
  105. if( !m_originPad )
  106. {
  107. m_originPad = item;
  108. m_originNet = item->Net();
  109. }
  110. if( m_originPad && item->Net() != m_originNet )
  111. {
  112. m_conflicting = true;
  113. }
  114. }
  115. }
  116. CN_CONNECTIVITY_ALGO::CN_CONNECTIVITY_ALGO()
  117. {
  118. }
  119. CN_CONNECTIVITY_ALGO::~CN_CONNECTIVITY_ALGO()
  120. {
  121. Clear();
  122. }
  123. bool CN_CONNECTIVITY_ALGO::Remove( BOARD_ITEM* aItem )
  124. {
  125. markItemNetAsDirty( aItem );
  126. switch( aItem->Type() )
  127. {
  128. case PCB_MODULE_T:
  129. for( auto pad : static_cast<MODULE*>( aItem ) -> Pads() )
  130. {
  131. m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( pad ) ].MarkItemsAsInvalid();
  132. m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( pad ) );
  133. }
  134. m_padList.SetDirty( true );
  135. break;
  136. case PCB_PAD_T:
  137. m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
  138. m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
  139. m_padList.SetDirty( true );
  140. break;
  141. case PCB_TRACE_T:
  142. m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
  143. m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
  144. m_trackList.SetDirty( true );
  145. break;
  146. case PCB_VIA_T:
  147. m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
  148. m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
  149. m_viaList.SetDirty( true );
  150. break;
  151. case PCB_ZONE_AREA_T:
  152. case PCB_ZONE_T:
  153. {
  154. m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
  155. m_itemMap.erase ( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
  156. m_zoneList.SetDirty( true );
  157. break;
  158. }
  159. default:
  160. return false;
  161. }
  162. return true;
  163. }
  164. void CN_CONNECTIVITY_ALGO::markItemNetAsDirty( const BOARD_ITEM* aItem )
  165. {
  166. if( aItem->IsConnected() )
  167. {
  168. auto citem = static_cast<const BOARD_CONNECTED_ITEM*>( aItem );
  169. MarkNetAsDirty( citem->GetNetCode() );
  170. }
  171. else
  172. {
  173. if( aItem->Type() == PCB_MODULE_T )
  174. {
  175. auto mod = static_cast <const MODULE*>( aItem );
  176. for( D_PAD* pad = mod->PadsList(); pad; pad = pad->Next() )
  177. MarkNetAsDirty( pad->GetNetCode() );
  178. }
  179. }
  180. }
  181. bool CN_CONNECTIVITY_ALGO::Add( BOARD_ITEM* aItem )
  182. {
  183. markItemNetAsDirty ( aItem );
  184. switch( aItem->Type() )
  185. {
  186. case PCB_NETINFO_T:
  187. {
  188. MarkNetAsDirty( static_cast<NETINFO_ITEM*>( aItem )->GetNet() );
  189. break;
  190. }
  191. case PCB_MODULE_T:
  192. for( auto pad : static_cast<MODULE*>( aItem ) -> Pads() )
  193. {
  194. if( m_itemMap.find( pad ) != m_itemMap.end() )
  195. return false;
  196. add( m_padList, pad );
  197. }
  198. break;
  199. case PCB_PAD_T:
  200. if( m_itemMap.find ( static_cast<D_PAD*>( aItem ) ) != m_itemMap.end() )
  201. return false;
  202. add( m_padList, static_cast<D_PAD*>( aItem ) );
  203. break;
  204. case PCB_TRACE_T:
  205. {
  206. if( m_itemMap.find( static_cast<TRACK*>( aItem ) ) != m_itemMap.end() )
  207. return false;
  208. add( m_trackList, static_cast<TRACK*>( aItem ) );
  209. break;
  210. }
  211. case PCB_VIA_T:
  212. if( m_itemMap.find( static_cast<VIA*>( aItem ) ) != m_itemMap.end() )
  213. return false;
  214. add( m_viaList, static_cast<VIA*>( aItem ) );
  215. break;
  216. case PCB_ZONE_AREA_T:
  217. case PCB_ZONE_T:
  218. {
  219. auto zone = static_cast<ZONE_CONTAINER*>( aItem );
  220. if( m_itemMap.find( static_cast<ZONE_CONTAINER*>( aItem ) ) != m_itemMap.end() )
  221. return false;
  222. m_itemMap[zone] = ITEM_MAP_ENTRY();
  223. for( auto zitem : m_zoneList.Add( zone ) )
  224. m_itemMap[zone].Link(zitem);
  225. break;
  226. }
  227. default:
  228. return false;
  229. }
  230. return true;
  231. }
  232. void CN_CONNECTIVITY_ALGO::searchConnections( bool aIncludeZones )
  233. {
  234. std::mutex cnListLock;
  235. int totalDirtyCount = 0;
  236. if( m_lastSearchWithZones != aIncludeZones )
  237. {
  238. m_padList.MarkAllAsDirty();
  239. m_viaList.MarkAllAsDirty();
  240. m_trackList.MarkAllAsDirty();
  241. m_zoneList.MarkAllAsDirty();
  242. }
  243. m_lastSearchWithZones = aIncludeZones;
  244. auto checkForConnection = [ &cnListLock ] ( const CN_ANCHOR_PTR point, CN_ITEM* aRefItem, int aMaxDist = 0 )
  245. {
  246. const auto parent = aRefItem->Parent();
  247. assert( point->Item() );
  248. assert( point->Item()->Parent() );
  249. assert( aRefItem->Parent() );
  250. if( !point->Item()->Valid() )
  251. return;
  252. if( !aRefItem->Valid() )
  253. return;
  254. if( parent == point->Item()->Parent() )
  255. return;
  256. if( !( parent->GetLayerSet() &
  257. point->Item()->Parent()->GetLayerSet() ).any() )
  258. return;
  259. switch( parent->Type() )
  260. {
  261. case PCB_PAD_T:
  262. case PCB_VIA_T:
  263. if( parent->HitTest( wxPoint( point->Pos().x, point->Pos().y ) ) )
  264. {
  265. std::lock_guard<std::mutex> lock( cnListLock );
  266. CN_ITEM::Connect( aRefItem, point->Item() );
  267. }
  268. break;
  269. case PCB_TRACE_T:
  270. {
  271. const auto track = static_cast<TRACK*> ( parent );
  272. const VECTOR2I d_start( VECTOR2I( track->GetStart() ) - point->Pos() );
  273. const VECTOR2I d_end( VECTOR2I( track->GetEnd() ) - point->Pos() );
  274. if( d_start.EuclideanNorm() < aMaxDist
  275. || d_end.EuclideanNorm() < aMaxDist )
  276. {
  277. std::lock_guard<std::mutex> lock( cnListLock );
  278. CN_ITEM::Connect( aRefItem, point->Item() );
  279. }
  280. break;
  281. }
  282. case PCB_ZONE_T:
  283. case PCB_ZONE_AREA_T:
  284. {
  285. const auto zone = static_cast<ZONE_CONTAINER*> ( parent );
  286. auto zoneItem = static_cast<CN_ZONE*> ( aRefItem );
  287. if( point->Item()->Net() != parent->GetNetCode() )
  288. return;
  289. if( !( zone->GetLayerSet() &
  290. point->Item()->Parent()->GetLayerSet() ).any() )
  291. return;
  292. if( zoneItem->ContainsAnchor( point ) )
  293. {
  294. std::lock_guard<std::mutex> lock( cnListLock );
  295. CN_ITEM::Connect( zoneItem, point->Item() );
  296. }
  297. break;
  298. }
  299. default :
  300. assert( false );
  301. }
  302. };
  303. auto checkInterZoneConnection = [ &cnListLock ] ( CN_ZONE* testedZone, CN_ZONE* aRefZone )
  304. {
  305. const auto parentZone = static_cast<const ZONE_CONTAINER*>( aRefZone->Parent() );
  306. if( testedZone->Parent()->Type () != PCB_ZONE_AREA_T )
  307. return;
  308. if( testedZone == aRefZone )
  309. return;
  310. if( testedZone->Parent() == aRefZone->Parent() )
  311. return;
  312. if( testedZone->Net() != parentZone->GetNetCode() )
  313. return; // we only test zones belonging to the same net
  314. if( !( testedZone->Parent()->GetLayerSet() & parentZone->GetLayerSet() ).any() )
  315. return; // and on same layer
  316. const auto& outline = parentZone->GetFilledPolysList().COutline( aRefZone->SubpolyIndex() );
  317. for( int i = 0; i < outline.PointCount(); i++ )
  318. {
  319. if( testedZone->ContainsPoint( outline.CPoint( i ) ) )
  320. {
  321. std::lock_guard<std::mutex> lock( cnListLock );
  322. CN_ITEM::Connect( aRefZone, testedZone );
  323. return;
  324. }
  325. }
  326. const auto testedZoneParent = static_cast<const ZONE_CONTAINER*>( testedZone->Parent() );
  327. const auto& outline2 = testedZoneParent->GetFilledPolysList().COutline( testedZone->SubpolyIndex() );
  328. for( int i = 0; i < outline2.PointCount(); i++ )
  329. {
  330. if( aRefZone->ContainsPoint( outline2.CPoint( i ) ) )
  331. {
  332. std::lock_guard<std::mutex> lock( cnListLock );
  333. CN_ITEM::Connect( aRefZone, testedZone );
  334. return;
  335. }
  336. }
  337. };
  338. #ifdef CONNECTIVITY_DEBUG
  339. printf("Search start\n");
  340. #endif
  341. std::vector<CN_ITEM*> garbage;
  342. garbage.reserve( 1024 );
  343. m_padList.RemoveInvalidItems( garbage );
  344. m_viaList.RemoveInvalidItems( garbage );
  345. m_trackList.RemoveInvalidItems( garbage );
  346. m_zoneList.RemoveInvalidItems( garbage );
  347. for( auto item : garbage )
  348. delete item;
  349. //auto all = allItemsInBoard();
  350. #ifdef CONNECTIVITY_DEBUG
  351. for( auto item : m_padList )
  352. if( all.find( item->Parent() ) == all.end() ) { printf("Failing pad : %p\n", item->Parent() ); assert ( false ); }
  353. for( auto item : m_viaList )
  354. if( all.find( item->Parent() ) == all.end() ) { printf("Failing via : %p\n", item->Parent() ); assert ( false ); }
  355. for( auto item : m_trackList )
  356. if( all.find( item->Parent() ) == all.end() ) { printf("Failing track : %p\n", item->Parent() ); assert ( false ); }
  357. for( auto item : m_zoneList )
  358. if( all.find( item->Parent() ) == all.end() ) { printf("Failing zome : %p\n", item->Parent() ); assert ( false ); }
  359. #endif
  360. #ifdef PROFILE
  361. PROF_COUNTER search_cnt( "search-connections" );
  362. PROF_COUNTER search_basic( "search-basic" );
  363. #endif
  364. if( m_padList.IsDirty() || m_trackList.IsDirty() || m_viaList.IsDirty() )
  365. {
  366. totalDirtyCount++;
  367. for( auto padItem : m_padList )
  368. {
  369. auto pad = static_cast<D_PAD*> ( padItem->Parent() );
  370. auto searchPads = std::bind( checkForConnection, _1, padItem );
  371. m_padList.FindNearby( pad->ShapePos(), pad->GetBoundingRadius(), searchPads );
  372. m_trackList.FindNearby( pad->ShapePos(), pad->GetBoundingRadius(), searchPads );
  373. m_viaList.FindNearby( pad->ShapePos(), pad->GetBoundingRadius(), searchPads );
  374. }
  375. for( auto& trackItem : m_trackList )
  376. {
  377. auto track = static_cast<TRACK*> ( trackItem->Parent() );
  378. int dist_max = track->GetWidth() / 2;
  379. auto searchTracks = std::bind( checkForConnection, _1, trackItem, dist_max );
  380. m_trackList.FindNearby( track->GetStart(), dist_max, searchTracks );
  381. m_trackList.FindNearby( track->GetEnd(), dist_max, searchTracks );
  382. }
  383. for( auto& viaItem : m_viaList )
  384. {
  385. auto via = static_cast<VIA*> ( viaItem->Parent() );
  386. int dist_max = via->GetWidth() / 2;
  387. auto searchVias = std::bind( checkForConnection, _1, viaItem, dist_max );
  388. totalDirtyCount++;
  389. m_viaList.FindNearby( via->GetStart(), dist_max, searchVias );
  390. m_trackList.FindNearby( via->GetStart(), dist_max, searchVias );
  391. }
  392. }
  393. #ifdef PROFILE
  394. search_basic.Show();
  395. #endif
  396. if( aIncludeZones )
  397. {
  398. int cnt = 0;
  399. if( m_progressReporter )
  400. {
  401. m_progressReporter->SetMaxProgress( m_zoneList.Size() );
  402. }
  403. #ifdef USE_OPENMP
  404. // launch at least two threads, one to compute, second to update UI
  405. #pragma omp parallel num_threads( std::max( omp_get_num_procs(), 2 ) )
  406. #endif
  407. {
  408. #ifdef USE_OPENMP
  409. #pragma omp master
  410. if (m_progressReporter)
  411. {
  412. m_progressReporter->KeepRefreshing( true );
  413. }
  414. #endif
  415. #ifdef USE_OPENMP
  416. #pragma omp for schedule(dynamic)
  417. #endif
  418. for(int i = 0; i < m_zoneList.Size(); i++ )
  419. {
  420. auto item = m_zoneList[i];
  421. auto zoneItem = static_cast<CN_ZONE *> (item);
  422. auto searchZones = std::bind( checkForConnection, _1, zoneItem );
  423. if( zoneItem->Dirty() || m_padList.IsDirty() || m_trackList.IsDirty() || m_viaList.IsDirty() )
  424. {
  425. totalDirtyCount++;
  426. m_viaList.FindNearby( zoneItem->BBox(), searchZones );
  427. m_trackList.FindNearby( zoneItem->BBox(), searchZones );
  428. m_padList.FindNearby( zoneItem->BBox(), searchZones );
  429. m_zoneList.FindNearbyZones( zoneItem->BBox(), std::bind( checkInterZoneConnection, _1, zoneItem ) );
  430. }
  431. {
  432. std::lock_guard<std::mutex> lock( cnListLock );
  433. cnt++;
  434. if (m_progressReporter)
  435. {
  436. m_progressReporter->AdvanceProgress();
  437. }
  438. }
  439. }
  440. }
  441. m_zoneList.ClearDirtyFlags();
  442. }
  443. m_padList.ClearDirtyFlags();
  444. m_viaList.ClearDirtyFlags();
  445. m_trackList.ClearDirtyFlags();
  446. #ifdef CONNECTIVITY_DEBUG
  447. printf("Search end\n");
  448. #endif
  449. #ifdef PROFILE
  450. search_cnt.Show();
  451. #endif
  452. }
  453. void CN_ITEM::RemoveInvalidRefs()
  454. {
  455. auto lastConn = std::remove_if(m_connected.begin(), m_connected.end(), [] ( CN_ITEM * item) {
  456. return !item->Valid();
  457. } );
  458. m_connected.resize( lastConn - m_connected.begin() );
  459. }
  460. void CN_LIST::RemoveInvalidItems( std::vector<CN_ITEM*>& aGarbage )
  461. {
  462. auto lastAnchor = std::remove_if(m_anchors.begin(), m_anchors.end(),
  463. [] ( const CN_ANCHOR_PTR anchor ) {
  464. return !anchor->Valid();
  465. } );
  466. m_anchors.resize( lastAnchor - m_anchors.begin() );
  467. auto lastItem = std::remove_if(m_items.begin(), m_items.end(), [&aGarbage] ( CN_ITEM* item ) {
  468. if( !item->Valid() )
  469. {
  470. aGarbage.push_back ( item );
  471. return true;
  472. }
  473. return false;
  474. } );
  475. m_items.resize( lastItem - m_items.begin() );
  476. // fixme: mem leaks
  477. for( auto item : m_items )
  478. item->RemoveInvalidRefs();
  479. }
  480. bool CN_CONNECTIVITY_ALGO::isDirty() const
  481. {
  482. return m_viaList.IsDirty() || m_trackList.IsDirty() || m_zoneList.IsDirty() || m_padList.IsDirty();
  483. }
  484. const CN_CONNECTIVITY_ALGO::CLUSTERS CN_CONNECTIVITY_ALGO::SearchClusters( CLUSTER_SEARCH_MODE aMode )
  485. {
  486. constexpr KICAD_T types[] = { PCB_TRACE_T, PCB_PAD_T, PCB_VIA_T, PCB_ZONE_AREA_T, PCB_MODULE_T, EOT };
  487. return SearchClusters( aMode, types, -1 );
  488. }
  489. const CN_CONNECTIVITY_ALGO::CLUSTERS CN_CONNECTIVITY_ALGO::SearchClusters( CLUSTER_SEARCH_MODE aMode,
  490. const KICAD_T aTypes[], int aSingleNet )
  491. {
  492. bool includeZones = ( aMode != CSM_PROPAGATE );
  493. bool withinAnyNet = ( aMode != CSM_PROPAGATE );
  494. std::deque<CN_ITEM*> Q;
  495. CN_ITEM* head = nullptr;
  496. CLUSTERS clusters;
  497. if( isDirty() )
  498. searchConnections( includeZones );
  499. auto addToSearchList = [&head, withinAnyNet, aSingleNet, aTypes] ( CN_ITEM *aItem )
  500. {
  501. if( withinAnyNet && aItem->Net() <= 0 )
  502. return;
  503. if( !aItem->Valid() )
  504. return;
  505. if( aSingleNet >=0 && aItem->Net() != aSingleNet )
  506. return;
  507. bool found = false;
  508. for( int i = 0; aTypes[i] != EOT; i++ )
  509. {
  510. if( aItem->Parent()->Type() == aTypes[i] )
  511. {
  512. found = true;
  513. break;
  514. }
  515. }
  516. if( !found )
  517. return;
  518. aItem->ListClear();
  519. aItem->SetVisited( false );
  520. if( !head )
  521. head = aItem;
  522. else
  523. head->ListInsert( aItem );
  524. };
  525. std::for_each( m_padList.begin(), m_padList.end(), addToSearchList );
  526. std::for_each( m_trackList.begin(), m_trackList.end(), addToSearchList );
  527. std::for_each( m_viaList.begin(), m_viaList.end(), addToSearchList );
  528. if( includeZones )
  529. {
  530. std::for_each( m_zoneList.begin(), m_zoneList.end(), addToSearchList );
  531. }
  532. while( head )
  533. {
  534. CN_CLUSTER_PTR cluster ( new CN_CLUSTER() );
  535. Q.clear();
  536. CN_ITEM* root = head;
  537. root->SetVisited ( true );
  538. head = root->ListRemove();
  539. Q.push_back( root );
  540. while( Q.size() )
  541. {
  542. CN_ITEM* current = Q.front();
  543. Q.pop_front();
  544. cluster->Add( current );
  545. for( auto n : current->ConnectedItems() )
  546. {
  547. if( withinAnyNet && n->Net() != root->Net() )
  548. continue;
  549. if( !n->Visited() && n->Valid() )
  550. {
  551. n->SetVisited( true );
  552. Q.push_back( n );
  553. head = n->ListRemove();
  554. }
  555. }
  556. }
  557. clusters.push_back( cluster );
  558. }
  559. std::sort( clusters.begin(), clusters.end(), []( CN_CLUSTER_PTR a, CN_CLUSTER_PTR b ) {
  560. return a->OriginNet() < b->OriginNet();
  561. } );
  562. #ifdef CONNECTIVITY_DEBUG
  563. printf("Active clusters: %d\n", clusters.size() );
  564. for( auto cl : clusters )
  565. {
  566. printf( "Net %d\n", cl->OriginNet() );
  567. cl->Dump();
  568. }
  569. #endif
  570. return clusters;
  571. }
  572. void CN_CONNECTIVITY_ALGO::Build( BOARD* aBoard )
  573. {
  574. for( int i = 0; i<aBoard->GetAreaCount(); i++ )
  575. {
  576. auto zone = aBoard->GetArea( i );
  577. Add( zone );
  578. }
  579. for( auto tv : aBoard->Tracks() )
  580. Add( tv );
  581. for( auto mod : aBoard->Modules() )
  582. {
  583. for( auto pad : mod->Pads() )
  584. Add( pad );
  585. }
  586. /*wxLogTrace( "CN", "zones : %lu, pads : %lu vias : %lu tracks : %lu\n",
  587. m_zoneList.Size(), m_padList.Size(),
  588. m_viaList.Size(), m_trackList.Size() );*/
  589. }
  590. void CN_CONNECTIVITY_ALGO::Build( const std::vector<BOARD_ITEM*>& aItems )
  591. {
  592. for( auto item : aItems )
  593. {
  594. switch( item->Type() )
  595. {
  596. case PCB_TRACE_T:
  597. case PCB_VIA_T:
  598. case PCB_ZONE_T:
  599. case PCB_PAD_T:
  600. Add( item );
  601. break;
  602. case PCB_MODULE_T:
  603. {
  604. for( auto pad : static_cast<MODULE*>( item )->Pads() )
  605. {
  606. Add( pad );
  607. }
  608. break;
  609. }
  610. default:
  611. break;
  612. }
  613. }
  614. }
  615. void CN_CONNECTIVITY_ALGO::propagateConnections()
  616. {
  617. for( const auto& cluster : m_connClusters )
  618. {
  619. if( cluster->IsConflicting() )
  620. {
  621. wxLogTrace( "CN", "Conflicting nets in cluster %p\n", cluster.get() );
  622. }
  623. else if( cluster->IsOrphaned() )
  624. {
  625. wxLogTrace( "CN", "Skipping orphaned cluster %p [net: %s]\n", cluster.get(),
  626. (const char*) cluster->OriginNetName().c_str() );
  627. }
  628. else if( cluster->HasValidNet() )
  629. {
  630. // normal cluster: just propagate from the pads
  631. int n_changed = 0;
  632. for( auto item : *cluster )
  633. {
  634. if( item->CanChangeNet() )
  635. {
  636. if( item->Valid() && item->Parent()->GetNetCode() != cluster->OriginNet() )
  637. {
  638. MarkNetAsDirty( item->Parent()->GetNetCode() );
  639. MarkNetAsDirty( cluster->OriginNet() );
  640. item->Parent()->SetNetCode( cluster->OriginNet() );
  641. n_changed++;
  642. }
  643. }
  644. }
  645. if( n_changed )
  646. wxLogTrace( "CN", "Cluster %p : net : %d %s\n", cluster.get(),
  647. cluster->OriginNet(), (const char*) cluster->OriginNetName().c_str() );
  648. else
  649. wxLogTrace( "CN", "Cluster %p : nothing to propagate\n", cluster.get() );
  650. }
  651. else
  652. {
  653. wxLogTrace( "CN", "Cluster %p : connected to unused net\n", cluster.get() );
  654. }
  655. }
  656. }
  657. void CN_CONNECTIVITY_ALGO::PropagateNets()
  658. {
  659. //searchConnections( false );
  660. m_connClusters = SearchClusters( CSM_PROPAGATE );
  661. propagateConnections();
  662. }
  663. void CN_CONNECTIVITY_ALGO::FindIsolatedCopperIslands( ZONE_CONTAINER* aZone, std::vector<int>& aIslands )
  664. {
  665. if( aZone->GetFilledPolysList().IsEmpty() )
  666. return;
  667. aIslands.clear();
  668. Remove( aZone );
  669. Add( aZone );
  670. m_connClusters = SearchClusters( CSM_CONNECTIVITY_CHECK );
  671. for( const auto& cluster : m_connClusters )
  672. {
  673. if( cluster->Contains( aZone ) && cluster->IsOrphaned() )
  674. {
  675. for( auto z : *cluster )
  676. {
  677. if( z->Parent() == aZone )
  678. {
  679. aIslands.push_back( static_cast<CN_ZONE*>(z)->SubpolyIndex() );
  680. }
  681. }
  682. }
  683. }
  684. wxLogTrace( "CN", "Found %u isolated islands\n", (unsigned)aIslands.size() );
  685. }
  686. void CN_CONNECTIVITY_ALGO::FindIsolatedCopperIslands( std::vector<CN_ZONE_ISOLATED_ISLAND_LIST>& aZones )
  687. {
  688. for ( auto& z : aZones )
  689. {
  690. if( z.m_zone->GetFilledPolysList().IsEmpty() )
  691. continue;
  692. Remove( z.m_zone );
  693. Add( z.m_zone );
  694. }
  695. m_connClusters = SearchClusters( CSM_CONNECTIVITY_CHECK );
  696. for ( auto& zone : aZones )
  697. {
  698. if( zone.m_zone->GetFilledPolysList().IsEmpty() )
  699. continue;
  700. for( const auto& cluster : m_connClusters )
  701. {
  702. if( cluster->Contains( zone.m_zone ) && cluster->IsOrphaned() )
  703. {
  704. for( auto z : *cluster )
  705. {
  706. if( z->Parent() == zone.m_zone )
  707. {
  708. zone.m_islands.push_back( static_cast<CN_ZONE*>(z)->SubpolyIndex() );
  709. }
  710. }
  711. }
  712. }
  713. }
  714. }
  715. const CN_CONNECTIVITY_ALGO::CLUSTERS& CN_CONNECTIVITY_ALGO::GetClusters()
  716. {
  717. m_ratsnestClusters = SearchClusters( CSM_RATSNEST );
  718. return m_ratsnestClusters;
  719. }
  720. void CN_CONNECTIVITY_ALGO::MarkNetAsDirty( int aNet )
  721. {
  722. if( aNet < 0 )
  723. return;
  724. if( (int) m_dirtyNets.size() <= aNet )
  725. m_dirtyNets.resize( aNet + 1 );
  726. m_dirtyNets[aNet] = true;
  727. }
  728. int CN_ITEM::AnchorCount() const
  729. {
  730. if( !m_valid )
  731. return 0;
  732. return m_parent->Type() == PCB_TRACE_T ? 2 : 1;
  733. }
  734. const VECTOR2I CN_ITEM::GetAnchor( int n ) const
  735. {
  736. if( !m_valid )
  737. return VECTOR2I();
  738. switch( m_parent->Type() )
  739. {
  740. case PCB_PAD_T:
  741. return static_cast<const D_PAD*>( m_parent )->ShapePos();
  742. break;
  743. case PCB_TRACE_T:
  744. {
  745. auto tr = static_cast<const TRACK*>( m_parent );
  746. return ( n == 0 ? tr->GetStart() : tr->GetEnd() );
  747. break;
  748. }
  749. case PCB_VIA_T:
  750. return static_cast<const VIA*>( m_parent )->GetStart();
  751. default:
  752. assert( false );
  753. return VECTOR2I();
  754. }
  755. }
  756. int CN_ZONE::AnchorCount() const
  757. {
  758. if( !Valid() )
  759. return 0;
  760. const auto zone = static_cast<const ZONE_CONTAINER*>( Parent() );
  761. const auto& outline = zone->GetFilledPolysList().COutline( m_subpolyIndex );
  762. return outline.PointCount() ? 1 : 0;
  763. }
  764. const VECTOR2I CN_ZONE::GetAnchor( int n ) const
  765. {
  766. if( !Valid() )
  767. return VECTOR2I();
  768. const auto zone = static_cast<const ZONE_CONTAINER*> ( Parent() );
  769. const auto& outline = zone->GetFilledPolysList().COutline( m_subpolyIndex );
  770. return outline.CPoint( 0 );
  771. }
  772. int CN_ITEM::Net() const
  773. {
  774. if( !m_parent || !m_valid )
  775. return -1;
  776. return m_parent->GetNetCode();
  777. }
  778. BOARD_CONNECTED_ITEM* CN_ANCHOR::Parent() const
  779. {
  780. assert( m_item->Valid() );
  781. return m_item->Parent();
  782. }
  783. bool CN_ANCHOR::Valid() const
  784. {
  785. if( !m_item )
  786. return false;
  787. return m_item->Valid();
  788. }
  789. void CN_CONNECTIVITY_ALGO::Clear()
  790. {
  791. m_ratsnestClusters.clear();
  792. m_connClusters.clear();
  793. m_itemMap.clear();
  794. m_padList.Clear();
  795. m_trackList.Clear();
  796. m_viaList.Clear();
  797. m_zoneList.Clear();
  798. }
  799. void CN_CONNECTIVITY_ALGO::ForEachItem( const std::function<void( CN_ITEM& )>& aFunc )
  800. {
  801. for( auto item : m_padList )
  802. aFunc( *item );
  803. for( auto item : m_viaList )
  804. aFunc( *item );
  805. for( auto item : m_trackList )
  806. aFunc( *item );
  807. for( auto item : m_zoneList )
  808. aFunc( *item );
  809. }
  810. void CN_CONNECTIVITY_ALGO::ForEachAnchor( const std::function<void( CN_ANCHOR& )>& aFunc )
  811. {
  812. for( const auto& anchor : m_padList.Anchors() )
  813. aFunc( *anchor );
  814. for( const auto& anchor : m_viaList.Anchors() )
  815. aFunc( *anchor );
  816. for( const auto& anchor : m_trackList.Anchors() )
  817. aFunc( *anchor );
  818. for( const auto& anchor : m_zoneList.Anchors() )
  819. aFunc( *anchor );
  820. }
  821. bool CN_ANCHOR::IsDangling() const
  822. {
  823. if( !m_cluster )
  824. return true;
  825. // Calculate the item count connected to this anchor.
  826. // m_cluster groups all items connected, but they are not necessary connected
  827. // at this coordinate point (they are only candidates)
  828. BOARD_CONNECTED_ITEM* item_ref = Parent();
  829. LSET layers = item_ref->GetLayerSet() & LSET::AllCuMask();
  830. // the number of items connected to item_ref at ths anchor point
  831. int connected_items_count = 0;
  832. // the minimal number of items connected to item_ref
  833. // at this anchor point to decide the anchor is *not* dangling
  834. int minimal_count = 1;
  835. // a via can be removed if connected to only one other item.
  836. // the minimal_count is therefore 2
  837. if( item_ref->Type() == PCB_VIA_T )
  838. minimal_count = 2;
  839. for( CN_ITEM* item : *m_cluster )
  840. {
  841. if( !item->Valid() )
  842. continue;
  843. BOARD_CONNECTED_ITEM* brd_item = item->Parent();
  844. if( brd_item == item_ref )
  845. continue;
  846. // count only items on the same layer at this coordinate (especially for zones)
  847. if( !( brd_item->GetLayerSet() & layers ).any() )
  848. continue;
  849. if( brd_item->Type() == PCB_ZONE_AREA_T )
  850. {
  851. ZONE_CONTAINER* zone = static_cast<ZONE_CONTAINER*>( brd_item );
  852. if( zone->HitTestInsideZone( wxPoint( Pos() ) ) )
  853. connected_items_count++;
  854. }
  855. else if( brd_item->HitTest( wxPoint( Pos() ) ) )
  856. connected_items_count++;
  857. }
  858. return connected_items_count < minimal_count;
  859. }
  860. void CN_CONNECTIVITY_ALGO::SetProgressReporter( PROGRESS_REPORTER* aReporter )
  861. {
  862. m_progressReporter = aReporter;
  863. }