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
33
34namespace KIGFX
35{
36
37class VIEW_ITEM;
38
46{
47public:
53 void Insert( VIEW_ITEM* aItem, const BOX2I& bbox )
54 {
55 const int mmin[2] = { std::min( bbox.GetX(), bbox.GetRight() ),
56 std::min( bbox.GetY(), bbox.GetBottom() ) };
57 const int mmax[2] = { std::max( bbox.GetX(), bbox.GetRight() ),
58 std::max( bbox.GetY(), bbox.GetBottom() ) };
59
60 m_tree.Insert( mmin, mmax, aItem );
61 }
62
68 void Remove( VIEW_ITEM* aItem, const BOX2I* aBbox )
69 {
70 if( aBbox )
71 {
72 const int mmin[2] = { std::min( aBbox->GetX(), aBbox->GetRight() ),
73 std::min( aBbox->GetY(), aBbox->GetBottom() ) };
74 const int mmax[2] = { std::max( aBbox->GetX(), aBbox->GetRight() ),
75 std::max( aBbox->GetY(), aBbox->GetBottom() ) };
76 m_tree.Remove( mmin, mmax, aItem );
77 return;
78 }
79
80 // DYNAMIC_RTREE::Remove falls back to full-tree search when the provided
81 // bbox doesn't match, so passing INT_MIN/INT_MAX triggers that path.
82 const int mmin[2] = { INT_MIN, INT_MIN };
83 const int mmax[2] = { INT_MAX, INT_MAX };
84 m_tree.Remove( mmin, mmax, aItem );
85 }
86
91 template <class Visitor>
92 void Query( const BOX2I& aBounds, Visitor& aVisitor ) const
93 {
94 int mmin[2] = { std::min( aBounds.GetX(), aBounds.GetRight() ),
95 std::min( aBounds.GetY(), aBounds.GetBottom() ) };
96 int mmax[2] = { std::max( aBounds.GetX(), aBounds.GetRight() ),
97 std::max( aBounds.GetY(), aBounds.GetBottom() ) };
98
99 // We frequently use the maximum bounding box to recache all items
100 // or for any item that overflows the integer width limits of BBOX2I
101 // in this case, we search the full rtree whose bounds are absolute
102 // coordinates rather than relative
103 BOX2I max_box;
104 max_box.SetMaximum();
105
106 if( aBounds == max_box )
107 {
108 mmin[0] = mmin[1] = INT_MIN;
109 mmax[0] = mmax[1] = INT_MAX;
110 }
111
112 m_tree.Search( mmin, mmax, aVisitor );
113 }
114
119 {
120 m_tree.RemoveAll();
121 }
122
128 void BulkLoad( std::vector<std::pair<VIEW_ITEM*, BOX2I>>& aItems )
129 {
131 std::vector<BULK_ENTRY> entries;
132 entries.reserve( aItems.size() );
133
134 for( const auto& [item, bbox] : aItems )
135 {
136 BULK_ENTRY e;
137 e.min[0] = std::min( bbox.GetX(), bbox.GetRight() );
138 e.min[1] = std::min( bbox.GetY(), bbox.GetBottom() );
139 e.max[0] = std::max( bbox.GetX(), bbox.GetRight() );
140 e.max[1] = std::max( bbox.GetY(), bbox.GetBottom() );
141 e.data = item;
142 entries.push_back( e );
143 }
144
145 m_tree.BulkLoad( entries );
146 }
147
148private:
150};
151
152} // namespace KIGFX
153
154#endif
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
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 a non-owning R-tree for fast spatial indexing of VIEW items.
Definition view_rtree.h:46
KIRTREE::DYNAMIC_RTREE< VIEW_ITEM *, int, 2 > m_tree
Definition view_rtree.h:149
void BulkLoad(std::vector< std::pair< VIEW_ITEM *, BOX2I > > &aItems)
Build the R-tree from a batch of items using packed bulk loading.
Definition view_rtree.h:128
void Insert(VIEW_ITEM *aItem, const BOX2I &bbox)
Insert an item into the tree.
Definition view_rtree.h:53
void RemoveAll()
Remove all items from the tree.
Definition view_rtree.h:118
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:92
void Remove(VIEW_ITEM *aItem, const BOX2I *aBbox)
Remove an item from the tree.
Definition view_rtree.h:68
Dynamic R*-tree with SoA node layout and stored insertion bounding boxes.
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:33
Entry type for bulk loading.