From 2a069e76a5093db44a87732edb72a1bb2b771e61 Mon Sep 17 00:00:00 2001 From: wwylele Date: Sun, 11 Dec 2016 23:26:23 +0200 Subject: Common::Event: add WaitUntil --- src/common/thread.h | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/common') 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 @@ #pragma once +#include #include #include #include @@ -54,6 +55,15 @@ public: is_set = false; } + template + bool WaitUntil(const std::chrono::time_point& time) { + std::unique_lock lk(mutex); + if (!condvar.wait_until(lk, time, [this] { return is_set; })) + return false; + is_set = false; + return true; + } + void Reset() { std::unique_lock lk(mutex); // no other action required, since wait loops on the predicate and any lingering signal will -- cgit v1.2.3 From 55f5d0f7770ea625e283aa7878340fc80a70cfd7 Mon Sep 17 00:00:00 2001 From: wwylele Date: Sun, 11 Dec 2016 23:27:30 +0200 Subject: MathUtil: add PI constant --- src/common/math_util.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/common') 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 @@ namespace MathUtil { +static constexpr float PI = 3.14159265f; + inline bool IntervalsIntersect(unsigned start0, unsigned length0, unsigned start1, unsigned length1) { return (std::max(start0, start1) < std::min(start0 + length0, start1 + length1)); -- cgit v1.2.3 From 2e6d8e1321c81e29a85f46c0ff3c0280e3f95b9f Mon Sep 17 00:00:00 2001 From: wwylele Date: Sun, 11 Dec 2016 23:28:55 +0200 Subject: vector math: add implementation of Length and Normalize --- src/common/vector_math.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/common') 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 operator*(const V& f, const Vec2& vec) { typedef Vec2 Vec2f; +template <> +inline float Vec2::Length() const { + return std::sqrt(x * x + y * y); +} + +template <> +inline float Vec2::Normalize() { + float length = Length(); + *this /= length; + return length; +} + template class Vec3 { public: @@ -388,6 +400,13 @@ inline Vec3 Vec3::Normalized() const { return *this / Length(); } +template <> +inline float Vec3::Normalize() { + float length = Length(); + *this /= length; + return length; +} + typedef Vec3 Vec3f; template -- cgit v1.2.3 From 6479f63091da35b1ac6f3930532cc8bde466f6c4 Mon Sep 17 00:00:00 2001 From: wwylele Date: Sun, 11 Dec 2016 23:32:01 +0200 Subject: Common: add Quaternion --- src/common/CMakeLists.txt | 1 + src/common/quaternion.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/common/quaternion.h (limited to 'src/common') 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 microprofileui.h platform.h profiler_reporting.h + quaternion.h scm_rev.h scope_exit.h 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 @@ +// Copyright 2016 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "common/vector_math.h" + +namespace Math { + +template +class Quaternion { +public: + Math::Vec3 xyz; + T w; + + Quaternion Inverse() const { + return {-xyz, w}; + } + + Quaternion operator+(const Quaternion& other) const { + return {xyz + other.xyz, w + other.w}; + } + + Quaternion operator-(const Quaternion& other) const { + return {xyz - other.xyz, w - other.w}; + } + + Quaternion operator*(const Quaternion& other) const { + return {xyz * other.w + other.xyz * w + Cross(xyz, other.xyz), + w * other.w - Dot(xyz, other.xyz)}; + } +}; + +template +auto QuaternionRotate(const Quaternion& q, const Math::Vec3& v) { + return v + 2 * Cross(q.xyz, Cross(q.xyz, v) + v * q.w); +} + +inline Quaternion MakeQuaternion(const Math::Vec3& axis, float angle) { + return {axis * std::sin(angle / 2), std::cos(angle / 2)}; +} + +} // namspace Math -- cgit v1.2.3