Merge pull request #8545 from jordan-woyak/imu-cursor-centering

WiimoteEmu: IMU pointing behavior improvements and code cleanup.
This commit is contained in:
Pierre Bourdon
2020-01-15 12:10:57 +01:00
committed by GitHub
8 changed files with 148 additions and 98 deletions

View File

@ -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 = {};

View File

@ -273,6 +273,7 @@ class Matrix33
{
public:
static Matrix33 Identity();
static Matrix33 FromQuaternion(float x, float y, float z, float w);
// Return a rotation matrix around the x,y,z axis
static Matrix33 RotateX(float rad);
@ -287,6 +288,8 @@ public:
static void Multiply(const Matrix33& a, const Matrix33& b, Matrix33* result);
static void Multiply(const Matrix33& a, const Vec3& vec, Vec3* result);
Matrix33 Inverted() const;
Matrix33& operator*=(const Matrix33& rhs)
{
Multiply(*this, rhs, this);