KiCad PCB EDA Suite
connectivity_algo.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) 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 
25 #include <connectivity_algo.h>
28 
29 #include <thread>
30 #include <mutex>
31 
32 #ifdef PROFILE
33 #include <profile.h>
34 #endif
35 
36 #ifdef USE_OPENMP
37 #include <omp.h>
38 #endif /* USE_OPENMP */
39 
40 using namespace std::placeholders;
41 
42 bool operator<( const CN_ANCHOR_PTR& a, const CN_ANCHOR_PTR& b )
43 {
44  if( a->Pos().x == b->Pos().x )
45  return a->Pos().y < b->Pos().y;
46  else
47  return a->Pos().x < b->Pos().x;
48 }
49 
50 
51 bool CN_ANCHOR::IsDirty() const
52 {
53  return m_item->Dirty();
54 }
55 
56 
58 {
59  m_items.reserve( 64 );
60  m_originPad = nullptr;
61  m_originNet = -1;
62  m_conflicting = false;
63 }
64 
65 
67 {
68 }
69 
70 
71 wxString CN_CLUSTER::OriginNetName() const
72 {
73  if( !m_originPad || !m_originPad->Valid() )
74  return "<none>";
75  else
76  return m_originPad->Parent()->GetNetname();
77 }
78 
79 
80 bool CN_CLUSTER::Contains( const CN_ITEM* aItem )
81 {
82  return std::find( m_items.begin(), m_items.end(), aItem ) != m_items.end();
83 }
84 
85 
87 {
88  for( auto item : m_items )
89  {
90  if( item->Valid() && item->Parent() == aItem )
91  return true;
92  }
93 
94  return false;
95 }
96 
97 
99 {
100  printf(" valid: %d, connected: \n", !!Valid());
101 
102  for( auto i : m_connected )
103  {
104  TRACK* t = static_cast<TRACK*>( i->Parent() );
105  printf( " - %p %d\n", t, t->Type() );
106  }
107 }
108 
109 
111 {
112  for( auto item : m_items )
113  {
114  wxLogTrace( "CN", " - item : %p bitem : %p type : %d inet %s\n", item, item->Parent(),
115  item->Parent()->Type(), (const char*) item->Parent()->GetNetname().c_str() );
116  printf( "- item : %p bitem : %p type : %d inet %s\n", item, item->Parent(),
117  item->Parent()->Type(), (const char*) item->Parent()->GetNetname().c_str() );
118  item->Dump();
119  }
120 }
121 
122 
124 {
125  m_items.push_back( item );
126 
127  if( m_originNet < 0 )
128  {
129  m_originNet = item->Net();
130  }
131 
132  if( item->Parent()->Type() == PCB_PAD_T )
133  {
134  if( !m_originPad )
135  {
136  m_originPad = item;
137  m_originNet = item->Net();
138  }
139 
140  if( m_originPad && item->Net() != m_originNet )
141  {
142  m_conflicting = true;
143  }
144  }
145 }
146 
147 
149 {
150 }
151 
152 
154 {
155  Clear();
156 }
157 
158 
160 {
161  markItemNetAsDirty( aItem );
162 
163  switch( aItem->Type() )
164  {
165  case PCB_MODULE_T:
166  for( auto pad : static_cast<MODULE*>( aItem ) -> Pads() )
167  {
168  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( pad ) ].MarkItemsAsInvalid();
169  m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( pad ) );
170  }
171 
172  m_itemList.SetDirty( true );
173  break;
174 
175  case PCB_PAD_T:
176  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
177  m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
178  m_itemList.SetDirty( true );
179  break;
180 
181  case PCB_TRACE_T:
182  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
183  m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
184  m_itemList.SetDirty( true );
185  break;
186 
187  case PCB_VIA_T:
188  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
189  m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
190  m_itemList.SetDirty( true );
191  break;
192 
193  case PCB_ZONE_AREA_T:
194  case PCB_ZONE_T:
195  {
196  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
197  m_itemMap.erase ( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
198  m_zoneList.SetDirty( true );
199  break;
200  }
201 
202  default:
203  return false;
204  }
205 
206  // Once we delete an item, it may connect between lists, so mark both as potentially invalid
207  m_itemList.SetHasInvalid( true );
208  m_zoneList.SetHasInvalid( true );
209 
210  return true;
211 }
212 
213 
215 {
216  if( aItem->IsConnected() )
217  {
218  auto citem = static_cast<const BOARD_CONNECTED_ITEM*>( aItem );
219  MarkNetAsDirty( citem->GetNetCode() );
220  }
221  else
222  {
223  if( aItem->Type() == PCB_MODULE_T )
224  {
225  auto mod = static_cast <const MODULE*>( aItem );
226 
227  for( D_PAD* pad = mod->PadsList(); pad; pad = pad->Next() )
228  MarkNetAsDirty( pad->GetNetCode() );
229  }
230  }
231 }
232 
233 
235 {
236  markItemNetAsDirty ( aItem );
237 
238  switch( aItem->Type() )
239  {
240  case PCB_NETINFO_T:
241  {
242  MarkNetAsDirty( static_cast<NETINFO_ITEM*>( aItem )->GetNet() );
243  break;
244  }
245  case PCB_MODULE_T:
246  for( auto pad : static_cast<MODULE*>( aItem ) -> Pads() )
247  {
248  if( m_itemMap.find( pad ) != m_itemMap.end() )
249  return false;
250 
251  add( m_itemList, pad );
252  }
253 
254  break;
255 
256  case PCB_PAD_T:
257  if( m_itemMap.find ( static_cast<D_PAD*>( aItem ) ) != m_itemMap.end() )
258  return false;
259 
260  add( m_itemList, static_cast<D_PAD*>( aItem ) );
261 
262  break;
263 
264  case PCB_TRACE_T:
265  {
266  if( m_itemMap.find( static_cast<TRACK*>( aItem ) ) != m_itemMap.end() )
267  return false;
268 
269  add( m_itemList, static_cast<TRACK*>( aItem ) );
270 
271  break;
272  }
273 
274  case PCB_VIA_T:
275  if( m_itemMap.find( static_cast<VIA*>( aItem ) ) != m_itemMap.end() )
276  return false;
277 
278  add( m_itemList, static_cast<VIA*>( aItem ) );
279 
280  break;
281 
282  case PCB_ZONE_AREA_T:
283  case PCB_ZONE_T:
284  {
285  auto zone = static_cast<ZONE_CONTAINER*>( aItem );
286 
287  if( m_itemMap.find( static_cast<ZONE_CONTAINER*>( aItem ) ) != m_itemMap.end() )
288  return false;
289 
290  m_itemMap[zone] = ITEM_MAP_ENTRY();
291 
292  for( auto zitem : m_zoneList.Add( zone ) )
293  m_itemMap[zone].Link(zitem);
294 
295  break;
296  }
297 
298  default:
299  return false;
300  }
301 
302  return true;
303 }
304 
305 
307 {
308 #ifdef CONNECTIVITY_DEBUG
309  printf("Search start\n");
310 #endif
311 
312 #ifdef PROFILE
313  PROF_COUNTER garbage_collection( "garbage-collection" );
314 #endif
315  std::vector<CN_ITEM*> garbage;
316  garbage.reserve( 1024 );
317 
318  m_itemList.RemoveInvalidItems( garbage );
319  m_zoneList.RemoveInvalidItems( garbage );
320 
321  for( auto item : garbage )
322  delete item;
323 
324  #ifdef CONNECTIVITY_DEBUG
325  for( auto item : m_padList )
326  if( all.find( item->Parent() ) == all.end() ) { printf("Failing pad : %p\n", item->Parent() ); assert ( false ); }
327 
328  for( auto item : m_viaList )
329  if( all.find( item->Parent() ) == all.end() ) { printf("Failing via : %p\n", item->Parent() ); assert ( false ); }
330 
331  for( auto item : m_trackList )
332  if( all.find( item->Parent() ) == all.end() ) { printf("Failing track : %p\n", item->Parent() ); assert ( false ); }
333 
334  for( auto item : m_zoneList )
335  if( all.find( item->Parent() ) == all.end() ) { printf("Failing zome : %p\n", item->Parent() ); assert ( false ); }
336  #endif
337 
338 #ifdef PROFILE
339  garbage_collection.Show();
340  PROF_COUNTER search_cnt( "search-connections" );
341  PROF_COUNTER search_basic( "search-basic" );
342 #endif
343 
344  if( m_progressReporter )
345  {
346  m_progressReporter->SetMaxProgress(
347  m_zoneList.Size() + ( m_itemList.IsDirty() ? m_itemList.Size() : 0 ) );
348  }
349 
350 #ifdef USE_OPENMP
351  #pragma omp parallel num_threads( std::max( omp_get_num_procs(), 2 ) )
352  {
353  if( omp_get_thread_num() == 0 && m_progressReporter )
354  m_progressReporter->KeepRefreshing( true );
355 #endif
356 
357  if( m_itemList.IsDirty() )
358  {
359 #ifdef USE_OPENMP
360  #pragma omp parallel for
361 #endif
362  for( int i = 0; i < m_itemList.Size(); i++ )
363  {
364  auto item = m_itemList[i];
365  if( item->Dirty() )
366  {
367  CN_VISITOR visitor( item, &m_listLock );
368  m_itemList.FindNearby( item, visitor );
369  m_zoneList.FindNearby( item, visitor );
370  }
371 
372  if( m_progressReporter )
373  m_progressReporter->AdvanceProgress();
374  }
375  }
376 
377 #ifdef PROFILE
378  search_basic.Show();
379 #endif
380 
381 #ifdef USE_OPENMP
382  #pragma omp parallel for
383 #endif
384  for( int i = 0; i < m_zoneList.Size(); i++ )
385  {
386  auto item = m_zoneList[i];
387  auto zoneItem = static_cast<CN_ZONE *>( item );
388 
389  if( zoneItem->Dirty() )
390  {
391  CN_VISITOR visitor( item, &m_listLock );
392  m_itemList.FindNearby( item, visitor );
393  m_zoneList.FindNearby( item, visitor );
394  }
395 
396  if( m_progressReporter )
397  m_progressReporter->AdvanceProgress();
398  }
399 
400 #ifdef USE_OPENMP
401  }
402 #endif
403 
404  m_zoneList.ClearDirtyFlags();
405  m_itemList.ClearDirtyFlags();
406 
407 #ifdef CONNECTIVITY_DEBUG
408  printf("Search end\n");
409 #endif
410 
411 #ifdef PROFILE
412  search_cnt.Show();
413 #endif
414 }
415 
416 
418 {
419  auto lastConn = std::remove_if(m_connected.begin(), m_connected.end(), [] ( CN_ITEM * item) {
420  return !item->Valid();
421  } );
422 
423  m_connected.resize( lastConn - m_connected.begin() );
424 }
425 
426 
427 void CN_LIST::RemoveInvalidItems( std::vector<CN_ITEM*>& aGarbage )
428 {
429  if( !m_hasInvalid )
430  return;
431 
432  auto lastItem = std::remove_if(m_items.begin(), m_items.end(), [&aGarbage] ( CN_ITEM* item )
433  {
434  if( !item->Valid() )
435  {
436  aGarbage.push_back ( item );
437  return true;
438  }
439 
440  return false;
441  } );
442 
443  m_items.resize( lastItem - m_items.begin() );
444 
445  // fixme: mem leaks
446  for( auto item : m_items )
447  item->RemoveInvalidRefs();
448 
449  for( auto item : aGarbage )
450  m_index.Remove( item );
451 
452  m_hasInvalid = false;
453 }
454 
455 
457 {
458  return m_itemList.IsDirty() || m_zoneList.IsDirty();
459 }
460 
461 
463 {
465  return SearchClusters( aMode, types, -1 );
466 }
467 
468 
470  const KICAD_T aTypes[], int aSingleNet )
471 {
472  bool includeZones = ( aMode != CSM_PROPAGATE );
473  bool withinAnyNet = ( aMode != CSM_PROPAGATE );
474 
475  std::deque<CN_ITEM*> Q;
476  CN_ITEM* head = nullptr;
477  CLUSTERS clusters;
478 
479  if( isDirty() )
480  searchConnections();
481 
482  auto addToSearchList = [&head, withinAnyNet, aSingleNet, aTypes] ( CN_ITEM *aItem )
483  {
484  if( withinAnyNet && aItem->Net() <= 0 )
485  return;
486 
487  if( !aItem->Valid() )
488  return;
489 
490  if( aSingleNet >=0 && aItem->Net() != aSingleNet )
491  return;
492 
493  bool found = false;
494 
495  for( int i = 0; aTypes[i] != EOT; i++ )
496  {
497  if( aItem->Parent()->Type() == aTypes[i] )
498  {
499  found = true;
500  break;
501  }
502  }
503 
504  if( !found )
505  return;
506 
507  aItem->ListClear();
508  aItem->SetVisited( false );
509 
510  if( !head )
511  head = aItem;
512  else
513  head->ListInsert( aItem );
514  };
515 
516  std::for_each( m_itemList.begin(), m_itemList.end(), addToSearchList );
517 
518  if( includeZones )
519  {
520  std::for_each( m_zoneList.begin(), m_zoneList.end(), addToSearchList );
521  }
522 
523 
524  while( head )
525  {
526  CN_CLUSTER_PTR cluster ( new CN_CLUSTER() );
527 
528  Q.clear();
529  CN_ITEM* root = head;
530  root->SetVisited ( true );
531 
532  head = root->ListRemove();
533 
534  Q.push_back( root );
535 
536  while( Q.size() )
537  {
538  CN_ITEM* current = Q.front();
539 
540  Q.pop_front();
541  cluster->Add( current );
542 
543  for( auto n : current->ConnectedItems() )
544  {
545  if( withinAnyNet && n->Net() != root->Net() )
546  continue;
547 
548  if( !n->Visited() && n->Valid() )
549  {
550  n->SetVisited( true );
551  Q.push_back( n );
552  head = n->ListRemove();
553  }
554  }
555  }
556 
557  clusters.push_back( cluster );
558  }
559 
560 
561  std::sort( clusters.begin(), clusters.end(), []( CN_CLUSTER_PTR a, CN_CLUSTER_PTR b ) {
562  return a->OriginNet() < b->OriginNet();
563  } );
564 
565 #ifdef CONNECTIVITY_DEBUG
566  printf("Active clusters: %d\n", clusters.size() );
567 
568  for( auto cl : clusters )
569  {
570  printf( "Net %d\n", cl->OriginNet() );
571  cl->Dump();
572  }
573 #endif
574 
575  return clusters;
576 }
577 
578 
580 {
581  for( int i = 0; i<aBoard->GetAreaCount(); i++ )
582  {
583  auto zone = aBoard->GetArea( i );
584  Add( zone );
585  }
586 
587  for( auto tv : aBoard->Tracks() )
588  Add( tv );
589 
590  for( auto mod : aBoard->Modules() )
591  {
592  for( auto pad : mod->Pads() )
593  Add( pad );
594  }
595 
596  /*wxLogTrace( "CN", "zones : %lu, pads : %lu vias : %lu tracks : %lu\n",
597  m_zoneList.Size(), m_padList.Size(),
598  m_viaList.Size(), m_trackList.Size() );*/
599 }
600 
601 
602 void CN_CONNECTIVITY_ALGO::Build( const std::vector<BOARD_ITEM*>& aItems )
603 {
604  for( auto item : aItems )
605  {
606  switch( item->Type() )
607  {
608  case PCB_TRACE_T:
609  case PCB_VIA_T:
610  case PCB_ZONE_T:
611  case PCB_PAD_T:
612  Add( item );
613  break;
614 
615  case PCB_MODULE_T:
616  {
617  for( auto pad : static_cast<MODULE*>( item )->Pads() )
618  {
619  Add( pad );
620  }
621 
622  break;
623  }
624 
625  default:
626  break;
627  }
628  }
629 }
630 
631 
633 {
634  for( const auto& cluster : m_connClusters )
635  {
636  if( cluster->IsConflicting() )
637  {
638  wxLogTrace( "CN", "Conflicting nets in cluster %p\n", cluster.get() );
639  }
640  else if( cluster->IsOrphaned() )
641  {
642  wxLogTrace( "CN", "Skipping orphaned cluster %p [net: %s]\n", cluster.get(),
643  (const char*) cluster->OriginNetName().c_str() );
644  }
645  else if( cluster->HasValidNet() )
646  {
647  // normal cluster: just propagate from the pads
648  int n_changed = 0;
649 
650  for( auto item : *cluster )
651  {
652  if( item->CanChangeNet() )
653  {
654  if( item->Valid() && item->Parent()->GetNetCode() != cluster->OriginNet() )
655  {
656  MarkNetAsDirty( item->Parent()->GetNetCode() );
657  MarkNetAsDirty( cluster->OriginNet() );
658 
659  item->Parent()->SetNetCode( cluster->OriginNet() );
660  n_changed++;
661  }
662  }
663  }
664 
665  if( n_changed )
666  wxLogTrace( "CN", "Cluster %p : net : %d %s\n", cluster.get(),
667  cluster->OriginNet(), (const char*) cluster->OriginNetName().c_str() );
668  else
669  wxLogTrace( "CN", "Cluster %p : nothing to propagate\n", cluster.get() );
670  }
671  else
672  {
673  wxLogTrace( "CN", "Cluster %p : connected to unused net\n", cluster.get() );
674  }
675  }
676 }
677 
678 
680 {
681  m_connClusters = SearchClusters( CSM_PROPAGATE );
682  propagateConnections();
683 }
684 
685 
686 void CN_CONNECTIVITY_ALGO::FindIsolatedCopperIslands( ZONE_CONTAINER* aZone, std::vector<int>& aIslands )
687 {
688  if( aZone->GetFilledPolysList().IsEmpty() )
689  return;
690 
691  aIslands.clear();
692 
693  Remove( aZone );
694  Add( aZone );
695 
696  m_connClusters = SearchClusters( CSM_CONNECTIVITY_CHECK );
697 
698  for( const auto& cluster : m_connClusters )
699  {
700  if( cluster->Contains( aZone ) && cluster->IsOrphaned() )
701  {
702  for( auto z : *cluster )
703  {
704  if( z->Parent() == aZone )
705  {
706  aIslands.push_back( static_cast<CN_ZONE*>(z)->SubpolyIndex() );
707  }
708  }
709  }
710  }
711 
712  wxLogTrace( "CN", "Found %u isolated islands\n", (unsigned)aIslands.size() );
713 }
714 
715 void CN_CONNECTIVITY_ALGO::FindIsolatedCopperIslands( std::vector<CN_ZONE_ISOLATED_ISLAND_LIST>& aZones )
716 {
717  for ( auto& z : aZones )
718  Remove( z.m_zone );
719 
720  for ( auto& z : aZones )
721  {
722  if( !z.m_zone->GetFilledPolysList().IsEmpty() )
723  Add( z.m_zone );
724  }
725 
726  m_connClusters = SearchClusters( CSM_CONNECTIVITY_CHECK );
727 
728  for ( auto& zone : aZones )
729  {
730  if( zone.m_zone->GetFilledPolysList().IsEmpty() )
731  continue;
732 
733  for( const auto& cluster : m_connClusters )
734  {
735  if( cluster->Contains( zone.m_zone ) && cluster->IsOrphaned() )
736  {
737  for( auto z : *cluster )
738  {
739  if( z->Parent() == zone.m_zone )
740  {
741  zone.m_islands.push_back( static_cast<CN_ZONE*>(z)->SubpolyIndex() );
742  }
743  }
744  }
745  }
746  }
747 }
748 
749 
751 {
752  m_ratsnestClusters = SearchClusters( CSM_RATSNEST );
753  return m_ratsnestClusters;
754 }
755 
756 
758 {
759  if( aNet < 0 )
760  return;
761 
762  if( (int) m_dirtyNets.size() <= aNet )
763  {
764  int lastNet = m_dirtyNets.size() - 1;
765 
766  if( lastNet < 0 )
767  lastNet = 0;
768 
769  m_dirtyNets.resize( aNet + 1 );
770 
771  for( int i = lastNet; i < aNet + 1; i++ )
772  m_dirtyNets[i] = true;
773  }
774 
775  m_dirtyNets[aNet] = true;
776 }
777 
778 
780 {
781  auto zoneItem = static_cast<CN_ZONE*> ( aZone );
782 
783  if( zoneItem->Net() != aItem->Net() && !aItem->CanChangeNet() )
784  return;
785 
786  if( zoneItem->ContainsPoint( aItem->GetAnchor( 0 ) ) ||
787  ( aItem->Parent()->Type() == PCB_TRACE_T &&
788  zoneItem->ContainsPoint( aItem->GetAnchor( 1 ) ) ) )
789  {
790  std::lock_guard<std::mutex> lock( *m_listLock );
791  CN_ITEM::Connect( zoneItem, aItem );
792  }
793 }
794 
796 {
797  const auto refParent = static_cast<const ZONE_CONTAINER*>( aZoneA->Parent() );
798  const auto testedParent = static_cast<const ZONE_CONTAINER*>( aZoneB->Parent() );
799 
800  if( testedParent->Type () != PCB_ZONE_AREA_T )
801  return;
802 
803  if( aZoneB == aZoneA || refParent == testedParent )
804  return;
805 
806  if( aZoneB->Net() != aZoneA->Net() )
807  return; // we only test zones belonging to the same net
808 
809  const auto& outline = refParent->GetFilledPolysList().COutline( aZoneA->SubpolyIndex() );
810 
811  for( int i = 0; i < outline.PointCount(); i++ )
812  {
813  if( aZoneB->ContainsPoint( outline.CPoint( i ) ) )
814  {
815  std::lock_guard<std::mutex> lock( *m_listLock );
816  CN_ITEM::Connect( aZoneA, aZoneB );
817  return;
818  }
819  }
820 
821  const auto& outline2 = testedParent->GetFilledPolysList().COutline( aZoneB->SubpolyIndex() );
822 
823  for( int i = 0; i < outline2.PointCount(); i++ )
824  {
825  if( aZoneA->ContainsPoint( outline2.CPoint( i ) ) )
826  {
827  std::lock_guard<std::mutex> lock( *m_listLock );
828  CN_ITEM::Connect( aZoneA, aZoneB );
829  return;
830  }
831  }
832 }
833 
834 
835 bool CN_VISITOR::operator()( CN_ITEM* aCandidate )
836 {
837  const auto parentA = aCandidate->Parent();
838  const auto parentB = m_item->Parent();
839 
840  if( !aCandidate->Valid() || !m_item->Valid() )
841  return true;
842 
843  if( parentA == parentB )
844  return true;
845 
846  if( !( parentA->GetLayerSet() & parentB->GetLayerSet() ).any() )
847  return true;
848 
849  // We should handle zone-zone connection separately
850  if ( ( parentA->Type() == PCB_ZONE_AREA_T || parentA->Type() == PCB_ZONE_T ) &&
851  ( parentB->Type() == PCB_ZONE_AREA_T || parentB->Type() == PCB_ZONE_T ) )
852  {
853  checkZoneZoneConnection( static_cast<CN_ZONE*>( m_item ),
854  static_cast<CN_ZONE*>( aCandidate ) );
855  return true;
856  }
857 
858  if( parentA->Type() == PCB_ZONE_AREA_T || parentA->Type() == PCB_ZONE_T)
859  {
860  checkZoneItemConnection( static_cast<CN_ZONE*>( aCandidate ), m_item );
861  return true;
862  }
863 
864  if( parentB->Type() == PCB_ZONE_AREA_T || parentB->Type() == PCB_ZONE_T)
865  {
866  checkZoneItemConnection( static_cast<CN_ZONE*>( m_item ), aCandidate );
867  return true;
868  }
869 
870  // Items do not necessarily have reciprocity as we only check for anchors
871  // therefore, we check HitTest both directions A->B & B->A
872  // TODO: Check for collision geometry on extended features
873  wxPoint ptA1( aCandidate->GetAnchor( 0 ).x, aCandidate->GetAnchor( 0 ).y );
874  wxPoint ptA2( aCandidate->GetAnchor( 1 ).x, aCandidate->GetAnchor( 1 ).y );
875  wxPoint ptB1( m_item->GetAnchor( 0 ).x, m_item->GetAnchor( 0 ).y );
876  wxPoint ptB2( m_item->GetAnchor( 1 ).x, m_item->GetAnchor( 1 ).y );
877  if( parentA->HitTest( ptB1 ) || parentB->HitTest( ptA1 ) ||
878  ( parentA->Type() == PCB_TRACE_T && parentB->HitTest( ptA2 ) ) ||
879  ( parentB->Type() == PCB_TRACE_T && parentA->HitTest( ptB2 ) ) )
880  {
881  std::lock_guard<std::mutex> lock( *m_listLock );
882  CN_ITEM::Connect( m_item, aCandidate );
883  }
884 
885  return true;
886 };
887 
888 
890 {
891  if( !m_valid )
892  return 0;
893 
894  return m_parent->Type() == PCB_TRACE_T ? 2 : 1;
895 }
896 
897 
898 const VECTOR2I CN_ITEM::GetAnchor( int n ) const
899 {
900  if( !m_valid )
901  return VECTOR2I();
902 
903  switch( m_parent->Type() )
904  {
905  case PCB_PAD_T:
906  return static_cast<const D_PAD*>( m_parent )->ShapePos();
907  break;
908 
909  case PCB_TRACE_T:
910  {
911  auto tr = static_cast<const TRACK*>( m_parent );
912  return ( n == 0 ? tr->GetStart() : tr->GetEnd() );
913 
914  break;
915  }
916 
917  case PCB_VIA_T:
918  return static_cast<const VIA*>( m_parent )->GetStart();
919 
920  default:
921  assert( false );
922  return VECTOR2I();
923  }
924 }
925 
926 
928 {
929  if( !Valid() )
930  return 0;
931 
932  const auto zone = static_cast<const ZONE_CONTAINER*>( Parent() );
933  const auto& outline = zone->GetFilledPolysList().COutline( m_subpolyIndex );
934 
935  return outline.PointCount() ? 1 : 0;
936 }
937 
938 
939 const VECTOR2I CN_ZONE::GetAnchor( int n ) const
940 {
941  if( !Valid() )
942  return VECTOR2I();
943 
944  const auto zone = static_cast<const ZONE_CONTAINER*> ( Parent() );
945  const auto& outline = zone->GetFilledPolysList().COutline( m_subpolyIndex );
946 
947  return outline.CPoint( 0 );
948 }
949 
950 
951 int CN_ITEM::Net() const
952 {
953  if( !m_parent || !m_valid )
954  return -1;
955 
956  return m_parent->GetNetCode();
957 }
958 
959 
961 {
962  assert( m_item->Valid() );
963  return m_item->Parent();
964 }
965 
966 
967 bool CN_ANCHOR::Valid() const
968 {
969  if( !m_item )
970  return false;
971 
972  return m_item->Valid();
973 }
974 
975 
977 {
978  m_ratsnestClusters.clear();
979  m_connClusters.clear();
980  m_itemMap.clear();
981  m_itemList.Clear();
982  m_zoneList.Clear();
983 
984 }
985 
986 
987 void CN_CONNECTIVITY_ALGO::ForEachItem( const std::function<void( CN_ITEM& )>& aFunc )
988 {
989 
990  for( auto item : m_itemList )
991  aFunc( *item );
992 
993  for( auto item : m_zoneList )
994  aFunc( *item );
995 }
996 
997 
998 void CN_CONNECTIVITY_ALGO::ForEachAnchor( const std::function<void( CN_ANCHOR& )>& aFunc )
999 {
1000  ForEachItem( [aFunc] ( CN_ITEM& item ) {
1001  for( const auto& anchor : item.Anchors() )
1002  aFunc( *anchor );
1003  }
1004  );
1005 }
1006 
1007 
1009 {
1010  if( !m_cluster )
1011  return true;
1012 
1013  // Calculate the item count connected to this anchor.
1014  // m_cluster groups all items connected, but they are not necessary connected
1015  // at this coordinate point (they are only candidates)
1016  BOARD_CONNECTED_ITEM* item_ref = Parent();
1017  LSET layers = item_ref->GetLayerSet() & LSET::AllCuMask();
1018 
1019  // the number of items connected to item_ref at ths anchor point
1020  int connected_items_count = 0;
1021 
1022  // the minimal number of items connected to item_ref
1023  // at this anchor point to decide the anchor is *not* dangling
1024  int minimal_count = 1;
1025 
1026  // a via can be removed if connected to only one other item.
1027  // the minimal_count is therefore 2
1028  if( item_ref->Type() == PCB_VIA_T )
1029  minimal_count = 2;
1030 
1031  for( CN_ITEM* item : *m_cluster )
1032  {
1033  if( !item->Valid() )
1034  continue;
1035 
1036  BOARD_CONNECTED_ITEM* brd_item = item->Parent();
1037 
1038  if( brd_item == item_ref )
1039  continue;
1040 
1041  // count only items on the same layer at this coordinate (especially for zones)
1042  if( !( brd_item->GetLayerSet() & layers ).any() )
1043  continue;
1044 
1045  if( brd_item->Type() == PCB_ZONE_AREA_T )
1046  {
1047  ZONE_CONTAINER* zone = static_cast<ZONE_CONTAINER*>( brd_item );
1048 
1049  if( zone->HitTestInsideZone( wxPoint( Pos() ) ) )
1050  connected_items_count++;
1051  }
1052  else if( brd_item->HitTest( wxPoint( Pos() ) ) )
1053  connected_items_count++;
1054  }
1055 
1056  return connected_items_count < minimal_count;
1057 }
1058 
1060 {
1061  m_progressReporter = aReporter;
1062 }
void RemoveInvalidItems(std::vector< CN_ITEM * > &aGarbage)
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Function AllCuMask returns a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:673
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
bool Contains(const CN_ITEM *aItem)
virtual int AnchorCount() const override
Class ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:60
void ListInsert(T *item)
bool Remove(BOARD_ITEM *aItem)
bool Valid() const
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
int PointCount() const
Function PointCount()
A progress reporter for use in multi-threaded environments.
bool IsDirty() const
void checkZoneItemConnection(CN_ZONE *aZone, CN_ITEM *aItem)
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:102
void ForEachAnchor(const std::function< void(CN_ANCHOR &)> &aFunc)
void SetVisited(bool aVisited)
BOARD_CONNECTED_ITEM * Parent() const
void MarkNetAsDirty(int aNet)
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
bool Add(BOARD_ITEM *aItem)
void Show()
Print the elapsed time (in ms) to STDERR.
Definition: profile.h:93
CN_ANCHORS & Anchors()
VECTOR2< int > VECTOR2I
Definition: vector2d.h:589
The class PROF_COUNTER is a small class to help profiling.
Definition: profile.h:45
Class BOARD_CONNECTED_ITEM is a base class derived from BOARD_ITEM for items that can be connected an...
search types array terminator (End Of Types)
Definition: typeinfo.h:82
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
bool ContainsPoint(const VECTOR2I p) const
wxString OriginNetName() const
const CLUSTERS SearchClusters(CLUSTER_SEARCH_MODE aMode, const KICAD_T aTypes[], int aSingleNet)
bool IsDangling() const
has meaning only for tracks and vias.
class MODULE, a footprint
Definition: typeinfo.h:89
void markItemNetAsDirty(const BOARD_ITEM *aItem)
bool operator<(const CN_ANCHOR_PTR &a, const CN_ANCHOR_PTR &b)
Class LSET is a set of PCB_LAYER_IDs.
DLIST_ITERATOR_WRAPPER< MODULE > Modules()
Definition: class_board.h:253
int SubpolyIndex() const
bool Valid() const
void ForEachItem(const std::function< void(CN_ITEM &)> &aFunc)
bool HitTestInsideZone(const wxPoint &aPosition) const
Function HitTest tests if a point is inside the zone area, i.e.
Definition: class_zone.h:256
virtual LSET GetLayerSet() const
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
a few functions useful in geometry calculations.
class SEGZONE, a segment used to fill a zone area (segment on a copper layer)
Definition: typeinfo.h:97
int GetAreaCount() const
Function GetAreaCount.
Definition: class_board.h:1017
std::shared_ptr< CN_CLUSTER > CN_CLUSTER_PTR
void Add(CN_ITEM *item)
int Net() const
const CONNECTED_ITEMS & ConnectedItems() const
const SHAPE_POLY_SET & GetFilledPolysList() const
Function GetFilledPolysList returns a reference to the list of filled polygons.
Definition: class_zone.h:534
virtual const VECTOR2I GetAnchor(int n) const
void FindIsolatedCopperIslands(ZONE_CONTAINER *aZone, std::vector< int > &aIslands)
void Build(BOARD *aBoard)
bool operator()(CN_ITEM *aCandidate)
virtual bool HitTest(const wxPoint &aPosition) const override
Function HitTest tests if aPosition is contained within or on the bounding area of an item...
const CLUSTERS & GetClusters()
BOARD_CONNECTED_ITEM * Parent() const
void checkZoneZoneConnection(CN_ZONE *aZoneA, CN_ZONE *aZoneB)
ZONE_CONTAINER * GetArea(int index) const
Function GetArea returns the Area (Zone Container) at a given index.
Definition: class_board.h:988
virtual const VECTOR2I GetAnchor(int n) const override
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:169
Struct CN_VISTOR.
virtual bool IsConnected() const
Function IsConnected() Returns information if the object is derived from BOARD_CONNECTED_ITEM.
class NETINFO_ITEM, a description of a net
Definition: typeinfo.h:104
size_t i
Definition: json11.cpp:597
static void Connect(CN_ITEM *a, CN_ITEM *b)
virtual int AnchorCount() const
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
DLIST_ITERATOR_WRAPPER< TRACK > Tracks()
Definition: class_board.h:252
bool CanChangeNet() const
bool IsEmpty() const
Returns true if the set is empty (no polygons at all)
std::shared_ptr< CN_ANCHOR > CN_ANCHOR_PTR
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
std::vector< CN_CLUSTER_PTR > CLUSTERS
#define mod(a, n)
Definition: greymap.cpp:24
void RemoveInvalidRefs()
void SetProgressReporter(PROGRESS_REPORTER *aReporter)