From 9db3f120489ff27aa560e488d82b5ae0d64019df Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 9 Apr 2024 19:09:50 -0400 Subject: [PATCH] printing vectors is fun --- CMakeLists.txt | 2 +- include/blt/math/fixed_point.h | 9 +++ include/blt/math/log_util.h | 49 ++++++++++--- include/blt/math/vectors.h | 125 +++++++++++++++++---------------- include/blt/std/string.h | 2 +- tests/src/math_tests.cpp | 15 ++++ 6 files changed, 133 insertions(+), 69 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c6ff5e7..1e3df96 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.7) +set(BLT_VERSION 0.16.8) set(BLT_TEST_VERSION 0.0.1) set(BLT_TARGET BLT) diff --git a/include/blt/math/fixed_point.h b/include/blt/math/fixed_point.h index 15fc682..3125583 100644 --- a/include/blt/math/fixed_point.h +++ b/include/blt/math/fixed_point.h @@ -25,8 +25,13 @@ //#define BLT_DEBUG_NO_INLINE BLT_ATTRIB_NO_INLINE #define BLT_DEBUG_NO_INLINE +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpedantic" +#endif namespace blt { + struct fp64 { private: @@ -248,4 +253,8 @@ namespace blt static constexpr const inline fp64 FP64_LOG2E = fp64::from_f64(1.4426950408889634074f); } +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif + #endif //BLT_FIXED_POINT_H diff --git a/include/blt/math/log_util.h b/include/blt/math/log_util.h index dec9035..5cb795d 100644 --- a/include/blt/math/log_util.h +++ b/include/blt/math/log_util.h @@ -10,17 +10,50 @@ #include #include #include +#include +#include "blt/std/string.h" -namespace blt { - - template - static inline logging::logger& operator<<(logging::logger& log, blt::vec vec){ - log << "("; - for (int i = 0; i < size; i++) - log << vec[i] << ((i == size-1) ? ")" : ", "); +namespace blt +{ + + template + static inline Writer& operator<<(Writer& log, const blt::vec& vec) + { + std::string type_string; + const auto tstr = blt::type_string(); + + if constexpr (std::is_arithmetic_v) + { + type_string += tstr[0]; + // for unsigned long / unsigned int + auto split = blt::string::split_sv(tstr, ' '); + if (tstr[0] == 'u'){ + if (split.size() > 1){ + type_string += split[1][0]; + } else + type_string += tstr[1]; + } + + } else + type_string = tstr; + + log << "Vec" << size << type_string << "("; + for (blt::u32 i = 0; i < size; i++) + log << vec[i] << ((i == size - 1) ? ")" : ", "); return log; } - + + template + inline Writer& operator<<(Writer& out, const mat4x4& v) + { + out << "Mat4x4(\n"; + out << "\t{" << v.m00() << ", " << v.m01() << ", " << v.m02() << ", " << v.m03() << "},\n"; + out << "\t{" << v.m10() << ", " << v.m11() << ", " << v.m12() << ", " << v.m13() << "},\n"; + out << "\t{" << v.m20() << ", " << v.m21() << ", " << v.m22() << ", " << v.m23() << "},\n"; + out << "\t{" << v.m30() << ", " << v.m31() << ", " << v.m32() << ", " << v.m33() << "})"; + return out; + } + } #endif //BLT_TESTS_LOG_UTIL_H diff --git a/include/blt/math/vectors.h b/include/blt/math/vectors.h index adfb2ce..73f01b8 100644 --- a/include/blt/math/vectors.h +++ b/include/blt/math/vectors.h @@ -13,6 +13,7 @@ #include #include #include +#include namespace blt { @@ -26,7 +27,7 @@ namespace blt return v1 >= v2 - EPSILON && v1 <= v2 + EPSILON; } - template + template struct vec { static_assert(std::is_arithmetic_v && "blt::vec must be created using an arithmetic type!"); @@ -44,7 +45,8 @@ namespace blt * @param t default value to fill with * @param args list of args */ - vec(T t, std::initializer_list args) + template || std::is_convertible_v, bool> = true> + vec(U t, std::initializer_list args) { auto b = args.begin(); for (auto& v : elements) @@ -63,7 +65,12 @@ namespace blt * 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 */ - vec(std::initializer_list args): vec(static_cast(0), 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) @@ -114,7 +121,7 @@ namespace blt [[nodiscard]] inline T magnitude() const { T total = 0; - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) total += elements[i] * elements[i]; return std::sqrt(total); } @@ -139,7 +146,7 @@ namespace blt inline vec& operator=(T v) { - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) elements[i] = v; return *this; } @@ -147,49 +154,49 @@ namespace blt inline vec operator-() { vec initializer{}; - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) initializer[i] = -elements[i]; return vec{initializer}; } inline vec& operator+=(const vec& other) { - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) elements[i] += other[i]; return *this; } inline vec& operator*=(const vec& other) { - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) elements[i] *= other[i]; return *this; } inline vec& operator+=(T f) { - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) elements[i] += f; return *this; } inline vec& operator*=(T f) { - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) elements[i] *= f; return *this; } inline vec& operator-=(const vec& other) { - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) elements[i] -= other[i]; return *this; } inline vec& operator-=(T f) { - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) elements[i] -= f; return *this; } @@ -200,7 +207,7 @@ namespace blt static inline constexpr T dot(const vec& left, const vec& right) { T dot = 0; - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) dot += left[i] * right[i]; return dot; } @@ -256,147 +263,147 @@ namespace blt } }; - template + template inline constexpr vec operator+(const vec& left, const vec& right) { vec initializer{}; - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) initializer[i] = left[i] + right[i]; return initializer; } - template + template inline constexpr vec operator-(const vec& left, const vec& right) { vec initializer{}; - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) initializer[i] = left[i] - right[i]; return initializer; } - template + template inline constexpr vec operator+(const vec& left, T f) { vec initializer{}; - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) initializer[i] = left[i] + f; return initializer; } - template + template inline constexpr vec operator-(const vec& left, T f) { vec initializer{}; - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) initializer[i] = left[i] + f; return initializer; } - template + template inline constexpr vec operator+(T f, const vec& right) { vec initializer{}; - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) initializer[i] = f + right[i]; return initializer; } - template + template inline constexpr vec operator-(T f, const vec& right) { vec initializer{}; - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) initializer[i] = f - right[i]; return initializer; } - template + template inline constexpr vec operator*(const vec& left, const vec& right) { vec initializer{}; - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) initializer[i] = left[i] * right[i]; return initializer; } - template + template inline constexpr vec operator*(const vec& left, T f) { vec initializer{}; - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) initializer[i] = left[i] * f; return initializer; } - template + template inline constexpr vec operator*(T f, const vec& right) { vec initializer{}; - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) initializer[i] = f * right[i]; return initializer; } - template + template inline constexpr vec operator/(const vec& left, T f) { vec initializer{}; - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) initializer[i] = left[i] / f; return initializer; } - template + template inline constexpr bool operator==(const vec& left, const vec& right) { - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) if (left[i] != right[i]) return false; return true; } - template + template inline constexpr bool operator!=(const vec& left, const vec& right) { return !(left == right); } - template + template inline constexpr bool operator&&(const vec& left, const vec& right) { - for (std::uint32_t i = 0; i < size; i++) + for (blt::u32 i = 0; i < size; i++) if (!f_equal(left[i], right[i])) return false; return true; } - typedef vec vec2f; - typedef vec vec3f; - typedef vec vec4f; + using vec2f = vec; + using vec3f = vec; + using vec4f = vec; - typedef vec vec2d; - typedef vec vec3d; - typedef vec vec4d; + using vec2d = vec; + using vec3d = vec; + using vec4d = vec; - typedef vec vec2i; - typedef vec vec3i; - typedef vec vec4i; + using vec2i = vec; + using vec3i = vec; + using vec4i = vec; - typedef vec vec2l; - typedef vec vec3l; - typedef vec vec4l; + using vec2l = vec; + using vec3l = vec; + using vec4l = vec; - typedef vec vec2ui; - typedef vec vec3ui; - typedef vec vec4ui; + using vec2ui = vec; + using vec3ui = vec; + using vec4ui = vec; - typedef vec vec2ul; - typedef vec vec3ul; - typedef vec vec4ul; + using vec2ul = vec; + using vec3ul = vec; + using vec4ul = vec; - typedef vec2f vec2; - typedef vec3f vec3; - typedef vec4f vec4; + using vec2 = vec2f; + using vec3 = vec3f; + using vec4 = vec4f; namespace vec_algorithm { diff --git a/include/blt/std/string.h b/include/blt/std/string.h index 25e688a..15f863e 100644 --- a/include/blt/std/string.h +++ b/include/blt/std/string.h @@ -91,7 +91,7 @@ namespace blt::string #endif } - static inline BLT_CPP20_CONSTEXPR bool ends_with(std::string_view string, std::string_view search); + BLT_CPP20_CONSTEXPR bool ends_with(std::string_view string, std::string_view search); static inline BLT_CPP20_CONSTEXPR bool ends_with(std::string_view string, char search) { diff --git a/tests/src/math_tests.cpp b/tests/src/math_tests.cpp index 870ac76..acb8492 100644 --- a/tests/src/math_tests.cpp +++ b/tests/src/math_tests.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -38,6 +39,20 @@ namespace blt::test void fixed_point() { + blt::vec3f vf(34, 34, 2); + blt::vec3ul vul(34, 34, 2); + + std::cout << vf << std::endl; + BLT_TRACE(vf); + BLT_TRACE(vul); + BLT_DEBUG_STREAM << vul << '\n'; + + blt::mat4x4 mat; + std::cout << mat << std::endl; + BLT_TRACE(mat); + BLT_DEBUG_STREAM << mat << '\n'; + + //vec3fp hello = {fp64::from_f64(32.023), fp64::from_f64(422.34023), fp64::from_f64(321.023)}; print(FP64_UMAX, "umax");