diff options
| author | 2016-12-11 23:32:01 +0200 | |
|---|---|---|
| committer | 2016-12-26 10:41:26 +0200 | |
| commit | 6479f63091da35b1ac6f3930532cc8bde466f6c4 (patch) | |
| tree | ce2201c23316151371704e19a51f1d1f709f8c74 /src | |
| parent | vector math: add implementation of Length and Normalize (diff) | |
| download | yuzu-6479f63091da35b1ac6f3930532cc8bde466f6c4.tar.gz yuzu-6479f63091da35b1ac6f3930532cc8bde466f6c4.tar.xz yuzu-6479f63091da35b1ac6f3930532cc8bde466f6c4.zip | |
Common: add Quaternion
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/common/quaternion.h | 44 |
2 files changed, 45 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 5aecf6e6e..a7a4a688c 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -46,6 +46,7 @@ set(HEADERS | |||
| 46 | microprofileui.h | 46 | microprofileui.h |
| 47 | platform.h | 47 | platform.h |
| 48 | profiler_reporting.h | 48 | profiler_reporting.h |
| 49 | quaternion.h | ||
| 49 | scm_rev.h | 50 | scm_rev.h |
| 50 | scope_exit.h | 51 | scope_exit.h |
| 51 | string_util.h | 52 | string_util.h |
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 | |||
| 9 | namespace Math { | ||
| 10 | |||
| 11 | template <typename T> | ||
| 12 | class Quaternion { | ||
| 13 | public: | ||
| 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 | |||
| 35 | template <typename T> | ||
| 36 | auto 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 | |||
| 40 | inline 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 | ||