2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2026-01-13 20:21:48 +00:00

chore: replace custom vector, quaternion, matrix implementation with eigen library

This commit is contained in:
Jan
2024-05-12 18:35:32 +02:00
parent 9979275f8c
commit c89a6631b0
16 changed files with 147 additions and 694 deletions

View File

@@ -1,35 +0,0 @@
#pragma once
template<typename T> class Matrix
{
public:
T m_data[4][4];
Matrix()
: m_data{
{T(1.0), 0, 0, 0 },
{0, T(1.0), 0, 0 },
{0, 0, T(1.0), 0 },
{0, 0, 0, T(1.0)},
}
{
}
Matrix(T d00, T d01, T d02, T d03, T d10, T d11, T d12, T d13, T d20, T d21, T d22, T d23, T d30, T d31, T d32, T d33)
: m_data{
{d00, d01, d02, d03},
{d10, d11, d12, d13},
{d20, d21, d22, d23},
{d30, d31, d32, d33},
}
{
}
static Matrix<T> Identity()
{
return Matrix();
}
};
typedef Matrix<float> Matrix32;
typedef Matrix<double> Matrix64;

View File

@@ -1,186 +0,0 @@
#pragma once
#include "Matrix.h"
#include "Utils/ClassUtils.h"
template<typename T> class Quaternion
{
public:
T m_x;
T m_y;
T m_z;
T m_w;
Quaternion()
{
m_x = T(0);
m_y = T(0);
m_z = T(0);
m_w = T(1);
}
Quaternion(T x, T y, T z, T w)
{
m_x = x;
m_y = y;
m_z = z;
m_w = w;
}
_NODISCARD Matrix<T> ToMatrix() const
{
const T xx = m_x * m_x;
const T xy = m_x * m_y;
const T xz = m_x * m_z;
const T xw = m_x * m_w;
const T yy = m_y * m_y;
const T yz = m_y * m_z;
const T yw = m_y * m_w;
const T zz = m_z * m_z;
const T zw = m_z * m_w;
const T m00 = 1 - 2 * yy - 2 * zz;
const T m01 = 2 * xy - 2 * zw;
const T m02 = 2 * xz + 2 * yw;
const T m10 = 2 * xy + 2 * zw;
const T m11 = 1 - 2 * xx - 2 * zz;
const T m12 = 2 * yz - 2 * xw;
const T m20 = 2 * xz - 2 * yw;
const T m21 = 2 * yz + 2 * xw;
const T m22 = 1 - 2 * xx - 2 * yy;
return Matrix<T>(m00, m01, m02, 0, m10, m11, m12, 0, m20, m21, m22, 0, 0, 0, 0, T(1.0));
}
static T dot(const Quaternion& q1, const Quaternion& q2)
{
return static_cast<T>((q1.m_x * q2.m_x) + (q1.m_y * q2.m_y) + (q1.m_z * q2.m_z) + (q1.m_w * q2.m_w));
}
static Quaternion& conj(Quaternion& result)
{
result.m_x = -result.m_x;
result.m_y = -result.m_y;
result.m_z = -result.m_z;
return result;
}
static Quaternion& invert(Quaternion& result)
{
// from game programming gems p198
// do result = conj( q ) / norm( q )
Quaternion::conj(result);
// return if norm() is near 0 (divide by 0 would result in NaN)
T l = result.lengthSquared();
if (l < static_cast<T>(0.0001))
{
return result;
}
T l_inv = static_cast<T>(1.0) / l;
result.m_x *= l_inv;
result.m_y *= l_inv;
result.m_z *= l_inv;
result.m_w *= l_inv;
return result;
}
T lengthSquared() const
{
return Quaternion::dot(*this, *this);
}
T length() const
{
return sqrt(lengthSquared());
}
void Normalize()
{
const auto l = length();
// return if no magnitude (already as normalized as possible)
if (l < static_cast<T>(0.0001))
return;
T inverseLength = static_cast<T>(1.0) / l;
m_x *= inverseLength;
m_y *= inverseLength;
m_z *= inverseLength;
m_w *= inverseLength;
}
friend Quaternion operator+(const Quaternion& lhs, const Quaternion& rhs)
{
return Quaternion(lhs.m_x + rhs.m_x, lhs.m_y + rhs.m_y, lhs.m_z + rhs.m_z, lhs.m_w + rhs.m_w);
}
friend Quaternion operator-(const Quaternion& lhs, const Quaternion& rhs)
{
return Quaternion(lhs.m_x - rhs.m_x, lhs.m_y - rhs.m_y, lhs.m_z - rhs.m_z, lhs.m_w - rhs.m_w);
}
friend Quaternion& operator+=(Quaternion& lhs, const Quaternion& rhs)
{
lhs.m_x += rhs.m_x;
lhs.m_y += rhs.m_y;
lhs.m_z += rhs.m_z;
lhs.m_w += rhs.m_w;
return lhs;
}
friend Quaternion& operator-=(Quaternion& lhs, const Quaternion& rhs)
{
lhs.m_x -= rhs.m_x;
lhs.m_y -= rhs.m_y;
lhs.m_z -= rhs.m_z;
lhs.m_w -= rhs.m_w;
return lhs;
}
friend Quaternion operator*(const Quaternion& lhs, const Quaternion& rhs)
{
const T x2 = lhs.m_w * rhs.m_x + lhs.m_x * rhs.m_w + lhs.m_y * rhs.m_z - lhs.m_z * rhs.m_y;
const T y2 = lhs.m_w * rhs.m_y + lhs.m_y * rhs.m_w + lhs.m_z * rhs.m_x - lhs.m_x * rhs.m_z;
const T z2 = lhs.m_w * rhs.m_z + lhs.m_z * rhs.m_w + lhs.m_x * rhs.m_y - lhs.m_y * rhs.m_x;
const T w2 = lhs.m_w * rhs.m_w - lhs.m_x * rhs.m_x - lhs.m_y * rhs.m_y - lhs.m_z * rhs.m_z;
return Quaternion(x2, y2, z2, w2);
}
friend Quaternion operator/(const Quaternion& lhs, const Quaternion& rhs)
{
Quaternion rhsInv = rhs;
Quaternion::invert(rhsInv);
return lhs * rhsInv;
}
friend Quaternion& operator*=(Quaternion& lhs, const Quaternion& rhs)
{
const T x2 = lhs.m_w * rhs.m_x + lhs.m_x * rhs.m_w + lhs.m_y * rhs.m_z - lhs.m_z * rhs.m_y;
const T y2 = lhs.m_w * rhs.m_y + lhs.m_y * rhs.m_w + lhs.m_z * rhs.m_x - lhs.m_x * rhs.m_z;
const T z2 = lhs.m_w * rhs.m_z + lhs.m_z * rhs.m_w + lhs.m_x * rhs.m_y - lhs.m_y * rhs.m_x;
const T w2 = lhs.m_w * rhs.m_w - lhs.m_x * rhs.m_x - lhs.m_y * rhs.m_y - lhs.m_z * rhs.m_z;
lhs.m_x = x2;
lhs.m_y = y2;
lhs.m_z = z2;
lhs.m_w = w2;
return lhs;
}
friend Quaternion& operator/=(Quaternion& lhs, const Quaternion& rhs)
{
Quaternion rhsInv = rhs;
Quaternion::invert(rhsInv);
lhs *= rhsInv;
return lhs;
}
};
typedef Quaternion<float> Quaternion32;
typedef Quaternion<double> Quaternion64;

View File

@@ -1,375 +0,0 @@
#pragma once
#include "Utils/ClassUtils.h"
#include <cassert>
#include <cstddef>
template<typename T> class Vector2
{
T m_value[2];
public:
Vector2()
: m_value{T(0), T(0)}
{
}
Vector2(T x, T y)
: m_value{x, y}
{
}
explicit Vector2(const T* value)
: m_value{value[0], value[1]}
{
}
~Vector2() = default;
Vector2(const Vector2& other) = default;
Vector2(Vector2&& other) noexcept = default;
Vector2& operator=(const Vector2& other) = default;
Vector2& operator=(Vector2&& other) noexcept = default;
_NODISCARD T& operator()(const size_t index)
{
assert(index < 2);
return m_value[index];
}
_NODISCARD const T& operator()(const size_t index) const
{
assert(index < 2);
return m_value[index];
}
_NODISCARD T& x()
{
return m_value[0];
}
_NODISCARD T& y()
{
return m_value[1];
}
_NODISCARD const T& x() const
{
return m_value[0];
}
_NODISCARD const T& y() const
{
return m_value[1];
}
friend Vector2 operator+(const Vector2& lhs, const Vector2& rhs)
{
return Vector2(lhs.m_value[0] + rhs.m_value[0], lhs.m_value[1] + rhs.m_value[1]);
}
friend Vector2 operator-(const Vector2& lhs, const Vector2& rhs)
{
return Vector2(lhs.m_value[0] - rhs.m_value[0], lhs.m_value[1] - rhs.m_value[1]);
}
friend Vector2& operator+=(Vector2& lhs, const Vector2& rhs)
{
lhs.m_value[0] += rhs.m_value[0];
lhs.m_value[1] += rhs.m_value[1];
return lhs;
}
friend Vector2& operator-=(Vector2& lhs, const Vector2& rhs)
{
lhs.m_value[0] -= rhs.m_value[0];
lhs.m_value[1] -= rhs.m_value[1];
return lhs;
}
};
typedef Vector2<float> Vector2f;
typedef Vector2<double> Vector2d;
template<typename T> class Vector3
{
T m_value[3];
public:
Vector3()
: m_value{T(0), T(0), T(0)}
{
}
Vector3(T x, T y, T z)
: m_value{x, y, z}
{
}
explicit Vector3(const T* value)
: m_value{value[0], value[1], value[2]}
{
}
~Vector3() = default;
Vector3(const Vector3& other) = default;
Vector3(Vector3&& other) noexcept = default;
Vector3& operator=(const Vector3& other) = default;
Vector3& operator=(Vector3&& other) noexcept = default;
_NODISCARD T& operator()(const size_t index)
{
assert(index < 3);
return m_value[index];
}
_NODISCARD const T& operator()(const size_t index) const
{
assert(index < 3);
return m_value[index];
}
_NODISCARD T& x()
{
return m_value[0];
}
_NODISCARD T& y()
{
return m_value[1];
}
_NODISCARD T& z()
{
return m_value[2];
}
_NODISCARD const T& x() const
{
return m_value[0];
}
_NODISCARD const T& y() const
{
return m_value[1];
}
_NODISCARD const T& z() const
{
return m_value[2];
}
_NODISCARD T& r()
{
return m_value[0];
}
_NODISCARD T& g()
{
return m_value[1];
}
_NODISCARD T& b()
{
return m_value[2];
}
_NODISCARD const T& r() const
{
return m_value[0];
}
_NODISCARD const T& g() const
{
return m_value[1];
}
_NODISCARD const T& b() const
{
return m_value[2];
}
friend Vector3 operator+(const Vector3& lhs, const Vector3& rhs)
{
return Vector3(lhs.m_value[0] + rhs.m_value[0], lhs.m_value[1] + rhs.m_value[1], lhs.m_value[2] + rhs.m_value[2]);
}
friend Vector3 operator-(const Vector3& lhs, const Vector3& rhs)
{
return Vector3(lhs.m_value[0] - rhs.m_value[0], lhs.m_value[1] - rhs.m_value[1], lhs.m_value[2] - rhs.m_value[2]);
}
friend Vector3& operator+=(Vector3& lhs, const Vector3& rhs)
{
lhs.m_value[0] += rhs.m_value[0];
lhs.m_value[1] += rhs.m_value[1];
lhs.m_value[2] += rhs.m_value[2];
return lhs;
}
friend Vector3& operator-=(Vector3& lhs, const Vector3& rhs)
{
lhs.m_value[0] -= rhs.m_value[0];
lhs.m_value[1] -= rhs.m_value[1];
lhs.m_value[2] -= rhs.m_value[2];
return lhs;
}
};
typedef Vector3<float> Vector3f;
typedef Vector3<double> Vector3d;
template<typename T> class Vector4
{
T m_value[4];
public:
Vector4()
: m_value{T(0), T(0), T(0), T(0)}
{
}
Vector4(T x, T y, T z, T w)
: m_value{x, y, z, w}
{
}
explicit Vector4(const T* value)
: m_value{value[0], value[1], value[2], value[3]}
{
}
~Vector4() = default;
Vector4(const Vector4& other) = default;
Vector4(Vector4&& other) noexcept = default;
Vector4& operator=(const Vector4& other) = default;
Vector4& operator=(Vector4&& other) noexcept = default;
_NODISCARD T& operator()(const size_t index)
{
assert(index < 4);
return m_value[index];
}
_NODISCARD const T& operator()(const size_t index) const
{
assert(index < 4);
return m_value[index];
}
_NODISCARD T& x()
{
return m_value[0];
}
_NODISCARD T& y()
{
return m_value[1];
}
_NODISCARD T& z()
{
return m_value[2];
}
_NODISCARD T& w()
{
return m_value[3];
}
_NODISCARD const T& x() const
{
return m_value[0];
}
_NODISCARD const T& y() const
{
return m_value[1];
}
_NODISCARD const T& z() const
{
return m_value[2];
}
_NODISCARD const T& w() const
{
return m_value[3];
}
_NODISCARD T& r()
{
return m_value[0];
}
_NODISCARD T& g()
{
return m_value[1];
}
_NODISCARD T& b()
{
return m_value[2];
}
_NODISCARD T& a()
{
return m_value[3];
}
_NODISCARD const T& r() const
{
return m_value[0];
}
_NODISCARD const T& g() const
{
return m_value[1];
}
_NODISCARD const T& b() const
{
return m_value[2];
}
_NODISCARD const T& a() const
{
return m_value[3];
}
friend Vector4 operator+(const Vector4& lhs, const Vector4& rhs)
{
return Vector4(lhs.m_value[0] + rhs.m_value[0], lhs.m_value[1] + rhs.m_value[1], lhs.m_value[2] + rhs.m_value[2], lhs.m_value[3] + rhs.m_value[3]);
}
friend Vector4 operator-(const Vector4& lhs, const Vector4& rhs)
{
return Vector4(lhs.m_value[0] - rhs.m_value[0], lhs.m_value[1] - rhs.m_value[1], lhs.m_value[2] - rhs.m_value[2], lhs.m_value[3] - rhs.m_value[3]);
}
friend Vector4& operator+=(Vector4& lhs, const Vector4& rhs)
{
lhs.m_value[0] += rhs.m_value[0];
lhs.m_value[1] += rhs.m_value[1];
lhs.m_value[2] += rhs.m_value[2];
lhs.m_value[3] += rhs.m_value[3];
return lhs;
}
friend Vector4& operator-=(Vector4& lhs, const Vector4& rhs)
{
lhs.m_value[0] -= rhs.m_value[0];
lhs.m_value[1] -= rhs.m_value[1];
lhs.m_value[2] -= rhs.m_value[2];
lhs.m_value[3] -= rhs.m_value[3];
return lhs;
}
};
typedef Vector4<float> Vector4f;
typedef Vector4<double> Vector4d;