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 <vector>
28#include <map>
29#include <optional>
30
31#include <wx/string.h>
32
33class FOOTPRINT;
34
35/* A very simple (but working) partial connection graph isomorphism algorithm,
36 operating on sets of footprints and connections between their pads.
37*/
38
39namespace TMATCH
40{
41
42class PIN;
44
46{
47 wxString m_reference;
48 wxString m_candidate;
49 wxString m_reason;
50};
51
53{
54 friend class PIN;
55 friend class CONNECTION_GRAPH;
56
57public:
58 COMPONENT( const wxString& aRef, FOOTPRINT* aParentFp, std::optional<VECTOR2I> aRaOffset = std::optional<VECTOR2I>() );
59 ~COMPONENT();
60
61 bool IsSameKind( const COMPONENT& b ) const;
62 void AddPin( PIN* p );
63 int GetPinCount() const { return m_pins.size(); }
65 std::vector<PIN*>& Pins() { return m_pins; }
67
68 bool HasRAOffset() const { return m_raOffset.has_value(); }
69 const VECTOR2I GetRAOffset() const { return *m_raOffset; }
70
71private:
72 void sortPinsByName();
73
80 static bool isChannelSuffix( const wxString& aSuffix );
81
89 static bool prefixesShareCommonBase( const wxString& aPrefixA, const wxString& aPrefixB );
90
91 std::optional<VECTOR2I> m_raOffset;
92 wxString m_reference;
93 wxString m_prefix;
95 std::vector<PIN*> m_pins;
96};
97
98class PIN
99{
100 friend class CONNECTION_GRAPH;
101
102public:
103 PIN() : m_netcode( 0 ), m_parent( nullptr ) {}
104 ~PIN() {}
105
106 void SetParent( COMPONENT* parent ) { m_parent = parent; }
107
108 const wxString Format() const { return m_parent->m_reference + wxT( "-" ) + m_ref; }
109
110 void AddConnection( PIN* pin ) { m_conns.push_back( pin ); }
111
112 bool IsTopologicallySimilar( const PIN& b ) const
113 {
114 wxASSERT( m_parent != b.m_parent );
115
116 if( !m_parent->IsSameKind( *b.m_parent ) )
117 return false;
118
119 return m_ref == b.m_ref;
120 }
121
122 bool IsIsomorphic( const PIN& b, TOPOLOGY_MISMATCH_REASON& aDetail ) const;
123
124 int GetNetCode() const { return m_netcode; }
125
126 const wxString& GetReference() const { return m_ref; }
127
128 COMPONENT* GetParent() const { return m_parent; }
129
130private:
131
132 wxString m_ref;
135 std::vector<PIN*> m_conns;
136};
137
139{
140 friend class CONNECTION_GRAPH;
141
142public:
144 {
145 m_ref = nullptr;
146 m_currentMatch = -1;
147 m_nloops = 0;
148 m_refIndex = 0;
149 }
150
152 {
154 m_ref = other.m_ref;
155 m_matches = other.m_matches;
156 m_locked = other.m_locked;
157 m_nloops = other.m_nloops;
158 m_refIndex = other.m_refIndex;
159 }
160
161 const std::map<COMPONENT*, COMPONENT*>& GetMatchingComponentPairs() const { return m_locked; }
162
163private:
167 std::vector<COMPONENT*> m_matches;
168 std::map<COMPONENT*, COMPONENT*> m_locked;
170};
171
172typedef std::map<FOOTPRINT*, FOOTPRINT*> COMPONENT_MATCHES;
173
175{
176public:
177 const int c_ITER_LIMIT = 10000;
178
181
182 void BuildConnectivity();
183 void AddFootprint( FOOTPRINT* aFp, const VECTOR2I& aOffset );
185 std::vector<TOPOLOGY_MISMATCH_REASON>& aFailureDetails );
186 static std::unique_ptr<CONNECTION_GRAPH> BuildFromFootprintSet( const std::set<FOOTPRINT*>& aFps );
187 std::vector<COMPONENT*> &Components() { return m_components; }
188
189private:
190 void sortByPinCount();
191
192
193 std::vector<COMPONENT*> findMatchingComponents( CONNECTION_GRAPH* aRefGraph,
194 COMPONENT* ref,
195 const BACKTRACK_STAGE& partialMatches,
196 std::vector<TOPOLOGY_MISMATCH_REASON>& aFailureDetails );
197
198 std::vector<COMPONENT*> m_components;
199
200};
201
202}; // namespace TMATCH
203
204#endif
friend class CONNECTION_GRAPH
Definition topo_match.h:140
const std::map< COMPONENT *, COMPONENT * > & GetMatchingComponentPairs() const
Definition topo_match.h:161
std::vector< COMPONENT * > m_matches
Definition topo_match.h:167
std::map< COMPONENT *, COMPONENT * > m_locked
Definition topo_match.h:168
BACKTRACK_STAGE(const BACKTRACK_STAGE &other)
Definition topo_match.h:151
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:55
bool IsSameKind(const COMPONENT &b) const
const VECTOR2I GetRAOffset() const
Definition topo_match.h:69
int GetPinCount() const
Definition topo_match.h:63
COMPONENT(const wxString &aRef, FOOTPRINT *aParentFp, std::optional< VECTOR2I > aRaOffset=std::optional< VECTOR2I >())
bool HasRAOffset() const
Definition topo_match.h:68
FOOTPRINT * m_parentFootprint
Definition topo_match.h:94
std::optional< VECTOR2I > m_raOffset
Definition topo_match.h:91
bool MatchesWith(COMPONENT *b, TOPOLOGY_MISMATCH_REASON &aDetail)
wxString m_prefix
Definition topo_match.h:93
void AddPin(PIN *p)
std::vector< PIN * > & Pins()
Definition topo_match.h:65
friend class PIN
Definition topo_match.h:54
wxString m_reference
Definition topo_match.h:92
static bool isChannelSuffix(const wxString &aSuffix)
Check if a suffix looks like a channel identifier.
std::vector< PIN * > m_pins
Definition topo_match.h:95
FOOTPRINT * GetParent() const
Definition topo_match.h:66
std::vector< COMPONENT * > & Components()
Definition topo_match.h:187
std::vector< COMPONENT * > m_components
Definition topo_match.h:198
void AddFootprint(FOOTPRINT *aFp, const VECTOR2I &aOffset)
static std::unique_ptr< CONNECTION_GRAPH > BuildFromFootprintSet(const std::set< FOOTPRINT * > &aFps)
bool FindIsomorphism(CONNECTION_GRAPH *target, COMPONENT_MATCHES &result, std::vector< TOPOLOGY_MISMATCH_REASON > &aFailureDetails)
std::vector< COMPONENT * > findMatchingComponents(CONNECTION_GRAPH *aRefGraph, COMPONENT *ref, const BACKTRACK_STAGE &partialMatches, std::vector< TOPOLOGY_MISMATCH_REASON > &aFailureDetails)
std::vector< PIN * > m_conns
Definition topo_match.h:135
friend class CONNECTION_GRAPH
Definition topo_match.h:100
void SetParent(COMPONENT *parent)
Definition topo_match.h:106
COMPONENT * m_parent
Definition topo_match.h:134
bool IsTopologicallySimilar(const PIN &b) const
Definition topo_match.h:112
const wxString & GetReference() const
Definition topo_match.h:126
bool IsIsomorphic(const PIN &b, TOPOLOGY_MISMATCH_REASON &aDetail) const
void AddConnection(PIN *pin)
Definition topo_match.h:110
COMPONENT * GetParent() const
Definition topo_match.h:128
int GetNetCode() const
Definition topo_match.h:124
const wxString Format() const
Definition topo_match.h:108
wxString m_ref
Definition topo_match.h:132
std::map< FOOTPRINT *, FOOTPRINT * > COMPONENT_MATCHES
Definition topo_match.h:172
KIBIS_PIN * pin
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695