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-2020 KiCad Developers, see AUTHORS.txt for contributors.
6 * Author: Tomasz Wlostowski <[email protected]>
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
25#include <deque>
26#include <list>
27#include <map>
28#include <unordered_set>
29
30#include <layer_ids.h>
32
33#include "pns_item.h"
34
35namespace PNS {
36
37
45class INDEX
46{
47public:
48 typedef std::list<ITEM*> NET_ITEMS_LIST;
50 typedef std::unordered_set<ITEM*> ITEM_SET;
51
52 INDEX(){};
53
57 void Add( ITEM* aItem );
58
62 void Remove( ITEM* aItem );
63
67 void Replace( ITEM* aOldItem, ITEM* aNewItem );
68
80 template<class Visitor>
81 int Query( const ITEM* aItem, int aMinDistance, Visitor& aVisitor ) const;
82
94 template<class Visitor>
95 int Query( const SHAPE* aShape, int aMinDistance, Visitor& aVisitor ) const;
96
100 NET_ITEMS_LIST* GetItemsForNet( int aNet );
101
107 bool Contains( ITEM* aItem ) const
108 {
109 return m_allItems.find( aItem ) != m_allItems.end();
110 }
111
115 int Size() const { return m_allItems.size(); }
116
117 ITEM_SET::iterator begin() { return m_allItems.begin(); }
118 ITEM_SET::iterator end() { return m_allItems.end(); }
119
120private:
121 template <class Visitor>
122 int querySingle( std::size_t aIndex, const SHAPE* aShape, int aMinDistance, Visitor& aVisitor ) const;
123
124private:
125 std::deque<ITEM_SHAPE_INDEX> m_subIndices;
126 std::map<int, NET_ITEMS_LIST> m_netMap;
128};
129
130
131template<class Visitor>
132int INDEX::querySingle( std::size_t aIndex, const SHAPE* aShape, int aMinDistance, Visitor& aVisitor ) const
133{
134 if( aIndex >= m_subIndices.size() )
135 return 0;
136
137 return m_subIndices[aIndex].Query( aShape, aMinDistance, aVisitor);
138}
139
140template<class Visitor>
141int INDEX::Query( const ITEM* aItem, int aMinDistance, Visitor& aVisitor ) const
142{
143 int total = 0;
144
145 const LAYER_RANGE& layers = aItem->Layers();
146
147 for( int i = layers.Start(); i <= layers.End(); ++i )
148 total += querySingle( i, aItem->Shape(), aMinDistance, aVisitor );
149
150 return total;
151}
152
153template<class Visitor>
154int INDEX::Query( const SHAPE* aShape, int aMinDistance, Visitor& aVisitor ) const
155{
156 int total = 0;
157
158 for( std::size_t i = 0; i < m_subIndices.size(); ++i )
159 total += querySingle( i, aShape, aMinDistance, aVisitor );
160
161 return total;
162}
163
164};
165
166#endif
Represent a contiguous set of PCB layers.
Definition: pns_layerset.h:32
int Start() const
Definition: pns_layerset.h:82
int End() const
Definition: pns_layerset.h:87
INDEX.
Definition: pns_index.h:46
std::map< int, NET_ITEMS_LIST > m_netMap
Definition: pns_index.h:126
void Replace(ITEM *aOldItem, ITEM *aNewItem)
Replaces one item with another.
Definition: pns_index.cpp:64
int querySingle(std::size_t aIndex, const SHAPE *aShape, int aMinDistance, Visitor &aVisitor) const
Definition: pns_index.h:132
std::unordered_set< ITEM * > ITEM_SET
Definition: pns_index.h:50
ITEM_SET m_allItems
Definition: pns_index.h:127
void Remove(ITEM *aItem)
Removes an item from the spatial index.
Definition: pns_index.cpp:46
bool Contains(ITEM *aItem) const
Function Contains()
Definition: pns_index.h:107
std::list< ITEM * > NET_ITEMS_LIST
Definition: pns_index.h:48
SHAPE_INDEX< ITEM * > ITEM_SHAPE_INDEX
Definition: pns_index.h:49
int Size() const
Returns number of items stored in the index.
Definition: pns_index.h:115
ITEM_SET::iterator end()
Definition: pns_index.h:118
ITEM_SET::iterator begin()
Definition: pns_index.h:117
void Add(ITEM *aItem)
Adds item to the spatial index.
Definition: pns_index.cpp:28
int Query(const ITEM *aItem, int aMinDistance, Visitor &aVisitor) const
Searches items in the index that are in proximity of aItem.
Definition: pns_index.h:141
std::deque< ITEM_SHAPE_INDEX > m_subIndices
Definition: pns_index.h:125
NET_ITEMS_LIST * GetItemsForNet(int aNet)
Returns list of all items in a given net.
Definition: pns_index.cpp:71
Base class for PNS router board items.
Definition: pns_item.h:56
virtual const SHAPE * Shape() const
Return the geometrical shape of the item.
Definition: pns_item.h:200
const LAYER_RANGE & Layers() const
Definition: pns_item.h:154
An abstract shape on 2D plane.
Definition: shape.h:123
Push and Shove diff pair dimensions (gap) settings dialog.