diff --git a/src/lib/matrix/matrix/Slice.hpp b/src/lib/matrix/matrix/Slice.hpp index 57f7cfcb70..53d24769e9 100644 --- a/src/lib/matrix/matrix/Slice.hpp +++ b/src/lib/matrix/matrix/Slice.hpp @@ -116,6 +116,39 @@ public: return self; } + template + Matrix operator-(const SliceT, Type, P, Q, MM, NN> &other) + { + return Matrix {*this} - other; + } + + + Matrix operator-(const Matrix &other) + { + return Matrix {*this} - other; + } + + Matrix operator-(const Type &other) + { + return Matrix {*this} - other; + } + + template + Matrix operator+(const SliceT, Type, P, Q, MM, NN> &other) + { + return Matrix {*this} + other; + } + + Matrix operator+(const Matrix &other) + { + return Matrix {*this} + other; + } + + Matrix operator+(const Type &other) + { + return Matrix {*this} + other; + } + // allow assigning vectors to a slice that are in the axis template // make this a template function since it only exists for some instantiations SliceT &operator=(const Vector &other) @@ -222,29 +255,19 @@ public: return self; } - SliceT &operator/=(const Type &other) + SliceT &operator/=(const Type &scalar) { - return operator*=(Type(1) / other); + return operator*=(Type(1) / scalar); } - Matrix operator*(const Type &other) const + Matrix operator*(const Type &scalar) const { - const SliceT &self = *this; - Matrix res; - - for (size_t i = 0; i < P; i++) { - for (size_t j = 0; j < Q; j++) { - res(i, j) = self(i, j) * other; - } - } - - return res; + return Matrix {*this} * scalar; } - Matrix operator/(const Type &other) const + Matrix operator/(const Type &scalar) const { - const SliceT &self = *this; - return self * (Type(1) / other); + return (*this) * (1 / scalar); } template diff --git a/src/lib/matrix/matrix/SquareMatrix.hpp b/src/lib/matrix/matrix/SquareMatrix.hpp index 0cd20bc5bc..610e313d38 100644 --- a/src/lib/matrix/matrix/SquareMatrix.hpp +++ b/src/lib/matrix/matrix/SquareMatrix.hpp @@ -317,6 +317,7 @@ public: } }; +using SquareMatrix2f = SquareMatrix; using SquareMatrix3f = SquareMatrix; using SquareMatrix3d = SquareMatrix; diff --git a/src/lib/matrix/matrix/Vector2.hpp b/src/lib/matrix/matrix/Vector2.hpp index 3376445fda..88ef40ba55 100644 --- a/src/lib/matrix/matrix/Vector2.hpp +++ b/src/lib/matrix/matrix/Vector2.hpp @@ -102,7 +102,6 @@ public: }; - using Vector2f = Vector2; using Vector2d = Vector2; diff --git a/src/lib/matrix/test/MatrixSliceTest.cpp b/src/lib/matrix/test/MatrixSliceTest.cpp index 9240e6b5a8..ec53f8eaaf 100644 --- a/src/lib/matrix/test/MatrixSliceTest.cpp +++ b/src/lib/matrix/test/MatrixSliceTest.cpp @@ -262,6 +262,79 @@ TEST(MatrixSliceTest, Slice) float O_check_data_12 [4] = {2.5, 3, 4, 5}; EXPECT_EQ(res_12, (SquareMatrix(O_check_data_12))); } +TEST(MatrixSliceTest, SliceAdditions) +{ + float data[9] = {0, 2, 3, + 4, 5, 6, + 7, 8, 10 + }; + SquareMatrix3f A{data}; + + float operand_data [4] = {2, 1, + -3, -1 + }; + const SquareMatrix2f operand(operand_data); + + // 2x2 Slice + 2x2 Matrix + SquareMatrix2f res_1 = A.slice<2, 2>(1, 0) + operand; + float res_1_check_data[4] = {6, 6, + 4, 7 + }; + EXPECT_EQ(res_1, (SquareMatrix2f(res_1_check_data))); + + // 2x1 Slice + 2x1 Slice + Vector2f res_2 = A.slice<2, 1>(1, 1) + operand.slice<2, 1>(0, 0); + EXPECT_EQ(res_2, Vector2f(7, 5)); + + // 3x3 Slice + Scalar + SquareMatrix3f res_3 = A.slice<3, 3>(0, 0) + (-1); + float res_3_check_data[9] = {-1, 1, 2, + 3, 4, 5, + 6, 7, 9 + }; + EXPECT_EQ(res_3, (SquareMatrix3f(res_3_check_data))); + + // 3x1 Slice + 3 Vector + Vector3f res_4 = A.col(1) + Vector3f(1, -2, 3); + EXPECT_EQ(res_4, Vector3f(3, 3, 11)); + +} +TEST(MatrixSliceTest, SliceSubtractions) +{ + float data[9] = {0, 2, 3, + 4, 5, 6, + 7, 8, 10 + }; + SquareMatrix3f A{data}; + + float operand_data[4] = {2, 1, + -3, -1 + }; + const SquareMatrix2f operand(operand_data); + + // 2x2 Slice - 2x2 Matrix + SquareMatrix2f res_1 = A.slice<2, 2>(1, 0) - operand; + float res_1_check_data[4] = {2, 4, + 10, 9 + }; + EXPECT_EQ(res_1, (SquareMatrix2f(res_1_check_data))); + + // 2x1 Slice - 2x1 Slice + Vector2f res_2 = A.slice<2, 1>(1, 1) - operand.slice<2, 1>(0, 0); + EXPECT_EQ(res_2, Vector2f(3, 11)); + + // 3x3 Slice - Scalar + SquareMatrix3f res_3 = A.slice<3, 3>(0, 0) - (-1); + float res_3_check_data[9] = {1, 3, 4, + 5, 6, 7, + 8, 9, 11 + }; + EXPECT_EQ(res_3, (SquareMatrix3f(res_3_check_data))); + + // 3x1 Slice - 3 Vector + Vector3f res_4 = A.col(1) - Vector3f(1, -2, 3); + EXPECT_EQ(res_4, Vector3f(1, 7, 5)); +} TEST(MatrixSliceTest, XYAssignmentTest) { diff --git a/src/lib/matrix/test/MatrixVector3Test.cpp b/src/lib/matrix/test/MatrixVector3Test.cpp index f125ad92df..ce4f998ac2 100644 --- a/src/lib/matrix/test/MatrixVector3Test.cpp +++ b/src/lib/matrix/test/MatrixVector3Test.cpp @@ -80,4 +80,13 @@ TEST(MatrixVector3Test, Vector3) Vector3f m2(3.1f, 4.1f, 5.1f); EXPECT_EQ(m2, m1 + 2.1f); EXPECT_EQ(m2 - 2.1f, m1); + + // Test Addition and Subtraction of Slices + Vector3f v1(3, 13, 0); + Vector3f v2(42, 6, 256); + + EXPECT_EQ(v1.xy() - v2.xy(), Vector2f(-39, 7)); + EXPECT_EQ(v1.xy() + v2.xy(), Vector2f(45, 19)); + EXPECT_EQ(v1.xy() + 2.f, Vector2f(5, 15)); + EXPECT_EQ(v1.xy() - 2.f, Vector2f(1, 11)); }