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.

617 lines
14 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 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. #ifdef PROFILE
  25. #include <profile.h>
  26. #endif
  27. #include <connectivity.h>
  28. #include <connectivity_algo.h>
  29. #include <ratsnest_data.h>
  30. #ifdef USE_OPENMP
  31. #include <omp.h>
  32. #endif /* USE_OPENMP */
  33. CONNECTIVITY_DATA::CONNECTIVITY_DATA()
  34. {
  35. m_connAlgo.reset( new CN_CONNECTIVITY_ALGO );
  36. }
  37. CONNECTIVITY_DATA::~CONNECTIVITY_DATA()
  38. {
  39. Clear();
  40. }
  41. bool CONNECTIVITY_DATA::Add( BOARD_ITEM* aItem )
  42. {
  43. m_connAlgo->Add( aItem );
  44. return true;
  45. }
  46. bool CONNECTIVITY_DATA::Remove( BOARD_ITEM* aItem )
  47. {
  48. m_connAlgo->Remove( aItem );
  49. return true;
  50. }
  51. bool CONNECTIVITY_DATA::Update( BOARD_ITEM* aItem )
  52. {
  53. m_connAlgo->Remove( aItem );
  54. m_connAlgo->Add( aItem );
  55. return true;
  56. }
  57. void CONNECTIVITY_DATA::Build( BOARD* aBoard )
  58. {
  59. m_connAlgo.reset( new CN_CONNECTIVITY_ALGO );
  60. m_connAlgo->Build( aBoard );
  61. RecalculateRatsnest();
  62. }
  63. void CONNECTIVITY_DATA::Build( const std::vector<BOARD_ITEM*>& aItems )
  64. {
  65. m_connAlgo.reset( new CN_CONNECTIVITY_ALGO );
  66. m_connAlgo->Build( aItems );
  67. RecalculateRatsnest();
  68. }
  69. void CONNECTIVITY_DATA::updateRatsnest()
  70. {
  71. int lastNet = m_connAlgo->NetCount();
  72. #ifdef PROFILE
  73. PROF_COUNTER rnUpdate( "update-ratsnest" );
  74. #endif
  75. int nDirty = 0;
  76. int i;
  77. #ifdef USE_OPENMP
  78. #pragma omp parallel shared(lastNet) private(i)
  79. {
  80. #pragma omp for schedule(guided, 1)
  81. #else /* USE_OPENMP */
  82. {
  83. #endif
  84. // Start with net number 1, as 0 stands for not connected
  85. for( i = 1; i < lastNet; ++i )
  86. {
  87. if( m_nets[i]->IsDirty() )
  88. {
  89. m_nets[i]->Update();
  90. nDirty++;
  91. }
  92. }
  93. } /* end of parallel section */
  94. #ifdef PROFILE
  95. rnUpdate.Show();
  96. #endif /* PROFILE */
  97. }
  98. void CONNECTIVITY_DATA::addRatsnestCluster( std::shared_ptr<CN_CLUSTER> aCluster )
  99. {
  100. auto rnNet = m_nets[ aCluster->OriginNet() ];
  101. rnNet->AddCluster( aCluster );
  102. }
  103. void CONNECTIVITY_DATA::RecalculateRatsnest()
  104. {
  105. m_connAlgo->PropagateNets();
  106. int lastNet = m_connAlgo->NetCount();
  107. if( lastNet >= (int) m_nets.size() )
  108. {
  109. unsigned int prevSize = m_nets.size();
  110. m_nets.resize( lastNet + 1 );
  111. for( unsigned int i = prevSize; i < m_nets.size(); i++ )
  112. m_nets[i] = new RN_NET;
  113. }
  114. auto clusters = m_connAlgo->GetClusters();
  115. int dirtyNets = 0;
  116. for( int net = 0; net < lastNet; net++ )
  117. {
  118. if( m_connAlgo->IsNetDirty( net ) )
  119. {
  120. m_nets[net]->Clear();
  121. dirtyNets++;
  122. }
  123. }
  124. for( auto c : clusters )
  125. {
  126. int net = c->OriginNet();
  127. if( m_connAlgo->IsNetDirty( net ) )
  128. {
  129. addRatsnestCluster( c );
  130. }
  131. }
  132. m_connAlgo->ClearDirtyFlags();
  133. updateRatsnest();
  134. }
  135. void CONNECTIVITY_DATA::BlockRatsnestItems( const std::vector<BOARD_ITEM*>& aItems )
  136. {
  137. std::vector<BOARD_CONNECTED_ITEM*> citems;
  138. for( auto item : aItems )
  139. {
  140. if( item->Type() == PCB_MODULE_T )
  141. {
  142. for( auto pad : static_cast<MODULE*>(item)->Pads() )
  143. citems.push_back( pad );
  144. }
  145. else
  146. {
  147. citems.push_back( static_cast<BOARD_CONNECTED_ITEM*>(item) );
  148. }
  149. }
  150. for( auto item : citems )
  151. {
  152. auto& entry = m_connAlgo->ItemEntry( item );
  153. for( auto cnItem : entry.GetItems() )
  154. {
  155. for( auto anchor : cnItem->Anchors() )
  156. anchor->SetNoLine( true );
  157. }
  158. }
  159. }
  160. int CONNECTIVITY_DATA::GetNetCount() const
  161. {
  162. return m_connAlgo->NetCount();
  163. }
  164. void CONNECTIVITY_DATA::FindIsolatedCopperIslands( ZONE_CONTAINER* aZone,
  165. std::vector<int>& aIslands )
  166. {
  167. m_connAlgo->FindIsolatedCopperIslands( aZone, aIslands );
  168. }
  169. void CONNECTIVITY_DATA::ComputeDynamicRatsnest( const std::vector<BOARD_ITEM*>& aItems )
  170. {
  171. m_dynamicConnectivity.reset( new CONNECTIVITY_DATA );
  172. m_dynamicConnectivity->Build( aItems );
  173. m_dynamicRatsnest.clear();
  174. BlockRatsnestItems( aItems );
  175. for( unsigned int nc = 1; nc < m_dynamicConnectivity->m_nets.size(); nc++ )
  176. {
  177. auto dynNet = m_dynamicConnectivity->m_nets[nc];
  178. if( dynNet->GetNodeCount() != 0 )
  179. {
  180. auto ourNet = m_nets[nc];
  181. CN_ANCHOR_PTR nodeA, nodeB;
  182. if( ourNet->NearestBicoloredPair( *dynNet, nodeA, nodeB ) )
  183. {
  184. RN_DYNAMIC_LINE l;
  185. l.a = nodeA->Pos();
  186. l.b = nodeB->Pos();
  187. l.netCode = nc;
  188. m_dynamicRatsnest.push_back( l );
  189. }
  190. }
  191. }
  192. for( auto net : m_dynamicConnectivity->m_nets )
  193. {
  194. if( !net )
  195. continue;
  196. const auto& edges = net->GetUnconnected();
  197. if( edges.empty() )
  198. continue;
  199. for( const auto& edge : edges )
  200. {
  201. const auto& nodeA = edge.GetSourceNode();
  202. const auto& nodeB = edge.GetTargetNode();
  203. RN_DYNAMIC_LINE l;
  204. l.a = nodeA->Pos();
  205. l.b = nodeB->Pos();
  206. l.netCode = 0;
  207. m_dynamicRatsnest.push_back( l );
  208. }
  209. }
  210. }
  211. void CONNECTIVITY_DATA::ClearDynamicRatsnest()
  212. {
  213. m_connAlgo->ForEachAnchor( [] (CN_ANCHOR_PTR anchor ) { anchor->SetNoLine( false ); } );
  214. HideDynamicRatsnest();
  215. }
  216. void CONNECTIVITY_DATA::HideDynamicRatsnest()
  217. {
  218. m_dynamicConnectivity.reset();
  219. m_dynamicRatsnest.clear();
  220. }
  221. void CONNECTIVITY_DATA::PropagateNets()
  222. {
  223. m_connAlgo->PropagateNets();
  224. }
  225. unsigned int CONNECTIVITY_DATA::GetUnconnectedCount() const
  226. {
  227. unsigned int unconnected = 0;
  228. for( auto net : m_nets )
  229. {
  230. if( !net )
  231. continue;
  232. const auto& edges = net->GetUnconnected();
  233. if( edges.empty() )
  234. continue;
  235. unconnected += edges.size();
  236. }
  237. return unconnected;
  238. }
  239. void CONNECTIVITY_DATA::Clear()
  240. {
  241. for( auto net : m_nets )
  242. delete net;
  243. m_nets.clear();
  244. }
  245. const std::list<BOARD_CONNECTED_ITEM*> CONNECTIVITY_DATA::GetConnectedItems(
  246. const BOARD_CONNECTED_ITEM* aItem,
  247. const KICAD_T aTypes[] ) const
  248. {
  249. std::list<BOARD_CONNECTED_ITEM*> rv;
  250. const auto clusters = m_connAlgo->SearchClusters( CN_CONNECTIVITY_ALGO::CSM_CONNECTIVITY_CHECK,
  251. aTypes, aItem->GetNetCode() );
  252. for( auto cl : clusters )
  253. {
  254. if( cl->Contains( aItem ) )
  255. {
  256. for( const auto item : *cl )
  257. {
  258. if( item->Valid() )
  259. rv.push_back( item->Parent() );
  260. }
  261. }
  262. }
  263. return rv;
  264. }
  265. const std::list<BOARD_CONNECTED_ITEM*> CONNECTIVITY_DATA::GetNetItems( int aNetCode,
  266. const KICAD_T aTypes[] ) const
  267. {
  268. std::set<BOARD_CONNECTED_ITEM*> items;
  269. std::list<BOARD_CONNECTED_ITEM*> rv;
  270. m_connAlgo->ForEachItem( [&items, aNetCode, &aTypes] ( CN_ITEM* aItem )
  271. {
  272. if( aItem->Valid() && aItem->Net() == aNetCode )
  273. {
  274. KICAD_T itemType = aItem->Parent()->Type();
  275. for( int i = 0; aTypes[i] > 0; ++i )
  276. {
  277. wxASSERT( aTypes[i] < MAX_STRUCT_TYPE_ID );
  278. if( itemType == aTypes[i] )
  279. {
  280. items.insert( aItem->Parent() );
  281. break;
  282. }
  283. }
  284. }
  285. } );
  286. std::copy( items.begin(), items.end(), std::front_inserter( rv ) );
  287. return rv;
  288. }
  289. bool CONNECTIVITY_DATA::CheckConnectivity( std::vector<CN_DISJOINT_NET_ENTRY>& aReport )
  290. {
  291. RecalculateRatsnest();
  292. for( auto net : m_nets )
  293. {
  294. if( net )
  295. {
  296. for( const auto& edge : net->GetEdges() )
  297. {
  298. CN_DISJOINT_NET_ENTRY ent;
  299. ent.net = edge.GetSourceNode()->Parent()->GetNetCode();
  300. ent.a = edge.GetSourceNode()->Parent();
  301. ent.b = edge.GetTargetNode()->Parent();
  302. ent.anchorA = edge.GetSourceNode()->Pos();
  303. ent.anchorB = edge.GetTargetNode()->Pos();
  304. aReport.push_back( ent );
  305. }
  306. }
  307. }
  308. return aReport.empty();
  309. }
  310. const std::vector<TRACK*> CONNECTIVITY_DATA::GetConnectedTracks( const BOARD_CONNECTED_ITEM* aItem )
  311. const
  312. {
  313. auto& entry = m_connAlgo->ItemEntry( aItem );
  314. std::set<TRACK*> tracks;
  315. std::vector<TRACK*> rv;
  316. for( auto citem : entry.GetItems() )
  317. {
  318. for( auto connected : citem->ConnectedItems() )
  319. {
  320. if( connected->Valid() && ( connected->Parent()->Type() == PCB_TRACE_T || connected->Parent()->Type() == PCB_VIA_T ) )
  321. tracks.insert( static_cast<TRACK*> ( connected->Parent() ) );
  322. }
  323. }
  324. std::copy( tracks.begin(), tracks.end(), std::back_inserter( rv ) );
  325. return rv;
  326. }
  327. const std::vector<D_PAD*> CONNECTIVITY_DATA::GetConnectedPads( const BOARD_CONNECTED_ITEM* aItem )
  328. const
  329. {
  330. auto& entry = m_connAlgo->ItemEntry( aItem );
  331. std::set<D_PAD*> pads;
  332. std::vector<D_PAD*> rv;
  333. for( auto citem : entry.GetItems() )
  334. {
  335. for( auto connected : citem->ConnectedItems() )
  336. {
  337. if( connected->Valid() && connected->Parent()->Type() == PCB_PAD_T )
  338. pads.insert( static_cast<D_PAD*> ( connected->Parent() ) );
  339. }
  340. }
  341. std::copy( pads.begin(), pads.end(), std::back_inserter( rv ) );
  342. return rv;
  343. }
  344. unsigned int CONNECTIVITY_DATA::GetNodeCount( int aNet ) const
  345. {
  346. int sum = 0;
  347. if( aNet < 0 ) // Node count for all nets
  348. {
  349. for( const auto& net : m_nets )
  350. sum += net->GetNodeCount();
  351. }
  352. else if( aNet < (int) m_nets.size() )
  353. {
  354. sum = m_nets[aNet]->GetNodeCount();
  355. }
  356. return sum;
  357. }
  358. unsigned int CONNECTIVITY_DATA::GetPadCount( int aNet ) const
  359. {
  360. int n = 0;
  361. for( auto pad : m_connAlgo->PadList() )
  362. {
  363. if( !pad->Valid() )
  364. continue;
  365. auto dpad = static_cast<D_PAD*>( pad->Parent() );
  366. if( aNet < 0 || aNet == dpad->GetNetCode() )
  367. {
  368. n++;
  369. }
  370. }
  371. return n;
  372. }
  373. const std::vector<VECTOR2I> CONNECTIVITY_DATA::NearestUnconnectedTargets(
  374. const BOARD_CONNECTED_ITEM* aRef,
  375. const VECTOR2I& aPos,
  376. int aNet )
  377. {
  378. CN_CLUSTER_PTR refCluster;
  379. int refNet = -1;
  380. if( aRef )
  381. refNet = aRef->GetNetCode();
  382. if( aNet >= 0 )
  383. refNet = aNet;
  384. if( aRef )
  385. {
  386. for( auto cl : m_connAlgo->GetClusters() )
  387. {
  388. if( cl->Contains( aRef ) )
  389. {
  390. refCluster = cl;
  391. break;
  392. }
  393. }
  394. }
  395. std::set <VECTOR2I> anchors;
  396. for( auto cl : m_connAlgo->GetClusters() )
  397. {
  398. if( cl != refCluster )
  399. {
  400. for( auto item : *cl )
  401. {
  402. if( item->Valid() && item->Parent()->GetNetCode() == refNet
  403. && item->Parent()->Type() != PCB_ZONE_AREA_T )
  404. {
  405. for( auto anchor : item->Anchors() )
  406. {
  407. anchors.insert( anchor->Pos() );
  408. }
  409. }
  410. }
  411. }
  412. }
  413. std::vector<VECTOR2I> rv;
  414. std::copy( anchors.begin(), anchors.end(), std::back_inserter( rv ) );
  415. std::sort( rv.begin(), rv.end(), [aPos] ( const VECTOR2I& a, const VECTOR2I& b )
  416. {
  417. auto da = (a - aPos).EuclideanNorm();
  418. auto db = (b - aPos).EuclideanNorm();
  419. return da < db;
  420. } );
  421. return rv;
  422. }
  423. void CONNECTIVITY_DATA::GetUnconnectedEdges( std::vector<CN_EDGE>& aEdges) const
  424. {
  425. for( auto rnNet : m_nets )
  426. {
  427. if( rnNet )
  428. {
  429. for( auto edge : rnNet->GetEdges() )
  430. {
  431. aEdges.push_back( edge );
  432. }
  433. }
  434. }
  435. }
  436. const std::vector<BOARD_CONNECTED_ITEM*> CONNECTIVITY_DATA::GetConnectedItems(
  437. const BOARD_CONNECTED_ITEM* aItem, const VECTOR2I& aAnchor, KICAD_T aTypes[] )
  438. {
  439. auto& entry = m_connAlgo->ItemEntry( aItem );
  440. std::vector<BOARD_CONNECTED_ITEM* > rv;
  441. for( auto cnItem : entry.GetItems() )
  442. {
  443. for( auto anchor : cnItem->Anchors() )
  444. {
  445. if( anchor->Pos() == aAnchor )
  446. {
  447. for( int i = 0; aTypes[i] > 0; i++ )
  448. {
  449. if( cnItem->Valid() && cnItem->Parent()->Type() == aTypes[i] )
  450. {
  451. rv.push_back( cnItem->Parent() );
  452. break;
  453. }
  454. }
  455. }
  456. }
  457. }
  458. return rv;
  459. }
  460. RN_NET* CONNECTIVITY_DATA::GetRatsnestForNet( int aNet )
  461. {
  462. if ( aNet < 0 || aNet >= (int) m_nets.size() )
  463. {
  464. return nullptr;
  465. }
  466. return m_nets[ aNet ];
  467. }
  468. void CONNECTIVITY_DATA::MarkItemNetAsDirty( BOARD_ITEM *aItem )
  469. {
  470. if (aItem->Type() == PCB_MODULE_T)
  471. {
  472. for ( auto pad : static_cast<MODULE*>( aItem )->Pads() )
  473. {
  474. m_connAlgo->MarkNetAsDirty( pad->GetNetCode() );
  475. }
  476. }
  477. if (aItem->IsConnected() )
  478. {
  479. m_connAlgo->MarkNetAsDirty( static_cast<BOARD_CONNECTED_ITEM*>( aItem )->GetNetCode() );
  480. }
  481. }