diff --git a/LinearAlgebra/Matrix.cpp b/LinearAlgebra/Matrix.cpp index c7e53c7..5fa6fcd 100644 --- a/LinearAlgebra/Matrix.cpp +++ b/LinearAlgebra/Matrix.cpp @@ -1,5 +1,5 @@ #include "Matrix.h" -#if !defined(NO_STD) +#if !defined(NO_STD) #include #endif @@ -7,6 +7,8 @@ namespace LinearAlgebra { #pragma region Matrix1 +Matrix1::Matrix1() {} + Matrix1::Matrix1(int size) : size(size) { if (this->size == 0) data = nullptr; @@ -257,6 +259,37 @@ void Matrix2::UpdateSlice(int rowStart, } } +Matrix2 Matrix2::DeleteRows(int rowStart, int rowStop) { + Matrix2 r = Matrix2(this->nRows - (rowStop - rowStart), this->nCols); + + int resultRowIx = 0; + for (int i = 0; i < this->nRows; i++) { + if (i >= rowStart && i < rowStop) + continue; + + for (int j = 0; j < this->nCols; j++) + r.data[resultRowIx * r.nCols + j] = this->data[i * this->nCols + j]; + + resultRowIx++; + } + return r; +} + +Matrix2 Matrix2::DeleteColumns(int colStart, int colStop) { + Matrix2 r = Matrix2(this->nRows, this->nCols - (colStop - colStart)); + + for (int i = 0; i < this->nRows; i++) { + int resultColIx = 0; + for (int j = 0; j < this->nCols; j++) { + if (j >= colStart && j < colStop) + continue; + + r.data[i * r.nCols + resultColIx++] = this->data[i * this->nCols + j]; + } + } + return r; +} + /// @brief Compute the Omega matrix of a 3D vector /// @param v The vector /// @return 4x4 Omega matrix diff --git a/LinearAlgebra/Matrix.h b/LinearAlgebra/Matrix.h index ef72922..fcb8055 100644 --- a/LinearAlgebra/Matrix.h +++ b/LinearAlgebra/Matrix.h @@ -12,12 +12,33 @@ class Matrix1 { float* data = nullptr; int size = 0; + Matrix1(); Matrix1(int size); Matrix1(float* data, int size); static Matrix1 FromQuaternion(Quaternion q); Quaternion ToQuaternion(); + Matrix1 Slice(int start, int stop); + + void Set(unsigned int start, unsigned int stop, float value) { + if (stop > this->size) + stop = this->size; + + for (unsigned int ix = start; ix < stop; ix++) + this->data[ix] = value; + } + void Set(unsigned int start, unsigned int stop, Matrix1 source) { + if (stop > this->size) + stop = this->size; + if (stop > source.size) + stop = source.size; + // Do I need to check on start??? + unsigned int sourceIx = 0; + for (unsigned int ix = start; ix < stop; ix++, sourceIx++) + this->data[ix] = source.data[sourceIx]; + } + private: bool externalData = true; }; @@ -96,13 +117,33 @@ class Matrix2 { return r; } + Matrix2 GetRows(int from, int to) { + if (from < 0 || to >= this->nRows) + std::cerr << "Slice index out of range." << "std::endl"; + + Matrix2 result = Matrix2(to-from, this->nCols); + int resultRowIx = 0; + for (int rowIx = from; rowIx < to; rowIx++) { + for (int colIx = 0; colIx < this->nCols; colIx++) + result.data[resultRowIx * result.nCols + colIx] = this->data[rowIx * this->nCols + colIx]; + + resultRowIx++; + } + + return result; + } + Matrix2 Slice(int rawStart, int rowStop, int colStart, int colStop); void UpdateSlice(int rowStart, - int rowStop, - int colStart, - int colStop, - const Matrix2& m) const; + int rowStop, + int colStart, + int colStop, + const Matrix2& m) const; + + Matrix2 DeleteRows(int rowStart, int rowStop); + Matrix2 DeleteColumns(int colStart, int colStop); + // private: // move constructor and move assignment operator Matrix2(Matrix2&& other) noexcept; diff --git a/LinearAlgebra/Vector2.cpp b/LinearAlgebra/Vector2.cpp index 1e34a99..f0d7894 100644 --- a/LinearAlgebra/Vector2.cpp +++ b/LinearAlgebra/Vector2.cpp @@ -180,3 +180,13 @@ Vector2 Vector2::Lerp(const Vector2& v1, const Vector2& v2, float f) { Vector2 v = v1 + (v2 - v1) * f; return v; } + + +#pragma region Vector2Of + +template +Vector2Of::Vector2Of() { + this->horizontal = 0; + this->vertical = 0; +} +#pragma endregion Vector2Of \ No newline at end of file diff --git a/LinearAlgebra/Vector2.h b/LinearAlgebra/Vector2.h index 04fa669..24aff28 100644 --- a/LinearAlgebra/Vector2.h +++ b/LinearAlgebra/Vector2.h @@ -201,6 +201,17 @@ struct Vector2 : Vec2 { static Vector2 Lerp(const Vector2& v1, const Vector2& v2, float f); }; +template +struct Vector2Of { + public: + Vector2Of(); + + T horizontal = 0; + T vertical = 0; +}; + +using Vector2Int = Vector2Of; + } // namespace LinearAlgebra using namespace LinearAlgebra;