Flow123d  jenkins-Flow123d-windows32-release-multijob-51
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  * Get vector of mesh elements bounding boxes
97  *
98  * @return elements_ vector
99  */
101 
102 protected:
103  /// required reduction in size of box to allow further splitting
104  static const double size_reduce_factor;
105 
106  /// create bounding boxes of element
107  void element_boxes();
108 
109  /// split tree node given by node_idx, distribute elements to child nodes
110  void split_node(const BoundingBox &node_box, unsigned int node_idx);
111 
112  /// create child nodes of node given by node_idx
113  void make_node(const BoundingBox &box, unsigned int node_idx);
114 
115  /**
116  * For given node takes projection of centers of bounding boxes of its elements to axis given by
117  * @p node::axis()
118  * and estimate median of these values. That is optimal split point.
119  * Precise median is computed for sets smaller then @p max_median_sample_size
120  * estimate from random sample is used for larger sets.
121  */
122  double estimate_median(unsigned char axis, const BIHNode &node);
123 
124  /// mesh
126  /// vector of mesh elements bounding boxes
128  /// vector of tree nodes
130  /// Main bounding box.
132  /// Maximal number of elements stored in a leaf node of BIH tree.
133  unsigned int leaf_size_limit;
134  /// Maximal count of BIH tree levels
135  unsigned int max_n_levels;
136 
137  /// vector stored element indexes in leaf nodes
139  /// temporary vector stored values of coordinations for calculating median
141 
142  // random generator
143  std::mt19937 r_gen;
144 
145 };
146 
147 #endif /* BIH_TREE_HH_ */
static const double size_reduce_factor
required reduction in size of box to allow further splitting
Definition: bih_tree.hh:104
std::vector< BoundingBox > & get_elements()
Definition: bih_tree.hh:100
Bounding box in 3d ambient space.
Definition: bounding_box.hh:55
const BoundingBox & tree_box()
Definition: bih_tree.cc:198
Mesh * mesh_
mesh
Definition: bih_tree.hh:125
void find_bounding_box(const BoundingBox &boundingBox, std::vector< unsigned int > &result_list)
Definition: bih_tree.cc:203
unsigned int max_n_levels
Maximal count of BIH tree levels.
Definition: bih_tree.hh:135
void split_node(const BoundingBox &node_box, unsigned int node_idx)
split tree node given by node_idx, distribute elements to child nodes
Definition: bih_tree.cc:72
std::vector< BoundingBox > elements_
vector of mesh elements bounding boxes
Definition: bih_tree.hh:127
double estimate_median(unsigned char axis, const BIHNode &node)
Definition: bih_tree.cc:161
???
Definition: mesh.h:108
void element_boxes()
create bounding boxes of element
Definition: bih_tree.cc:254
BoundingBox main_box_
Main bounding box.
Definition: bih_tree.hh:131
void make_node(const BoundingBox &box, unsigned int node_idx)
create child nodes of node given by node_idx
Definition: bih_tree.cc:124
unsigned int leaf_size_limit
Maximal number of elements stored in a leaf node of BIH tree.
Definition: bih_tree.hh:133
arma::vec::fixed< spacedim > Point
Definition: point.hh:23
static const unsigned int dimension
count of dimensions
Definition: bih_tree.hh:49
Class for O(log N) lookup for intersections with a set of bounding boxes.
Definition: bih_tree.hh:46
~BIHTree()
Definition: bih_tree.cc:67
BIHTree(Mesh *mesh, unsigned int soft_leaf_size_limit=20)
Definition: bih_tree.cc:41
std::vector< double > coors_
temporary vector stored values of coordinations for calculating median
Definition: bih_tree.hh:140
std::vector< unsigned int > in_leaves_
vector stored element indexes in leaf nodes
Definition: bih_tree.hh:138
std::mt19937 r_gen
Definition: bih_tree.hh:143
unsigned int get_element_count()
Definition: bih_tree.cc:193
void find_point(const Space< 3 >::Point &point, std::vector< unsigned int > &result_list)
Definition: bih_tree.cc:249
static const unsigned int max_median_sample_size
max count of elements to estimate median - value must be even
Definition: bih_tree.hh:51
std::vector< BIHNode > nodes_
vector of tree nodes
Definition: bih_tree.hh:129