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-2021 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 
28 #include <footprint.h>
29 #include <fp_text.h>
30 #include <fp_shape.h>
31 #include <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_fp_item( const EDA_ITEM* aItem, int aFlags )
50 {
51  size_t ret = 0;
52 
53  switch( aItem->Type() )
54  {
55  case PCB_FOOTPRINT_T:
56  {
57  const FOOTPRINT* footprint = static_cast<const FOOTPRINT*>( aItem );
58 
59  ret = hash_board_item( footprint, aFlags );
60 
61  if( aFlags & HASH_POS )
62  hash_combine( ret, footprint->GetPosition().x, footprint->GetPosition().y );
63 
64  if( aFlags & HASH_ROT )
65  hash_combine( ret, footprint->GetOrientation() );
66 
67  for( BOARD_ITEM* item : footprint->GraphicalItems() )
68  hash_combine( ret, hash_fp_item( item, aFlags ) );
69 
70  for( PAD* pad : footprint->Pads() )
71  hash_combine( ret, hash_fp_item( static_cast<EDA_ITEM*>( pad ), aFlags ) );
72  }
73  break;
74 
75  case PCB_PAD_T:
76  {
77  const PAD* pad = static_cast<const PAD*>( aItem );
78 
79  ret = hash<int>{}( static_cast<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_FP_TEXT_T:
107  {
108  const FP_TEXT* text = static_cast<const FP_TEXT*>( aItem );
109 
110  if( !( aFlags & HASH_REF ) && text->GetType() == FP_TEXT::TEXT_is_REFERENCE )
111  break;
112 
113  if( !( aFlags & HASH_VALUE ) && text->GetType() == FP_TEXT::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_FP_SHAPE_T:
140  {
141  const FP_SHAPE* shape = static_cast<const FP_SHAPE*>( aItem );
142  ret = hash_board_item( shape, aFlags );
143  hash_combine( ret, shape->GetShape() );
144  hash_combine( ret, shape->GetWidth() );
145  hash_combine( ret, shape->IsFilled() );
146 
147  if( shape->GetShape() == SHAPE_T::ARC || shape->GetShape() == SHAPE_T::CIRCLE )
148  hash_combine( ret, shape->GetRadius() );
149 
150  if( aFlags & HASH_POS )
151  {
152  if( aFlags & REL_COORD )
153  {
154  hash_combine( ret, shape->GetStart0().x );
155  hash_combine( ret, shape->GetStart0().y );
156  hash_combine( ret, shape->GetEnd0().x );
157  hash_combine( ret, shape->GetEnd0().y );
158 
159  if( shape->GetShape() == SHAPE_T::ARC )
160  {
161  hash_combine( ret, shape->GetCenter0().x );
162  hash_combine( ret, shape->GetCenter0().y );
163  hash_combine( ret, shape->GetArcAngle() );
164  }
165  }
166  else
167  {
168  hash_combine( ret, shape->GetStart().x );
169  hash_combine( ret, shape->GetStart().y );
170  hash_combine( ret, shape->GetEnd().x );
171  hash_combine( ret, shape->GetEnd().y );
172 
173  if( shape->GetShape() == SHAPE_T::ARC )
174  {
175  hash_combine( ret, shape->GetCenter().x );
176  hash_combine( ret, shape->GetCenter().y );
177  hash_combine( ret, shape->GetArcAngle() );
178  }
179  }
180  }
181  }
182  break;
183 
184  default:
185  wxASSERT_MSG( false, "Unhandled type in function hash_fp_item() (exporter_gencad.cpp)" );
186  }
187 
188  return ret;
189 }
int GetWidth() const
Definition: eda_shape.h:89
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
const wxPoint & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:97
use coordinates relative to the parent object
Definition: hash_eda.h:43
double GetOrientation() const
Definition: footprint.h:191
double GetArcAngle() const
Definition: eda_shape.cpp:514
const wxPoint & GetStart0() const
Definition: fp_shape.h:112
Definition: bitmap.cpp:64
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:93
class PAD, a pad in a footprint
Definition: typeinfo.h:89
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:67
PADS & Pads()
Definition: footprint.h:169
bool IsFilled() const
Definition: eda_shape.h:81
wxPoint GetCenter0() const
Definition: fp_shape.cpp:145
const wxPoint & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:122
wxPoint GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_shape.h:79
size_t hash_fp_item(const EDA_ITEM *aItem, int aFlags)
Calculate hash of an EDA_ITEM.
Definition: hash_eda.cpp:49
DRAWINGS & GraphicalItems()
Definition: footprint.h:172
static size_t hash_board_item(const BOARD_ITEM *aItem, int aFlags)
Definition: hash_eda.cpp:38
class FOOTPRINT, a footprint
Definition: typeinfo.h:88
const wxPoint & GetEnd0() const
Definition: fp_shape.h:115
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:99
wxPoint GetPosition() const override
Definition: footprint.h:187
SHAPE_T GetShape() const
Definition: eda_shape.h:92
int GetRadius() const
Definition: eda_shape.cpp:466
Definition: pad.h:57
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:147
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:112