KiCad PCB EDA Suite
Loading...
Searching...
No Matches
view_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) 2013 CERN
5 * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#ifndef __VIEW_RTREE_H
28#define __VIEW_RTREE_H
29
30#include <math/box2.h>
31
32#include <geometry/rtree.h>
33
34namespace KIGFX
35{
36typedef RTree<VIEW_ITEM*, int, 2, double> VIEW_RTREE_BASE;
37
42{
43public:
49 void Insert( VIEW_ITEM* aItem, const BOX2I& bbox )
50 {
51 const int mmin[2] = { std::min( bbox.GetX(), bbox.GetRight() ), std::min( bbox.GetY(), bbox.GetBottom() ) };
52 const int mmax[2] = { std::max( bbox.GetX(), bbox.GetRight() ), std::max( bbox.GetY(), bbox.GetBottom() ) };
53
54 VIEW_RTREE_BASE::Insert( mmin, mmax, aItem );
55 }
56
62 void Remove( VIEW_ITEM* aItem, const BOX2I* aBbox )
63 {
64 // const BOX2I& bbox = aItem->ViewBBox();
65
66 if( aBbox )
67 {
68 const int mmin[2] = { std::min( aBbox->GetX(), aBbox->GetRight() ), std::min( aBbox->GetY(), aBbox->GetBottom() ) };
69 const int mmax[2] = { std::max( aBbox->GetX(), aBbox->GetRight() ), std::max( aBbox->GetY(), aBbox->GetBottom() ) };
70 VIEW_RTREE_BASE::Remove( mmin, mmax, aItem );
71 return;
72 }
73
74 // FIXME: use cached bbox or ptr_map to speed up pointer <-> node lookups.
75 const int mmin[2] = { INT_MIN, INT_MIN };
76 const int mmax[2] = { INT_MAX, INT_MAX };
77
78 VIEW_RTREE_BASE::Remove( mmin, mmax, aItem );
79 }
80
85 template <class Visitor>
86 void Query( const BOX2I& aBounds, Visitor& aVisitor ) const
87 {
88 int mmin[2] = { std::min( aBounds.GetX(), aBounds.GetRight() ), std::min( aBounds.GetY(), aBounds.GetBottom() ) };
89 int mmax[2] = { std::max( aBounds.GetX(), aBounds.GetRight() ), std::max( aBounds.GetY(), aBounds.GetBottom() ) };
90
91 // We frequently use the maximum bounding box to recache all items
92 // or for any item that overflows the integer width limits of BBOX2I
93 // in this case, we search the full rtree whose bounds are absolute
94 // coordinates rather than relative
95 BOX2I max_box;
96 max_box.SetMaximum();
97
98 if( aBounds == max_box )
99 {
100 mmin[0] = mmin[1] = INT_MIN;
101 mmax[0] = mmax[1] = INT_MAX;
102 }
103
104 VIEW_RTREE_BASE::Search( mmin, mmax, aVisitor );
105 }
106
107private:
108};
109} // namespace KIGFX
110
111#endif
constexpr void SetMaximum()
Definition: box2.h:80
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
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:84
Implement an non-owning R-tree for fast spatial indexing of VIEW items.
Definition: view_rtree.h:42
void Insert(VIEW_ITEM *aItem, const BOX2I &bbox)
Insert an item into the tree.
Definition: view_rtree.h:49
void Query(const BOX2I &aBounds, Visitor &aVisitor) const
Execute a function object aVisitor for each item whose bounding box intersects with aBounds.
Definition: view_rtree.h:86
void Remove(VIEW_ITEM *aItem, const BOX2I *aBbox)
Remove an item from the tree.
Definition: view_rtree.h:62
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
RTree< VIEW_ITEM *, int, 2, double > VIEW_RTREE_BASE
Definition: view_rtree.h:36