diff --git a/README.md b/README.md index 50603c2..cfdabd3 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,9 @@ Most functions and types are based on the HLSL ([High-Level Shading Language for - [Vector Types](#Vector-Types) - [Matrix Types](#Matrix-Types) - [Vector Operators](#Vector-Operators) +- [Vector Relational Operators](#Vector-Relational-Operators) - [Matrix Operators](#Matrix-Operators) +- [Matrix Relational Operators](#Matrix-Relational-Operators) - [Angle and Trigonometry Functions](#Angle-and-Trigonometry-Functions) - [Exponential Functions](#Exponential-Functions) - [Common Functions](#Common-Functions) @@ -552,6 +554,54 @@ template < typename T, size_t Size > constexpr bool operator<(const vec& xs, const vec& ys); ``` +### Vector Relational Operators + +```cpp +// operator< + +template < typename T, size_t Size > +constexpr vec operator<(const vec& xs, T y); + +template < typename T, size_t Size > +constexpr vec operator<(T x, const vec& ys); + +template < typename T, size_t Size > +constexpr vec operator<(const vec& xs, const vec& ys); + +// operator<= + +template < typename T, size_t Size > +constexpr vec operator<=(const vec& xs, T y); + +template < typename T, size_t Size > +constexpr vec operator<=(T x, const vec& ys); + +template < typename T, size_t Size > +constexpr vec operator<=(const vec& xs, const vec& ys); + +// operator> + +template < typename T, size_t Size > +constexpr vec operator>(const vec& xs, T y); + +template < typename T, size_t Size > +constexpr vec operator>(T x, const vec& ys); + +template < typename T, size_t Size > +constexpr vec operator>(const vec& xs, const vec& ys); + +// operator>= + +template < typename T, size_t Size > +constexpr vec operator>=(const vec& xs, T y); + +template < typename T, size_t Size > +constexpr vec operator>=(T x, const vec& ys); + +template < typename T, size_t Size > +constexpr vec operator>=(const vec& xs, const vec& ys); +``` + ### Matrix Operators ```cpp @@ -739,6 +789,54 @@ template < typename T, size_t Size > constexpr bool operator<(const mat& xs, const mat& ys); ``` +### Matrix Relational Operators + +```cpp +// operator< + +template < typename T, size_t Size > +constexpr mat operator<(const mat& xs, T y); + +template < typename T, size_t Size > +constexpr mat operator<(T x, const mat& ys); + +template < typename T, size_t Size > +constexpr mat operator<(const mat& xs, const mat& ys); + +// operator<= + +template < typename T, size_t Size > +constexpr mat operator<=(const mat& xs, T y); + +template < typename T, size_t Size > +constexpr mat operator<=(T x, const mat& ys); + +template < typename T, size_t Size > +constexpr mat operator<=(const mat& xs, const mat& ys); + +// operator> + +template < typename T, size_t Size > +constexpr mat operator>(const mat& xs, T y); + +template < typename T, size_t Size > +constexpr mat operator>(T x, const mat& ys); + +template < typename T, size_t Size > +constexpr mat operator>(const mat& xs, const mat& ys); + +// operator>= + +template < typename T, size_t Size > +constexpr mat operator>=(const mat& xs, T y); + +template < typename T, size_t Size > +constexpr mat operator>=(T x, const mat& ys); + +template < typename T, size_t Size > +constexpr mat operator>=(const mat& xs, const mat& ys); +``` + ### Angle and Trigonometry Functions #### Scalar @@ -1156,18 +1254,6 @@ vec refract(const vec& i, const vec& n, T eta); #### Scalar ```cpp -template < arithmetic T > -constexpr bool less(T x, T y) noexcept; - -template < arithmetic T > -constexpr bool less_equal(T x, T y) noexcept; - -template < arithmetic T > -constexpr bool greater(T x, T y) noexcept; - -template < arithmetic T > -constexpr bool greater_equal(T x, T y) noexcept; - template < arithmetic T > constexpr bool equal_to(T x, T y) noexcept; @@ -1190,42 +1276,6 @@ constexpr bool all(T x) noexcept; #### Vector ```cpp -template < typename T, size_t Size > -constexpr vec less(const vec& xs, T y); - -template < typename T, size_t Size > -constexpr vec less(T x, const vec& ys); - -template < typename T, size_t Size > -constexpr vec less(const vec& xs, const vec& ys); - -template < typename T, size_t Size > -constexpr vec less_equal(const vec& xs, T y); - -template < typename T, size_t Size > -constexpr vec less_equal(T x, const vec& ys); - -template < typename T, size_t Size > -constexpr vec less_equal(const vec& xs, const vec& ys); - -template < typename T, size_t Size > -constexpr vec greater(const vec& xs, T y); - -template < typename T, size_t Size > -constexpr vec greater(T x, const vec& ys); - -template < typename T, size_t Size > -constexpr vec greater(const vec& xs, const vec& ys); - -template < typename T, size_t Size > -constexpr vec greater_equal(const vec& xs, T y); - -template < typename T, size_t Size > -constexpr vec greater_equal(T x, const vec& ys); - -template < typename T, size_t Size > -constexpr vec greater_equal(const vec& xs, const vec& ys); - template < typename T, size_t Size > constexpr vec equal_to(const vec& xs, T y); diff --git a/headers/vmath.hpp/vmath_fun.hpp b/headers/vmath.hpp/vmath_fun.hpp index a876bd8..5fb5d74 100644 --- a/headers/vmath.hpp/vmath_fun.hpp +++ b/headers/vmath.hpp/vmath_fun.hpp @@ -404,30 +404,6 @@ namespace vmath_hpp namespace vmath_hpp { - template < typename T > - [[nodiscard]] std::enable_if_t, bool> - constexpr less(T x, T y) noexcept { - return x < y; - } - - template < typename T > - [[nodiscard]] std::enable_if_t, bool> - constexpr less_equal(T x, T y) noexcept { - return x <= y; - } - - template < typename T > - [[nodiscard]] std::enable_if_t, bool> - constexpr greater(T x, T y) noexcept { - return x > y; - } - - template < typename T > - [[nodiscard]] std::enable_if_t, bool> - constexpr greater_equal(T x, T y) noexcept { - return x >= y; - } - template < typename T > [[nodiscard]] std::enable_if_t, bool> constexpr equal_to(T x, T y) noexcept { diff --git a/headers/vmath.hpp/vmath_mat_fun.hpp b/headers/vmath.hpp/vmath_mat_fun.hpp index ad03a96..137460b 100644 --- a/headers/vmath.hpp/vmath_mat_fun.hpp +++ b/headers/vmath.hpp/vmath_mat_fun.hpp @@ -467,20 +467,80 @@ namespace vmath_hpp [[nodiscard]] constexpr bool operator!=(const mat& xs, const mat& ys) { return !(xs == ys); } +} +// +// Relational Operators +// + +namespace vmath_hpp +{ // operator< template < typename T, std::size_t Size > - [[nodiscard]] constexpr bool operator<(const mat& xs, const mat& ys) { - for ( std::size_t i = 0; i < Size; ++i ) { - if ( xs[i] < ys[i] ) { - return true; - } - if ( ys[i] < xs[i] ) { - return false; - } - } - return false; + [[nodiscard]] constexpr mat operator<(const mat& xs, T y) { + return map_join([y](const vec& x){ return x < y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator<(T x, const mat& ys) { + return map_join([x](const vec& y){ return x < y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator<(const mat& xs, const mat& ys) { + return map_join([](const vec& x, const vec& y){ return x < y; }, xs, ys); + } + + // operator<= + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator<=(const mat& xs, T y) { + return map_join([y](const vec& x){ return x <= y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator<=(T x, const mat& ys) { + return map_join([x](const vec& y){ return x <= y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator<=(const mat& xs, const mat& ys) { + return map_join([](const vec& x, const vec& y){ return x <= y; }, xs, ys); + } + + // operator> + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator>(const mat& xs, T y) { + return map_join([y](const vec& x){ return x > y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator>(T x, const mat& ys) { + return map_join([x](const vec& y){ return x > y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator>(const mat& xs, const mat& ys) { + return map_join([](const vec& x, const vec& y){ return x > y; }, xs, ys); + } + + // operator>= + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator>=(const mat& xs, T y) { + return map_join([y](const vec& x){ return x >= y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator>=(T x, const mat& ys) { + return map_join([x](const vec& y){ return x >= y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator>=(const mat& xs, const mat& ys) { + return map_join([](const vec& x, const vec& y){ return x >= y; }, xs, ys); } } diff --git a/headers/vmath.hpp/vmath_vec_fun.hpp b/headers/vmath.hpp/vmath_vec_fun.hpp index 3958c74..fb59157 100644 --- a/headers/vmath.hpp/vmath_vec_fun.hpp +++ b/headers/vmath.hpp/vmath_vec_fun.hpp @@ -436,20 +436,80 @@ namespace vmath_hpp [[nodiscard]] constexpr bool operator!=(const vec& xs, const vec& ys) { return !(xs == ys); } +} +// +// Relational Operators +// + +namespace vmath_hpp +{ // operator< template < typename T, std::size_t Size > - [[nodiscard]] constexpr bool operator<(const vec& xs, const vec& ys) { - for ( std::size_t i = 0; i < Size; ++i ) { - if ( xs[i] < ys[i] ) { - return true; - } - if ( ys[i] < xs[i] ) { - return false; - } - } - return false; + [[nodiscard]] constexpr vec operator<(const vec& xs, T y) { + return map_join([y](T x){ return x < y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator<(T x, const vec& ys) { + return map_join([x](T y){ return x < y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator<(const vec& xs, const vec& ys) { + return map_join([](T x, T y){ return x < y; }, xs, ys); + } + + // operator<= + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator<=(const vec& xs, T y) { + return map_join([y](T x){ return x <= y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator<=(T x, const vec& ys) { + return map_join([x](T y){ return x <= y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator<=(const vec& xs, const vec& ys) { + return map_join([](T x, T y){ return x <= y; }, xs, ys); + } + + // operator> + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator>(const vec& xs, T y) { + return map_join([y](T x){ return x > y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator>(T x, const vec& ys) { + return map_join([x](T y){ return x > y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator>(const vec& xs, const vec& ys) { + return map_join([](T x, T y){ return x > y; }, xs, ys); + } + + // operator>= + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator>=(const vec& xs, T y) { + return map_join([y](T x){ return x >= y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator>=(T x, const vec& ys) { + return map_join([x](T y){ return x >= y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator>=(const vec& xs, const vec& ys) { + return map_join([](T x, T y){ return x >= y; }, xs, ys); } } @@ -843,74 +903,6 @@ namespace vmath_hpp namespace vmath_hpp { - // less - - template < typename T, std::size_t Size > - [[nodiscard]] constexpr vec less(const vec& xs, T y) { - return map_join([y](T x){ return less(x, y); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] constexpr vec less(T x, const vec& ys) { - return map_join([x](T y){ return less(x, y); }, ys); - } - - template < typename T, std::size_t Size > - [[nodiscard]] constexpr vec less(const vec& xs, const vec& ys) { - return map_join([](T x, T y){ return less(x, y); }, xs, ys); - } - - // less_equal - - template < typename T, std::size_t Size > - [[nodiscard]] constexpr vec less_equal(const vec& xs, T y) { - return map_join([y](T x){ return less_equal(x, y); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] constexpr vec less_equal(T x, const vec& ys) { - return map_join([x](T y){ return less_equal(x, y); }, ys); - } - - template < typename T, std::size_t Size > - [[nodiscard]] constexpr vec less_equal(const vec& xs, const vec& ys) { - return map_join([](T x, T y){ return less_equal(x, y); }, xs, ys); - } - - // greater - - template < typename T, std::size_t Size > - [[nodiscard]] constexpr vec greater(const vec& xs, T y) { - return map_join([y](T x){ return greater(x, y); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] constexpr vec greater(T x, const vec& ys) { - return map_join([x](T y){ return greater(x, y); }, ys); - } - - template < typename T, std::size_t Size > - [[nodiscard]] constexpr vec greater(const vec& xs, const vec& ys) { - return map_join([](T x, T y){ return greater(x, y); }, xs, ys); - } - - // greater_equal - - template < typename T, std::size_t Size > - [[nodiscard]] constexpr vec greater_equal(const vec& xs, T y) { - return map_join([y](T x){ return greater_equal(x, y); }, xs); - } - - template < typename T, std::size_t Size > - [[nodiscard]] constexpr vec greater_equal(T x, const vec& ys) { - return map_join([x](T y){ return greater_equal(x, y); }, ys); - } - - template < typename T, std::size_t Size > - [[nodiscard]] constexpr vec greater_equal(const vec& xs, const vec& ys) { - return map_join([](T x, T y){ return greater_equal(x, y); }, xs, ys); - } - // equal_to template < typename T, std::size_t Size > diff --git a/untests/vmath_ext_tests.cpp b/untests/vmath_ext_tests.cpp index 51446e9..c47d810 100644 --- a/untests/vmath_ext_tests.cpp +++ b/untests/vmath_ext_tests.cpp @@ -7,8 +7,6 @@ #include "vmath_tests.hpp" #include "doctest/doctest.hpp" -#include -#include #include #include @@ -64,19 +62,6 @@ TEST_CASE("vmath/ext") { REQUIRE(std::hash{}({1,2,3,4}) == std::hash{}({1,2,3,4})); REQUIRE_FALSE(std::hash{}({1,2,3,4}) == std::hash{}({1,2,4,3})); - { - std::set s; - s.insert(int2(1,2)); - REQUIRE(s.count(int2(1,2)) > 0); - REQUIRE_FALSE(s.count(int2(1,1)) > 0); - } - { - std::map s; - s.emplace(int2(1,2),3); - s.emplace(int2(2,3),5); - REQUIRE(s[int2(1,2)] == 3); - REQUIRE(s[int2(2,3)] == 5); - } { std::unordered_set s; s.insert(int2(1,2)); diff --git a/untests/vmath_fun_tests.cpp b/untests/vmath_fun_tests.cpp index 33787f5..b9634d8 100644 --- a/untests/vmath_fun_tests.cpp +++ b/untests/vmath_fun_tests.cpp @@ -143,16 +143,6 @@ TEST_CASE("vmath/fun") { } SUBCASE("Scalar Relational Functions") { - STATIC_REQUIRE(less(0, 1)); - STATIC_REQUIRE(less_equal(0, 1)); - STATIC_REQUIRE_FALSE(less(1, 1)); - STATIC_REQUIRE(less_equal(1, 1)); - - STATIC_REQUIRE(greater(1, 0)); - STATIC_REQUIRE(greater_equal(1, 0)); - STATIC_REQUIRE_FALSE(greater(1, 1)); - STATIC_REQUIRE(greater_equal(1, 1)); - STATIC_REQUIRE(equal_to(1, 1)); STATIC_REQUIRE_FALSE(equal_to(0, 1)); STATIC_REQUIRE_FALSE(equal_to(0, 1, 0)); diff --git a/untests/vmath_mat_fun_tests.cpp b/untests/vmath_mat_fun_tests.cpp index cb34554..24d919d 100644 --- a/untests/vmath_mat_fun_tests.cpp +++ b/untests/vmath_mat_fun_tests.cpp @@ -163,6 +163,24 @@ TEST_CASE("vmath/mat_fun") { } } + SUBCASE("relational operators") { + STATIC_REQUIRE((int2x2(1,1,1,1) < int2x2(0,1,2,3)) == bool2x2(false, false, true, true)); + STATIC_REQUIRE((int2x2(0,1,2,3) < 1) == bool2x2(true, false, false, false)); + STATIC_REQUIRE((1 < int2x2(0,1,2,3)) == bool2x2(false, false, true, true)); + + STATIC_REQUIRE((int2x2(1,1,1,1) <= int2x2(0,1,2,3)) == bool2x2(false, true, true, true)); + STATIC_REQUIRE((int2x2(0,1,2,3) <= 1) == bool2x2(true, true, false, false)); + STATIC_REQUIRE((1 <= int2x2(0,1,2,3)) == bool2x2(false, true, true, true)); + + STATIC_REQUIRE((int2x2(1,1,1,1) > int2x2(0,1,2,3)) == bool2x2(true, false, false, false)); + STATIC_REQUIRE((int2x2(0,1,2,3) > 1) == bool2x2(false, false, true, true)); + STATIC_REQUIRE((1 > int2x2(0,1,2,3)) == bool2x2(true, false, false, false)); + + STATIC_REQUIRE((int2x2(1,1,1,1) >= int2x2(0,1,2,3)) == bool2x2(true, true, false, false)); + STATIC_REQUIRE((int2x2(0,1,2,3) >= 1) == bool2x2(false, true, true, true)); + STATIC_REQUIRE((1 >= int2x2(0,1,2,3)) == bool2x2(true, true, false, false)); + } + SUBCASE("transpose") { STATIC_REQUIRE(transpose(int2x2( 1, 2, diff --git a/untests/vmath_mat_tests.cpp b/untests/vmath_mat_tests.cpp index fed8be3..e582dda 100644 --- a/untests/vmath_mat_tests.cpp +++ b/untests/vmath_mat_tests.cpp @@ -270,14 +270,4 @@ TEST_CASE("vmath/mat") { STATIC_REQUIRE(int2x2(1,2,3,4) != int2x2(2,2,3,4)); STATIC_REQUIRE(int2x2(1,2,3,4) != int2x2(1,3,3,4)); } - - SUBCASE("operator<") { - STATIC_REQUIRE_FALSE(int2x2(1,2,3,4) < int2x2(1,2,3,4)); - - STATIC_REQUIRE(int2x2(1,1,3,4) < int2x2(1,2,3,4)); - STATIC_REQUIRE_FALSE(int2x2(1,2,3,4) < int2x2(1,1,3,4)); - - STATIC_REQUIRE(int2x2(0,3,3,4) < int2x2(1,2,3,4)); - STATIC_REQUIRE_FALSE(int2x2(1,2,3,4) < int2x2(0,3,3,4)); - } } diff --git a/untests/vmath_vec_fun_tests.cpp b/untests/vmath_vec_fun_tests.cpp index 9de7d7e..accb607 100644 --- a/untests/vmath_vec_fun_tests.cpp +++ b/untests/vmath_vec_fun_tests.cpp @@ -129,6 +129,24 @@ TEST_CASE("vmath/vec_fun") { } } + SUBCASE("Relational Operators") { + STATIC_REQUIRE((int3(1,1,1) < int3(0,1,2)) == bool3(false, false, true)); + STATIC_REQUIRE((int3(0,1,2) < 1) == bool3(true, false, false)); + STATIC_REQUIRE((1 < int3(0,1,2)) == bool3(false, false, true)); + + STATIC_REQUIRE((int3(1,1,1) <= int3(0,1,2)) == bool3(false, true, true)); + STATIC_REQUIRE((int3(0,1,2) <= 1) == bool3(true, true, false)); + STATIC_REQUIRE((1 <= int3(0,1,2)) == bool3(false, true, true)); + + STATIC_REQUIRE((int3(1,1,1) > int3(0,1,2)) == bool3(true, false, false)); + STATIC_REQUIRE((int3(0,1,2) > 1) == bool3(false, false, true)); + STATIC_REQUIRE((1 > int3(0,1,2)) == bool3(true, false, false)); + + STATIC_REQUIRE((int3(1,1,1) >= int3(0,1,2)) == bool3(true, true, false)); + STATIC_REQUIRE((int3(0,1,2) >= 1) == bool3(false, true, true)); + STATIC_REQUIRE((1 >= int3(0,1,2)) == bool3(true, true, false)); + } + SUBCASE("Angle and Trigonometry Functions") { STATIC_REQUIRE(radians(degrees(float2(12.13f))) == approx2(12.13f)); STATIC_REQUIRE(degrees(radians(float2(12.13f))) == approx2(12.13f)); @@ -251,22 +269,6 @@ TEST_CASE("vmath/vec_fun") { } SUBCASE("Vector Relational Functions") { - STATIC_REQUIRE(less(int3(1,1,1), int3(0,1,2)) == bool3(false, false, true)); - STATIC_REQUIRE(less(int3(0,1,2),1) == bool3(true, false, false)); - STATIC_REQUIRE(less(1,int3(0,1,2)) == bool3(false, false, true)); - - STATIC_REQUIRE(less_equal(int3(1,1,1), int3(0,1,2)) == bool3(false, true, true)); - STATIC_REQUIRE(less_equal(int3(0,1,2),1) == bool3(true, true, false)); - STATIC_REQUIRE(less_equal(1,int3(0,1,2)) == bool3(false, true, true)); - - STATIC_REQUIRE(greater(int3(1,1,1), int3(0,1,2)) == bool3(true, false, false)); - STATIC_REQUIRE(greater(int3(0,1,2),1) == bool3(false, false, true)); - STATIC_REQUIRE(greater(1,int3(0,1,2)) == bool3(true, false, false)); - - STATIC_REQUIRE(greater_equal(int3(1,1,1), int3(0,1,2)) == bool3(true, true, false)); - STATIC_REQUIRE(greater_equal(int3(0,1,2),1) == bool3(false, true, true)); - STATIC_REQUIRE(greater_equal(1,int3(0,1,2)) == bool3(true, true, false)); - STATIC_REQUIRE(equal_to(int3(1,1,1), int3(0,1,2)) == bool3(false, true, false)); STATIC_REQUIRE(equal_to(int3(0,1,2),1) == bool3(false, true, false)); STATIC_REQUIRE(equal_to(1,int3(0,1,2)) == bool3(false, true, false)); diff --git a/untests/vmath_vec_tests.cpp b/untests/vmath_vec_tests.cpp index 39e3a38..673683c 100644 --- a/untests/vmath_vec_tests.cpp +++ b/untests/vmath_vec_tests.cpp @@ -267,14 +267,4 @@ TEST_CASE("vmath/vec") { STATIC_REQUIRE(int2(1,2) != int2(2,2)); STATIC_REQUIRE(int2(1,2) != int2(1,3)); } - - SUBCASE("operator<") { - STATIC_REQUIRE_FALSE(int2(1,2) < int2(1,2)); - - STATIC_REQUIRE(int2(1,1) < int2(1,2)); - STATIC_REQUIRE_FALSE(int2(1,2) < int2(1,1)); - - STATIC_REQUIRE(int2(0,3) < int2(1,2)); - STATIC_REQUIRE_FALSE(int2(1,2) < int2(0,3)); - } }