diff options
Diffstat (limited to 'src/Math/Matrix.hpp')
| -rw-r--r-- | src/Math/Matrix.hpp | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/src/Math/Matrix.hpp b/src/Math/Matrix.hpp index 56663e0..d51d171 100644 --- a/src/Math/Matrix.hpp +++ b/src/Math/Matrix.hpp @@ -1,11 +1,11 @@ #pragma once #include <sstream> -#include <cstddef> +#include "../Common/Sizes.hpp" #include "Rotation.hpp" #include "Trig.hpp" -template <size_t R, size_t C, typename T = float> +template <uint R, uint C, typename T = Real> struct Matrix { Matrix() : elements{} {} @@ -13,8 +13,8 @@ struct Matrix { std::fill(elements, elements + R * C, scalar); } - template<typename ...Args, std::enable_if_t<sizeof...(Args) == R * C, int> = 0> - Matrix(Args... args): elements{ args... } {} + template<typename ...Args, std::enable_if_t<sizeof...(Args) == R * C, Int> = 0> + Matrix(Args... args): elements{ static_cast<T>(args)... } {} explicit Matrix(const T values[R * C]) { std::copy(values, values + R * C, elements); @@ -22,42 +22,42 @@ struct Matrix { static Matrix<R, R, T> identity() { Matrix<R, R, T> result{}; - for (int i = 0; i < R; i++) { + for (Int i = 0; i < R; i++) { result(i, i) = 1; } return result; } - static Matrix<4, 4> transformation(Vector<3> position) { + static Matrix<4, 4, T> transformation(Vector<3> position) { return { - 1.0f, 0.0f, 0.0f, position.x(), - 0.0f, 1.0f, 0.0f, position.y(), - 0.0f, 0.0f, 1.0f, position.z(), - 0.0f, 0.0f, 0.0f, 1.0f + 1.0, 0.0, 0.0, position.x(), + 0.0, 1.0, 0.0, position.y(), + 0.0, 0.0, 1.0, position.z(), + 0.0, 0.0, 0.0, 1.0 }; } - static Matrix<4, 4> rotation(Rotation angles) { + static Matrix<4, 4, T> rotation(Rotation angles) { auto radians = angles.vector.map([](auto a) { return Math::radians(a); }); auto c = radians.map([](auto a) { return cos(a); }); auto s = radians.map([](auto a) { return sin(a); }); - Matrix<4, 4> rotation_x{ + Matrix<4, 4, T> rotation_x{ 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, c.x(), -s.x(), 0.0f, 0.0f, s.x(), c.x(), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, }; - Matrix<4, 4> rotation_y{ + Matrix<4, 4, T> rotation_y{ c.y(), 0.0f, s.y(), 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -s.y(), 0.0f, c.y(), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, }; - Matrix<4, 4> rotation_z{ + Matrix<4, 4, T> rotation_z{ c.z(), -s.z(), 0.0f, 0.0f, s.z(), c.z(), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, @@ -67,13 +67,13 @@ struct Matrix { return rotation_x * rotation_y * rotation_z; } - Vector<C, T> row(size_t index) const { + Vector<C, T> row(USize index) const { return Vector<C, T>{ &elements[index * C] }; } - Vector<R, T> col(size_t index) const { + Vector<R, T> col(USize index) const { Vector<R, T> result{}; - for (int i = 0; i < R; i++) { + for (Int i = 0; i < R; i++) { result[i] = this->operator()(index, i); } return result; @@ -81,8 +81,8 @@ struct Matrix { Matrix transpose() const { Matrix result{}; - for (int y = 0; y < R; y++) { - for (int x = 0; x < C; x++) { + for (Int y = 0; y < R; y++) { + for (Int x = 0; x < C; x++) { result(x, y) = this->operator()(y, x); } } @@ -91,25 +91,25 @@ struct Matrix { Matrix operator+(const Matrix other) const { Matrix result{}; - for (int i = 0; i < R * C; i++) { + for (Int i = 0; i < R * C; i++) { result.elements[i] = elements[i] + other.elements[i]; } return result; } - Matrix operator*(float scalar) { + Matrix operator*(Real scalar) { Matrix result{}; - for (int i = 0; i < R * C; i++) { + for (Int i = 0; i < R * C; i++) { result.elements[i] = elements[i] * scalar; } return result; } - template<size_t N> + template<uint N> Matrix<R, N, T> operator*(const Matrix<C, N, T> other) const { Matrix<R, N, T> result{}; - for (int y = 0; y < R; y++) { - for (int x = 0; x < N; x++) { + for (Int y = 0; y < R; y++) { + for (Int x = 0; x < N; x++) { auto r = row(y); auto c = other.col(x); @@ -127,17 +127,17 @@ struct Matrix { return Vector<R, T>{ matrix.elements }; } - const T& operator()(const size_t x, const size_t y) const { + const T& operator()(const USize x, const USize y) const { return elements[y * C + x]; } - T& operator()(const size_t x, const size_t y) { + T& operator()(const USize x, const USize y) { return elements[y * C + x]; } std::string string() { std::stringstream str{}; - for (int x = 0; x < R; x++) { - for (int y = 0; y < C; y++) { + for (Int x = 0; x < R; x++) { + for (Int y = 0; y < C; y++) { str << this->operator()(x, y) << " "; } |
