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 The 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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <hash_eda.h>
23#include <hash.h>
24#include <footprint.h>
25#include <pcb_text.h>
26#include <pcb_table.h>
27#include <pcb_textbox.h>
28#include <pcb_shape.h>
29#include <pad.h>
30#include <pcb_track.h>
31#include <pcb_barcode.h>
32
33#include <macros.h>
34#include <functional>
35#include <algorithm>
36#include <vector>
37
38#include <wx/log.h>
39
40using namespace std;
41
42// Common calculation part for all BOARD_ITEMs
43static inline size_t hash_board_item( const BOARD_ITEM* aItem, int aFlags )
44{
45 size_t ret = 0;
46
47 if( aFlags & HASH_LAYER )
48 ret = hash<BASE_SET>{}( aItem->GetLayerSet() );
49
50 return ret;
51}
52
53
54size_t hash_fp_item( const EDA_ITEM* aItem, int aFlags )
55{
56 size_t ret = 0;
57
58 switch( aItem->Type() )
59 {
60 case PCB_FOOTPRINT_T:
61 {
62 const FOOTPRINT* footprint = static_cast<const FOOTPRINT*>( aItem );
63
64 ret = hash_board_item( footprint, aFlags );
65
66 if( aFlags & HASH_POS )
67 hash_combine( ret, footprint->GetPosition().x, footprint->GetPosition().y );
68
69 if( aFlags & HASH_ROT )
70 hash_combine( ret, footprint->GetOrientation().AsDegrees() );
71
72 std::vector<size_t> hashes;
73
74 for( BOARD_ITEM* item : footprint->GraphicalItems() )
75 hashes.push_back( hash_fp_item( item, aFlags ) );
76
77 for( PAD* pad : footprint->Pads() )
78 hashes.push_back( hash_fp_item( static_cast<EDA_ITEM*>( pad ), aFlags ) );
79
80 std::sort( hashes.begin(), hashes.end() );
81
82 for( size_t h : hashes )
83 hash_combine( ret, h );
84 }
85 break;
86
87 case PCB_VIA_T:
88 {
89 const PCB_VIA* via = static_cast<const PCB_VIA*>( aItem );
90
91 ret = hash<int>{}( via->GetDrillValue() );
92 hash_combine( ret, via->TopLayer() );
93 hash_combine( ret, via->BottomLayer() );
94
95 via->GetLayerSet().RunOnLayers(
96 [&]( PCB_LAYER_ID layer )
97 {
98 hash_combine( ret, via->GetWidth( layer ) );
99 hash_combine( ret, via->FlashLayer( layer ) );
100 } );
101
102 break;
103 }
104
105 case PCB_PAD_T:
106 {
107 const PAD* pad = static_cast<const PAD*>( aItem );
108
109 ret = hash<int>{}( static_cast<int>( pad->GetAttribute() ) );
110
111 auto hashPadLayer =
112 [&]( PCB_LAYER_ID aLayer )
113 {
114 hash_combine( ret, pad->GetShape( aLayer ) );
115 hash_combine( ret, pad->GetSize( aLayer ).x, pad->GetSize( aLayer ).y );
116 hash_combine( ret, pad->GetOffset( aLayer ).x, pad->GetOffset( aLayer ).y );
117
118 switch( pad->GetShape( aLayer ) )
119 {
121 hash_combine( ret, pad->GetChamferPositions( aLayer ) );
122 hash_combine( ret, pad->GetChamferRectRatio( aLayer ) );
123 break;
124
126 hash_combine( ret, pad->GetRoundRectCornerRadius( aLayer ) );
127 break;
128
130 hash_combine( ret, pad->GetDelta( aLayer ).x, pad->GetDelta( aLayer ).y );
131 break;
132
134 {
135 auto poly = pad->GetEffectivePolygon( aLayer, ERROR_INSIDE );
136
137 for( int ii = 0; ii < poly->VertexCount(); ++ii )
138 {
139 VECTOR2I point = poly->CVertex( ii ) - pad->GetPosition();
140 hash_combine( ret, point.x, point.y );
141 }
142
143 break;
144 }
145 default:
146 break;
147 }
148 };
149
150 pad->Padstack().ForEachUniqueLayer( hashPadLayer );
151
152 if( pad->GetAttribute() == PAD_ATTRIB::PTH || pad->GetAttribute() == PAD_ATTRIB::NPTH )
153 {
154 hash_combine( ret, pad->GetDrillSize().x, pad->GetDrillSize().y );
155 hash_combine( ret, pad->GetDrillShape() );
156
157 pad->GetLayerSet().RunOnLayers(
158 [&]( PCB_LAYER_ID layer )
159 {
160 hash_combine( ret, pad->FlashLayer( layer ) );
161 } );
162 }
163
164 hash_combine( ret, hash_board_item( pad, aFlags ) );
165
166 if( aFlags & HASH_POS )
167 {
168 if( aFlags & RELATIVE_TO_PARENT_COORDS )
169 hash_combine( ret, pad->GetFPRelativePosition().x, pad->GetFPRelativePosition().y );
170 else
171 hash_combine( ret, pad->GetPosition().x, pad->GetPosition().y );
172 }
173
174 if( aFlags & HASH_ROT )
175 hash_combine( ret, pad->GetOrientation().AsDegrees() );
176
177 if( aFlags & HASH_NET )
178 hash_combine( ret, pad->GetNetCode() );
179 }
180 break;
181
182 case PCB_FIELD_T:
183 if( !( aFlags & HASH_REF ) && static_cast<const PCB_FIELD*>( aItem )->IsReference() )
184 break;
185
186 if( !( aFlags & HASH_VALUE ) && static_cast<const PCB_FIELD*>( aItem )->IsValue() )
187 break;
188
190 case PCB_TEXT_T:
191 {
192 const PCB_TEXT* text = static_cast<const PCB_TEXT*>( aItem );
193
194 ret = hash_board_item( text, aFlags );
195 hash_combine( ret, text->GetText().ToStdString() );
196 hash_combine( ret, text->IsItalic() );
197 hash_combine( ret, text->IsBold() );
198 hash_combine( ret, text->IsMirrored() );
199 hash_combine( ret, text->GetTextWidth() );
200 hash_combine( ret, text->GetTextHeight() );
201 hash_combine( ret, text->GetHorizJustify() );
202 hash_combine( ret, text->GetVertJustify() );
203
204 if( aFlags & HASH_POS )
205 {
206 VECTOR2I pos = ( aFlags & RELATIVE_TO_PARENT_COORDS ) ? text->GetFPRelativePosition()
207 : text->GetPosition();
208
209 hash_combine( ret, pos.x, pos.y );
210 }
211
212 if( aFlags & HASH_ROT )
213 hash_combine( ret, text->GetTextAngle().AsDegrees() );
214
215 break;
216 }
217
218 case PCB_BARCODE_T:
219 {
220 const PCB_BARCODE* barcode = static_cast<const PCB_BARCODE*>( aItem );
221
222 ret = hash_board_item( barcode, aFlags );
223 hash_combine( ret, barcode->GetWidth(), barcode->GetHeight() );
224 hash_combine( ret, barcode->GetPosition().x, barcode->GetPosition().y );
225 hash_combine( ret, barcode->GetMargin().x, barcode->GetMargin().y );
226 hash_combine( ret, barcode->GetText() );
227 hash_combine( ret, barcode->GetTextSize() );
228 hash_combine( ret, barcode->GetKind() );
229 hash_combine( ret, barcode->GetAngle().AsDegrees() );
230 hash_combine( ret, barcode->GetErrorCorrection() );
231 break;
232 }
233
234 case PCB_SHAPE_T:
235 {
236 const PCB_SHAPE* shape = static_cast<const PCB_SHAPE*>( aItem );
237 ret = hash_board_item( shape, aFlags );
238 hash_combine( ret, shape->GetShape() );
239 hash_combine( ret, shape->GetWidth() );
240 hash_combine( ret, shape->GetFillMode() );
241 hash_combine( ret, shape->GetLineStyle() );
242
243 if( shape->GetShape() == SHAPE_T::ARC || shape->GetShape() == SHAPE_T::CIRCLE )
244 hash_combine( ret, shape->GetRadius() );
245
246 if( shape->GetShape() == SHAPE_T::ELLIPSE || shape->GetShape() == SHAPE_T::ELLIPSE_ARC )
247 {
248 hash_combine( ret, shape->GetEllipseMajorRadius() );
249 hash_combine( ret, shape->GetEllipseMinorRadius() );
250 hash_combine( ret, shape->GetEllipseRotation().AsDegrees() );
251
252 if( shape->GetShape() == SHAPE_T::ELLIPSE_ARC )
253 {
255 hash_combine( ret, shape->GetEllipseEndAngle().AsDegrees() );
256 }
257 }
258
259 if( shape->GetShape() == SHAPE_T::RECTANGLE )
260 hash_combine( ret, shape->GetCornerRadius() );
261
262 if( aFlags & HASH_POS )
263 {
264 std::vector<VECTOR2I> points;
265
266 points.push_back( shape->GetStart() );
267 points.push_back( shape->GetEnd() );
268
269 switch( shape->GetShape() )
270 {
271 case SHAPE_T::CIRCLE:
272 points.push_back( shape->GetCenter() );
273 break;
274
275 case SHAPE_T::ARC:
276 points.push_back( shape->GetArcMid() );
277 break;
278
279 case SHAPE_T::ELLIPSE:
281 points.push_back( shape->GetEllipseCenter() );
282 break;
283
284 case SHAPE_T::POLY:
285 for( auto it = shape->GetPolyShape().CIterateWithHoles(); it; it++ )
286 points.push_back( *it );
287
288 break;
289
290 case SHAPE_T::BEZIER:
291 points.push_back( shape->GetBezierC1() );
292 points.push_back( shape->GetBezierC2() );
293 break;
294
295 default:
296 break;
297 }
298
299 FOOTPRINT* parentFP = shape->GetParentFootprint();
300
301 if( parentFP && ( aFlags & RELATIVE_TO_PARENT_COORDS ) )
302 {
303 for( VECTOR2I& point : points )
304 {
305 point -= parentFP->GetPosition();
306 RotatePoint( point, -parentFP->GetOrientation() );
307 }
308 }
309
310 if( aFlags & RELATIVE_TO_SHAPE_POS )
311 {
312 for( VECTOR2I& point : points )
313 point -= shape->GetPosition();
314 }
315
316 //Basic sort of start/end points to try to always draw the same direction (left to right, down to up)
317 //The hashes are summed, so it doesn't matter what order the lines are drawn, only that the same points are used
318 if( points.size() > 1 )
319 {
320 if( points[0].x > points[1].x || points[0].y > points[1].y )
321 std::swap( points[0], points[1] );
322 }
323
324 for( VECTOR2I& point : points )
325 hash_combine( ret, point.x, point.y );
326 }
327 }
328 break;
329
330 case PCB_TABLECELL_T:
331 case PCB_TEXTBOX_T:
332 {
333 const PCB_TEXTBOX* textbox = static_cast<const PCB_TEXTBOX*>( aItem );
334
335 ret = hash_board_item( textbox, aFlags );
336 hash_combine( ret, textbox->GetText().ToStdString() );
337 hash_combine( ret, textbox->IsItalic() );
338 hash_combine( ret, textbox->IsBold() );
339 hash_combine( ret, textbox->IsMirrored() );
340 hash_combine( ret, textbox->GetTextWidth() );
341 hash_combine( ret, textbox->GetTextHeight() );
342 hash_combine( ret, textbox->GetHorizJustify() );
343 hash_combine( ret, textbox->GetVertJustify() );
344
345 if( aFlags & HASH_ROT )
346 hash_combine( ret, textbox->GetTextAngle().AsDegrees() );
347
348 hash_combine( ret, textbox->GetShape() );
349 hash_combine( ret, textbox->GetWidth() );
350 hash_combine( ret, textbox->GetLineStyle() );
351
352 if( aFlags & HASH_POS )
353 {
354 VECTOR2I start = textbox->GetStart();
355 VECTOR2I end = textbox->GetEnd();
356
357 FOOTPRINT* parentFP = textbox->GetParentFootprint();
358
359 if( parentFP && ( aFlags & RELATIVE_TO_PARENT_COORDS ) )
360 {
361 start -= parentFP->GetPosition();
362 end -= parentFP->GetPosition();
363
364 RotatePoint( start, -parentFP->GetOrientation() );
365 RotatePoint( end, -parentFP->GetOrientation() );
366 }
367
368 hash_combine( ret, start.x );
369 hash_combine( ret, start.y );
370 hash_combine( ret, end.x );
371 hash_combine( ret, end.y );
372 }
373 }
374 break;
375
376 case PCB_TABLE_T:
377 {
378 const PCB_TABLE* table = static_cast<const PCB_TABLE*>( aItem );
379
380 ret = hash_board_item( table, aFlags );
381
382 hash_combine( ret, table->StrokeExternal() );
383 hash_combine( ret, table->StrokeHeaderSeparator() );
384 hash_combine( ret, table->StrokeColumns() );
385 hash_combine( ret, table->StrokeRows() );
386
387 auto hash_stroke =
388 [&]( const STROKE_PARAMS& stroke )
389 {
390 hash_combine( ret, stroke.GetColor() );
391 hash_combine( ret, stroke.GetWidth() );
392 hash_combine( ret, stroke.GetLineStyle() );
393 };
394
395 hash_stroke( table->GetSeparatorsStroke() );
396 hash_stroke( table->GetBorderStroke() );
397 }
398 break;
399
400 default:
401 UNIMPLEMENTED_FOR( aItem->GetClass() );
402 }
403
404 return ret;
405}
@ ERROR_INSIDE
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:99
FOOTPRINT * GetParentFootprint() const
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition board_item.h:369
double AsDegrees() const
Definition eda_angle.h:115
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
int GetEllipseMinorRadius() const
Definition eda_shape.h:404
const VECTOR2I & GetBezierC2() const
Definition eda_shape.h:377
const VECTOR2I & GetEllipseCenter() const
Definition eda_shape.h:386
EDA_ANGLE GetEllipseEndAngle() const
Definition eda_shape.h:432
FILL_T GetFillMode() const
Definition eda_shape.h:151
int GetEllipseMajorRadius() const
Definition eda_shape.h:395
SHAPE_POLY_SET & GetPolyShape()
EDA_ANGLE GetEllipseRotation() const
Definition eda_shape.h:413
int GetRadius() const
SHAPE_T GetShape() const
Definition eda_shape.h:178
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:334
LINE_STYLE GetLineStyle() const
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:284
EDA_ANGLE GetEllipseStartAngle() const
Definition eda_shape.h:423
const VECTOR2I & GetBezierC1() const
Definition eda_shape.h:374
int GetCornerRadius() const
VECTOR2I GetArcMid() const
bool IsItalic() const
Definition eda_text.h:200
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:118
virtual int GetTextHeight() const
Definition eda_text.h:307
virtual int GetTextWidth() const
Definition eda_text.h:304
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition eda_text.h:239
bool IsMirrored() const
Definition eda_text.h:229
bool IsBold() const
Definition eda_text.h:215
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition eda_text.h:242
EDA_ANGLE GetOrientation() const
Definition footprint.h:382
std::deque< PAD * > & Pads()
Definition footprint.h:355
VECTOR2I GetPosition() const override
Definition footprint.h:379
DRAWINGS & GraphicalItems()
Definition footprint.h:358
virtual wxString GetClass() const =0
Return the class name.
Definition pad.h:61
const VECTOR2I & GetMargin() const
Get the barcode margin (in internal units).
VECTOR2I GetPosition() const override
Get the position (center) of the barcode in internal units.
wxString GetText() const
int GetTextSize() const
int GetHeight() const
Get the barcode height (in internal units).
BARCODE_ECC_T GetErrorCorrection() const
EDA_ANGLE GetAngle() const
BARCODE_T GetKind() const
Returns the type of the barcode (QR, CODE_39, etc.).
int GetWidth() const
Get the barcode width (in internal units).
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition pcb_shape.h:80
int GetWidth() const override
VECTOR2I GetPosition() const override
Definition pcb_shape.h:78
EDA_ANGLE GetTextAngle() const override
CONST_ITERATOR CIterateWithHoles(int aOutline) const
Simple container to manage line stroke parameters.
@ ELLIPSE
Definition eda_shape.h:63
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:58
@ ELLIPSE_ARC
Definition eda_shape.h:64
static constexpr void hash_combine(std::size_t &seed)
This is a dummy function to take the final case of hash_combine below.
Definition hash.h:28
size_t hash_fp_item(const EDA_ITEM *aItem, int aFlags)
Calculate hash of an EDA_ITEM.
Definition hash_eda.cpp:54
static size_t hash_board_item(const BOARD_ITEM *aItem, int aFlags)
Definition hash_eda.cpp:43
Hashing functions for EDA_ITEMs.
@ HASH_POS
Definition hash_eda.h:43
@ HASH_VALUE
Definition hash_eda.h:51
@ HASH_LAYER
Definition hash_eda.h:48
@ HASH_REF
Definition hash_eda.h:50
@ RELATIVE_TO_SHAPE_POS
Use coordinates relative to the parent object (footprint).
Definition hash_eda.h:45
@ RELATIVE_TO_PARENT_COORDS
Definition hash_eda.h:44
@ HASH_ROT
Use coordinates relative to the shape position.
Definition hash_eda.h:47
@ HASH_NET
Definition hash_eda.h:49
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
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:79
#define UNIMPLEMENTED_FOR(type)
Definition macros.h:92
STL namespace.
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition padstack.h:104
@ PTH
Plated through hole pad.
Definition padstack.h:99
@ CHAMFERED_RECT
Definition padstack.h:61
@ ROUNDRECT
Definition padstack.h:58
@ TRAPEZOID
Definition padstack.h:57
BARCODE class definition.
const std::string table
VECTOR2I end
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:227
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:80
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:85
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:84
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:82
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:93
@ PCB_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
Definition typeinfo.h:87
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:78
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:79
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition typeinfo.h:86
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708