KiCad PCB EDA Suite
Loading...
Searching...
No Matches
topo_match.h
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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef __TOPO_MATCH_H
21#define __TOPO_MATCH_H
22
23#include <atomic>
24#include <vector>
25#include <map>
26#include <unordered_map>
27#include <unordered_set>
28#include <optional>
29
30#include <wx/string.h>
31
32class FOOTPRINT;
33
34/* A very simple (but working) partial connection graph isomorphism algorithm,
35 operating on sets of footprints and connections between their pads.
36*/
37
38namespace TMATCH
39{
40
41class PIN;
43
45{
46 std::atomic<bool>* m_cancelled = nullptr;
47 std::atomic<int>* m_matchedComponents = nullptr;
48 std::atomic<int>* m_totalComponents = nullptr;
49};
50
52{
53 wxString m_reference;
54 wxString m_candidate;
55 wxString m_reason;
56};
57
59{
60 friend class PIN;
61 friend class CONNECTION_GRAPH;
62
63public:
64 COMPONENT( const wxString& aRef, FOOTPRINT* aParentFp, std::optional<VECTOR2I> aRaOffset = std::optional<VECTOR2I>() );
65 ~COMPONENT();
66
67 bool IsSameKind( const COMPONENT& b ) const;
68 void AddPin( PIN* p );
69 int GetPinCount() const { return m_pins.size(); }
71 std::vector<PIN*>& Pins() { return m_pins; }
73
74 bool HasRAOffset() const { return m_raOffset.has_value(); }
75 const VECTOR2I GetRAOffset() const { return *m_raOffset; }
76
77private:
78 void sortPinsByName();
79
86 static bool isChannelSuffix( const wxString& aSuffix );
87
95 static bool prefixesShareCommonBase( const wxString& aPrefixA, const wxString& aPrefixB );
96
101 static bool isUnannotatedRef( const wxString& aRef );
102
103 std::optional<VECTOR2I> m_raOffset;
104 wxString m_reference;
105 wxString m_prefix;
107 std::vector<PIN*> m_pins;
108};
109
110class PIN
111{
112 friend class CONNECTION_GRAPH;
113
114public:
115 PIN() : m_netcode( 0 ), m_parent( nullptr ) {}
116 ~PIN() {}
117
118 void SetParent( COMPONENT* parent ) { m_parent = parent; }
119
120 const wxString Format() const { return m_parent->m_reference + wxT( "-" ) + m_ref; }
121
122 void AddConnection( PIN* pin ) { m_conns.push_back( pin ); }
123
124 bool IsTopologicallySimilar( const PIN& b ) const
125 {
126 wxASSERT( m_parent != b.m_parent );
127
128 if( !m_parent->IsSameKind( *b.m_parent ) )
129 return false;
130
131 return m_ref == b.m_ref;
132 }
133
134 bool IsIsomorphic( const PIN& b, TOPOLOGY_MISMATCH_REASON& aDetail ) const;
135
136 int GetNetCode() const { return m_netcode; }
137
138 const wxString& GetReference() const { return m_ref; }
139
140 COMPONENT* GetParent() const { return m_parent; }
141
142private:
143
144 wxString m_ref;
147 std::vector<PIN*> m_conns;
148};
149
151{
152 friend class CONNECTION_GRAPH;
153
154public:
156 {
157 m_ref = nullptr;
158 m_currentMatch = -1;
159 m_nloops = 0;
160 m_refIndex = 0;
161 }
162
164 {
166 m_ref = other.m_ref;
167 m_matches = other.m_matches;
168 m_locked = other.m_locked;
169 m_nloops = other.m_nloops;
170 m_refIndex = other.m_refIndex;
171 }
172
173 const std::unordered_map<COMPONENT*, COMPONENT*>& GetMatchingComponentPairs() const
174 {
175 return m_locked;
176 }
177
178private:
182 std::vector<COMPONENT*> m_matches;
183 std::unordered_map<COMPONENT*, COMPONENT*> m_locked;
185};
186
187typedef std::map<FOOTPRINT*, FOOTPRINT*> COMPONENT_MATCHES;
188
190{
191public:
192 const int c_ITER_LIMIT = 10000;
193
196
197 void BuildConnectivity( const std::unordered_set<int>& aExternalNets = {} );
198 void AddFootprint( FOOTPRINT* aFp, const VECTOR2I& aOffset );
200 std::vector<TOPOLOGY_MISMATCH_REASON>& aFailureDetails,
201 const ISOMORPHISM_PARAMS& aParams = {} );
209 static std::unique_ptr<CONNECTION_GRAPH> BuildFromFootprintSet( const std::set<FOOTPRINT*>& aFps,
210 const std::set<FOOTPRINT*>& aOtherChannelFps = {},
211 const std::unordered_set<int>& aGlobalNets = {} );
212 std::vector<COMPONENT*> &Components() { return m_components; }
213
214private:
220 void breakTie( COMPONENT* aRef, std::vector<COMPONENT*>& aMatches ) const;
225 bool breakTieBySymbolUuid( COMPONENT* aRef, std::vector<COMPONENT*>& aMatches ) const;
226
231 bool breakTieByValue( COMPONENT* aRef, std::vector<COMPONENT*>& aMatches ) const;
232
233 void sortByPinCount();
234
235
236 std::vector<COMPONENT*> findMatchingComponents( COMPONENT* ref,
237 const std::vector<COMPONENT*>& aStructuralMatches,
238 const TOPOLOGY_MISMATCH_REASON& aStructuralReason,
239 const BACKTRACK_STAGE& partialMatches,
240 std::vector<TOPOLOGY_MISMATCH_REASON>& aFailureDetails,
241 const std::atomic<bool>* aCancelled = nullptr );
242
243 std::vector<COMPONENT*> m_components;
244 std::unordered_set<int> m_externalNets;
245
246};
247
248}; // namespace TMATCH
249
250#endif
friend class CONNECTION_GRAPH
Definition topo_match.h:152
const std::unordered_map< COMPONENT *, COMPONENT * > & GetMatchingComponentPairs() const
Definition topo_match.h:173
std::unordered_map< COMPONENT *, COMPONENT * > m_locked
Definition topo_match.h:183
std::vector< COMPONENT * > m_matches
Definition topo_match.h:182
BACKTRACK_STAGE(const BACKTRACK_STAGE &other)
Definition topo_match.h:163
static bool prefixesShareCommonBase(const wxString &aPrefixA, const wxString &aPrefixB)
Check if two prefixes share a common starting sequence.
static bool isUnannotatedRef(const wxString &aRef)
True for un-annotated placeholder refs like REF** that match any counterpart on FPID and topology alo...
friend class CONNECTION_GRAPH
Definition topo_match.h:61
bool IsSameKind(const COMPONENT &b) const
const VECTOR2I GetRAOffset() const
Definition topo_match.h:75
int GetPinCount() const
Definition topo_match.h:69
COMPONENT(const wxString &aRef, FOOTPRINT *aParentFp, std::optional< VECTOR2I > aRaOffset=std::optional< VECTOR2I >())
bool HasRAOffset() const
Definition topo_match.h:74
FOOTPRINT * m_parentFootprint
Definition topo_match.h:106
std::optional< VECTOR2I > m_raOffset
Definition topo_match.h:103
bool MatchesWith(COMPONENT *b, TOPOLOGY_MISMATCH_REASON &aDetail)
void AddPin(PIN *p)
std::vector< PIN * > & Pins()
Definition topo_match.h:71
friend class PIN
Definition topo_match.h:60
wxString m_reference
Definition topo_match.h:104
static bool isChannelSuffix(const wxString &aSuffix)
Check if a suffix looks like a channel identifier.
std::vector< PIN * > m_pins
Definition topo_match.h:107
FOOTPRINT * GetParent() const
Definition topo_match.h:72
std::vector< COMPONENT * > findMatchingComponents(COMPONENT *ref, const std::vector< COMPONENT * > &aStructuralMatches, const TOPOLOGY_MISMATCH_REASON &aStructuralReason, const BACKTRACK_STAGE &partialMatches, std::vector< TOPOLOGY_MISMATCH_REASON > &aFailureDetails, const std::atomic< bool > *aCancelled=nullptr)
bool FindIsomorphism(CONNECTION_GRAPH *target, COMPONENT_MATCHES &result, std::vector< TOPOLOGY_MISMATCH_REASON > &aFailureDetails, const ISOMORPHISM_PARAMS &aParams={})
std::vector< COMPONENT * > & Components()
Definition topo_match.h:212
void BuildConnectivity(const std::unordered_set< int > &aExternalNets={})
static std::unique_ptr< CONNECTION_GRAPH > BuildFromFootprintSet(const std::set< FOOTPRINT * > &aFps, const std::set< FOOTPRINT * > &aOtherChannelFps={}, const std::unordered_set< int > &aGlobalNets={})
bool breakTieByValue(COMPONENT *aRef, std::vector< COMPONENT * > &aMatches) const
Break a tie by footprint value when the symbol UUID can't, e.g.
std::vector< COMPONENT * > m_components
Definition topo_match.h:243
void AddFootprint(FOOTPRINT *aFp, const VECTOR2I &aOffset)
bool breakTieBySymbolUuid(COMPONENT *aRef, std::vector< COMPONENT * > &aMatches) const
The most useful tie breaker is based on symbol/sheet instances, since multiple channels in a design a...
void breakTie(COMPONENT *aRef, std::vector< COMPONENT * > &aMatches) const
Many times components are electrically/topologically identical, e.g.
std::unordered_set< int > m_externalNets
Definition topo_match.h:244
std::vector< PIN * > m_conns
Definition topo_match.h:147
friend class CONNECTION_GRAPH
Definition topo_match.h:112
void SetParent(COMPONENT *parent)
Definition topo_match.h:118
COMPONENT * m_parent
Definition topo_match.h:146
bool IsTopologicallySimilar(const PIN &b) const
Definition topo_match.h:124
const wxString & GetReference() const
Definition topo_match.h:138
bool IsIsomorphic(const PIN &b, TOPOLOGY_MISMATCH_REASON &aDetail) const
void AddConnection(PIN *pin)
Definition topo_match.h:122
COMPONENT * GetParent() const
Definition topo_match.h:140
int GetNetCode() const
Definition topo_match.h:136
const wxString Format() const
Definition topo_match.h:120
wxString m_ref
Definition topo_match.h:144
std::map< FOOTPRINT *, FOOTPRINT * > COMPONENT_MATCHES
Definition topo_match.h:187
std::atomic< bool > * m_cancelled
Definition topo_match.h:46
std::atomic< int > * m_matchedComponents
Definition topo_match.h:47
std::atomic< int > * m_totalComponents
Definition topo_match.h:48
KIBIS_PIN * pin
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683