Horizon
pns_index.h
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
36namespace PNS {
37
38
46class INDEX
47{
48public:
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
143private:
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
162
163template<class Visitor>
164int 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
172template<class Visitor>
173int 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
205template<class Visitor>
206int 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 LAYER_RANGE.
Definition: pns_layerset.h:33
Class INDEX.
Definition: pns_index.h:47
void Replace(ITEM *aOldItem, ITEM *aNewItem)
Function Add()
Definition: pns_index.cpp:117
int Query(const ITEM *aItem, int aMinDistance, Visitor &aVisitor)
Function Query()
Definition: pns_index.h:173
void Remove(ITEM *aItem)
Function Remove()
Definition: pns_index.cpp:102
bool Contains(ITEM *aItem) const
Function Contains()
Definition: pns_index.h:128
int Size() const
Function Size()
Definition: pns_index.h:138
void Add(ITEM *aItem)
Function Add()
Definition: pns_index.cpp:85
void Clear()
Function Clear()
Definition: pns_index.cpp:124
NET_ITEMS_LIST * GetItemsForNet(int aNet)
Function GetItemsForNet()
Definition: pns_index.cpp:138
Class ITEM.
Definition: pns_item.h:55
virtual const SHAPE * Shape() const
Function Shape()
Definition: pns_item.h:304
const LAYER_RANGE & Layers() const
Function Layers()
Definition: pns_item.h:215
Definition: shape_index.h:108
int Query(const SHAPE *aShape, int aMinDistance, V &aVisitor, bool aExact)
Function Query()
Definition: shape_index.h:270
Class SHAPE.
Definition: shape.h:59
Board layer functions and definitions.