Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
Spatial_DynamicTree.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_DYNAMIC_TREE_H
22 #define GF_SPATIAL_DYNAMIC_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 
42  class GF_CORE_API DynamicTree {
43  public:
47  DynamicTree();
48 
56  SpatialId insert(Handle handle, const RectF& bounds);
57 
64  void modify(SpatialId id, RectF bounds);
65 
74  std::size_t query(const RectF& bounds, SpatialQueryCallback callback, SpatialQuery kind = SpatialQuery::Intersect);
75 
81  void remove(SpatialId id);
82 
86  void clear();
87 
93  Handle operator[](SpatialId id);
94 
95  private:
96  std::size_t allocateNode();
97  void disposeNode(std::size_t index);
98 
99  void doInsert(std::size_t leaf);
100  void doRemove(std::size_t leaf);
101 
102  std::size_t balance(std::size_t iA);
103 
104  private:
105  struct Node {
106  Handle handle;
107  RectF bounds;
108  std::size_t parent;
109  std::size_t child1;
110  std::size_t child2;
111  int32_t height;
112 
113  bool isLeaf() const {
114  return child1 == NullIndex;
115  }
116  };
117 
118  BlockAllocator<Node> m_nodes;
119 
120  std::size_t m_root;
121  };
122 
123 #ifndef DOXYGEN_SHOULD_SKIP_THIS
124 }
125 #endif
126 }
127 
128 #endif // GF_SPATIAL_DYNAMIC_TREE_H
The structure represents an internal node.
SpatialQuery
A kind of spatial query.
Definition: SpatialTypes.h:73
An implementation of dynamic tree.
Definition: Spatial_DynamicTree.h:42
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
constexpr std::size_t NullIndex
A null index in a block allocator.
Definition: BlockAllocator.h:38
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