From bc79111c07e799a76b346f2f0451a7b5bb8bda56 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Tue, 1 Jul 2025 18:01:18 +0200 Subject: [PATCH] Added partial impl. triangulation --- LinearAlgebra/Matrix.cpp | 34 +++++++++++++++++++++++++++++++++- LinearAlgebra/Matrix.h | 7 ++++++- LinearAlgebra/Quaternion.cpp | 4 ++++ LinearAlgebra/Quaternion.h | 2 ++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/LinearAlgebra/Matrix.cpp b/LinearAlgebra/Matrix.cpp index edde63e..73b18c8 100644 --- a/LinearAlgebra/Matrix.cpp +++ b/LinearAlgebra/Matrix.cpp @@ -22,6 +22,10 @@ Matrix1::Matrix1(float* data, int size) : data(data), size(size) { this->externalData = true; } +Vector3 Matrix1::ToVector3() { + return Vector3(this->data[0], this->data[1], this->data[2]); +} + Matrix1 LinearAlgebra::Matrix1::FromQuaternion(Quaternion q) { Matrix1 r = Matrix1(4); float* data = r.data; @@ -37,7 +41,15 @@ Quaternion LinearAlgebra::Matrix1::ToQuaternion() { } Matrix1 Matrix1::Slice(int start, int stop) { - return Matrix1(); + if (start < 0 || stop >= this->size) + std::cerr << "Slice index out of range." << std::endl; + + Matrix1 result(stop - start); + int resultIx = 0; + for (int ix = start; ix < stop; ix++) + result.data[resultIx++] = this->data[ix]; + + return result; } // Matrix1 @@ -251,6 +263,26 @@ Matrix2 Matrix2::GetRows(int from, int to) { return result; } +Vector3 Matrix2::GetRow3(int rowIx) { + int cellIx = rowIx * this->nCols; + Vector3 row(this->data[cellIx, 0], this->data[cellIx, 1], + this->data[cellIx, 2]); + return row; +} + +void Matrix2::SetRow(int rowIx, Matrix1 source) { + int cellIx = rowIx * this->nCols; + for (int ix = 0; ix < source.size; ix++) + this->data[cellIx + ix] = source.data[ix]; +} + +void Matrix2::SetRow3(int rowIx, Vector3 v) { + int cellIx = rowIx * this->nCols; + this->data[cellIx + 0] = v.x; + this->data[cellIx + 1] = v.y; + this->data[cellIx + 2] = v.z; +} + Matrix2 Matrix2::Slice(int rowStart, int rowStop, int colStart, int colStop) { Matrix2 r = Matrix2(rowStop - rowStart, colStop - colStart); diff --git a/LinearAlgebra/Matrix.h b/LinearAlgebra/Matrix.h index 5c7bcdc..be94054 100644 --- a/LinearAlgebra/Matrix.h +++ b/LinearAlgebra/Matrix.h @@ -16,6 +16,8 @@ class Matrix1 { Matrix1(int size); Matrix1(float* data, int size); + Vector3 ToVector3(); + static Matrix1 FromQuaternion(Quaternion q); Quaternion ToQuaternion(); @@ -120,8 +122,11 @@ class Matrix2 { } Matrix2 GetRows(int from, int to); + Vector3 GetRow3(int rowIx); + + void SetRow(int rowIx, Matrix1 source); + void SetRow3(int rowIx, Vector3 v); - void SetRow(int rowIx, Matrix1 source) {} Matrix2 Slice(int rawStart, int rowStop, int colStart, int colStop); void UpdateSlice(int rowStart, diff --git a/LinearAlgebra/Quaternion.cpp b/LinearAlgebra/Quaternion.cpp index dda8c18..5bb5ecd 100644 --- a/LinearAlgebra/Quaternion.cpp +++ b/LinearAlgebra/Quaternion.cpp @@ -168,6 +168,10 @@ Quaternion Quaternion::Inverse(Quaternion r) { return Quaternion(-r.x / n, -r.y / n, -r.z / n, r.w / n); } +Quaternion Quaternion::Reflect(Quaternion q) { + return Quaternion(-q.x, -q.y, -q.z, q.w); +} + Quaternion Quaternion::LookRotation(const Vector3& forward) { Vector3 up = Vector3(0, 1, 0); return LookRotation(forward, up); diff --git a/LinearAlgebra/Quaternion.h b/LinearAlgebra/Quaternion.h index 1687dc9..9c12c2c 100644 --- a/LinearAlgebra/Quaternion.h +++ b/LinearAlgebra/Quaternion.h @@ -126,6 +126,8 @@ struct Quaternion : Quat { /// needed The inverted quaternion static Quaternion Inverse(Quaternion quaternion); + static Quaternion Reflect(Quaternion q); + /// /// A rotation which looks in the given direction ///