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 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
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 
28 #include <class_module.h>
29 #include <class_text_mod.h>
30 #include <class_edge_mod.h>
31 #include <class_pad.h>
32 
33 #include <functional>
34 
35 using namespace std;
36 
37 // Common calculation part for all BOARD_ITEMs
38 static inline size_t hash_board_item( const BOARD_ITEM* aItem, int aFlags )
39 {
40  size_t ret = 0;
41 
42  if( aFlags & HASH_LAYER )
43  ret = hash<unsigned long long>{}( aItem->GetLayerSet().to_ullong() );
44 
45  return ret;
46 }
47 
48 
49 size_t hash_eda( const EDA_ITEM* aItem, int aFlags )
50 {
51  size_t ret = 0;
52 
53  switch( aItem->Type() )
54  {
55  case PCB_MODULE_T:
56  {
57  const MODULE* module = static_cast<const MODULE*>( aItem );
58 
59  ret = hash_board_item( module, aFlags );
60 
61  if( aFlags & HASH_POS )
62  hash_combine( ret, module->GetPosition().x, module->GetPosition().y );
63 
64  if( aFlags & HASH_ROT )
65  hash_combine( ret, module->GetOrientation() );
66 
67  for( auto i : module->GraphicalItems() )
68  hash_combine( ret, hash_eda( i, aFlags ) );
69 
70  for( auto i : module->Pads() )
71  hash_combine( ret, hash_eda( static_cast<EDA_ITEM*>( i ), aFlags ) );
72  }
73  break;
74 
75  case PCB_PAD_T:
76  {
77  const D_PAD* pad = static_cast<const D_PAD*>( aItem );
78 
79  ret = hash<int>{}( pad->GetShape() << 16 );
80  hash_combine( ret, pad->GetDrillShape() << 18 );
81  hash_combine( ret, pad->GetSize().x << 8 );
82  hash_combine( ret, pad->GetSize().y << 9 );
83  hash_combine( ret, pad->GetOffset().x << 6 );
84  hash_combine( ret, pad->GetOffset().y << 7 );
85  hash_combine( ret, pad->GetDelta().x << 4 );
86  hash_combine( ret, pad->GetDelta().y << 5 );
87 
88  hash_combine( ret, hash_board_item( pad, aFlags ) );
89 
90  if( aFlags & HASH_POS )
91  {
92  if( aFlags & REL_COORD )
93  hash_combine( ret, pad->GetPos0().x, pad->GetPos0().y );
94  else
95  hash_combine( ret, pad->GetPosition().x, pad->GetPosition().y );
96  }
97 
98  if( aFlags & HASH_ROT )
99  hash_combine( ret, pad->GetOrientation() );
100 
101  if( aFlags & HASH_NET )
102  hash_combine( ret, pad->GetNetCode() );
103  }
104  break;
105 
106  case PCB_MODULE_TEXT_T:
107  {
108  const TEXTE_MODULE* text = static_cast<const TEXTE_MODULE*>( aItem );
109 
110  if( !( aFlags & HASH_REF ) && text->GetType() == TEXTE_MODULE::TEXT_is_REFERENCE )
111  break;
112 
113  if( !( aFlags & HASH_VALUE ) && text->GetType() == TEXTE_MODULE::TEXT_is_VALUE )
114  break;
115 
116  ret = hash_board_item( text, aFlags );
117  hash_combine( ret, text->GetText().ToStdString() );
118  hash_combine( ret, text->IsItalic() );
119  hash_combine( ret, text->IsBold() );
120  hash_combine( ret, text->IsMirrored() );
121  hash_combine( ret, text->GetTextWidth() );
122  hash_combine( ret, text->GetTextHeight() );
123  hash_combine( ret, text->GetHorizJustify() );
124  hash_combine( ret, text->GetVertJustify() );
125 
126  if( aFlags & HASH_POS )
127  {
128  if( aFlags & REL_COORD )
129  hash_combine( ret, text->GetPos0().x, text->GetPos0().y );
130  else
131  hash_combine( ret, text->GetPosition().x, text->GetPosition().y );
132  }
133 
134  if( aFlags & HASH_ROT )
135  hash_combine( ret, text->GetTextAngle() );
136  }
137  break;
138 
139  case PCB_MODULE_EDGE_T:
140  {
141  const EDGE_MODULE* segment = static_cast<const EDGE_MODULE*>( aItem );
142  ret = hash_board_item( segment, aFlags );
143  hash_combine( ret, segment->GetType() );
144  hash_combine( ret, segment->GetShape() );
145  hash_combine( ret, segment->GetWidth() );
146  hash_combine( ret, segment->GetRadius() );
147 
148  if( aFlags & HASH_POS )
149  {
150  if( aFlags & REL_COORD )
151  {
152  hash_combine( ret, segment->GetStart0().x );
153  hash_combine( ret, segment->GetStart0().y );
154  hash_combine( ret, segment->GetEnd0().x );
155  hash_combine( ret, segment->GetEnd0().y );
156  }
157  else
158  {
159  hash_combine( ret, segment->GetStart().x );
160  hash_combine( ret, segment->GetStart().y );
161  hash_combine( ret, segment->GetEnd().x );
162  hash_combine( ret, segment->GetEnd().y );
163  }
164  }
165 
166  if( aFlags & HASH_ROT )
167  hash_combine( ret, segment->GetAngle() );
168  }
169  break;
170 
171  default:
172  wxASSERT_MSG( false, "Unhandled type in function hashModItem() (exporter_gencad.cpp)" );
173  }
174 
175  return ret;
176 }
bool IsBold() const
Definition: eda_text.h:183
double GetOrientation() const
Definition: class_module.h:217
int GetNetCode() const
Function GetNetCode.
const wxPoint & GetPos0() const
Definition: class_pad.h:218
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:200
use coordinates relative to the parent object
Definition: hash_eda.h:44
bool IsMirrored() const
Definition: eda_text.h:189
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
wxPoint GetPosition() const override
Definition: class_pad.h:165
STROKE_T GetShape() const
PADS & Pads()
Definition: class_module.h:174
int GetType() const
double GetTextAngle() const
Definition: eda_text.h:174
Template specialization to enable wxStrings for certain containers (e.g. unordered_map)
Definition: bitmap.cpp:56
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
DRAWINGS & GraphicalItems()
Definition: class_module.h:184
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:94
static void hash_combine(std::size_t &seed)
This is a dummy function to take the final case of hash_combine below.
Definition: hash_eda.h:64
int GetTextHeight() const
Definition: eda_text.h:245
bool IsItalic() const
Definition: eda_text.h:180
const wxPoint & GetStart0() const
class MODULE, a footprint
Definition: typeinfo.h:89
Footprint text class description.
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:199
const wxPoint & GetOffset() const
Definition: class_pad.h:233
PAD_DRILL_SHAPE_T GetDrillShape() const
Definition: class_pad.h:326
TEXT_TYPE GetType() const
int GetRadius() const
Function GetRadius returns the radius of this item Has meaning only for arc and circle.
const wxSize & GetDelta() const
Definition: class_pad.h:227
static size_t hash_board_item(const BOARD_ITEM *aItem, int aFlags)
Definition: hash_eda.cpp:38
int GetWidth() const
Pad object description.
const wxPoint & GetPos0() const
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
double GetAngle() const
int GetTextWidth() const
Definition: eda_text.h:242
double GetOrientation() const
Function GetOrientation returns the rotation angle of the pad in a variety of units (the basic call r...
Definition: class_pad.h:321
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
const wxPoint & GetEnd0() const
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
wxPoint GetPosition() const override
Definition: class_module.h:212
virtual wxPoint GetPosition() const override
PAD_SHAPE_T GetShape() const
Definition: class_pad.h:157
const wxSize & GetSize() const
Definition: class_pad.h:224
EDGE_MODULE class definition.
size_t hash_eda(const EDA_ITEM *aItem, int aFlags)
Calculates hash of an EDA_ITEM.
Definition: hash_eda.cpp:49
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:127
virtual LSET GetLayerSet() const
Function GetLayerSet returns a std::bitset of all layers on which the item physically resides.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193