GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
DynamicAABBTree.hpp
Go to the documentation of this file.
1 // This file is part of GranOO, a workbench for DEM simulation.
2 //
3 // Author(s) : - Damien Andre IRCER/UNILIM, Limoges France
4 // <damien.andre@unilim.fr>
5 // - Jean-luc Charles Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
6 // <jean-luc.charles@ensam.eu>
7 // - Jeremie Girardot Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
8 // <jeremie.girardot@ensam.eu>
9 // - Cedric Hubert LAMIH/UPHF, Valenciennes France
10 // <cedric.hubert@uphf.fr>
11 // - Ivan Iordanoff Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
12 // <ivan.iordanoff@ensam.eu>
13 //
14 // Copyright (C) 2008-2019 D. Andre, JL. Charles, J. Girardot, C. Hubert, I. Iordanoff
15 //
16 // This program is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 // This program is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with this program. If not, see <http://www.gnu.org/licenses/>.
28 
29 #ifndef DynamicAABBTree_hpp
30 #define DynamicAABBTree_hpp
31 
32 #include <vector> // std::vector
33 #include <map> // std::map
34 
35 #include "GranOO3/Algo/StaticAABBTree.hpp" // AABB trees traits
36 
38 
39 public:
44 
49 
50 public:
69  static AABB new_lonely_aabb(double minPoint[3], double maxPoint[3], double fatness, unsigned int primtiveIndex = -1);
70 
83  static AABB merged_lonely_aabb(AABB aabb1, AABB aabb2, unsigned int primtiveIndex = -1);
84 
85 public:
91  void insert_node(AABB anAABB);
92 
102  void set_fatness(double fatness) {
103  _fatness = fatness;
104  };
105 
115  bool update_primitive(unsigned int primitiveIndex, AABB tightAABB);
116 
122  void remove_primitive(unsigned int primitiveIndex);
123 
130  void clear();
131 
135  const unsigned int node_number() const {
136  return _node_count;
137  };
138 
142  const AABBNode node_at_index(unsigned int nodeIndex) const {
143  return _nodes[nodeIndex];
144  };
145 
149  const unsigned int aabb_number() const {
150  return _aabbsCount;
151  };
152 
156  const AABB aabb_at_index(unsigned int aabbIndex) const {
157  return _aabbs[aabbIndex];
158  };
159 
163  const bool is_node_leaf(unsigned int nodeIndex) const {
164  return _nodes[nodeIndex].leftNode == NotInList && _nodes[nodeIndex].leftNode == NotInList;
165  };
166 
170  const int NodeBalance(unsigned int nodeIndex) const {
171  return _nodes[_nodes[nodeIndex].leftNode].height-_nodes[_nodes[nodeIndex].rightNode].height;
172  };
173 
177  const bool Primitiveexists(unsigned int primitiveIndex) const;
178 
182  void PrimitivesList(std::vector<unsigned int> &primitivesList) const;
183 
192  void collidersPairs(std::vector<std::pair<unsigned int, unsigned int> > &collidersPairs) const;
193 
202  void collidersList(unsigned int primitiveIndex, std::vector<unsigned int> &collidersList) const;
203 
204  /* ******************************************** */
205  /* DEBUGGING METHODS */
206  /* ******************************************** */
207  void printAABBNodes();
208  void saveAABBs();
209  void saveAABBs(std::vector<unsigned int> primitivesList);
210 
211 private:
219  unsigned int new_aabb_node();
220 
228  unsigned int new_aabb_node(AABB anAABB);
229 
240  unsigned int new_aabb(double minPoint[3], double maxPoint[3], unsigned int primitiveIndex = -1);
241 
249  unsigned int new_aabb(AABB anAABB);
250 
256  void remove_node(unsigned int leafNode);
257 
264  void delete_node(unsigned int aNodeIndex);
265 
272  void delete_aabb(unsigned int anAABBIndex);
273 
282  unsigned int balance_node(unsigned int rootNodeIndex);
283 
292  unsigned int rotate_left(unsigned int rootNodeIndex);
293 
302  unsigned int rotate_right(unsigned int rootNodeIndex);
303 
310  void update_node_aabb(unsigned int aNode);
311 
318  void update_node_height(unsigned int aNode);
319 
326  const bool aabb_do_overlap(unsigned int aabb1, unsigned int aabb2) const;
327 
334  const bool is_aabb_contain_aabb(unsigned int anAABBIndex, AABB anAABB) const;
335 
336 private:
340  static const unsigned int NotInList = (unsigned int)(-1);
341 
342 private:
346  double _fatness;
347 
351  unsigned int _aabbsCount;
352 
356  unsigned int _node_count;
357 
361  unsigned int _rootNodeIndex;
362 
366  unsigned int _firstFreeNodeIndex;
367 
371  unsigned int _firstFreeAABBIndex;
372 
376  std::vector<unsigned int> _primitivesToNode;
377 
381  std::vector<AABB> _aabbs;
382 
386  std::vector<AABBNode> _nodes;
387 };
388 
389 
390 #endif /* DynamicAABBTree_hpp */
Definition: DynamicAABBTree.hpp:37
std::vector< AABBNode > _nodes
The leaves AABB nodes container.
Definition: DynamicAABBTree.hpp:386
const unsigned int node_number() const
Definition: DynamicAABBTree.hpp:135
void clear()
Clears the AABB tree.
Definition: DynamicAABBTree.cpp:487
void saveAABBs()
Definition: DynamicAABBTree.cpp:911
void delete_aabb(unsigned int anAABBIndex)
Deletes an AABB and updates the first available space in the AABBs list.
Definition: DynamicAABBTree.cpp:263
unsigned int rotate_left(unsigned int rootNodeIndex)
Performs a left-side rotation of a sub tree rooted at rootNodeIndex.
Definition: DynamicAABBTree.cpp:341
const AABBNode node_at_index(unsigned int nodeIndex) const
Definition: DynamicAABBTree.hpp:142
void update_node_aabb(unsigned int aNode)
Updates the dimensions of a node aabb, according to its childen.
Definition: DynamicAABBTree.cpp:435
void remove_node(unsigned int leafNode)
Removes a leaf node from the AABB tree.
Definition: DynamicAABBTree.cpp:173
bool update_primitive(unsigned int primitiveIndex, AABB tightAABB)
Updates the primitive in case it is out of its bounding box.
Definition: DynamicAABBTree.cpp:456
unsigned int _aabbsCount
The current number of AABBs in the tree.
Definition: DynamicAABBTree.hpp:351
void collidersPairs(std::vector< std::pair< unsigned int, unsigned int > > &collidersPairs) const
Queries the AABB tree to find all AABBs overlapping themselves.
Definition: DynamicAABBTree.cpp:585
static AABB merged_lonely_aabb(AABB aabb1, AABB aabb2, unsigned int primtiveIndex=-1)
Creates a new AABB which is a combination of two AABB.
Definition: DynamicAABBTree.cpp:784
DynamicAABBTree()
instanciates a new empty DynamicAABBTree object.
Definition: DynamicAABBTree.cpp:41
void insert_node(AABB anAABB)
Inserts a new AABB in the AABB tree.
Definition: DynamicAABBTree.cpp:56
std::vector< AABB > _aabbs
The AABBs container.
Definition: DynamicAABBTree.hpp:381
void set_fatness(double fatness)
Allows to define the expansion factor for fat AABBs.
Definition: DynamicAABBTree.hpp:102
unsigned int balance_node(unsigned int rootNodeIndex)
Balances a node according to its height.
Definition: DynamicAABBTree.cpp:287
const bool aabb_do_overlap(unsigned int aabb1, unsigned int aabb2) const
Checks if two AABBs overlap themselves.
Definition: DynamicAABBTree.cpp:837
const AABB aabb_at_index(unsigned int aabbIndex) const
Definition: DynamicAABBTree.hpp:156
unsigned int _firstFreeNodeIndex
The index of the first free node in the _nodes list.
Definition: DynamicAABBTree.hpp:366
void delete_node(unsigned int aNodeIndex)
Deletes a node and updates the first available space in the AABB nodes list.
Definition: DynamicAABBTree.cpp:235
unsigned int _firstFreeAABBIndex
The index of the first free aabb in the _aabbs list.
Definition: DynamicAABBTree.hpp:371
unsigned int _node_count
The current number of AABB nodes in the tree.
Definition: DynamicAABBTree.hpp:356
void printAABBNodes()
Definition: DynamicAABBTree.cpp:873
void remove_primitive(unsigned int primitiveIndex)
Removes an AABB from the AABB tree.
Definition: DynamicAABBTree.cpp:160
static AABB new_lonely_aabb(double minPoint[3], double maxPoint[3], double fatness, unsigned int primtiveIndex=-1)
Creates a new 'lonely' AABB with given extreme points.
Definition: DynamicAABBTree.cpp:749
~DynamicAABBTree()
Default destructor.
Definition: DynamicAABBTree.cpp:49
const int NodeBalance(unsigned int nodeIndex) const
Definition: DynamicAABBTree.hpp:170
unsigned int new_aabb(double minPoint[3], double maxPoint[3], unsigned int primitiveIndex=-1)
Creates a new AABB with given extreme points and, optionally, the primitive index.
Definition: DynamicAABBTree.cpp:795
std::vector< unsigned int > _primitivesToNode
stores the relationship from primitives to nodes indices.
Definition: DynamicAABBTree.hpp:376
void update_node_height(unsigned int aNode)
Updates the height of a node, according to its childen.
Definition: DynamicAABBTree.cpp:447
static const unsigned int NotInList
A flag to determine if an index is in a list.
Definition: DynamicAABBTree.hpp:340
double _fatness
The factor by which the AABBs dimensions are expanded.
Definition: DynamicAABBTree.hpp:346
const unsigned int aabb_number() const
Definition: DynamicAABBTree.hpp:149
void PrimitivesList(std::vector< unsigned int > &primitivesList) const
Populates the primitivesList vector with indices of all primitives contained in the tree.
Definition: DynamicAABBTree.cpp:515
const bool Primitiveexists(unsigned int primitiveIndex) const
Definition: DynamicAABBTree.cpp:499
const bool is_aabb_contain_aabb(unsigned int anAABBIndex, AABB anAABB) const
Checks if an AABB is contained inside another one.
Definition: DynamicAABBTree.cpp:853
unsigned int new_aabb_node()
Creates a new AABBNode.
Definition: DynamicAABBTree.cpp:703
void collidersList(unsigned int primitiveIndex, std::vector< unsigned int > &collidersList) const
Queries the AABB tree to find all AABBs overlapping with the AABB that stores a given primitive.
Definition: DynamicAABBTree.cpp:528
const bool is_node_leaf(unsigned int nodeIndex) const
Definition: DynamicAABBTree.hpp:163
unsigned int _rootNodeIndex
The root node of the AABB tree, necessary when the tree is dynamic.
Definition: DynamicAABBTree.hpp:361
unsigned int rotate_right(unsigned int rootNodeIndex)
Performs a right-side rotation of a sub tree rooted at rootNodeIndex.
Definition: DynamicAABBTree.cpp:388
Definition: StaticAABBTree.hpp:38
Definition: StaticAABBTree.hpp:54