GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
StaticAABBTree.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 StaticAABBTree_hpp
30 #define StaticAABBTree_hpp
31 
32 #include <vector> // std::vector
33 #include <functional> // std::function
34 
35 // AABB structure (used by both Static and Dynamic AABB trees)
36 // -----------------------------------------------------------
37 struct AABB
38 {
39 
40  double minPoint[3];
41  double midPoint[3];
42  double maxPoint[3];
43  unsigned int primitive = -1;
44  unsigned int aabbNode = -1;
45  unsigned int nextFree = -1;
46  double volume;
47  double surface;
48  bool isActive = false;
49 };
50 
51 // AABBNode structure (used by both Static and Dynamic AABB trees)
52 // ---------------------------------------------------------------
53 struct AABBNode
54 {
55 
56  double location[3];
57  unsigned int aabb = -1;
58  int height = 0;
59  unsigned int depth = 0;
60  unsigned int parentNode = -1;
61  unsigned int leftNode = -1;
62  unsigned int rightNode = -1;
63  unsigned int nextFree = -1;
64  bool isActive = false;
65  bool isCrossed = false;
66 };
67 
68 // simple Ray structure, to perform ray casting
69 // --------------------------------------------
70 struct Ray
71 {
72 
73  double origin[3]; // origin point of the ray
74  double direction[3]; // direction of the ray
75 };
76 
77 // now the big piece, the StaticAABBTree class
78 // -------------------------------------
80 
81 public:
86 
90  StaticAABBTree(const std::vector<AABB> &leavesAABBs);
91 
96 
97 public:
110  static AABB new_lonely_aabb(double minPoint[3], double maxPoint[3], unsigned int primtiveIndex = -1);
111 
112 public:
120  void build(const std::vector<AABB> &leavesAABBs);
121 
125  const unsigned int node_number() const {
126  return _node_count;
127  };
128 
132  const AABBNode node_at_index(unsigned int nodeIndex) const {
133  return _nodes[nodeIndex];
134  };
135 
139  const unsigned int aabb_number() const {
140  return _aabbsCount;
141  };
142 
146  const AABB aabb_at_index(unsigned int aabbIndex) const {
147  return _aabbs[aabbIndex];
148  };
149 
153  const bool is_node_leaf(unsigned int nodeIndex) const {
154  return _nodes[nodeIndex].leftNode == NotInList && _nodes[nodeIndex].leftNode == NotInList;
155  };
156 
170  void xraycast_aabb_tree(Ray aRay, unsigned int aNode = 0) const;
171 
181  void raycast_aabb_tree(Ray aRay, unsigned int aNode = 0) const;
182 
190  void closest_point(const double *aPoint, double &distance, unsigned int aNode = 0) const;
191 
199  void furthest_vertex_along_axis(const double *aPoint, const double *anAxis, double &distance, unsigned int aNode = 0) const;
200 
208  void set_closest_point_callback(std::function<void(unsigned int, const double *, double &)> callback) const {
209  closestPointCallback_ = callback;
210  };
211 
218  void set_raycast_aabb_tree_callback(std::function<bool(unsigned int, Ray)> callback) const {
219  raycastAABBTreeCallback_ = callback;
220  };
221 
229  void set_furthest_vertex_callback (std::function<void(unsigned int, const double *, const double *, double &)> callback) const {
230  furthestVertexCallback_ = callback;
231  };
232 
233 private:
241  unsigned int build(std::vector<unsigned int> aabbsIndices, unsigned int depth = 0);
242 
249  void clear();
250 
258  unsigned int new_aabb_node();
259 
267  unsigned int new_aabb_node(AABB anAABB);
268 
279  unsigned int new_aabb(double minPoint[3], double maxPoint[3], unsigned int primitiveIndex = -1);
280 
288  unsigned int new_aabb(AABB anAABB);
289 
298  unsigned int new_aabb_from_aabb(const std::vector<unsigned int> &aabbs);
299 
306  bool is_point_inside_aabb(const double *aPoint, unsigned int anAABB) const;
307 
314  double squared_distance_to_aabb_centre(const double *aPoint, unsigned int anAABB) const;
315 
322  double squared_distance_to_aabb(const double *aPoint, unsigned int anAABB) const;
323 
330  double inner_squared_distance_to_aabb(const double *aPoint, unsigned int anAABB) const;
331 
338  void furthest_vertex_on_aabb_along_axis(const double *anAxis, unsigned int anAABB, double (&furthestPoint)[3]) const;
339 
346  bool is_aabb_hit_by_xray(unsigned int anAABB, Ray aRay) const;
347 
354  bool is_aabb_hit_by_ray(unsigned int anAABB, Ray aRay) const;
355 
363  bool AABBFacetIsHitByRay(unsigned int anAABB, double facetNormal[3], Ray aRay) const;
364 
365 private:
369  static const unsigned int NotInList = (unsigned int)(-1);
370 
371 private:
375  unsigned int _aabbsCount;
376 
380  unsigned int _node_count;
381 
385  std::vector<AABB> _aabbs;
386 
390  std::vector<AABBNode> _nodes;
391 
399  mutable std::function<void(unsigned int primitiveIndex, const double *aPoint, double &distance)> closestPointCallback_;
400 
407  mutable std::function<bool(unsigned int primitiveIndex, Ray aRay)> raycastAABBTreeCallback_;
408 
416  mutable std::function<void(unsigned int primitiveIndex, const double *aPoint, const double *anAxis, double &distance)> furthestVertexCallback_;
417 };
418 
419 #endif /* StaticAABBTree_hpp */
Definition: StaticAABBTree.hpp:79
const AABB aabb_at_index(unsigned int aabbIndex) const
Definition: StaticAABBTree.hpp:146
const unsigned int node_number() const
Definition: StaticAABBTree.hpp:125
void build(const std::vector< AABB > &leavesAABBs)
Builds the AABB tree from a list of AABBs.
Definition: StaticAABBTree.cpp:75
std::vector< AABB > _aabbs
The AABBs container.
Definition: StaticAABBTree.hpp:385
void xraycast_aabb_tree(Ray aRay, unsigned int aNode=0) const
Raycasts the AABB tree using an x-oriented axis.
Definition: StaticAABBTree.cpp:166
std::function< bool(unsigned int primitiveIndex, Ray aRay)> raycastAABBTreeCallback_
The callback used to check if a primitive with index primitiveIndex is hit by the ray aRay.
Definition: StaticAABBTree.hpp:407
const AABBNode node_at_index(unsigned int nodeIndex) const
Definition: StaticAABBTree.hpp:132
double squared_distance_to_aabb(const double *aPoint, unsigned int anAABB) const
Computes the quared distance between a point and the AABB.
Definition: StaticAABBTree.cpp:466
void set_raycast_aabb_tree_callback(std::function< bool(unsigned int, Ray)> callback) const
Allows to define the callback method that will be used to check if a primitive is intersected by a ra...
Definition: StaticAABBTree.hpp:218
std::vector< AABBNode > _nodes
The AABB nodes container.
Definition: StaticAABBTree.hpp:390
void furthest_vertex_on_aabb_along_axis(const double *anAxis, unsigned int anAABB, double(&furthestPoint)[3]) const
Finds the furthest point on an AABB along a given axis.
Definition: StaticAABBTree.cpp:500
double inner_squared_distance_to_aabb(const double *aPoint, unsigned int anAABB) const
Computes the inner squared distance between a point and the AABB.
Definition: StaticAABBTree.cpp:483
std::function< void(unsigned int primitiveIndex, const double *aPoint, double &distance)> closestPointCallback_
The callback used to check if a primitive with index primitiveIndex contains a vertex closer to aPoin...
Definition: StaticAABBTree.hpp:399
unsigned int new_aabb_from_aabb(const std::vector< unsigned int > &aabbs)
Creates a new AABB that encloses all the AABBs contained in aabbs.
Definition: StaticAABBTree.cpp:387
bool is_aabb_hit_by_xray(unsigned int anAABB, Ray aRay) const
Checks wether an AABB is hit by an x-oriented ray.
Definition: StaticAABBTree.cpp:519
double squared_distance_to_aabb_centre(const double *aPoint, unsigned int anAABB) const
Computes the squared distance between a point and the AABB centre.
Definition: StaticAABBTree.cpp:454
const unsigned int aabb_number() const
Definition: StaticAABBTree.hpp:139
bool AABBFacetIsHitByRay(unsigned int anAABB, double facetNormal[3], Ray aRay) const
Checks wether a facet of an AABB is hit by an arbitrarily oriented ray.
Definition: StaticAABBTree.cpp:548
void set_closest_point_callback(std::function< void(unsigned int, const double *, double &)> callback) const
Allows to define the callback method that will be used to check if a primitive contains a vertex clos...
Definition: StaticAABBTree.hpp:208
void raycast_aabb_tree(Ray aRay, unsigned int aNode=0) const
Raycasts the AABB tree using an arbitrarily oriented axis.
Definition: StaticAABBTree.cpp:196
StaticAABBTree()
instanciates a new empty StaticAABBTree object.
Definition: StaticAABBTree.cpp:40
unsigned int new_aabb_node()
Creates a new AABBNode.
Definition: StaticAABBTree.cpp:338
const bool is_node_leaf(unsigned int nodeIndex) const
Definition: StaticAABBTree.hpp:153
bool is_aabb_hit_by_ray(unsigned int anAABB, Ray aRay) const
Checks wether an AABB is hit by an arbitrarily oriented ray.
Definition: StaticAABBTree.cpp:528
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: StaticAABBTree.cpp:377
unsigned int _node_count
The current number of AABB nodes in the tree.
Definition: StaticAABBTree.hpp:380
std::function< void(unsigned int primitiveIndex, const double *aPoint, const double *anAxis, double &distance)> furthestVertexCallback_
The callback used to check if a vertex of a primitive with index primitiveIndex is further than the o...
Definition: StaticAABBTree.hpp:416
unsigned int _aabbsCount
The current number of AABBs in the tree.
Definition: StaticAABBTree.hpp:375
bool is_point_inside_aabb(const double *aPoint, unsigned int anAABB) const
Checks whether a point is inside an AABB.
Definition: StaticAABBTree.cpp:437
void clear()
Clears the AABB tree data structure.
Definition: StaticAABBTree.cpp:64
static const unsigned int NotInList
A flag to determine if an index is in a list.
Definition: StaticAABBTree.hpp:369
~StaticAABBTree()
Default destructor.
Definition: StaticAABBTree.cpp:57
static AABB new_lonely_aabb(double minPoint[3], double maxPoint[3], unsigned int primtiveIndex=-1)
Creates a new 'lonely' AABB with given extreme points.
Definition: StaticAABBTree.cpp:415
void set_furthest_vertex_callback(std::function< void(unsigned int, const double *, const double *, double &)> callback) const
Allows to define the callback that will be used to check if a primitive contains a vertex further to ...
Definition: StaticAABBTree.hpp:229
void closest_point(const double *aPoint, double &distance, unsigned int aNode=0) const
Finds the closest point to the query.
Definition: StaticAABBTree.cpp:226
void furthest_vertex_along_axis(const double *aPoint, const double *anAxis, double &distance, unsigned int aNode=0) const
Finds the furthest vertex in a given direction.
Definition: StaticAABBTree.cpp:283
Definition: StaticAABBTree.hpp:38
double surface
Definition: StaticAABBTree.hpp:47
unsigned int nextFree
Definition: StaticAABBTree.hpp:45
unsigned int primitive
Definition: StaticAABBTree.hpp:43
double volume
Definition: StaticAABBTree.hpp:46
double minPoint[3]
Definition: StaticAABBTree.hpp:40
unsigned int aabbNode
Definition: StaticAABBTree.hpp:44
double midPoint[3]
Definition: StaticAABBTree.hpp:41
double maxPoint[3]
Definition: StaticAABBTree.hpp:42
bool isActive
Definition: StaticAABBTree.hpp:48
Definition: StaticAABBTree.hpp:54
unsigned int parentNode
Definition: StaticAABBTree.hpp:60
int height
Definition: StaticAABBTree.hpp:58
bool isActive
Definition: StaticAABBTree.hpp:64
double location[3]
Definition: StaticAABBTree.hpp:56
unsigned int nextFree
Definition: StaticAABBTree.hpp:63
unsigned int leftNode
Definition: StaticAABBTree.hpp:61
unsigned int aabb
Definition: StaticAABBTree.hpp:57
bool isCrossed
Definition: StaticAABBTree.hpp:65
unsigned int rightNode
Definition: StaticAABBTree.hpp:62
unsigned int depth
Definition: StaticAABBTree.hpp:59
Definition: StaticAABBTree.hpp:71
double direction[3]
Definition: StaticAABBTree.hpp:74
double origin[3]
Definition: StaticAABBTree.hpp:73