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_itemList.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 
209  return true;
210 }
211 
212 
214 {
215  if( aItem->IsConnected() )
216  {
217  auto citem = static_cast<const BOARD_CONNECTED_ITEM*>( aItem );
218  MarkNetAsDirty( citem->GetNetCode() );
219  }
220  else
221  {
222  if( aItem->Type() == PCB_MODULE_T )
223  {
224  auto mod = static_cast <const MODULE*>( aItem );
225 
226  for( D_PAD* pad = mod->PadsList(); pad; pad = pad->Next() )
227  MarkNetAsDirty( pad->GetNetCode() );
228  }
229  }
230 }
231 
232 
234 {
235  if( !IsCopperLayer( aItem->GetLayer() ) )
236  return false;
237 
238  markItemNetAsDirty ( aItem );
239 
240  switch( aItem->Type() )
241  {
242  case PCB_NETINFO_T:
243  {
244  MarkNetAsDirty( static_cast<NETINFO_ITEM*>( aItem )->GetNet() );
245  break;
246  }
247  case PCB_MODULE_T:
248  for( auto pad : static_cast<MODULE*>( aItem ) -> Pads() )
249  {
250  if( m_itemMap.find( pad ) != m_itemMap.end() )
251  return false;
252 
253  add( m_itemList, pad );
254  }
255 
256  break;
257 
258  case PCB_PAD_T:
259  if( m_itemMap.find ( static_cast<D_PAD*>( aItem ) ) != m_itemMap.end() )
260  return false;
261 
262  add( m_itemList, static_cast<D_PAD*>( aItem ) );
263 
264  break;
265 
266  case PCB_TRACE_T:
267  {
268  if( m_itemMap.find( static_cast<TRACK*>( aItem ) ) != m_itemMap.end() )
269  return false;
270 
271  add( m_itemList, static_cast<TRACK*>( aItem ) );
272 
273  break;
274  }
275 
276  case PCB_VIA_T:
277  if( m_itemMap.find( static_cast<VIA*>( aItem ) ) != m_itemMap.end() )
278  return false;
279 
280  add( m_itemList, static_cast<VIA*>( aItem ) );
281 
282  break;
283 
284  case PCB_ZONE_AREA_T:
285  case PCB_ZONE_T:
286  {
287  auto zone = static_cast<ZONE_CONTAINER*>( aItem );
288 
289  if( m_itemMap.find( static_cast<ZONE_CONTAINER*>( aItem ) ) != m_itemMap.end() )
290  return false;
291 
292  m_itemMap[zone] = ITEM_MAP_ENTRY();
293 
294  for( auto zitem : m_itemList.Add( zone ) )
295  m_itemMap[zone].Link(zitem);
296 
297  break;
298  }
299 
300  default:
301  return false;
302  }
303 
304  return true;
305 }
306 
307 
309 {
310 #ifdef CONNECTIVITY_DEBUG
311  printf("Search start\n");
312 #endif
313 
314 #ifdef PROFILE
315  PROF_COUNTER garbage_collection( "garbage-collection" );
316 #endif
317  std::vector<CN_ITEM*> garbage;
318  garbage.reserve( 1024 );
319 
320  m_itemList.RemoveInvalidItems( garbage );
321 
322  for( auto item : garbage )
323  delete item;
324 
325 #ifdef PROFILE
326  garbage_collection.Show();
327  PROF_COUNTER search_cnt( "search-connections" );
328  PROF_COUNTER search_basic( "search-basic" );
329 #endif
330 
331  if( m_progressReporter )
332  {
333  m_progressReporter->SetMaxProgress( m_itemList.IsDirty() ? m_itemList.Size() : 0 );
334  }
335 
336 #ifdef USE_OPENMP
337  #pragma omp parallel num_threads( std::max( omp_get_num_procs(), 2 ) )
338  {
339  if( omp_get_thread_num() == 0 && m_progressReporter )
340  m_progressReporter->KeepRefreshing( true );
341 #endif
342 
343  if( m_itemList.IsDirty() )
344  {
345 #ifdef USE_OPENMP
346  #pragma omp parallel for
347 #endif
348  for( int i = 0; i < m_itemList.Size(); i++ )
349  {
350  auto item = m_itemList[i];
351  if( item->Dirty() )
352  {
353  CN_VISITOR visitor( item, &m_listLock );
354  m_itemList.FindNearby( item, visitor );
355  }
356 
357  if( m_progressReporter )
358  m_progressReporter->AdvanceProgress();
359  }
360  }
361 
362 #ifdef PROFILE
363  search_basic.Show();
364 #endif
365 
366 #ifdef USE_OPENMP
367  }
368 #endif
369 
370  m_itemList.ClearDirtyFlags();
371 
372 #ifdef CONNECTIVITY_DEBUG
373  printf("Search end\n");
374 #endif
375 
376 #ifdef PROFILE
377  search_cnt.Show();
378 #endif
379 }
380 
381 
383 {
384  for( auto it = m_connected.begin(); it != m_connected.end(); )
385  {
386  if( !(*it)->Valid() )
387  it = m_connected.erase( it );
388  else
389  ++it;
390  }
391 }
392 
393 
394 void CN_LIST::RemoveInvalidItems( std::vector<CN_ITEM*>& aGarbage )
395 {
396  if( !m_hasInvalid )
397  return;
398 
399  auto lastItem = std::remove_if(m_items.begin(), m_items.end(), [&aGarbage] ( CN_ITEM* item )
400  {
401  if( !item->Valid() )
402  {
403  aGarbage.push_back ( item );
404  return true;
405  }
406 
407  return false;
408  } );
409 
410  m_items.resize( lastItem - m_items.begin() );
411 
412  // fixme: mem leaks
413  for( auto item : m_items )
414  item->RemoveInvalidRefs();
415 
416  for( auto item : aGarbage )
417  m_index.Remove( item );
418 
419  m_hasInvalid = false;
420 }
421 
422 
424 {
425  return m_itemList.IsDirty();
426 }
427 
428 
430 {
432  constexpr KICAD_T no_zones[] = { PCB_TRACE_T, PCB_PAD_T, PCB_VIA_T, PCB_MODULE_T, EOT };
433 
434  if( aMode == CSM_PROPAGATE )
435  return SearchClusters( aMode, no_zones, -1 );
436  else
437  return SearchClusters( aMode, types, -1 );
438 }
439 
440 
442  const KICAD_T aTypes[], int aSingleNet )
443 {
444  bool withinAnyNet = ( aMode != CSM_PROPAGATE );
445 
446  std::deque<CN_ITEM*> Q;
447  CN_ITEM* head = nullptr;
448  CLUSTERS clusters;
449 
450  if( isDirty() )
451  searchConnections();
452 
453  auto addToSearchList = [&head, withinAnyNet, aSingleNet, aTypes] ( CN_ITEM *aItem )
454  {
455  if( withinAnyNet && aItem->Net() <= 0 )
456  return;
457 
458  if( !aItem->Valid() )
459  return;
460 
461  if( aSingleNet >=0 && aItem->Net() != aSingleNet )
462  return;
463 
464  bool found = false;
465 
466  for( int i = 0; aTypes[i] != EOT; i++ )
467  {
468  if( aItem->Parent()->Type() == aTypes[i] )
469  {
470  found = true;
471  break;
472  }
473  }
474 
475  if( !found )
476  return;
477 
478  aItem->ListClear();
479  aItem->SetVisited( false );
480 
481  if( !head )
482  head = aItem;
483  else
484  head->ListInsert( aItem );
485  };
486 
487  std::for_each( m_itemList.begin(), m_itemList.end(), addToSearchList );
488 
489  while( head )
490  {
491  CN_CLUSTER_PTR cluster ( new CN_CLUSTER() );
492 
493  Q.clear();
494  CN_ITEM* root = head;
495  root->SetVisited ( true );
496 
497  head = root->ListRemove();
498 
499  Q.push_back( root );
500 
501  while( Q.size() )
502  {
503  CN_ITEM* current = Q.front();
504 
505  Q.pop_front();
506  cluster->Add( current );
507 
508  for( auto n : current->ConnectedItems() )
509  {
510  if( withinAnyNet && n->Net() != root->Net() )
511  continue;
512 
513  if( !n->Visited() && n->Valid() )
514  {
515  n->SetVisited( true );
516  Q.push_back( n );
517  head = n->ListRemove();
518  }
519  }
520  }
521 
522  clusters.push_back( cluster );
523  }
524 
525 
526  std::sort( clusters.begin(), clusters.end(), []( CN_CLUSTER_PTR a, CN_CLUSTER_PTR b ) {
527  return a->OriginNet() < b->OriginNet();
528  } );
529 
530 #ifdef CONNECTIVITY_DEBUG
531  printf("Active clusters: %d\n", clusters.size() );
532 
533  for( auto cl : clusters )
534  {
535  printf( "Net %d\n", cl->OriginNet() );
536  cl->Dump();
537  }
538 #endif
539 
540  return clusters;
541 }
542 
543 
545 {
546  for( int i = 0; i<aBoard->GetAreaCount(); i++ )
547  {
548  auto zone = aBoard->GetArea( i );
549  Add( zone );
550  }
551 
552  for( auto tv : aBoard->Tracks() )
553  Add( tv );
554 
555  for( auto mod : aBoard->Modules() )
556  {
557  for( auto pad : mod->Pads() )
558  Add( pad );
559  }
560 
561  /*wxLogTrace( "CN", "zones : %lu, pads : %lu vias : %lu tracks : %lu\n",
562  m_zoneList.Size(), m_padList.Size(),
563  m_viaList.Size(), m_trackList.Size() );*/
564 }
565 
566 
567 void CN_CONNECTIVITY_ALGO::Build( const std::vector<BOARD_ITEM*>& aItems )
568 {
569  for( auto item : aItems )
570  {
571  switch( item->Type() )
572  {
573  case PCB_TRACE_T:
574  case PCB_VIA_T:
575  case PCB_ZONE_T:
576  case PCB_PAD_T:
577  Add( item );
578  break;
579 
580  case PCB_MODULE_T:
581  {
582  for( auto pad : static_cast<MODULE*>( item )->Pads() )
583  {
584  Add( pad );
585  }
586 
587  break;
588  }
589 
590  default:
591  break;
592  }
593  }
594 }
595 
596 
598 {
599  for( const auto& cluster : m_connClusters )
600  {
601  if( cluster->IsConflicting() )
602  {
603  wxLogTrace( "CN", "Conflicting nets in cluster %p\n", cluster.get() );
604  }
605  else if( cluster->IsOrphaned() )
606  {
607  wxLogTrace( "CN", "Skipping orphaned cluster %p [net: %s]\n", cluster.get(),
608  (const char*) cluster->OriginNetName().c_str() );
609  }
610  else if( cluster->HasValidNet() )
611  {
612  // normal cluster: just propagate from the pads
613  int n_changed = 0;
614 
615  for( auto item : *cluster )
616  {
617  if( item->CanChangeNet() )
618  {
619  if( item->Valid() && item->Parent()->GetNetCode() != cluster->OriginNet() )
620  {
621  MarkNetAsDirty( item->Parent()->GetNetCode() );
622  MarkNetAsDirty( cluster->OriginNet() );
623 
624  item->Parent()->SetNetCode( cluster->OriginNet() );
625  n_changed++;
626  }
627  }
628  }
629 
630  if( n_changed )
631  wxLogTrace( "CN", "Cluster %p : net : %d %s\n", cluster.get(),
632  cluster->OriginNet(), (const char*) cluster->OriginNetName().c_str() );
633  else
634  wxLogTrace( "CN", "Cluster %p : nothing to propagate\n", cluster.get() );
635  }
636  else
637  {
638  wxLogTrace( "CN", "Cluster %p : connected to unused net\n", cluster.get() );
639  }
640  }
641 }
642 
643 
645 {
646  m_connClusters = SearchClusters( CSM_PROPAGATE );
647  propagateConnections();
648 }
649 
650 
651 void CN_CONNECTIVITY_ALGO::FindIsolatedCopperIslands( ZONE_CONTAINER* aZone, std::vector<int>& aIslands )
652 {
653  if( aZone->GetFilledPolysList().IsEmpty() )
654  return;
655 
656  aIslands.clear();
657 
658  Remove( aZone );
659  Add( aZone );
660 
661  m_connClusters = SearchClusters( CSM_CONNECTIVITY_CHECK );
662 
663  for( const auto& cluster : m_connClusters )
664  {
665  if( cluster->Contains( aZone ) && cluster->IsOrphaned() )
666  {
667  for( auto z : *cluster )
668  {
669  if( z->Parent() == aZone )
670  {
671  aIslands.push_back( static_cast<CN_ZONE*>(z)->SubpolyIndex() );
672  }
673  }
674  }
675  }
676 
677  wxLogTrace( "CN", "Found %u isolated islands\n", (unsigned)aIslands.size() );
678 }
679 
680 void CN_CONNECTIVITY_ALGO::FindIsolatedCopperIslands( std::vector<CN_ZONE_ISOLATED_ISLAND_LIST>& aZones )
681 {
682  for ( auto& z : aZones )
683  Remove( z.m_zone );
684 
685  for ( auto& z : aZones )
686  {
687  if( !z.m_zone->GetFilledPolysList().IsEmpty() )
688  Add( z.m_zone );
689  }
690 
691  m_connClusters = SearchClusters( CSM_CONNECTIVITY_CHECK );
692 
693  for ( auto& zone : aZones )
694  {
695  if( zone.m_zone->GetFilledPolysList().IsEmpty() )
696  continue;
697 
698  for( const auto& cluster : m_connClusters )
699  {
700  if( cluster->Contains( zone.m_zone ) && cluster->IsOrphaned() )
701  {
702  for( auto z : *cluster )
703  {
704  if( z->Parent() == zone.m_zone )
705  {
706  zone.m_islands.push_back( static_cast<CN_ZONE*>(z)->SubpolyIndex() );
707  }
708  }
709  }
710  }
711  }
712 }
713 
714 
716 {
717  m_ratsnestClusters = SearchClusters( CSM_RATSNEST );
718  return m_ratsnestClusters;
719 }
720 
721 
723 {
724  if( aNet < 0 )
725  return;
726 
727  if( (int) m_dirtyNets.size() <= aNet )
728  {
729  int lastNet = m_dirtyNets.size() - 1;
730 
731  if( lastNet < 0 )
732  lastNet = 0;
733 
734  m_dirtyNets.resize( aNet + 1 );
735 
736  for( int i = lastNet; i < aNet + 1; i++ )
737  m_dirtyNets[i] = true;
738  }
739 
740  m_dirtyNets[aNet] = true;
741 }
742 
743 
745 {
746  auto zoneItem = static_cast<CN_ZONE*> ( aZone );
747 
748  if( zoneItem->Net() != aItem->Net() && !aItem->CanChangeNet() )
749  return;
750 
751  if( zoneItem->ContainsPoint( aItem->GetAnchor( 0 ) ) ||
752  ( aItem->Parent()->Type() == PCB_TRACE_T &&
753  zoneItem->ContainsPoint( aItem->GetAnchor( 1 ) ) ) )
754  {
755  std::lock_guard<std::mutex> lock( *m_listLock );
756  CN_ITEM::Connect( zoneItem, aItem );
757  }
758 }
759 
761 {
762  const auto refParent = static_cast<const ZONE_CONTAINER*>( aZoneA->Parent() );
763  const auto testedParent = static_cast<const ZONE_CONTAINER*>( aZoneB->Parent() );
764 
765  if( testedParent->Type () != PCB_ZONE_AREA_T )
766  return;
767 
768  if( aZoneB == aZoneA || refParent == testedParent )
769  return;
770 
771  if( aZoneB->Net() != aZoneA->Net() )
772  return; // we only test zones belonging to the same net
773 
774  const auto& outline = refParent->GetFilledPolysList().COutline( aZoneA->SubpolyIndex() );
775 
776  for( int i = 0; i < outline.PointCount(); i++ )
777  {
778  if( aZoneB->ContainsPoint( outline.CPoint( i ) ) )
779  {
780  std::lock_guard<std::mutex> lock( *m_listLock );
781  CN_ITEM::Connect( aZoneA, aZoneB );
782  return;
783  }
784  }
785 
786  const auto& outline2 = testedParent->GetFilledPolysList().COutline( aZoneB->SubpolyIndex() );
787 
788  for( int i = 0; i < outline2.PointCount(); i++ )
789  {
790  if( aZoneA->ContainsPoint( outline2.CPoint( i ) ) )
791  {
792  std::lock_guard<std::mutex> lock( *m_listLock );
793  CN_ITEM::Connect( aZoneA, aZoneB );
794  return;
795  }
796  }
797 }
798 
799 
800 bool CN_VISITOR::operator()( CN_ITEM* aCandidate )
801 {
802  const auto parentA = aCandidate->Parent();
803  const auto parentB = m_item->Parent();
804 
805  if( !aCandidate->Valid() || !m_item->Valid() )
806  return true;
807 
808  if( parentA == parentB )
809  return true;
810 
811  if( !( parentA->GetLayerSet() & parentB->GetLayerSet() ).any() )
812  return true;
813 
814  // If both m_item and aCandidate are marked dirty, they will both be searched
815  // Since we are reciprocal in our connection, we arbitrarily pick one of the connections
816  // to conduct the expensive search
817  if( aCandidate->Dirty() && aCandidate < m_item )
818  return true;
819 
820  // We should handle zone-zone connection separately
821  if ( ( parentA->Type() == PCB_ZONE_AREA_T || parentA->Type() == PCB_ZONE_T ) &&
822  ( parentB->Type() == PCB_ZONE_AREA_T || parentB->Type() == PCB_ZONE_T ) )
823  {
824  checkZoneZoneConnection( static_cast<CN_ZONE*>( m_item ),
825  static_cast<CN_ZONE*>( aCandidate ) );
826  return true;
827  }
828 
829  if( parentA->Type() == PCB_ZONE_AREA_T || parentA->Type() == PCB_ZONE_T)
830  {
831  checkZoneItemConnection( static_cast<CN_ZONE*>( aCandidate ), m_item );
832  return true;
833  }
834 
835  if( parentB->Type() == PCB_ZONE_AREA_T || parentB->Type() == PCB_ZONE_T)
836  {
837  checkZoneItemConnection( static_cast<CN_ZONE*>( m_item ), aCandidate );
838  return true;
839  }
840 
841  // Items do not necessarily have reciprocity as we only check for anchors
842  // therefore, we check HitTest both directions A->B & B->A
843  // TODO: Check for collision geometry on extended features
844  wxPoint ptA1( aCandidate->GetAnchor( 0 ).x, aCandidate->GetAnchor( 0 ).y );
845  wxPoint ptA2( aCandidate->GetAnchor( 1 ).x, aCandidate->GetAnchor( 1 ).y );
846  wxPoint ptB1( m_item->GetAnchor( 0 ).x, m_item->GetAnchor( 0 ).y );
847  wxPoint ptB2( m_item->GetAnchor( 1 ).x, m_item->GetAnchor( 1 ).y );
848  if( parentA->HitTest( ptB1 ) || parentB->HitTest( ptA1 ) ||
849  ( parentA->Type() == PCB_TRACE_T && parentB->HitTest( ptA2 ) ) ||
850  ( parentB->Type() == PCB_TRACE_T && parentA->HitTest( ptB2 ) ) )
851  {
852  std::lock_guard<std::mutex> lock( *m_listLock );
853  CN_ITEM::Connect( m_item, aCandidate );
854  }
855 
856  return true;
857 };
858 
859 
861 {
862  if( !m_valid )
863  return 0;
864 
865  return m_parent->Type() == PCB_TRACE_T ? 2 : 1;
866 }
867 
868 
869 const VECTOR2I CN_ITEM::GetAnchor( int n ) const
870 {
871  if( !m_valid )
872  return VECTOR2I();
873 
874  switch( m_parent->Type() )
875  {
876  case PCB_PAD_T:
877  return static_cast<const D_PAD*>( m_parent )->ShapePos();
878  break;
879 
880  case PCB_TRACE_T:
881  {
882  auto tr = static_cast<const TRACK*>( m_parent );
883  return ( n == 0 ? tr->GetStart() : tr->GetEnd() );
884 
885  break;
886  }
887 
888  case PCB_VIA_T:
889  return static_cast<const VIA*>( m_parent )->GetStart();
890 
891  default:
892  assert( false );
893  return VECTOR2I();
894  }
895 }
896 
897 
899 {
900  if( !Valid() )
901  return 0;
902 
903  const auto zone = static_cast<const ZONE_CONTAINER*>( Parent() );
904  const auto& outline = zone->GetFilledPolysList().COutline( m_subpolyIndex );
905 
906  return outline.PointCount() ? 1 : 0;
907 }
908 
909 
910 const VECTOR2I CN_ZONE::GetAnchor( int n ) const
911 {
912  if( !Valid() )
913  return VECTOR2I();
914 
915  const auto zone = static_cast<const ZONE_CONTAINER*> ( Parent() );
916  const auto& outline = zone->GetFilledPolysList().COutline( m_subpolyIndex );
917 
918  return outline.CPoint( 0 );
919 }
920 
921 
922 int CN_ITEM::Net() const
923 {
924  if( !m_parent || !m_valid )
925  return -1;
926 
927  return m_parent->GetNetCode();
928 }
929 
930 
932 {
933  assert( m_item->Valid() );
934  return m_item->Parent();
935 }
936 
937 
938 bool CN_ANCHOR::Valid() const
939 {
940  if( !m_item )
941  return false;
942 
943  return m_item->Valid();
944 }
945 
946 
948 {
949  m_ratsnestClusters.clear();
950  m_connClusters.clear();
951  m_itemMap.clear();
952  m_itemList.Clear();
953 
954 }
955 
956 
957 void CN_CONNECTIVITY_ALGO::ForEachItem( const std::function<void( CN_ITEM& )>& aFunc )
958 {
959  for( auto item : m_itemList )
960  aFunc( *item );
961 }
962 
963 
964 void CN_CONNECTIVITY_ALGO::ForEachAnchor( const std::function<void( CN_ANCHOR& )>& aFunc )
965 {
966  ForEachItem( [aFunc] ( CN_ITEM& item ) {
967  for( const auto& anchor : item.Anchors() )
968  aFunc( *anchor );
969  }
970  );
971 }
972 
973 
975 {
976  if( !m_cluster )
977  return true;
978 
979  // Calculate the item count connected to this anchor.
980  // m_cluster groups all items connected, but they are not necessary connected
981  // at this coordinate point (they are only candidates)
982  BOARD_CONNECTED_ITEM* item_ref = Parent();
983  LSET layers = item_ref->GetLayerSet() & LSET::AllCuMask();
984 
985  // the number of items connected to item_ref at ths anchor point
986  int connected_items_count = 0;
987 
988  // the minimal number of items connected to item_ref
989  // at this anchor point to decide the anchor is *not* dangling
990  int minimal_count = 1;
991 
992  // a via can be removed if connected to only one other item.
993  // the minimal_count is therefore 2
994  if( item_ref->Type() == PCB_VIA_T )
995  minimal_count = 2;
996 
997  for( CN_ITEM* item : *m_cluster )
998  {
999  if( !item->Valid() )
1000  continue;
1001 
1002  BOARD_CONNECTED_ITEM* brd_item = item->Parent();
1003 
1004  if( brd_item == item_ref )
1005  continue;
1006 
1007  // count only items on the same layer at this coordinate (especially for zones)
1008  if( !( brd_item->GetLayerSet() & layers ).any() )
1009  continue;
1010 
1011  if( brd_item->Type() == PCB_ZONE_AREA_T )
1012  {
1013  ZONE_CONTAINER* zone = static_cast<ZONE_CONTAINER*>( brd_item );
1014 
1015  if( zone->HitTestInsideZone( wxPoint( Pos() ) ) )
1016  connected_items_count++;
1017  }
1018  else if( brd_item->HitTest( wxPoint( Pos() ) ) )
1019  connected_items_count++;
1020  }
1021 
1022  return connected_items_count < minimal_count;
1023 }
1024 
1026 {
1027  m_progressReporter = aReporter;
1028 }
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...
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
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:587
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:254
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:262
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:1020
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:540
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:991
virtual const VECTOR2I GetAnchor(int n) const override
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:170
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)
bool IsCopperLayer(LAYER_NUM aLayerId)
Function IsCopperLayer tests whether a layer is a copper layer.
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:253
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
bool Dirty() const
void RemoveInvalidRefs()
void SetProgressReporter(PROGRESS_REPORTER *aReporter)