Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
Spatial_Quadtree.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 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_QUAD_TREE_H
22 #define GF_SPATIAL_QUAD_TREE_H
23 
24 #include <cassert>
25 #include <vector>
26 
27 #include "BlockAllocator.h"
28 #include "CoreApi.h"
29 #include "Handle.h"
30 #include "Rect.h"
31 #include "SpatialTypes.h"
32 
33 namespace gf {
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 inline namespace v1 {
36 #endif
37 
45  class GF_CORE_API Quadtree {
46  public:
50  Quadtree(const RectF& bounds);
51 
59  SpatialId insert(Handle handle, const RectF& bounds);
60 
67  void modify(SpatialId id, RectF bounds);
68 
77  std::size_t query(const RectF& bounds, SpatialQueryCallback callback, SpatialQuery kind = SpatialQuery::Intersect);
78 
84  void remove(SpatialId id);
85 
89  void clear();
90 
96  Handle operator[](SpatialId id);
97 
98  private:
99  std::size_t allocateEntry();
100  void disposeEntry(std::size_t index);
101 
102  std::size_t allocateNode();
103  void disposeNode(std::size_t index);
104 
105  bool doInsert(std::size_t entryIndex, std::size_t nodeIndex);
106  std::size_t doQuery(std::size_t nodeIndex, const RectF& bounds, SpatialQueryCallback callback, SpatialQuery kind);
107  void doRemove(std::size_t entryIndex);
108 
109  void subdivide(std::size_t nodeIndex);
110  void sanitize(std::size_t nodeIndex);
111 
112  private:
113  static constexpr std::size_t Size = 16;
114  static constexpr std::size_t Null = static_cast<std::size_t>(-1);
115 
116  struct Entry {
117  Handle handle;
118  RectF bounds;
119  std::size_t node;
120  };
121 
122  BlockAllocator<Entry> m_entries;
123 
124  struct Node {
125  RectF bounds;
126  std::vector<std::size_t> entries;
127  std::size_t parent;
128  std::size_t children[4];
129 
130  bool isLeaf() {
131  return children[0] == Null;
132  }
133  };
134 
135  BlockAllocator<Node> m_nodes;
136 
137  std::size_t m_root;
138  };
139 
140 #ifndef DOXYGEN_SHOULD_SKIP_THIS
141 }
142 #endif
143 }
144 
145 #endif // GF_SPATIAL_QUAD_TREE_H
The structure represents an internal node.
SpatialQuery
A kind of spatial query.
Definition: SpatialTypes.h:73
SpatialId
A spatial id.
Definition: SpatialTypes.h:42
Search for all objects that intersect the given bounds.
The namespace for gf classes.
Definition: Action.h:35
An implementation of quadtree.
Definition: Spatial_Quadtree.h:45
std::function< void(Handle)> SpatialQueryCallback
A callback for spatial query.
Definition: SpatialTypes.h:82
A handle to an object or an id.
Definition: Handle.h:40