Newton Dynamics  4.00
dQuaternion.h
1 /* Copyright (c) <2003-2019> <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 __D_QUATERNION_H__
23 #define __D_QUATERNION_H__
24 
25 #include "dCoreStdafx.h"
26 #include "dTypes.h"
27 
28 class dVector;
29 class dMatrix;
30 
31 D_MSV_NEWTON_ALIGN_16
33 {
34  public:
35  dQuaternion ();
36  D_CORE_API dQuaternion (const dMatrix& matrix);
37  dQuaternion (dFloat32 q0, dFloat32 q1, dFloat32 q2, dFloat32 q3);
38  D_CORE_API dQuaternion (const dVector &unit_Axis, dFloat32 angle = dFloat32 (0.0f));
39 
40 // dFloat32& operator[] (dInt32 i);
41 // const dFloat32& operator[] (dInt32 i) const;
42 
43  void Scale (dFloat32 scale);
44  void Normalize ();
45  dQuaternion Inverse () const;
46  D_CORE_API dQuaternion Slerp (const dQuaternion &q1, dFloat32 t) const;
47 
48  dFloat32 DotProduct (const dQuaternion &QB) const;
49  D_CORE_API dVector CalcAverageOmega (const dQuaternion &q1, dFloat32 invdt) const;
50 
51  dQuaternion operator* (const dQuaternion &B) const;
52  dQuaternion operator+ (const dQuaternion &B) const;
53  dQuaternion operator- (const dQuaternion &B) const;
54 
55  dFloat32 m_x;
56  dFloat32 m_y;
57  dFloat32 m_z;
58  dFloat32 m_w;
59 } D_GCC_NEWTON_ALIGN_32;
60 
61 D_INLINE dQuaternion::dQuaternion()
62  :m_x(dFloat32(0.0f))
63  ,m_y(dFloat32(0.0f))
64  ,m_z(dFloat32(0.0f))
65  ,m_w(dFloat32(1.0f))
66 {
67 }
68 
69 D_INLINE dQuaternion::dQuaternion(dFloat32 Q0, dFloat32 Q1, dFloat32 Q2, dFloat32 Q3)
70  :m_x(Q1)
71  ,m_y(Q2)
72  ,m_z(Q3)
73  ,m_w(Q0)
74 {
75 // dAssert (dAbs (DotProduct (*this) -dFloat32 (1.0f)) < dFloat32(1.0e-4f));
76 }
77 
78 /*
79 D_INLINE dFloat32& dQuaternion::operator[] (dInt32 i)
80 {
81  dAssert(i < 4);
82  dAssert(i >= 0);
83  return (&m_w)[i];
84 }
85 
86 D_INLINE const dFloat32& dQuaternion::operator[] (dInt32 i) const
87 {
88  dAssert(i < 4);
89  dAssert(i >= 0);
90  return (&m_w)[i];
91 }
92 */
93 
94 D_INLINE void dQuaternion::Scale (dFloat32 scale)
95 {
96  m_w *= scale;
97  m_x *= scale;
98  m_y *= scale;
99  m_z *= scale;
100 }
101 
102 D_INLINE void dQuaternion::Normalize ()
103 {
104  Scale (dRsqrt (DotProduct (*this)));
105 }
106 
107 D_INLINE dFloat32 dQuaternion::DotProduct (const dQuaternion &q1) const
108 {
109  return m_w * q1.m_w + m_x * q1.m_x + m_y * q1.m_y + m_z * q1.m_z;
110 }
111 
112 D_INLINE dQuaternion dQuaternion::Inverse () const
113 {
114  return dQuaternion (m_w, -m_x, -m_y, -m_z);
115 }
116 
117 D_INLINE dQuaternion dQuaternion::operator+ (const dQuaternion &q) const
118 {
119  return dQuaternion (m_w + q.m_w, m_x + q.m_x, m_y + q.m_y, m_z + q.m_z);
120 }
121 
122 D_INLINE dQuaternion dQuaternion::operator- (const dQuaternion &q) const
123 {
124  return dQuaternion (m_w - q.m_w, m_x - q.m_x, m_y - q.m_y, m_z - q.m_z);
125 }
126 
127 D_INLINE dQuaternion dQuaternion::operator* (const dQuaternion &q) const
128 {
129  return dQuaternion (q.m_w * m_w - q.m_x * m_x - q.m_y * m_y - q.m_z * m_z,
130  q.m_x * m_w + q.m_w * m_x - q.m_z * m_y + q.m_y * m_z,
131  q.m_y * m_w + q.m_z * m_x + q.m_w * m_y - q.m_x * m_z,
132  q.m_z * m_w - q.m_y * m_x + q.m_x * m_y + q.m_w * m_z);
133 }
134 
135 #endif
136 
dQuaternion
Definition: dQuaternion.h:33
dVector
Definition: dVectorArmNeon.h:1104
dMatrix
Definition: dMatrix.h:39