From 593cab5a5b14f1db0d2de317d0d478c08e5bae01 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 27 Dec 2023 00:28:40 +0100 Subject: [PATCH] Implemented MLPPMatrix::rotate. --- mlpp/lin_alg/lin_alg.cpp | 15 ------ mlpp/lin_alg/lin_alg.h | 2 - mlpp/lin_alg/mlpp_matrix.cpp | 89 ++++++++++++++++++++++++++++++++---- mlpp/lin_alg/mlpp_matrix.h | 13 ++++-- 4 files changed, 88 insertions(+), 31 deletions(-) diff --git a/mlpp/lin_alg/lin_alg.cpp b/mlpp/lin_alg/lin_alg.cpp index 6f2bb19..9190857 100644 --- a/mlpp/lin_alg/lin_alg.cpp +++ b/mlpp/lin_alg/lin_alg.cpp @@ -1501,21 +1501,6 @@ Ref MLPPLinAlg::cosnv(const Ref &a) { return out; } -/* -std::vector> MLPPLinAlg::rotate(std::vector> A, real_t theta, int axis) { - std::vector> rotationMatrix = { { Math::cos(theta), -Math::sin(theta) }, { Math::sin(theta), Math::cos(theta) } }; - if (axis == 0) { - rotationMatrix = { { 1, 0, 0 }, { 0, Math::cos(theta), -Math::sin(theta) }, { 0, Math::sin(theta), Math::cos(theta) } }; - } else if (axis == 1) { - rotationMatrix = { { Math::cos(theta), 0, Math::sin(theta) }, { 0, 1, 0 }, { -Math::sin(theta), 0, Math::cos(theta) } }; - } else if (axis == 2) { - rotationMatrix = { { Math::cos(theta), -Math::sin(theta), 0 }, { Math::sin(theta), Math::cos(theta), 0 }, { 1, 0, 0 } }; - } - - return matmult(A, rotationMatrix); -} -*/ - Ref MLPPLinAlg::maxnm(const Ref &A, const Ref &B) { Ref C; C.instance(); diff --git a/mlpp/lin_alg/lin_alg.h b/mlpp/lin_alg/lin_alg.h index 3400588..9d76d89 100644 --- a/mlpp/lin_alg/lin_alg.h +++ b/mlpp/lin_alg/lin_alg.h @@ -82,8 +82,6 @@ public: Ref sinnm(const Ref &A); Ref cosnm(const Ref &A); - //std::vector> rotate(std::vector> A, real_t theta, int axis = -1); - Ref maxnm(const Ref &A, const Ref &B); //real_t max(std::vector> A); diff --git a/mlpp/lin_alg/mlpp_matrix.cpp b/mlpp/lin_alg/mlpp_matrix.cpp index f7a09e4..ef3f158 100644 --- a/mlpp/lin_alg/mlpp_matrix.cpp +++ b/mlpp/lin_alg/mlpp_matrix.cpp @@ -1839,20 +1839,85 @@ void MLPPMatrix::cosb(const Ref &A) { } } -/* -std::vector> MLPPMatrix::rotate(std::vector> A, real_t theta, int axis) { - std::vector> rotationMatrix = { { Math::cos(theta), -Math::sin(theta) }, { Math::sin(theta), Math::cos(theta) } }; - if (axis == 0) { - rotationMatrix = { { 1, 0, 0 }, { 0, Math::cos(theta), -Math::sin(theta) }, { 0, Math::sin(theta), Math::cos(theta) } }; +Ref MLPPMatrix::create_rotation_matrix(real_t theta, int axis) { + Ref rotation_matrix; + rotation_matrix.instance(); + + if (axis == -1) { + rotation_matrix->resize(Size2(2, 2)); + real_t *rptr = rotation_matrix->ptrw(); + + rptr[rotation_matrix->calculate_index(0, 0)] = Math::cos(theta); + rptr[rotation_matrix->calculate_index(0, 1)] = -Math::sin(theta); + + rptr[rotation_matrix->calculate_index(1, 0)] = Math::cos(theta); + rptr[rotation_matrix->calculate_index(1, 1)] = Math::cos(theta); + } else if (axis == 0) { + rotation_matrix->resize(Size2(3, 3)); + real_t *rptr = rotation_matrix->ptrw(); + + rptr[rotation_matrix->calculate_index(0, 0)] = 1; + rptr[rotation_matrix->calculate_index(0, 1)] = 0; + rptr[rotation_matrix->calculate_index(0, 2)] = 0; + + rptr[rotation_matrix->calculate_index(1, 0)] = 0; + rptr[rotation_matrix->calculate_index(1, 1)] = Math::cos(theta); + rptr[rotation_matrix->calculate_index(1, 2)] = -Math::sin(theta); + + rptr[rotation_matrix->calculate_index(2, 0)] = 0; + rptr[rotation_matrix->calculate_index(2, 1)] = Math::sin(theta); + rptr[rotation_matrix->calculate_index(2, 2)] = Math::cos(theta); } else if (axis == 1) { - rotationMatrix = { { Math::cos(theta), 0, Math::sin(theta) }, { 0, 1, 0 }, { -Math::sin(theta), 0, Math::cos(theta) } }; + rotation_matrix->resize(Size2(3, 3)); + real_t *rptr = rotation_matrix->ptrw(); + + rptr[rotation_matrix->calculate_index(0, 0)] = Math::cos(theta); + rptr[rotation_matrix->calculate_index(0, 1)] = 0; + rptr[rotation_matrix->calculate_index(0, 2)] = Math::sin(theta); + + rptr[rotation_matrix->calculate_index(1, 0)] = 0; + rptr[rotation_matrix->calculate_index(1, 1)] = 1; + rptr[rotation_matrix->calculate_index(1, 2)] = 0; + + rptr[rotation_matrix->calculate_index(2, 0)] = -Math::sin(theta); + rptr[rotation_matrix->calculate_index(2, 1)] = 0; + rptr[rotation_matrix->calculate_index(2, 2)] = Math::cos(theta); + } else if (axis == 2) { - rotationMatrix = { { Math::cos(theta), -Math::sin(theta), 0 }, { Math::sin(theta), Math::cos(theta), 0 }, { 1, 0, 0 } }; + rotation_matrix->resize(Size2(3, 3)); + real_t *rptr = rotation_matrix->ptrw(); + + rptr[rotation_matrix->calculate_index(0, 0)] = Math::cos(theta); + rptr[rotation_matrix->calculate_index(0, 1)] = -Math::sin(theta); + rptr[rotation_matrix->calculate_index(0, 2)] = 0; + + rptr[rotation_matrix->calculate_index(1, 0)] = Math::sin(theta); + rptr[rotation_matrix->calculate_index(1, 1)] = Math::cos(theta); + rptr[rotation_matrix->calculate_index(1, 2)] = 0; + + rptr[rotation_matrix->calculate_index(2, 0)] = 1; + rptr[rotation_matrix->calculate_index(2, 1)] = 0; + rptr[rotation_matrix->calculate_index(2, 2)] = 0; } - return matmult(A, rotationMatrix); + return rotation_matrix; +} + +void MLPPMatrix::rotate(real_t theta, int axis) { + Ref rm = create_rotation_matrix(theta, axis); + + mult(rm); +} +Ref MLPPMatrix::rotaten(real_t theta, int axis) { + Ref rm = create_rotation_matrix(theta, axis); + + return multn(rm); +} +void MLPPMatrix::rotateb(const Ref &A, real_t theta, int axis) { + Ref rm = create_rotation_matrix(theta, axis); + + multb(A, rm); } -*/ void MLPPMatrix::max(const Ref &B) { ERR_FAIL_COND(!B.is_valid()); @@ -3257,6 +3322,12 @@ void MLPPMatrix::_bind_methods() { ClassDB::bind_method(D_METHOD("cosn"), &MLPPMatrix::cosn); ClassDB::bind_method(D_METHOD("cosb", "A"), &MLPPMatrix::cosb); + ClassDB::bind_method(D_METHOD("create_rotation_matrix", "theta", "axis"), &MLPPMatrix::create_rotation_matrix, -1); + + ClassDB::bind_method(D_METHOD("rotate", "theta", "axis"), &MLPPMatrix::rotate, -1); + ClassDB::bind_method(D_METHOD("rotaten", "theta", "axis"), &MLPPMatrix::rotaten, -1); + ClassDB::bind_method(D_METHOD("rotateb", "A", "theta", "axis"), &MLPPMatrix::rotateb, -1); + ClassDB::bind_method(D_METHOD("max", "B"), &MLPPMatrix::max); ClassDB::bind_method(D_METHOD("maxn", "B"), &MLPPMatrix::maxn); ClassDB::bind_method(D_METHOD("maxb", "A", "B"), &MLPPMatrix::maxb); diff --git a/mlpp/lin_alg/mlpp_matrix.h b/mlpp/lin_alg/mlpp_matrix.h index 3c87b24..b890a18 100644 --- a/mlpp/lin_alg/mlpp_matrix.h +++ b/mlpp/lin_alg/mlpp_matrix.h @@ -14,19 +14,18 @@ #include "core/object/resource.h" -#else +#else +#include "core/containers/vector.h" #include "core/defs.h" #include "core/math_funcs.h" -#include "core/pool_arrays.h" -#include "core/containers/vector.h" #include "core/os/memory.h" +#include "core/pool_arrays.h" #include "gen/resource.h" #endif - #include "mlpp_vector.h" class Image; @@ -245,7 +244,11 @@ public: Ref cosn() const; void cosb(const Ref &A); - //std::vector> rotate(std::vector> A, real_t theta, int axis = -1); + Ref create_rotation_matrix(real_t theta, int axis = -1); + + void rotate(real_t theta, int axis = -1); + Ref rotaten(real_t theta, int axis = -1); + void rotateb(const Ref &A, real_t theta, int axis = -1); void max(const Ref &B); Ref maxn(const Ref &B) const;