Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Spatial_RStarTree.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 Julien Bernard
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21#ifndef GF_SPATIAL_R_STAR_TREE_H
22#define GF_SPATIAL_R_STAR_TREE_H
23
24#include <cassert>
25#include <vector>
26
27#include <boost/container/static_vector.hpp>
28
29#include "BlockAllocator.h"
30#include "CoreApi.h"
31#include "Handle.h"
32#include "Rect.h"
33#include "SpatialTypes.h"
34
35namespace gf {
36#ifndef DOXYGEN_SHOULD_SKIP_THIS
37inline namespace v1 {
38#endif
39
49 class GF_CORE_API RStarTree {
50 public:
51 static constexpr std::size_t MaxSize = 16;
52 static constexpr std::size_t MinSize = 4;
53
58
66 SpatialId insert(Handle handle, const RectF& bounds);
67
74 void modify(SpatialId id, RectF bounds);
75
84 std::size_t query(const RectF& bounds, SpatialQueryCallback callback, SpatialQuery kind = SpatialQuery::Intersect);
85
91 void remove(SpatialId id);
92
96 void clear();
97
104
105 private:
106 std::size_t allocateEntry();
107 void disposeEntry(std::size_t index);
108
109 std::size_t allocateNode();
110 void disposeNode(std::size_t index);
111
112 RectF computeBounds(std::size_t nodeIndex);
113 void updateBoundsForChild(std::size_t parentIndex, const RectF& bounds, std::size_t childIndex);
114
115 void doInsert(std::size_t entryIndex, const RectF& bounds);
116
117 std::size_t chooseSubtree(std::size_t nodeIndex, const RectF& bounds);
118 std::size_t chooseNode(std::size_t nodeIndex, const RectF& bounds);
119 std::size_t searchForCoveringNode(std::size_t nodeIndex, const RectF& bounds);
120 bool existsEmptyVolumeExtension(std::size_t nodeIndex, const RectF& bounds);
121
122 struct Candidate {
123 std::size_t index;
124 float overlap;
125 bool isCandidate;
126 };
127
128 template<typename OverlapEnlargement>
129 std::size_t findCandidates(std::size_t nodeIndex, std::size_t t, std::size_t p, const RectF& bounds, std::vector<Candidate>& candidates);
130
131 std::size_t doInsertInLeaf(std::size_t nodeIndex, std::size_t entryIndex, const RectF& entryBounds);
132 std::size_t doInsertInBranch(std::size_t nodeIndex, std::size_t childIndex, const RectF& childBounds);
133
134 std::size_t doQuery(std::size_t nodeIndex, const RectF& bounds, SpatialQueryCallback callback, SpatialQuery kind);
135
136 void getEntriesAndDispose(std::size_t nodeIndex, std::vector<std::size_t>& eliminated);
137 void doRemove(std::size_t entryIndex);
138
139 void validate() const;
140 std::size_t validateNode(std::size_t nodeIndex) const;
141
142 private:
143 static constexpr std::size_t Size = MaxSize + 1;
144
145 struct Entry {
146 Handle handle;
147 RectF bounds;
148 std::size_t node;
149 };
150
151 BlockAllocator<Entry> m_entries;
152
153 struct Member {
154 RectF bounds;
155 std::size_t index;
156 };
157
158 struct Node {
159 enum NodeType {
160 Branch,
161 Leaf,
162 };
163
164 RectF bounds;
165 std::size_t parent;
166 NodeType type;
167 boost::container::static_vector<Member, Size> members;
168 };
169
170 BlockAllocator<Node> m_nodes;
171 std::size_t m_root;
172 };
173
174#ifndef DOXYGEN_SHOULD_SKIP_THIS
175}
176#endif
177}
178
179#endif // GF_SPATIAL_R_STAR_TREE_H
A handle to an object or an id.
Definition: Handle.h:40
An implemntation of a R* tree.
Definition: Spatial_RStarTree.h:49
SpatialId insert(Handle handle, const RectF &bounds)
Insert an object in the tree.
void remove(SpatialId id)
Remove an object from the tree.
Handle operator[](SpatialId id)
Get the handle associated to a spatial id.
std::size_t query(const RectF &bounds, SpatialQueryCallback callback, SpatialQuery kind=SpatialQuery::Intersect)
Query objects in the tree.
void clear()
Remove all the objects from the tree.
void modify(SpatialId id, RectF bounds)
Modify the bounds of an object.
RStarTree()
Constructor.
Rect< float > RectF
A float rectangle.
Definition: Rect.h:493
SpatialQuery
A kind of spatial query.
Definition: SpatialTypes.h:73
std::function< void(Handle)> SpatialQueryCallback
A callback for spatial query.
Definition: SpatialTypes.h:82
SpatialId
A spatial id.
Definition: SpatialTypes.h:42
@ Node
The structure represents an internal node.
@ Intersect
Search for all objects that intersect the given bounds.
The namespace for gf classes.