Gamedev Framework (gf)  0.11.0
A C++14 framework for 2D games
RandomBinaryTree.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_RANDOM_BINARY_TREE_H
22 #define GF_RANDOM_BINARY_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 
41  class GF_API RandomBinaryTree {
42  public:
43 
47  class Node {
48  public:
56  Node(const RectI& area, std::size_t parent, int level)
57  : m_parent(parent)
58  , m_left(0)
59  , m_right(0)
60  , m_level(level)
61  , m_area(area)
62  {
63 
64  }
65 
73  int getLevel() const {
74  return m_level;
75  }
76 
84  bool isLeaf() const {
85  return m_left == 0 && m_right == 0;
86  }
87 
95  std::size_t getParentIndex() const {
96  return m_parent;
97  }
98 
104  std::size_t getLeftChildIndex() const {
105  return m_left;
106  }
107 
113  std::size_t getRightChildIndex() const {
114  return m_right;
115  }
116 
122  const RectI& getArea() const {
123  return m_area;
124  }
125 
131  bool contains(Vector2i position) const {
132  return m_area.contains(position);
133  }
134 
141  void setChildrenIndices(std::size_t left, std::size_t right) {
142  m_left = left;
143  m_right = right;
144  }
145 
146  private:
147  std::size_t m_parent;
148  std::size_t m_left;
149  std::size_t m_right;
150  int m_level;
151  RectI m_area;
152  };
153 
160  using Callback = std::function<bool(const RandomBinaryTree&, const Node&)>;
161 
167  RandomBinaryTree(const RectI& area);
168 
178  void create(Random& random, int levelMax, Vector2i minSize, Vector2i maxSize, float maxRatio = 1.5f);
179 
185  const Node& getRoot() const;
186 
192  const Node& getLeftChild(const Node& node) const;
193 
199  const Node& getRightChild(const Node& node) const;
200 
208  const Node& getParent(const Node& node) const;
209 
210 
217  bool contains(Vector2i position) const {
218  return getRoot().contains(position);
219  }
220 
231  const Node& find(Vector2i position) const;
232 
240  bool traversePreOrder(Callback callback) const;
241 
249  bool traverseInOrder(Callback callback) const;
250 
258  bool traversePostOrder(Callback callback) const;
259 
269  bool traverseLevelOrder(Callback callback) const;
270 
278  bool traverseInvertedLevelOrder(Callback callback) const;
279 
280  private:
281  std::vector<Node> m_nodes;
282  };
283 
284 #ifndef DOXYGEN_SHOULD_SKIP_THIS
285 }
286 #endif
287 }
288 
289 #endif // GF_RANDOM_BINARY_TREE_H
A random engine.
Definition: Random.h:45
A random binary space partionning tree.
Definition: RandomBinaryTree.h:41
int getLevel() const
Get the level of the node in the tree.
Definition: RandomBinaryTree.h:73
bool contains(Vector2i position) const
Check if the area of the node contains a position.
Definition: RandomBinaryTree.h:131
std::size_t getLeftChildIndex() const
Get the left child&#39;s index.
Definition: RandomBinaryTree.h:104
std::function< bool(const RandomBinaryTree &, const Node &)> Callback
A callback function for traversing the tree.
Definition: RandomBinaryTree.h:160
The namespace for gf classes.
Definition: Action.h:35
bool contains(Vector2i position) const
Check if the tree contains a position.
Definition: RandomBinaryTree.h:217
bool isLeaf() const
Check if a node is a leaf.
Definition: RandomBinaryTree.h:84
A node of the random binary space partionning tree.
Definition: RandomBinaryTree.h:47
std::size_t getParentIndex() const
Get the parent&#39;s indes of the node.
Definition: RandomBinaryTree.h:95
void setChildrenIndices(std::size_t left, std::size_t right)
Set the children indices of the node.
Definition: RandomBinaryTree.h:141
std::size_t getRightChildIndex() const
Get the right child&#39;s index.
Definition: RandomBinaryTree.h:113
Node(const RectI &area, std::size_t parent, int level)
Constructor.
Definition: RandomBinaryTree.h:56
const RectI & getArea() const
Get the area of the node.
Definition: RandomBinaryTree.h:122