Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
RStarTree.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 <gf/Handle.h>
30 #include <gf/Portability.h>
31 #include <gf/Rect.h>
32 
33 #include "BlockAllocator.h"
34 #include "Types.h"
35 
36 namespace gf {
37 #ifndef DOXYGEN_SHOULD_SKIP_THIS
38 inline namespace v1 {
39 #endif
40 
50  class GF_API RStarTree {
51  public:
52  static constexpr std::size_t MaxSize = 16;
53  static constexpr std::size_t MinSize = 4;
54 
58  RStarTree();
59 
67  SpatialId insert(Handle handle, const RectF& bounds);
68 
75  void modify(SpatialId id, RectF bounds);
76 
85  std::size_t query(const RectF& bounds, SpatialQueryCallback callback, SpatialQuery kind = SpatialQuery::Intersect);
86 
92  void remove(SpatialId id);
93 
97  void clear();
98 
104  Handle operator[](SpatialId id);
105 
106  private:
107  std::size_t allocateEntry();
108  void disposeEntry(std::size_t index);
109 
110  std::size_t allocateNode();
111  void disposeNode(std::size_t index);
112 
113  RectF computeBounds(std::size_t nodeIndex);
114  void updateBoundsForChild(std::size_t parentIndex, const RectF& bounds, std::size_t childIndex);
115 
116  void doInsert(std::size_t entryIndex, const RectF& bounds);
117 
118  std::size_t chooseSubtree(std::size_t nodeIndex, const RectF& bounds);
119  std::size_t chooseNode(std::size_t nodeIndex, const RectF& bounds);
120  std::size_t searchForCoveringNode(std::size_t nodeIndex, const RectF& bounds);
121  bool existsEmptyVolumeExtension(std::size_t nodeIndex, const RectF& bounds);
122 
123  struct Candidate {
124  std::size_t index;
125  float overlap;
126  bool isCandidate;
127  };
128 
129  template<typename OverlapEnlargement>
130  std::size_t findCandidates(std::size_t nodeIndex, std::size_t t, std::size_t p, const RectF& bounds, std::vector<Candidate>& candidates);
131 
132  std::size_t doInsertInLeaf(std::size_t nodeIndex, std::size_t entryIndex, const RectF& entryBounds);
133  std::size_t doInsertInBranch(std::size_t nodeIndex, std::size_t childIndex, const RectF& childBounds);
134 
135  std::size_t doQuery(std::size_t nodeIndex, const RectF& bounds, SpatialQueryCallback callback, SpatialQuery kind);
136 
137  void getEntriesAndDispose(std::size_t nodeIndex, std::vector<std::size_t>& eliminated);
138  void doRemove(std::size_t entryIndex);
139 
140  void validate() const;
141  std::size_t validateNode(std::size_t nodeIndex) const;
142 
143  private:
144  static constexpr std::size_t Size = MaxSize + 1;
145 
146  struct Entry {
147  Handle handle;
148  RectF bounds;
149  std::size_t node;
150  };
151 
152  BlockAllocator<Entry> m_entries;
153 
154  struct Node {
155  enum NodeType {
156  Branch,
157  Leaf,
158  };
159 
160  struct Member {
162  std::size_t index;
163  };
164 
165  RectF bounds;
166  std::size_t parent;
167  NodeType type;
168  boost::container::static_vector<Member, Size> members;
169  };
170 
171  BlockAllocator<Node> m_nodes;
172  std::size_t m_root;
173  };
174 
175 #ifndef DOXYGEN_SHOULD_SKIP_THIS
176 }
177 #endif
178 }
179 
180 #endif // GF_SPATIAL_R_STAR_TREE_H
The structure represents an internal node.
std::function< void(Handle)> SpatialQueryCallback
A callback for spatial query.
Definition: spatial/Types.h:81
SpatialId
A spatial id.
Definition: spatial/Types.h:41
Search for all objects that intersect the given bounds.
The namespace for gf classes.
Definition: Action.h:35
Definition: RStarTree.h:160
RectF bounds
Definition: RStarTree.h:161
An implemntation of a R* tree.
Definition: RStarTree.h:50
SpatialQuery
A kind of spatial query.
Definition: spatial/Types.h:72
Definition: Handle.h:33
std::size_t index
Definition: RStarTree.h:162