ug4
ntree.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 #ifndef __H__UG__ntree__
34 #define __H__UG__ntree__
35 
36 #include "ntree_iterator.h"
37 
38 namespace ug{
39 
47 template <int tree_dim, int world_dim, class elem_t, class common_data_t>
49 {
50  typedef int real_t;
51  typedef int vector_t;
52  typedef int box_t;
53 
54  static void calculate_center(vector_t& centerOut, const elem_t& e,
55  const common_data_t& commonData);
56 
57  static void calculate_bounding_box(box_t& boxOut, const elem_t& e,
58  const common_data_t& commonData);
59 
61  static void grow_box(box_t& boxOut, const box_t& box,
62  const vector_t& offset);
63 
64  static vector_t box_diagonal(const box_t& box);
65 
66  static bool box_contains_point(const box_t& box, const vector_t& point);
67 
69  static bool box_box_intersection(const box_t& box1, const box_t& box2);
70 
72  static void merge_boxes(box_t& boxOut, const box_t& box1, const box_t& box2);
73 
75 
78  static void split_box(box_t* boxesOut, const box_t& box, const vector_t& splitPoint);
79 
82  static bool contains_point(const elem_t& e, const vector_t& point,
83  const common_data_t& commonData);
84 //
86 // real_t distance_point_to_entry(const elem_t& e, const vector_t& point,
87 // const elem_data_t& elemData,
88 // const common_data_t& commonData);
89 //
91 // bool ray_intersects_entry(real_t distOut, const elem_t& elem,
92 // const vector_t& rayFrom, const vector_t& rayTo,
93 // const elem_data_t& elemData,
94 // const common_data_t& commonData);
95 };
96 
97 
98 
99 
100 struct NTreeDesc{
101  size_t maxDepth;
103  //todo: split-strategy (center-of-space, center-of-mass)
104 
106 };
107 
108 
110 
161 template <int tree_dim, int world_dim, class TElem, class TCommonData>
162 class ntree
163 {
164  private:
165  struct Entry;
166 
167  public:
168  typedef TElem elem_t;
169  typedef TCommonData common_data_t;
171  typedef typename traits::real_t real_t;
172  typedef typename traits::vector_t vector_t;
173  typedef typename traits::box_t box_t;
175 
176  ntree();
177 
178  void clear();
179 
181 
183  void enable_warnings(bool enable) {m_warningsEnabled = enable;}
184  bool warnings_enabled () const {return m_warningsEnabled;}
185 
187 
190  void set_desc(const NTreeDesc& desc);
191 
193  const NTreeDesc& desc() const;
194 
196  void set_common_data(const common_data_t& commonData);
197 
199  const common_data_t& common_data() const;
200 
202  bool empty() const;
203 
205  size_t size() const;
206 
208 
209  size_t num_delayed_elements() const;
210 
212 
215  void add_element(const elem_t& elem);
216 
218 
219  void rebalance();
220 
222  size_t num_nodes() const;
223 
225  size_t num_child_nodes(size_t nodeId) const;
226 
228 
229  const size_t* child_node_ids(size_t nodeId) const;
230 
232  elem_iterator_t elems_begin(size_t nodeId) const;
233 
235 
236  elem_iterator_t elems_end(size_t nodeId) const;
237 
239  size_t num_elements(size_t nodeId) const;
240 
242  size_t level(size_t nodeId) const;
243 
245  const box_t& bounding_box(size_t nodeId) const;
246 
247  private:
248 
250  void clear_nodes ();
251 
253  template <size_t n, size_t exponent>
254  struct pow;
255 
256  template <size_t n>
257  struct pow<n, 0> {static const size_t val = 1;};
258 
259  template <size_t n, size_t exponent>
260  struct pow {static const size_t val = n * pow<n, exponent - 1>::val;};
261 
263  static const size_t s_numChildren = pow<2, tree_dim>::val;
264 
266  static const size_t s_invalidIndex = -1;
267 
268 
270 
273  struct Entry{
277  size_t nextEntryInd;
278 
279  Entry(const elem_t& e) :
281  };
282 
283 
286  struct Node{
288  size_t firstEntryInd;
289  size_t lastEntryInd;
290  size_t numEntries;
291  size_t level;
294 
297  {
298  tightBox = looseBox;
299  for(size_t i = 0; i < s_numChildren; ++i)
301  }
302 
306  {
307  for(size_t i = 0; i < s_numChildren; ++i)
308  childNodeInd[i] = n.childNodeInd[i];
309  }
310  };
311 
313 
316  void split_leaf_node(size_t nodeIndex);
317 
319 
321  size_t find_leaf_node(const vector_t& point, size_t curNode = 0);
322 
324  void add_entry_to_node(Node& node, size_t entryInd);
325 
327  void update_loose_bounding_box(Node& node);
328 
331 
334  std::vector<Node> m_nodes;
335  std::vector<Entry> m_entries;
338 };
339 
340 
341 }// end of namespace
342 
343 
345 // include implementation
346 #include "ntree_impl.hpp"
347 
348 #endif
this iterator is used by the ntree class to provide access to the elements of a given node
Definition: ntree_iterator.h:43
The n-tree class can be used to construct space partitioning trees of dimensions 1,...
Definition: ntree.h:163
static const size_t s_invalidIndex
marks an index as invalid
Definition: ntree.h:266
std::vector< Entry > m_entries
Definition: ntree.h:335
void add_entry_to_node(Node &node, size_t entryInd)
adds an entry to the given node
Definition: ntree_impl.hpp:297
traits::vector_t vector_t
Definition: ntree.h:172
size_t num_elements(size_t nodeId) const
returns the number of elements that the given node contains
Definition: ntree_impl.hpp:417
size_t num_delayed_elements() const
returns the number of elements which have been added but are not yet accessible in the tree.
Definition: ntree_impl.hpp:116
void add_element(const elem_t &elem)
adds an element to the tree.
Definition: ntree_impl.hpp:124
elem_iterator_t elems_end(size_t nodeId) const
returns an iterator to the end of the element-sequence of a given node
Definition: ntree_impl.hpp:409
void enable_warnings(bool enable)
enabled or disable warning messages
Definition: ntree.h:183
void clear()
Definition: ntree_impl.hpp:62
const NTreeDesc & desc() const
returns the balancing-parameters of the tree.
Definition: ntree_impl.hpp:79
void split_leaf_node(size_t nodeIndex)
splits a node into 2^tree_dim child nodes and assigns entries to those children.
Definition: ntree_impl.hpp:188
size_t num_child_nodes(size_t nodeId) const
returns the number of children of a node
Definition: ntree_impl.hpp:376
size_t num_nodes() const
returns the total number of nodes in the tree
Definition: ntree_impl.hpp:368
size_t size() const
returns the number of entries in the tree (delayed entries excluded)
Definition: ntree_impl.hpp:108
bool m_warningsEnabled
Definition: ntree.h:337
NTreeDesc m_desc
Definition: ntree.h:332
ntree_traits< tree_dim, world_dim, elem_t, common_data_t > traits
Definition: ntree.h:170
bool warnings_enabled() const
Definition: ntree.h:184
void clear_nodes()
clear only the nodes
Definition: ntree_impl.hpp:52
TCommonData common_data_t
Definition: ntree.h:169
TElem elem_t
Definition: ntree.h:165
bool empty() const
returns true if the tree is empty
Definition: ntree_impl.hpp:100
common_data_t m_commonData
Definition: ntree.h:333
ntree()
Definition: ntree_impl.hpp:43
vector_t calculate_center_of_mass(Node &node)
calculates the center of mass of a given node
Definition: ntree_impl.hpp:345
const box_t & bounding_box(size_t nodeId) const
returns the smallest box which contains all elements of the given node
Definition: ntree_impl.hpp:434
void rebalance()
rebalances the whole tree
Definition: ntree_impl.hpp:161
const common_data_t & common_data() const
returns the common-data stored in the tree
Definition: ntree_impl.hpp:93
size_t level(size_t nodeId) const
returns the number tree-level in which the node is located
Definition: ntree_impl.hpp:425
size_t find_leaf_node(const vector_t &point, size_t curNode=0)
returns an index to the leaf-node which contains the given point
Definition: ntree_impl.hpp:276
size_t m_numDelayedElements
Definition: ntree.h:336
void update_loose_bounding_box(Node &node)
updates the loose bounding box of the given node
Definition: ntree_impl.hpp:315
static const size_t s_numChildren
the number of children each non-leaf node has
Definition: ntree.h:263
const_ntree_element_iterator< elem_t, Entry > elem_iterator_t
Definition: ntree.h:174
traits::box_t box_t
Definition: ntree.h:173
void set_desc(const NTreeDesc &desc)
sets the balancing-parameters of the tree.
Definition: ntree_impl.hpp:72
elem_iterator_t elems_begin(size_t nodeId) const
returns an iterator to the first element of a given node
Definition: ntree_impl.hpp:397
void set_common_data(const common_data_t &commonData)
sets the common-data which the tree passes on to callback methods
Definition: ntree_impl.hpp:86
const size_t * child_node_ids(size_t nodeId) const
returns an array of child-id's for the given node
Definition: ntree_impl.hpp:387
traits::real_t real_t
Definition: ntree.h:171
std::vector< Node > m_nodes
m_nodes[0] is always considered to be the root node.
Definition: ntree.h:334
the ug namespace
Definition: ntree.h:100
size_t maxDepth
Definition: ntree.h:101
NTreeDesc()
Definition: ntree.h:105
size_t splitThreshold
Definition: ntree.h:102
An Entry combines an element with the index to the next entry in a leaf-node's entry list.
Definition: ntree.h:273
Entry(const elem_t &e)
Definition: ntree.h:279
elem_t elem
Definition: ntree.h:274
size_t nextEntryInd
Definition: ntree.h:277
Definition: ntree.h:286
size_t numEntries
number of entries in the node
Definition: ntree.h:290
Node(const Node &n)
Definition: ntree.h:303
size_t lastEntryInd
index into m_entries. s_invalidIndex: no entry
Definition: ntree.h:289
box_t tightBox
tight bounding box - disjunct partition of the root box
Definition: ntree.h:292
box_t looseBox
loose bounding box - contains all bounding boxes of its entries
Definition: ntree.h:293
Node()
Definition: ntree.h:295
size_t firstEntryInd
< index into m_nodes. s_invalidIndex: no child node.
Definition: ntree.h:288
size_t level
Definition: ntree.h:291
size_t childNodeInd[s_numChildren]
Definition: ntree.h:287
static template implementation to raise n to the power exponent
Definition: ntree.h:260
static const size_t val
Definition: ntree.h:260
Definition: ntree.h:49
static void calculate_bounding_box(box_t &boxOut, const elem_t &e, const common_data_t &commonData)
static void calculate_center(vector_t &centerOut, const elem_t &e, const common_data_t &commonData)
static bool box_box_intersection(const box_t &box1, const box_t &box2)
returns true if the given boxes intersect
static void merge_boxes(box_t &boxOut, const box_t &box1, const box_t &box2)
returns the smallest box that contains both box1 and box2
static bool box_contains_point(const box_t &box, const vector_t &point)
int real_t
Definition: ntree.h:50
static vector_t box_diagonal(const box_t &box)
int vector_t
Definition: ntree.h:51
int box_t
Definition: ntree.h:52
static void split_box(box_t *boxesOut, const box_t &box, const vector_t &splitPoint)
splits the given box into (2^tree_dim sub-boxes).
static void grow_box(box_t &boxOut, const box_t &box, const vector_t &offset)
adds the given offset to box.max and subtracts it from box.min
static bool contains_point(const elem_t &e, const vector_t &point, const common_data_t &commonData)