KiCad PCB EDA Suite
Loading...
Searching...
No Matches
connectivity_rtree.h
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) 2018 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#ifndef PCBNEW_CONNECTIVITY_RTREE_H_
25#define PCBNEW_CONNECTIVITY_RTREE_H_
26
27#include <math/box2.h>
28#include <router/pns_layerset.h>
29
30#include <geometry/rtree.h>
31
32
38template< class T >
40{
41public:
42
44 {
45 this->m_tree = new RTree<T, int, 3, double>();
46 }
47
49 {
50 delete this->m_tree;
51 }
52
57 void Insert( T aItem )
58 {
59 const BOX2I& bbox = aItem->BBox();
60
61 const int mmin[3] = { aItem->StartLayer(), bbox.GetX(), bbox.GetY() };
62 const int mmax[3] = { aItem->EndLayer(), bbox.GetRight(), bbox.GetBottom() };
63
64 m_tree->Insert( mmin, mmax, aItem );
65 }
66
72 void Remove( T aItem )
73 {
74
75 // First, attempt to remove the item using its given BBox
76 const BOX2I& bbox = aItem->BBox();
77
78 const int mmin[3] = { aItem->StartLayer(), bbox.GetX(), bbox.GetY() };
79 const int mmax[3] = { aItem->EndLayer(), bbox.GetRight(), bbox.GetBottom() };
80
81 // If we are not successful ( 1 == not found ), then we expand
82 // the search to the full tree
83 if( m_tree->Remove( mmin, mmax, aItem ) )
84 {
85 // N.B. We must search the whole tree for the pointer to remove
86 // because the item may have been moved before we have the chance to
87 // delete it from the tree
88 const int mmin2[3] = { INT_MIN, INT_MIN, INT_MIN };
89 const int mmax2[3] = { INT_MAX, INT_MAX, INT_MAX };
90 m_tree->Remove( mmin2, mmax2, aItem );
91 }
92 }
93
98 void RemoveAll( )
99 {
100 m_tree->RemoveAll();
101 }
102
108 template <class Visitor>
109 void Query( const BOX2I& aBounds, int aStartLayer, int aEndLayer, Visitor& aVisitor ) const
110 {
111 int start_layer = aStartLayer == B_Cu ? INT_MAX : aStartLayer;
112 int end_layer = aEndLayer == B_Cu ? INT_MAX : aEndLayer;
113
114 const int mmin[3] = { start_layer, aBounds.GetX(), aBounds.GetY() };
115 const int mmax[3] = { end_layer, aBounds.GetRight(), aBounds.GetBottom() };
116
117 m_tree->Search( mmin, mmax, aVisitor );
118 }
119
120private:
121
122 RTree<T, int, 3, double>* m_tree;
123};
124
125
126#endif /* PCBNEW_CONNECTIVITY_RTREE_H_ */
constexpr coord_type GetY() const
Definition: box2.h:208
constexpr coord_type GetX() const
Definition: box2.h:207
constexpr coord_type GetRight() const
Definition: box2.h:217
constexpr coord_type GetBottom() const
Definition: box2.h:222
CN_RTREE - Implements an R-tree for fast spatial indexing of connectivity items.
void Insert(T aItem)
Function Insert() Inserts an item into the tree.
void RemoveAll()
Function RemoveAll() Removes all items from the RTree.
void Remove(T aItem)
Function Remove() Removes an item from the tree.
RTree< T, int, 3, double > * m_tree
void Query(const BOX2I &aBounds, int aStartLayer, int aEndLayer, Visitor &aVisitor) const
Function Query() Executes a function object aVisitor for each item whose bounding box intersects with...
@ B_Cu
Definition: layer_ids.h:65