BaseTree.h
Go to the documentation of this file.
1 #ifndef GfxTL_BASETREE_HEADER__
2 #define GfxTL_BASETREE_HEADER__
3 #include <utility>
4 #include <vector>
5 
6 namespace GfxTL
7 {
8  template <class Cell>
9  class BaseTree
10  {
11  public:
12  typedef Cell CellType;
13 
14  BaseTree();
15  BaseTree(const BaseTree<Cell>& bt);
16  virtual ~BaseTree();
17  virtual void Clear();
18  virtual void Init();
19 
20  CellType*&
21  Root()
22  {
23  return m_root;
24  }
25 
26  const CellType*
27  Root() const
28  {
29  return m_root;
30  }
31 
32  inline bool IsLeaf(const CellType& cell) const;
33  inline bool ExistChild(const CellType& cell, unsigned int i) const;
35  size_t NumberOfLeaves() const;
36  size_t NumberOfNodesOnLevel(size_t level) const;
37  size_t MaxDepth() const;
38  double AvgDepth() const;
39  template <class ScalarT>
40  void LeafDepthVariance(size_t* numLeaves, ScalarT* variance) const;
41 
42  protected:
43  CellType*
45  {
46  return (CellType*)1;
47  }
48 
49  private:
50  CellType* m_root;
51  };
52 
53  template <class Cell>
54  inline bool
55  BaseTree<Cell>::IsLeaf(const CellType& cell) const
56  {
57  return &(cell[0]) == NULL;
58  }
59 
60  template <class Cell>
61  inline bool
62  BaseTree<Cell>::ExistChild(const CellType& cell, unsigned int i) const
63  {
64  return &(cell[i]) > (CellType*)1;
65  }
66 
67  template <class Cell>
68  BaseTree<Cell>::BaseTree() : m_root(NULL)
69  {
70  }
71 
72  template <class Cell>
73  BaseTree<Cell>::BaseTree(const BaseTree<Cell>& bt) : m_root(NULL)
74  {
75  if (bt.m_root)
76  {
77  m_root = new Cell(*bt.m_root);
78  }
79  }
80 
81  template <class Cell>
83  {
84  Clear();
85  }
86 
87  template <class Cell>
88  void
90  {
91  if (m_root)
92  {
93  delete m_root;
94  m_root = NULL;
95  }
96  }
97 
98  template <class Cell>
99  void
101  {
102  }
103 
104  template <class Cell>
107  {
108  Clear();
109  if (bt.m_root)
110  {
111  m_root = new Cell(*bt.m_root);
112  }
113  return *this;
114  }
115 
116  template <class Cell>
117  size_t
119  {
120  if (!Root())
121  {
122  return 0;
123  }
124  size_t numLeaves = 0;
125  std::vector<const CellType*> stack;
126  stack.push_back(Root());
127  while (stack.size())
128  {
129  const CellType* c = stack.back();
130  stack.pop_back();
131  if (IsLeaf(*c))
132  {
133  ++numLeaves;
134  }
135  else
136  for (unsigned int i = 0; i < CellType::NChildren; ++i)
137  if (ExistChild(*c, i))
138  {
139  stack.push_back(&((*c)[i]));
140  }
141  }
142  return numLeaves;
143  }
144 
145  template <class Cell>
146  size_t
148  {
149  if (!Root())
150  {
151  return 0;
152  }
153  typedef std::pair<const CellType*, size_t> Pair;
154  size_t numNodes = 0;
155  std::vector<Pair> stack;
156  stack.push_back(Pair(Root(), 0));
157  while (stack.size())
158  {
159  Pair p = stack.back();
160  stack.pop_back();
161  if (p.second == level)
162  {
163  ++numNodes;
164  continue;
165  }
166  else if (IsLeaf(*p.first))
167  {
168  continue;
169  }
170  else
171  for (unsigned int i = 0; i < CellType::NChildren; ++i)
172  if (ExistChild(*p.first, i))
173  {
174  stack.push_back(Pair(&((*p.first)[i]), p.second + 1));
175  }
176  }
177  return numNodes;
178  }
179 
180  template <class Cell>
181  size_t
183  {
184  size_t maxLevel = 0;
185  if (!Root())
186  {
187  return maxLevel;
188  }
189  typedef std::pair<const CellType*, size_t> Pair;
190  std::vector<Pair> stack;
191  stack.push_back(Pair(Root(), 0));
192  while (stack.size())
193  {
194  Pair p = stack.back();
195  stack.pop_back();
196  if (p.second > maxLevel)
197  {
198  maxLevel = p.second;
199  }
200  if (IsLeaf(*p.first))
201  {
202  continue;
203  }
204  else
205  for (unsigned int i = 0; i < CellType::NChildren; ++i)
206  if (ExistChild(*p.first, i))
207  {
208  stack.push_back(Pair(&((*p.first)[i]), p.second + 1));
209  }
210  }
211  return maxLevel;
212  }
213 
214  template <class Cell>
215  double
217  {
218  if (!Root())
219  {
220  return 0.0;
221  }
222  size_t levelSum = 0;
223  size_t leaveCount = 0;
224  typedef std::pair<const CellType*, size_t> Pair;
225  std::vector<Pair> stack;
226  stack.push_back(Pair(Root(), 0));
227  while (stack.size())
228  {
229  Pair p = stack.back();
230  stack.pop_back();
231  if (IsLeaf(*p.first))
232  {
233  levelSum += p.second;
234  ++leaveCount;
235  continue;
236  }
237  else
238  for (unsigned int i = 0; i < CellType::NChildren; ++i)
239  if (ExistChild(*p.first, i))
240  {
241  stack.push_back(Pair(&((*p.first)[i]), p.second + 1));
242  }
243  }
244  return double(levelSum) / double(leaveCount);
245  }
246 
247  template <class Cell>
248  template <class ScalarT>
249  void
250  BaseTree<Cell>::LeafDepthVariance(size_t* numLeaves, ScalarT* variance) const
251  {
252  typedef ScalarT ScalarType;
253  *numLeaves = 0;
254  *variance = 0;
255  if (!Root())
256  {
257  return;
258  }
259  ScalarType avgDepth = 0;
260  std::vector<std::pair<const CellType*, size_t>> stack;
261  stack.push_back(std::make_pair(Root(), size_t(0)));
262  while (stack.size())
263  {
264  std::pair<const CellType*, size_t> c = stack.back();
265  stack.pop_back();
266  if (IsLeaf(*c.first))
267  {
268  ++(*numLeaves);
269  avgDepth += (ScalarType)c.second;
270  }
271  else
272  for (unsigned int i = 0; i < Cell::NChildren; ++i)
273  if (ExistChild(*c.first, i))
274  {
275  stack.push_back(std::make_pair(&((*c.first)[i]), c.second + 1));
276  }
277  }
278  avgDepth /= ScalarType(*numLeaves);
279  *variance = 0;
280  stack.push_back(std::make_pair(Root(), size_t(0)));
281  while (stack.size())
282  {
283  std::pair<const CellType*, size_t> c = stack.back();
284  stack.pop_back();
285  if (IsLeaf(*c.first))
286  {
287  ScalarType t = ScalarType(c.second) - avgDepth;
288  *variance += t * t;
289  }
290  else
291  for (unsigned int i = 0; i < Cell::NChildren; ++i)
292  if (ExistChild(*c.first, i))
293  {
294  stack.push_back(std::make_pair(&((*c.first)[i]), c.second + 1));
295  }
296  }
297  *variance /= ScalarType(*numLeaves);
298  }
299 }; // namespace GfxTL
300 
301 #endif
GfxTL::BaseTree::CellType
Cell CellType
Definition: BaseTree.h:12
GfxTL::BaseTree::AvgDepth
double AvgDepth() const
Definition: BaseTree.h:216
GfxTL::BaseTree::MaxDepth
size_t MaxDepth() const
Definition: BaseTree.h:182
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
GfxTL::BaseTree::ExistChild
bool ExistChild(const CellType &cell, unsigned int i) const
Definition: BaseTree.h:62
GfxTL::BaseTree::~BaseTree
virtual ~BaseTree()
Definition: BaseTree.h:82
GfxTL::BaseTree::NumberOfNodesOnLevel
size_t NumberOfNodesOnLevel(size_t level) const
Definition: BaseTree.h:147
GfxTL::BaseTree::NumberOfLeaves
size_t NumberOfLeaves() const
Definition: BaseTree.h:118
GfxTL::BaseTree::LeafDepthVariance
void LeafDepthVariance(size_t *numLeaves, ScalarT *variance) const
Definition: BaseTree.h:250
GfxTL::BaseTree::IsLeaf
bool IsLeaf(const CellType &cell) const
Definition: BaseTree.h:55
GfxTL::BaseTree
Definition: BaseTree.h:9
GfxTL::BaseTree::Init
virtual void Init()
Definition: BaseTree.h:100
GfxTL
Definition: AABox.h:9
GfxTL::BaseTree::Root
const CellType * Root() const
Definition: BaseTree.h:27
GfxTL::BaseTree::Clear
virtual void Clear()
Definition: BaseTree.h:89
GfxTL::BaseTree::operator=
BaseTree< Cell > & operator=(const BaseTree< Cell > &bt)
Definition: BaseTree.h:106
GfxTL::BaseTree::Root
CellType *& Root()
Definition: BaseTree.h:21
GfxTL::BaseTree::InnerNodeMarker
CellType * InnerNodeMarker() const
Definition: BaseTree.h:44
GfxTL::BaseTree::BaseTree
BaseTree()
Definition: BaseTree.h:68