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_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_refIndex = 0;
125 }
126
128 {
130 m_ref = other.m_ref;
131 m_matches = other.m_matches;
132 m_locked = other.m_locked;
133 m_nloops = other.m_nloops;
134 m_refIndex = other.m_refIndex;
135 }
136
137 const std::map<COMPONENT*, COMPONENT*>& GetMatchingComponentPairs() const { return m_locked; }
138
139private:
143 std::vector<COMPONENT*> m_matches;
144 std::map<COMPONENT*, COMPONENT*> m_locked;
146};
147
148typedef std::map<FOOTPRINT*, FOOTPRINT*> COMPONENT_MATCHES;
149
151{
152public:
153 const int c_ITER_LIMIT = 10000;
154
156 {
161 ST_OK = 0
162 };
163
166
167 void BuildConnectivity();
168 void AddFootprint( FOOTPRINT* aFp, const VECTOR2I& aOffset );
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 BACKTRACK_STAGE& partialMatches );
187
188 std::vector<COMPONENT*> m_components;
189
190};
191
192}; // namespace TMATCH
193
194#endif
Calculate the connectivity of a schematic and generates netlists.
const std::map< COMPONENT *, COMPONENT * > & GetMatchingComponentPairs() const
Definition: topo_match.h:137
std::vector< COMPONENT * > m_matches
Definition: topo_match.h:143
std::map< COMPONENT *, COMPONENT * > m_locked
Definition: topo_match.h:144
BACKTRACK_STAGE(const BACKTRACK_STAGE &other)
Definition: topo_match.h:127
bool MatchesWith(COMPONENT *b)
Definition: topo_match.cpp:476
bool IsSameKind(const COMPONENT &b) const
Definition: topo_match.cpp:465
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:470
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:171
std::vector< COMPONENT * > m_components
Definition: topo_match.h:188
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:505
static std::unique_ptr< CONNECTION_GRAPH > BuildFromFootprintSet(const std::set< FOOTPRINT * > &aFps)
Definition: topo_match.cpp:520
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:148