ug4
kd_tree_static.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-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 #ifndef __H__LIB_GRID__KD_TREE__
34 #define __H__LIB_GRID__KD_TREE__
35 
36 #include "lib_grid/grid/grid.h"
37 
38 namespace ug
39 {
40 
43 
45 //KDTREE
46 
50 {
51  public:
53  KDVertexDistance(Vertex* vrt, float nDistSQ) : vertex(vrt), distSQ(nDistSQ) {}
54 
57 };
58 
62 {
65 };
66 
67 typedef std::list<KDVertexDistance> KDVertexDistanceList;
68 
71 // KDTreeStatic
73 
83 template <class TPositionAttachment, int numDimensions = 3, class TVector = vector3 >
85 {
86  public:
87  typedef std::vector<Vertex*> VertexVec;
88 
89  class Node
90  {
91  public:
92  Node() : m_pvVertices(NULL) {m_pChild[0] = m_pChild[1] = NULL;}
93  ~Node() {clear();}
94 
95  void clear();
96 
97  Node* m_pChild[2]; // 0: pos, 1: neg
101  };
102 
103  // the functions
104  KDTreeStatic() : m_pGrid(NULL) {};
105 
106  void clear();
107 
108  template <class TVrtIterator>
109  bool create_from_grid(Grid& grid, TVrtIterator vrtsBegin, TVrtIterator vrtsEnd,
110  TPositionAttachment& aPos, int maxTreeDepth, int splitThreshold,
111  KDSplitDimension splitDimension = KDSD_LARGEST);
112 
113  template <class TVrtIterator>
114  bool create_from_grid(Grid& grid, TVrtIterator vrtsBegin, TVrtIterator vrtsEnd,
116  int maxTreeDepth, int splitThreshold,
117  KDSplitDimension splitDimension = KDSD_LARGEST);
118 
119  bool get_neighbourhood(std::vector<Vertex*>& vrtsOut,
120  typename TPositionAttachment::ValueType& pos, int numClosest);
121 
122  bool get_points_in_box(std::vector<Vertex*>& vrtsOut,
123  const TVector& boxMin, const TVector& boxMax);
124 
125  Node* get_root() {return &m_parentNode;}
126 
127  void get_leafs(std::vector<Node*>& vLeafsOut);
128 
129  protected:
130  bool get_points_in_box(std::vector<Vertex*>& vrtsOut, Node* pNode,
131  const TVector& boxMin, const TVector& boxMax);
132 
133  void neighbourhood(KDVertexDistanceList& vrtsOut, Node* pNode, TVector& pos, int numClosest);
134 
135  template <class TVertexIterator>
136  bool create_barycentric(TVertexIterator vrts_begin, TVertexIterator vrts_end,
137  int numVertices, Node* pNode, int actDimension, int maxTreeDepth);
138 
139  template <class TVertexIterator>
140  int get_largest_dimension(TVertexIterator vrts_begin, TVertexIterator vrts_end);
141 
142  template <class TVertexIterator>
143  int get_next_split_dimension(int actSplitDimension, TVertexIterator vrts_begin,
144  TVertexIterator vrts_end);
145 
146  void get_leafs_recursive(std::vector<Node*>& vLeafsOut, Node* pNode);
147 
148  // members
153  KDSplitDimension m_splitDimension; // how is the next split dimension choosen?
154 
155  // some helper vars for neighbourhood search
157  float m_maxDistSQ;
158 };
159 
161 
162 }// end of namespace
163 
165 // include implementation
166 #include "kd_tree_static_impl.hpp"
167 
168 #endif
Manages the elements of a grid and their interconnection.
Definition: grid.h:132
Definition: kd_tree_static.h:90
float m_fSplitValue
Definition: kd_tree_static.h:98
int m_iSplitDimension
Definition: kd_tree_static.h:99
~Node()
Definition: kd_tree_static.h:93
void clear()
Definition: kd_tree_static_impl.hpp:50
Node * m_pChild[2]
Definition: kd_tree_static.h:97
VertexVec * m_pvVertices
Definition: kd_tree_static.h:100
Node()
Definition: kd_tree_static.h:92
organizes vertices in a binary-tree structure. Only for static use!
Definition: kd_tree_static.h:85
Node * get_root()
Definition: kd_tree_static.h:125
void clear()
Definition: kd_tree_static_impl.hpp:65
void get_leafs(std::vector< Node * > &vLeafsOut)
Definition: kd_tree_static_impl.hpp:128
void get_leafs_recursive(std::vector< Node * > &vLeafsOut, Node *pNode)
Definition: kd_tree_static_impl.hpp:404
int m_iSplitThreshold
Definition: kd_tree_static.h:151
Grid * m_pGrid
Definition: kd_tree_static.h:149
bool create_barycentric(TVertexIterator vrts_begin, TVertexIterator vrts_end, int numVertices, Node *pNode, int actDimension, int maxTreeDepth)
Definition: kd_tree_static_impl.hpp:278
KDSplitDimension m_splitDimension
Definition: kd_tree_static.h:153
float m_maxDistSQ
Definition: kd_tree_static.h:157
std::vector< Vertex * > VertexVec
Definition: kd_tree_static.h:87
bool get_neighbourhood(std::vector< Vertex * > &vrtsOut, typename TPositionAttachment::ValueType &pos, int numClosest)
Definition: kd_tree_static_impl.hpp:104
Grid::VertexAttachmentAccessor< TPositionAttachment > m_aaPos
Definition: kd_tree_static.h:150
bool get_points_in_box(std::vector< Vertex * > &vrtsOut, const TVector &boxMin, const TVector &boxMax)
Definition: kd_tree_static_impl.hpp:119
void neighbourhood(KDVertexDistanceList &vrtsOut, Node *pNode, TVector &pos, int numClosest)
Definition: kd_tree_static_impl.hpp:184
bool create_from_grid(Grid &grid, TVrtIterator vrtsBegin, TVrtIterator vrtsEnd, TPositionAttachment &aPos, int maxTreeDepth, int splitThreshold, KDSplitDimension splitDimension=KDSD_LARGEST)
Definition: kd_tree_static_impl.hpp:75
Node m_parentNode
Definition: kd_tree_static.h:152
int m_numNeighboursFound
Definition: kd_tree_static.h:156
KDTreeStatic()
Definition: kd_tree_static.h:104
int get_next_split_dimension(int actSplitDimension, TVertexIterator vrts_begin, TVertexIterator vrts_end)
Definition: kd_tree_static_impl.hpp:385
int get_largest_dimension(TVertexIterator vrts_begin, TVertexIterator vrts_end)
Definition: kd_tree_static_impl.hpp:340
used by KDTreeStatic
Definition: kd_tree_static.h:50
KDVertexDistance()
Definition: kd_tree_static.h:52
number distSQ
Definition: kd_tree_static.h:56
Vertex * vertex
Definition: kd_tree_static.h:55
KDVertexDistance(Vertex *vrt, float nDistSQ)
Definition: kd_tree_static.h:53
Base-class for all vertex-types.
Definition: grid_base_objects.h:231
std::list< KDVertexDistance > KDVertexDistanceList
Definition: kd_tree_static.h:67
KDSplitDimension
used by KDTreeStatic
Definition: kd_tree_static.h:62
@ KDSD_LARGEST
Definition: kd_tree_static.h:64
@ KDSD_CIRCULAR
Definition: kd_tree_static.h:63
double number
Definition: types.h:124
the ug namespace