From dd963fe88344e81d38924d49ef2c3f59e0510eb3 Mon Sep 17 00:00:00 2001 From: VDm Date: Sun, 15 Jun 2025 20:03:30 +0400 Subject: [PATCH] feat(matrix): update external operators --- tempest/matrix/C33Matrix.cpp | 97 ++++++++++++++------- tempest/matrix/C33Matrix.hpp | 42 ++-------- tempest/matrix/C44Matrix.cpp | 158 ++++++++++++++++++++++++++++++----- tempest/matrix/C44Matrix.hpp | 12 ++- 4 files changed, 227 insertions(+), 82 deletions(-) diff --git a/tempest/matrix/C33Matrix.cpp b/tempest/matrix/C33Matrix.cpp index 5ea1564..4513a56 100644 --- a/tempest/matrix/C33Matrix.cpp +++ b/tempest/matrix/C33Matrix.cpp @@ -1,5 +1,7 @@ #include "tempest/matrix/C33Matrix.hpp" #include "tempest/math/CMath.hpp" +#include "tempest/vector/C2Vector.hpp" +#include "tempest/vector/C3Vector.hpp" #include "tempest/matrix/C44Matrix.hpp" #include "tempest/quaternion/C4Quaternion.hpp" @@ -66,6 +68,18 @@ C33Matrix C33Matrix::Rotation(float angle, const C3Vector& axis, bool unit) { return result; } +C33Matrix::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::C33Matrix(const C44Matrix& m) : a0(m.a0) , a1(m.a1) @@ -78,6 +92,33 @@ C33Matrix::C33Matrix(const C44Matrix& m) , c2(m.c2) { } +C33Matrix::C33Matrix(const C4Quaternion& rotation) { +} + +C33Matrix::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) { +} + +C33Matrix::C33Matrix(float a) + : a0(a) + , a1(a) + , a2(a) + , b0(a) + , b1(a) + , b2(a) + , c0(a) + , c1(a) + , c2(a) { +} + C33Matrix& C33Matrix::operator+=(const C33Matrix& a) { this->a0 += a.a0; this->a1 += a.a1; @@ -331,22 +372,6 @@ C33Matrix C33Matrix::AffineInverse(float a) const { return matrix; } -C33Matrix operator*(const C33Matrix& l, float a) { - float a0 = l.a0 * a; - float a1 = l.a1 * a; - float a2 = l.a2 * a; - - float b0 = l.b0 * a; - float b1 = l.b1 * a; - float b2 = l.b2 * a; - - float c0 = l.c0 * a; - float c1 = l.c1 * a; - float c2 = l.c2 * a; - - return { a0, a1, a2, b0, b1, b2, c0, c1, c2 }; -} - C33Matrix operator*(const C33Matrix& l, const C33Matrix& r) { float a0 = l.a0 * r.a0 + l.a1 * r.b0 + l.a2 * r.c0; float a1 = l.a0 * r.a1 + l.a1 * r.b1 + l.a2 * r.c1; @@ -363,18 +388,34 @@ C33Matrix operator*(const C33Matrix& l, const C33Matrix& r) { return { a0, a1, a2, b0, b1, b2, c0, c1, c2 }; } -C33Matrix operator/(const C33Matrix& l, float a) { - float a0 = l.a0 / a; - float a1 = l.a1 / a; - float a2 = l.a2 / a; +C33Matrix operator*(const C33Matrix& l, float a) { + return { + l.a0 * a, + l.a1 * a, + l.a2 * a, - float b0 = l.b0 / a; - float b1 = l.b1 / a; - float b2 = l.b2 / a; + l.b0 * a, + l.b1 * a, + l.b2 * a, - float c0 = l.c0 / a; - float c1 = l.c1 / a; - float c2 = l.c2 / a; - - return { a0, a1, a2, b0, b1, b2, c0, c1, c2 }; + l.c0 * a, + l.c1 * a, + l.c2 * a, + }; +} + +C33Matrix operator/(const C33Matrix& l, float a) { + return { + l.a0 / a, + l.a1 / a, + l.a2 / a, + + l.b0 / a, + l.b1 / a, + l.b2 / a, + + l.c0 / a, + l.c1 / a, + l.c2 / a, + }; } diff --git a/tempest/matrix/C33Matrix.hpp b/tempest/matrix/C33Matrix.hpp index ef99e7c..f706b60 100644 --- a/tempest/matrix/C33Matrix.hpp +++ b/tempest/matrix/C33Matrix.hpp @@ -1,9 +1,10 @@ #ifndef TEMPEST_MATRIX_C_33MATRIX_HPP #define TEMPEST_MATRIX_C_33MATRIX_HPP -#include "tempest/vector/C2Vector.hpp" -#include "tempest/vector/C3Vector.hpp" +#include +class C2Vector; +class C3Vector; class C44Matrix; class C4Quaternion; @@ -31,37 +32,11 @@ class C33Matrix { // 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(const C3Vector& r0, const C3Vector& r1, const C3Vector& r2); explicit C33Matrix(const C44Matrix& m); - 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) {}; + explicit C33Matrix(const C4Quaternion& rotation); + C33Matrix(float a0, float a1, float a2, float b0, float b1, float b2, float c0, float c1, float c2); + explicit C33Matrix(float a); C33Matrix& operator+=(const C33Matrix& a); C33Matrix& operator-=(const C33Matrix& a); @@ -91,8 +66,9 @@ class C33Matrix { C33Matrix AffineInverse(float a) const; }; -C33Matrix operator*(const C33Matrix& l, float a); C33Matrix operator*(const C33Matrix& l, const C33Matrix& r); +C33Matrix operator*(const C33Matrix& l, float a); + C33Matrix operator/(const C33Matrix& l, float a); #endif diff --git a/tempest/matrix/C44Matrix.cpp b/tempest/matrix/C44Matrix.cpp index c78890b..8d5afb6 100644 --- a/tempest/matrix/C44Matrix.cpp +++ b/tempest/matrix/C44Matrix.cpp @@ -3,7 +3,7 @@ #include "tempest/vector/C3Vector.hpp" #include "tempest/vector/C4Vector.hpp" #include "tempest/matrix/C33Matrix.hpp" -#include "tempest/Quaternion.hpp" +#include "tempest/quaternion/C4Quaternion.hpp" #include @@ -688,28 +688,100 @@ C44Matrix C44Matrix::AffineInverse() const { return matrix; } -C44Matrix operator*(const C44Matrix& l, float a) { - float a0 = l.a0 * a; - float a1 = l.a1 * a; - float a2 = l.a2 * a; - float a3 = l.a3 * a; +C44Matrix operator+(const C44Matrix& l, const C44Matrix& r) { + return { + l.a0 + r.a0, + l.a1 + r.a1, + l.a2 + r.a2, + l.a3 + r.a3, - float b0 = l.b0 * a; - float b1 = l.b1 * a; - float b2 = l.b2 * a; - float b3 = l.b3 * a; + l.b0 + r.b0, + l.b1 + r.b1, + l.b2 + r.b2, + l.b3 + r.b3, - float c0 = l.c0 * a; - float c1 = l.c1 * a; - float c2 = l.c2 * a; - float c3 = l.c3 * a; + l.c0 + r.c0, + l.c1 + r.c1, + l.c2 + r.c2, + l.c3 + r.c3, - float d0 = l.d0 * a; - float d1 = l.d1 * a; - float d2 = l.d2 * a; - float d3 = l.d3 * a; + l.d0 + r.d0, + l.d1 + r.d1, + l.d2 + r.d2, + l.d3 + r.d3 + }; +} - return { a0, a1, a2, a3, b0, b1, b2, b3, c0, c1, c2, c3, d0, d1, d2, d3 }; +C44Matrix operator+(const C44Matrix& l, float a) { + return { + l.a0 + a, + l.a1 + a, + l.a2 + a, + l.a3 + a, + + l.b0 + a, + l.b1 + a, + l.b2 + a, + l.b3 + a, + + l.c0 + a, + l.c1 + a, + l.c2 + a, + l.c3 + a, + + l.d0 + a, + l.d1 + a, + l.d2 + a, + l.d3 + a, + }; +} + +C44Matrix operator-(const C44Matrix& l, const C44Matrix& r) { + return { + l.a0 - r.a0, + l.a1 - r.a1, + l.a2 - r.a2, + l.a3 - r.a3, + + l.b0 - r.b0, + l.b1 - r.b1, + l.b2 - r.b2, + l.b3 - r.b3, + + l.c0 - r.c0, + l.c1 - r.c1, + l.c2 - r.c2, + l.c3 - r.c3, + + l.d0 - r.d0, + l.d1 - r.d1, + l.d2 - r.d2, + l.d3 - r.d3 + }; +} + +C44Matrix operator-(const C44Matrix& l, float a) { + return { + l.a0 - a, + l.a1 - a, + l.a2 - a, + l.a3 - a, + + l.b0 - a, + l.b1 - a, + l.b2 - a, + l.b3 - a, + + l.c0 - a, + l.c1 - a, + l.c2 - a, + l.c3 - a, + + l.d0 - a, + l.d1 - a, + l.d2 - a, + l.d3 - a, + }; } C44Matrix operator*(const C44Matrix& l, const C44Matrix& r) { @@ -735,3 +807,51 @@ C44Matrix operator*(const C44Matrix& l, const C44Matrix& r) { return { a0, a1, a2, a3, b0, b1, b2, b3, c0, c1, c2, c3, d0, d1, d2, d3 }; } + +C44Matrix operator*(const C44Matrix& l, float a) { + return { + l.a0 * a, + l.a1 * a, + l.a2 * a, + l.a3 * a, + + l.b0 * a, + l.b1 * a, + l.b2 * a, + l.b3 * a, + + l.c0 * a, + l.c1 * a, + l.c2 * a, + l.c3 * a, + + l.d0 * a, + l.d1 * a, + l.d2 * a, + l.d3 * a, + }; +} + +C44Matrix operator/(const C44Matrix& l, float a) { + return { + l.a0 / a, + l.a1 / a, + l.a2 / a, + l.a3 / a, + + l.b0 / a, + l.b1 / a, + l.b2 / a, + l.b3 / a, + + l.c0 / a, + l.c1 / a, + l.c2 / a, + l.c3 / a, + + l.d0 / a, + l.d1 / a, + l.d2 / a, + l.d3 / a, + }; +} diff --git a/tempest/matrix/C44Matrix.hpp b/tempest/matrix/C44Matrix.hpp index f93b0c4..1cd87be 100644 --- a/tempest/matrix/C44Matrix.hpp +++ b/tempest/matrix/C44Matrix.hpp @@ -41,7 +41,7 @@ class C44Matrix { C44Matrix() = default; C44Matrix(const C4Vector& r0, const C4Vector& r1, const C4Vector& r2, const C4Vector& r3); explicit C44Matrix(const C33Matrix& m); - C44Matrix(const C4Quaternion& rotation); + explicit C44Matrix(const C4Quaternion& rotation); C44Matrix(float a0, float a1, float a2, float a3, float b0, float b1, float b2, float b3, float c0, float c1, float c2, float c3, float d0, float d1, float d2, float d3); explicit C44Matrix(float a); @@ -71,7 +71,15 @@ class C44Matrix { C44Matrix AffineInverse() const; }; -C44Matrix operator*(const C44Matrix& l, float a); +C44Matrix operator+(const C44Matrix& l, const C44Matrix& r); +C44Matrix operator+(const C44Matrix& l, float a); + +C44Matrix operator-(const C44Matrix& l, const C44Matrix& r); +C44Matrix operator-(const C44Matrix& l, float a); + C44Matrix operator*(const C44Matrix& l, const C44Matrix& r); +C44Matrix operator*(const C44Matrix& l, float a); + +C44Matrix operator/(const C44Matrix& l, float a); #endif