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
74
75 std::optional<VECTOR2I> m_raOffset;
76 wxString m_reference;
77 wxString m_prefix;
79 std::vector<PIN*> m_pins;
80};
81
82class PIN
83{
84 friend class CONNECTION_GRAPH;
85
86public:
87 PIN() : m_netcode( 0 ), m_parent( nullptr ) {}
88 ~PIN() {}
89
90 void SetParent( COMPONENT* parent ) { m_parent = parent; }
91
92 const wxString Format() const { return m_parent->m_reference + wxT( "-" ) + m_ref; }
93
94 void AddConnection( PIN* pin ) { m_conns.push_back( pin ); }
95
96 bool IsTopologicallySimilar( const PIN& b ) const
97 {
98 wxASSERT( m_parent != b.m_parent );
99
100 if( !m_parent->IsSameKind( *b.m_parent ) )
101 return false;
102
103 return m_ref == b.m_ref;
104 }
105
106 bool IsIsomorphic( const PIN& b, TOPOLOGY_MISMATCH_REASON& aDetail ) const;
107
108 int GetNetCode() const { return m_netcode; }
109
110 const wxString& GetReference() const { return m_ref; }
111
112 COMPONENT* GetParent() const { return m_parent; }
113
114private:
115
116 wxString m_ref;
119 std::vector<PIN*> m_conns;
120};
121
123{
124 friend class CONNECTION_GRAPH;
125
126public:
128 {
129 m_ref = nullptr;
130 m_currentMatch = -1;
131 m_nloops = 0;
132 m_refIndex = 0;
133 }
134
136 {
138 m_ref = other.m_ref;
139 m_matches = other.m_matches;
140 m_locked = other.m_locked;
141 m_nloops = other.m_nloops;
142 m_refIndex = other.m_refIndex;
143 }
144
145 const std::map<COMPONENT*, COMPONENT*>& GetMatchingComponentPairs() const { return m_locked; }
146
147private:
151 std::vector<COMPONENT*> m_matches;
152 std::map<COMPONENT*, COMPONENT*> m_locked;
154};
155
156typedef std::map<FOOTPRINT*, FOOTPRINT*> COMPONENT_MATCHES;
157
159{
160public:
161 const int c_ITER_LIMIT = 10000;
162
165
166 void BuildConnectivity();
167 void AddFootprint( FOOTPRINT* aFp, const VECTOR2I& aOffset );
169 std::vector<TOPOLOGY_MISMATCH_REASON>& aFailureDetails );
170 static std::unique_ptr<CONNECTION_GRAPH> BuildFromFootprintSet( const std::set<FOOTPRINT*>& aFps );
171 std::vector<COMPONENT*> &Components() { return m_components; }
172
173private:
175 {
176 std::sort( m_components.begin(), m_components.end(),
177 []( COMPONENT* a, COMPONENT* b )
178 {
179 return a->GetPinCount() > b->GetPinCount();
180 } );
181 }
182
183
184 std::vector<COMPONENT*> findMatchingComponents( CONNECTION_GRAPH* aRefGraph,
185 COMPONENT* ref,
186 const BACKTRACK_STAGE& partialMatches,
187 std::vector<TOPOLOGY_MISMATCH_REASON>& aFailureDetails );
188
189 std::vector<COMPONENT*> m_components;
190
191};
192
193}; // namespace TMATCH
194
195#endif
friend class CONNECTION_GRAPH
Definition topo_match.h:124
const std::map< COMPONENT *, COMPONENT * > & GetMatchingComponentPairs() const
Definition topo_match.h:145
std::vector< COMPONENT * > m_matches
Definition topo_match.h:151
std::map< COMPONENT *, COMPONENT * > m_locked
Definition topo_match.h:152
BACKTRACK_STAGE(const BACKTRACK_STAGE &other)
Definition topo_match.h:135
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:78
std::optional< VECTOR2I > m_raOffset
Definition topo_match.h:75
bool MatchesWith(COMPONENT *b, TOPOLOGY_MISMATCH_REASON &aDetail)
wxString m_prefix
Definition topo_match.h:77
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:76
std::vector< PIN * > m_pins
Definition topo_match.h:79
FOOTPRINT * GetParent() const
Definition topo_match.h:66
std::vector< COMPONENT * > & Components()
Definition topo_match.h:171
std::vector< COMPONENT * > m_components
Definition topo_match.h:189
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:119
friend class CONNECTION_GRAPH
Definition topo_match.h:84
void SetParent(COMPONENT *parent)
Definition topo_match.h:90
COMPONENT * m_parent
Definition topo_match.h:118
bool IsTopologicallySimilar(const PIN &b) const
Definition topo_match.h:96
const wxString & GetReference() const
Definition topo_match.h:110
bool IsIsomorphic(const PIN &b, TOPOLOGY_MISMATCH_REASON &aDetail) const
void AddConnection(PIN *pin)
Definition topo_match.h:94
COMPONENT * GetParent() const
Definition topo_match.h:112
int GetNetCode() const
Definition topo_match.h:108
const wxString Format() const
Definition topo_match.h:92
wxString m_ref
Definition topo_match.h:116
std::map< FOOTPRINT *, FOOTPRINT * > COMPONENT_MATCHES
Definition topo_match.h:156
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695