Flow123d  JS_before_hm-1804-gf2ad740aa
bih_node.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file bih_node.hh
15  * @brief
16  */
17 
18 #ifndef BIH_NODE_HH_
19 #define BIH_NODE_HH_
20 
21 #include "system/system.hh"
22 #include "mesh/bounding_box.hh"
23 #include <armadillo>
24 #include <algorithm>
25 
26 class BIHNode {
27 public:
28 
29  /// count of subareas - don't change
30  static const unsigned int child_count = 2;
31  /// count of dimensions
32  static const unsigned char dimension = 3;
33 
34  /**
35  * Set leaf node.
36  */
37  void set_leaf(unsigned int begin, unsigned int end, double bound,unsigned int depth) {
38  child_[0]=begin;
39  child_[1]=end;
40  bound_ = bound;
42  }
43 
44  /**
45  * Set non-leaf node.
46  */
47  void set_non_leaf(unsigned int left, unsigned int right, unsigned int axis) {
48  OLD_ASSERT(axis < dimension," ");
49  OLD_ASSERT(is_leaf(), " "); // first must be leaf node (must set bound_)
50  axis_=axis;
51  child_[0]=left;
52  child_[1]=right;
53  }
54 
55  /// return true if node is leaf
56  bool is_leaf() const
57  { return axis_ >= dimension; }
58 
59  /// return depth of leaf node
60 
61  inline unsigned char depth() const
62  {
63  OLD_ASSERT( is_leaf(), "Not leaf node.");
64  return axis_ - dimension;
65  }
66 
67  unsigned int leaf_begin() const
68  {
69  OLD_ASSERT( is_leaf(), "Not leaf node.");
70  return child_[0];
71  }
72 
73  unsigned int leaf_end() const
74  {
75  OLD_ASSERT( is_leaf(), "Not leaf node.");
76  return child_[1];
77  }
78 
79  /**
80  * Get count of elements stored in
81  *
82  * @return Count of elements contained in node
83  */
84  unsigned int leaf_size() const
85  {
86  OLD_ASSERT( is_leaf(), "Not leaf node.");
87  return child_[1] - child_[0];
88  }
89 
90  /// return axes (coordination of splitting) of inner node
91  unsigned int axis() const
92  {
93  OLD_ASSERT(!is_leaf(), "Not in branch node.\n");
94  return axis_;
95  }
96 
97  double bound() const
98  {
99  return bound_;
100  }
101 
102  /// Return index of child node.
103  unsigned int child(unsigned int i_child) const
104  {
105  ASSERT(!is_leaf()).error("Not in branch node.\n");
106  ASSERT_LT( i_child, child_count );
107  return child_[i_child];
108 
109  }
110 private:
111 
112 
113  /**
114  * Set depth of node to axes_ class members
115  *
116  * @param depth Depth of node in tree.
117  */
118  void set_depth(unsigned int depth) {
119  axis_ = depth + dimension;
120  }
121 
122  /// child nodes indexes
123  unsigned int child_[child_count];
124 
125  /**
126  * A non-leaf node has two childs (left and right). Their bounding boxes are created from the bounding box of parent
127  * so that in one direction the left child set max to its bound_ and the right child set its min to bound_.
128  * This way we can always repcreate bounding box of every node when traversing the tree from the root.
129  */
130  double bound_;
131 
132  /**
133  * Value stores coordination of splitting area for inner nodes or depth for leaf nodes
134  * - values 0,1,2 indicate inner node of tree and coordination of splitting area
135  * - values 3 and greater indicate leaf node of tree and store depth of node (depth = axes_ - 3)
136  */
137  unsigned char axis_;
138 
139 };
140 
141 #endif /* BIH_NODE_HH_ */
LimitSide::right
@ right
BIHNode::depth
unsigned char depth() const
return depth of leaf node
Definition: bih_node.hh:61
BIHNode::bound_
double bound_
Definition: bih_node.hh:130
ASSERT
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:347
system.hh
BIHNode::child_count
static const unsigned int child_count
count of subareas - don't change
Definition: bih_node.hh:30
ASSERT_LT
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than)
Definition: asserts.hh:296
LimitSide::left
@ left
BIHNode::bound
double bound() const
Definition: bih_node.hh:97
BIHNode::leaf_end
unsigned int leaf_end() const
Definition: bih_node.hh:73
bounding_box.hh
BIHNode::set_depth
void set_depth(unsigned int depth)
Definition: bih_node.hh:118
BIHNode::set_non_leaf
void set_non_leaf(unsigned int left, unsigned int right, unsigned int axis)
Definition: bih_node.hh:47
BIHNode::leaf_size
unsigned int leaf_size() const
Definition: bih_node.hh:84
OLD_ASSERT
#define OLD_ASSERT(...)
Definition: global_defs.h:108
BIHNode::child
unsigned int child(unsigned int i_child) const
Return index of child node.
Definition: bih_node.hh:103
BIHNode
Definition: bih_node.hh:26
BIHNode::is_leaf
bool is_leaf() const
return true if node is leaf
Definition: bih_node.hh:56
BIHNode::set_leaf
void set_leaf(unsigned int begin, unsigned int end, double bound, unsigned int depth)
Definition: bih_node.hh:37
BIHNode::dimension
static const unsigned char dimension
count of dimensions
Definition: bih_node.hh:32
BIHNode::axis_
unsigned char axis_
Definition: bih_node.hh:137
BIHNode::leaf_begin
unsigned int leaf_begin() const
Definition: bih_node.hh:67
BIHNode::axis
unsigned int axis() const
return axes (coordination of splitting) of inner node
Definition: bih_node.hh:91
BIHNode::child_
unsigned int child_[child_count]
child nodes indexes
Definition: bih_node.hh:123