diff options
| author | 2017-01-07 12:39:20 -0500 | |
|---|---|---|
| committer | 2017-01-07 12:39:20 -0500 | |
| commit | 7cfe3ef0463ace034b1e5786c9581cfa5f2e810c (patch) | |
| tree | 6b06cb03d276b29070ca29599fc4c5fcf77edb39 /src/common | |
| parent | Merge pull request #2410 from Subv/sleepthread (diff) | |
| parent | Frontend: make motion sensor interfaced thread-safe (diff) | |
| download | yuzu-7cfe3ef0463ace034b1e5786c9581cfa5f2e810c.tar.gz yuzu-7cfe3ef0463ace034b1e5786c9581cfa5f2e810c.tar.xz yuzu-7cfe3ef0463ace034b1e5786c9581cfa5f2e810c.zip | |
Merge pull request #1951 from wwylele/motion-sensor
Emulate motion sensor in frontend
Diffstat (limited to '')
| -rw-r--r-- | src/common/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/common/math_util.h | 2 | ||||
| -rw-r--r-- | src/common/quaternion.h | 44 | ||||
| -rw-r--r-- | src/common/thread.h | 10 | ||||
| -rw-r--r-- | src/common/vector_math.h | 19 |
5 files changed, 76 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/math_util.h b/src/common/math_util.h index cdeaeb733..45a1ed367 100644 --- a/src/common/math_util.h +++ b/src/common/math_util.h | |||
| @@ -10,6 +10,8 @@ | |||
| 10 | 10 | ||
| 11 | namespace MathUtil { | 11 | namespace MathUtil { |
| 12 | 12 | ||
| 13 | static constexpr float PI = 3.14159265f; | ||
| 14 | |||
| 13 | inline bool IntervalsIntersect(unsigned start0, unsigned length0, unsigned start1, | 15 | inline bool IntervalsIntersect(unsigned start0, unsigned length0, unsigned start1, |
| 14 | unsigned length1) { | 16 | unsigned length1) { |
| 15 | return (std::max(start0, start1) < std::min(start0 + length0, start1 + length1)); | 17 | return (std::max(start0, start1) < std::min(start0 + length0, start1 + length1)); |
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 | ||
diff --git a/src/common/thread.h b/src/common/thread.h index 9c08be7e3..fa475ab51 100644 --- a/src/common/thread.h +++ b/src/common/thread.h | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <chrono> | ||
| 7 | #include <condition_variable> | 8 | #include <condition_variable> |
| 8 | #include <cstddef> | 9 | #include <cstddef> |
| 9 | #include <mutex> | 10 | #include <mutex> |
| @@ -54,6 +55,15 @@ public: | |||
| 54 | is_set = false; | 55 | is_set = false; |
| 55 | } | 56 | } |
| 56 | 57 | ||
| 58 | template <class Clock, class Duration> | ||
| 59 | bool WaitUntil(const std::chrono::time_point<Clock, Duration>& time) { | ||
| 60 | std::unique_lock<std::mutex> lk(mutex); | ||
| 61 | if (!condvar.wait_until(lk, time, [this] { return is_set; })) | ||
| 62 | return false; | ||
| 63 | is_set = false; | ||
| 64 | return true; | ||
| 65 | } | ||
| 66 | |||
| 57 | void Reset() { | 67 | void Reset() { |
| 58 | std::unique_lock<std::mutex> lk(mutex); | 68 | std::unique_lock<std::mutex> lk(mutex); |
| 59 | // no other action required, since wait loops on the predicate and any lingering signal will | 69 | // no other action required, since wait loops on the predicate and any lingering signal will |
diff --git a/src/common/vector_math.h b/src/common/vector_math.h index a57d86d88..7ca8e15f5 100644 --- a/src/common/vector_math.h +++ b/src/common/vector_math.h | |||
| @@ -186,6 +186,18 @@ Vec2<T> operator*(const V& f, const Vec2<T>& vec) { | |||
| 186 | 186 | ||
| 187 | typedef Vec2<float> Vec2f; | 187 | typedef Vec2<float> Vec2f; |
| 188 | 188 | ||
| 189 | template <> | ||
| 190 | inline float Vec2<float>::Length() const { | ||
| 191 | return std::sqrt(x * x + y * y); | ||
| 192 | } | ||
| 193 | |||
| 194 | template <> | ||
| 195 | inline float Vec2<float>::Normalize() { | ||
| 196 | float length = Length(); | ||
| 197 | *this /= length; | ||
| 198 | return length; | ||
| 199 | } | ||
| 200 | |||
| 189 | template <typename T> | 201 | template <typename T> |
| 190 | class Vec3 { | 202 | class Vec3 { |
| 191 | public: | 203 | public: |
| @@ -388,6 +400,13 @@ inline Vec3<float> Vec3<float>::Normalized() const { | |||
| 388 | return *this / Length(); | 400 | return *this / Length(); |
| 389 | } | 401 | } |
| 390 | 402 | ||
| 403 | template <> | ||
| 404 | inline float Vec3<float>::Normalize() { | ||
| 405 | float length = Length(); | ||
| 406 | *this /= length; | ||
| 407 | return length; | ||
| 408 | } | ||
| 409 | |||
| 391 | typedef Vec3<float> Vec3f; | 410 | typedef Vec3<float> Vec3f; |
| 392 | 411 | ||
| 393 | template <typename T> | 412 | template <typename T> |