From f1fc192ddc4c739fa8b4b376c759b7d3218a34eb Mon Sep 17 00:00:00 2001 From: Mel Date: Fri, 7 Jul 2023 21:39:42 +0200 Subject: Chunk-bound tree decoration --- src/Math/Vector.hpp | 79 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 19 deletions(-) (limited to 'src/Math/Vector.hpp') diff --git a/src/Math/Vector.hpp b/src/Math/Vector.hpp index 260891d..54207f0 100644 --- a/src/Math/Vector.hpp +++ b/src/Math/Vector.hpp @@ -10,31 +10,36 @@ struct Vector { Vector(): elements{} {}; template = 0> - Vector(Args... args) : elements{ args... } {}; + Vector(Args... args) : elements{ args... } {}; - Vector(T values[S]) { + Vector(const T values[S]) { std::copy(values, values + S, elements); }; - Vector(T scalar) { + explicit Vector(const T scalar) { std::fill(elements, elements + S, scalar); }; - Vector(Vector vector, T scalar) { + Vector(const Vector vector, const T scalar) { std::copy(vector.elements, vector.elements + S - 1, elements); elements[S - 1] = scalar; } - Vector map(std::function f) const { - Vector result{}; + template S), int> = 0> + explicit Vector(const Vector vector) { + std::copy(vector.elements, vector.elements + S, elements); + } + + Vector map(std::function f) const { + Vector result{}; for (int i = 0; i < S; i++) { result[i] = f(elements[i]); } return result; } - Vector map(std::function f) const { - Vector result{}; + Vector map(std::function f) const { + Vector result{}; for (int i = 0; i < S; i++) { result[i] = f(i, elements[i]); } @@ -57,20 +62,27 @@ struct Vector { return sqrt(map([](auto x) { return x * x;}).sum()); } - Vector normalize() const { + Vector normalize() const { auto m = magnitude(); return map([=](auto x) { return x / m; }); } - T distance(Vector other) const { + T distance(const Vector other) const { return (*this - other).magnitude(); } - Vector abs() const { + Vector<3, T> any_orthogonal() { + if (Vector a{y(), -x(), 0.0f}; a != zero()) return a; + if (Vector b{z(), 0.0f, -x()}; b != zero()) return b; + if (Vector c{0.0f, z(), -y()}; c != zero()) return c; + return zero(); + } + + Vector abs() const { return map([=](auto x) { return std::abs(x); }); } - Vector<3, T> cross(Vector<3, T> other) const { + Vector<3, T> cross(const Vector<3, T> other) const { return { y() * other.z() - z() * other.y(), z() * other.x() - x() * other.z(), @@ -86,34 +98,52 @@ struct Vector { return elements[index]; } - Vector operator+(Vector other) const { + Vector operator+(const Vector other) const { return map([&](auto i, auto x) { return x + other[i]; }); } - Vector operator+(T scalar) const { + Vector operator+(T scalar) const { return map([=](auto x) { return x + scalar; }); } - Vector operator*(T scalar) const { + Vector operator*(T scalar) const { return map([=](auto x) { return x * scalar; }); } - T operator*(Vector other) const { + T operator*(const Vector other) const { return map([&](auto i, auto x) { return x * other[i]; }).sum(); } - Vector operator-(Vector other) const { + Vector operator-(const Vector other) const { return map([&](auto i, auto x) { return x - other[i]; }); } - Vector operator-() const { + Vector operator-() const { return map([](T x) -> T { return -x; }); } - Vector operator/(T scalar) const { + Vector operator/(T scalar) const { return map([=](auto x) { return x / scalar; }); } + bool operator==(const Vector& other) { + for (int i = 0; i < S; i++) { + if (elements[i] != other[i]) { + return false; + } + } + return true; + } + + bool operator!=(const Vector& other) { + return !this->operator==(other); + } + + Vector& operator+=(const Vector& other) { + *this = *this + other; + return *this; + } + T& x() { static_assert(S > 0); return elements[0]; } const T& x() const { static_assert(S > 0); return elements[0]; } @@ -138,5 +168,16 @@ struct Vector { return str.str(); } + static Vector<3, T> up() { return {(T)0, (T)1, (T)0}; } + static Vector<3, T> down() { return {(T)0, (T)-1, (T)0}; } + static Vector<3, T> forward() { return {(T)0, (T)0, (T)1}; } + static Vector<3, T> back() { return {(T)0, (T)0, (T)-1}; } + static Vector<3, T> right() { return {(T)1, (T)0, (T)0}; } + static Vector<3, T> left() { return {(T)-1, (T)0, (T)0}; } + + static Vector<3, T> one() { return Vector{(T)1}; } + static Vector<3, T> zero() { return Vector{(T)0}; } + static Vector<3, T> max() { return Vector{(T)std::numeric_limits::max()}; } + T elements[S]; }; -- cgit 1.4.1