SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLAABBox.h
Go to the documentation of this file.
1 /**
2  * \file SLAABBox.h
3  * \date July 2014
4  * \authors Marcus Hudritsch
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #ifndef SLAABBox_H
11 #define SLAABBox_H
12 
13 #include <SLGLVertexArrayExt.h>
14 #include <SLMat4.h>
15 #include <SLRect.h>
16 
17 class SLRay;
18 
19 //-----------------------------------------------------------------------------
20 //! Defines an axis aligned bounding box
21 /*!
22 The SLAABBox class defines an axis aligned bounding box with a minimal and
23 maximal point. Each node (SLNode) will have an AABB the will be calculated
24 in buildAABB. A mesh (SLMesh) will implement buildAABB and calculate the
25 minimal and maximal coordinates in object space (stored in _minOS and _maxOS).
26 For a fast ray-AABB intersection in world space we transform _minOS and _maxOS
27 into world space (with the shapes world matrix) and store it in _minWS and
28 _maxWS.
29 For an even faster intersection test with the plane of the view frustum we
30 calculate in addition the bounding sphere around the AABB. The radius and the
31 center point are stored in _radiusOS/_centerOS and _radiusWS/_centerWS.
32 */
33 class SLAABBox
34 {
35 public:
36  SLAABBox();
37 
38  // Setters
39  void minWS(const SLVec3f& minC) { _minWS = minC; }
40  void maxWS(const SLVec3f& maxC) { _maxWS = maxC; }
41  void minOS(const SLVec3f& minC) { _minOS = minC; }
42  void maxOS(const SLVec3f& maxC) { _maxOS = maxC; }
43 
44  void isVisible(SLbool visible) { _isVisible = visible; }
45  void sqrViewDist(SLfloat sqrVD) { _sqrViewDist = sqrVD; }
46 
47  // Getters
48  SLVec3f minWS() { return _minWS; }
49  SLVec3f maxWS() { return _maxWS; }
50  SLVec3f centerWS() { return _centerWS; }
51  SLfloat radiusWS() { return _radiusWS; }
52  SLVec3f minOS() { return _minOS; }
53  SLVec3f maxOS() { return _maxOS; }
54  SLVec3f centerOS() { return _centerOS; }
55  SLfloat radiusOS() { return _radiusOS; }
56  SLbool isVisible() { return _isVisible; }
58  SLRectf& rectSS() { return _rectSS; }
59 
60  // Misc.
61  void reset();
62  void fromOStoWS(const SLVec3f& minOS,
63  const SLVec3f& maxOS,
64  const SLMat4f& wm);
65  void fromWStoOS(const SLVec3f& minWS,
66  const SLVec3f& maxWS,
67  const SLMat4f& wmI);
68  void updateAxisWS(const SLMat4f& wm);
69  void updateBoneWS(const SLMat4f& parentWM,
70  SLbool isRoot,
71  const SLMat4f& nodeWM);
72  void mergeWS(SLAABBox& bb);
73  void drawWS(const SLCol4f& color);
74  void drawAxisWS();
75  void drawBoneWS();
76  void setCenterAndRadiusWS();
77  void generateVAO();
78  SLbool isHitInOS(SLRay* ray);
79  SLbool isHitInWS(SLRay* ray);
80  void calculateRectSS();
82 
83 private:
84  SLVec3f _minWS; //!< Min. corner in world space
85  SLVec3f _minOS; //!< Min. corner in object space
86  SLVec3f _maxWS; //!< Max. corner in world space
87  SLVec3f _maxOS; //!< Max. corner in object space
88  SLVec3f _centerWS; //!< Center of AABB in world space
89  SLVec3f _centerOS; //!< Center of AABB in object space
90  SLfloat _radiusWS; //!< Radius of sphere around AABB in WS
91  SLfloat _radiusOS; //!< Radius of sphere around AABB in OS
92  SLfloat _sqrViewDist; //!< Squared dist. from center to viewpoint
93  SLVec3f _axis0WS; //!< World space axis center point
94  SLVec3f _axisXWS; //!< World space x-axis vector
95  SLVec3f _axisYWS; //!< World space y-axis vector
96  SLVec3f _axisZWS; //!< World space z-axis vector
97  SLbool _boneIsOffset; //!< Flag if the connection parent to us is a bone or an offset
98  SLVec3f _parent0WS; //!< World space vector to the parent position
99  SLbool _isVisible; //!< Flag if AABB is in the view frustum
100  SLRectf _rectSS; //!< Bounding rectangle in screen space
101  SLGLVertexArrayExt _vao; //!< Vertex array object for rendering
102 };
103 //-----------------------------------------------------------------------------
104 
105 #endif
float SLfloat
Definition: SL.h:173
bool SLbool
Definition: SL.h:175
Extension class with functions for quick line & point drawing.
Defines an axis aligned bounding box.
Definition: SLAABBox.h:34
SLVec3f minWS()
Definition: SLAABBox.h:48
SLfloat rectCoverageInSS()
Calculates the bounding rectangle in screen space and returns coverage in SS.
Definition: SLAABBox.cpp:446
SLbool isVisible()
Definition: SLAABBox.h:56
SLGLVertexArrayExt _vao
Vertex array object for rendering.
Definition: SLAABBox.h:101
void generateVAO()
Generates the vertex buffer for the line visualization.
Definition: SLAABBox.cpp:212
SLfloat sqrViewDist()
Definition: SLAABBox.h:57
SLVec3f _centerOS
Center of AABB in object space.
Definition: SLAABBox.h:89
void reset()
Resets initial state without contents.
Definition: SLAABBox.cpp:23
SLVec3f _minOS
Min. corner in object space.
Definition: SLAABBox.h:85
void minOS(const SLVec3f &minC)
Definition: SLAABBox.h:41
SLVec3f _axisZWS
World space z-axis vector.
Definition: SLAABBox.h:96
SLfloat _radiusWS
Radius of sphere around AABB in WS.
Definition: SLAABBox.h:90
SLVec3f centerWS()
Definition: SLAABBox.h:50
void mergeWS(SLAABBox &bb)
Merges the bounding box bb to this one by extending this one axis aligned.
Definition: SLAABBox.cpp:390
SLVec3f _parent0WS
World space vector to the parent position.
Definition: SLAABBox.h:98
SLbool isHitInWS(SLRay *ray)
SLAABBox::isHitInWS: Ray - AABB Intersection Test in world space.
Definition: SLAABBox.cpp:361
SLRectf _rectSS
Bounding rectangle in screen space.
Definition: SLAABBox.h:100
SLVec3f _maxWS
Max. corner in world space.
Definition: SLAABBox.h:86
SLVec3f _axisXWS
World space x-axis vector.
Definition: SLAABBox.h:94
SLAABBox()
Default constructor with default zero vector initialization.
Definition: SLAABBox.cpp:17
SLRectf & rectSS()
Definition: SLAABBox.h:58
void fromOStoWS(const SLVec3f &minOS, const SLVec3f &maxOS, const SLMat4f &wm)
Recalculate min and max after transformation in world coords.
Definition: SLAABBox.cpp:46
void calculateRectSS()
Calculates the AABBs min. and max. corners in screen space.
Definition: SLAABBox.cpp:400
SLVec3f _maxOS
Max. corner in object space.
Definition: SLAABBox.h:87
SLfloat _sqrViewDist
Squared dist. from center to viewpoint.
Definition: SLAABBox.h:92
SLVec3f _axisYWS
World space y-axis vector.
Definition: SLAABBox.h:95
void isVisible(SLbool visible)
Definition: SLAABBox.h:44
SLVec3f _centerWS
Center of AABB in world space.
Definition: SLAABBox.h:88
void minWS(const SLVec3f &minC)
Definition: SLAABBox.h:39
SLfloat radiusWS()
Definition: SLAABBox.h:51
void drawAxisWS()
Draws the axis in world space with lines in a color.
Definition: SLAABBox.cpp:271
SLVec3f _axis0WS
World space axis center point.
Definition: SLAABBox.h:93
SLfloat _radiusOS
Radius of sphere around AABB in OS.
Definition: SLAABBox.h:91
void sqrViewDist(SLfloat sqrVD)
Definition: SLAABBox.h:45
void drawWS(const SLCol4f &color)
Draws the AABB in world space with lines in a color.
Definition: SLAABBox.cpp:260
void fromWStoOS(const SLVec3f &minWS, const SLVec3f &maxWS, const SLMat4f &wmI)
Recalculate min and max before transformation in object coords.
Definition: SLAABBox.cpp:96
SLfloat radiusOS()
Definition: SLAABBox.h:55
SLVec3f maxOS()
Definition: SLAABBox.h:53
void maxWS(const SLVec3f &maxC)
Definition: SLAABBox.h:40
void updateAxisWS(const SLMat4f &wm)
Updates the axis of the owning node.
Definition: SLAABBox.cpp:141
void updateBoneWS(const SLMat4f &parentWM, SLbool isRoot, const SLMat4f &nodeWM)
Updates joints axis and the bone line from the parent to us.
Definition: SLAABBox.cpp:162
SLbool _boneIsOffset
Flag if the connection parent to us is a bone or an offset.
Definition: SLAABBox.h:97
void drawBoneWS()
Draws the joint axis and the parent bone in world space.
Definition: SLAABBox.cpp:296
SLbool isHitInOS(SLRay *ray)
SLAABBox::isHitInWS: Ray - AABB Intersection Test in object space.
Definition: SLAABBox.cpp:331
SLbool _isVisible
Flag if AABB is in the view frustum.
Definition: SLAABBox.h:99
SLVec3f minOS()
Definition: SLAABBox.h:52
void setCenterAndRadiusWS()
Calculates center & radius of the bounding sphere around the AABB.
Definition: SLAABBox.cpp:204
SLVec3f _minWS
Min. corner in world space.
Definition: SLAABBox.h:84
SLVec3f maxWS()
Definition: SLAABBox.h:49
void maxOS(const SLVec3f &maxC)
Definition: SLAABBox.h:42
SLVec3f centerOS()
Definition: SLAABBox.h:54
SLGLVertexArray adds Helper Functions for quick Line & Point Drawing.
Ray class with ray and intersection properties.
Definition: SLRay.h:40