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  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <hash_eda.h>
26 
27 #include <class_module.h>
28 #include <class_text_mod.h>
29 #include <class_edge_mod.h>
30 #include <class_pad.h>
31 
32 #include <functional>
33 
34 using namespace std;
35 
36 // Common calculation part for all BOARD_ITEMs
37 static inline size_t hash_board_item( const BOARD_ITEM* aItem, int aFlags )
38 {
39  size_t ret = 0;
40 
41  if( aFlags & LAYER )
42  ret ^= hash<unsigned long long>{}( aItem->GetLayerSet().to_ullong() );
43 
44  return ret;
45 }
46 
47 
48 size_t hash_eda( const EDA_ITEM* aItem, int aFlags )
49 {
50  size_t ret = 0xa82de1c0;
51 
52  switch( aItem->Type() )
53  {
54  case PCB_MODULE_T:
55  {
56  const MODULE* module = static_cast<const MODULE*>( aItem );
57 
58  ret ^= hash_board_item( module, aFlags );
59 
60  if( aFlags & POSITION )
61  {
62  ret ^= hash<int>{}( module->GetPosition().x );
63  ret ^= hash<int>{}( module->GetPosition().y );
64  }
65 
66  if( aFlags & ROTATION )
67  ret ^= hash<double>{}( module->GetOrientation() );
68 
69  for( const BOARD_ITEM* i = module->GraphicalItemsList(); i; i = i->Next() )
70  ret ^= hash_eda( i, aFlags );
71 
72  for( const D_PAD* i = module->PadsList(); i; i = i->Next() )
73  ret ^= hash_eda( i, aFlags );
74  }
75  break;
76 
77  case PCB_PAD_T:
78  {
79  const D_PAD* pad = static_cast<const D_PAD*>( aItem );
80  ret ^= hash_board_item( pad, aFlags );
81  ret ^= hash<int>{}( pad->GetShape() << 16 );
82  ret ^= hash<int>{}( pad->GetDrillShape() << 18 );
83  ret ^= hash<int>{}( pad->GetSize().x << 8 );
84  ret ^= hash<int>{}( pad->GetSize().y << 9 );
85  ret ^= hash<int>{}( pad->GetOffset().x << 6 );
86  ret ^= hash<int>{}( pad->GetOffset().y << 7 );
87  ret ^= hash<int>{}( pad->GetDelta().x << 4 );
88  ret ^= hash<int>{}( pad->GetDelta().y << 5 );
89 
90  if( aFlags & POSITION )
91  {
92  if( aFlags & REL_COORD )
93  {
94  ret ^= hash<int>{}( pad->GetPos0().x );
95  ret ^= hash<int>{}( pad->GetPos0().y );
96  }
97  else
98  {
99  ret ^= hash<int>{}( pad->GetPosition().x );
100  ret ^= hash<int>{}( pad->GetPosition().y );
101  }
102  }
103 
104  if( aFlags & ROTATION )
105  ret ^= hash<double>{}( pad->GetOrientation() );
106 
107  if( aFlags & NET )
108  ret ^= hash<int>{}( pad->GetNetCode() << 6 );
109  }
110  break;
111 
112  case PCB_MODULE_TEXT_T:
113  {
114  const TEXTE_MODULE* text = static_cast<const TEXTE_MODULE*>( aItem );
115 
116  if( !( aFlags & REFERENCE ) && text->GetType() == TEXTE_MODULE::TEXT_is_REFERENCE )
117  break;
118 
119  if( !( aFlags & VALUE ) && text->GetType() == TEXTE_MODULE::TEXT_is_VALUE )
120  break;
121 
122  ret ^= hash_board_item( text, aFlags );
123  ret ^= hash<string>{}( text->GetText().ToStdString() );
124  ret ^= hash<bool>{}( text->IsItalic() );
125  ret ^= hash<bool>{}( text->IsBold() );
126  ret ^= hash<bool>{}( text->IsMirrored() );
127  ret ^= hash<int>{}( text->GetTextWidth() );
128  ret ^= hash<int>{}( text->GetTextHeight() );
129  ret ^= hash<int>{}( text->GetHorizJustify() );
130  ret ^= hash<int>{}( text->GetVertJustify() );
131 
132  if( aFlags & POSITION )
133  {
134  if( aFlags & REL_COORD )
135  {
136  ret ^= hash<int>{}( text->GetPos0().x );
137  ret ^= hash<int>{}( text->GetPos0().y );
138  }
139  else
140  {
141  ret ^= hash<int>{}( text->GetPosition().x );
142  ret ^= hash<int>{}( text->GetPosition().y );
143  }
144  }
145 
146  if( aFlags & ROTATION )
147  ret ^= hash<double>{}( text->GetTextAngle() );
148  }
149  break;
150 
151  case PCB_MODULE_EDGE_T:
152  {
153  const EDGE_MODULE* segment = static_cast<const EDGE_MODULE*>( aItem );
154  ret ^= hash_board_item( segment, aFlags );
155  ret ^= hash<int>{}( segment->GetType() );
156  ret ^= hash<int>{}( segment->GetShape() );
157  ret ^= hash<int>{}( segment->GetWidth() );
158  ret ^= hash<int>{}( segment->GetRadius() );
159 
160  if( aFlags & POSITION )
161  {
162  if( aFlags & REL_COORD )
163  {
164  ret ^= hash<int>{}( segment->GetStart0().x );
165  ret ^= hash<int>{}( segment->GetStart0().y );
166  ret ^= hash<int>{}( segment->GetEnd0().x );
167  ret ^= hash<int>{}( segment->GetEnd0().y );
168  }
169  else
170  {
171  ret ^= hash<int>{}( segment->GetStart().x );
172  ret ^= hash<int>{}( segment->GetStart().y );
173  ret ^= hash<int>{}( segment->GetEnd().x );
174  ret ^= hash<int>{}( segment->GetEnd().y );
175  }
176  }
177 
178  if( aFlags & ROTATION )
179  ret ^= hash<double>{}( segment->GetAngle() );
180  }
181  break;
182 
183  default:
184  wxASSERT_MSG( false, "Unhandled type in function hashModItem() (exporter_gencad.cpp)" );
185  }
186 
187  return ret;
188 }
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201
use coordinates relative to the parent object
Definition: hash_eda.h:40
bool IsItalic() const
Definition: eda_text.h:183
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
TEXT_TYPE GetType() const
const wxPoint & GetPos0() const
int GetTextWidth() const
Definition: eda_text.h:231
const wxPoint & GetEnd0() const
const wxPoint & GetPos0() const
Definition: class_pad.h:263
Template specialization to enable wxStrings for certain containers (e.g. unordered_map) ...
Definition: bitmap.cpp:55
PAD_DRILL_SHAPE_T GetDrillShape() const
Definition: class_pad.h:388
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
PAD_SHAPE_T GetShape() const
Function GetShape.
Definition: class_pad.h:216
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:203
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:94
Field Reference of part, i.e. "IC21".
double GetTextAngle() const
Definition: eda_text.h:177
bool IsBold() const
Definition: eda_text.h:186
class MODULE, a footprint
Definition: typeinfo.h:89
STROKE_T GetShape() const
Footprint text class description.
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
double GetOrientation() const
Definition: class_module.h:189
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:143
virtual LSET GetLayerSet() const
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
const wxSize & GetSize() const
Definition: class_pad.h:269
Definition: hash_eda.h:43
int GetType() const
static size_t hash_board_item(const BOARD_ITEM *aItem, int aFlags)
Definition: hash_eda.cpp:37
Pad object description.
int GetNetCode() const
Function GetNetCode.
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:204
double GetAngle() const
const wxPoint & GetStart0() const
Class LAYER holds information pertinent to a layer of a BOARD.
Definition: class_board.h:86
bool IsMirrored() const
Definition: eda_text.h:192
virtual const wxPoint GetPosition() const override
size_t i
Definition: json11.cpp:597
double GetOrientation() const
Function GetOrientation returns the rotation angle of the pad in tenths of degrees, but soon degrees.
Definition: class_pad.h:382
const wxSize & GetDelta() const
Definition: class_pad.h:272
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:154
int GetWidth() const
int GetTextHeight() const
Definition: eda_text.h:234
DLIST< BOARD_ITEM > & GraphicalItemsList()
Definition: class_module.h:166
DLIST< D_PAD > & PadsList()
Definition: class_module.h:163
Module description (excepted pads)
const wxPoint GetPosition() const override
Definition: class_pad.h:220
EDGE_MODULE class definition.
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
size_t hash_eda(const EDA_ITEM *aItem, int aFlags)
Definition: hash_eda.cpp:48
int GetRadius() const
Function GetRadius returns the radius of this item Has meaning only for arc and circle.
#define VALUE
const wxPoint GetPosition() const override
Definition: class_module.h:184
const wxPoint & GetOffset() const
Definition: class_pad.h:278