53 wxLogTrace(
traceTopoMatch, wxT(
"[conns mismatch n1 %d n2 %d c-ref %d c-other %d thispin %s-%s otherpin %s-%s"),
65 wxLogTrace(
traceTopoMatch, wxT(
"%s-%s "), c->m_parent->m_reference, c->m_ref );
72 wxLogTrace(
traceTopoMatch, wxT(
"%s-%s "), c->m_parent->m_reference, c->m_ref );
86 std::vector<bool> matches(
m_conns.size() );
88 for(
int i = 0; i <
m_conns.size(); i++ )
96 for(
int i = 0; i <
m_conns.size(); i++ )
98 if( b.
m_conns[i]->IsTopologicallySimilar( *cref ) )
101 matches[nref] =
true;
109 for(
int i = 0; i <
m_conns.size(); i++ )
123 std::map<PIN*, PIN*> pairs;
124 std::vector<PIN*> pref, ptgt;
129 for(
PIN* p : m.second->Pins() )
134 for(
PIN* p : m.first->Pins() )
150 if( pref.size() != ptgt.size() )
155 for(
unsigned int i = 0; i < pref.size(); i++ )
157 pairs[pref[i]] = ptgt[i];
160 for(
PIN* refPin : aRef->
Pins() )
164 std::optional<int> prevNet;
168 for(
PIN* ppin : refCmp->Pins() )
170 if ( ppin->GetNetCode() != refPin->GetNetCode() )
173 wxLogTrace(
traceTopoMatch, wxT(
"{ref %s-%s:%d} "), ppin->GetParent()->GetParent()->GetReferenceAsString(), ppin->GetReference(), ppin->GetNetCode() );
175 auto tpin = pairs.find( ppin );
177 if( tpin != pairs.end() )
179 int nc = tpin->second->GetNetCode();
183 if( prevNet && ( *prevNet != nc ) )
199std::vector<COMPONENT*>
202 std::vector<COMPONENT*> matches;
206 if( partialMatches.
m_locked.find( cmpTarget ) != partialMatches.
m_locked.end() )
220 matches.push_back( cmpTarget );
224 wxLogTrace(
traceTopoMatch, wxT(
"Reject [net topo mismatch]\n") );
244 return a->GetReference() < b->GetReference();
250 std::map<int, std::vector<PIN*>> nets;
258 for(
auto p : c->Pins() )
260 if( p->GetNetCode() > 0 )
261 nets[p->GetNetCode()].push_back( p );
265 for(
auto iter : nets )
267 wxLogTrace(
traceTopoMatch, wxT(
"net %d: %d connections\n"), iter.first, (
int) iter.second.size() );
268 for(
auto p : iter.second )
270 for(
auto p2 : iter.second )
273 p->m_conns.push_back( p2 );
294 std::vector<BACKTRACK_STAGE> stack;
306 stack.push_back( top );
308 bool matchFound =
false;
310 while( !stack.empty() )
313 auto& current = stack.back();
315 for(
auto it = current.m_locked.begin(); it != current.m_locked.end(); it++ )
317 if (it->second == current.m_ref)
319 wxLogTrace(
traceTopoMatch, wxT(
"stk: Remove %s from locked\n"), current.m_ref->m_reference );
320 current.m_locked.erase( it );
331 if( current.m_currentMatch < 0 )
334 current.m_currentMatch = 0;
337 wxLogTrace(
traceTopoMatch, wxT(
"stk: Current '%s' stack %d cm %d/%d locked %d/%d\n" ),
338 current.m_ref->m_reference, (
int) stack.size(),
339 current.m_currentMatch, (
int) current.m_matches.size(),
340 (
int) current.m_locked.size(), (
int)
m_components.size() );
342 if ( current.m_matches.empty() )
344 wxLogTrace(
traceTopoMatch, wxT(
"stk: No matches at all, going up [level=%d]\n"), (
int) stack.size() );
349 if( current.m_currentMatch >= 0 && current.m_currentMatch >= current.m_matches.size() )
351 wxLogTrace(
traceTopoMatch, wxT(
"stk: No more matches, going up [level=%d]\n"), (
int) stack.size() );
356 auto& match = current.m_matches[current.m_currentMatch];
358 wxLogTrace(
traceTopoMatch, wxT(
"stk: candidate '%s', match list : ( "),
359 current.m_matches[current.m_currentMatch]->m_reference,
360 current.m_refIndex );
362 for(
auto m : current.m_matches )
363 wxLogTrace(
traceTopoMatch, wxT(
"%s "), m->GetParent()->GetReferenceAsString() );
369 current.m_currentMatch++;
370 current.m_locked[match] = current.m_ref;
375 current.m_nloops = nloops;
379 for(
auto iter : current.m_locked )
380 aResult[ iter.second->GetParent() ] = iter.first->GetParent();
387 next.m_currentMatch = -1;
389 next.m_refIndex = current.m_refIndex + 1;
391 stack.push_back(
next );
401 FILE * f = fopen(
"connectivity.dump",
"rb" );
402 auto cgRef = loadCGraph(f);
403 auto cgTarget = loadCGraph(f);
405 cgRef->buildConnectivity();
406 cgTarget->buildConnectivity();
416 const BacktrackStage latest = cgRef->matchCGraphs( cgTarget );
418 if( !latest.locked.size() )
420 printf(
"MATCH FAIL\n");
436 if( latest.nloops > max_loops )
438 max_loops = latest.nloops;
441 if (attempts % 10000 == 0)
443 printf(
"attempts: %d maxloops: %d\n", attempts, max_loops );
458 m_reference( aRef ), m_parentFootprint( aParentFp ), m_raOffset( aRaOffset )
510 pin->m_netcode =
pad->GetNetCode();
511 pin->m_ref =
pad->GetNumber();
520 auto cgraph = std::make_unique<CONNECTION_GRAPH>();
523 if( aFps.size() > 0 )
524 ref = (*aFps.begin())->GetPosition();
526 for(
auto fp : aFps )
528 cgraph->AddFootprint( fp, fp->GetPosition() - ref );
531 cgraph->BuildConnectivity();
533 return std::move(cgraph);
const std::map< COMPONENT *, COMPONENT * > & GetMatchingComponentPairs() const
std::map< COMPONENT *, COMPONENT * > m_locked
bool MatchesWith(COMPONENT *b)
bool IsSameKind(const COMPONENT &b) const
COMPONENT(const wxString &aRef, FOOTPRINT *aParentFp, std::optional< VECTOR2I > aRaOffset=std::optional< VECTOR2I >())
FOOTPRINT * m_parentFootprint
std::vector< PIN * > & Pins()
std::vector< PIN * > m_pins
FOOTPRINT * GetParent() const
STATUS FindIsomorphism(CONNECTION_GRAPH *target, COMPONENT_MATCHES &result)
std::vector< COMPONENT * > & Components()
@ ST_COMPONENT_COUNT_MISMATCH
@ ST_ITERATION_COUNT_EXCEEDED
std::vector< COMPONENT * > m_components
std::vector< COMPONENT * > findMatchingComponents(CONNECTION_GRAPH *aRefGraph, COMPONENT *ref, BACKTRACK_STAGE &partialMatches)
void AddFootprint(FOOTPRINT *aFp, const VECTOR2I &aOffset)
static std::unique_ptr< CONNECTION_GRAPH > BuildFromFootprintSet(const std::set< FOOTPRINT * > &aFps)
std::vector< PIN * > m_conns
void SetParent(COMPONENT *parent)
bool IsIsomorphic(const PIN &b) const
std::map< FOOTPRINT *, FOOTPRINT * > COMPONENT_MATCHES
bool checkIfPadNetsMatch(BACKTRACK_STAGE &aMatches, CONNECTION_GRAPH *aRefGraph, COMPONENT *aRef, COMPONENT *aTgt)
wxString GetRefDesPrefix(const wxString &aRefDes)
Get the (non-numeric) prefix from a refdes - e.g.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Collection of utility functions for component reference designators (refdes)
static const wxString traceTopoMatch