KiCad PCB EDA Suite
pns_index.h
Go to the documentation of this file.
1 /*
2  * KiRouter - a push-and-(sometimes-)shove PCB router
3  *
4  * Copyright (C) 2013-2014 CERN
5  * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
6  * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef __PNS_INDEX_H
23 #define __PNS_INDEX_H
24 
26 #include <map>
27 #include <unordered_set>
28 
29 #include <boost/range/adaptor/map.hpp>
30 
31 #include <list>
32 #include <geometry/shape_index.h>
33 
34 #include "pns_item.h"
35 
36 namespace PNS {
37 
38 
46 class INDEX
47 {
48 public:
49  typedef std::list<ITEM*> NET_ITEMS_LIST;
51  typedef std::unordered_set<ITEM*> ITEM_SET;
52 
53  INDEX();
54  ~INDEX();
55 
61  void Add( ITEM* aItem );
62 
68  void Remove( ITEM* aItem );
69 
75  void Replace( ITEM* aOldItem, ITEM* aNewItem );
76 
90  template<class Visitor>
91  int Query( const ITEM* aItem, int aMinDistance, Visitor& aVisitor );
92 
106  template<class Visitor>
107  int Query( const SHAPE* aShape, int aMinDistance, Visitor& aVisitor );
108 
114  void Clear();
115 
121  NET_ITEMS_LIST* GetItemsForNet( int aNet );
122 
128  bool Contains( ITEM* aItem ) const
129  {
130  return m_allItems.find( aItem ) != m_allItems.end();
131  }
132 
138  int Size() const { return m_allItems.size(); }
139 
140  ITEM_SET::iterator begin() { return m_allItems.begin(); }
141  ITEM_SET::iterator end() { return m_allItems.end(); }
142 
143 private:
144  static const int MaxSubIndices = 128;
145  static const int SI_Multilayer = 2;
146  static const int SI_SegDiagonal = 0;
147  static const int SI_SegStraight = 1;
148  static const int SI_Traces = 3;
149  static const int SI_PadsTop = 0;
150  static const int SI_PadsBottom = 1;
151 
152  template <class Visitor>
153  int querySingle( int index, const SHAPE* aShape, int aMinDistance, Visitor& aVisitor );
154 
155  ITEM_SHAPE_INDEX* getSubindex( const ITEM* aItem );
156 
157  ITEM_SHAPE_INDEX* m_subIndices[MaxSubIndices];
158  std::map<int, NET_ITEMS_LIST> m_netMap;
159  ITEM_SET m_allItems;
160 };
161 
163 {
164  memset( m_subIndices, 0, sizeof( m_subIndices ) );
165 }
166 
168 {
169  int idx_n = -1;
170 
171  const LAYER_RANGE l = aItem->Layers();
172 
173  switch( aItem->Kind() )
174  {
175  case ITEM::VIA_T:
176  idx_n = SI_Multilayer;
177  break;
178 
179  case ITEM::SOLID_T:
180  {
181  if( l.IsMultilayer() )
182  idx_n = SI_Multilayer;
183  else if( l.Start() == B_Cu ) // fixme: use kicad layer codes
184  idx_n = SI_PadsTop;
185  else if( l.Start() == F_Cu )
186  idx_n = SI_PadsBottom;
187  else
188  idx_n = SI_Traces + 2 * l.Start() + SI_SegStraight;
189  }
190  break;
191 
192  case ITEM::SEGMENT_T:
193  case ITEM::LINE_T:
194  idx_n = SI_Traces + 2 * l.Start() + SI_SegStraight;
195  break;
196 
197  default:
198  break;
199  }
200 
201  if( idx_n < 0 || idx_n >= MaxSubIndices )
202  {
203  wxASSERT( idx_n >= 0 );
204  wxASSERT( idx_n < MaxSubIndices );
205  return nullptr;
206  }
207 
208  if( !m_subIndices[idx_n] )
209  m_subIndices[idx_n] = new ITEM_SHAPE_INDEX;
210 
211  return m_subIndices[idx_n];
212 }
213 
214 void INDEX::Add( ITEM* aItem )
215 {
216  ITEM_SHAPE_INDEX* idx = getSubindex( aItem );
217 
218  if( !idx )
219  return;
220 
221  idx->Add( aItem );
222  m_allItems.insert( aItem );
223  int net = aItem->Net();
224 
225  if( net >= 0 )
226  {
227  m_netMap[net].push_back( aItem );
228  }
229 }
230 
231 void INDEX::Remove( ITEM* aItem )
232 {
233  ITEM_SHAPE_INDEX* idx = getSubindex( aItem );
234 
235  if( !idx )
236  return;
237 
238  idx->Remove( aItem );
239  m_allItems.erase( aItem );
240  int net = aItem->Net();
241 
242  if( net >= 0 && m_netMap.find( net ) != m_netMap.end() )
243  m_netMap[net].remove( aItem );
244 }
245 
246 void INDEX::Replace( ITEM* aOldItem, ITEM* aNewItem )
247 {
248  Remove( aOldItem );
249  Add( aNewItem );
250 }
251 
252 template<class Visitor>
253 int INDEX::querySingle( int index, const SHAPE* aShape, int aMinDistance, Visitor& aVisitor )
254 {
255  if( !m_subIndices[index] )
256  return 0;
257 
258  return m_subIndices[index]->Query( aShape, aMinDistance, aVisitor, false );
259 }
260 
261 template<class Visitor>
262 int INDEX::Query( const ITEM* aItem, int aMinDistance, Visitor& aVisitor )
263 {
264  const SHAPE* shape = aItem->Shape();
265  int total = 0;
266 
267  total += querySingle( SI_Multilayer, shape, aMinDistance, aVisitor );
268 
269  const LAYER_RANGE layers = aItem->Layers();
270 
271  if( layers.IsMultilayer() )
272  {
273  total += querySingle( SI_PadsTop, shape, aMinDistance, aVisitor );
274  total += querySingle( SI_PadsBottom, shape, aMinDistance, aVisitor );
275 
276  for( int i = layers.Start(); i <= layers.End(); ++i )
277  total += querySingle( SI_Traces + 2 * i + SI_SegStraight, shape, aMinDistance, aVisitor );
278  }
279  else
280  {
281  int l = layers.Start();
282 
283  if( l == B_Cu )
284  total += querySingle( SI_PadsTop, shape, aMinDistance, aVisitor );
285  else if( l == F_Cu )
286  total += querySingle( SI_PadsBottom, shape, aMinDistance, aVisitor );
287 
288  total += querySingle( SI_Traces + 2 * l + SI_SegStraight, shape, aMinDistance, aVisitor );
289  }
290 
291  return total;
292 }
293 
294 template<class Visitor>
295 int INDEX::Query( const SHAPE* aShape, int aMinDistance, Visitor& aVisitor )
296 {
297  int total = 0;
298 
299  for( int i = 0; i < MaxSubIndices; i++ )
300  total += querySingle( i, aShape, aMinDistance, aVisitor );
301 
302  return total;
303 }
304 
306 {
307  for( int i = 0; i < MaxSubIndices; ++i )
308  {
310 
311  if( idx )
312  delete idx;
313 
314  m_subIndices[i] = NULL;
315  }
316 }
317 
319 {
320  Clear();
321 }
322 
324 {
325  if( m_netMap.find( aNet ) == m_netMap.end() )
326  return NULL;
327 
328  return &m_netMap[aNet];
329 }
330 
331 }
332 
333 #endif
Class ITEM.
Definition: pns_item.h:53
std::list< ITEM * > NET_ITEMS_LIST
Definition: pns_index.h:49
static const int SI_Traces
Definition: pns_index.h:148
const LAYER_RANGE & Layers() const
Function Layers()
Definition: pns_item.h:209
bool Contains(ITEM *aItem) const
Function Contains()
Definition: pns_index.h:128
virtual const SHAPE * Shape() const
Function Shape()
Definition: pns_item.h:298
int Size() const
Function Size()
Definition: pns_index.h:138
void Add(T aShape)
Function Add()
Definition: shape_index.h:310
SHAPE_INDEX< ITEM * > ITEM_SHAPE_INDEX
Definition: pns_index.h:50
int End() const
Definition: pns_layerset.h:88
void Remove(T aShape)
Function Remove()
Definition: shape_index.h:320
void Clear()
Function Clear()
Definition: pns_index.h:305
static const int SI_SegStraight
Definition: pns_index.h:147
ITEM_SHAPE_INDEX * m_subIndices[MaxSubIndices]
Definition: pns_index.h:157
Class SHAPE.
Definition: shape.h:58
int Start() const
Definition: pns_layerset.h:83
ITEM_SET m_allItems
Definition: pns_index.h:159
static const int SI_PadsBottom
Definition: pns_index.h:150
PnsKind Kind() const
Function Kind()
Definition: pns_item.h:122
NET_ITEMS_LIST * GetItemsForNet(int aNet)
Function GetItemsForNet()
Definition: pns_index.h:323
int Query(const ITEM *aItem, int aMinDistance, Visitor &aVisitor)
Function Query()
Definition: pns_index.h:262
void Replace(ITEM *aOldItem, ITEM *aNewItem)
Function Add()
Definition: pns_index.h:246
ITEM_SET::iterator end()
Definition: pns_index.h:141
ITEM_SET::iterator begin()
Definition: pns_index.h:140
Board layer functions and definitions.
std::map< int, NET_ITEMS_LIST > m_netMap
Definition: pns_index.h:158
static const int SI_Multilayer
Definition: pns_index.h:145
size_t i
Definition: json11.cpp:597
static const int SI_PadsTop
Definition: pns_index.h:149
void Add(ITEM *aItem)
Function Add()
Definition: pns_index.h:214
int Query(const SHAPE *aShape, int aMinDistance, V &aVisitor, bool aExact)
Function Query()
Definition: shape_index.h:270
int Net() const
Function Net()
Definition: pns_item.h:179
bool IsMultilayer() const
Definition: pns_layerset.h:78
std::unordered_set< ITEM * > ITEM_SET
Definition: pns_index.h:51
static const int MaxSubIndices
Definition: pns_index.h:144
ITEM_SHAPE_INDEX * getSubindex(const ITEM *aItem)
Definition: pns_index.h:167
int querySingle(int index, const SHAPE *aShape, int aMinDistance, Visitor &aVisitor)
Definition: pns_index.h:253
Push and Shove diff pair dimensions (gap) settings dialog.
void Remove(ITEM *aItem)
Function Remove()
Definition: pns_index.h:231
static const int SI_SegDiagonal
Definition: pns_index.h:146
Class INDEX.
Definition: pns_index.h:46
Class LAYER_RANGE.
Definition: pns_layerset.h:32