summaryrefslogtreecommitdiff
path: root/src/common/quaternion.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/common/quaternion.h44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/common/quaternion.h b/src/common/quaternion.h
new file mode 100644
index 000000000..84ac82ed3
--- /dev/null
+++ b/src/common/quaternion.h
@@ -0,0 +1,44 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "common/vector_math.h"
8
9namespace Math {
10
11template <typename T>
12class Quaternion {
13public:
14 Math::Vec3<T> xyz;
15 T w;
16
17 Quaternion<decltype(-T{})> Inverse() const {
18 return {-xyz, w};
19 }
20
21 Quaternion<decltype(T{} + T{})> operator+(const Quaternion& other) const {
22 return {xyz + other.xyz, w + other.w};
23 }
24
25 Quaternion<decltype(T{} - T{})> operator-(const Quaternion& other) const {
26 return {xyz - other.xyz, w - other.w};
27 }
28
29 Quaternion<decltype(T{} * T{} - T{} * T{})> operator*(const Quaternion& other) const {
30 return {xyz * other.w + other.xyz * w + Cross(xyz, other.xyz),
31 w * other.w - Dot(xyz, other.xyz)};
32 }
33};
34
35template <typename T>
36auto QuaternionRotate(const Quaternion<T>& q, const Math::Vec3<T>& v) {
37 return v + 2 * Cross(q.xyz, Cross(q.xyz, v) + v * q.w);
38}
39
40inline Quaternion<float> MakeQuaternion(const Math::Vec3<float>& axis, float angle) {
41 return {axis * std::sin(angle / 2), std::cos(angle / 2)};
42}
43
44} // namspace Math