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 (C) Kicad Developers, see change_log.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 friend class PIN;
48 friend class CONNECTION_GRAPH;
49
50public:
51 COMPONENT( const wxString& aRef, FOOTPRINT* aParentFp, std::optional<VECTOR2I> aRaOffset = std::optional<VECTOR2I>() );
52 ~COMPONENT();
53
54 bool IsSameKind( const COMPONENT& b ) const;
55 void AddPin( PIN* p );
56 int GetPinCount() const { return m_pins.size(); }
57 bool MatchesWith( COMPONENT* b );
58 std::vector<PIN*>& Pins() { return m_pins; }
60
61 bool HasRAOffset() const { return m_raOffset.has_value(); }
62 const VECTOR2I GetRAOffset() const { return *m_raOffset; }
63
64private:
65 void sortPinsByName();
66
67
68 std::optional<VECTOR2I> m_raOffset;
69 wxString m_reference;
70 wxString m_prefix;
72 std::vector<PIN*> m_pins;
73};
74
75class PIN
76{
77 friend class CONNECTION_GRAPH;
78
79public:
80 PIN() : m_netcode( 0 ), m_parent( nullptr ) {}
81 ~PIN() {}
82
83 void SetParent( COMPONENT* parent ) { m_parent = parent; }
84
85 const wxString Format() const { return m_parent->m_reference + wxT( "-" ) + m_ref; }
86
87 void AddConnection( PIN* pin ) { m_conns.push_back( pin ); }
88
89 bool IsTopologicallySimilar( const PIN& b ) const
90 {
91 wxASSERT( m_parent != b.m_parent );
92
93 if( !m_parent->IsSameKind( *b.m_parent ) )
94 return false;
95
96 return m_ref == b.m_ref;
97 }
98
99 bool IsIsomorphic( const PIN& b ) const;
100
101 int GetNetCode() const { return m_netcode; }
102
103 const wxString& GetReference() const { return m_ref; }
104
105 COMPONENT* GetParent() const { return m_parent; }
106
107private:
108
109 wxString m_ref;
112 std::vector<PIN*> m_conns;
113};
114
116{
117 friend class CONNECTION_GRAPH;
118
119public:
121 {
122 m_ref = nullptr;
123 m_currentMatch = -1;
124 m_nloops = 0;
125 m_refIndex = 0;
126 }
127
129 {
131 m_ref = other.m_ref;
132 m_matches = other.m_matches;
133 m_locked = other.m_locked;
134 m_nloops = other.m_nloops;
135 m_refIndex = other.m_refIndex;
136 }
137
138 const std::map<COMPONENT*, COMPONENT*>& GetMatchingComponentPairs() const { return m_locked; }
139
140private:
144 std::vector<COMPONENT*> m_matches;
145 std::map<COMPONENT*, COMPONENT*> m_locked;
147};
148
149typedef std::map<FOOTPRINT*, FOOTPRINT*> COMPONENT_MATCHES;
150
152{
153public:
154 const int c_ITER_LIMIT = 10000;
155
157 {
162 ST_OK = 0
163 };
164
167
168 void BuildConnectivity();
169 void AddFootprint( FOOTPRINT* aFp, const VECTOR2I& aOffset );
171 static std::unique_ptr<CONNECTION_GRAPH> BuildFromFootprintSet( const std::set<FOOTPRINT*>& aFps );
172 std::vector<COMPONENT*> &Components() { return m_components; }
173
174private:
176 {
177 std::sort( m_components.begin(), m_components.end(),
178 []( COMPONENT* a, COMPONENT* b )
179 {
180 return a->GetPinCount() > b->GetPinCount();
181 } );
182 }
183
184
185 std::vector<COMPONENT*> findMatchingComponents( CONNECTION_GRAPH* aRefGraph,
186 COMPONENT* ref,
187 BACKTRACK_STAGE& partialMatches );
188
189 std::vector<COMPONENT*> m_components;
190
191};
192
193}; // namespace TMATCH
194
195#endif
Calculate the connectivity of a schematic and generates netlists.
const std::map< COMPONENT *, COMPONENT * > & GetMatchingComponentPairs() const
Definition: topo_match.h:138
std::vector< COMPONENT * > m_matches
Definition: topo_match.h:144
std::map< COMPONENT *, COMPONENT * > m_locked
Definition: topo_match.h:145
BACKTRACK_STAGE(const BACKTRACK_STAGE &other)
Definition: topo_match.h:128
bool MatchesWith(COMPONENT *b)
Definition: topo_match.cpp:474
bool IsSameKind(const COMPONENT &b) const
Definition: topo_match.cpp:463
const VECTOR2I GetRAOffset() const
Definition: topo_match.h:62
int GetPinCount() const
Definition: topo_match.h:56
bool HasRAOffset() const
Definition: topo_match.h:61
FOOTPRINT * m_parentFootprint
Definition: topo_match.h:71
std::optional< VECTOR2I > m_raOffset
Definition: topo_match.h:68
wxString m_prefix
Definition: topo_match.h:70
void AddPin(PIN *p)
Definition: topo_match.cpp:468
std::vector< PIN * > & Pins()
Definition: topo_match.h:58
wxString m_reference
Definition: topo_match.h:69
std::vector< PIN * > m_pins
Definition: topo_match.h:72
FOOTPRINT * GetParent() const
Definition: topo_match.h:59
STATUS FindIsomorphism(CONNECTION_GRAPH *target, COMPONENT_MATCHES &result)
Definition: topo_match.cpp:291
std::vector< COMPONENT * > & Components()
Definition: topo_match.h:172
std::vector< COMPONENT * > m_components
Definition: topo_match.h:189
std::vector< COMPONENT * > findMatchingComponents(CONNECTION_GRAPH *aRefGraph, COMPONENT *ref, BACKTRACK_STAGE &partialMatches)
Definition: topo_match.cpp:200
void AddFootprint(FOOTPRINT *aFp, const VECTOR2I &aOffset)
Definition: topo_match.cpp:503
static std::unique_ptr< CONNECTION_GRAPH > BuildFromFootprintSet(const std::set< FOOTPRINT * > &aFps)
Definition: topo_match.cpp:518
std::vector< PIN * > m_conns
Definition: topo_match.h:112
void SetParent(COMPONENT *parent)
Definition: topo_match.h:83
COMPONENT * m_parent
Definition: topo_match.h:111
bool IsTopologicallySimilar(const PIN &b) const
Definition: topo_match.h:89
const wxString & GetReference() const
Definition: topo_match.h:103
bool IsIsomorphic(const PIN &b) const
Definition: topo_match.cpp:49
void AddConnection(PIN *pin)
Definition: topo_match.h:87
COMPONENT * GetParent() const
Definition: topo_match.h:105
int GetNetCode() const
Definition: topo_match.h:101
const wxString Format() const
Definition: topo_match.h:85
wxString m_ref
Definition: topo_match.h:109
std::map< FOOTPRINT *, FOOTPRINT * > COMPONENT_MATCHES
Definition: topo_match.h:149