From bccb1f60f10764bce74c9d233f21c3e65850f33b Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Wed, 25 Nov 2020 23:46:58 +0700 Subject: [PATCH] vector and matrix predef units --- headers/vmath.hpp/vmath_mat_ext.hpp | 15 +++++++++++++++ headers/vmath.hpp/vmath_vec_ext.hpp | 23 +++++++++++++++++++++++ untests/vmath_mat_ext_tests.cpp | 14 ++++++++++++++ untests/vmath_vec_ext_tests.cpp | 21 +++++++++++++++++++++ 4 files changed, 73 insertions(+) diff --git a/headers/vmath.hpp/vmath_mat_ext.hpp b/headers/vmath.hpp/vmath_mat_ext.hpp index bf6494d..ae33bf1 100644 --- a/headers/vmath.hpp/vmath_mat_ext.hpp +++ b/headers/vmath.hpp/vmath_mat_ext.hpp @@ -18,6 +18,21 @@ #include "vmath_mat.hpp" #include "vmath_mat_fun.hpp" +namespace vmath_hpp +{ + template < typename T > constexpr mat zero2x2() { return {0, 0, 0, 0}; } + template < typename T > constexpr mat zero3x3() { return {0, 0, 0, 0, 0, 0, 0, 0, 0}; } + template < typename T > constexpr mat zero4x4() { return {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; } + + template < typename T > constexpr mat unit2x2() { return {1, 1, 1, 1}; } + template < typename T > constexpr mat unit3x3() { return {1, 1, 1, 1, 1, 1, 1, 1, 1}; } + template < typename T > constexpr mat unit4x4() { return {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; } + + template < typename T > constexpr mat identity2x2() { return {1, 0, 0, 1}; } + template < typename T > constexpr mat identity3x3() { return {1, 0, 0, 0, 1, 0, 0, 0, 1}; } + template < typename T > constexpr mat identity4x4() { return {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; } +} + namespace std { template < typename T, size_t Size > diff --git a/headers/vmath.hpp/vmath_vec_ext.hpp b/headers/vmath.hpp/vmath_vec_ext.hpp index 75b3019..f47069e 100644 --- a/headers/vmath.hpp/vmath_vec_ext.hpp +++ b/headers/vmath.hpp/vmath_vec_ext.hpp @@ -14,6 +14,29 @@ #include "vmath_vec.hpp" #include "vmath_vec_fun.hpp" +namespace vmath_hpp +{ + template < typename T > constexpr vec zero2() { return {0, 0}; } + template < typename T > constexpr vec zero3() { return {0, 0, 0}; } + template < typename T > constexpr vec zero4() { return {0, 0, 0, 0}; } + + template < typename T > constexpr vec unit2() { return {1, 1}; } + template < typename T > constexpr vec unit3() { return {1, 1, 1}; } + template < typename T > constexpr vec unit4() { return {1, 1, 1, 1}; } + + template < typename T > constexpr vec unit2_x() { return {1, 0}; } + template < typename T > constexpr vec unit2_y() { return {0, 1}; } + + template < typename T > constexpr vec unit3_x() { return {1, 0, 0}; } + template < typename T > constexpr vec unit3_y() { return {0, 1, 0}; } + template < typename T > constexpr vec unit3_z() { return {0, 0, 1}; } + + template < typename T > constexpr vec unit4_x() { return {1, 0, 0, 0}; } + template < typename T > constexpr vec unit4_y() { return {0, 1, 0, 0}; } + template < typename T > constexpr vec unit4_z() { return {0, 0, 1, 0}; } + template < typename T > constexpr vec unit4_w() { return {0, 0, 0, 1}; } +} + namespace std { template < typename T, size_t Size > diff --git a/untests/vmath_mat_ext_tests.cpp b/untests/vmath_mat_ext_tests.cpp index 9cceae5..cb09f10 100644 --- a/untests/vmath_mat_ext_tests.cpp +++ b/untests/vmath_mat_ext_tests.cpp @@ -18,6 +18,20 @@ namespace } TEST_CASE("vmath/mat_ext") { + SECTION("units") { + STATIC_REQUIRE(zero2x2() == mat2i(0,0,0,0)); + STATIC_REQUIRE(zero3x3() == mat3i(0,0,0,0,0,0,0,0,0)); + STATIC_REQUIRE(zero4x4() == mat4i(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)); + + STATIC_REQUIRE(unit2x2() == mat2i(1,1,1,1)); + STATIC_REQUIRE(unit3x3() == mat3i(1,1,1,1,1,1,1,1,1)); + STATIC_REQUIRE(unit4x4() == mat4i(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)); + + STATIC_REQUIRE(identity2x2() == mat2i()); + STATIC_REQUIRE(identity3x3() == mat3i()); + STATIC_REQUIRE(identity4x4() == mat4i()); + } + SECTION("hash") { 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})); diff --git a/untests/vmath_vec_ext_tests.cpp b/untests/vmath_vec_ext_tests.cpp index 0db5f33..0fcdbb5 100644 --- a/untests/vmath_vec_ext_tests.cpp +++ b/untests/vmath_vec_ext_tests.cpp @@ -18,6 +18,27 @@ namespace } TEST_CASE("vmath/vec_ext") { + SECTION("units") { + STATIC_REQUIRE(zero2() == vec2i(0,0)); + STATIC_REQUIRE(zero3() == vec3i(0,0,0)); + STATIC_REQUIRE(zero4() == vec4i(0,0,0,0)); + + STATIC_REQUIRE(unit2() == vec2i(1,1)); + STATIC_REQUIRE(unit2_x() == vec2i(1,0)); + STATIC_REQUIRE(unit2_y() == vec2i(0,1)); + + STATIC_REQUIRE(unit3() == vec3i(1,1,1)); + STATIC_REQUIRE(unit3_x() == vec3i(1,0,0)); + STATIC_REQUIRE(unit3_y() == vec3i(0,1,0)); + STATIC_REQUIRE(unit3_z() == vec3i(0,0,1)); + + STATIC_REQUIRE(unit4() == vec4i(1,1,1,1)); + STATIC_REQUIRE(unit4_x() == vec4i(1,0,0,0)); + STATIC_REQUIRE(unit4_y() == vec4i(0,1,0,0)); + STATIC_REQUIRE(unit4_z() == vec4i(0,0,1,0)); + STATIC_REQUIRE(unit4_w() == vec4i(0,0,0,1)); + } + SECTION("hash") { REQUIRE(std::hash{}({1,2}) == std::hash{}({1,2})); REQUIRE_FALSE(std::hash{}({1,2}) == std::hash{}({2,1}));