Flow123d
bih_tree.hh
Go to the documentation of this file.
1 /**!
2  *
3  * Copyright (C) 2007 Technical University of Liberec. All rights reserved.
4  *
5  * Please make a following refer to Flow123d on your project site if you use the program for any purpose,
6  * especially for academic research:
7  * Flow123d, Research Centre: Advanced Remedial Technologies, Technical University of Liberec, Czech Republic
8  *
9  * This program is free software; you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License version 3 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with this program; if not,
17  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 021110-1307, USA.
18  *
19  *
20  * $Id: bih_tree.hh 1567 2012-02-28 13:24:58Z jan.brezina $
21  * $Revision: 1567 $
22  * $LastChangedBy: jan.brezina $
23  * $LastChangedDate: 2012-02-28 14:24:58 +0100 (Tue, 28 Feb 2012) $
24  *
25  *
26  */
27 
28 #ifndef BIH_TREE_HH_
29 #define BIH_TREE_HH_
30 
31 #include "mesh/bih_node.hh"
32 #include "mesh/mesh.h"
33 #include "mesh/point.hh"
34 #include <armadillo>
35 
36 
37 
38 /**
39  * @brief Class for O(log N) lookup for intersections with a set of bounding boxes.
40  *
41  * Notes:
42  * Assumes spacedim=3. Implementation was designed for arbitrary number of childs per node, but
43  * currently it supports max 2 childs per node (binary tree).
44  *
45  */
46 class BIHTree {
47 public:
48  /// count of dimensions
49  static const unsigned int dimension = 3;
50  /// max count of elements to estimate median - value must be even
51  static const unsigned int max_median_sample_size = 5;
52 
53  /**
54  * Constructor
55  *
56  * Set class members and call functions which create tree
57  * @param mesh - Mesh used for creation the tree
58  * @param soft_leaf_size_limit - Maximal number of elements stored in a leaf node of BIH tree.
59  */
60  BIHTree(Mesh* mesh, unsigned int soft_leaf_size_limit = 20);
61 
62  /**
63  * Destructor
64  */
65  ~BIHTree();
66 
67  /**
68  * Get count of elements stored in tree
69  *
70  * @return Count of bounding boxes stored in elements_ member
71  */
72  unsigned int get_element_count();
73 
74  /**
75  * Main bounding box of the whole tree.
76  */
77  const BoundingBox &tree_box();
78 
79  /**
80  * Gets elements which can have intersection with bounding box
81  *
82  * @param boundingBox Bounding box which is tested if has intersection
83  * @param searchedElements vector of ids of suspect elements
84  */
85  void find_bounding_box(const BoundingBox &boundingBox, std::vector<unsigned int> &result_list);
86 
87  /**
88  * Gets elements which can have intersection with point
89  *
90  * @param point Point which is tested if has intersection
91  * @param searchedElements vector of ids of suspect elements
92  */
93  void find_point(const Space<3>::Point &point, std::vector<unsigned int> &result_list);
94 
95  /**
96  * Browse tree and get its typical parameters
97  * Method for gtests
98  *
99  * @param maxDepth Gets maximal depth of tree
100  * @param minDepth Gets minimal depth of tree
101  * @param avgDepth Gets average depth of tree
102  * @param leafNodesCount Gets count of all leaf nodes of tree
103  * @param innerNodesCount Gets count of all inner nodes of tree
104  * @param elementLeafCount Gets sum of elements contained in all leaf nodes
105  */
106  void get_tree_params(unsigned int &maxDepth, unsigned int &minDepth, double &avgDepth, unsigned int &leafNodesCount,
107  unsigned int &innerNodesCount, unsigned int &sumElements);
108 
109  /**
110  * Get vector of mesh elements bounding boxes
111  *
112  * @return elements_ vector
113  */
115 
116 private:
117  /// required reduction in size of box to allow further splitting
118  static const double size_reduce_factor;
119  /// value indicates ratio of the number of element in node and number of elements of its children
120  //static const double max_grow_factor;
121 
122  /// create bounding boxes of element
123  void element_boxes();
124  /**
125  * soft_leaf_size_limit - we try to split node if its size (number of elements) is
126  * greater then this limit. However we stop branching if number of redundant elements is to big.
127  */
128  //void create_tree(unsigned int soft_leaf_size_limit);
129  /// Creates root node of tree, finds its bounding coordinations
130  /// and pushes elements to the vector using for creating tree
131  //void create_root_node();
132 
133  void split_node(const BoundingBox &node_box, unsigned int node_idx);
134  void make_node(const BoundingBox &box, unsigned int node_idx);
135 
136 
137  /**
138  * For given node takes projection of centers of bounding boxes of its elements to axis given by
139  * @p node::axis()
140  * and estimate median of these values. That is optimal split point.
141  * Precise median is computed for sets smaller then @p max_median_sample_size
142  * estimate from random sample is used for larger sets.
143  */
144  //void set_node_median(unsigned char axis, BIHNode &node);
145  double estimate_median(unsigned char axis, const BIHNode &node);
146 
147  /// Put indexes of elements to in_leaves_ vector if node is marked as leaf
148  //void put_leaf_elements();
149  /// Deallocate memory reserved by vectors
150  //void free_memory();
151  /// Test if processed node is at the end of level during creating of tree
152  //void test_new_level();
153  /**
154  * Sort elements in node to 3 groups (contained only in left child, in both children, only in right child)
155  *
156  * @param bound1 Bound of sorting between first and second group
157  * @param bound2 Bound of sorting between second and third group
158  */
159  //void sort_elements(unsigned int &bound1, unsigned int &bound2);
160  /**
161  * Distribute elements into subareas
162  *
163  * @param leftChild Left child of actually processed node
164  * @param rightChild Right child of actually processed node
165  */
166  //void distribute_elements(BIHNode &left_child, BIHNode &right_child);
167 
168  /// mesh
170  /// vector of mesh elements bounding boxes
172  /// vector of tree nodes
174  /// Main bounding box.
176 
177  unsigned int leaf_size_limit;
178  unsigned int max_n_levels;
179 
180 
181  /// vector stored elements for level-order walk of tree
182  //std::deque<unsigned int> queue_;
183 
184 
185  //std::deque<BoundingBox> box_queue_;
186 
187  /// vector stored element indexes in leaf nodes
189  /// temporary vector stored element indexes in last complete level of the BFS tree
190  //std::vector<unsigned int> list_element_index_;
191  /// temporary vector stored element indexes in actually constructed level of the BFS tree
192  //std::vector<unsigned int> list_element_index_next_;
193  /// temporary vector stored values of coordinations for calculating median
195 
196  // random generator
197  std::mt19937 r_gen;
198 
199 };
200 
201 #endif /* BIH_TREE_HH_ */