KiCad PCB EDA Suite
Loading...
Searching...
No Matches
hash_eda.cpp
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) 2017 CERN
5 * Copyright (C) 2020-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <hash_eda.h>
27#include <hash.h>
28#include <footprint.h>
29#include <pcb_text.h>
30#include <pcb_textbox.h>
31#include <pcb_shape.h>
32#include <pad.h>
33
34#include <functional>
35
36using namespace std;
37
38// Common calculation part for all BOARD_ITEMs
39static inline size_t hash_board_item( const BOARD_ITEM* aItem, int aFlags )
40{
41 size_t ret = 0;
42
43 if( aFlags & HASH_LAYER )
44 ret = hash<unsigned long long>{}( aItem->GetLayerSet().to_ullong() );
45
46 return ret;
47}
48
49
50size_t hash_fp_item( const EDA_ITEM* aItem, int aFlags )
51{
52 size_t ret = 0;
53
54 switch( aItem->Type() )
55 {
56 case PCB_FOOTPRINT_T:
57 {
58 const FOOTPRINT* footprint = static_cast<const FOOTPRINT*>( aItem );
59
60 ret = hash_board_item( footprint, aFlags );
61
62 if( aFlags & HASH_POS )
63 hash_combine( ret, footprint->GetPosition().x, footprint->GetPosition().y );
64
65 if( aFlags & HASH_ROT )
66 hash_combine( ret, footprint->GetOrientation().AsDegrees() );
67
68 for( BOARD_ITEM* item : footprint->GraphicalItems() )
69 hash_combine( ret, hash_fp_item( item, aFlags ) );
70
71 for( PAD* pad : footprint->Pads() )
72 hash_combine( ret, hash_fp_item( static_cast<EDA_ITEM*>( pad ), aFlags ) );
73 }
74 break;
75
76 case PCB_PAD_T:
77 {
78 const PAD* pad = static_cast<const PAD*>( aItem );
79
80 ret = hash<int>{}( static_cast<int>( pad->GetShape() ) );
81 hash_combine( ret, pad->GetDrillShape() );
82 hash_combine( ret, pad->GetSize().x, pad->GetSize().y );
83 hash_combine( ret, pad->GetOffset().x, pad->GetOffset().y );
84 hash_combine( ret, pad->GetDelta().x, pad->GetDelta().y );
85
86 hash_combine( ret, hash_board_item( pad, aFlags ) );
87
88 if( aFlags & HASH_POS )
89 {
90 if( aFlags & REL_COORD )
91 hash_combine( ret, pad->GetFPRelativePosition().x, pad->GetFPRelativePosition().y );
92 else
93 hash_combine( ret, pad->GetPosition().x, pad->GetPosition().y );
94 }
95
96 if( aFlags & HASH_ROT )
97 hash_combine( ret, pad->GetOrientation().AsDegrees() );
98
99 if( aFlags & HASH_NET )
100 hash_combine( ret, pad->GetNetCode() );
101 }
102 break;
103
104 case PCB_TEXT_T:
105 {
106 const PCB_TEXT* text = static_cast<const PCB_TEXT*>( aItem );
107
108 if( !( aFlags & HASH_REF ) && text->GetType() == PCB_TEXT::TEXT_is_REFERENCE )
109 break;
110
111 if( !( aFlags & HASH_VALUE ) && text->GetType() == PCB_TEXT::TEXT_is_VALUE )
112 break;
113
114 ret = hash_board_item( text, aFlags );
115 hash_combine( ret, text->GetText().ToStdString() );
116 hash_combine( ret, text->IsItalic() );
117 hash_combine( ret, text->IsBold() );
118 hash_combine( ret, text->IsMirrored() );
119 hash_combine( ret, text->GetTextWidth() );
120 hash_combine( ret, text->GetTextHeight() );
121 hash_combine( ret, text->GetHorizJustify() );
122 hash_combine( ret, text->GetVertJustify() );
123
124 if( aFlags & HASH_POS )
125 {
126 VECTOR2I pos = ( aFlags & REL_COORD ) ? text->GetFPRelativePosition()
127 : text->GetPosition();
128
129 hash_combine( ret, pos.x, pos.y );
130 }
131
132 if( aFlags & HASH_ROT )
133 hash_combine( ret, text->GetTextAngle().AsDegrees() );
134 }
135 break;
136
137 case PCB_SHAPE_T:
138 {
139 const PCB_SHAPE* shape = static_cast<const PCB_SHAPE*>( aItem );
140 ret = hash_board_item( shape, aFlags );
141 hash_combine( ret, shape->GetShape() );
142 hash_combine( ret, shape->GetWidth() );
143 hash_combine( ret, shape->IsFilled() );
144
145 if( shape->GetShape() == SHAPE_T::ARC || shape->GetShape() == SHAPE_T::CIRCLE )
146 hash_combine( ret, shape->GetRadius() );
147
148 if( aFlags & HASH_POS )
149 {
150 VECTOR2I start = shape->GetStart();
151 VECTOR2I end = shape->GetEnd();
152 VECTOR2I center = shape->GetCenter();
153
154 FOOTPRINT* parentFP = shape->GetParentFootprint();
155
156 if( parentFP && ( aFlags & REL_COORD ) )
157 {
158 start -= parentFP->GetPosition();
159 end -= parentFP->GetPosition();
160 center -= parentFP->GetPosition();
161
162 RotatePoint( start, -parentFP->GetOrientation() );
163 RotatePoint( end, -parentFP->GetOrientation() );
164 RotatePoint( center, -parentFP->GetOrientation() );
165 }
166
167 hash_combine( ret, start.x );
168 hash_combine( ret, start.y );
169 hash_combine( ret, end.x );
170 hash_combine( ret, end.y );
171
172 if( shape->GetShape() == SHAPE_T::ARC )
173 {
174 hash_combine( ret, center.x );
175 hash_combine( ret, center.y );
176 }
177 }
178 }
179 break;
180
181 case PCB_TEXTBOX_T:
182 {
183 const PCB_TEXTBOX* textbox = static_cast<const PCB_TEXTBOX*>( aItem );
184
185 ret = hash_board_item( textbox, aFlags );
186 hash_combine( ret, textbox->GetText().ToStdString() );
187 hash_combine( ret, textbox->IsItalic() );
188 hash_combine( ret, textbox->IsBold() );
189 hash_combine( ret, textbox->IsMirrored() );
190 hash_combine( ret, textbox->GetTextWidth() );
191 hash_combine( ret, textbox->GetTextHeight() );
192 hash_combine( ret, textbox->GetHorizJustify() );
193 hash_combine( ret, textbox->GetVertJustify() );
194
195 if( aFlags & HASH_ROT )
196 hash_combine( ret, textbox->GetTextAngle().AsDegrees() );
197
198 hash_combine( ret, textbox->GetShape() );
199 hash_combine( ret, textbox->GetWidth() );
200
201 if( aFlags & HASH_POS )
202 {
203 VECTOR2I start = textbox->GetStart();
204 VECTOR2I end = textbox->GetEnd();
205
206 FOOTPRINT* parentFP = textbox->GetParentFootprint();
207
208 if( parentFP && ( aFlags & REL_COORD ) )
209 {
210 start -= parentFP->GetPosition();
211 end -= parentFP->GetPosition();
212
213 RotatePoint( start, -parentFP->GetOrientation() );
214 RotatePoint( end, -parentFP->GetOrientation() );
215 }
216
217 hash_combine( ret, start.x );
218 hash_combine( ret, start.y );
219 hash_combine( ret, end.x );
220 hash_combine( ret, end.y );
221 }
222 }
223 break;
224
225 default:
226 wxASSERT_MSG( false, "Unhandled type in function hash_fp_item() (exporter_gencad.cpp)" );
227 }
228
229 return ret;
230}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:71
FOOTPRINT * GetParentFootprint() const
Definition: board_item.cpp:240
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:201
double AsDegrees() const
Definition: eda_angle.h:149
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
bool IsFilled() const
Definition: eda_shape.h:90
int GetRadius() const
Definition: eda_shape.cpp:523
SHAPE_T GetShape() const
Definition: eda_shape.h:113
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:145
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:120
int GetWidth() const
Definition: eda_shape.h:109
int GetTextHeight() const
Definition: eda_text.h:205
bool IsItalic() const
Definition: eda_text.h:133
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:123
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:87
int GetTextWidth() const
Definition: eda_text.h:202
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:152
bool IsMirrored() const
Definition: eda_text.h:142
bool IsBold() const
Definition: eda_text.h:136
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition: eda_text.h:155
EDA_ANGLE GetOrientation() const
Definition: footprint.h:193
PADS & Pads()
Definition: footprint.h:172
VECTOR2I GetPosition() const override
Definition: footprint.h:190
DRAWINGS & GraphicalItems()
Definition: footprint.h:175
Definition: pad.h:59
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_shape.h:67
@ TEXT_is_REFERENCE
Definition: pcb_text.h:50
@ TEXT_is_VALUE
Definition: pcb_text.h:51
static void hash_combine(std::size_t &seed)
This is a dummy function to take the final case of hash_combine below.
Definition: hash.h:34
size_t hash_fp_item(const EDA_ITEM *aItem, int aFlags)
Calculate hash of an EDA_ITEM.
Definition: hash_eda.cpp:50
static size_t hash_board_item(const BOARD_ITEM *aItem, int aFlags)
Definition: hash_eda.cpp:39
@ HASH_POS
use coordinates relative to the parent object
Definition: hash_eda.h:43
@ HASH_VALUE
Definition: hash_eda.h:51
@ REL_COORD
Definition: hash_eda.h:46
@ HASH_LAYER
Definition: hash_eda.h:48
@ HASH_REF
Definition: hash_eda.h:50
@ HASH_ROT
Definition: hash_eda.h:47
@ HASH_NET
Definition: hash_eda.h:49
STL namespace.
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Definition: trigo.cpp:183
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:88
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:91
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:90
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87