diff options
| author | Mel <einebeere@gmail.com> | 2022-10-02 04:34:18 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2022-10-02 04:34:18 +0200 |
| commit | 3b289a2f75b6e96735519a65d93b6babd1b1759f (patch) | |
| tree | 27cfffa0d8fe85b96f380362ab0a2ffc367a1a6b | |
| parent | 46ab7c6c8af19dcf537cab25aa468f4afc403940 (diff) | |
| download | meowcraft-3b289a2f75b6e96735519a65d93b6babd1b1759f.tar.zst meowcraft-3b289a2f75b6e96735519a65d93b6babd1b1759f.zip | |
Expand Vector and Matrix classes
| -rw-r--r-- | src/Math/Math.hpp | 3 | ||||
| -rw-r--r-- | src/Math/Matrix.hpp | 65 | ||||
| -rw-r--r-- | src/Math/Vector.hpp | 72 | ||||
| -rw-r--r-- | src/Mesh.hpp | 4 | ||||
| -rw-r--r-- | src/main.cpp | 12 |
5 files changed, 143 insertions, 13 deletions
diff --git a/src/Math/Math.hpp b/src/Math/Math.hpp index 66ffd36..8204728 100644 --- a/src/Math/Math.hpp +++ b/src/Math/Math.hpp @@ -1,3 +1,4 @@ #pragma once -#include "Vector.hpp" \ No newline at end of file +#include "Vector.hpp" +#include "Matrix.hpp" \ No newline at end of file diff --git a/src/Math/Matrix.hpp b/src/Math/Matrix.hpp new file mode 100644 index 0000000..adc34a9 --- /dev/null +++ b/src/Math/Matrix.hpp @@ -0,0 +1,65 @@ +#pragma once + +#include <sstream> +#include <stddef.h> +#include <iostream> + +template <size_t R, size_t C, typename T = float> +struct Matrix { +public: + Matrix<R, C, T>() : elements{} {}; + + template<typename ...Args> + explicit Matrix<R, C, T>(Args... args): elements{ args... } {}; + + Vector<C, T> row(size_t index) { + return { &elements[index * C] }; + } + + Vector<R, T> col(size_t index) { + Vector<R, T> result{}; + for (int i = 0; i < R; i++) { + result[i] = this->operator()(index, i); + } + return result; + } + + template<size_t N> + Matrix<R, N, T> operator*(Matrix<C, N, T> other) { + Matrix<R, N, T> result{}; + for (int y = 0; y < R; y++) { + for (int x = 0; x < N; x++) { + auto r = row(y); + auto c = other.col(x); + + auto dot = r * c; + + std::cout << x << ", " << y << ": " + << "(" << r.string() << ", " << c.string() << ")" + << dot << std::endl; + + result(x, y) = dot; + } + } + return result; + } + + auto& operator()(size_t x, size_t 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++) { + str << this->operator()(x, y) << " "; + } + + str << "\n"; + } + + return str.str(); + } + + T elements[R * C]; +}; diff --git a/src/Math/Vector.hpp b/src/Math/Vector.hpp index 9c76758..0145b58 100644 --- a/src/Math/Vector.hpp +++ b/src/Math/Vector.hpp @@ -1,9 +1,73 @@ #pragma once -struct Vector3 { +#include <stddef.h> +#include <sstream> +#include <iostream> +#include <iterator> + +template <size_t S, typename T = float> +struct Vector { public: - Vector3(float x, float y, float z) - : x(x), y(y), z(z) {}; + Vector(): elements{} {}; + + template<typename ...Args> + Vector<S, T>(Args... args) : elements{ args... } {}; + + Vector<S, T>(T values[S]) { + std::copy(values, values + S, elements); + }; + + T& operator[](size_t index) { + return elements[index]; + } + + Vector<S, T> operator*(T scalar) const { + Vector<S, T> result; + for (size_t index; index < S; index++) { + result = this[index] * scalar; + } + return result; + } + + T operator*(Vector<S, T> other) const { + T result = 0; + for (size_t index = 0; index < S; index++) { + result += this->elements[index] * other[index]; + } + return result; + } + + T x() { + static_assert(S > 0); + return elements[0]; + } + + T y() { + static_assert(S > 1); + return elements[1]; + } + + T z() { + static_assert(S > 2); + return elements[2]; + } + + T w() { + static_assert(S > 3); + return elements[3]; + } + + std::string string() { + std::stringstream str{}; + + str << "[ "; + for (int i = 0; i < S; i++) { + str << elements[i] << " "; + } + str << "]"; + + return str.str(); + } - float x, y, z; + T elements[S]; }; diff --git a/src/Mesh.hpp b/src/Mesh.hpp index acf4f3b..a6d80d3 100644 --- a/src/Mesh.hpp +++ b/src/Mesh.hpp @@ -9,12 +9,12 @@ namespace MC { class Mesh { public: - Mesh(std::vector<Vector3> positions) : m_positions(std::move(positions)) {}; + Mesh(std::vector<Vector<3>> positions) : m_positions(std::move(positions)) {}; std::size_t size(); float* flat(); private: - std::vector<Vector3> m_positions; + std::vector<Vector<3>> m_positions; }; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ad6148e..3fc1711 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,13 +39,13 @@ void run() { glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); MC::Mesh quad({ - {-0.5, -0.5, 0.0}, - {0.5, -0.5, 0.0}, - {-0.5, 0.5, 0.0}, + {-0.5f, -0.5f, 0.0f}, + {0.5f, -0.5f, 0.0f}, + {-0.5f, 0.5f, 0.0f}, - {0.5, 0.5, 0.0}, - {-0.5, 0.5, 0.0}, - {0.5, -0.5, 0.0}, + {0.5f, 0.5f, 0.0f}, + {-0.5f, 0.5f, 0.0f}, + {0.5f, -0.5f, 0.0f}, }); auto mesh = MC::Binder::load(quad); |
