diff --git a/CMakeLists.txt b/CMakeLists.txt index 681c977..d61999d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ else() "Quaternion.cpp" "Polar.cpp" "Spherical.cpp" + "Matrix.cpp" ) enable_testing() @@ -41,6 +42,7 @@ else() "test/Vector2_test.cc" "test/Vector3_test.cc" "test/Quaternion_test.cc" + "test/Matrix_test.cc" ) target_link_libraries( VectorAlgebraTest diff --git a/Matrix.cpp b/Matrix.cpp new file mode 100644 index 0000000..d32e95b --- /dev/null +++ b/Matrix.cpp @@ -0,0 +1,55 @@ +#include "Matrix.h" + +template <> void MatrixOf::Transpose(MatrixOf *r) { + // Check dimensions first + // We dont care about the rows and cols (we overwrite them) + // but the data size should be equal to avoid problems + // We cannot check the data size directly, but the row*col should be equal + int matrixSize = this->rows * this->cols; + if (matrixSize != r->rows * r->cols) + // Exception??? For now we don't do anything + return; + + for (int dataIx = 0; dataIx < matrixSize; dataIx++) { + int rowIx = dataIx / rows; + int colIx = dataIx & rows; + r->data[dataIx] = this->data[cols * colIx + rowIx]; + } + r->rows = cols; + r->cols = rows; +} + +template <> +void MatrixOf::Multiply(MatrixOf *m1, MatrixOf *m2, + MatrixOf *r) { + for (int rowIx1 = 0; rowIx1 < m1->rows; rowIx1++) { + for (int colIx2 = 0; colIx2 < m2->cols; colIx2++) { + int dataIx = colIx2 * m2->cols + rowIx1; + r->data[dataIx] = 0.0F; + for (int rowIx2 = 0; rowIx2 < m2->rows; rowIx2++) { + int dataIx1 = rowIx2 * m1->cols + rowIx1; + int dataIx2 = colIx2 * m2->cols + rowIx2; + r->data[dataIx] += m1->data[dataIx1] * m2->data[dataIx2]; + } + } + } +} + +template <> +void MatrixOf::Multiply(MatrixOf *m2, MatrixOf *r) { + Multiply(this, m2, r); +} + +template <> Vector3 MatrixOf::Multiply(MatrixOf *m, Vector3 v) { + float *vData = new float[3]{v.x, v.y, v.z}; + MatrixOf v_m = MatrixOf(3, 1, vData); + float *rData = new float[3]{}; + MatrixOf r_m = MatrixOf(3, 1, rData); + + Multiply(m, &v_m, &r_m); + + Vector3 r = Vector3(r_m.data[0], r_m.data[1], r_m.data[2]); + delete[] vData; + delete[] rData; + return r; +} \ No newline at end of file diff --git a/Matrix.h b/Matrix.h new file mode 100644 index 0000000..c4ed428 --- /dev/null +++ b/Matrix.h @@ -0,0 +1,27 @@ +#ifndef MATRIX_H +#define MATRIX_H + +#include "Vector3.h" + +/// @brief Single precision float matrix +template class MatrixOf { +public: + MatrixOf(int rows, int cols, T *data) : rows(rows), cols(cols), data(data) {} + MatrixOf(Vector3 v); // creates a 3,1 matrix + + /// @brief Transpose with result in matrix m + /// @param r The matrix in which the transposed matrix is stored + void Transpose(MatrixOf *r); + + static void Multiply(MatrixOf *m1, MatrixOf *m2, MatrixOf *r); + void Multiply(MatrixOf *m, MatrixOf *r); + + static Vector3 Multiply(MatrixOf *m, Vector3 v); + +private: + int rows; + int cols; + T *data; +}; + +#endif \ No newline at end of file diff --git a/test/Matrix_test.cc b/test/Matrix_test.cc new file mode 100644 index 0000000..4b5fd33 --- /dev/null +++ b/test/Matrix_test.cc @@ -0,0 +1,47 @@ +#if GTEST +#include +#include +#include + +#include "Matrix.h" + +TEST(MatrixSingle, Init) { + // zero + float data0[] = {}; + MatrixOf m0 = MatrixOf(0, 0, data0); + + // one + float data1[] = {1.0F}; + MatrixOf m1 = MatrixOf(1, 1, data1); + + // two + float data2[] = {1.0F, 2.0F, 3.0F, 4.0F}; + MatrixOf m2 = MatrixOf(2, 2, data2); + + // negative + MatrixOf m_1 = MatrixOf(-1, -1, data0); +} + +TEST(MatrixSingle, Transpose) { + float data1[] = {1.0F}; + MatrixOf m = MatrixOf(1, 1, data1); + + float data2[] = {1.0F}; + MatrixOf r = MatrixOf(1, 1, data2); + m.Transpose(&r); + + float data2[] = {1.0F, 2.0F, 3.0F, 4.0F}; + m = MatrixOf(2, 2, data1); + m.Transpose(&r); + EXPECT_FLOAT_EQ(r.data[0], 1.0F); + EXPECT_FLOAT_EQ(r.data[1], 3.0F); + EXPECT_FLOAT_EQ(r.data[2], 2.0F); + EXPECT_FLOAT_EQ(r.data[3], 4.0F); +} + +TEST(MatrixSingle, Multiply) { + float data[] = {1.0F}; + MatrixOf m = MatrixOf(1, 1, data); +} + +#endif \ No newline at end of file