ug4
kd_tree.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2015: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 
34 #ifndef __H__UG__kd_tree__
35 #define __H__UG__kd_tree__
36 
37 namespace ug{
38 
40 {
43 };
44 
45 struct KDTreeDesc{
47  KDTreeDesc(int nDim, int nSplitThreshold, KDTreeSplitStrategy nStrategy) :
48  dim(nDim),
49  splitThreshold(nSplitThreshold),
50  strategy(nStrategy) {}
51 
52  int dim;
55 };
56 
59 template <class point_t, class data_t, class real_t = float>
60 class KDTree
61 {
62  public:
64  class Element{
65  friend class KDTree;
66  public:
67  point_t point;
68  data_t data;
69 
70  private:
74  };
75 
76 
78  void set_desc(const KDTreeDesc& desc);
79 
81  bool empty() const;
82 
84  size_t size() const;
85 
87 
88  size_t num_delayed_elements() const;
89 
91 
98  void add_element(const point_t& point, const data_t& data, bool delayInsertion = false);
99 
101 
103  Element closest_element(const point_t& point) const;
104 
106 
107  void closest_elements(vector<Element>& elemsOut, const point_t& point,
108  size_t n) const;
109 
111 
115 
117  void rebalance();
118 
119  private:
122  struct Node{
123  int childNodeInds[2];
127  float splitValue;
128  int splitDim;
129  };
130 
132 
135  void split_leaf_node(Node& node);
136 
138  int find_leaf_node(const point_t& point);
139 
140 
143  std::vector<Node> m_nodes;
144  std::vector<Element> m_elements;
146 };
147 
148 }// end of namespace
149 
150 #endif
An Element represents a point with an associated data value.
Definition: kd_tree.h:64
int nextElemInd
Definition: kd_tree.h:73
point_t point
Definition: kd_tree.h:67
data_t data
Definition: kd_tree.h:68
Definition: kd_tree.h:61
size_t num_delayed_elements() const
returns the number of elements which are scheduled for delayed insertion
Element closest_element(const point_t &point) const
finds the closest tree-element to a given point in space
real_t estimate_balance_quality()
estimates the balance quality of the tree in the range [0, 1].
std::vector< Element > m_elements
Definition: kd_tree.h:144
void add_element(const point_t &point, const data_t &data, bool delayInsertion=false)
adds an element to the tree.
bool empty() const
returns true if the tree is empty
void rebalance()
rebalances the whole tree
size_t size() const
returns the number of elements in the tree
size_t m_numDelayedElements
Definition: kd_tree.h:145
KDTreeDesc m_desc
Definition: kd_tree.h:141
void closest_elements(vector< Element > &elemsOut, const point_t &point, size_t n) const
returns the n closest tree-elements to a given point in space
int find_leaf_node(const point_t &point)
returns an index to the leaf-node which contains the given point
std::vector< Node > m_nodes
m_nodes[0] is always considered to be the root node.
Definition: kd_tree.h:143
void set_desc(const KDTreeDesc &desc)
sets the balancing-parameters of the tree. Triggers a rebalance if a value changed.
void split_leaf_node(Node &node)
splits a node into two child nodes and assigns elements to those children.
the ug namespace
KDTreeSplitStrategy
Definition: kd_tree.h:40
@ KDTSS_CIRCULAR
Definition: kd_tree.h:41
@ KDTSS_LARGEST
Definition: kd_tree.h:42
Definition: kd_tree.h:122
float splitValue
Definition: kd_tree.h:127
int firstElemInd
< index into m_nodes. -1: no child node.
Definition: kd_tree.h:124
int numElements
number of elements in the node
Definition: kd_tree.h:126
int childNodeInds[2]
Definition: kd_tree.h:123
int splitDim
Definition: kd_tree.h:128
int lastElemInd
index into m_elements. -1: no element
Definition: kd_tree.h:125
Definition: kd_tree.h:45
int splitThreshold
Definition: kd_tree.h:53
KDTreeSplitStrategy strategy
Definition: kd_tree.h:54
KDTreeDesc(int nDim, int nSplitThreshold, KDTreeSplitStrategy nStrategy)
Definition: kd_tree.h:47
int dim
Definition: kd_tree.h:52
KDTreeDesc()
Definition: kd_tree.h:46