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