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)
 
ITEM_MAP_ENTRYItemEntry (const BOARD_CONNECTED_ITEM *aItem)
 
bool IsNetDirty (int aNet) const
 
void ClearDirtyFlags ()
 
void GetDirtyClusters (CLUSTERS &aClusters)
 
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, 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...
 
bool CheckConnectivity (std::vector< CN_DISJOINT_NET_ENTRY > &aReport)
 
const CLUSTERSGetClusters ()
 
int GetUnconnectedCount ()
 
CN_LISTItemList ()
 
void ForEachAnchor (const std::function< void(CN_ANCHOR &)> &aFunc)
 
void ForEachItem (const std::function< void(CN_ITEM &)> &aFunc)
 
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_CONNECTED_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 102 of file connectivity_algo.h.

Member Typedef Documentation

◆ CLUSTERS

Definition at line 112 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 173 of file connectivity_algo.h.

173 {}

◆ ~CN_CONNECTIVITY_ALGO()

CN_CONNECTIVITY_ALGO::~CN_CONNECTIVITY_ALGO ( )
inline

Definition at line 174 of file connectivity_algo.h.

174 { 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 162 of file connectivity_algo.h.

163  {
164  auto item = c.Add( brditem );
165 
166  m_itemMap[ brditem ] = ITEM_MAP_ENTRY( item );
167  }
std::unordered_map< const BOARD_CONNECTED_ITEM *, ITEM_MAP_ENTRY > m_itemMap

References m_itemMap.

Referenced by Add().

◆ Add()

bool CN_CONNECTIVITY_ALGO::Add ( BOARD_ITEM aItem)

Definition at line 113 of file connectivity_algo.cpp.

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

References add(), CN_LIST::Add(), BOARD_ITEM::IsOnCopperLayer(), m_itemList, m_itemMap, markItemNetAsDirty(), MarkNetAsDirty(), 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 396 of file connectivity_algo.cpp.

397 {
398  for( int i = 0; i<aBoard->GetAreaCount(); i++ )
399  {
400  auto zone = aBoard->GetArea( i );
401  Add( zone );
402  }
403 
404  for( auto tv : aBoard->Tracks() )
405  Add( tv );
406 
407  for( auto mod : aBoard->Modules() )
408  {
409  for( auto pad : mod->Pads() )
410  Add( pad );
411  }
412 
413  /*wxLogTrace( "CN", "zones : %lu, pads : %lu vias : %lu tracks : %lu\n",
414  m_zoneList.Size(), m_padList.Size(),
415  m_viaList.Size(), m_trackList.Size() );*/
416 }
bool Add(BOARD_ITEM *aItem)
int GetAreaCount() const
Function GetAreaCount.
Definition: class_board.h:955
MODULES & Modules()
Definition: class_board.h:236
size_t i
Definition: json11.cpp:597
ZONE_CONTAINER * GetArea(int index) const
Function GetArea returns the Area (Zone Container) at a given index.
Definition: class_board.h:926
#define mod(a, n)
Definition: greymap.cpp:24
TRACKS & Tracks()
Definition: class_board.h:227

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

◆ Build() [2/2]

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

Definition at line 419 of file connectivity_algo.cpp.

420 {
421  for( auto item : aItems )
422  {
423  switch( item->Type() )
424  {
425  case PCB_TRACE_T:
426  case PCB_VIA_T:
427  case PCB_PAD_T:
428  Add( item );
429  break;
430 
431  case PCB_MODULE_T:
432  {
433  for( auto pad : static_cast<MODULE*>( item )->Pads() )
434  {
435  Add( pad );
436  }
437 
438  break;
439  }
440 
441  default:
442  break;
443  }
444  }
445 }
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:95
class MODULE, a footprint
Definition: typeinfo.h:89
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96

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

◆ CheckConnectivity()

bool CN_CONNECTIVITY_ALGO::CheckConnectivity ( std::vector< CN_DISJOINT_NET_ENTRY > &  aReport)

◆ Clear()

void CN_CONNECTIVITY_ALGO::Clear ( )

Definition at line 713 of file connectivity_algo.cpp.

714 {
715  m_ratsnestClusters.clear();
716  m_connClusters.clear();
717  m_itemMap.clear();
718  m_itemList.Clear();
719 
720 }
std::unordered_map< const BOARD_CONNECTED_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 194 of file connectivity_algo.h.

195  {
196  for( auto i = m_dirtyNets.begin(); i != m_dirtyNets.end(); ++i )
197  *i = false;
198  }
std::vector< bool > m_dirtyNets
size_t i
Definition: json11.cpp:597

References i, and m_dirtyNets.

◆ FindIsolatedCopperIslands() [1/2]

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

Definition at line 505 of file connectivity_algo.cpp.

506 {
507  if( aZone->GetFilledPolysList().IsEmpty() )
508  return;
509 
510  aIslands.clear();
511 
512  Remove( aZone );
513  Add( aZone );
514 
516 
517  for( const auto& cluster : m_connClusters )
518  {
519  if( cluster->Contains( aZone ) && cluster->IsOrphaned() )
520  {
521  for( auto z : *cluster )
522  {
523  if( z->Parent() == aZone )
524  {
525  aIslands.push_back( static_cast<CN_ZONE*>(z)->SubpolyIndex() );
526  }
527  }
528  }
529  }
530 
531  wxLogTrace( "CN", "Found %u isolated islands\n", (unsigned)aIslands.size() );
532 }
bool Remove(BOARD_ITEM *aItem)
bool IsEmpty() const
Returns true if the set is empty (no polygons at all)
bool Add(BOARD_ITEM *aItem)
const CLUSTERS SearchClusters(CLUSTER_SEARCH_MODE aMode, const KICAD_T aTypes[], int aSingleNet)
const SHAPE_POLY_SET & GetFilledPolysList() const
Function GetFilledPolysList returns a reference to the list of filled polygons.
Definition: class_zone.h:543

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 534 of file connectivity_algo.cpp.

535 {
536  for ( auto& z : aZones )
537  Remove( z.m_zone );
538 
539  for ( auto& z : aZones )
540  {
541  if( !z.m_zone->GetFilledPolysList().IsEmpty() )
542  Add( z.m_zone );
543  }
544 
546 
547  for ( auto& zone : aZones )
548  {
549  if( zone.m_zone->GetFilledPolysList().IsEmpty() )
550  continue;
551 
552  for( const auto& cluster : m_connClusters )
553  {
554  if( cluster->Contains( zone.m_zone ) && cluster->IsOrphaned() )
555  {
556  for( auto z : *cluster )
557  {
558  if( z->Parent() == zone.m_zone )
559  {
560  zone.m_islands.push_back( static_cast<CN_ZONE*>(z)->SubpolyIndex() );
561  }
562  }
563  }
564  }
565  }
566 }
bool Remove(BOARD_ITEM *aItem)
bool Add(BOARD_ITEM *aItem)
const CLUSTERS SearchClusters(CLUSTER_SEARCH_MODE aMode, const KICAD_T aTypes[], int aSingleNet)

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

◆ ForEachAnchor()

void CN_CONNECTIVITY_ALGO::ForEachAnchor ( const std::function< void(CN_ANCHOR &)> &  aFunc)

Definition at line 730 of file connectivity_algo.cpp.

731 {
732  ForEachItem( [aFunc] ( CN_ITEM& item ) {
733  for( const auto& anchor : item.Anchors() )
734  aFunc( *anchor );
735  }
736  );
737 }
CN_ANCHORS & Anchors()
void ForEachItem(const std::function< void(CN_ITEM &)> &aFunc)

References CN_ITEM::Anchors(), and ForEachItem().

◆ ForEachItem()

void CN_CONNECTIVITY_ALGO::ForEachItem ( const std::function< void(CN_ITEM &)> &  aFunc)

Definition at line 723 of file connectivity_algo.cpp.

724 {
725  for( auto item : m_itemList )
726  aFunc( *item );
727 }

References m_itemList.

Referenced by ForEachAnchor().

◆ GetClusters()

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

Definition at line 569 of file connectivity_algo.cpp.

570 {
572  return m_ratsnestClusters;
573 }
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)
inline

Definition at line 200 of file connectivity_algo.h.

201  {
202  for( auto cl : m_ratsnestClusters )
203  {
204  int net = cl->OriginNet();
205 
206  if( net >= 0 && m_dirtyNets[net] )
207  aClusters.push_back( cl );
208  }
209  }
std::vector< bool > m_dirtyNets

References m_dirtyNets, and m_ratsnestClusters.

◆ GetUnconnectedCount()

int CN_CONNECTIVITY_ALGO::GetUnconnectedCount ( )

◆ IsNetDirty()

bool CN_CONNECTIVITY_ALGO::IsNetDirty ( int  aNet) const
inline

Definition at line 186 of file connectivity_algo.h.

187  {
188  if( aNet < 0 )
189  return false;
190 
191  return m_dirtyNets[ aNet ];
192  }
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 181 of file connectivity_algo.h.

182  {
183  return m_itemMap[ aItem ];
184  }
std::unordered_map< const BOARD_CONNECTED_ITEM *, ITEM_MAP_ENTRY > m_itemMap

References m_itemMap.

◆ ItemExists()

bool CN_CONNECTIVITY_ALGO::ItemExists ( const BOARD_CONNECTED_ITEM aItem)
inline

Definition at line 176 of file connectivity_algo.h.

177  {
178  return m_itemMap.find( aItem ) != m_itemMap.end();
179  }
std::unordered_map< const BOARD_CONNECTED_ITEM *, ITEM_MAP_ENTRY > m_itemMap

References m_itemMap.

◆ ItemList()

CN_LIST& CN_CONNECTIVITY_ALGO::ItemList ( )
inline

Definition at line 248 of file connectivity_algo.h.

248 { return m_itemList; }

References m_itemList.

◆ markItemNetAsDirty()

void CN_CONNECTIVITY_ALGO::markItemNetAsDirty ( const BOARD_ITEM aItem)
private

Definition at line 93 of file connectivity_algo.cpp.

94 {
95  if( aItem->IsConnected() )
96  {
97  auto citem = static_cast<const BOARD_CONNECTED_ITEM*>( aItem );
98  MarkNetAsDirty( citem->GetNetCode() );
99  }
100  else
101  {
102  if( aItem->Type() == PCB_MODULE_T )
103  {
104  auto mod = static_cast <const MODULE*>( aItem );
105 
106  for( auto pad : mod->Pads() )
107  MarkNetAsDirty( pad->GetNetCode() );
108  }
109  }
110 }
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.
#define mod(a, n)
Definition: greymap.cpp:24
KICAD_T Type() const
Function Type()
Definition: base_struct.h:210

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

Referenced by Add(), and Remove().

◆ MarkNetAsDirty()

void CN_CONNECTIVITY_ALGO::MarkNetAsDirty ( int  aNet)

Definition at line 576 of file connectivity_algo.cpp.

577 {
578  if( aNet < 0 )
579  return;
580 
581  if( (int) m_dirtyNets.size() <= aNet )
582  {
583  int lastNet = m_dirtyNets.size() - 1;
584 
585  if( lastNet < 0 )
586  lastNet = 0;
587 
588  m_dirtyNets.resize( aNet + 1 );
589 
590  for( int i = lastNet; i < aNet + 1; i++ )
591  m_dirtyNets[i] = true;
592  }
593 
594  m_dirtyNets[aNet] = true;
595 }
std::vector< bool > m_dirtyNets
size_t i
Definition: json11.cpp:597

References i, and m_dirtyNets.

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

◆ NetCount()

int CN_CONNECTIVITY_ALGO::NetCount ( ) const
inline

Definition at line 211 of file connectivity_algo.h.

212  {
213  return m_dirtyNets.size();
214  }
std::vector< bool > m_dirtyNets

References m_dirtyNets.

◆ propagateConnections()

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

Definition at line 448 of file connectivity_algo.cpp.

449 {
450  for( const auto& cluster : m_connClusters )
451  {
452  if( cluster->IsConflicting() )
453  {
454  wxLogTrace( "CN", "Conflicting nets in cluster %p\n", cluster.get() );
455  }
456  else if( cluster->IsOrphaned() )
457  {
458  wxLogTrace( "CN", "Skipping orphaned cluster %p [net: %s]\n", cluster.get(),
459  (const char*) cluster->OriginNetName().c_str() );
460  }
461  else if( cluster->HasValidNet() )
462  {
463  // normal cluster: just propagate from the pads
464  int n_changed = 0;
465 
466  for( auto item : *cluster )
467  {
468  if( item->CanChangeNet() )
469  {
470  if( item->Valid() && item->Parent()->GetNetCode() != cluster->OriginNet() )
471  {
472  MarkNetAsDirty( item->Parent()->GetNetCode() );
473  MarkNetAsDirty( cluster->OriginNet() );
474 
475  if( aCommit )
476  aCommit->Modify( item->Parent() );
477 
478  item->Parent()->SetNetCode( cluster->OriginNet() );
479  n_changed++;
480  }
481  }
482  }
483 
484  if( n_changed )
485  wxLogTrace( "CN", "Cluster %p : net : %d %s\n", cluster.get(),
486  cluster->OriginNet(), (const char*) cluster->OriginNetName().c_str() );
487  else
488  wxLogTrace( "CN", "Cluster %p : nothing to propagate\n", cluster.get() );
489  }
490  else
491  {
492  wxLogTrace( "CN", "Cluster %p : connected to unused net\n", cluster.get() );
493  }
494  }
495 }
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 498 of file connectivity_algo.cpp.

499 {
501  propagateConnections( aCommit );
502 }
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  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
64  m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
65  m_itemList.SetDirty( true );
66  break;
67 
68  case PCB_VIA_T:
69  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
70  m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
71  m_itemList.SetDirty( true );
72  break;
73 
74  case PCB_ZONE_AREA_T:
75  {
76  m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
77  m_itemMap.erase ( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
78  m_itemList.SetDirty( true );
79  break;
80  }
81 
82  default:
83  return false;
84  }
85 
86  // Once we delete an item, it may connect between lists, so mark both as potentially invalid
87  m_itemList.SetHasInvalid( true );
88 
89  return true;
90 }
void SetHasInvalid(bool aInvalid=true)
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:100
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
std::unordered_map< const BOARD_CONNECTED_ITEM *, ITEM_MAP_ENTRY > m_itemMap
void SetDirty(bool aDirty=true)
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
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:96
KICAD_T Type() const
Function Type()
Definition: base_struct.h:210

References m_itemList, m_itemMap, markItemNetAsDirty(), 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 293 of file connectivity_algo.cpp.

295 {
296  bool withinAnyNet = ( aMode != CSM_PROPAGATE );
297 
298  std::deque<CN_ITEM*> Q;
299  CN_ITEM* head = nullptr;
300  CLUSTERS clusters;
301 
302  if( m_itemList.IsDirty() )
304 
305  auto addToSearchList = [&head, withinAnyNet, aSingleNet, aTypes] ( CN_ITEM *aItem )
306  {
307  if( withinAnyNet && aItem->Net() <= 0 )
308  return;
309 
310  if( !aItem->Valid() )
311  return;
312 
313  if( aSingleNet >=0 && aItem->Net() != aSingleNet )
314  return;
315 
316  bool found = false;
317 
318  for( int i = 0; aTypes[i] != EOT; i++ )
319  {
320  if( aItem->Parent()->Type() == aTypes[i] )
321  {
322  found = true;
323  break;
324  }
325  }
326 
327  if( !found )
328  return;
329 
330  aItem->ListClear();
331  aItem->SetVisited( false );
332 
333  if( !head )
334  head = aItem;
335  else
336  head->ListInsert( aItem );
337  };
338 
339  std::for_each( m_itemList.begin(), m_itemList.end(), addToSearchList );
340 
341  while( head )
342  {
343  CN_CLUSTER_PTR cluster ( new CN_CLUSTER() );
344 
345  Q.clear();
346  CN_ITEM* root = head;
347  root->SetVisited ( true );
348 
349  head = root->ListRemove();
350 
351  Q.push_back( root );
352 
353  while( Q.size() )
354  {
355  CN_ITEM* current = Q.front();
356 
357  Q.pop_front();
358  cluster->Add( current );
359 
360  for( auto n : current->ConnectedItems() )
361  {
362  if( withinAnyNet && n->Net() != root->Net() )
363  continue;
364 
365  if( !n->Visited() && n->Valid() )
366  {
367  n->SetVisited( true );
368  Q.push_back( n );
369  head = n->ListRemove();
370  }
371  }
372  }
373 
374  clusters.push_back( cluster );
375  }
376 
377 
378  std::sort( clusters.begin(), clusters.end(), []( CN_CLUSTER_PTR a, CN_CLUSTER_PTR b ) {
379  return a->OriginNet() < b->OriginNet();
380  } );
381 
382 #ifdef CONNECTIVITY_DEBUG
383  printf("Active clusters: %d\n", clusters.size() );
384 
385  for( auto cl : clusters )
386  {
387  printf( "Net %d\n", cl->OriginNet() );
388  cl->Dump();
389  }
390 #endif
391 
392  return clusters;
393 }
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
size_t i
Definition: json11.cpp:597
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, i, 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 281 of file connectivity_algo.cpp.

282 {
284  constexpr KICAD_T no_zones[] = { PCB_TRACE_T, PCB_PAD_T, PCB_VIA_T, PCB_MODULE_T, EOT };
285 
286  if( aMode == CSM_PROPAGATE )
287  return SearchClusters( aMode, no_zones, -1 );
288  else
289  return SearchClusters( aMode, types, -1 );
290 }
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:100
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:95
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:96

References CSM_PROPAGATE, EOT, 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 187 of file connectivity_algo.cpp.

188 {
189 #ifdef CONNECTIVITY_DEBUG
190  printf("Search start\n");
191 #endif
192 
193 #ifdef PROFILE
194  PROF_COUNTER garbage_collection( "garbage-collection" );
195 #endif
196  std::vector<CN_ITEM*> garbage;
197  garbage.reserve( 1024 );
198 
199  m_itemList.RemoveInvalidItems( garbage );
200 
201  for( auto item : garbage )
202  delete item;
203 
204 #ifdef PROFILE
205  garbage_collection.Show();
206  PROF_COUNTER search_basic( "search-basic" );
207 #endif
208 
209  std::vector<CN_ITEM*> dirtyItems;
210  std::copy_if( m_itemList.begin(), m_itemList.end(), std::back_inserter( dirtyItems ),
211  [] ( CN_ITEM* aItem ) { return aItem->Dirty(); } );
212 
213  if( m_progressReporter )
214  {
215  m_progressReporter->SetMaxProgress( dirtyItems.size() );
217  }
218 
219  if( m_itemList.IsDirty() )
220  {
221  size_t parallelThreadCount = std::min<size_t>( std::thread::hardware_concurrency(),
222  ( dirtyItems.size() + 7 ) / 8 );
223 
224  std::atomic<size_t> nextItem( 0 );
225  std::vector<std::future<size_t>> returns( parallelThreadCount );
226 
227  auto conn_lambda = [&nextItem, &dirtyItems]
228  ( CN_LIST* aItemList, PROGRESS_REPORTER* aReporter) -> size_t
229  {
230  for( size_t i = nextItem++; i < dirtyItems.size(); i = nextItem++ )
231  {
232  CN_VISITOR visitor( dirtyItems[i] );
233  aItemList->FindNearby( dirtyItems[i], visitor );
234 
235  if( aReporter )
236  aReporter->AdvanceProgress();
237  }
238 
239  return 1;
240  };
241 
242  if( parallelThreadCount <= 1 )
243  conn_lambda( &m_itemList, m_progressReporter );
244  else
245  {
246  for( size_t ii = 0; ii < parallelThreadCount; ++ii )
247  returns[ii] = std::async( std::launch::async, conn_lambda,
249 
250  for( size_t ii = 0; ii < parallelThreadCount; ++ii )
251  {
252  // Here we balance returns with a 100ms timeout to allow UI updating
253  std::future_status status;
254  do
255  {
256  if( m_progressReporter )
258 
259  status = returns[ii].wait_for( std::chrono::milliseconds( 100 ) );
260  } while( status != std::future_status::ready );
261  }
262  }
263 
264  if( m_progressReporter )
266  }
267 
268 #ifdef PROFILE
269  search_basic.Show();
270 #endif
271 
273 
274 #ifdef CONNECTIVITY_DEBUG
275  printf("Search end\n");
276 #endif
277 
278 }
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.
size_t i
Definition: json11.cpp:597
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(), i, 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 740 of file connectivity_algo.cpp.

741 {
742  m_progressReporter = aReporter;
743 }
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_CONNECTED_ITEM*, ITEM_MAP_ENTRY> CN_CONNECTIVITY_ALGO::m_itemMap
private

Definition at line 148 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 153 of file connectivity_algo.h.

Referenced by searchConnections(), and SetProgressReporter().

◆ m_ratsnestClusters

CLUSTERS CN_CONNECTIVITY_ALGO::m_ratsnestClusters
private

Definition at line 151 of file connectivity_algo.h.

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


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