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, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef __VIEW_RTREE_H
24#define __VIEW_RTREE_H
25
26#include <math/box2.h>
27
29
30namespace KIGFX
31{
32
33class VIEW_ITEM;
34
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 m_tree.Insert( mmin, mmax, aItem );
57 }
58
64 void Remove( VIEW_ITEM* aItem, const BOX2I* aBbox )
65 {
66 if( aBbox )
67 {
68 const int mmin[2] = { std::min( aBbox->GetX(), aBbox->GetRight() ),
69 std::min( aBbox->GetY(), aBbox->GetBottom() ) };
70 const int mmax[2] = { std::max( aBbox->GetX(), aBbox->GetRight() ),
71 std::max( aBbox->GetY(), aBbox->GetBottom() ) };
72 m_tree.Remove( mmin, mmax, aItem );
73 return;
74 }
75
76 // DYNAMIC_RTREE::Remove falls back to full-tree search when the provided
77 // bbox doesn't match, so passing INT_MIN/INT_MAX triggers that path.
78 const int mmin[2] = { INT_MIN, INT_MIN };
79 const int mmax[2] = { INT_MAX, INT_MAX };
80 m_tree.Remove( mmin, mmax, aItem );
81 }
82
87 template <class Visitor>
88 void Query( const BOX2I& aBounds, Visitor& aVisitor ) const
89 {
90 int mmin[2] = { std::min( aBounds.GetX(), aBounds.GetRight() ),
91 std::min( aBounds.GetY(), aBounds.GetBottom() ) };
92 int mmax[2] = { std::max( aBounds.GetX(), aBounds.GetRight() ),
93 std::max( aBounds.GetY(), aBounds.GetBottom() ) };
94
95 // We frequently use the maximum bounding box to recache all items
96 // or for any item that overflows the integer width limits of BBOX2I
97 // in this case, we search the full rtree whose bounds are absolute
98 // coordinates rather than relative
99 BOX2I max_box;
100 max_box.SetMaximum();
101
102 if( aBounds == max_box )
103 {
104 mmin[0] = mmin[1] = INT_MIN;
105 mmax[0] = mmax[1] = INT_MAX;
106 }
107
108 m_tree.Search( mmin, mmax, aVisitor );
109 }
110
115 {
116 m_tree.RemoveAll();
117 }
118
124 void BulkLoad( std::vector<std::pair<VIEW_ITEM*, BOX2I>>& aItems )
125 {
127 std::vector<BULK_ENTRY> entries;
128 entries.reserve( aItems.size() );
129
130 for( const auto& [item, bbox] : aItems )
131 {
132 BULK_ENTRY e;
133 e.min[0] = std::min( bbox.GetX(), bbox.GetRight() );
134 e.min[1] = std::min( bbox.GetY(), bbox.GetBottom() );
135 e.max[0] = std::max( bbox.GetX(), bbox.GetRight() );
136 e.max[1] = std::max( bbox.GetY(), bbox.GetBottom() );
137 e.data = item;
138 entries.push_back( e );
139 }
140
141 m_tree.BulkLoad( entries );
142 }
143
144 bool IsEmpty() const { return m_tree.empty(); }
145
146private:
148};
149
150} // namespace KIGFX
151
152#endif
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr void SetMaximum()
Definition box2.h:76
constexpr coord_type GetY() const
Definition box2.h:204
constexpr coord_type GetX() const
Definition box2.h:203
constexpr coord_type GetRight() const
Definition box2.h:213
constexpr coord_type GetBottom() const
Definition box2.h:218
An abstract base class for deriving all objects that can be added to a VIEW.
Definition view_item.h:82
Implement a non-owning R-tree for fast spatial indexing of VIEW items.
Definition view_rtree.h:42
KIRTREE::DYNAMIC_RTREE< VIEW_ITEM *, int, 2 > m_tree
Definition view_rtree.h:147
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:124
void Insert(VIEW_ITEM *aItem, const BOX2I &bbox)
Insert an item into the tree.
Definition view_rtree.h:49
void RemoveAll()
Remove all items from the tree.
Definition view_rtree.h:114
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:88
bool IsEmpty() const
Definition view_rtree.h:144
void Remove(VIEW_ITEM *aItem, const BOX2I *aBbox)
Remove an item from the tree.
Definition view_rtree.h:64
Dynamic R*-tree with SoA node layout and stored insertion bounding boxes.
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
Entry type for bulk loading.