feat(matrix): add C33Matrix class

This commit is contained in:
VDm 2025-05-29 01:39:17 +04:00
parent f3b5a84428
commit 95bbe515dd
4 changed files with 163 additions and 11 deletions

View file

@ -0,0 +1,61 @@
#include "tempest/matrix/C33Matrix.hpp"
#include "tempest/math/CMath.hpp"
#include <storm/Error.hpp>
float C33Matrix::Det(float a, float b, float c, float d) {
return (a * d) - (b * c);
}
C33Matrix C33Matrix::Rotation(float angle) {
float cosa = cos(angle);
float sina = sin(angle);
C33Matrix result;
result.a0 = cosa;
result.a1 = sina;
result.a2 = 0.0;
result.b0 = -sina;
result.b1 = cosa;
result.b2 = 0.0;
result.c0 = 0.0;
result.c1 = 0.0;
result.c2 = 1.0;
return result;
}
C33Matrix C33Matrix::Rotation(float angle, const C3Vector& axis, bool unit) {
C3Vector axis_ = axis;
if (!unit) {
axis_.Normalize();
}
STORM_ASSERT(axis_.IsUnit());
float sina = CMath::sin(angle);
float cosa = CMath::cos(angle);
float xs = axis_.x * sina;
float ys = axis_.y * sina;
float zs = axis_.z * sina;
float one_c = 1.0f - cosa;
C33Matrix result;
result.a0 = axis_.x * axis_.x * one_c + cosa;
result.a1 = one_c * axis_.y * axis_.x + zs;
result.a2 = one_c * axis_.z * axis_.x - ys;
result.b0 = one_c * axis_.y * axis_.x - zs;
result.b1 = axis_.y * axis_.y * one_c + cosa;
result.b2 = one_c * axis_.z * axis_.y + xs;
result.c0 = one_c * axis_.z * axis_.x + ys;
result.c1 = one_c * axis_.z * axis_.y - xs;
result.c2 = axis_.z * axis_.z * one_c + cosa;
return result;
}

View file

@ -0,0 +1,93 @@
#ifndef TEMPEST_MATRIX_C_33MATRIX_HPP
#define TEMPEST_MATRIX_C_33MATRIX_HPP
#include "tempest/vector/C2Vector.hpp"
#include "tempest/vector/C3Vector.hpp"
class C4Quaternion;
class C33Matrix {
public:
enum : uint32_t {
eComponents = 9
};
// Static functions
static float Det(float a, float b, float c, float d);
static C33Matrix Rotation(float angle);
static C33Matrix Rotation(float angle, const C3Vector& axis, bool unit);
// Member variables
float a0 = 1.0f;
float a1 = 0.0f;
float a2 = 0.0f;
float b0 = 0.0f;
float b1 = 1.0f;
float b2 = 0.0f;
float c0 = 0.0f;
float c1 = 0.0f;
float c2 = 1.0f;
// Member functions
C33Matrix() = default;
C33Matrix(const C3Vector& r0, const C3Vector& r1, const C3Vector& r2)
: a0(r0.x)
, a1(r0.y)
, a2(r0.z)
, b0(r1.x)
, b1(r1.y)
, b2(r1.z)
, c0(r2.x)
, c1(r2.y)
, c2(r2.z) {};
C33Matrix(float a0, float a1, float a2, float b0, float b1, float b2, float c0, float c1, float c2)
: a0(a0)
, a1(a1)
, a2(a2)
, b0(b0)
, b1(b1)
, b2(b2)
, c0(c0)
, c1(c1)
, c2(c2) {};
explicit C33Matrix(float a)
: a0(a)
, a1(a)
, a2(a)
, b0(a)
, b1(a)
, b2(a)
, c0(a)
, c1(a)
, c2(a) {};
C33Matrix& operator+=(const C33Matrix& a);
C33Matrix& operator-=(const C33Matrix& a);
C33Matrix& operator*=(float a);
C33Matrix& operator*=(const C33Matrix& a);
C33Matrix& operator/=(float a);
void Zero();
void Identity();
float Trace();
void Scale(float scale);
void Scale(float x, float y);
void Scale(const C2Vector& scale);
void Scale(float x, float y, float z);
void Scale(const C3Vector& scale);
void Rotate(float angle);
void Rotate(const C4Quaternion& rotation);
void Rotate(float angle, const C3Vector& axis, bool unit);
void Translate(const C2Vector& move);
C33Matrix Transpose() const;
float Determinant() const;
C33Matrix Cofactors() const;
C33Matrix Adjoint() const;
C33Matrix Inverse() const;
C33Matrix Inverse(float det) const;
C33Matrix AffineInverse(const C3Vector& v);
C33Matrix AffineInverse(float a);
C33Matrix AffineInverse();
};
#endif

View file

@ -5,10 +5,6 @@
class C2Vector {
public:
// Member variables
float x = 0.0f;
float y = 0.0f;
enum : uint32_t {
eComponents = 2
};
@ -21,13 +17,16 @@ class C2Vector {
static float Dot(const C2Vector& l, const C2Vector& r);
static float Cross(const C2Vector& l, const C2Vector& r);
// Member variables
float x = 0.0f;
float y = 0.0f;
// Member functions
C2Vector() = default;
C2Vector(float x, float y)
: x(x)
, y(y) {};
C2Vector(float a)
explicit C2Vector(float a)
: x(a)
, y(a) {};

View file

@ -8,11 +8,6 @@ class C44Matrix;
class C3Vector {
public:
// Member variables
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
enum : uint32_t {
eComponents = 3
};
@ -32,6 +27,10 @@ class C3Vector {
static C3Vector ProjectionOnPlane(const C3Vector& v, const C3Vector& normal);
static C3Vector NearestOnPlane(const C3Vector& p, const C3Vector& onplane, const C3Vector& normal);
// Member variables
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
// Member functions
C3Vector() = default;
@ -47,7 +46,7 @@ class C3Vector {
: x(x)
, y(y)
, z(z) {};
C3Vector(float a)
explicit C3Vector(float a)
: x(a)
, y(a)
, z(a) {};