// 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 Common { template class Quaternion { public: Vec3 xyz; T w{}; [[nodiscard]] Quaternion Inverse() const { return {-xyz, w}; } [[nodiscard]] Quaternion operator+(const Quaternion& other) const { return {xyz + other.xyz, w + other.w}; } [[nodiscard]] Quaternion operator-(const Quaternion& other) const { return {xyz - other.xyz, w - other.w}; } [[nodiscard]] Quaternion operator*( const Quaternion& other) const { return {xyz * other.w + other.xyz * w + Cross(xyz, other.xyz), w * other.w - Dot(xyz, other.xyz)}; } [[nodiscard]] Quaternion Normalized() const { T length = std::sqrt(xyz.Length2() + w * w); return {xyz / length, w / length}; } }; template [[nodiscard]] auto QuaternionRotate(const Quaternion& q, const Vec3& v) { return v + 2 * Cross(q.xyz, Cross(q.xyz, v) + v * q.w); } [[nodiscard]] inline Quaternion MakeQuaternion(const Vec3& axis, float angle) { return {axis * std::sin(angle / 2), std::cos(angle / 2)}; } } // namespace Common