From f66811b772c81fc182e353308b1c1a3667201e9b Mon Sep 17 00:00:00 2001 From: Mel Date: Wed, 5 Oct 2022 18:27:19 +0200 Subject: Non-camera relative movement --- src/Math/Vector.hpp | 80 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 23 deletions(-) (limited to 'src/Math/Vector.hpp') diff --git a/src/Math/Vector.hpp b/src/Math/Vector.hpp index 2e97217..b6690ef 100644 --- a/src/Math/Vector.hpp +++ b/src/Math/Vector.hpp @@ -4,6 +4,7 @@ #include #include #include +#include template struct Vector { @@ -22,62 +23,95 @@ public: elements[S - 1] = scalar; } - Vector apply(T f(T)) { - return apply(static_cast>(f)); + Vector map(std::function f) const { + Vector result{}; + for (int i = 0; i < S; i++) { + result[i] = f(elements[i]); + } + return result; } - Vector apply(std::function f) { + Vector map(std::function f) const { Vector result{}; for (int i = 0; i < S; i++) { - result[i] = f(elements[i]); + result[i] = f(i, elements[i]); } return result; } + T reduce(std::function f) const { + T result = elements[0]; + for (int i = 1; i < S; i++) { + result = f(result, elements[i]); + } + return result; + } + + T sum() const { + return reduce([](auto x, auto y) { return x + y; }); + } + + T magnitude() const { + return sqrt(map([](auto x) { return x * x;}).sum()); + } + + Vector normalize() const { + auto m = magnitude(); + return map([=](auto x) { return x / m; }); + } + + Vector<3, T> cross(Vector<3, T> other) const { + return { + y() * other.z() - z() * other.y(), + z() * other.x() - x() * other.z(), + x() * other.y() - y() * other.x(), + }; + } + + T operator[](size_t index) const { + return elements[index]; + } + T& operator[](size_t index) { return elements[index]; } Vector operator+(Vector other) const { - Vector result{}; - for (int i = 0; i < S; i++) { - result[i] = elements[i] + other[i]; - } - return result; + return map([&](auto i, auto x) { return x + other[i]; }); } Vector operator*(T scalar) const { - Vector result; - for (size_t index; index < S; index++) { - result = this[index] * scalar; - } - return result; + return map([=](auto x) { return x * scalar; }); } T operator*(Vector other) const { - T result = 0; - for (size_t index = 0; index < S; index++) { - result += this->elements[index] * other[index]; - } - return result; + return map([&](auto i, auto x) { return x * other[i]; }).sum(); + } + + Vector operator-(Vector other) const { + return map([&](auto i, auto x) { return x - other[i]; }); + } + + Vector operator-() const { + return map([](T x) -> T { return -x; }); } - T x() { + T x() const { static_assert(S > 0); return elements[0]; } - T y() { + T y() const { static_assert(S > 1); return elements[1]; } - T z() { + T z() const { static_assert(S > 2); return elements[2]; } - T w() { + T w() const { static_assert(S > 3); return elements[3]; } -- cgit 1.4.1