From 172fd86c3c1fa595ac96b95173074429a8a10498 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sat, 13 Feb 2021 12:53:29 +0700 Subject: [PATCH] min(scalar, vector), max(scalar, vector) funcs --- headers/vmath.hpp/vmath_ext.hpp | 2 +- headers/vmath.hpp/vmath_vec_fun.hpp | 10 ++++++++++ untests/vmath_vec_fun_tests.cpp | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/headers/vmath.hpp/vmath_ext.hpp b/headers/vmath.hpp/vmath_ext.hpp index 88eb349..a80cfe4 100644 --- a/headers/vmath.hpp/vmath_ext.hpp +++ b/headers/vmath.hpp/vmath_ext.hpp @@ -866,7 +866,7 @@ namespace vmath_hpp /// REFERENCE: /// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/ - auto xyzw = T(0.5) * sqrt(max(zero4, { + auto xyzw = T(0.5) * sqrt(max(T(0), vec{ T(1) + m[0][0] - m[1][1] - m[2][2], T(1) - m[0][0] + m[1][1] - m[2][2], T(1) - m[0][0] - m[1][1] + m[2][2], diff --git a/headers/vmath.hpp/vmath_vec_fun.hpp b/headers/vmath.hpp/vmath_vec_fun.hpp index df12117..6927a8b 100644 --- a/headers/vmath.hpp/vmath_vec_fun.hpp +++ b/headers/vmath.hpp/vmath_vec_fun.hpp @@ -590,6 +590,11 @@ namespace vmath_hpp return map_join([y](T x) { return min(x, y); }, xs); } + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec min(T x, const vec& ys) { + return map_join([x](T y) { return min(x, y); }, ys); + } + template < typename T, std::size_t Size > [[nodiscard]] constexpr vec min(const vec& xs, const vec& ys) { return map_join([](T x, T y) { return min(x, y); }, xs, ys); @@ -605,6 +610,11 @@ namespace vmath_hpp return map_join([y](T x) { return max(x, y); }, xs); } + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec max(T x, const vec& ys) { + return map_join([x](T y) { return max(x, y); }, ys); + } + template < typename T, std::size_t Size > [[nodiscard]] constexpr vec max(const vec& xs, const vec& ys) { return map_join([](T x, T y) { return max(x, y); }, xs, ys); diff --git a/untests/vmath_vec_fun_tests.cpp b/untests/vmath_vec_fun_tests.cpp index 7efdfa9..3640ff5 100644 --- a/untests/vmath_vec_fun_tests.cpp +++ b/untests/vmath_vec_fun_tests.cpp @@ -209,10 +209,12 @@ TEST_CASE("vmath/vec_fun") { STATIC_REQUIRE(min(int2(1,2)) == 1); STATIC_REQUIRE(min(int2(1,2), 1) == int2(1,1)); + STATIC_REQUIRE(min(1, int2(1,2)) == int2(1,1)); STATIC_REQUIRE(min(int2(1,1), int2(0,2)) == int2(0,1)); STATIC_REQUIRE(max(int2(1,2)) == 2); STATIC_REQUIRE(max(int2(1,2), 1) == int2(1,2)); + STATIC_REQUIRE(max(1, int2(1,2)) == int2(1,2)); STATIC_REQUIRE(max(int2(1,1), int2(0,2)) == int2(1,2)); STATIC_REQUIRE(clamp(int2(1,2), 0, 1) == int2(1,1));