Newton Dynamics  4.00
ndFastAabb.h
1 /* Copyright (c) <2003-2022> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifndef __ND_FAST_AABB_H__
23 #define __ND_FAST_AABB_H__
24 
25 #include "ndCoreStdafx.h"
26 #include "ndDebug.h"
27 #include "ndVector.h"
28 #include "ndMatrix.h"
29 
30 D_MSV_NEWTON_ALIGN_32
31 class ndFastAabb : public ndMatrix
32 {
33  public:
34  ndFastAabb();
35  ndFastAabb(const ndVector& p0, const ndVector& p1);
36  ndFastAabb(const ndMatrix& matrix, const ndVector& size);
37 
38  const ndVector& GetOrigin() const;
39  const ndVector& GetTarget() const;
40 
41  void SetSeparatingDistance(const ndFloat32 distance);
42  void SetTransposeAbsMatrix(const ndMatrix& matrix);
43 
44  D_CORE_API ndFloat32 PolygonBoxDistance(const ndVector& faceNormal, ndInt32 indexCount, const ndInt32* const indexArray, ndInt32 stride, const ndFloat32* const vertexArray) const;
45  D_CORE_API ndFloat32 PolygonBoxRayDistance(const ndVector& faceNormal, ndInt32 indexCount, const ndInt32* const indexArray, ndInt32 stride, const ndFloat32* const vertexArray, const ndFastRay& ray) const;
46 
47  private:
48  ndMatrix MakeFaceMatrix(const ndVector& faceNormal, ndInt32, const ndInt32* const indexArray, ndInt32 stride, const ndFloat32* const vertexArray) const;
49  void MakeBox1(ndInt32 indexCount, const ndInt32* const indexArray, ndInt32 stride, const ndFloat32* const vertexArray, ndVector& minBox, ndVector& maxBox) const;
50  void MakeBox2(const ndMatrix& faceMatrix, ndInt32 indexCount, const ndInt32* const indexArray, ndInt32 stride, const ndFloat32* const vertexArray, ndVector& minBox, ndVector& maxBox) const;
51 
52  protected:
53  ndMatrix m_absDir;
54  ndVector m_p0;
55  ndVector m_p1;
56  ndVector m_size;
57  mutable ndVector m_separationDistance;
58 
59  friend class ndAabbPolygonSoup;
60 } D_GCC_NEWTON_ALIGN_32 ;
61 
62 inline ndFastAabb::ndFastAabb()
63  :ndMatrix(ndGetIdentityMatrix())
64  ,m_absDir(ndGetIdentityMatrix())
65  ,m_p0(ndVector::m_zero)
66  ,m_p1(ndVector::m_zero)
67  ,m_size(ndVector::m_zero)
68  ,m_separationDistance(ndFloat32(1.0e10f))
69 {
70 }
71 
72 inline ndFastAabb::ndFastAabb(const ndMatrix& matrix, const ndVector& size)
73  :ndMatrix(matrix)
74  ,m_separationDistance(ndFloat32(1.0e10f))
75 {
76  SetTransposeAbsMatrix(matrix);
77  m_size = ndVector(matrix[0].Abs().Scale(size.m_x) + matrix[1].Abs().Scale(size.m_y) + matrix[2].Abs().Scale(size.m_z));
78  m_p0 = (matrix[3] - m_size) & ndVector::m_triplexMask;
79  m_p1 = (matrix[3] + m_size) & ndVector::m_triplexMask;
80 }
81 
82 inline ndFastAabb::ndFastAabb(const ndVector& p0, const ndVector& p1)
83  :ndMatrix(ndGetIdentityMatrix())
84  ,m_absDir(ndGetIdentityMatrix())
85  ,m_p0(p0)
86  ,m_p1(p1)
87  ,m_size(ndVector::m_half * (p1 - p0))
88  ,m_separationDistance(ndFloat32(1.0e10f))
89 {
90  m_posit = (ndVector::m_half * (p1 + p0)) | ndVector::m_wOne;
91  ndAssert(m_size.m_w == ndFloat32(0.0f));
92  ndAssert(m_posit.m_w == ndFloat32(1.0f));
93 }
94 
95 inline const ndVector& ndFastAabb::GetOrigin() const
96 {
97  return m_p0;
98 }
99 
100 inline const ndVector& ndFastAabb::GetTarget() const
101 {
102  return m_p1;
103 }
104 
105 inline void ndFastAabb::SetSeparatingDistance(const ndFloat32 distance)
106 {
107  m_separationDistance = distance;
108 }
109 
110 inline void ndFastAabb::SetTransposeAbsMatrix(const ndMatrix& matrix)
111 {
112  m_absDir = matrix.Transpose();
113  m_absDir[0] = m_absDir[0].Abs();
114  m_absDir[1] = m_absDir[1].Abs();
115  m_absDir[2] = m_absDir[2].Abs();
116 }
117 
118 #endif
119 
ndMatrix
Definition: ndMatrix.h:42
ndFastRay
Definition: ndFastRay.h:48
ndFastAabb
Definition: ndFastAabb.h:32
ndAabbPolygonSoup
Base class for creating a leafless bounding box hierarchy for queering a polygon list index list mesh...
Definition: ndAabbPolygonSoup.h:55
ndVector
Definition: ndVectorArmNeon.h:41