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>
27 
28 #include <thread>
29 #include <mutex>
30 
31 #ifdef PROFILE
32 #include <profile.h>
33 #endif
34 
35 #ifdef USE_OPENMP
36 #include <omp.h>
37 #endif /* USE_OPENMP */
38 
39 using namespace std::placeholders;
40 
41 bool operator<( const CN_ANCHOR_PTR& a, const CN_ANCHOR_PTR& b )
42 {
43  if( a->Pos().x == b->Pos().x )
44  return a->Pos().y < b->Pos().y;
45  else
46  return a->Pos().x < b->Pos().x;
47 }
48 
49 
50 bool CN_ANCHOR::IsDirty() const
51 {
52  return m_item->Dirty();
53 }
54 
55 
57 {
58  m_items.reserve( 64 );
59  m_originPad = nullptr;
60  m_originNet = -1;
61  m_conflicting = false;
62 }
63 
64 
66 {
67 }
68 
69 
70 wxString CN_CLUSTER::OriginNetName() const
71 {
72  if( !m_originPad || !m_originPad->Valid() )
73  return "<none>";
74  else
75  return m_originPad->Parent()->GetNetname();
76 }
77 
78 
79 bool CN_CLUSTER::Contains( const CN_ITEM* aItem )
80 {
81  return std::find( m_items.begin(), m_items.end(), aItem ) != m_items.end();
82 }
83 
84 
86 {
87  for( auto item : m_items )
88  {
89  if( item->Valid() && item->Parent() == aItem )
90  return true;
91  }
92 
93  return false;
94 }
95 
96 
98 {
99  printf(" valid: %d, connected: \n", !!Valid());
100 
101  for( auto i : m_connected )
102  {
103  TRACK* t = static_cast<TRACK*>( i->Parent() );
104  printf( " - %p %d\n", t, t->Type() );
105  }
106 }
107 
108 
110 {
111  for( auto item : m_items )
112  {
113  wxLogTrace( "CN", " - item : %p bitem : %p type : %d inet %s\n", item, item->Parent(),
114  item->Parent()->Type(), (const char*) item->Parent()->GetNetname().c_str() );
115  printf( "- item : %p bitem : %p type : %d inet %s\n", item, item->Parent(),
116  item->Parent()->Type(), (const char*) item->Parent()->GetNetname().c_str() );
117  item->Dump();
118  }
119 }
120 
121 
123 {
124  m_items.push_back( item );
125 
126  if( m_originNet < 0 )
127  {
128  m_originNet = item->Net();
129  }
130 
131  if( item->Parent()->Type() == PCB_PAD_T )
132  {
133  if( !m_originPad )
134  {
135  m_originPad = item;
136  m_originNet = item->Net();
137  }
138 
139  if( m_originPad && item->Net() != m_originNet )
140  {
141  m_conflicting = true;
142  }
143  }
144 }
145 
146 
148 {
149 }
150 
151 
153 {
154  Clear();
155 }
156 
157 
159 {
160  markItemNetAsDirty( aItem );
161 
162  switch( aItem->Type() )
163  {
164  case PCB_MODULE_T:
165  for( auto pad : static_cast<MODULE*>( aItem ) -> Pads() )
166  {
167  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( pad ) ].MarkItemsAsInvalid();
168  m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( pad ) );
169  }
170 
171  m_padList.SetDirty( true );
172  break;
173 
174  case PCB_PAD_T:
175  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
176  m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
177  m_padList.SetDirty( true );
178  break;
179 
180  case PCB_TRACE_T:
181  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
182  m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
183  m_trackList.SetDirty( true );
184  break;
185 
186  case PCB_VIA_T:
187  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
188  m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
189  m_viaList.SetDirty( true );
190  break;
191 
192  case PCB_ZONE_AREA_T:
193  case PCB_ZONE_T:
194  {
195  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
196  m_itemMap.erase ( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
197  m_zoneList.SetDirty( true );
198  break;
199  }
200 
201  default:
202  return false;
203  }
204 
205  return true;
206 }
207 
208 
210 {
211  if( aItem->IsConnected() )
212  {
213  auto citem = static_cast<const BOARD_CONNECTED_ITEM*>( aItem );
214  MarkNetAsDirty( citem->GetNetCode() );
215  }
216  else
217  {
218  if( aItem->Type() == PCB_MODULE_T )
219  {
220  auto mod = static_cast <const MODULE*>( aItem );
221 
222  for( D_PAD* pad = mod->PadsList(); pad; pad = pad->Next() )
223  MarkNetAsDirty( pad->GetNetCode() );
224  }
225  }
226 }
227 
228 
230 {
231  markItemNetAsDirty ( aItem );
232 
233  switch( aItem->Type() )
234  {
235  case PCB_NETINFO_T:
236  {
237  MarkNetAsDirty( static_cast<NETINFO_ITEM*>( aItem )->GetNet() );
238  break;
239  }
240  case PCB_MODULE_T:
241  for( auto pad : static_cast<MODULE*>( aItem ) -> Pads() )
242  {
243  if( m_itemMap.find( pad ) != m_itemMap.end() )
244  return false;
245 
246  add( m_padList, pad );
247  }
248 
249  break;
250 
251  case PCB_PAD_T:
252  if( m_itemMap.find ( static_cast<D_PAD*>( aItem ) ) != m_itemMap.end() )
253  return false;
254 
255  add( m_padList, static_cast<D_PAD*>( aItem ) );
256 
257  break;
258 
259  case PCB_TRACE_T:
260  {
261  if( m_itemMap.find( static_cast<TRACK*>( aItem ) ) != m_itemMap.end() )
262  return false;
263 
264  add( m_trackList, static_cast<TRACK*>( aItem ) );
265 
266  break;
267  }
268 
269  case PCB_VIA_T:
270  if( m_itemMap.find( static_cast<VIA*>( aItem ) ) != m_itemMap.end() )
271  return false;
272 
273  add( m_viaList, static_cast<VIA*>( aItem ) );
274 
275  break;
276 
277  case PCB_ZONE_AREA_T:
278  case PCB_ZONE_T:
279  {
280  auto zone = static_cast<ZONE_CONTAINER*>( aItem );
281 
282  if( m_itemMap.find( static_cast<ZONE_CONTAINER*>( aItem ) ) != m_itemMap.end() )
283  return false;
284 
285  m_itemMap[zone] = ITEM_MAP_ENTRY();
286 
287  for( auto zitem : m_zoneList.Add( zone ) )
288  m_itemMap[zone].Link(zitem);
289 
290  break;
291  }
292 
293  default:
294  return false;
295  }
296 
297  return true;
298 }
299 
300 
301 void CN_CONNECTIVITY_ALGO::searchConnections( bool aIncludeZones )
302 {
303  std::mutex cnListLock;
304 
305  int totalDirtyCount = 0;
306 
307  if( m_lastSearchWithZones != aIncludeZones )
308  {
309  m_padList.MarkAllAsDirty();
310  m_viaList.MarkAllAsDirty();
311  m_trackList.MarkAllAsDirty();
312  m_zoneList.MarkAllAsDirty();
313  }
314 
315  m_lastSearchWithZones = aIncludeZones;
316 
317  auto checkForConnection = [ &cnListLock ] ( const CN_ANCHOR_PTR point, CN_ITEM* aRefItem, int aMaxDist = 0 )
318  {
319  const auto parent = aRefItem->Parent();
320 
321  assert( point->Item() );
322  assert( point->Item()->Parent() );
323  assert( aRefItem->Parent() );
324 
325  if( !point->Item()->Valid() )
326  return;
327 
328  if( !aRefItem->Valid() )
329  return;
330 
331  if( parent == point->Item()->Parent() )
332  return;
333 
334  if( !( parent->GetLayerSet() &
335  point->Item()->Parent()->GetLayerSet() ).any() )
336  return;
337 
338  switch( parent->Type() )
339  {
340  case PCB_PAD_T:
341  case PCB_VIA_T:
342 
343  if( parent->HitTest( wxPoint( point->Pos().x, point->Pos().y ) ) )
344  {
345  std::lock_guard<std::mutex> lock( cnListLock );
346  CN_ITEM::Connect( aRefItem, point->Item() );
347  }
348 
349  break;
350 
351  case PCB_TRACE_T:
352  {
353  const auto track = static_cast<TRACK*> ( parent );
354 
355  const VECTOR2I d_start( VECTOR2I( track->GetStart() ) - point->Pos() );
356  const VECTOR2I d_end( VECTOR2I( track->GetEnd() ) - point->Pos() );
357 
358  if( d_start.EuclideanNorm() < aMaxDist
359  || d_end.EuclideanNorm() < aMaxDist )
360  {
361  std::lock_guard<std::mutex> lock( cnListLock );
362  CN_ITEM::Connect( aRefItem, point->Item() );
363  }
364  break;
365  }
366 
367  case PCB_ZONE_T:
368  case PCB_ZONE_AREA_T:
369  {
370  const auto zone = static_cast<ZONE_CONTAINER*> ( parent );
371  auto zoneItem = static_cast<CN_ZONE*> ( aRefItem );
372 
373  if( point->Item()->Net() != parent->GetNetCode() )
374  return;
375 
376  if( !( zone->GetLayerSet() &
377  point->Item()->Parent()->GetLayerSet() ).any() )
378  return;
379 
380  if( zoneItem->ContainsAnchor( point ) )
381  {
382  std::lock_guard<std::mutex> lock( cnListLock );
383  CN_ITEM::Connect( zoneItem, point->Item() );
384  }
385 
386  break;
387 
388  }
389  default :
390  assert( false );
391  }
392  };
393 
394  auto checkInterZoneConnection = [ &cnListLock ] ( CN_ZONE* testedZone, CN_ZONE* aRefZone )
395  {
396  const auto parentZone = static_cast<const ZONE_CONTAINER*>( aRefZone->Parent() );
397 
398  if( testedZone->Parent()->Type () != PCB_ZONE_AREA_T )
399  return;
400 
401  if( testedZone == aRefZone )
402  return;
403 
404  if( testedZone->Parent() == aRefZone->Parent() )
405  return;
406 
407  if( testedZone->Net() != parentZone->GetNetCode() )
408  return; // we only test zones belonging to the same net
409 
410  if( !( testedZone->Parent()->GetLayerSet() & parentZone->GetLayerSet() ).any() )
411  return; // and on same layer
412 
413  const auto& outline = parentZone->GetFilledPolysList().COutline( aRefZone->SubpolyIndex() );
414 
415  for( int i = 0; i < outline.PointCount(); i++ )
416  {
417  if( testedZone->ContainsPoint( outline.CPoint( i ) ) )
418  {
419  std::lock_guard<std::mutex> lock( cnListLock );
420 
421  CN_ITEM::Connect( aRefZone, testedZone );
422  return;
423  }
424  }
425 
426  const auto testedZoneParent = static_cast<const ZONE_CONTAINER*>( testedZone->Parent() );
427 
428  const auto& outline2 = testedZoneParent->GetFilledPolysList().COutline( testedZone->SubpolyIndex() );
429 
430  for( int i = 0; i < outline2.PointCount(); i++ )
431  {
432  if( aRefZone->ContainsPoint( outline2.CPoint( i ) ) )
433  {
434  std::lock_guard<std::mutex> lock( cnListLock );
435 
436  CN_ITEM::Connect( aRefZone, testedZone );
437  return;
438  }
439  }
440  };
441 
442 #ifdef CONNECTIVITY_DEBUG
443  printf("Search start\n");
444 #endif
445 
446 #ifdef PROFILE
447  PROF_COUNTER garbage_collection( "garbage-collection" );
448 #endif
449  std::vector<CN_ITEM*> garbage;
450  garbage.reserve( 1024 );
451 
452  m_padList.RemoveInvalidItems( garbage );
453  m_viaList.RemoveInvalidItems( garbage );
454  m_trackList.RemoveInvalidItems( garbage );
455  m_zoneList.RemoveInvalidItems( garbage );
456 
457  for( auto item : garbage )
458  delete item;
459 
460  //auto all = allItemsInBoard();
461 
462  #ifdef CONNECTIVITY_DEBUG
463  for( auto item : m_padList )
464  if( all.find( item->Parent() ) == all.end() ) { printf("Failing pad : %p\n", item->Parent() ); assert ( false ); }
465 
466  for( auto item : m_viaList )
467  if( all.find( item->Parent() ) == all.end() ) { printf("Failing via : %p\n", item->Parent() ); assert ( false ); }
468 
469  for( auto item : m_trackList )
470  if( all.find( item->Parent() ) == all.end() ) { printf("Failing track : %p\n", item->Parent() ); assert ( false ); }
471 
472  for( auto item : m_zoneList )
473  if( all.find( item->Parent() ) == all.end() ) { printf("Failing zome : %p\n", item->Parent() ); assert ( false ); }
474  #endif
475 
476 #ifdef PROFILE
477  garbage_collection.Show();
478  PROF_COUNTER search_cnt( "search-connections" );
479  PROF_COUNTER search_basic( "search-basic" );
480  PROF_COUNTER search_pads( "search-pads" );
481 #endif
482 
483  if( m_padList.IsDirty() || m_trackList.IsDirty() || m_viaList.IsDirty() )
484  {
485  totalDirtyCount++;
486 
487  for( auto padItem : m_padList )
488  {
489  auto pad = static_cast<D_PAD*> ( padItem->Parent() );
490  auto searchPads = std::bind( checkForConnection, _1, padItem );
491 
492  m_padList.FindNearby( pad->ShapePos(), pad->GetBoundingRadius(), searchPads, pad->GetLayerSet() );
493  m_trackList.FindNearby( pad->ShapePos(), pad->GetBoundingRadius(), searchPads, pad->GetLayerSet() );
494  m_viaList.FindNearby( pad->ShapePos(), pad->GetBoundingRadius(), searchPads, pad->GetLayerSet() );
495  }
496 #ifdef PROFILE
497  search_pads.Show();
498  PROF_COUNTER search_tracks( "search-tracks" );
499 #endif
500 
501  for( auto& trackItem : m_trackList )
502  {
503  auto track = static_cast<TRACK*> ( trackItem->Parent() );
504  int dist_max = track->GetWidth() / 2;
505  auto searchTracks = std::bind( checkForConnection, _1, trackItem, dist_max );
506 
507  m_trackList.FindNearby( track->GetStart(), dist_max, searchTracks, track->GetLayerSet() );
508  m_trackList.FindNearby( track->GetEnd(), dist_max, searchTracks, track->GetLayerSet() );
509  }
510 #ifdef PROFILE
511  search_tracks.Show();
512 #endif
513 
514  for( auto& viaItem : m_viaList )
515  {
516  auto via = static_cast<VIA*> ( viaItem->Parent() );
517  int dist_max = via->GetWidth() / 2;
518  auto searchVias = std::bind( checkForConnection, _1, viaItem, dist_max );
519 
520  totalDirtyCount++;
521  m_viaList.FindNearby( via->GetStart(), dist_max, searchVias );
522  m_trackList.FindNearby( via->GetStart(), dist_max, searchVias );
523  }
524  }
525 
526 #ifdef PROFILE
527  search_basic.Show();
528 #endif
529 
530  if( aIncludeZones )
531  {
532  int cnt = 0;
533 
534  if( m_progressReporter )
535  {
536  m_progressReporter->SetMaxProgress( m_zoneList.Size() );
537  }
538 
539  #ifdef USE_OPENMP
540  // launch at least two threads, one to compute, second to update UI
541  #pragma omp parallel num_threads( std::max( omp_get_num_procs(), 2 ) )
542  #endif
543  {
544  #ifdef USE_OPENMP
545  #pragma omp master
546  if (m_progressReporter)
547  {
548  m_progressReporter->KeepRefreshing( true );
549  }
550  #endif
551 
552  #ifdef USE_OPENMP
553  #pragma omp for schedule(dynamic)
554  #endif
555  for(int i = 0; i < m_zoneList.Size(); i++ )
556  {
557  auto item = m_zoneList[i];
558  auto zoneItem = static_cast<CN_ZONE *> (item);
559  auto searchZones = std::bind( checkForConnection, _1, zoneItem );
560 
561  if( zoneItem->Dirty() || m_padList.IsDirty() || m_trackList.IsDirty() || m_viaList.IsDirty() )
562  {
563  totalDirtyCount++;
564  m_viaList.FindNearby( zoneItem->BBox(), searchZones );
565  m_trackList.FindNearby( zoneItem->BBox(), searchZones );
566  m_padList.FindNearby( zoneItem->BBox(), searchZones );
567  m_zoneList.FindNearbyZones( zoneItem->BBox(), std::bind( checkInterZoneConnection, _1, zoneItem ) );
568  }
569 
570  {
571  std::lock_guard<std::mutex> lock( cnListLock );
572  cnt++;
573 
574  if (m_progressReporter)
575  {
576  m_progressReporter->AdvanceProgress();
577  }
578  }
579  }
580  }
581 
582  m_zoneList.ClearDirtyFlags();
583  }
584 
585  m_padList.ClearDirtyFlags();
586  m_viaList.ClearDirtyFlags();
587  m_trackList.ClearDirtyFlags();
588 
589 #ifdef CONNECTIVITY_DEBUG
590  printf("Search end\n");
591 #endif
592 
593 #ifdef PROFILE
594  search_cnt.Show();
595 #endif
596 }
597 
598 
600 {
601  auto lastConn = std::remove_if(m_connected.begin(), m_connected.end(), [] ( CN_ITEM * item) {
602  return !item->Valid();
603  } );
604 
605  m_connected.resize( lastConn - m_connected.begin() );
606 }
607 
608 
609 void CN_LIST::RemoveInvalidItems( std::vector<CN_ITEM*>& aGarbage )
610 {
611  auto lastItem = std::remove_if(m_items.begin(), m_items.end(), [&aGarbage] ( CN_ITEM* item )
612  {
613  if( !item->Valid() )
614  {
615  aGarbage.push_back ( item );
616  return true;
617  }
618 
619  return false;
620  } );
621 
622  if( lastItem != m_items.end())
623  {
624  auto lastAnchor = std::remove_if(m_anchors.begin(), m_anchors.end(),
625  [] ( const CN_ANCHOR_PTR anchor ) {
626  return !anchor->Valid();
627  } );
628 
629  m_anchors.resize( lastAnchor - m_anchors.begin() );
630  for( auto i = 0; i < PCB_LAYER_ID_COUNT; i++ )
631  {
632  lastAnchor = std::remove_if(m_layer_anchors[i].begin(), m_layer_anchors[i].end(),
633  [] ( const CN_ANCHOR_PTR anchor ) {
634  return !anchor->Valid();
635  } );
636 
637  m_layer_anchors[i].resize( lastAnchor - m_layer_anchors[i].begin() );
638  }
639 
640  m_items.resize( lastItem - m_items.begin() );
641  }
642 
643  // fixme: mem leaks
644  for( auto item : m_items )
645  item->RemoveInvalidRefs();
646 }
647 
648 
650 {
651  return m_viaList.IsDirty() || m_trackList.IsDirty() || m_zoneList.IsDirty() || m_padList.IsDirty();
652 }
653 
654 
656 {
658  return SearchClusters( aMode, types, -1 );
659 }
660 
661 
663  const KICAD_T aTypes[], int aSingleNet )
664 {
665  bool includeZones = ( aMode != CSM_PROPAGATE );
666  bool withinAnyNet = ( aMode != CSM_PROPAGATE );
667 
668  std::deque<CN_ITEM*> Q;
669  CN_ITEM* head = nullptr;
670  CLUSTERS clusters;
671 
672  if( isDirty() )
673  searchConnections( includeZones );
674 
675  auto addToSearchList = [&head, withinAnyNet, aSingleNet, aTypes] ( CN_ITEM *aItem )
676  {
677  if( withinAnyNet && aItem->Net() <= 0 )
678  return;
679 
680  if( !aItem->Valid() )
681  return;
682 
683  if( aSingleNet >=0 && aItem->Net() != aSingleNet )
684  return;
685 
686  bool found = false;
687 
688  for( int i = 0; aTypes[i] != EOT; i++ )
689  {
690  if( aItem->Parent()->Type() == aTypes[i] )
691  {
692  found = true;
693  break;
694  }
695  }
696 
697  if( !found )
698  return;
699 
700  aItem->ListClear();
701  aItem->SetVisited( false );
702 
703  if( !head )
704  head = aItem;
705  else
706  head->ListInsert( aItem );
707  };
708 
709  std::for_each( m_padList.begin(), m_padList.end(), addToSearchList );
710  std::for_each( m_trackList.begin(), m_trackList.end(), addToSearchList );
711  std::for_each( m_viaList.begin(), m_viaList.end(), addToSearchList );
712 
713  if( includeZones )
714  {
715  std::for_each( m_zoneList.begin(), m_zoneList.end(), addToSearchList );
716  }
717 
718 
719  while( head )
720  {
721  CN_CLUSTER_PTR cluster ( new CN_CLUSTER() );
722 
723  Q.clear();
724  CN_ITEM* root = head;
725  root->SetVisited ( true );
726 
727  head = root->ListRemove();
728 
729  Q.push_back( root );
730 
731  while( Q.size() )
732  {
733  CN_ITEM* current = Q.front();
734 
735  Q.pop_front();
736  cluster->Add( current );
737 
738  for( auto n : current->ConnectedItems() )
739  {
740  if( withinAnyNet && n->Net() != root->Net() )
741  continue;
742 
743  if( !n->Visited() && n->Valid() )
744  {
745  n->SetVisited( true );
746  Q.push_back( n );
747  head = n->ListRemove();
748  }
749  }
750  }
751 
752  clusters.push_back( cluster );
753  }
754 
755 
756  std::sort( clusters.begin(), clusters.end(), []( CN_CLUSTER_PTR a, CN_CLUSTER_PTR b ) {
757  return a->OriginNet() < b->OriginNet();
758  } );
759 
760 #ifdef CONNECTIVITY_DEBUG
761  printf("Active clusters: %d\n", clusters.size() );
762 
763  for( auto cl : clusters )
764  {
765  printf( "Net %d\n", cl->OriginNet() );
766  cl->Dump();
767  }
768 #endif
769 
770  return clusters;
771 }
772 
773 
775 {
776  for( int i = 0; i<aBoard->GetAreaCount(); i++ )
777  {
778  auto zone = aBoard->GetArea( i );
779  Add( zone );
780  }
781 
782  for( auto tv : aBoard->Tracks() )
783  Add( tv );
784 
785  for( auto mod : aBoard->Modules() )
786  {
787  for( auto pad : mod->Pads() )
788  Add( pad );
789  }
790 
791  /*wxLogTrace( "CN", "zones : %lu, pads : %lu vias : %lu tracks : %lu\n",
792  m_zoneList.Size(), m_padList.Size(),
793  m_viaList.Size(), m_trackList.Size() );*/
794 }
795 
796 
797 void CN_CONNECTIVITY_ALGO::Build( const std::vector<BOARD_ITEM*>& aItems )
798 {
799  for( auto item : aItems )
800  {
801  switch( item->Type() )
802  {
803  case PCB_TRACE_T:
804  case PCB_VIA_T:
805  case PCB_ZONE_T:
806  case PCB_PAD_T:
807  Add( item );
808  break;
809 
810  case PCB_MODULE_T:
811  {
812  for( auto pad : static_cast<MODULE*>( item )->Pads() )
813  {
814  Add( pad );
815  }
816 
817  break;
818  }
819 
820  default:
821  break;
822  }
823  }
824 }
825 
826 
828 {
829  for( const auto& cluster : m_connClusters )
830  {
831  if( cluster->IsConflicting() )
832  {
833  wxLogTrace( "CN", "Conflicting nets in cluster %p\n", cluster.get() );
834  }
835  else if( cluster->IsOrphaned() )
836  {
837  wxLogTrace( "CN", "Skipping orphaned cluster %p [net: %s]\n", cluster.get(),
838  (const char*) cluster->OriginNetName().c_str() );
839  }
840  else if( cluster->HasValidNet() )
841  {
842  // normal cluster: just propagate from the pads
843  int n_changed = 0;
844 
845  for( auto item : *cluster )
846  {
847  if( item->CanChangeNet() )
848  {
849  if( item->Valid() && item->Parent()->GetNetCode() != cluster->OriginNet() )
850  {
851  MarkNetAsDirty( item->Parent()->GetNetCode() );
852  MarkNetAsDirty( cluster->OriginNet() );
853 
854  item->Parent()->SetNetCode( cluster->OriginNet() );
855  n_changed++;
856  }
857  }
858  }
859 
860  if( n_changed )
861  wxLogTrace( "CN", "Cluster %p : net : %d %s\n", cluster.get(),
862  cluster->OriginNet(), (const char*) cluster->OriginNetName().c_str() );
863  else
864  wxLogTrace( "CN", "Cluster %p : nothing to propagate\n", cluster.get() );
865  }
866  else
867  {
868  wxLogTrace( "CN", "Cluster %p : connected to unused net\n", cluster.get() );
869  }
870  }
871 }
872 
873 
875 {
876  //searchConnections( false );
877  m_connClusters = SearchClusters( CSM_PROPAGATE );
878  propagateConnections();
879 }
880 
881 
882 void CN_CONNECTIVITY_ALGO::FindIsolatedCopperIslands( ZONE_CONTAINER* aZone, std::vector<int>& aIslands )
883 {
884  if( aZone->GetFilledPolysList().IsEmpty() )
885  return;
886 
887  aIslands.clear();
888 
889  Remove( aZone );
890  Add( aZone );
891 
892  m_connClusters = SearchClusters( CSM_CONNECTIVITY_CHECK );
893 
894  for( const auto& cluster : m_connClusters )
895  {
896  if( cluster->Contains( aZone ) && cluster->IsOrphaned() )
897  {
898  for( auto z : *cluster )
899  {
900  if( z->Parent() == aZone )
901  {
902  aIslands.push_back( static_cast<CN_ZONE*>(z)->SubpolyIndex() );
903  }
904  }
905  }
906  }
907 
908  wxLogTrace( "CN", "Found %u isolated islands\n", (unsigned)aIslands.size() );
909 }
910 
911 void CN_CONNECTIVITY_ALGO::FindIsolatedCopperIslands( std::vector<CN_ZONE_ISOLATED_ISLAND_LIST>& aZones )
912 {
913  for ( auto& z : aZones )
914  {
915  if( z.m_zone->GetFilledPolysList().IsEmpty() )
916  continue;
917 
918  Remove( z.m_zone );
919  Add( z.m_zone );
920  }
921 
922  m_connClusters = SearchClusters( CSM_CONNECTIVITY_CHECK );
923 
924  for ( auto& zone : aZones )
925  {
926  if( zone.m_zone->GetFilledPolysList().IsEmpty() )
927  continue;
928 
929  for( const auto& cluster : m_connClusters )
930  {
931  if( cluster->Contains( zone.m_zone ) && cluster->IsOrphaned() )
932  {
933  for( auto z : *cluster )
934  {
935  if( z->Parent() == zone.m_zone )
936  {
937  zone.m_islands.push_back( static_cast<CN_ZONE*>(z)->SubpolyIndex() );
938  }
939  }
940  }
941  }
942  }
943 }
944 
945 
947 {
948  m_ratsnestClusters = SearchClusters( CSM_RATSNEST );
949  return m_ratsnestClusters;
950 }
951 
952 
954 {
955  if( aNet < 0 )
956  return;
957 
958  if( (int) m_dirtyNets.size() <= aNet )
959  {
960  int lastNet = m_dirtyNets.size() - 1;
961 
962  if( lastNet < 0 )
963  lastNet = 0;
964 
965  m_dirtyNets.resize( aNet + 1 );
966 
967  for( int i = lastNet; i < aNet + 1; i++ )
968  m_dirtyNets[i] = true;
969  }
970 
971  m_dirtyNets[aNet] = true;
972 }
973 
974 
976 {
977  if( !m_valid )
978  return 0;
979 
980  return m_parent->Type() == PCB_TRACE_T ? 2 : 1;
981 }
982 
983 
984 const VECTOR2I CN_ITEM::GetAnchor( int n ) const
985 {
986  if( !m_valid )
987  return VECTOR2I();
988 
989  switch( m_parent->Type() )
990  {
991  case PCB_PAD_T:
992  return static_cast<const D_PAD*>( m_parent )->ShapePos();
993  break;
994 
995  case PCB_TRACE_T:
996  {
997  auto tr = static_cast<const TRACK*>( m_parent );
998  return ( n == 0 ? tr->GetStart() : tr->GetEnd() );
999 
1000  break;
1001  }
1002 
1003  case PCB_VIA_T:
1004  return static_cast<const VIA*>( m_parent )->GetStart();
1005 
1006  default:
1007  assert( false );
1008  return VECTOR2I();
1009  }
1010 }
1011 
1012 
1014 {
1015  if( !Valid() )
1016  return 0;
1017 
1018  const auto zone = static_cast<const ZONE_CONTAINER*>( Parent() );
1019  const auto& outline = zone->GetFilledPolysList().COutline( m_subpolyIndex );
1020 
1021  return outline.PointCount() ? 1 : 0;
1022 }
1023 
1024 
1025 const VECTOR2I CN_ZONE::GetAnchor( int n ) const
1026 {
1027  if( !Valid() )
1028  return VECTOR2I();
1029 
1030  const auto zone = static_cast<const ZONE_CONTAINER*> ( Parent() );
1031  const auto& outline = zone->GetFilledPolysList().COutline( m_subpolyIndex );
1032 
1033  return outline.CPoint( 0 );
1034 }
1035 
1036 
1037 int CN_ITEM::Net() const
1038 {
1039  if( !m_parent || !m_valid )
1040  return -1;
1041 
1042  return m_parent->GetNetCode();
1043 }
1044 
1045 
1047 {
1048  assert( m_item->Valid() );
1049  return m_item->Parent();
1050 }
1051 
1052 
1053 bool CN_ANCHOR::Valid() const
1054 {
1055  if( !m_item )
1056  return false;
1057 
1058  return m_item->Valid();
1059 }
1060 
1061 
1063 {
1064  m_ratsnestClusters.clear();
1065  m_connClusters.clear();
1066  m_itemMap.clear();
1067  m_padList.Clear();
1068  m_trackList.Clear();
1069  m_viaList.Clear();
1070  m_zoneList.Clear();
1071 
1072 }
1073 
1074 
1075 void CN_CONNECTIVITY_ALGO::ForEachItem( const std::function<void( CN_ITEM& )>& aFunc )
1076 {
1077  for( auto item : m_padList )
1078  aFunc( *item );
1079 
1080  for( auto item : m_viaList )
1081  aFunc( *item );
1082 
1083  for( auto item : m_trackList )
1084  aFunc( *item );
1085 
1086  for( auto item : m_zoneList )
1087  aFunc( *item );
1088 }
1089 
1090 
1091 void CN_CONNECTIVITY_ALGO::ForEachAnchor( const std::function<void( CN_ANCHOR& )>& aFunc )
1092 {
1093  for( const auto& anchor : m_padList.Anchors() )
1094  aFunc( *anchor );
1095 
1096  for( const auto& anchor : m_viaList.Anchors() )
1097  aFunc( *anchor );
1098 
1099  for( const auto& anchor : m_trackList.Anchors() )
1100  aFunc( *anchor );
1101 
1102  for( const auto& anchor : m_zoneList.Anchors() )
1103  aFunc( *anchor );
1104 }
1105 
1106 
1108 {
1109  if( !m_cluster )
1110  return true;
1111 
1112  // Calculate the item count connected to this anchor.
1113  // m_cluster groups all items connected, but they are not necessary connected
1114  // at this coordinate point (they are only candidates)
1115  BOARD_CONNECTED_ITEM* item_ref = Parent();
1116  LSET layers = item_ref->GetLayerSet() & LSET::AllCuMask();
1117 
1118  // the number of items connected to item_ref at ths anchor point
1119  int connected_items_count = 0;
1120 
1121  // the minimal number of items connected to item_ref
1122  // at this anchor point to decide the anchor is *not* dangling
1123  int minimal_count = 1;
1124 
1125  // a via can be removed if connected to only one other item.
1126  // the minimal_count is therefore 2
1127  if( item_ref->Type() == PCB_VIA_T )
1128  minimal_count = 2;
1129 
1130  for( CN_ITEM* item : *m_cluster )
1131  {
1132  if( !item->Valid() )
1133  continue;
1134 
1135  BOARD_CONNECTED_ITEM* brd_item = item->Parent();
1136 
1137  if( brd_item == item_ref )
1138  continue;
1139 
1140  // count only items on the same layer at this coordinate (especially for zones)
1141  if( !( brd_item->GetLayerSet() & layers ).any() )
1142  continue;
1143 
1144  if( brd_item->Type() == PCB_ZONE_AREA_T )
1145  {
1146  ZONE_CONTAINER* zone = static_cast<ZONE_CONTAINER*>( brd_item );
1147 
1148  if( zone->HitTestInsideZone( wxPoint( Pos() ) ) )
1149  connected_items_count++;
1150  }
1151  else if( brd_item->HitTest( wxPoint( Pos() ) ) )
1152  connected_items_count++;
1153  }
1154 
1155  return connected_items_count < minimal_count;
1156 }
1157 
1159 {
1160  m_progressReporter = aReporter;
1161 }
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:209
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
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
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:251
int SubpolyIndex() 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
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:1012
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)
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
ZONE_CONTAINER * GetArea(int index) const
Function GetArea returns the Area (Zone Container) at a given index.
Definition: class_board.h:983
virtual const VECTOR2I GetAnchor(int n) const override
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:169
int GetWidth() const
Definition: class_track.h:116
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:250
void searchConnections(bool aIncludeZones=false)
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)