KiCad PCB EDA Suite
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-2022 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 <fp_text.h>
30#include <fp_textbox.h>
31#include <fp_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->GetPos0().x, pad->GetPos0().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_FP_TEXT_T:
105 {
106 const FP_TEXT* text = static_cast<const FP_TEXT*>( aItem );
107
108 if( !( aFlags & HASH_REF ) && text->GetType() == FP_TEXT::TEXT_is_REFERENCE )
109 break;
110
111 if( !( aFlags & HASH_VALUE ) && text->GetType() == FP_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 if( aFlags & REL_COORD )
127 hash_combine( ret, text->GetPos0().x, text->GetPos0().y );
128 else
129 hash_combine( ret, text->GetPosition().x, text->GetPosition().y );
130 }
131
132 if( aFlags & HASH_ROT )
133 hash_combine( ret, text->GetTextAngle().AsDegrees() );
134 }
135 break;
136
137 case PCB_FP_SHAPE_T:
138 {
139 const FP_SHAPE* shape = static_cast<const FP_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 if( aFlags & REL_COORD )
151 {
152 hash_combine( ret, shape->GetStart0().x );
153 hash_combine( ret, shape->GetStart0().y );
154 hash_combine( ret, shape->GetEnd0().x );
155 hash_combine( ret, shape->GetEnd0().y );
156
157 if( shape->GetShape() == SHAPE_T::ARC )
158 {
159 hash_combine( ret, shape->GetCenter0().x );
160 hash_combine( ret, shape->GetCenter0().y );
161 }
162 }
163 else
164 {
165 hash_combine( ret, shape->GetStart().x );
166 hash_combine( ret, shape->GetStart().y );
167 hash_combine( ret, shape->GetEnd().x );
168 hash_combine( ret, shape->GetEnd().y );
169
170 if( shape->GetShape() == SHAPE_T::ARC )
171 {
172 hash_combine( ret, shape->GetCenter().x );
173 hash_combine( ret, shape->GetCenter().y );
174 }
175 }
176 }
177 }
178 break;
179
180 case PCB_FP_TEXTBOX_T:
181 {
182 const FP_TEXTBOX* textbox = static_cast<const FP_TEXTBOX*>( aItem );
183
184 ret = hash_board_item( textbox, aFlags );
185 hash_combine( ret, textbox->GetText().ToStdString() );
186 hash_combine( ret, textbox->IsItalic() );
187 hash_combine( ret, textbox->IsBold() );
188 hash_combine( ret, textbox->IsMirrored() );
189 hash_combine( ret, textbox->GetTextWidth() );
190 hash_combine( ret, textbox->GetTextHeight() );
191 hash_combine( ret, textbox->GetHorizJustify() );
192 hash_combine( ret, textbox->GetVertJustify() );
193
194 if( aFlags & HASH_ROT )
195 hash_combine( ret, textbox->GetTextAngle().AsDegrees() );
196
197 hash_combine( ret, textbox->GetShape() );
198 hash_combine( ret, textbox->GetWidth() );
199
200 if( aFlags & HASH_POS )
201 {
202 if( aFlags & REL_COORD )
203 {
204 hash_combine( ret, textbox->GetStart0().x );
205 hash_combine( ret, textbox->GetStart0().y );
206 hash_combine( ret, textbox->GetEnd0().x );
207 hash_combine( ret, textbox->GetEnd0().y );
208 }
209 else
210 {
211 hash_combine( ret, textbox->GetStart().x );
212 hash_combine( ret, textbox->GetStart().y );
213 hash_combine( ret, textbox->GetEnd().x );
214 hash_combine( ret, textbox->GetEnd().y );
215 }
216 }
217 }
218 break;
219
220 default:
221 wxASSERT_MSG( false, "Unhandled type in function hash_fp_item() (exporter_gencad.cpp)" );
222 }
223
224 return ret;
225}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:58
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:185
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:202
bool IsItalic() const
Definition: eda_text.h:130
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:120
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:199
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:149
bool IsMirrored() const
Definition: eda_text.h:139
bool IsBold() const
Definition: eda_text.h:133
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition: eda_text.h:152
EDA_ANGLE GetOrientation() const
Definition: footprint.h:191
PADS & Pads()
Definition: footprint.h:170
VECTOR2I GetPosition() const override
Definition: footprint.h:188
DRAWINGS & GraphicalItems()
Definition: footprint.h:173
const VECTOR2I & GetEnd0() const
Definition: fp_shape.h:95
VECTOR2I GetCenter0() const
Definition: fp_shape.cpp:144
const VECTOR2I & GetStart0() const
Definition: fp_shape.h:92
@ TEXT_is_REFERENCE
Definition: fp_text.h:49
@ TEXT_is_VALUE
Definition: fp_text.h:50
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
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
Definition: bitmap.cpp:64
@ PCB_FP_SHAPE_T
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:94
@ PCB_FP_TEXTBOX_T
class FP_TEXTBOX, wrapped text in a footprint
Definition: typeinfo.h:93
@ 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
@ PCB_FP_TEXT_T
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92