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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#ifndef __TOPO_MATCH_H
25#define __TOPO_MATCH_H
26
27#include <atomic>
28#include <vector>
29#include <map>
30#include <unordered_map>
31#include <optional>
32
33#include <wx/string.h>
34
35class FOOTPRINT;
36
37/* A very simple (but working) partial connection graph isomorphism algorithm,
38 operating on sets of footprints and connections between their pads.
39*/
40
41namespace TMATCH
42{
43
44class PIN;
46
48{
49 std::atomic<bool>* m_cancelled = nullptr;
50 std::atomic<int>* m_matchedComponents = nullptr;
51 std::atomic<int>* m_totalComponents = nullptr;
52};
53
55{
56 wxString m_reference;
57 wxString m_candidate;
58 wxString m_reason;
59};
60
62{
63 friend class PIN;
64 friend class CONNECTION_GRAPH;
65
66public:
67 COMPONENT( const wxString& aRef, FOOTPRINT* aParentFp, std::optional<VECTOR2I> aRaOffset = std::optional<VECTOR2I>() );
68 ~COMPONENT();
69
70 bool IsSameKind( const COMPONENT& b ) const;
71 void AddPin( PIN* p );
72 int GetPinCount() const { return m_pins.size(); }
74 std::vector<PIN*>& Pins() { return m_pins; }
76
77 bool HasRAOffset() const { return m_raOffset.has_value(); }
78 const VECTOR2I GetRAOffset() const { return *m_raOffset; }
79
80private:
81 void sortPinsByName();
82
89 static bool isChannelSuffix( const wxString& aSuffix );
90
98 static bool prefixesShareCommonBase( const wxString& aPrefixA, const wxString& aPrefixB );
99
100 std::optional<VECTOR2I> m_raOffset;
101 wxString m_reference;
102 wxString m_prefix;
104 std::vector<PIN*> m_pins;
105};
106
107class PIN
108{
109 friend class CONNECTION_GRAPH;
110
111public:
112 PIN() : m_netcode( 0 ), m_parent( nullptr ) {}
113 ~PIN() {}
114
115 void SetParent( COMPONENT* parent ) { m_parent = parent; }
116
117 const wxString Format() const { return m_parent->m_reference + wxT( "-" ) + m_ref; }
118
119 void AddConnection( PIN* pin ) { m_conns.push_back( pin ); }
120
121 bool IsTopologicallySimilar( const PIN& b ) const
122 {
123 wxASSERT( m_parent != b.m_parent );
124
125 if( !m_parent->IsSameKind( *b.m_parent ) )
126 return false;
127
128 return m_ref == b.m_ref;
129 }
130
131 bool IsIsomorphic( const PIN& b, TOPOLOGY_MISMATCH_REASON& aDetail ) const;
132
133 int GetNetCode() const { return m_netcode; }
134
135 const wxString& GetReference() const { return m_ref; }
136
137 COMPONENT* GetParent() const { return m_parent; }
138
139private:
140
141 wxString m_ref;
144 std::vector<PIN*> m_conns;
145};
146
148{
149 friend class CONNECTION_GRAPH;
150
151public:
153 {
154 m_ref = nullptr;
155 m_currentMatch = -1;
156 m_nloops = 0;
157 m_refIndex = 0;
158 }
159
161 {
163 m_ref = other.m_ref;
164 m_matches = other.m_matches;
165 m_locked = other.m_locked;
166 m_nloops = other.m_nloops;
167 m_refIndex = other.m_refIndex;
168 }
169
170 const std::unordered_map<COMPONENT*, COMPONENT*>& GetMatchingComponentPairs() const
171 {
172 return m_locked;
173 }
174
175private:
179 std::vector<COMPONENT*> m_matches;
180 std::unordered_map<COMPONENT*, COMPONENT*> m_locked;
182};
183
184typedef std::map<FOOTPRINT*, FOOTPRINT*> COMPONENT_MATCHES;
185
187{
188public:
189 const int c_ITER_LIMIT = 10000;
190
193
194 void BuildConnectivity();
195 void AddFootprint( FOOTPRINT* aFp, const VECTOR2I& aOffset );
197 std::vector<TOPOLOGY_MISMATCH_REASON>& aFailureDetails,
198 const ISOMORPHISM_PARAMS& aParams = {} );
199 static std::unique_ptr<CONNECTION_GRAPH> BuildFromFootprintSet( const std::set<FOOTPRINT*>& aFps );
200 std::vector<COMPONENT*> &Components() { return m_components; }
201
202private:
208 void breakTie( COMPONENT* aRef, std::vector<COMPONENT*>& aMatches ) const;
213 bool breakTieBySymbolUuid( COMPONENT* aRef, std::vector<COMPONENT*>& aMatches ) const;
214
215 void sortByPinCount();
216
217
218 std::vector<COMPONENT*> findMatchingComponents( COMPONENT* ref,
219 const std::vector<COMPONENT*>& aStructuralMatches,
220 const BACKTRACK_STAGE& partialMatches,
221 std::vector<TOPOLOGY_MISMATCH_REASON>& aFailureDetails,
222 const std::atomic<bool>* aCancelled = nullptr );
223
224 std::vector<COMPONENT*> m_components;
225
226};
227
228}; // namespace TMATCH
229
230#endif
friend class CONNECTION_GRAPH
Definition topo_match.h:149
const std::unordered_map< COMPONENT *, COMPONENT * > & GetMatchingComponentPairs() const
Definition topo_match.h:170
std::unordered_map< COMPONENT *, COMPONENT * > m_locked
Definition topo_match.h:180
std::vector< COMPONENT * > m_matches
Definition topo_match.h:179
BACKTRACK_STAGE(const BACKTRACK_STAGE &other)
Definition topo_match.h:160
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:64
bool IsSameKind(const COMPONENT &b) const
const VECTOR2I GetRAOffset() const
Definition topo_match.h:78
int GetPinCount() const
Definition topo_match.h:72
COMPONENT(const wxString &aRef, FOOTPRINT *aParentFp, std::optional< VECTOR2I > aRaOffset=std::optional< VECTOR2I >())
bool HasRAOffset() const
Definition topo_match.h:77
FOOTPRINT * m_parentFootprint
Definition topo_match.h:103
std::optional< VECTOR2I > m_raOffset
Definition topo_match.h:100
bool MatchesWith(COMPONENT *b, TOPOLOGY_MISMATCH_REASON &aDetail)
void AddPin(PIN *p)
std::vector< PIN * > & Pins()
Definition topo_match.h:74
friend class PIN
Definition topo_match.h:63
wxString m_reference
Definition topo_match.h:101
static bool isChannelSuffix(const wxString &aSuffix)
Check if a suffix looks like a channel identifier.
std::vector< PIN * > m_pins
Definition topo_match.h:104
FOOTPRINT * GetParent() const
Definition topo_match.h:75
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:200
std::vector< COMPONENT * > findMatchingComponents(COMPONENT *ref, const std::vector< COMPONENT * > &aStructuralMatches, const BACKTRACK_STAGE &partialMatches, std::vector< TOPOLOGY_MISMATCH_REASON > &aFailureDetails, const std::atomic< bool > *aCancelled=nullptr)
std::vector< COMPONENT * > m_components
Definition topo_match.h:224
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...
static std::unique_ptr< CONNECTION_GRAPH > BuildFromFootprintSet(const std::set< FOOTPRINT * > &aFps)
void breakTie(COMPONENT *aRef, std::vector< COMPONENT * > &aMatches) const
Many times components are electrically/topologically identical, e.g.
std::vector< PIN * > m_conns
Definition topo_match.h:144
friend class CONNECTION_GRAPH
Definition topo_match.h:109
void SetParent(COMPONENT *parent)
Definition topo_match.h:115
COMPONENT * m_parent
Definition topo_match.h:143
bool IsTopologicallySimilar(const PIN &b) const
Definition topo_match.h:121
const wxString & GetReference() const
Definition topo_match.h:135
bool IsIsomorphic(const PIN &b, TOPOLOGY_MISMATCH_REASON &aDetail) const
void AddConnection(PIN *pin)
Definition topo_match.h:119
COMPONENT * GetParent() const
Definition topo_match.h:137
int GetNetCode() const
Definition topo_match.h:133
const wxString Format() const
Definition topo_match.h:117
wxString m_ref
Definition topo_match.h:141
std::map< FOOTPRINT *, FOOTPRINT * > COMPONENT_MATCHES
Definition topo_match.h:184
std::atomic< bool > * m_cancelled
Definition topo_match.h:49
std::atomic< int > * m_matchedComponents
Definition topo_match.h:50
std::atomic< int > * m_totalComponents
Definition topo_match.h:51
KIBIS_PIN * pin
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695