Flow123d  release_2.2.0-22-g936454a
bih_tree.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_tree.hh
15  * @brief
16  */
17 
18 #ifndef BIH_TREE_HH_
19 #define BIH_TREE_HH_
20 
21 #include "mesh/bih_node.hh"
22 #include "mesh/mesh.h"
23 #include "mesh/point.hh"
24 #include <armadillo>
25 #include <random>
26 
27 
28 /**
29  * @brief Class for O(log N) lookup for intersections with a set of bounding boxes.
30  *
31  * Notes:
32  * Assumes spacedim=3. Implementation was designed for arbitrary number of childs per node, but
33  * currently it supports max 2 childs per node (binary tree).
34  *
35  */
36 class BIHTree {
37 public:
38  /// count of dimensions
39  static const unsigned int dimension = 3;
40  /// max count of elements to estimate median - value must be even
41  static const unsigned int max_median_sample_size = 5;
42 
43  /**
44  * Constructor
45  *
46  * Set class members and call functions which create tree
47  * @param mesh - Mesh used for creation the tree
48  * @param soft_leaf_size_limit - Maximal number of elements stored in a leaf node of BIH tree.
49  */
50  BIHTree(Mesh* mesh, unsigned int soft_leaf_size_limit = 20);
51 
52  /**
53  * Destructor
54  */
55  ~BIHTree();
56 
57  /**
58  * Get count of elements stored in tree
59  *
60  * @return Count of bounding boxes stored in elements_ member
61  */
62  unsigned int get_element_count();
63 
64  /**
65  * Main bounding box of the whole tree.
66  */
67  const BoundingBox &tree_box() const;
68 
69  /**
70  * Gets elements which can have intersection with bounding box
71  *
72  * @param boundingBox Bounding box which is tested if has intersection
73  * @param searchedElements vector of ids of suspect elements
74  */
75  void find_bounding_box(const BoundingBox &boundingBox, std::vector<unsigned int> &result_list) const;
76 
77  /**
78  * Gets elements which can have intersection with point
79  *
80  * @param point Point which is tested if has intersection
81  * @param searchedElements vector of ids of suspect elements
82  */
83  void find_point(const Space<3>::Point &point, std::vector<unsigned int> &result_list) const;
84 
85  /**
86  * Get vector of mesh elements bounding boxes
87  *
88  * @return elements_ vector
89  */
91 
92 protected:
93  /// required reduction in size of box to allow further splitting
94  static const double size_reduce_factor;
95 
96  /// create bounding boxes of element
97  void element_boxes();
98 
99  /// split tree node given by node_idx, distribute elements to child nodes
100  void split_node(const BoundingBox &node_box, unsigned int node_idx);
101 
102  /// create child nodes of node given by node_idx
103  void make_node(const BoundingBox &box, unsigned int node_idx);
104 
105  /**
106  * For given node takes projection of centers of bounding boxes of its elements to axis given by
107  * @p node::axis()
108  * and estimate median of these values. That is optimal split point.
109  * Precise median is computed for sets smaller then @p max_median_sample_size
110  * estimate from random sample is used for larger sets.
111  */
112  double estimate_median(unsigned char axis, const BIHNode &node);
113 
114  /// mesh
116  /// vector of mesh elements bounding boxes
118  /// vector of tree nodes
120  /// Main bounding box.
122  /// Maximal number of elements stored in a leaf node of BIH tree.
123  unsigned int leaf_size_limit;
124  /// Maximal count of BIH tree levels
125  unsigned int max_n_levels;
126 
127  /// vector stored element indexes in leaf nodes
129  /// temporary vector stored values of coordinations for calculating median
131 
132  // random generator
133  std::mt19937 r_gen;
134 
135 };
136 
137 #endif /* BIH_TREE_HH_ */
static const double size_reduce_factor
required reduction in size of box to allow further splitting
Definition: bih_tree.hh:94
std::vector< BoundingBox > & get_elements()
Definition: bih_tree.hh:90
Bounding box in 3d ambient space.
Definition: bounding_box.hh:45
Mesh * mesh_
mesh
Definition: bih_tree.hh:115
unsigned int max_n_levels
Maximal count of BIH tree levels.
Definition: bih_tree.hh:125
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:62
std::vector< BoundingBox > elements_
vector of mesh elements bounding boxes
Definition: bih_tree.hh:117
double estimate_median(unsigned char axis, const BIHNode &node)
Definition: bih_tree.cc:151
Definition: mesh.h:97
void element_boxes()
create bounding boxes of element
Definition: bih_tree.cc:246
BoundingBox main_box_
Main bounding box.
Definition: bih_tree.hh:121
void make_node(const BoundingBox &box, unsigned int node_idx)
create child nodes of node given by node_idx
Definition: bih_tree.cc:114
unsigned int leaf_size_limit
Maximal number of elements stored in a leaf node of BIH tree.
Definition: bih_tree.hh:123
arma::vec::fixed< spacedim > Point
Definition: point.hh:33
static const unsigned int dimension
count of dimensions
Definition: bih_tree.hh:39
const BoundingBox & tree_box() const
Definition: bih_tree.cc:188
Class for O(log N) lookup for intersections with a set of bounding boxes.
Definition: bih_tree.hh:36
~BIHTree()
Definition: bih_tree.cc:57
BIHTree(Mesh *mesh, unsigned int soft_leaf_size_limit=20)
Definition: bih_tree.cc:31
std::vector< double > coors_
temporary vector stored values of coordinations for calculating median
Definition: bih_tree.hh:130
void find_point(const Space< 3 >::Point &point, std::vector< unsigned int > &result_list) const
Definition: bih_tree.cc:240
void find_bounding_box(const BoundingBox &boundingBox, std::vector< unsigned int > &result_list) const
Definition: bih_tree.cc:193
std::vector< unsigned int > in_leaves_
vector stored element indexes in leaf nodes
Definition: bih_tree.hh:128
std::mt19937 r_gen
Definition: bih_tree.hh:133
unsigned int get_element_count()
Definition: bih_tree.cc:183
static const unsigned int max_median_sample_size
max count of elements to estimate median - value must be even
Definition: bih_tree.hh:41
std::vector< BIHNode > nodes_
vector of tree nodes
Definition: bih_tree.hh:119