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 <macros.h>
35#include <functional>
36
37using namespace std;
38
39// Common calculation part for all BOARD_ITEMs
40static inline size_t hash_board_item( const BOARD_ITEM* aItem, int aFlags )
41{
42 size_t ret = 0;
43
44 if( aFlags & HASH_LAYER )
45 ret = hash<unsigned long long>{}( aItem->GetLayerSet().to_ullong() );
46
47 return ret;
48}
49
50
51size_t hash_fp_item( const EDA_ITEM* aItem, int aFlags )
52{
53 size_t ret = 0;
54
55 switch( aItem->Type() )
56 {
57 case PCB_FOOTPRINT_T:
58 {
59 const FOOTPRINT* footprint = static_cast<const FOOTPRINT*>( aItem );
60
61 ret = hash_board_item( footprint, aFlags );
62
63 if( aFlags & HASH_POS )
64 hash_combine( ret, footprint->GetPosition().x, footprint->GetPosition().y );
65
66 if( aFlags & HASH_ROT )
67 hash_combine( ret, footprint->GetOrientation().AsDegrees() );
68
69 for( BOARD_ITEM* item : footprint->GraphicalItems() )
70 hash_combine( ret, hash_fp_item( item, aFlags ) );
71
72 for( PAD* pad : footprint->Pads() )
73 hash_combine( ret, hash_fp_item( static_cast<EDA_ITEM*>( pad ), aFlags ) );
74 }
75 break;
76
77 case PCB_PAD_T:
78 {
79 const PAD* pad = static_cast<const PAD*>( aItem );
80
81 ret = hash<int>{}( static_cast<int>( pad->GetShape() ) );
82 hash_combine( ret, pad->GetDrillShape() );
83 hash_combine( ret, pad->GetSize().x, pad->GetSize().y );
84 hash_combine( ret, pad->GetOffset().x, pad->GetOffset().y );
85 hash_combine( ret, pad->GetDelta().x, pad->GetDelta().y );
86
87 hash_combine( ret, hash_board_item( pad, aFlags ) );
88
89 if( aFlags & HASH_POS )
90 {
91 if( aFlags & REL_COORD )
92 hash_combine( ret, pad->GetFPRelativePosition().x, pad->GetFPRelativePosition().y );
93 else
94 hash_combine( ret, pad->GetPosition().x, pad->GetPosition().y );
95 }
96
97 if( aFlags & HASH_ROT )
98 hash_combine( ret, pad->GetOrientation().AsDegrees() );
99
100 if( aFlags & HASH_NET )
101 hash_combine( ret, pad->GetNetCode() );
102 }
103 break;
104
105 case PCB_FIELD_T:
106 if( !( aFlags & HASH_REF ) && static_cast<const PCB_FIELD*>( aItem )->IsReference() )
107 break;
108
109 if( !( aFlags & HASH_VALUE ) && static_cast<const PCB_FIELD*>( aItem )->IsValue() )
110 break;
111
113 case PCB_TEXT_T:
114 {
115 const PCB_TEXT* text = static_cast<const PCB_TEXT*>( aItem );
116
117 ret = hash_board_item( text, aFlags );
118 hash_combine( ret, text->GetText().ToStdString() );
119 hash_combine( ret, text->IsItalic() );
120 hash_combine( ret, text->IsBold() );
121 hash_combine( ret, text->IsMirrored() );
122 hash_combine( ret, text->GetTextWidth() );
123 hash_combine( ret, text->GetTextHeight() );
124 hash_combine( ret, text->GetHorizJustify() );
125 hash_combine( ret, text->GetVertJustify() );
126
127 if( aFlags & HASH_POS )
128 {
129 VECTOR2I pos = ( aFlags & REL_COORD ) ? text->GetFPRelativePosition()
130 : text->GetPosition();
131
132 hash_combine( ret, pos.x, pos.y );
133 }
134
135 if( aFlags & HASH_ROT )
136 hash_combine( ret, text->GetTextAngle().AsDegrees() );
137 }
138 break;
139
140 case PCB_SHAPE_T:
141 {
142 const PCB_SHAPE* shape = static_cast<const PCB_SHAPE*>( aItem );
143 ret = hash_board_item( shape, aFlags );
144 hash_combine( ret, shape->GetShape() );
145 hash_combine( ret, shape->GetWidth() );
146 hash_combine( ret, shape->IsFilled() );
147
148 if( shape->GetShape() == SHAPE_T::ARC || shape->GetShape() == SHAPE_T::CIRCLE )
149 hash_combine( ret, shape->GetRadius() );
150
151 if( aFlags & HASH_POS )
152 {
153 VECTOR2I start = shape->GetStart();
154 VECTOR2I end = shape->GetEnd();
155 VECTOR2I center = shape->GetCenter();
156
157 FOOTPRINT* parentFP = shape->GetParentFootprint();
158
159 if( parentFP && ( aFlags & REL_COORD ) )
160 {
161 start -= parentFP->GetPosition();
162 end -= parentFP->GetPosition();
163 center -= parentFP->GetPosition();
164
165 RotatePoint( start, -parentFP->GetOrientation() );
166 RotatePoint( end, -parentFP->GetOrientation() );
167 RotatePoint( center, -parentFP->GetOrientation() );
168 }
169
170 hash_combine( ret, start.x );
171 hash_combine( ret, start.y );
172 hash_combine( ret, end.x );
173 hash_combine( ret, end.y );
174
175 if( shape->GetShape() == SHAPE_T::ARC )
176 {
177 hash_combine( ret, center.x );
178 hash_combine( ret, center.y );
179 }
180 }
181 }
182 break;
183
184 case PCB_TEXTBOX_T:
185 {
186 const PCB_TEXTBOX* textbox = static_cast<const PCB_TEXTBOX*>( aItem );
187
188 ret = hash_board_item( textbox, aFlags );
189 hash_combine( ret, textbox->GetText().ToStdString() );
190 hash_combine( ret, textbox->IsItalic() );
191 hash_combine( ret, textbox->IsBold() );
192 hash_combine( ret, textbox->IsMirrored() );
193 hash_combine( ret, textbox->GetTextWidth() );
194 hash_combine( ret, textbox->GetTextHeight() );
195 hash_combine( ret, textbox->GetHorizJustify() );
196 hash_combine( ret, textbox->GetVertJustify() );
197
198 if( aFlags & HASH_ROT )
199 hash_combine( ret, textbox->GetTextAngle().AsDegrees() );
200
201 hash_combine( ret, textbox->GetShape() );
202 hash_combine( ret, textbox->GetWidth() );
203
204 if( aFlags & HASH_POS )
205 {
206 VECTOR2I start = textbox->GetStart();
207 VECTOR2I end = textbox->GetEnd();
208
209 FOOTPRINT* parentFP = textbox->GetParentFootprint();
210
211 if( parentFP && ( aFlags & REL_COORD ) )
212 {
213 start -= parentFP->GetPosition();
214 end -= parentFP->GetPosition();
215
216 RotatePoint( start, -parentFP->GetOrientation() );
217 RotatePoint( end, -parentFP->GetOrientation() );
218 }
219
220 hash_combine( ret, start.x );
221 hash_combine( ret, start.y );
222 hash_combine( ret, end.x );
223 hash_combine( ret, end.y );
224 }
225 }
226 break;
227
228 default:
229 wxASSERT_MSG( false, "Unhandled type in function hash_fp_item() (exporter_gencad.cpp)" );
230 }
231
232 return ret;
233}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
FOOTPRINT * GetParentFootprint() const
Definition: board_item.cpp:247
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:209
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:91
int GetRadius() const
Definition: eda_shape.cpp:588
SHAPE_T GetShape() const
Definition: eda_shape.h:117
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:149
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:124
int GetTextHeight() const
Definition: eda_text.h:213
bool IsItalic() const
Definition: eda_text.h:141
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:131
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:95
int GetTextWidth() const
Definition: eda_text.h:210
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:160
bool IsMirrored() const
Definition: eda_text.h:150
bool IsBold() const
Definition: eda_text.h:144
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition: eda_text.h:163
EDA_ANGLE GetOrientation() const
Definition: footprint.h:209
PADS & Pads()
Definition: footprint.h:188
VECTOR2I GetPosition() const override
Definition: footprint.h:206
DRAWINGS & GraphicalItems()
Definition: footprint.h:191
Definition: pad.h:58
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_shape.h:72
int GetWidth() const override
Definition: pcb_shape.cpp:149
@ ARC
use RECTANGLE instead of RECT to avoid collision in a Windows header
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:51
static size_t hash_board_item(const BOARD_ITEM *aItem, int aFlags)
Definition: hash_eda.cpp:40
@ 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
This file contains miscellaneous commonly used macros and functions.
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83
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:92
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:91
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
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