-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuaternion.hpp
173 lines (136 loc) · 3.55 KB
/
Quaternion.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#pragma once
#ifndef __MATHS_QUATERNION_HPP__
#define __MATHS_QUATERNION_HPP__
#include "Vector3.hpp"
#include "Vector4.hpp"
namespace Math
{
class Quaternion : public DirectX::XMFLOAT4A
{
public:
//--------------------------------------------------------------------------
// Constructors
//
Quaternion()
{
DirectX::XMStoreFloat4A(this, DirectX::XMQuaternionIdentity());
}
Quaternion(float x, float y, float z, float w) : DirectX::XMFLOAT4A(z, y, z, w)
{
}
Quaternion(float yaw, float pitch, float roll)
{
DirectX::XMStoreFloat4A(this, DirectX::XMQuaternionRotationRollPitchYaw(pitch, yaw, roll));
}
Quaternion(const Vector3& axis, float angle)
{
DirectX::XMStoreFloat4A(this, DirectX::XMQuaternionRotationAxis(axis, angle));
}
explicit Quaternion(const float* values) : DirectX::XMFLOAT4A(values)
{
}
explicit Quaternion(DirectX::FXMVECTOR v)
{
DirectX::XMStoreFloat4A(this, v);
}
Quaternion(const Quaternion& q) : DirectX::XMFLOAT4A(q)
{
}
//--------------------------------------------------------------------------
// Assignment
//
Quaternion& operator = (DirectX::FXMVECTOR v)
{
DirectX::XMStoreFloat4A(this, v);
return *this;
}
Quaternion& operator = (const Quaternion& q)
{
x = q.x;
y = q.y;
z = q.z;
w = q.w;
return *this;
}
//--------------------------------------------------------------------------
// Conversion
//
operator DirectX::XMVECTOR () const
{
return DirectX::XMLoadFloat4A(this);
}
//--------------------------------------------------------------------------
// Comparison
//
bool operator == (const Quaternion& q) const
{
return x == q.x && y == q.y && z == q.z && w == q.w;
}
bool operator != (const Quaternion& q) const
{
return x != q.x || y != q.y || z != q.z || w != q.w;
}
//--------------------------------------------------------------------------
// Inspection
//
bool is_identity() const
{
return DirectX::XMQuaternionIsIdentity(*this);
}
//--------------------------------------------------------------------------
// Computation
//
void from_axis_angle(const Vector3& axis, float angle)
{
DirectX::XMStoreFloat4A(this, DirectX::XMQuaternionRotationAxis(axis, angle));
}
void to_axis_angle(Vector3& axis, float& angle) const
{
DirectX::XMVECTOR a;
DirectX::XMQuaternionToAxisAngle(&a, &angle, *this);
axis = a;
}
float dot(const Quaternion& q) const
{
return DirectX::XMVectorGetX(DirectX::XMQuaternionDot(*this, q));
}
float length() const
{
return DirectX::XMVectorGetX(DirectX::XMQuaternionLength(*this));
}
float length_squared() const
{
return DirectX::XMVectorGetX(DirectX::XMQuaternionLengthSq(*this));
}
void normalise()
{
DirectX::XMStoreFloat4A(this, DirectX::XMQuaternionNormalize(*this));
}
Quaternion conjugate() const
{
return Quaternion(DirectX::XMQuaternionConjugate(*this));
}
Quaternion inverse() const
{
return Quaternion(DirectX::XMQuaternionInverse(*this));
}
Quaternion exp() const
{
return Quaternion(DirectX::XMQuaternionExp(*this));
}
Quaternion ln() const
{
return Quaternion(DirectX::XMQuaternionLn(*this));
}
Quaternion slerp(const Quaternion& q, float t) const
{
return Quaternion(DirectX::XMQuaternionSlerp(*this, q, t));
}
Vector3 transform(const Vector3& v) const;
//--------------------------------------------------------------------------
// Constants
//
static const Quaternion IDENTITY;
};
} // namespace Math
#endif // __MATHS_QUATERNION_HPP__