diff --git a/CMakeLists.txt b/CMakeLists.txt index 77c6563..902085a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 0.16.20) +set(BLT_VERSION 0.16.21) set(BLT_TEST_VERSION 0.0.1) set(BLT_TARGET BLT) diff --git a/include/blt/math/vectors.h b/include/blt/math/vectors.h index 89fe7fc..0219503 100644 --- a/include/blt/math/vectors.h +++ b/include/blt/math/vectors.h @@ -28,13 +28,12 @@ namespace blt } template - struct vec - { + struct vec { static_assert(std::is_arithmetic_v && "blt::vec must be created using an arithmetic type!"); private: std::array elements; public: - vec() + constexpr vec() { for (auto& v : elements) v = static_cast(0); @@ -46,13 +45,11 @@ namespace blt * @param args list of args */ template || std::is_convertible_v, bool> = true> - vec(U t, std::initializer_list args) + constexpr vec(U t, std::initializer_list args): elements() { auto b = args.begin(); - for (auto& v : elements) - { - if (b == args.end()) - { + for (auto& v : elements) { + if (b == args.end()) { v = t; continue; } @@ -66,59 +63,65 @@ namespace blt * @param args */ template || std::is_convertible_v, bool> = true> - vec(std::initializer_list args): vec(U(), args) + constexpr vec(std::initializer_list args): vec(U(), args) {} template - explicit vec(Args... args): vec(std::array{static_cast(args)...}) + constexpr explicit vec(Args... args): vec(std::array{static_cast(args)...}) {} - explicit vec(T t) + constexpr explicit vec(T t) { for (auto& v : elements) v = t; } - explicit vec(const T elem[size]) + constexpr explicit vec(const T elem[size]) { for (size_t i = 0; i < size; i++) elements[i] = elem[i]; } - explicit vec(std::array elem) + constexpr explicit vec(std::array elem): elements(elem) + {} + + template, bool> = true> + constexpr explicit vec(std::array el): elements() { - auto b = elem.begin(); - for (auto& v : elements) + auto b = el.begin(); + auto m = elements.begin(); + while (b != el.end() && m != elements.end()) { - v = *b; + *m = *b; + ++m; ++b; } } - [[nodiscard]] inline T x() const + [[nodiscard]] constexpr inline T x() const { return elements[0]; } - [[nodiscard]] inline T y() const + [[nodiscard]] constexpr inline T y() const { static_assert(size > 1); return elements[1]; } - [[nodiscard]] inline T z() const + [[nodiscard]] constexpr inline T z() const { static_assert(size > 2); return elements[2]; } - [[nodiscard]] inline T w() const + [[nodiscard]] constexpr inline T w() const { static_assert(size > 3); return elements[3]; } - [[nodiscard]] inline T magnitude() const + [[nodiscard]] constexpr inline T magnitude() const { T total = 0; for (blt::u32 i = 0; i < size; i++) @@ -126,7 +129,7 @@ namespace blt return std::sqrt(total); } - [[nodiscard]] inline vec normalize() const + [[nodiscard]] constexpr inline vec normalize() const { T mag = this->magnitude(); if (mag == 0) @@ -134,24 +137,24 @@ namespace blt return *this / mag; } - inline T& operator[](int index) + constexpr inline T& operator[](int index) { return elements[index]; } - inline T operator[](int index) const + constexpr inline T operator[](int index) const { return elements[index]; } - inline vec& operator=(T v) + constexpr inline vec& operator=(T v) { for (blt::u32 i = 0; i < size; i++) elements[i] = v; return *this; } - inline vec operator-() + constexpr inline vec operator-() { vec initializer{}; for (blt::u32 i = 0; i < size; i++) @@ -159,42 +162,42 @@ namespace blt return vec{initializer}; } - inline vec& operator+=(const vec& other) + constexpr 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) + constexpr 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) + constexpr inline vec& operator+=(T f) { for (blt::u32 i = 0; i < size; i++) elements[i] += f; return *this; } - inline vec& operator*=(T f) + constexpr inline vec& operator*=(T f) { for (blt::u32 i = 0; i < size; i++) elements[i] *= f; return *this; } - inline vec& operator-=(const vec& other) + constexpr 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) + constexpr inline vec& operator-=(T f) { for (blt::u32 i = 0; i < size; i++) elements[i] -= f; @@ -204,7 +207,7 @@ namespace blt /** * performs the dot product of left * right */ - static inline constexpr T dot(const vec& left, const vec& right) + constexpr static inline T dot(const vec& left, const vec& right) { T dot = 0; for (blt::u32 i = 0; i < size; i++) @@ -212,7 +215,7 @@ namespace blt return dot; } - static inline constexpr vec cross( + constexpr static inline vec cross( const vec& left, const vec& right ) { @@ -223,7 +226,7 @@ namespace blt left.x() * right.y() - left.y() * right.x()}; } - static inline constexpr vec project( + constexpr static inline vec project( const vec& u, const vec& v ) { @@ -232,42 +235,42 @@ namespace blt return (du / dv) * v; } - inline auto* data() + constexpr inline auto* data() { return elements.data(); } - [[nodiscard]] inline const auto* data() const + [[nodiscard]] constexpr inline const auto* data() const { return elements.data(); } - auto begin() + constexpr auto begin() { return elements.begin(); } - auto end() + constexpr auto end() { return elements.end(); } - auto rbegin() + constexpr auto rbegin() { return elements.rbegin(); } - auto rend() + constexpr auto rend() { return elements.rend(); } - [[nodiscard]] auto cbegin() const + [[nodiscard]] constexpr auto cbegin() const { return elements.cbegin(); } - [[nodiscard]] auto cend() const + [[nodiscard]] constexpr auto cend() const { return elements.cend(); } @@ -435,11 +438,9 @@ namespace blt template inline blt::vec make_vec2(const blt::vec& t, size_t fill = 0) { - if constexpr (size >= 2) - { + if constexpr (size >= 2) { return blt::vec(t.x(), t.y()); - } else - { + } else { return blt::vec(t.x(), fill); } } @@ -447,11 +448,9 @@ namespace blt template inline blt::vec make_vec3(const blt::vec& t, size_t fill = 0) { - if constexpr (size >= 3) - { + if constexpr (size >= 3) { return blt::vec(t.x(), t.y(), t.z()); - } else - { + } else { blt::vec ret; for (size_t i = 0; i < size; i++) ret[i] = t[i]; @@ -464,11 +463,9 @@ namespace blt template inline blt::vec make_vec4(const blt::vec& t, size_t fill = 0) { - if constexpr (size >= 4) - { + if constexpr (size >= 4) { return blt::vec(t.x(), t.y(), t.z(), t.w()); - } else - { + } else { blt::vec ret; for (size_t i = 0; i < size; i++) ret[i] = t[i]; @@ -485,8 +482,7 @@ namespace blt v1 = v.normalize(); vec3 arbitraryVector{1, 0, 0}; - if (std::abs(vec3::dot(v, arbitraryVector)) > 0.9) - { + if (std::abs(vec3::dot(v, arbitraryVector)) > 0.9) { arbitraryVector = vec3{0, 1, 0}; } @@ -505,12 +501,10 @@ namespace blt basis[0] = basis[0].normalize(); // iterate over the rest of the vectors - for (int i = 1; i < n; ++i) - { + 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) - { + 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();