KiCad PCB EDA Suite
CN_CONNECTIVITY_ALGO Class Reference

#include <connectivity_algo.h>

Classes

class  ITEM_MAP_ENTRY
 

Public Types

enum  CLUSTER_SEARCH_MODE { CSM_PROPAGATE, CSM_CONNECTIVITY_CHECK, CSM_RATSNEST }
 
using CLUSTERS = std::vector< CN_CLUSTER_PTR >
 

Public Member Functions

 CN_CONNECTIVITY_ALGO ()
 
 ~CN_CONNECTIVITY_ALGO ()
 
bool ItemExists (const BOARD_CONNECTED_ITEM *aItem) const
 
ITEM_MAP_ENTRYItemEntry (const BOARD_CONNECTED_ITEM *aItem)
 
bool IsNetDirty (int aNet) const
 
void ClearDirtyFlags ()
 
void GetDirtyClusters (CLUSTERS &aClusters) const
 
int NetCount () const
 
void Build (BOARD *aBoard)
 
void Build (const std::vector< BOARD_ITEM * > &aItems)
 
void Clear ()
 
bool Remove (BOARD_ITEM *aItem)
 
bool Add (BOARD_ITEM *aItem)
 
const CLUSTERS SearchClusters (CLUSTER_SEARCH_MODE aMode, const KICAD_T aTypes[], int aSingleNet)
 
const CLUSTERS SearchClusters (CLUSTER_SEARCH_MODE aMode)
 
void PropagateNets (BOARD_COMMIT *aCommit=nullptr)
 Propagates nets from pads to other items in clusters. More...
 
void FindIsolatedCopperIslands (ZONE_CONTAINER *aZone, PCB_LAYER_ID aLayer, std::vector< int > &aIslands)
 
void FindIsolatedCopperIslands (std::vector< CN_ZONE_ISOLATED_ISLAND_LIST > &aZones)
 Finds the copper islands that are not connected to a net. More...
 
const CLUSTERSGetClusters ()
 
const CN_LISTItemList () const
 
template<typename Func >
void ForEachAnchor (Func &&aFunc) const
 
template<typename Func >
void ForEachItem (Func &&aFunc) const
 
void MarkNetAsDirty (int aNet)
 
void SetProgressReporter (PROGRESS_REPORTER *aReporter)
 

Private Member Functions

void searchConnections ()
 
void update ()
 
void propagateConnections (BOARD_COMMIT *aCommit=nullptr)
 
template<class Container , class BItem >
void add (Container &c, BItem brditem)
 
void markItemNetAsDirty (const BOARD_ITEM *aItem)
 

Private Attributes

CN_LIST m_itemList
 
std::unordered_map< const BOARD_ITEM *, ITEM_MAP_ENTRYm_itemMap
 
CLUSTERS m_connClusters
 
CLUSTERS m_ratsnestClusters
 
std::vector< bool > m_dirtyNets
 
PROGRESS_REPORTERm_progressReporter = nullptr
 

Detailed Description

Definition at line 115 of file connectivity_algo.h.

Member Typedef Documentation

◆ CLUSTERS

Definition at line 125 of file connectivity_algo.h.

Member Enumeration Documentation

◆ CLUSTER_SEARCH_MODE

Constructor & Destructor Documentation

◆ CN_CONNECTIVITY_ALGO()

CN_CONNECTIVITY_ALGO::CN_CONNECTIVITY_ALGO ( )
inline

Definition at line 186 of file connectivity_algo.h.

186 {}

◆ ~CN_CONNECTIVITY_ALGO()

CN_CONNECTIVITY_ALGO::~CN_CONNECTIVITY_ALGO ( )
inline

Definition at line 187 of file connectivity_algo.h.

187 { Clear(); }

References Clear().

Member Function Documentation

◆ add()

template<class Container , class BItem >
void CN_CONNECTIVITY_ALGO::add ( Container &  c,
BItem  brditem 
)
inlineprivate

Definition at line 175 of file connectivity_algo.h.

176  {
177  auto item = c.Add( brditem );
178 
179  m_itemMap[ brditem ] = ITEM_MAP_ENTRY( item );
180  }
std::unordered_map< const BOARD_ITEM *, ITEM_MAP_ENTRY > m_itemMap

References m_itemMap.

Referenced by Add().

◆ Add()

bool CN_CONNECTIVITY_ALGO::Add ( BOARD_ITEM aItem)

Definition at line 114 of file connectivity_algo.cpp.

115 {
116  if( !aItem->IsOnCopperLayer() )
117  return false;
118 
119  markItemNetAsDirty ( aItem );
120 
121  switch( aItem->Type() )
122  {
123  case PCB_NETINFO_T:
124  {
125  MarkNetAsDirty( static_cast<NETINFO_ITEM*>( aItem )->GetNet() );
126  break;
127  }
128  case PCB_MODULE_T:
129  for( auto pad : static_cast<MODULE*>( aItem ) -> Pads() )
130  {
131  if( m_itemMap.find( pad ) != m_itemMap.end() )
132  return false;
133 
134  add( m_itemList, pad );
135  }
136 
137  break;
138 
139  case PCB_PAD_T:
140  if( m_itemMap.find ( aItem ) != m_itemMap.end() )
141  return false;
142 
143  add( m_itemList, static_cast<D_PAD*>( aItem ) );
144 
145  break;
146 
147  case PCB_TRACE_T:
148  {
149  if( m_itemMap.find( aItem ) != m_itemMap.end() )
150  return false;
151 
152  add( m_itemList, static_cast<TRACK*>( aItem ) );
153 
154  break;
155  }
156 
157  case PCB_ARC_T:
158  {
159  if( m_itemMap.find( aItem ) != m_itemMap.end() )
160  return false;
161 
162  add( m_itemList, static_cast<ARC*>( aItem ) );
163 
164  break;
165  }
166 
167  case PCB_VIA_T:
168  if( m_itemMap.find( aItem ) != m_itemMap.end() )
169  return false;
170 
171  add( m_itemList, static_cast<VIA*>( aItem ) );
172 
173  break;
174 
175  case PCB_ZONE_AREA_T:
176  {
177  auto zone = static_cast<ZONE_CONTAINER*>( aItem );
178 
179  if( m_itemMap.find( aItem ) != m_itemMap.end() )
180  return false;
181 
182  m_itemMap[zone] = ITEM_MAP_ENTRY();
183 
184  for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
185  for( auto zitem : m_itemList.Add( zone, layer ) )
186  m_itemMap[zone].Link( zitem );
187 
188  break;
189  }
190 
191  default:
192  return false;
193  }
194 
195  return true;
196 }
CN_ITEM * Add(D_PAD *pad)
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:102
class ARC, an arc track segment on a copper layer
Definition: typeinfo.h:98
void MarkNetAsDirty(int aNet)
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
void add(Container &c, BItem brditem)
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
class MODULE, a footprint
Definition: typeinfo.h:89
PCB_LAYER_ID
A quick note on layer IDs:
void markItemNetAsDirty(const BOARD_ITEM *aItem)
class NETINFO_ITEM, a description of a net
Definition: typeinfo.h:104
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
virtual bool IsOnCopperLayer() const
std::unordered_map< const BOARD_ITEM *, ITEM_MAP_ENTRY > m_itemMap
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193

References add(), CN_LIST::Add(), BOARD_ITEM::IsOnCopperLayer(), m_itemList, m_itemMap, markItemNetAsDirty(), MarkNetAsDirty(), PCB_ARC_T, PCB_MODULE_T, PCB_NETINFO_T, PCB_PAD_T, PCB_TRACE_T, PCB_VIA_T, PCB_ZONE_AREA_T, and EDA_ITEM::Type().

Referenced by Build(), and FindIsolatedCopperIslands().

◆ Build() [1/2]

void CN_CONNECTIVITY_ALGO::Build ( BOARD aBoard)

Definition at line 410 of file connectivity_algo.cpp.

411 {
412  for( int i = 0; i<aBoard->GetAreaCount(); i++ )
413  {
414  auto zone = aBoard->GetArea( i );
415  Add( zone );
416  }
417 
418  for( auto tv : aBoard->Tracks() )
419  Add( tv );
420 
421  for( auto mod : aBoard->Modules() )
422  {
423  for( auto pad : mod->Pads() )
424  Add( pad );
425  }
426 
427  /*wxLogTrace( "CN", "zones : %lu, pads : %lu vias : %lu tracks : %lu\n",
428  m_zoneList.Size(), m_padList.Size(),
429  m_viaList.Size(), m_trackList.Size() );*/
430 }
bool Add(BOARD_ITEM *aItem)
int GetAreaCount() const
Function GetAreaCount.
Definition: class_board.h:932
MODULES & Modules()
Definition: class_board.h:266
ZONE_CONTAINER * GetArea(int index) const
Function GetArea returns the Area (Zone Container) at a given index.
Definition: class_board.h:897
TRACKS & Tracks()
Definition: class_board.h:257

References Add(), BOARD::GetArea(), BOARD::GetAreaCount(), BOARD::Modules(), and BOARD::Tracks().

◆ Build() [2/2]

void CN_CONNECTIVITY_ALGO::Build ( const std::vector< BOARD_ITEM * > &  aItems)

Definition at line 433 of file connectivity_algo.cpp.

434 {
435  for( auto item : aItems )
436  {
437  switch( item->Type() )
438  {
439  case PCB_TRACE_T:
440  case PCB_ARC_T:
441  case PCB_VIA_T:
442  case PCB_PAD_T:
443  Add( item );
444  break;
445 
446  case PCB_MODULE_T:
447  {
448  for( auto pad : static_cast<MODULE*>( item )->Pads() )
449  {
450  Add( pad );
451  }
452 
453  break;
454  }
455 
456  default:
457  break;
458  }
459  }
460 }
class ARC, an arc track segment on a copper layer
Definition: typeinfo.h:98
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
bool Add(BOARD_ITEM *aItem)
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
class MODULE, a footprint
Definition: typeinfo.h:89
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97

References Add(), PCB_ARC_T, PCB_MODULE_T, PCB_PAD_T, PCB_TRACE_T, and PCB_VIA_T.

◆ Clear()

void CN_CONNECTIVITY_ALGO::Clear ( )

Definition at line 751 of file connectivity_algo.cpp.

752 {
753  m_ratsnestClusters.clear();
754  m_connClusters.clear();
755  m_itemMap.clear();
756  m_itemList.Clear();
757 
758 }
std::unordered_map< const BOARD_ITEM *, ITEM_MAP_ENTRY > m_itemMap

References CN_LIST::Clear(), m_connClusters, m_itemList, m_itemMap, and m_ratsnestClusters.

Referenced by ~CN_CONNECTIVITY_ALGO().

◆ ClearDirtyFlags()

void CN_CONNECTIVITY_ALGO::ClearDirtyFlags ( )
inline

Definition at line 207 of file connectivity_algo.h.

208  {
209  for( auto i = m_dirtyNets.begin(); i != m_dirtyNets.end(); ++i )
210  *i = false;
211  }
std::vector< bool > m_dirtyNets

References m_dirtyNets.

◆ FindIsolatedCopperIslands() [1/2]

void CN_CONNECTIVITY_ALGO::FindIsolatedCopperIslands ( ZONE_CONTAINER aZone,
PCB_LAYER_ID  aLayer,
std::vector< int > &  aIslands 
)

Definition at line 522 of file connectivity_algo.cpp.

525 {
526  if( aZone->GetFilledPolysList( aLayer ).IsEmpty() )
527  return;
528 
529  aIslands.clear();
530 
531  Remove( aZone );
532  Add( aZone );
533 
535 
536  for( const auto& cluster : m_connClusters )
537  {
538  if( cluster->Contains( aZone ) && cluster->IsOrphaned() )
539  {
540  for( auto z : *cluster )
541  {
542  if( z->Parent() == aZone && z->Layer() == aLayer )
543  {
544  aIslands.push_back( static_cast<CN_ZONE*>(z)->SubpolyIndex() );
545  }
546  }
547  }
548  }
549 
550  wxLogTrace( "CN", "Found %u isolated islands\n", (unsigned)aIslands.size() );
551 }
bool Remove(BOARD_ITEM *aItem)
bool IsEmpty() const
Returns true if the set is empty (no polygons at all)
const SHAPE_POLY_SET & GetFilledPolysList(PCB_LAYER_ID aLayer) const
Function GetFilledPolysList returns a reference to the list of filled polygons.
Definition: class_zone.h:602
bool Add(BOARD_ITEM *aItem)
const CLUSTERS SearchClusters(CLUSTER_SEARCH_MODE aMode, const KICAD_T aTypes[], int aSingleNet)

References Add(), CSM_CONNECTIVITY_CHECK, ZONE_CONTAINER::GetFilledPolysList(), SHAPE_POLY_SET::IsEmpty(), m_connClusters, Remove(), and SearchClusters().

◆ FindIsolatedCopperIslands() [2/2]

void CN_CONNECTIVITY_ALGO::FindIsolatedCopperIslands ( std::vector< CN_ZONE_ISOLATED_ISLAND_LIST > &  aZones)

Finds the copper islands that are not connected to a net.

These are added to the m_islands vector. N.B. This must be called after aZones has been refreshed.

Parameters
aZones The set of zones to search for islands

Definition at line 553 of file connectivity_algo.cpp.

554 {
555  for( auto& z : aZones )
556  {
557  Remove( z.m_zone );
558  Add( z.m_zone );
559  }
560 
562 
563  for( auto& zone : aZones )
564  {
565  for( PCB_LAYER_ID layer : zone.m_zone->GetLayerSet().Seq() )
566  {
567  if( zone.m_zone->GetFilledPolysList( layer ).IsEmpty() )
568  continue;
569 
570  for( const auto& cluster : m_connClusters )
571  {
572  if( cluster->Contains( zone.m_zone ) && cluster->IsOrphaned() )
573  {
574  for( auto z : *cluster )
575  {
576  if( z->Parent() == zone.m_zone && z->Layer() == layer )
577  {
578  zone.m_islands[layer].push_back(
579  static_cast<CN_ZONE*>( z )->SubpolyIndex() );
580  }
581  }
582  }
583  }
584  }
585  }
586 }
bool Remove(BOARD_ITEM *aItem)
bool Add(BOARD_ITEM *aItem)
const CLUSTERS SearchClusters(CLUSTER_SEARCH_MODE aMode, const KICAD_T aTypes[], int aSingleNet)
PCB_LAYER_ID
A quick note on layer IDs:

References Add(), CSM_CONNECTIVITY_CHECK, m_connClusters, Remove(), and SearchClusters().

◆ ForEachAnchor()

template<typename Func >
void CN_CONNECTIVITY_ALGO::ForEachAnchor ( Func &&  aFunc) const
inline

Definition at line 265 of file connectivity_algo.h.

266  {
267  for( auto&& item : m_itemList )
268  {
269  for( auto&& anchor : item->Anchors() )
270  aFunc( *anchor );
271  }
272  }

References m_itemList.

◆ ForEachItem()

template<typename Func >
void CN_CONNECTIVITY_ALGO::ForEachItem ( Func &&  aFunc) const
inline

Definition at line 275 of file connectivity_algo.h.

276  {
277  for( auto&& item : m_itemList )
278  aFunc( *item );
279  }

References m_itemList.

◆ GetClusters()

const CN_CONNECTIVITY_ALGO::CLUSTERS & CN_CONNECTIVITY_ALGO::GetClusters ( )

Definition at line 589 of file connectivity_algo.cpp.

590 {
592  return m_ratsnestClusters;
593 }
const CLUSTERS SearchClusters(CLUSTER_SEARCH_MODE aMode, const KICAD_T aTypes[], int aSingleNet)

References CSM_RATSNEST, m_ratsnestClusters, and SearchClusters().

◆ GetDirtyClusters()

void CN_CONNECTIVITY_ALGO::GetDirtyClusters ( CLUSTERS aClusters) const
inline

Definition at line 213 of file connectivity_algo.h.

214  {
215  for( const auto& cl : m_ratsnestClusters )
216  {
217  int net = cl->OriginNet();
218 
219  if( net >= 0 && m_dirtyNets[net] )
220  aClusters.push_back( cl );
221  }
222  }
std::vector< bool > m_dirtyNets

References m_dirtyNets, and m_ratsnestClusters.

◆ IsNetDirty()

bool CN_CONNECTIVITY_ALGO::IsNetDirty ( int  aNet) const
inline

Definition at line 199 of file connectivity_algo.h.

200  {
201  if( aNet < 0 )
202  return false;
203 
204  return m_dirtyNets[ aNet ];
205  }
std::vector< bool > m_dirtyNets

References m_dirtyNets.

◆ ItemEntry()

ITEM_MAP_ENTRY& CN_CONNECTIVITY_ALGO::ItemEntry ( const BOARD_CONNECTED_ITEM aItem)
inline

Definition at line 194 of file connectivity_algo.h.

195  {
196  return m_itemMap[ aItem ];
197  }
std::unordered_map< const BOARD_ITEM *, ITEM_MAP_ENTRY > m_itemMap

References m_itemMap.

◆ ItemExists()

bool CN_CONNECTIVITY_ALGO::ItemExists ( const BOARD_CONNECTED_ITEM aItem) const
inline

Definition at line 189 of file connectivity_algo.h.

190  {
191  return m_itemMap.find( aItem ) != m_itemMap.end();
192  }
std::unordered_map< const BOARD_ITEM *, ITEM_MAP_ENTRY > m_itemMap

References m_itemMap.

◆ ItemList()

const CN_LIST& CN_CONNECTIVITY_ALGO::ItemList ( ) const
inline

Definition at line 259 of file connectivity_algo.h.

260  {
261  return m_itemList;
262  }

References m_itemList.

◆ markItemNetAsDirty()

void CN_CONNECTIVITY_ALGO::markItemNetAsDirty ( const BOARD_ITEM aItem)
private

Definition at line 94 of file connectivity_algo.cpp.

95 {
96  if( aItem->IsConnected() )
97  {
98  auto citem = static_cast<const BOARD_CONNECTED_ITEM*>( aItem );
99  MarkNetAsDirty( citem->GetNetCode() );
100  }
101  else
102  {
103  if( aItem->Type() == PCB_MODULE_T )
104  {
105  auto mod = static_cast <const MODULE*>( aItem );
106 
107  for( auto pad : mod->Pads() )
108  MarkNetAsDirty( pad->GetNetCode() );
109  }
110  }
111 }
void MarkNetAsDirty(int aNet)
class MODULE, a footprint
Definition: typeinfo.h:89
virtual bool IsConnected() const
Function IsConnected() Returns information if the object is derived from BOARD_CONNECTED_ITEM.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193

References BOARD_ITEM::IsConnected(), MarkNetAsDirty(), PCB_MODULE_T, and EDA_ITEM::Type().

Referenced by Add(), and Remove().

◆ MarkNetAsDirty()

void CN_CONNECTIVITY_ALGO::MarkNetAsDirty ( int  aNet)

Definition at line 596 of file connectivity_algo.cpp.

597 {
598  if( aNet < 0 )
599  return;
600 
601  if( (int) m_dirtyNets.size() <= aNet )
602  {
603  int lastNet = m_dirtyNets.size() - 1;
604 
605  if( lastNet < 0 )
606  lastNet = 0;
607 
608  m_dirtyNets.resize( aNet + 1 );
609 
610  for( int i = lastNet; i < aNet + 1; i++ )
611  m_dirtyNets[i] = true;
612  }
613 
614  m_dirtyNets[aNet] = true;
615 }
std::vector< bool > m_dirtyNets

References m_dirtyNets.

Referenced by Add(), markItemNetAsDirty(), and propagateConnections().

◆ NetCount()

int CN_CONNECTIVITY_ALGO::NetCount ( ) const
inline

Definition at line 224 of file connectivity_algo.h.

225  {
226  return m_dirtyNets.size();
227  }
std::vector< bool > m_dirtyNets

References m_dirtyNets.

◆ propagateConnections()

void CN_CONNECTIVITY_ALGO::propagateConnections ( BOARD_COMMIT aCommit = nullptr)
private

Definition at line 463 of file connectivity_algo.cpp.

464 {
465  for( const auto& cluster : m_connClusters )
466  {
467  if( cluster->IsConflicting() )
468  {
469  wxLogTrace( "CN", "Conflicting nets in cluster %p\n", cluster.get() );
470  }
471  else if( cluster->IsOrphaned() )
472  {
473  wxLogTrace( "CN", "Skipping orphaned cluster %p [net: %s]\n", cluster.get(),
474  (const char*) cluster->OriginNetName().c_str() );
475  }
476  else if( cluster->HasValidNet() )
477  {
478  // normal cluster: just propagate from the pads
479  int n_changed = 0;
480 
481  for( auto item : *cluster )
482  {
483  if( item->CanChangeNet() )
484  {
485  if( item->Valid() && item->Parent()->GetNetCode() != cluster->OriginNet() )
486  {
487  MarkNetAsDirty( item->Parent()->GetNetCode() );
488  MarkNetAsDirty( cluster->OriginNet() );
489 
490  if( aCommit )
491  aCommit->Modify( item->Parent() );
492 
493  item->Parent()->SetNetCode( cluster->OriginNet() );
494  n_changed++;
495  }
496  }
497  }
498 
499  if( n_changed )
500  {
501  wxLogTrace( "CN", "Cluster %p : net : %d %s\n", cluster.get(),
502  cluster->OriginNet(), (const char*) cluster->OriginNetName().c_str() );
503  }
504  else
505  wxLogTrace( "CN", "Cluster %p : nothing to propagate\n", cluster.get() );
506  }
507  else
508  {
509  wxLogTrace( "CN", "Cluster %p : connected to unused net\n", cluster.get() );
510  }
511  }
512 }
COMMIT & Modify(EDA_ITEM *aItem)
Modifies a given item in the model.
Definition: commit.h:103
void MarkNetAsDirty(int aNet)

References m_connClusters, MarkNetAsDirty(), and COMMIT::Modify().

Referenced by PropagateNets().

◆ PropagateNets()

void CN_CONNECTIVITY_ALGO::PropagateNets ( BOARD_COMMIT aCommit = nullptr)

Propagates nets from pads to other items in clusters.

Parameters
aCommitis used to store undo information for items modified by the call

Definition at line 515 of file connectivity_algo.cpp.

516 {
518  propagateConnections( aCommit );
519 }
void propagateConnections(BOARD_COMMIT *aCommit=nullptr)
const CLUSTERS SearchClusters(CLUSTER_SEARCH_MODE aMode, const KICAD_T aTypes[], int aSingleNet)

References CSM_PROPAGATE, m_connClusters, propagateConnections(), and SearchClusters().

◆ Remove()

bool CN_CONNECTIVITY_ALGO::Remove ( BOARD_ITEM aItem)

Definition at line 40 of file connectivity_algo.cpp.

41 {
42  markItemNetAsDirty( aItem );
43 
44  switch( aItem->Type() )
45  {
46  case PCB_MODULE_T:
47  for( auto pad : static_cast<MODULE*>( aItem ) -> Pads() )
48  {
49  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( pad ) ].MarkItemsAsInvalid();
50  m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( pad ) );
51  }
52 
53  m_itemList.SetDirty( true );
54  break;
55 
56  case PCB_PAD_T:
57  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
58  m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
59  m_itemList.SetDirty( true );
60  break;
61 
62  case PCB_TRACE_T:
63  case PCB_ARC_T:
64  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
65  m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
66  m_itemList.SetDirty( true );
67  break;
68 
69  case PCB_VIA_T:
70  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
71  m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
72  m_itemList.SetDirty( true );
73  break;
74 
75  case PCB_ZONE_AREA_T:
76  {
77  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
78  m_itemMap.erase ( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
79  m_itemList.SetDirty( true );
80  break;
81  }
82 
83  default:
84  return false;
85  }
86 
87  // Once we delete an item, it may connect between lists, so mark both as potentially invalid
88  m_itemList.SetHasInvalid( true );
89 
90  return true;
91 }
void SetHasInvalid(bool aInvalid=true)
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:102
class ARC, an arc track segment on a copper layer
Definition: typeinfo.h:98
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
void SetDirty(bool aDirty=true)
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
class MODULE, a footprint
Definition: typeinfo.h:89
void markItemNetAsDirty(const BOARD_ITEM *aItem)
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
std::unordered_map< const BOARD_ITEM *, ITEM_MAP_ENTRY > m_itemMap
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193

References m_itemList, m_itemMap, markItemNetAsDirty(), PCB_ARC_T, PCB_MODULE_T, PCB_PAD_T, PCB_TRACE_T, PCB_VIA_T, PCB_ZONE_AREA_T, CN_LIST::SetDirty(), CN_LIST::SetHasInvalid(), and EDA_ITEM::Type().

Referenced by FindIsolatedCopperIslands().

◆ SearchClusters() [1/2]

const CN_CONNECTIVITY_ALGO::CLUSTERS CN_CONNECTIVITY_ALGO::SearchClusters ( CLUSTER_SEARCH_MODE  aMode,
const KICAD_T  aTypes[],
int  aSingleNet 
)

Definition at line 307 of file connectivity_algo.cpp.

309 {
310  bool withinAnyNet = ( aMode != CSM_PROPAGATE );
311 
312  std::deque<CN_ITEM*> Q;
313  CN_ITEM* head = nullptr;
314  CLUSTERS clusters;
315 
316  if( m_itemList.IsDirty() )
318 
319  auto addToSearchList = [&head, withinAnyNet, aSingleNet, aTypes] ( CN_ITEM *aItem )
320  {
321  if( withinAnyNet && aItem->Net() <= 0 )
322  return;
323 
324  if( !aItem->Valid() )
325  return;
326 
327  if( aSingleNet >=0 && aItem->Net() != aSingleNet )
328  return;
329 
330  bool found = false;
331 
332  for( int i = 0; aTypes[i] != EOT; i++ )
333  {
334  if( aItem->Parent()->Type() == aTypes[i] )
335  {
336  found = true;
337  break;
338  }
339  }
340 
341  if( !found )
342  return;
343 
344  aItem->ListClear();
345  aItem->SetVisited( false );
346 
347  if( !head )
348  head = aItem;
349  else
350  head->ListInsert( aItem );
351  };
352 
353  std::for_each( m_itemList.begin(), m_itemList.end(), addToSearchList );
354 
355  while( head )
356  {
357  CN_CLUSTER_PTR cluster ( new CN_CLUSTER() );
358 
359  Q.clear();
360  CN_ITEM* root = head;
361  root->SetVisited ( true );
362 
363  head = root->ListRemove();
364 
365  Q.push_back( root );
366 
367  while( Q.size() )
368  {
369  CN_ITEM* current = Q.front();
370 
371  Q.pop_front();
372  cluster->Add( current );
373 
374  for( auto n : current->ConnectedItems() )
375  {
376  if( withinAnyNet && n->Net() != root->Net() )
377  continue;
378 
379  if( !n->Visited() && n->Valid() )
380  {
381  n->SetVisited( true );
382  Q.push_back( n );
383  head = n->ListRemove();
384  }
385  }
386  }
387 
388  clusters.push_back( cluster );
389  }
390 
391 
392  std::sort( clusters.begin(), clusters.end(), []( CN_CLUSTER_PTR a, CN_CLUSTER_PTR b ) {
393  return a->OriginNet() < b->OriginNet();
394  } );
395 
396 #ifdef CONNECTIVITY_DEBUG
397  printf("Active clusters: %d\n", clusters.size() );
398 
399  for( auto cl : clusters )
400  {
401  printf( "Net %d\n", cl->OriginNet() );
402  cl->Dump();
403  }
404 #endif
405 
406  return clusters;
407 }
const CONNECTED_ITEMS & ConnectedItems() const
void ListInsert(T *item)
void SetVisited(bool aVisited)
bool IsDirty() const
int Net() const
search types array terminator (End Of Types)
Definition: typeinfo.h:82
std::shared_ptr< CN_CLUSTER > CN_CLUSTER_PTR
std::vector< CN_CLUSTER_PTR > CLUSTERS

References CN_LIST::begin(), CN_ITEM::ConnectedItems(), CSM_PROPAGATE, CN_ITEM::Dump(), CN_LIST::end(), EOT, CN_LIST::IsDirty(), INTRUSIVE_LIST< T >::ListInsert(), INTRUSIVE_LIST< T >::ListRemove(), m_itemList, CN_ITEM::Net(), searchConnections(), and CN_ITEM::SetVisited().

Referenced by FindIsolatedCopperIslands(), GetClusters(), PropagateNets(), and SearchClusters().

◆ SearchClusters() [2/2]

const CN_CONNECTIVITY_ALGO::CLUSTERS CN_CONNECTIVITY_ALGO::SearchClusters ( CLUSTER_SEARCH_MODE  aMode)

Definition at line 293 of file connectivity_algo.cpp.

294 {
295  constexpr KICAD_T types[] =
297  constexpr KICAD_T no_zones[] =
299 
300  if( aMode == CSM_PROPAGATE )
301  return SearchClusters( aMode, no_zones, -1 );
302  else
303  return SearchClusters( aMode, types, -1 );
304 }
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:102
class ARC, an arc track segment on a copper layer
Definition: typeinfo.h:98
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
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:96
const CLUSTERS SearchClusters(CLUSTER_SEARCH_MODE aMode, const KICAD_T aTypes[], int aSingleNet)
class MODULE, a footprint
Definition: typeinfo.h:89
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97

References CSM_PROPAGATE, EOT, PCB_ARC_T, PCB_MODULE_T, PCB_PAD_T, PCB_TRACE_T, PCB_VIA_T, PCB_ZONE_AREA_T, and SearchClusters().

◆ searchConnections()

void CN_CONNECTIVITY_ALGO::searchConnections ( )
private

Definition at line 199 of file connectivity_algo.cpp.

200 {
201 #ifdef CONNECTIVITY_DEBUG
202  printf("Search start\n");
203 #endif
204 
205 #ifdef PROFILE
206  PROF_COUNTER garbage_collection( "garbage-collection" );
207 #endif
208  std::vector<CN_ITEM*> garbage;
209  garbage.reserve( 1024 );
210 
211  m_itemList.RemoveInvalidItems( garbage );
212 
213  for( auto item : garbage )
214  delete item;
215 
216 #ifdef PROFILE
217  garbage_collection.Show();
218  PROF_COUNTER search_basic( "search-basic" );
219 #endif
220 
221  std::vector<CN_ITEM*> dirtyItems;
222  std::copy_if( m_itemList.begin(), m_itemList.end(), std::back_inserter( dirtyItems ),
223  [] ( CN_ITEM* aItem ) { return aItem->Dirty(); } );
224 
225  if( m_progressReporter )
226  {
227  m_progressReporter->SetMaxProgress( dirtyItems.size() );
229  }
230 
231  if( m_itemList.IsDirty() )
232  {
233  size_t parallelThreadCount = std::min<size_t>( std::thread::hardware_concurrency(),
234  ( dirtyItems.size() + 7 ) / 8 );
235 
236  std::atomic<size_t> nextItem( 0 );
237  std::vector<std::future<size_t>> returns( parallelThreadCount );
238 
239  auto conn_lambda = [&nextItem, &dirtyItems]
240  ( CN_LIST* aItemList, PROGRESS_REPORTER* aReporter) -> size_t
241  {
242  for( size_t i = nextItem++; i < dirtyItems.size(); i = nextItem++ )
243  {
244  CN_VISITOR visitor( dirtyItems[i] );
245  aItemList->FindNearby( dirtyItems[i], visitor );
246 
247  if( aReporter )
248  aReporter->AdvanceProgress();
249  }
250 
251  return 1;
252  };
253 
254  if( parallelThreadCount <= 1 )
255  conn_lambda( &m_itemList, m_progressReporter );
256  else
257  {
258  for( size_t ii = 0; ii < parallelThreadCount; ++ii )
259  returns[ii] = std::async( std::launch::async, conn_lambda,
261 
262  for( size_t ii = 0; ii < parallelThreadCount; ++ii )
263  {
264  // Here we balance returns with a 100ms timeout to allow UI updating
265  std::future_status status;
266  do
267  {
268  if( m_progressReporter )
270 
271  status = returns[ii].wait_for( std::chrono::milliseconds( 100 ) );
272  } while( status != std::future_status::ready );
273  }
274  }
275 
276  if( m_progressReporter )
278  }
279 
280 #ifdef PROFILE
281  search_basic.Show();
282 #endif
283 
285 
286 #ifdef CONNECTIVITY_DEBUG
287  printf("Search end\n");
288 #endif
289 
290 }
void RemoveInvalidItems(std::vector< CN_ITEM * > &aGarbage)
A progress reporter for use in multi-threaded environments.
void ClearDirtyFlags()
bool IsDirty() const
The class PROF_COUNTER is a small class to help profiling.
Definition: profile.h:44
Struct CN_VISTOR.
bool KeepRefreshing(bool aWait=false)
Update the UI dialog.
void SetMaxProgress(int aMaxProgress)
Fix the value thar gives the 100 precent progress bar length (inside the current virtual zone)
PROGRESS_REPORTER * m_progressReporter

References CN_LIST::begin(), CN_LIST::ClearDirtyFlags(), CN_LIST::end(), CN_LIST::IsDirty(), PROGRESS_REPORTER::KeepRefreshing(), m_itemList, m_progressReporter, CN_LIST::RemoveInvalidItems(), PROGRESS_REPORTER::SetMaxProgress(), and PROF_COUNTER::Show().

Referenced by SearchClusters().

◆ SetProgressReporter()

void CN_CONNECTIVITY_ALGO::SetProgressReporter ( PROGRESS_REPORTER aReporter)

Definition at line 760 of file connectivity_algo.cpp.

761 {
762  m_progressReporter = aReporter;
763 }
PROGRESS_REPORTER * m_progressReporter

References m_progressReporter.

◆ update()

void CN_CONNECTIVITY_ALGO::update ( )
private

Member Data Documentation

◆ m_connClusters

CLUSTERS CN_CONNECTIVITY_ALGO::m_connClusters
private

◆ m_dirtyNets

std::vector<bool> CN_CONNECTIVITY_ALGO::m_dirtyNets
private

◆ m_itemList

CN_LIST CN_CONNECTIVITY_ALGO::m_itemList
private

◆ m_itemMap

std::unordered_map<const BOARD_ITEM*, ITEM_MAP_ENTRY> CN_CONNECTIVITY_ALGO::m_itemMap
private

Definition at line 161 of file connectivity_algo.h.

Referenced by add(), Add(), Clear(), ItemEntry(), ItemExists(), and Remove().

◆ m_progressReporter

PROGRESS_REPORTER* CN_CONNECTIVITY_ALGO::m_progressReporter = nullptr
private

Definition at line 166 of file connectivity_algo.h.

Referenced by searchConnections(), and SetProgressReporter().

◆ m_ratsnestClusters

CLUSTERS CN_CONNECTIVITY_ALGO::m_ratsnestClusters
private

Definition at line 164 of file connectivity_algo.h.

Referenced by Clear(), GetClusters(), and GetDirtyClusters().


The documentation for this class was generated from the following files: