Gamedev Framework (gf)  0.7.0
A C++14 framework for 2D games
SpaceTree.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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_SPACE_TREE_H
22 #define GF_SPACE_TREE_H
23 
24 #include <functional>
25 #include <memory>
26 
27 #include "Portability.h"
28 #include "Random.h"
29 #include "Rect.h"
30 #include "Vector.h"
31 
32 namespace gf {
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 inline namespace v1 {
35 #endif
36 
44  class GF_API SpaceTree {
45  public:
52  using Callback = std::function<bool(const SpaceTree&)>;
53 
59  SpaceTree(const RectI& area);
60 
66  const RectI& getArea() const {
67  return m_area;
68  }
69 
77  int getLevel() const {
78  return m_level;
79  }
80 
84  void removeChildren();
85 
96  bool splitOnce(Random& random, Vector2i minSize, float maxRatio = 1.5f);
97 
107  void splitRecursive(Random& random, int levelMax, Vector2i minSize, Vector2i maxSize, float maxRatio = 1.5f);
108 
116  bool isLeaf() const {
117  return !m_left && !m_right;
118  }
119 
125  const SpaceTree *getLeftChild() const {
126  return m_left.get();
127  }
128 
135  return m_left.get();
136  }
137 
143  const SpaceTree *getRightChild() const {
144  return m_right.get();
145  }
146 
153  return m_right.get();
154  }
155 
163  const SpaceTree *getFather() const {
164  return m_father;
165  }
166 
175  return m_father;
176  }
177 
183  bool contains(Vector2i position) const;
184 
191  const SpaceTree *find(Vector2i position) const;
192 
200  bool traversePreOrder(Callback callback) const;
201 
209  bool traverseInOrder(Callback callback) const;
210 
218  bool traversePostOrder(Callback callback) const;
219 
229  bool traverseLevelOrder(Callback callback) const;
230 
238  bool traverseInvertedLevelOrder(Callback callback) const;
239 
240  private:
241  enum class Split {
242  None,
243  Vertical,
244  Horizontal,
245  };
246 
247  RectI m_area;
248 
249  Split m_split;
250  int m_position;
251 
252  int m_level;
253 
254  std::unique_ptr<SpaceTree> m_left;
255  std::unique_ptr<SpaceTree> m_right;
256 
257  SpaceTree *m_father;
258  };
259 
260 #ifndef DOXYGEN_SHOULD_SKIP_THIS
261 }
262 #endif
263 }
264 
265 #endif // GF_SPACE_TREE_H
A random engine.
Definition: Random.h:43
No alignement.
SpaceTree * getRightChild()
Get the right child.
Definition: SpaceTree.h:152
const SpaceTree * getRightChild() const
Get the right child.
Definition: SpaceTree.h:143
std::function< bool(const SpaceTree &)> Callback
A callback function for traversing the tree.
Definition: SpaceTree.h:52
The namespace for gf classes.
Definition: Action.h:34
const SpaceTree * getFather() const
Get the father of the node.
Definition: SpaceTree.h:163
SpaceTree * getLeftChild()
Get the left child.
Definition: SpaceTree.h:134
bool isLeaf() const
Check if a node is a leaf.
Definition: SpaceTree.h:116
const RectI & getArea() const
Get the area of the node.
Definition: SpaceTree.h:66
Binary space random partionning tree.
Definition: SpaceTree.h:44
int getLevel() const
Get the level of the node in the tree.
Definition: SpaceTree.h:77
SpaceTree * getFather()
Get the father of the node.
Definition: SpaceTree.h:174
const SpaceTree * getLeftChild() const
Get the left child.
Definition: SpaceTree.h:125