From bf258a00ac36060a4cd268d5a258ae67ad6c05fc Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 26 Nov 2020 05:07:42 +0700 Subject: [PATCH] vector rotate functions --- headers/vmath.hpp/vmath.hpp | 7 +- headers/vmath.hpp/vmath_ext.hpp | 279 ++++++++++++++++++++++++++++ headers/vmath.hpp/vmath_mat_ext.hpp | 188 ------------------- headers/vmath.hpp/vmath_vec_ext.hpp | 79 -------- untests/vmath_ext_tests.cpp | 133 ++++++++++++- untests/vmath_mat_ext_tests.cpp | 88 --------- untests/vmath_mat_fun_tests.cpp | 3 +- untests/vmath_vec_ext_tests.cpp | 75 -------- untests/vmath_vec_fun_tests.cpp | 2 +- 9 files changed, 416 insertions(+), 438 deletions(-) delete mode 100644 headers/vmath.hpp/vmath_mat_ext.hpp delete mode 100644 headers/vmath.hpp/vmath_vec_ext.hpp delete mode 100644 untests/vmath_mat_ext_tests.cpp delete mode 100644 untests/vmath_vec_ext_tests.cpp diff --git a/headers/vmath.hpp/vmath.hpp b/headers/vmath.hpp/vmath.hpp index 5353745..4151a03 100644 --- a/headers/vmath.hpp/vmath.hpp +++ b/headers/vmath.hpp/vmath.hpp @@ -6,10 +6,13 @@ #pragma once +#include "vmath_fwd.hpp" + #include "vmath_fun.hpp" +#include "vmath_ext.hpp" + #include "vmath_mat.hpp" #include "vmath_mat_fun.hpp" -#include "vmath_mat_ext.hpp" + #include "vmath_vec.hpp" #include "vmath_vec_fun.hpp" -#include "vmath_vec_ext.hpp" diff --git a/headers/vmath.hpp/vmath_ext.hpp b/headers/vmath.hpp/vmath_ext.hpp index a5a0c02..652a448 100644 --- a/headers/vmath.hpp/vmath_ext.hpp +++ b/headers/vmath.hpp/vmath_ext.hpp @@ -9,6 +9,51 @@ #include "vmath_fwd.hpp" #include "vmath_fun.hpp" +#include "vmath_vec_fun.hpp" +#include "vmath_mat_fun.hpp" + +// +// Units +// + +namespace vmath_hpp +{ + template < typename T > inline constexpr vec zero2{0, 0}; + template < typename T > inline constexpr vec zero3{0, 0, 0}; + template < typename T > inline constexpr vec zero4{0, 0, 0, 0}; + + template < typename T > inline constexpr vec unit2{1, 1}; + template < typename T > inline constexpr vec unit3{1, 1, 1}; + template < typename T > inline constexpr vec unit4{1, 1, 1, 1}; + + template < typename T > inline constexpr vec unit2_x{1, 0}; + template < typename T > inline constexpr vec unit2_y{0, 1}; + + template < typename T > inline constexpr vec unit3_x{1, 0, 0}; + template < typename T > inline constexpr vec unit3_y{0, 1, 0}; + template < typename T > inline constexpr vec unit3_z{0, 0, 1}; + + template < typename T > inline constexpr vec unit4_x{1, 0, 0, 0}; + template < typename T > inline constexpr vec unit4_y{0, 1, 0, 0}; + template < typename T > inline constexpr vec unit4_z{0, 0, 1, 0}; + template < typename T > inline constexpr vec unit4_w{0, 0, 0, 1}; + + template < typename T > inline constexpr mat zero2x2{0, 0, 0, 0}; + template < typename T > inline constexpr mat zero3x3{0, 0, 0, 0, 0, 0, 0, 0, 0}; + template < typename T > inline constexpr mat zero4x4{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + + template < typename T > inline constexpr mat unit2x2{1, 1, 1, 1}; + template < typename T > inline constexpr mat unit3x3{1, 1, 1, 1, 1, 1, 1, 1, 1}; + template < typename T > inline constexpr mat unit4x4{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + + template < typename T > inline constexpr mat identity2x2{1, 0, 0, 1}; + template < typename T > inline constexpr mat identity3x3{1, 0, 0, 0, 1, 0, 0, 0, 1}; + template < typename T > inline constexpr mat identity4x4{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; +} + +// +// Hash +// namespace vmath_hpp::detail { @@ -18,12 +63,246 @@ namespace vmath_hpp::detail return (seed ^= std::hash{}(x) + 0x9e3779b9 + (seed << 6) + ( seed >> 2)); } }; + + template < typename T, size_t Size > + std::size_t hash(const vec& v) noexcept { + return fold(hash_combiner{}, std::size_t{}, v); + } + + template < typename T, size_t Size > + std::size_t hash(const mat& m) noexcept { + return fold(hash_combiner{}, std::size_t{}, m); + } } +namespace std +{ + template < typename T, size_t Size > + struct hash> { + size_t operator()(const vmath_hpp::vec& v) const noexcept { + return vmath_hpp::detail::hash(v); + } + }; + + template < typename T, size_t Size > + struct hash> { + size_t operator()(const vmath_hpp::mat& m) const noexcept { + return vmath_hpp::detail::hash(m); + } + }; +} + +// +// Cast +// + namespace vmath_hpp { template < typename To, typename From > constexpr To cast_to(From x) noexcept { return static_cast(x); } + + template < typename To, typename From, std::size_t Size > + constexpr vec cast_to(const vec& v) { + return detail::map([](From x){ return cast_to(x); }, v); + } + + template < typename To, typename From, std::size_t Size > + constexpr mat cast_to(const mat& m) { + return detail::map([](const vec& v){ return cast_to(v); }, m); + } +} + +// +// Access +// + +namespace vmath_hpp +{ + // component + + template < typename T, std::size_t Size > + constexpr T component(const vec& v, std::size_t index) { + return v[index]; + } + + template < typename T, std::size_t Size > + constexpr vec component(vec v, std::size_t index, T x) { + v[index] = x; + return v; + } + + // row + + template < typename T, std::size_t Size > + constexpr vec row(const mat& m, std::size_t index) { + return m.rows[index]; + } + + template < typename T, std::size_t Size > + constexpr mat row(mat m, std::size_t index, const vec& v) { + m.rows[index] = v; + return m; + } + + // column + + namespace impl + { + template < typename T, std::size_t Size, std::size_t... Is > + constexpr vec column_impl(const mat& m, std::size_t index, std::index_sequence) { + return { m[Is][index]... }; + } + + template < typename T, std::size_t Size, std::size_t... Is > + constexpr mat column_impl(const mat& m, std::size_t index, const vec& v, std::index_sequence) { + return { component(m[Is], index, v[Is])... }; + } + } + + template < typename T, std::size_t Size > + constexpr vec column(const mat& m, std::size_t index) { + return impl::column_impl(m, index, std::make_index_sequence{}); + } + + template < typename T, std::size_t Size > + constexpr mat column(const mat& m, std::size_t index, const vec& v) { + return impl::column_impl(m, index, v, std::make_index_sequence{}); + } +} + +// +// Matrix Transform +// + +namespace vmath_hpp +{ + // translate + + template < typename T > + constexpr mat translate(T x, T y, T z) { + return { + {1, 0, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 1, 0}, + {x, y, z, 1}}; + } + + template < typename T > + constexpr mat translate(const vec& xyz) { + return translate(xyz.x, xyz.y, xyz.z); + } + + template < typename T > + constexpr mat translate(const mat& m, T x, T y, T z) { + return m * translate(x, y, z); + } + + template < typename T > + constexpr mat translate(const mat& m, const vec& xyz) { + return m * translate(xyz); + } + + // rotate + + template < typename T > + mat rotate(T angle, T axis_x, T axis_y, T axis_z) { + const T x = axis_x; + const T y = axis_y; + const T z = axis_z; + const T px = x * x; + const T py = y * y; + const T pz = z * z; + const T cs = cos(angle); + const T sn = sin(angle); + const T ics = T(1) - cs; + const T xym = x * y * ics; + const T xzm = x * z * ics; + const T yzm = y * z * ics; + const T xsn = x * sn; + const T ysn = y * sn; + const T zsn = z * sn; + return { + px * ics + cs, xym + zsn, xzm - ysn, 0, + xym - zsn, py * ics + cs, yzm + xsn, 0, + xzm + ysn, yzm - xsn, pz * ics + cs, 0, + 0, 0, 0, 1}; + } + + template < typename T > + mat rotate(T angle, const vec& axis) { + return rotate(angle, axis.x, axis.y, axis.z); + } + + template < typename T > + mat rotate(const mat& m, T angle, T axis_x, T axis_y, T axis_z) { + return m * rotate(angle, axis_x, axis_y, axis_z); + } + + template < typename T > + mat rotate(const mat& m, T angle, const vec& axis) { + return m * rotate(angle, axis); + } + + // scale + + template < typename T > + constexpr mat scale(T x, T y, T z) { + return { + {x, 0, 0, 0}, + {0, y, 0, 0}, + {0, 0, z, 0}, + {0, 0, 0, 1}}; + } + + template < typename T > + constexpr mat scale(const vec& xyz) { + return scale(xyz.x, xyz.y, xyz.z); + } + + template < typename T > + constexpr mat scale(const mat& m, T x, T y, T z) { + return m * scale(x, y, z); + } + + template < typename T > + constexpr mat scale(const mat& m, const vec& xyz) { + return m * scale(xyz); + } +} + +// +// Vector Transform +// + +namespace vmath_hpp +{ + // angle + + template < typename T, std::size_t Size > + T angle(const vec& x, const vec& y) { + return acos(dot(x, y) * invsqrt(length2(x) * length2(y))); + } + + // rotate + + template < typename T > + vec rotate(const vec& v, T angle) { + const T cs = cos(angle); + const T sn = sin(angle); + return { + v.x * cs - v.y * sn, + v.x * sn + v.y * cs}; + } + + template < typename T > + vec rotate(const vec& v, T angle, const vec& normal) { + return v * mat(rotate(angle, normal)); + } + + template < typename T > + vec rotate(const vec& v, T angle, const vec& normal) { + return v * rotate(angle, normal); + } } diff --git a/headers/vmath.hpp/vmath_mat_ext.hpp b/headers/vmath.hpp/vmath_mat_ext.hpp deleted file mode 100644 index 0fab15e..0000000 --- a/headers/vmath.hpp/vmath_mat_ext.hpp +++ /dev/null @@ -1,188 +0,0 @@ -/******************************************************************************* - * This file is part of the "https://github.com/blackmatov/vmath.hpp" - * For conditions of distribution and use, see copyright notice in LICENSE.md - * Copyright (C) 2020, by Matvey Cherevko (blackmatov@gmail.com) - ******************************************************************************/ - -#pragma once - -#include "vmath_fwd.hpp" - -#include "vmath_ext.hpp" -#include "vmath_fun.hpp" - -#include "vmath_vec.hpp" -#include "vmath_vec_fun.hpp" -#include "vmath_vec_ext.hpp" - -#include "vmath_mat.hpp" -#include "vmath_mat_fun.hpp" - -namespace vmath_hpp -{ - template < typename T > inline constexpr mat zero2x2{0, 0, 0, 0}; - template < typename T > inline constexpr mat zero3x3{0, 0, 0, 0, 0, 0, 0, 0, 0}; - template < typename T > inline constexpr mat zero4x4{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - - template < typename T > inline constexpr mat unit2x2{1, 1, 1, 1}; - template < typename T > inline constexpr mat unit3x3{1, 1, 1, 1, 1, 1, 1, 1, 1}; - template < typename T > inline constexpr mat unit4x4{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; - - template < typename T > inline constexpr mat identity2x2{1, 0, 0, 1}; - template < typename T > inline constexpr mat identity3x3{1, 0, 0, 0, 1, 0, 0, 0, 1}; - template < typename T > inline constexpr mat identity4x4{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; -} - -namespace std -{ - template < typename T, size_t Size > - struct hash> { - size_t operator()(const vmath_hpp::mat& m) const noexcept { - return vmath_hpp::detail::fold(vmath_hpp::detail::hash_combiner{}, size_t{}, m); - } - }; -} - -namespace vmath_hpp -{ - // cast_to - - template < typename To, typename From, std::size_t Size > - constexpr mat cast_to(const mat& m) { - return detail::map([](const vec& v){ return cast_to(v); }, m); - } - - // row - - template < typename T, std::size_t Size > - constexpr vec row(const mat& m, std::size_t index) { - return m.rows[index]; - } - - template < typename T, std::size_t Size > - constexpr mat row(mat m, std::size_t index, const vec& v) { - m.rows[index] = v; - return m; - } - - // column - - namespace impl - { - template < typename T, std::size_t Size, std::size_t... Is > - constexpr vec column_impl(const mat& m, std::size_t index, std::index_sequence) { - return { m[Is][index]... }; - } - - template < typename T, std::size_t Size, std::size_t... Is > - constexpr mat column_impl(const mat& m, std::size_t index, const vec& v, std::index_sequence) { - return { component(m[Is], index, v[Is])... }; - } - } - - template < typename T, std::size_t Size > - constexpr vec column(const mat& m, std::size_t index) { - return impl::column_impl(m, index, std::make_index_sequence{}); - } - - template < typename T, std::size_t Size > - constexpr mat column(const mat& m, std::size_t index, const vec& v) { - return impl::column_impl(m, index, v, std::make_index_sequence{}); - } -} - -namespace vmath_hpp -{ - // translate - - template < typename T > - constexpr mat translate(T x, T y, T z) { - return { - {1, 0, 0, 0}, - {0, 1, 0, 0}, - {0, 0, 1, 0}, - {x, y, z, 1}}; - } - - template < typename T > - constexpr mat translate(const vec& xyz) { - return translate(xyz.x, xyz.y, xyz.z); - } - - template < typename T > - constexpr mat translate(const mat& m, T x, T y, T z) { - return m * translate(x, y, z); - } - - template < typename T > - constexpr mat translate(const mat& m, const vec& xyz) { - return m * translate(xyz); - } - - // rotate - - template < typename T > - mat rotate(T angle, T axis_x, T axis_y, T axis_z) { - const T x = axis_x; - const T y = axis_y; - const T z = axis_z; - const T px = x * x; - const T py = y * y; - const T pz = z * z; - const T cs = cos(angle); - const T sn = sin(angle); - const T ics = T(1) - cs; - const T xym = x * y * ics; - const T xzm = x * z * ics; - const T yzm = y * z * ics; - const T xsn = x * sn; - const T ysn = y * sn; - const T zsn = z * sn; - return { - px * ics + cs, xym + zsn, xzm - ysn, 0, - xym - zsn, py * ics + cs, yzm + xsn, 0, - xzm + ysn, yzm - xsn, pz * ics + cs, 0, - 0, 0, 0, 1}; - } - - template < typename T > - mat rotate(T angle, const vec& axis) { - return rotate(angle, axis.x, axis.y, axis.z); - } - - template < typename T > - mat rotate(const mat& m, T angle, T axis_x, T axis_y, T axis_z) { - return m * rotate(angle, axis_x, axis_y, axis_z); - } - - template < typename T > - mat rotate(const mat& m, T angle, const vec& axis) { - return m * rotate(angle, axis); - } - - // scale - - template < typename T > - constexpr mat scale(T x, T y, T z) { - return { - {x, 0, 0, 0}, - {0, y, 0, 0}, - {0, 0, z, 0}, - {0, 0, 0, 1}}; - } - - template < typename T > - constexpr mat scale(const vec& xyz) { - return scale(xyz.x, xyz.y, xyz.z); - } - - template < typename T > - constexpr mat scale(const mat& m, T x, T y, T z) { - return m * scale(x, y, z); - } - - template < typename T > - constexpr mat scale(const mat& m, const vec& xyz) { - return m * scale(xyz); - } -} diff --git a/headers/vmath.hpp/vmath_vec_ext.hpp b/headers/vmath.hpp/vmath_vec_ext.hpp deleted file mode 100644 index e62c142..0000000 --- a/headers/vmath.hpp/vmath_vec_ext.hpp +++ /dev/null @@ -1,79 +0,0 @@ -/******************************************************************************* - * This file is part of the "https://github.com/blackmatov/vmath.hpp" - * For conditions of distribution and use, see copyright notice in LICENSE.md - * Copyright (C) 2020, by Matvey Cherevko (blackmatov@gmail.com) - ******************************************************************************/ - -#pragma once - -#include "vmath_fwd.hpp" - -#include "vmath_ext.hpp" -#include "vmath_fun.hpp" - -#include "vmath_vec.hpp" -#include "vmath_vec_fun.hpp" - -namespace vmath_hpp -{ - template < typename T > inline constexpr vec zero2{0, 0}; - template < typename T > inline constexpr vec zero3{0, 0, 0}; - template < typename T > inline constexpr vec zero4{0, 0, 0, 0}; - - template < typename T > inline constexpr vec unit2{1, 1}; - template < typename T > inline constexpr vec unit3{1, 1, 1}; - template < typename T > inline constexpr vec unit4{1, 1, 1, 1}; - - template < typename T > inline constexpr vec unit2_x{1, 0}; - template < typename T > inline constexpr vec unit2_y{0, 1}; - - template < typename T > inline constexpr vec unit3_x{1, 0, 0}; - template < typename T > inline constexpr vec unit3_y{0, 1, 0}; - template < typename T > inline constexpr vec unit3_z{0, 0, 1}; - - template < typename T > inline constexpr vec unit4_x{1, 0, 0, 0}; - template < typename T > inline constexpr vec unit4_y{0, 1, 0, 0}; - template < typename T > inline constexpr vec unit4_z{0, 0, 1, 0}; - template < typename T > inline constexpr vec unit4_w{0, 0, 0, 1}; -} - -namespace std -{ - template < typename T, size_t Size > - struct hash> { - size_t operator()(const vmath_hpp::vec& v) const noexcept { - return vmath_hpp::detail::fold(vmath_hpp::detail::hash_combiner{}, size_t{}, v); - } - }; -} - -namespace vmath_hpp -{ - // cast_to - - template < typename To, typename From, std::size_t Size > - constexpr vec cast_to(const vec& v) { - return detail::map([](From x){ return cast_to(x); }, v); - } - - // component - - template < typename T, std::size_t Size > - constexpr T component(const vec& v, std::size_t index) { - return v[index]; - } - - template < typename T, std::size_t Size > - constexpr vec component(vec v, std::size_t index, T x) { - v[index] = x; - return v; - } -} - -namespace vmath_hpp -{ - template < typename T, std::size_t Size > - T angle(const vec& x, const vec& y) { - return acos(dot(x, y) * invsqrt(length2(x) * length2(y))); - } -} diff --git a/untests/vmath_ext_tests.cpp b/untests/vmath_ext_tests.cpp index 8a30350..1944be6 100644 --- a/untests/vmath_ext_tests.cpp +++ b/untests/vmath_ext_tests.cpp @@ -18,9 +18,136 @@ namespace } TEST_CASE("vmath/ext") { + SECTION("units") { + STATIC_REQUIRE(zero2 == int2(0,0)); + STATIC_REQUIRE(zero3 == int3(0,0,0)); + STATIC_REQUIRE(zero4 == int4(0,0,0,0)); + + STATIC_REQUIRE(unit2 == int2(1,1)); + STATIC_REQUIRE(unit2_x == int2(1,0)); + STATIC_REQUIRE(unit2_y == int2(0,1)); + + STATIC_REQUIRE(unit3 == int3(1,1,1)); + STATIC_REQUIRE(unit3_x == int3(1,0,0)); + STATIC_REQUIRE(unit3_y == int3(0,1,0)); + STATIC_REQUIRE(unit3_z == int3(0,0,1)); + + STATIC_REQUIRE(unit4 == int4(1,1,1,1)); + STATIC_REQUIRE(unit4_x == int4(1,0,0,0)); + STATIC_REQUIRE(unit4_y == int4(0,1,0,0)); + STATIC_REQUIRE(unit4_z == int4(0,0,1,0)); + STATIC_REQUIRE(unit4_w == int4(0,0,0,1)); + + STATIC_REQUIRE(zero2x2 == int2x2(0,0,0,0)); + STATIC_REQUIRE(zero3x3 == int3x3(0,0,0,0,0,0,0,0,0)); + STATIC_REQUIRE(zero4x4 == int4x4(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)); + + STATIC_REQUIRE(unit2x2 == int2x2(1,1,1,1)); + STATIC_REQUIRE(unit3x3 == int3x3(1,1,1,1,1,1,1,1,1)); + STATIC_REQUIRE(unit4x4 == int4x4(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)); + + STATIC_REQUIRE(identity2x2 == int2x2()); + STATIC_REQUIRE(identity3x3 == int3x3()); + STATIC_REQUIRE(identity4x4 == int4x4()); + } + + SECTION("hash") { + REQUIRE(std::hash{}({1,2}) == std::hash{}({1,2})); + REQUIRE_FALSE(std::hash{}({1,2}) == std::hash{}({2,1})); + + REQUIRE(std::hash{}({1,2,3}) == std::hash{}({1,2,3})); + REQUIRE_FALSE(std::hash{}({1,2,3}) == std::hash{}({3,2,1})); + + REQUIRE(std::hash{}({1,2,3,4}) == std::hash{}({1,2,3,4})); + REQUIRE_FALSE(std::hash{}({1,2,3,4}) == std::hash{}({3,2,1,4})); + + 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})); + } + SECTION("cast_to") { - constexpr auto i = cast_to(1.5f); - STATIC_REQUIRE(i == 1); - STATIC_REQUIRE(std::is_same_v); + { + constexpr auto i = cast_to(1.5f); + STATIC_REQUIRE(i == 1); + STATIC_REQUIRE(std::is_same_v); + } + { + constexpr auto v = cast_to(float2{1.5f}); + STATIC_REQUIRE(v == int2(1)); + STATIC_REQUIRE(std::is_same_v); + } + { + constexpr auto m = cast_to(float2x2{1.5f}); + STATIC_REQUIRE(m == int2x2(1)); + STATIC_REQUIRE(std::is_same_v); + } + } + + SECTION("component") { + STATIC_REQUIRE(component(int2{1,2}, 0) == 1); + STATIC_REQUIRE(component(int2{1,2}, 1) == 2); + + STATIC_REQUIRE(component(int2{0,0}, 0, 1) == int2{1,0}); + STATIC_REQUIRE(component(int2{0,0}, 1, 2) == int2{0,2}); + } + + SECTION("row") { + STATIC_REQUIRE(row(int2x2(1,2,3,4), 0) == int2(1,2)); + STATIC_REQUIRE(row(int2x2(1,2,3,4), 1) == int2(3,4)); + + STATIC_REQUIRE(row(int2x2(), 0, {1,2}) == int2x2(1,2,0,1)); + STATIC_REQUIRE(row(int2x2(), 1, {3,4}) == int2x2(1,0,3,4)); + } + + SECTION("column") { + STATIC_REQUIRE(column(int2x2(1,2,3,4), 0) == int2(1,3)); + STATIC_REQUIRE(column(int2x2(1,2,3,4), 1) == int2(2,4)); + + STATIC_REQUIRE(column(int2x2(), 0, {2,3}) == int2x2(2,0,3,1)); + STATIC_REQUIRE(column(int2x2(), 1, {3,4}) == int2x2(1,3,0,4)); + } + + SECTION("matrix translate") { + STATIC_REQUIRE(float4(2.f,3.f,4.f,1.f) * translate(1.f,2.f,3.f) == approx4(3.f,5.f,7.f,1.f)); + STATIC_REQUIRE(float4(2.f,3.f,4.f,1.f) * translate(float3{1.f,2.f,3.f}) == approx4(3.f,5.f,7.f,1.f)); + + STATIC_REQUIRE(float4(2.f,3.f,4.f,1.f) * translate(translate(1.f,2.f,3.f), 1.f,2.f,3.f) == approx4(4.f,7.f,10.f,1.f)); + STATIC_REQUIRE(float4(2.f,3.f,4.f,1.f) * translate(translate(1.f,2.f,3.f), float3{1.f,2.f,3.f}) == approx4(4.f,7.f,10.f,1.f)); + } + + SECTION("matrix rotate") { + constexpr float pi = radians(180.f); + constexpr float pi_2 = radians(90.f); + + REQUIRE(float4(2.f,3.f,4.f,1.f) * rotate(pi,0.f,0.f,1.f) == approx4(-2.f,-3.f,4.f,1.f)); + REQUIRE(float4(2.f,3.f,4.f,1.f) * rotate(pi,float3{0.f,0.f,1.f}) == approx4(-2.f,-3.f,4.f,1.f)); + + REQUIRE(float4(2.f,3.f,4.f,1.f) * rotate(rotate(pi_2,0.f,0.f,1.f),pi_2,0.f,0.f,1.f) == approx4(-2.f,-3.f,4.f,1.f)); + REQUIRE(float4(2.f,3.f,4.f,1.f) * rotate(rotate(pi_2,0.f,0.f,1.f),pi_2,float3{0.f,0.f,1.f}) == approx4(-2.f,-3.f,4.f,1.f)); + } + + SECTION("matrix scale") { + STATIC_REQUIRE(float4(2.f,3.f,4.f,1.f) * scale(2.f,3.f,4.f) == approx4(4.f,9.f,16.f,1.f)); + STATIC_REQUIRE(float4(2.f,3.f,4.f,1.f) * scale(float3{2.f,3.f,4.f}) == approx4(4.f,9.f,16.f,1.f)); + + STATIC_REQUIRE(float4(2.f,3.f,4.f,1.f) * scale(scale(2.f,2.f,2.f), 2.f,3.f,4.f) == approx4(8.f,18.f,32.f,1.f)); + STATIC_REQUIRE(float4(2.f,3.f,4.f,1.f) * scale(scale(2.f,2.f,2.f), float3{2.f,3.f,4.f}) == approx4(8.f,18.f,32.f,1.f)); + } + + SECTION("vector angle") { + REQUIRE(angle(float2(2.f,0.f), float2(0.f,1.f)) == Approx(radians(90.f))); + REQUIRE(angle(float2(0.f,3.f), float2(1.f,0.f)) == Approx(radians(90.f))); + REQUIRE(angle(float2(0.5f,0.f), float2(-1.f,0.f)) == Approx(radians(180.f))); + REQUIRE(angle(float2(-0.2f,0.f), float2(1.f,0.f)) == Approx(radians(180.f))); + REQUIRE(angle(float3(0.f,2.f,0.f), float3(0.f,0.f,1.f)) == Approx(radians(90.f))); + REQUIRE(angle(float3(0.f,0.f,3.f), float3(0.f,1.f,0.f)) == Approx(radians(90.f))); + } + + SECTION("vector rotate") { + REQUIRE(rotate(float2(2.f,0.f), radians(90.f)) == approx2(0.f,2.f)); + REQUIRE(rotate(float2(1.5f,0.f), radians(-90.f)) == approx2(0.f,-1.5f)); + + REQUIRE(rotate(float3(1.5f,0.f,0.f), radians(90.f), float3(0,0,1)) == approx3(0.f,1.5f,0.f)); + REQUIRE(rotate(float4(1.5f,0.f,0.f,1.f), radians(90.f), float3(0,0,1)) == approx4(0.f,1.5f,0.f,1.f)); } } diff --git a/untests/vmath_mat_ext_tests.cpp b/untests/vmath_mat_ext_tests.cpp deleted file mode 100644 index 36fbc95..0000000 --- a/untests/vmath_mat_ext_tests.cpp +++ /dev/null @@ -1,88 +0,0 @@ -/******************************************************************************* - * This file is part of the "https://github.com/blackmatov/vmath.hpp" - * For conditions of distribution and use, see copyright notice in LICENSE.md - * Copyright (C) 2020, by Matvey Cherevko (blackmatov@gmail.com) - ******************************************************************************/ - -#include - -#define CATCH_CONFIG_FAST_COMPILE -#include - -#include "vmath_tests.hpp" - -namespace -{ - using namespace vmath_hpp; - using namespace vmath_tests; -} - -TEST_CASE("vmath/mat_ext") { - SECTION("units") { - STATIC_REQUIRE(zero2x2 == int2x2(0,0,0,0)); - STATIC_REQUIRE(zero3x3 == int3x3(0,0,0,0,0,0,0,0,0)); - STATIC_REQUIRE(zero4x4 == int4x4(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)); - - STATIC_REQUIRE(unit2x2 == int2x2(1,1,1,1)); - STATIC_REQUIRE(unit3x3 == int3x3(1,1,1,1,1,1,1,1,1)); - STATIC_REQUIRE(unit4x4 == int4x4(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)); - - STATIC_REQUIRE(identity2x2 == int2x2()); - STATIC_REQUIRE(identity3x3 == int3x3()); - STATIC_REQUIRE(identity4x4 == int4x4()); - } - - 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})); - } - - SECTION("cast_to") { - constexpr auto m = cast_to(float2x2{1.5f}); - STATIC_REQUIRE(m == int2x2(1)); - STATIC_REQUIRE(std::is_same_v); - } - - SECTION("row") { - STATIC_REQUIRE(row(int2x2(1,2,3,4), 0) == int2(1,2)); - STATIC_REQUIRE(row(int2x2(1,2,3,4), 1) == int2(3,4)); - - STATIC_REQUIRE(row(int2x2(), 0, {1,2}) == int2x2(1,2,0,1)); - STATIC_REQUIRE(row(int2x2(), 1, {3,4}) == int2x2(1,0,3,4)); - } - - SECTION("column") { - STATIC_REQUIRE(column(int2x2(1,2,3,4), 0) == int2(1,3)); - STATIC_REQUIRE(column(int2x2(1,2,3,4), 1) == int2(2,4)); - - STATIC_REQUIRE(column(int2x2(), 0, {2,3}) == int2x2(2,0,3,1)); - STATIC_REQUIRE(column(int2x2(), 1, {3,4}) == int2x2(1,3,0,4)); - } - - SECTION("translate") { - STATIC_REQUIRE(float4(2.f,3.f,4.f,1.f) * translate(1.f,2.f,3.f) == approx4(3.f,5.f,7.f,1.f)); - STATIC_REQUIRE(float4(2.f,3.f,4.f,1.f) * translate(float3{1.f,2.f,3.f}) == approx4(3.f,5.f,7.f,1.f)); - - STATIC_REQUIRE(float4(2.f,3.f,4.f,1.f) * translate(translate(1.f,2.f,3.f), 1.f,2.f,3.f) == approx4(4.f,7.f,10.f,1.f)); - STATIC_REQUIRE(float4(2.f,3.f,4.f,1.f) * translate(translate(1.f,2.f,3.f), float3{1.f,2.f,3.f}) == approx4(4.f,7.f,10.f,1.f)); - } - - SECTION("rotate") { - constexpr float pi = radians(180.f); - constexpr float pi_2 = radians(90.f); - - REQUIRE(float4(2.f,3.f,4.f,1.f) * rotate(pi,0.f,0.f,1.f) == approx4(-2.f,-3.f,4.f,1.f)); - REQUIRE(float4(2.f,3.f,4.f,1.f) * rotate(pi,float3{0.f,0.f,1.f}) == approx4(-2.f,-3.f,4.f,1.f)); - - REQUIRE(float4(2.f,3.f,4.f,1.f) * rotate(rotate(pi_2,0.f,0.f,1.f),pi_2,0.f,0.f,1.f) == approx4(-2.f,-3.f,4.f,1.f)); - REQUIRE(float4(2.f,3.f,4.f,1.f) * rotate(rotate(pi_2,0.f,0.f,1.f),pi_2,float3{0.f,0.f,1.f}) == approx4(-2.f,-3.f,4.f,1.f)); - } - - SECTION("scale") { - STATIC_REQUIRE(float4(2.f,3.f,4.f,1.f) * scale(2.f,3.f,4.f) == approx4(4.f,9.f,16.f,1.f)); - STATIC_REQUIRE(float4(2.f,3.f,4.f,1.f) * scale(float3{2.f,3.f,4.f}) == approx4(4.f,9.f,16.f,1.f)); - - STATIC_REQUIRE(float4(2.f,3.f,4.f,1.f) * scale(scale(2.f,2.f,2.f), 2.f,3.f,4.f) == approx4(8.f,18.f,32.f,1.f)); - STATIC_REQUIRE(float4(2.f,3.f,4.f,1.f) * scale(scale(2.f,2.f,2.f), float3{2.f,3.f,4.f}) == approx4(8.f,18.f,32.f,1.f)); - } -} diff --git a/untests/vmath_mat_fun_tests.cpp b/untests/vmath_mat_fun_tests.cpp index f220f4d..f1b19bb 100644 --- a/untests/vmath_mat_fun_tests.cpp +++ b/untests/vmath_mat_fun_tests.cpp @@ -4,8 +4,7 @@ * Copyright (C) 2020, by Matvey Cherevko (blackmatov@gmail.com) ******************************************************************************/ -#include -#include +#include #define CATCH_CONFIG_FAST_COMPILE #include diff --git a/untests/vmath_vec_ext_tests.cpp b/untests/vmath_vec_ext_tests.cpp deleted file mode 100644 index a72c2e5..0000000 --- a/untests/vmath_vec_ext_tests.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/******************************************************************************* - * This file is part of the "https://github.com/blackmatov/vmath.hpp" - * For conditions of distribution and use, see copyright notice in LICENSE.md - * Copyright (C) 2020, by Matvey Cherevko (blackmatov@gmail.com) - ******************************************************************************/ - -#include - -#define CATCH_CONFIG_FAST_COMPILE -#include - -#include "vmath_tests.hpp" - -namespace -{ - using namespace vmath_hpp; - using namespace vmath_tests; -} - -TEST_CASE("vmath/vec_ext") { - SECTION("units") { - STATIC_REQUIRE(zero2 == int2(0,0)); - STATIC_REQUIRE(zero3 == int3(0,0,0)); - STATIC_REQUIRE(zero4 == int4(0,0,0,0)); - - STATIC_REQUIRE(unit2 == int2(1,1)); - STATIC_REQUIRE(unit2_x == int2(1,0)); - STATIC_REQUIRE(unit2_y == int2(0,1)); - - STATIC_REQUIRE(unit3 == int3(1,1,1)); - STATIC_REQUIRE(unit3_x == int3(1,0,0)); - STATIC_REQUIRE(unit3_y == int3(0,1,0)); - STATIC_REQUIRE(unit3_z == int3(0,0,1)); - - STATIC_REQUIRE(unit4 == int4(1,1,1,1)); - STATIC_REQUIRE(unit4_x == int4(1,0,0,0)); - STATIC_REQUIRE(unit4_y == int4(0,1,0,0)); - STATIC_REQUIRE(unit4_z == int4(0,0,1,0)); - STATIC_REQUIRE(unit4_w == int4(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})); - - REQUIRE(std::hash{}({1,2,3}) == std::hash{}({1,2,3})); - REQUIRE_FALSE(std::hash{}({1,2,3}) == std::hash{}({3,2,1})); - - REQUIRE(std::hash{}({1,2,3,4}) == std::hash{}({1,2,3,4})); - REQUIRE_FALSE(std::hash{}({1,2,3,4}) == std::hash{}({3,2,1,4})); - } - - SECTION("cast_to") { - constexpr auto v = cast_to(float2{1.5f}); - STATIC_REQUIRE(v == int2(1)); - STATIC_REQUIRE(std::is_same_v); - } - - SECTION("component") { - STATIC_REQUIRE(component(int2{1,2}, 0) == 1); - STATIC_REQUIRE(component(int2{1,2}, 1) == 2); - - STATIC_REQUIRE(component(int2{0,0}, 0, 1) == int2{1,0}); - STATIC_REQUIRE(component(int2{0,0}, 1, 2) == int2{0,2}); - } - - SECTION("angle") { - REQUIRE(angle(float2(2.f,0.f), float2(0.f,1.f)) == Approx(radians(90.f))); - REQUIRE(angle(float2(0.f,3.f), float2(1.f,0.f)) == Approx(radians(90.f))); - REQUIRE(angle(float2(0.5f,0.f), float2(-1.f,0.f)) == Approx(radians(180.f))); - REQUIRE(angle(float2(-0.2f,0.f), float2(1.f,0.f)) == Approx(radians(180.f))); - REQUIRE(angle(float3(0.f,2.f,0.f), float3(0.f,0.f,1.f)) == Approx(radians(90.f))); - REQUIRE(angle(float3(0.f,0.f,3.f), float3(0.f,1.f,0.f)) == Approx(radians(90.f))); - } -} diff --git a/untests/vmath_vec_fun_tests.cpp b/untests/vmath_vec_fun_tests.cpp index 3991383..18584d4 100644 --- a/untests/vmath_vec_fun_tests.cpp +++ b/untests/vmath_vec_fun_tests.cpp @@ -4,7 +4,7 @@ * Copyright (C) 2020, by Matvey Cherevko (blackmatov@gmail.com) ******************************************************************************/ -#include +#include #define CATCH_CONFIG_FAST_COMPILE #include