/* * 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 #include #include #include #include #include namespace blt { #define MSVC_COMPILER (!defined(__GNUC__) && !defined(__clang__)) constexpr float EPSILON = 0.0001f; static inline constexpr bool f_equal(float v1, float v2) { return v1 >= v2 - EPSILON && v1 <= v2 + EPSILON; } template struct vec { static_assert(std::is_arithmetic_v && "blt::vec must be created using an arithmetic type!"); private: std::array elements; public: vec() { for (auto& v : elements) v = static_cast(0); } /** * Create a vector with initializer list, if the initializer list doesn't contain enough values to fill this vec, it will use t * @param t default value to fill with * @param args list of args */ template || std::is_convertible_v, bool> = true> vec(U t, std::initializer_list args) { auto b = args.begin(); for (auto& v : elements) { if (b == args.end()) { v = t; continue; } v = *b; ++b; } } /** * Create a vector from an initializer list, if the list doesn't have enough elements it will be filled with the default value (0) * @param args */ template || std::is_convertible_v, bool> = true> vec(std::initializer_list args): vec(U(), args) {} template explicit vec(Args... args): vec(std::array{static_cast(args)...}) {} explicit vec(T t) { for (auto& v : elements) v = t; } explicit vec(const T elem[size]) { for (size_t i = 0; i < size; i++) elements[i] = elem[i]; } explicit vec(std::array elem) { auto b = elem.begin(); for (auto& v : elements) { v = *b; ++b; } } [[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 (blt::u32 i = 0; i < size; i++) total += elements[i] * elements[i]; return std::sqrt(total); } [[nodiscard]] inline vec normalize() const { T mag = this->magnitude(); if (mag == 0) return vec(*this); return *this / mag; } inline T& operator[](int index) { return elements[index]; } inline T operator[](int index) const { return elements[index]; } inline vec& operator=(T v) { for (blt::u32 i = 0; i < size; i++) elements[i] = v; return *this; } inline vec operator-() { vec initializer{}; for (blt::u32 i = 0; i < size; i++) initializer[i] = -elements[i]; return vec{initializer}; } inline vec& operator+=(const vec& other) { for (blt::u32 i = 0; i < size; i++) elements[i] += other[i]; return *this; } inline vec& operator*=(const vec& other) { for (blt::u32 i = 0; i < size; i++) elements[i] *= other[i]; return *this; } inline vec& operator+=(T f) { for (blt::u32 i = 0; i < size; i++) elements[i] += f; return *this; } inline vec& operator*=(T f) { for (blt::u32 i = 0; i < size; i++) elements[i] *= f; return *this; } inline vec& operator-=(const vec& other) { for (blt::u32 i = 0; i < size; i++) elements[i] -= other[i]; return *this; } inline vec& operator-=(T f) { for (blt::u32 i = 0; i < size; i++) elements[i] -= f; return *this; } /** * performs the dot product of left * right */ static inline constexpr T dot(const vec& left, const vec& right) { T dot = 0; for (blt::u32 i = 0; i < size; i++) dot += left[i] * right[i]; return dot; } static inline constexpr vec cross( const vec& left, const vec& right ) { // cross is only defined on vectors of size 3. 2D could be implemented, which is a TODO static_assert(size == 3); return {left.y() * right.z() - left.z() * right.y(), left.z() * right.x() - left.x() * right.z(), left.x() * right.y() - left.y() * right.x()}; } static inline constexpr vec project( const vec& u, const vec& v ) { T du = dot(u); T dv = dot(v); return (du / dv) * v; } inline auto* data() { return elements.data(); } [[nodiscard]] inline const auto* data() const { return elements.data(); } auto begin() { return elements.begin(); } auto end() { return elements.end(); } auto rbegin() { return elements.rbegin(); } auto rend() { return elements.rend(); } [[nodiscard]] auto cbegin() const { return elements.cbegin(); } [[nodiscard]] auto cend() const { return elements.cend(); } }; template inline constexpr vec operator+(const vec& left, const vec& right) { vec initializer{}; for (blt::u32 i = 0; i < size; i++) initializer[i] = left[i] + right[i]; return initializer; } template inline constexpr vec operator-(const vec& left, const vec& right) { vec initializer{}; for (blt::u32 i = 0; i < size; i++) initializer[i] = left[i] - right[i]; return initializer; } template inline constexpr vec operator+(const vec& left, T f) { vec initializer{}; for (blt::u32 i = 0; i < size; i++) initializer[i] = left[i] + f; return initializer; } template inline constexpr vec operator-(const vec& left, T f) { vec initializer{}; for (blt::u32 i = 0; i < size; i++) initializer[i] = left[i] + f; return initializer; } template inline constexpr vec operator+(T f, const vec& right) { vec initializer{}; for (blt::u32 i = 0; i < size; i++) initializer[i] = f + right[i]; return initializer; } template inline constexpr vec operator-(T f, const vec& right) { vec initializer{}; for (blt::u32 i = 0; i < size; i++) initializer[i] = f - right[i]; return initializer; } template inline constexpr vec operator*(const vec& left, const vec& right) { vec initializer{}; for (blt::u32 i = 0; i < size; i++) initializer[i] = left[i] * right[i]; return initializer; } template inline constexpr vec operator*(const vec& left, T f) { vec initializer{}; for (blt::u32 i = 0; i < size; i++) initializer[i] = left[i] * f; return initializer; } template inline constexpr vec operator*(T f, const vec& right) { vec initializer{}; for (blt::u32 i = 0; i < size; i++) initializer[i] = f * right[i]; return initializer; } template inline constexpr vec operator/(const vec& left, T f) { vec initializer{}; for (blt::u32 i = 0; i < size; i++) initializer[i] = left[i] / f; return initializer; } template inline constexpr bool operator==(const vec& left, const vec& right) { for (blt::u32 i = 0; i < size; i++) if (left[i] != right[i]) return false; return true; } template inline constexpr bool operator!=(const vec& left, const vec& right) { return !(left == right); } template inline constexpr bool operator&&(const vec& left, const vec& right) { for (blt::u32 i = 0; i < size; i++) if (!f_equal(left[i], right[i])) return false; return true; } using vec2f = vec; using vec3f = vec; using vec4f = vec; using vec2d = vec; using vec3d = vec; using vec4d = vec; using vec2i = vec; using vec3i = vec; using vec4i = vec; using vec2l = vec; using vec3l = vec; using vec4l = vec; using vec2ui = vec; using vec3ui = vec; using vec4ui = vec; using vec2ul = vec; using vec3ul = vec; using vec4ul = vec; using vec2 = vec2f; using vec3 = vec3f; using vec4 = vec4f; using color4 = vec4; using color3 = vec3; inline color4 make_color(float r, float g, float b) { return color4{r, g, b, 1.0f}; } namespace vec_algorithm { static inline void findOrthogonalBasis(const vec3& v, vec3& v1, vec3& v2, vec3& v3) { v1 = v.normalize(); vec3 arbitraryVector{1, 0, 0}; if (std::abs(vec3::dot(v, arbitraryVector)) > 0.9) { arbitraryVector = vec3{0, 1, 0}; } v2 = vec3::cross(v, arbitraryVector).normalize(); v3 = vec3::cross(v1, v2); } // Gram-Schmidt orthonormalization algorithm static inline void gramSchmidt(std::vector& vectors) { int n = (int) vectors.size(); std::vector basis; // normalize first vector basis.push_back(vectors[0]); basis[0] = basis[0].normalize(); // iterate over the rest of the vectors for (int i = 1; i < n; ++i) { // subtract the projections of the vector onto the previous basis vectors vec3 new_vector = vectors[i]; for (int j = 0; j < i; ++j) { float projection = vec3::dot(vectors[i], basis[j]); new_vector[0] -= projection * basis[j].x(); new_vector[1] -= projection * basis[j].y(); new_vector[2] -= projection * basis[j].z(); } // normalize the new basis vector new_vector = new_vector.normalize(); basis.push_back(new_vector); } vectors = basis; } } } #endif //BLT_TESTS_VECTORS_H