53 resQuat = *
this * resQuat;
55 return Vec3( resQuat.
x, resQuat.
y, resQuat.
z );
66 w * aQ.
y +
y * aQ.
w +
z * aQ.
x -
x * aQ.
z,
67 w * aQ.
z +
z * aQ.
w +
x * aQ.
y -
y * aQ.
x ),
68 w * aQ.
w -
x * aQ.
x -
y * aQ.
y -
z * aQ.
z );
78 const float acceptableDelta = 0.00001f;
80 return std::fabs(
x - q.
x) < acceptableDelta &&
81 std::fabs(
y - q.
y) < acceptableDelta &&
82 std::fabs(
z - q.
z) < acceptableDelta &&
83 std::fabs(
w - q.
w) < acceptableDelta;
116 Vec3 orthonormal1, orthonormal2;
117 FindOrthonormals( axis, orthonormal1, orthonormal2 );
119 Vec3 transformed = *
this * orthonormal1;
122 Vec3 flattened = transformed - axis *
Vec3::Dot( transformed, axis );
126 return std::acos( Vec3::Dot( orthonormal1, flattened ) );
135 const float scale = std::sqrt(
x *
x +
y *
y +
z *
z );
137 if (std::fabs( scale ) < 0.00001f)
139 outAxis =
Vec3( 0, 0, 0 );
143 outAxis.
x =
x / scale;
144 outAxis.
y =
y / scale;
145 outAxis.
z = z / scale;
148 outAngleRad = std::acos(
w ) * 2;
155 const Vec3 xAxis( 1, 0, 0 );
156 const Vec3 yAxis( 0, 1, 0 );
157 const Vec3 zAxis( 0, 0, 1 );
173 float angleRad = angleDeg * (3.1418693659f / 180.0f);
177 const float sinAngle = std::sin( angleRad );
179 x = axis.
x * sinAngle;
180 y = axis.
y * sinAngle;
181 z = axis.
z * sinAngle;
182 w = std::cos( angleRad );
199 const float trace = 1.0f + mat.m[0] + mat.m[5] + mat.m[10];
203 assert( trace > 0 &&
"Quaternion from matrix: trying to get an sqrt of a negative value" );
205 const float S = sqrtf( trace ) * 2.0f;
206 x = ( mat.m[9] - mat.m[6] ) / S;
207 y = ( mat.m[2] - mat.m[8] ) / S;
208 z = ( mat.m[4] - mat.m[1] ) / S;
211 else if (mat.m[0] > mat.m[5] && mat.m[0] > mat.m[10])
213 const float val = 1.0f + mat.m[0] - mat.m[5] - mat.m[10];
214 assert( val > 0 &&
"Quaternion from matrix: trying to get an sqrt of a negative value" );
216 const float S = std::sqrt( val ) * 2.0f;
218 y = (mat.m[4] + mat.m[1] ) / S;
219 z = (mat.m[2] + mat.m[8] ) / S;
220 w = (mat.m[9] - mat.m[6] ) / S;
222 else if (mat.m[5] > mat.m[10])
224 const float val = 1.0f + mat.m[5] - mat.m[0] - mat.m[10];
225 assert( val > 0 &&
"Quaternion from matrix: trying to get an sqrt of a negative value" );
227 const float S = std::sqrt(val ) * 2.0f;
228 x = (mat.m[4] + mat.m[1] ) / S;
230 z = (mat.m[9] + mat.m[6] ) / S;
231 w = (mat.m[2] - mat.m[8] ) / S;
235 const float val = 1.0f + mat.m[10] - mat.m[0] - mat.m[5];
236 assert( val > 0 &&
"Quaternion from matrix: trying to get an sqrt of a negative value" );
238 const float S = std::sqrt( val ) * 2.0f;
239 x = (mat.m[2] + mat.m[8] ) / S;
240 y = (mat.m[9] + mat.m[6] ) / S;
242 w = (mat.m[4] - mat.m[1] ) / S;
253 const float x2 =
x *
x;
254 const float y2 =
y *
y;
255 const float z2 =
z *
z;
256 const float xy = x *
y;
257 const float xz = x *
z;
258 const float yz = y *
z;
259 const float wx =
w *
x;
260 const float wy =
w *
y;
261 const float wz =
w *
z;
263 outMatrix.m[ 0] = 1 - 2 * (y2 + z2);
264 outMatrix.m[ 1] = 2 * (xy - wz);
265 outMatrix.m[ 2] = 2 * (xz + wy);
267 outMatrix.m[ 4] = 2 * (xy + wz);
268 outMatrix.m[ 5] = 1 - 2 * (x2 + z2);
269 outMatrix.m[ 6] = 2 * (yz - wx);
271 outMatrix.m[ 8] = 2 * (xz - wy);
272 outMatrix.m[ 9] = 2 * (yz + wx);
273 outMatrix.m[10] = 1 - 2 * (x2 + y2);
288 out.
z = std::atan2( 2 *
y *
w - 2 *
x *
z, 1 - 2 *
y *
y - 2 * z * z );
289 out.
y = std::asin( 2 *
x *
y + 2 * z *
w );
290 out.
x = std::atan2( 2 *
x * w - 2 *
y * z, 1 - 2 *
x *
x - 2 * z * z );
291 return out / (3.14159265358979f / 180.0f);
297 const float mag2 =
w *
w +
x *
x +
y *
y +
z *
z;
298 const float acceptableDelta = 0.00001f;
300 if (std::fabs( mag2 ) > acceptableDelta && std::fabs( mag2 - 1.0f ) > acceptableDelta)
302 const float oneOverMag = 1.0f / std::sqrt( mag2 );
321 void FindOrthonormals(
const Vec3& normal,
Vec3& orthonormal1,
Vec3& orthonormal2 )
const 323 Matrix44 orthoX( 90, 0, 0 );
324 Matrix44 orthoY( 0, 90, 0 );
327 Matrix44::TransformDirection( normal, orthoX, &ww );
328 const float dot =
Vec3::Dot( normal, ww );
330 if (std::fabs( dot ) > 0.6f)
332 Matrix44::TransformDirection( normal, orthoY, &ww );
339 orthonormal2 =
Vec3::Cross( normal, orthonormal1 );
Quaternion()
Constructor.
Definition: Quaternion.hpp:15
Quaternion & operator=(Quaternion &&) noexcept=default
Move assignment.
void FromMatrix(const Matrix44 &mat)
Definition: Quaternion.hpp:197
float x
X coordinate.
Definition: Vec3.hpp:15
Definition: AudioClip.hpp:4
static Quaternion FromEuler(const Vec3 &euler)
Definition: Quaternion.hpp:153
Quaternion operator*(const Quaternion &aQ) const
Applies rotation aQ to this quaternion and returns the result.
Definition: Quaternion.hpp:63
Vec3 GetEuler() const
Definition: Quaternion.hpp:285
float z
Definition: Quaternion.hpp:316
3-component vector.
Definition: Vec3.hpp:12
void GetMatrix(Matrix44 &outMatrix) const
Definition: Quaternion.hpp:251
float y
Definition: Quaternion.hpp:314
bool operator==(const Quaternion &q) const
Definition: Quaternion.hpp:76
static Quaternion CreateFromAxisAngle(const Vec3 &axis, float angleDeg)
Definition: Quaternion.hpp:189
float w
Definition: Quaternion.hpp:318
Quaternion Conjugate() const
Definition: Quaternion.hpp:102
float x
Definition: Quaternion.hpp:312
float z
Z coordinate.
Definition: Vec3.hpp:19
static Vec3 Cross(const Vec3 &v1, const Vec3 &v2)
Cross product.
Definition: Vec3.hpp:28
void Normalize()
Definition: Quaternion.hpp:295
float FindTwist(const Vec3 &axis) const
Finds twist angle around axis.
Definition: Quaternion.hpp:113
static float Dot(const Vec3 &v1, const Vec3 &v2)
Definition: Vec3.hpp:100
void FromAxisAngle(const Vec3 &axis, float angleDeg)
Definition: Quaternion.hpp:171
void GetAxisAngle(Vec3 &outAxis, float &outAngleRad) const
Definition: Quaternion.hpp:133
Vec3 Normalized() const
Definition: Vec3.hpp:311
float y
Y coordinate.
Definition: Vec3.hpp:17
Quaternion(const Vec3 &vec, float aW)
Definition: Quaternion.hpp:21
bool operator!=(const Quaternion &q) const
Definition: Quaternion.hpp:91
Stores an orientation.
Definition: Quaternion.hpp:12
Vec3 operator*(const Vec3 &vec) const
Multiplying a quaternion q with a vector v applies the q-rotation to vec.
Definition: Quaternion.hpp:41