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 The 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() ),
52 std::min( bbox.GetY(), bbox.GetBottom() ) };
53 const int mmax[2] = { std::max( bbox.GetX(), bbox.GetRight() ),
54 std::max( bbox.GetY(), bbox.GetBottom() ) };
55
56 VIEW_RTREE_BASE::Insert( mmin, mmax, aItem );
57 }
58
64 void Remove( VIEW_ITEM* aItem, const BOX2I* aBbox )
65 {
66 // const BOX2I& bbox = aItem->ViewBBox();
67
68 if( aBbox )
69 {
70 const int mmin[2] = { std::min( aBbox->GetX(), aBbox->GetRight() ),
71 std::min( aBbox->GetY(), aBbox->GetBottom() ) };
72 const int mmax[2] = { std::max( aBbox->GetX(), aBbox->GetRight() ),
73 std::max( aBbox->GetY(), aBbox->GetBottom() ) };
74 VIEW_RTREE_BASE::Remove( mmin, mmax, aItem );
75 return;
76 }
77
78 // FIXME: use cached bbox or ptr_map to speed up pointer <-> node lookups.
79 const int mmin[2] = { INT_MIN, INT_MIN };
80 const int mmax[2] = { INT_MAX, INT_MAX };
81
82 VIEW_RTREE_BASE::Remove( mmin, mmax, aItem );
83 }
84
89 template <class Visitor>
90 void Query( const BOX2I& aBounds, Visitor& aVisitor ) const
91 {
92 int mmin[2] = { std::min( aBounds.GetX(), aBounds.GetRight() ),
93 std::min( aBounds.GetY(), aBounds.GetBottom() ) };
94 int mmax[2] = { std::max( aBounds.GetX(), aBounds.GetRight() ),
95 std::max( aBounds.GetY(), aBounds.GetBottom() ) };
96
97 // We frequently use the maximum bounding box to recache all items
98 // or for any item that overflows the integer width limits of BBOX2I
99 // in this case, we search the full rtree whose bounds are absolute
100 // coordinates rather than relative
101 BOX2I max_box;
102 max_box.SetMaximum();
103
104 if( aBounds == max_box )
105 {
106 mmin[0] = mmin[1] = INT_MIN;
107 mmax[0] = mmax[1] = INT_MAX;
108 }
109
110 VIEW_RTREE_BASE::Search( mmin, mmax, aVisitor );
111 }
112
113private:
114};
115} // namespace KIGFX
116
117#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:86
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:90
void Remove(VIEW_ITEM *aItem, const BOX2I *aBbox)
Remove an item from the tree.
Definition: view_rtree.h:64
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