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 
158  std::map<int, NET_ITEMS_LIST> m_netMap;
160 };
161 
162 
163 template<class Visitor>
164 int INDEX::querySingle( int index, const SHAPE* aShape, int aMinDistance, Visitor& aVisitor )
165 {
166  if( !m_subIndices[index] )
167  return 0;
168 
169  return m_subIndices[index]->Query( aShape, aMinDistance, aVisitor, false );
170 }
171 
172 template<class Visitor>
173 int INDEX::Query( const ITEM* aItem, int aMinDistance, Visitor& aVisitor )
174 {
175  const SHAPE* shape = aItem->Shape();
176  int total = 0;
177 
178  total += querySingle( SI_Multilayer, shape, aMinDistance, aVisitor );
179 
180  const LAYER_RANGE layers = aItem->Layers();
181 
182  if( layers.IsMultilayer() )
183  {
184  total += querySingle( SI_PadsTop, shape, aMinDistance, aVisitor );
185  total += querySingle( SI_PadsBottom, shape, aMinDistance, aVisitor );
186 
187  for( int i = layers.Start(); i <= layers.End(); ++i )
188  total += querySingle( SI_Traces + 2 * i + SI_SegStraight, shape, aMinDistance, aVisitor );
189  }
190  else
191  {
192  int l = layers.Start();
193 
194  if( l == B_Cu )
195  total += querySingle( SI_PadsTop, shape, aMinDistance, aVisitor );
196  else if( l == F_Cu )
197  total += querySingle( SI_PadsBottom, shape, aMinDistance, aVisitor );
198 
199  total += querySingle( SI_Traces + 2 * l + SI_SegStraight, shape, aMinDistance, aVisitor );
200  }
201 
202  return total;
203 }
204 
205 template<class Visitor>
206 int INDEX::Query( const SHAPE* aShape, int aMinDistance, Visitor& aVisitor )
207 {
208  int total = 0;
209 
210  for( int i = 0; i < MaxSubIndices; i++ )
211  total += querySingle( i, aShape, aMinDistance, aVisitor );
212 
213  return total;
214 }
215 
216 };
217 
218 #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
bool IsMultilayer() const
Definition: pns_layerset.h:78
int Start() const
Definition: pns_layerset.h:83
int End() const
Definition: pns_layerset.h:88
SHAPE_INDEX< ITEM * > ITEM_SHAPE_INDEX
Definition: pns_index.h:50
virtual const SHAPE * Shape() const
Function Shape()
Definition: pns_item.h:303
void Clear()
Function Clear()
Definition: pns_index.cpp:124
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
ITEM_SET m_allItems
Definition: pns_index.h:159
static const int SI_PadsBottom
Definition: pns_index.h:150
NET_ITEMS_LIST * GetItemsForNet(int aNet)
Function GetItemsForNet()
Definition: pns_index.cpp:138
int Query(const ITEM *aItem, int aMinDistance, Visitor &aVisitor)
Function Query()
Definition: pns_index.h:173
bool Contains(ITEM *aItem) const
Function Contains()
Definition: pns_index.h:128
void Replace(ITEM *aOldItem, ITEM *aNewItem)
Function Add()
Definition: pns_index.cpp:117
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
int Size() const
Function Size()
Definition: pns_index.h:138
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.cpp:85
int Query(const SHAPE *aShape, int aMinDistance, V &aVisitor, bool aExact)
Function Query()
Definition: shape_index.h:270
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.cpp:38
int querySingle(int index, const SHAPE *aShape, int aMinDistance, Visitor &aVisitor)
Definition: pns_index.h:164
Push and Shove diff pair dimensions (gap) settings dialog.
void Remove(ITEM *aItem)
Function Remove()
Definition: pns_index.cpp:102
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
const LAYER_RANGE & Layers() const
Function Layers()
Definition: pns_item.h:214