summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Matrix.cpp
diff options
context:
space:
mode:
authorPierre Bourdon <delroth@gmail.com>2020-01-15 12:10:57 +0100
committerGitHub <noreply@github.com>2020-01-15 12:10:57 +0100
commit1ac3264d5d5c21b6b5ac9eb5a756e7cf142423c8 (patch)
tree80ba0d531ae047584a7cfa5f3224a6748d089578 /Source/Core/Common/Matrix.cpp
parentab07841e1bfdd88e4498106df8f4820270f611fb (diff)
parent0aacf3a62768eac380800a1b8279aa642b17ca92 (diff)
Merge pull request #8545 from jordan-woyak/imu-cursor-centering
WiimoteEmu: IMU pointing behavior improvements and code cleanup.
Diffstat (limited to 'Source/Core/Common/Matrix.cpp')
-rw-r--r--Source/Core/Common/Matrix.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/Source/Core/Common/Matrix.cpp b/Source/Core/Common/Matrix.cpp
index 9581a5a4ce..e64cc91be0 100644
--- a/Source/Core/Common/Matrix.cpp
+++ b/Source/Core/Common/Matrix.cpp
@@ -45,6 +45,22 @@ Matrix33 Matrix33::Identity()
return mtx;
}
+Matrix33 Matrix33::FromQuaternion(float qx, float qy, float qz, float qw)
+{
+ // Normalize.
+ const float n = 1.0f / sqrt(qx * qx + qy * qy + qz * qz + qw * qw);
+ qx *= n;
+ qy *= n;
+ qz *= n;
+ qw *= n;
+
+ return {
+ 1 - 2 * qy * qy - 2 * qz * qz, 2 * qx * qy - 2 * qz * qw, 2 * qx * qz + 2 * qy * qw,
+ 2 * qx * qy + 2 * qz * qw, 1 - 2 * qx * qx - 2 * qz * qz, 2 * qy * qz - 2 * qx * qw,
+ 2 * qx * qz - 2 * qy * qw, 2 * qy * qz + 2 * qx * qw, 1 - 2 * qx * qx - 2 * qy * qy,
+ };
+}
+
Matrix33 Matrix33::RotateX(float rad)
{
const float s = std::sin(rad);
@@ -120,6 +136,33 @@ void Matrix33::Multiply(const Matrix33& a, const Vec3& vec, Vec3* result)
result->data = MatrixMultiply<3, 3, 1>(a.data, vec.data);
}
+Matrix33 Matrix33::Inverted() const
+{
+ const auto m = [this](int x, int y) { return data[y + x * 3]; };
+
+ const auto det = m(0, 0) * (m(1, 1) * m(2, 2) - m(2, 1) * m(1, 2)) -
+ m(0, 1) * (m(1, 0) * m(2, 2) - m(1, 2) * m(2, 0)) +
+ m(0, 2) * (m(1, 0) * m(2, 1) - m(1, 1) * m(2, 0));
+
+ const auto invdet = 1 / det;
+
+ Matrix33 result;
+
+ const auto minv = [&result](int x, int y) -> auto& { return result.data[y + x * 3]; };
+
+ minv(0, 0) = (m(1, 1) * m(2, 2) - m(2, 1) * m(1, 2)) * invdet;
+ minv(0, 1) = (m(0, 2) * m(2, 1) - m(0, 1) * m(2, 2)) * invdet;
+ minv(0, 2) = (m(0, 1) * m(1, 2) - m(0, 2) * m(1, 1)) * invdet;
+ minv(1, 0) = (m(1, 2) * m(2, 0) - m(1, 0) * m(2, 2)) * invdet;
+ minv(1, 1) = (m(0, 0) * m(2, 2) - m(0, 2) * m(2, 0)) * invdet;
+ minv(1, 2) = (m(1, 0) * m(0, 2) - m(0, 0) * m(1, 2)) * invdet;
+ minv(2, 0) = (m(1, 0) * m(2, 1) - m(2, 0) * m(1, 1)) * invdet;
+ minv(2, 1) = (m(2, 0) * m(0, 1) - m(0, 0) * m(2, 1)) * invdet;
+ minv(2, 2) = (m(0, 0) * m(1, 1) - m(1, 0) * m(0, 1)) * invdet;
+
+ return result;
+}
+
Matrix44 Matrix44::Identity()
{
Matrix44 mtx = {};