Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
QuadTree.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_QUAD_TREE_H
22 #define GF_SPATIAL_QUAD_TREE_H
23 
24 #include <cassert>
25 #include <vector>
26 
27 #include <gf/Handle.h>
28 #include <gf/Portability.h>
29 #include <gf/Rect.h>
30 
31 #include "BlockAllocator.h"
32 #include "Types.h"
33 
34 namespace gf {
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 inline namespace v1 {
37 #endif
38 
46  class GF_API Quadtree {
47  public:
51  Quadtree(const RectF& bounds);
52 
60  SpatialId insert(Handle handle, const RectF& bounds);
61 
68  void modify(SpatialId id, RectF bounds);
69 
78  std::size_t query(const RectF& bounds, SpatialQueryCallback callback, SpatialQuery kind = SpatialQuery::Intersect);
79 
85  void remove(SpatialId id);
86 
90  void clear();
91 
97  Handle operator[](SpatialId id);
98 
99  private:
100  std::size_t allocateEntry();
101  void disposeEntry(std::size_t index);
102 
103  std::size_t allocateNode();
104  void disposeNode(std::size_t index);
105 
106  bool doInsert(std::size_t entryIndex, std::size_t nodeIndex);
107  std::size_t doQuery(std::size_t nodeIndex, const RectF& bounds, SpatialQueryCallback callback, SpatialQuery kind);
108  void doRemove(std::size_t entryIndex);
109 
110  void subdivide(std::size_t nodeIndex);
111  void sanitize(std::size_t nodeIndex);
112 
113  private:
114  static constexpr std::size_t Size = 16;
115  static constexpr std::size_t Null = -1;
116 
117  struct Entry {
118  Handle handle;
119  RectF bounds;
120  std::size_t node;
121  };
122 
123  BlockAllocator<Entry> m_entries;
124 
125  struct Node {
126  RectF bounds;
127  std::vector<std::size_t> entries;
128  std::size_t parent;
129  std::size_t children[4];
130 
131  bool isLeaf() {
132  return children[0] == Null;
133  }
134  };
135 
136  BlockAllocator<Node> m_nodes;
137 
138  std::size_t m_root;
139  };
140 
141 #ifndef DOXYGEN_SHOULD_SKIP_THIS
142 }
143 #endif
144 }
145 
146 #endif // GF_SPATIAL_QUAD_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
An implementation of quadtree.
Definition: QuadTree.h:46
SpatialQuery
A kind of spatial query.
Definition: spatial/Types.h:72
Definition: Handle.h:33