Flow123d  release_2.2.0-914-gf1a3a4f
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() const;
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 result_list vector of ids of suspect elements
74  * @param full_list put to result_list all suspect elements found in leaf node or add only those that has intersection with boundingBox
75  */
76  void find_bounding_box(const BoundingBox &boundingBox, std::vector<unsigned int> &result_list, bool full_list = false) const;
77 
78  /**
79  * Gets elements which can have intersection with point
80  *
81  * @param point Point which is tested if has intersection
82  * @param result_list vector of ids of suspect elements
83  * @param full_list put to result_list all suspect elements found in leaf node or add only those that has intersection with point
84  */
85  void find_point(const Space<3>::Point &point, std::vector<unsigned int> &result_list, bool full_list = false) const;
86 
87  /**
88  * Get vector of mesh elements bounding boxes
89  *
90  * @return elements_ vector
91  */
93 
94  /// Gets bounding box of element of given index @p ele_index.
95  const BoundingBox & ele_bounding_box(unsigned int ele_idx) const;
96 
97 protected:
98  /// required reduction in size of box to allow further splitting
99  static const double size_reduce_factor;
100 
101  /// create bounding boxes of element
102  //void element_boxes();
103 
104  /// split tree node given by node_idx, distribute elements to child nodes
105  void split_node(const BoundingBox &node_box, unsigned int node_idx);
106 
107  /// create child nodes of node given by node_idx
108  void make_node(const BoundingBox &box, unsigned int node_idx);
109 
110  /**
111  * For given node takes projection of centers of bounding boxes of its elements to axis given by
112  * @p node::axis()
113  * and estimate median of these values. That is optimal split point.
114  * Precise median is computed for sets smaller then @p max_median_sample_size
115  * estimate from random sample is used for larger sets.
116  */
117  double estimate_median(unsigned char axis, const BIHNode &node);
118 
119  /// mesh
121  /// vector of mesh elements bounding boxes (from mesh)
123  /// Main bounding box. (from mesh)
125 
126  /// vector of tree nodes
128  /// Maximal number of elements stored in a leaf node of BIH tree.
129  unsigned int leaf_size_limit;
130  /// Maximal count of BIH tree levels
131  unsigned int max_n_levels;
132 
133  /// vector stored element indexes in leaf nodes
135  /// temporary vector stored values of coordinations for calculating median
137 
138  // random generator
139  std::mt19937 r_gen;
140 
141 
142 };
143 
144 #endif /* BIH_TREE_HH_ */
static const double size_reduce_factor
required reduction in size of box to allow further splitting
Definition: bih_tree.hh:99
std::vector< BoundingBox > & get_elements()
Definition: bih_tree.hh:92
Bounding box in 3d ambient space.
Definition: bounding_box.hh:45
Mesh * mesh_
mesh
Definition: bih_tree.hh:120
unsigned int max_n_levels
Maximal count of BIH tree levels.
Definition: bih_tree.hh:131
void split_node(const BoundingBox &node_box, unsigned int node_idx)
create bounding boxes of element
Definition: bih_tree.cc:64
BoundingBox & main_box_
Main bounding box. (from mesh)
Definition: bih_tree.hh:124
double estimate_median(unsigned char axis, const BIHNode &node)
Definition: bih_tree.cc:166
Definition: mesh.h:99
std::vector< BoundingBox > & elements_
vector of mesh elements bounding boxes (from mesh)
Definition: bih_tree.hh:122
void make_node(const BoundingBox &box, unsigned int node_idx)
create child nodes of node given by node_idx
Definition: bih_tree.cc:119
void find_bounding_box(const BoundingBox &boundingBox, std::vector< unsigned int > &result_list, bool full_list=false) const
Definition: bih_tree.cc:215
unsigned int leaf_size_limit
Maximal number of elements stored in a leaf node of BIH tree.
Definition: bih_tree.hh:129
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:210
Class for O(log N) lookup for intersections with a set of bounding boxes.
Definition: bih_tree.hh:36
~BIHTree()
Definition: bih_tree.cc:53
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:136
std::vector< unsigned int > in_leaves_
vector stored element indexes in leaf nodes
Definition: bih_tree.hh:134
unsigned int get_element_count() const
Definition: bih_tree.cc:205
void find_point(const Space< 3 >::Point &point, std::vector< unsigned int > &result_list, bool full_list=false) const
Definition: bih_tree.cc:265
std::mt19937 r_gen
Definition: bih_tree.hh:139
static const unsigned int max_median_sample_size
max count of elements to estimate median - value must be even
Definition: bih_tree.hh:41
const BoundingBox & ele_bounding_box(unsigned int ele_idx) const
Gets bounding box of element of given index ele_index.
Definition: bih_tree.cc:57
std::vector< BIHNode > nodes_
vector of tree nodes
Definition: bih_tree.hh:127