/* * Created by Brett on 28/02/23. * Licensed under GNU General Public License V3.0 * See LICENSE file for license detail */ #ifndef BLT_TESTS_VECTORS_H #define BLT_TESTS_VECTORS_H #include #include namespace blt { template struct vec { private: T elements[size]{}; public: vec() { for (int i = 0; i < size; i++) elements[i] = 0; } vec(std::initializer_list args): vec() { for (int i = 0; i < args.size(); i++) { elements[i] = *(args.begin() + i); } } explicit vec(const T elem[size]) { for (int i = 0; i < size; i++) { elements[i] = elem[i]; } } vec(const vec& copy): vec(copy.elements) {} [[nodiscard]] inline T x() const { return elements[0]; } [[nodiscard]] inline T y() const { static_assert(size > 1); return elements[1]; } [[nodiscard]] inline T z() const { static_assert(size > 2); return elements[2]; } [[nodiscard]] inline T w() const { static_assert(size > 3); return elements[3]; } [[nodiscard]] inline T magnitude() const { T total = 0; for (int i = 0; i < size; i++) total += elements[i] * elements[i]; return std::sqrt(total); } [[nodiscard]] inline vec normalize() const { return *this / this->magnitude(); } inline T& operator[](int index) { return elements[index]; } inline T operator[](int index) const { return elements[index]; } inline vec& operator=(T v) { for (int i = 0; i < size; i++) elements[i] = v; return *this; } inline vec operator-() { T negativeCopy[size]; for (int i = 0; i < size; i++) negativeCopy[i] = -elements[i]; return vec{negativeCopy}; } inline vec& operator+=(const vec& other) { for (int i = 0; i < size; i++) elements[i] += other[i]; return *this; } inline vec& operator*=(const vec& other) { for (int i = 0; i < size; i++) elements[i] *= other[i]; return *this; } inline vec& operator+=(T f) { for (int i = 0; i < size; i++) elements[i] += f; return *this; } inline vec& operator*=(T f) { for (int i = 0; i < size; i++) elements[i] *= f; return *this; } inline vec& operator-=(const vec& other) { for (int i = 0; i < size; i++) elements[i] -= other[i]; return *this; } inline vec& operator-=(T f) { for (int i = 0; i < size; i++) elements[i] -= f; return *this; } /** * performs the dot product of left * right */ static inline T dot(const vec& left, const vec& right) { T dot = 0; for (int i = 0; i < size; i++) dot += left[i] * right[i]; return dot; } }; template inline vec operator+(const vec& left, const vec& right) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] + right[i]; return vec{initializer}; } template inline vec operator-(const vec& left, const vec& right) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] - right[i]; return vec{initializer}; } template inline vec operator+(const vec& left, float f) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] + f; return vec{initializer}; } template inline vec operator-(const vec& left, float f) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] + f; return vec{initializer}; } template inline vec operator+(float f, const vec& right) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = f + right[i]; return vec{initializer}; } template inline vec operator-(float f, const vec& right) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = f - right[i]; return vec{initializer}; } template inline vec operator*(const vec& left, const vec& right) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] * right[i]; return vec{initializer}; } template inline vec operator*(const vec& left, float f) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] * f; return vec{initializer}; } template inline vec operator*(float f, const vec& right) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = f * right[i]; return vec{initializer}; } template inline vec operator/(const vec& left, float f) { T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] / f; return vec{initializer}; } typedef vec vec2f; typedef vec vec3f; typedef vec vec4f; typedef vec vec2d; typedef vec vec3d; typedef vec vec4d; typedef vec vec2i; typedef vec vec3i; typedef vec vec4i; typedef vec vec2l; typedef vec vec3l; typedef vec vec4l; typedef vec vec2ui; typedef vec vec3ui; typedef vec vec4ui; typedef vec vec2ul; typedef vec vec3ul; typedef vec vec4ul; typedef vec2f vec2; typedef vec3f vec3; typedef vec4f vec4; } #endif //BLT_TESTS_VECTORS_H