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 <optional>
28
29#include <wx/string.h>
30
31class FOOTPRINT;
32
33/* A very simple (but working) partial connection graph isomorphism algorithm,
34 operating on sets of footprints and connections between their pads.
35*/
36
37namespace TMATCH
38{
39
40class PIN;
42
44{
45 std::atomic<bool>* m_cancelled = nullptr;
46 std::atomic<int>* m_matchedComponents = nullptr;
47 std::atomic<int>* m_totalComponents = nullptr;
48};
49
51{
52 wxString m_reference;
53 wxString m_candidate;
54 wxString m_reason;
55};
56
58{
59 friend class PIN;
60 friend class CONNECTION_GRAPH;
61
62public:
63 COMPONENT( const wxString& aRef, FOOTPRINT* aParentFp, std::optional<VECTOR2I> aRaOffset = std::optional<VECTOR2I>() );
64 ~COMPONENT();
65
66 bool IsSameKind( const COMPONENT& b ) const;
67 void AddPin( PIN* p );
68 int GetPinCount() const { return m_pins.size(); }
70 std::vector<PIN*>& Pins() { return m_pins; }
72
73 bool HasRAOffset() const { return m_raOffset.has_value(); }
74 const VECTOR2I GetRAOffset() const { return *m_raOffset; }
75
76private:
77 void sortPinsByName();
78
85 static bool isChannelSuffix( const wxString& aSuffix );
86
94 static bool prefixesShareCommonBase( const wxString& aPrefixA, const wxString& aPrefixB );
95
96 std::optional<VECTOR2I> m_raOffset;
97 wxString m_reference;
98 wxString m_prefix;
100 std::vector<PIN*> m_pins;
101};
102
103class PIN
104{
105 friend class CONNECTION_GRAPH;
106
107public:
108 PIN() : m_netcode( 0 ), m_parent( nullptr ) {}
109 ~PIN() {}
110
111 void SetParent( COMPONENT* parent ) { m_parent = parent; }
112
113 const wxString Format() const { return m_parent->m_reference + wxT( "-" ) + m_ref; }
114
115 void AddConnection( PIN* pin ) { m_conns.push_back( pin ); }
116
117 bool IsTopologicallySimilar( const PIN& b ) const
118 {
119 wxASSERT( m_parent != b.m_parent );
120
121 if( !m_parent->IsSameKind( *b.m_parent ) )
122 return false;
123
124 return m_ref == b.m_ref;
125 }
126
127 bool IsIsomorphic( const PIN& b, TOPOLOGY_MISMATCH_REASON& aDetail ) const;
128
129 int GetNetCode() const { return m_netcode; }
130
131 const wxString& GetReference() const { return m_ref; }
132
133 COMPONENT* GetParent() const { return m_parent; }
134
135private:
136
137 wxString m_ref;
140 std::vector<PIN*> m_conns;
141};
142
144{
145 friend class CONNECTION_GRAPH;
146
147public:
149 {
150 m_ref = nullptr;
151 m_currentMatch = -1;
152 m_nloops = 0;
153 m_refIndex = 0;
154 }
155
157 {
159 m_ref = other.m_ref;
160 m_matches = other.m_matches;
161 m_locked = other.m_locked;
162 m_nloops = other.m_nloops;
163 m_refIndex = other.m_refIndex;
164 }
165
166 const std::unordered_map<COMPONENT*, COMPONENT*>& GetMatchingComponentPairs() const
167 {
168 return m_locked;
169 }
170
171private:
175 std::vector<COMPONENT*> m_matches;
176 std::unordered_map<COMPONENT*, COMPONENT*> m_locked;
178};
179
180typedef std::map<FOOTPRINT*, FOOTPRINT*> COMPONENT_MATCHES;
181
183{
184public:
185 const int c_ITER_LIMIT = 10000;
186
189
190 void BuildConnectivity( const std::unordered_set<int>& aExternalNets = {} );
191 void AddFootprint( FOOTPRINT* aFp, const VECTOR2I& aOffset );
193 std::vector<TOPOLOGY_MISMATCH_REASON>& aFailureDetails,
194 const ISOMORPHISM_PARAMS& aParams = {} );
195 static std::unique_ptr<CONNECTION_GRAPH> BuildFromFootprintSet( const std::set<FOOTPRINT*>& aFps,
196 const std::set<FOOTPRINT*>& aOtherChannelFps = {} );
197 std::vector<COMPONENT*> &Components() { return m_components; }
198
199private:
205 void breakTie( COMPONENT* aRef, std::vector<COMPONENT*>& aMatches ) const;
210 bool breakTieBySymbolUuid( COMPONENT* aRef, std::vector<COMPONENT*>& aMatches ) const;
211
212 void sortByPinCount();
213
214
215 std::vector<COMPONENT*> findMatchingComponents( COMPONENT* ref,
216 const std::vector<COMPONENT*>& aStructuralMatches,
217 const TOPOLOGY_MISMATCH_REASON& aStructuralReason,
218 const BACKTRACK_STAGE& partialMatches,
219 std::vector<TOPOLOGY_MISMATCH_REASON>& aFailureDetails,
220 const std::atomic<bool>* aCancelled = nullptr );
221
222 std::vector<COMPONENT*> m_components;
223 std::unordered_set<int> m_externalNets;
224
225};
226
227}; // namespace TMATCH
228
229#endif
friend class CONNECTION_GRAPH
Definition topo_match.h:145
const std::unordered_map< COMPONENT *, COMPONENT * > & GetMatchingComponentPairs() const
Definition topo_match.h:166
std::unordered_map< COMPONENT *, COMPONENT * > m_locked
Definition topo_match.h:176
std::vector< COMPONENT * > m_matches
Definition topo_match.h:175
BACKTRACK_STAGE(const BACKTRACK_STAGE &other)
Definition topo_match.h:156
static bool prefixesShareCommonBase(const wxString &aPrefixA, const wxString &aPrefixB)
Check if two prefixes share a common starting sequence.
friend class CONNECTION_GRAPH
Definition topo_match.h:60
bool IsSameKind(const COMPONENT &b) const
const VECTOR2I GetRAOffset() const
Definition topo_match.h:74
int GetPinCount() const
Definition topo_match.h:68
COMPONENT(const wxString &aRef, FOOTPRINT *aParentFp, std::optional< VECTOR2I > aRaOffset=std::optional< VECTOR2I >())
bool HasRAOffset() const
Definition topo_match.h:73
FOOTPRINT * m_parentFootprint
Definition topo_match.h:99
std::optional< VECTOR2I > m_raOffset
Definition topo_match.h:96
bool MatchesWith(COMPONENT *b, TOPOLOGY_MISMATCH_REASON &aDetail)
wxString m_prefix
Definition topo_match.h:98
void AddPin(PIN *p)
std::vector< PIN * > & Pins()
Definition topo_match.h:70
friend class PIN
Definition topo_match.h:59
wxString m_reference
Definition topo_match.h:97
static bool isChannelSuffix(const wxString &aSuffix)
Check if a suffix looks like a channel identifier.
std::vector< PIN * > m_pins
Definition topo_match.h:100
FOOTPRINT * GetParent() const
Definition topo_match.h:71
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:197
void BuildConnectivity(const std::unordered_set< int > &aExternalNets={})
std::vector< COMPONENT * > m_components
Definition topo_match.h:222
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:223
static std::unique_ptr< CONNECTION_GRAPH > BuildFromFootprintSet(const std::set< FOOTPRINT * > &aFps, const std::set< FOOTPRINT * > &aOtherChannelFps={})
std::vector< PIN * > m_conns
Definition topo_match.h:140
friend class CONNECTION_GRAPH
Definition topo_match.h:105
void SetParent(COMPONENT *parent)
Definition topo_match.h:111
COMPONENT * m_parent
Definition topo_match.h:139
bool IsTopologicallySimilar(const PIN &b) const
Definition topo_match.h:117
const wxString & GetReference() const
Definition topo_match.h:131
bool IsIsomorphic(const PIN &b, TOPOLOGY_MISMATCH_REASON &aDetail) const
void AddConnection(PIN *pin)
Definition topo_match.h:115
COMPONENT * GetParent() const
Definition topo_match.h:133
int GetNetCode() const
Definition topo_match.h:129
const wxString Format() const
Definition topo_match.h:113
wxString m_ref
Definition topo_match.h:137
std::map< FOOTPRINT *, FOOTPRINT * > COMPONENT_MATCHES
Definition topo_match.h:180
std::atomic< bool > * m_cancelled
Definition topo_match.h:45
std::atomic< int > * m_matchedComponents
Definition topo_match.h:46
std::atomic< int > * m_totalComponents
Definition topo_match.h:47
KIBIS_PIN * pin
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683