From 7acd53c085ed9c16a61821774681717ab758676f Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 3 Dec 2020 05:48:25 +0700 Subject: [PATCH 01/10] matrix ctor from diagonal vector --- README.md | 9 ++++--- headers/vmath.hpp/vmath_mat.hpp | 42 +++++++++++++++++++++++---------- untests/vmath_mat_tests.cpp | 3 +++ 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ee16105..12514d4 100644 --- a/README.md +++ b/README.md @@ -186,7 +186,8 @@ public: {0, 1}}; constexpr mat_base() = default; - constexpr explicit mat_base(T v); + constexpr explicit mat_base(T d); + constexpr explicit mat_base(const row_type& d); constexpr mat_base( T m11, T m12, @@ -211,7 +212,8 @@ public: {0, 0, 1}}; constexpr mat_base() = default; - constexpr explicit mat_base(T v); + constexpr explicit mat_base(T d); + constexpr explicit mat_base(const row_type& d); constexpr mat_base( T m11, T m12, T m13, @@ -239,7 +241,8 @@ public: {0, 0, 0, 1}}; constexpr mat_base() = default; - constexpr explicit mat_base(T v); + constexpr explicit mat_base(T d); + constexpr explicit mat_base(const row_type& d); constexpr mat_base( T m11, T m12, T m13, T m14, diff --git a/headers/vmath.hpp/vmath_mat.hpp b/headers/vmath.hpp/vmath_mat.hpp index e843f00..bac8fc2 100644 --- a/headers/vmath.hpp/vmath_mat.hpp +++ b/headers/vmath.hpp/vmath_mat.hpp @@ -26,10 +26,15 @@ namespace vmath_hpp::detail public: constexpr mat_base() = default; - constexpr explicit mat_base(T v) + constexpr explicit mat_base(T d) : rows{ - row_type{v, 0}, - row_type{0, v}} {} + row_type{d, 0}, + row_type{0, d}} {} + + constexpr explicit mat_base(const row_type& d) + : rows{ + row_type{d[0], 0}, + row_type{0, d[1]}} {} constexpr mat_base( T m11, T m12, @@ -67,11 +72,17 @@ namespace vmath_hpp::detail public: constexpr mat_base() = default; - constexpr explicit mat_base(T v) + constexpr explicit mat_base(T d) : rows{ - row_type{v, 0, 0}, - row_type{0, v, 0}, - row_type{0, 0, v}} {} + row_type{d, 0, 0}, + row_type{0, d, 0}, + row_type{0, 0, d}} {} + + constexpr explicit mat_base(const row_type& d) + : rows{ + row_type{d[0], 0, 0}, + row_type{0, d[1], 0}, + row_type{0, 0, d[2]}} {} constexpr mat_base( T m11, T m12, T m13, @@ -115,12 +126,19 @@ namespace vmath_hpp::detail public: constexpr mat_base() = default; - constexpr explicit mat_base(T v) + constexpr explicit mat_base(T d) : rows{ - row_type{v, 0, 0, 0}, - row_type{0, v, 0, 0}, - row_type{0, 0, v, 0}, - row_type{0, 0, 0, v}} {} + row_type{d, 0, 0, 0}, + row_type{0, d, 0, 0}, + row_type{0, 0, d, 0}, + row_type{0, 0, 0, d}} {} + + constexpr explicit mat_base(const row_type& d) + : rows{ + row_type{d[0], 0, 0, 0}, + row_type{0, d[1], 0, 0}, + row_type{0, 0, d[2], 0}, + row_type{0, 0, 0, d[3]}} {} constexpr mat_base( T m11, T m12, T m13, T m14, diff --git a/untests/vmath_mat_tests.cpp b/untests/vmath_mat_tests.cpp index df2ab85..e8830b1 100644 --- a/untests/vmath_mat_tests.cpp +++ b/untests/vmath_mat_tests.cpp @@ -48,6 +48,7 @@ TEST_CASE("vmath/mat") { { STATIC_REQUIRE(int2x2() == int2x2(1,0,0,1)); STATIC_REQUIRE(int2x2(2) == int2x2(2,0,0,2)); + STATIC_REQUIRE(int2x2(int2{2,3}) == int2x2(2,0,0,3)); STATIC_REQUIRE(int2x2(1,2,3,4) == int2x2(1,2,3,4)); STATIC_REQUIRE(int2x2({1,2},{3,4}) == int2x2(1,2,3,4)); STATIC_REQUIRE(int2x2(int2x2({1,2},{3,4})) == int2x2(1,2,3,4)); @@ -56,6 +57,7 @@ TEST_CASE("vmath/mat") { STATIC_REQUIRE(int3x3() == int3x3(1,0,0,0,1,0,0,0,1)); STATIC_REQUIRE(int3x3(2) == int3x3(2,0,0,0,2,0,0,0,2)); + STATIC_REQUIRE(int3x3(int3{2,3,4}) == int3x3(2,0,0,0,3,0,0,0,4)); STATIC_REQUIRE(int3x3(1,2,3,4,5,6,7,8,9) == int3x3(1,2,3,4,5,6,7,8,9)); STATIC_REQUIRE(int3x3({1,2,3},{4,5,6},{7,8,9}) == int3x3(1,2,3,4,5,6,7,8,9)); STATIC_REQUIRE(int3x3(int3x3({1,2,3},{4,5,6},{7,8,9})) == int3x3(1,2,3,4,5,6,7,8,9)); @@ -64,6 +66,7 @@ TEST_CASE("vmath/mat") { STATIC_REQUIRE(int4x4() == int4x4(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)); STATIC_REQUIRE(int4x4(2) == int4x4(2,0,0,0,0,2,0,0,0,0,2,0,0,0,0,2)); + STATIC_REQUIRE(int4x4(int4{2,3,4,5}) == int4x4(2,0,0,0,0,3,0,0,0,0,4,0,0,0,0,5)); STATIC_REQUIRE(int4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) == int4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)); STATIC_REQUIRE(int4x4({1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}) == int4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)); STATIC_REQUIRE(int4x4(int4x4({1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16})) == int4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)); From dc05e543a4f530e6ea851aa210fac04a9e88963d Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 3 Dec 2020 06:11:31 +0700 Subject: [PATCH 02/10] vector and matrix deduction guides --- headers/vmath.hpp/vmath_mat.hpp | 38 ++++++++++++++++++++++++++++ headers/vmath.hpp/vmath_vec.hpp | 44 +++++++++++++++++++++++++++++++++ untests/vmath_mat_tests.cpp | 14 +++++++++++ untests/vmath_vec_tests.cpp | 16 ++++++++++++ 4 files changed, 112 insertions(+) diff --git a/headers/vmath.hpp/vmath_mat.hpp b/headers/vmath.hpp/vmath_mat.hpp index bac8fc2..9fa1acb 100644 --- a/headers/vmath.hpp/vmath_mat.hpp +++ b/headers/vmath.hpp/vmath_mat.hpp @@ -230,6 +230,44 @@ namespace vmath_hpp return rows[index]; } }; +} + +namespace vmath_hpp +{ + // mat2 + + template < typename T > + mat(T, T, T, T) -> mat; + + template < typename T > + mat(const vec&, const vec&) -> mat; + + template < typename T > + mat(std::initializer_list, std::initializer_list) -> mat; + + // mat3 + + template < typename T > + mat(T, T, T, T, T, T, T, T, T) -> mat; + + template < typename T > + mat(const vec&, const vec&, const vec&) -> mat; + + template < typename T > + mat(std::initializer_list, std::initializer_list, std::initializer_list) -> mat; + + // mat4 + + template < typename T > + mat(T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T) -> mat; + + template < typename T > + mat(const vec&, const vec&, const vec&, const vec&) -> mat; + + template < typename T > + mat(std::initializer_list, std::initializer_list, std::initializer_list, std::initializer_list) -> mat; + + // swap template < typename T, std::size_t Size > void swap(mat& l, mat& r) noexcept(noexcept(l.swap(r))) { diff --git a/headers/vmath.hpp/vmath_vec.hpp b/headers/vmath.hpp/vmath_vec.hpp index 3f37281..bfd4346 100644 --- a/headers/vmath.hpp/vmath_vec.hpp +++ b/headers/vmath.hpp/vmath_vec.hpp @@ -189,6 +189,50 @@ namespace vmath_hpp return (*this)[index]; } }; +} + +namespace vmath_hpp +{ + // vec2 + + template < typename T > + vec(T, T) -> vec; + + // vec3 + + template < typename T > + vec(T, T, T) -> vec; + + template < typename T > + vec(const vec&, T) -> vec; + + template < typename T > + vec(T, const vec&) -> vec; + + // vec4 + + template < typename T > + vec(T, T, T, T) -> vec; + + template < typename T > + vec(const vec&, T, T) -> vec; + + template < typename T > + vec(T, const vec&, T) -> vec; + + template < typename T > + vec(T, T, const vec&) -> vec; + + template < typename T > + vec(const vec&, const vec&) -> vec; + + template < typename T > + vec(const vec&, T) -> vec; + + template < typename T > + vec(T, const vec&) -> vec; + + // swap template < typename T, std::size_t Size > void swap(vec& l, vec& r) noexcept(noexcept(l.swap(r))) { diff --git a/untests/vmath_mat_tests.cpp b/untests/vmath_mat_tests.cpp index e8830b1..0f2abf9 100644 --- a/untests/vmath_mat_tests.cpp +++ b/untests/vmath_mat_tests.cpp @@ -24,6 +24,20 @@ TEST_CASE("vmath/mat") { STATIC_REQUIRE(sizeof(int4x4{}) == sizeof(int) * 4 * 4); } + SUBCASE("guides") { + STATIC_REQUIRE(mat{1,2,3,4}.size == 2); + STATIC_REQUIRE(mat{{1,2},{3,4}}.size == 2); + STATIC_REQUIRE(mat{vec{1,2},vec{3,4}}.size == 2); + + STATIC_REQUIRE(mat{1,2,3,4,5,6,7,8,9}.size == 3); + STATIC_REQUIRE(mat{{1,2,3},{4,5,6},{7,8,9}}.size == 3); + STATIC_REQUIRE(mat{vec{1,2,3},vec{4,5,6},vec{7,8,9}}.size == 3); + + STATIC_REQUIRE(mat{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}.size == 4); + STATIC_REQUIRE(mat{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}.size == 4); + STATIC_REQUIRE(mat{vec{1,2,3,4},vec{5,6,7,8},vec{9,10,11,12},vec{13,14,15,16}}.size == 4); + } + SUBCASE("ctors") { { STATIC_REQUIRE(int2x2()[0] == int2(1,0)); diff --git a/untests/vmath_vec_tests.cpp b/untests/vmath_vec_tests.cpp index 4a77442..7c3d2da 100644 --- a/untests/vmath_vec_tests.cpp +++ b/untests/vmath_vec_tests.cpp @@ -24,6 +24,22 @@ TEST_CASE("vmath/vec") { STATIC_REQUIRE(sizeof(int4{}) == sizeof(int) * 4); } + SUBCASE("guides") { + STATIC_REQUIRE(vec{1,2}.size == 2); + + STATIC_REQUIRE(vec{1,2,3}.size == 3); + STATIC_REQUIRE(vec{{1,2},3}.size == 3); + STATIC_REQUIRE(vec{1,{2,3}}.size == 3); + + STATIC_REQUIRE(vec{1,2,3,4}.size == 4); + STATIC_REQUIRE(vec{vec{1,2},3,4}.size == 4); + STATIC_REQUIRE(vec{1,vec{2,3},4}.size == 4); + STATIC_REQUIRE(vec{1,2,vec{3,4}}.size == 4); + STATIC_REQUIRE(vec{vec{1,2},vec{3,4}}.size == 4); + STATIC_REQUIRE(vec{vec{1,2,3},4}.size == 4); + STATIC_REQUIRE(vec{1,vec{2,3,4}}.size == 4); + } + SUBCASE("ctors") { { STATIC_REQUIRE(int2().x == 0); From 6513122fc5979e92ffa1c81e70b6ef2a62e1ec5d Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 3 Dec 2020 06:30:56 +0700 Subject: [PATCH 03/10] rename vector value_type to component_type --- README.md | 10 +++++----- headers/vmath.hpp/vmath_mat_fun.hpp | 12 ++++++------ headers/vmath.hpp/vmath_vec.hpp | 10 +++++----- untests/vmath_ext_tests.cpp | 2 +- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 12514d4..161a82e 100644 --- a/README.md +++ b/README.md @@ -122,13 +122,13 @@ public: template < typename T, size_t Size > class vec final : public vec_base { public: - using value_type = T; + using component_type = T; - using pointer = value_type*; - using const_pointer = const value_type*; + using pointer = component_type*; + using const_pointer = const component_type*; - using reference = value_type&; - using const_reference = const value_type&; + using reference = component_type&; + using const_reference = const component_type&; static constexpr size_t size = Size; diff --git a/headers/vmath.hpp/vmath_mat_fun.hpp b/headers/vmath.hpp/vmath_mat_fun.hpp index cbea454..4b182d1 100644 --- a/headers/vmath.hpp/vmath_mat_fun.hpp +++ b/headers/vmath.hpp/vmath_mat_fun.hpp @@ -21,7 +21,7 @@ namespace vmath_hpp::detail template < typename A, std::size_t Size, typename F, std::size_t... Is > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE auto map_impl(F&& f, const mat& a, std::index_sequence) - -> mat>::value_type, Size> + -> mat>::component_type, Size> { return { f(a[Is])... }; } @@ -29,7 +29,7 @@ namespace vmath_hpp::detail template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE auto zip_impl(F&& f, const mat& a, const mat& b, std::index_sequence) - -> mat, vec>::value_type, Size> + -> mat, vec>::component_type, Size> { return { f(a[Is], b[Is])... }; } @@ -37,7 +37,7 @@ namespace vmath_hpp::detail template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE auto zip_impl(F&& f, const mat& a, const mat& b, const mat& c, std::index_sequence) - -> mat, vec, vec>::value_type, Size> + -> mat, vec, vec>::component_type, Size> { return { f(a[Is], b[Is], c[Is])... }; } @@ -65,7 +65,7 @@ namespace vmath_hpp::detail template < typename A, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE auto map(F&& f, const mat& a) - -> mat>::value_type, Size> + -> mat>::component_type, Size> { return impl::map_impl(std::forward(f), a, std::make_index_sequence{}); } @@ -73,7 +73,7 @@ namespace vmath_hpp::detail template < typename A, typename B, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE auto zip(F&& f, const mat& a, const mat& b) - -> mat, vec>::value_type, Size> + -> mat, vec>::component_type, Size> { return impl::zip_impl(std::forward(f), a, b, std::make_index_sequence{}); } @@ -81,7 +81,7 @@ namespace vmath_hpp::detail template < typename A, typename B, typename C, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE auto zip(F&& f, const mat& a, const mat& b, const mat& c) - -> mat, vec, vec>::value_type, Size> + -> mat, vec, vec>::component_type, Size> { return impl::zip_impl(std::forward(f), a, b, c, std::make_index_sequence{}); } diff --git a/headers/vmath.hpp/vmath_vec.hpp b/headers/vmath.hpp/vmath_vec.hpp index bfd4346..f15d325 100644 --- a/headers/vmath.hpp/vmath_vec.hpp +++ b/headers/vmath.hpp/vmath_vec.hpp @@ -151,13 +151,13 @@ namespace vmath_hpp using self_type = vec; using base_type = detail::vec_base; public: - using value_type = T; + using component_type = T; - using pointer = value_type*; - using const_pointer = const value_type*; + using pointer = component_type*; + using const_pointer = const component_type*; - using reference = value_type&; - using const_reference = const value_type&; + using reference = component_type&; + using const_reference = const component_type&; static constexpr std::size_t size = Size; public: diff --git a/untests/vmath_ext_tests.cpp b/untests/vmath_ext_tests.cpp index 00e7c24..abee61b 100644 --- a/untests/vmath_ext_tests.cpp +++ b/untests/vmath_ext_tests.cpp @@ -101,7 +101,7 @@ TEST_CASE("vmath/ext") { { constexpr auto v = cast_to(float2{1.5f}); STATIC_REQUIRE(v == int2(1)); - STATIC_REQUIRE(std::is_same_v); + STATIC_REQUIRE(std::is_same_v); } { constexpr auto m = cast_to(float2x2{1.5f}); From 76174171f37b0f6b0354b2830cf0c2724fbf278f Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 3 Dec 2020 06:34:28 +0700 Subject: [PATCH 04/10] data function for vector and matrix --- README.md | 6 ++++++ headers/vmath.hpp/vmath_mat.hpp | 10 +++++++++- headers/vmath.hpp/vmath_vec.hpp | 8 ++++++++ untests/vmath_mat_tests.cpp | 23 +++++++++++++++++++++++ untests/vmath_vec_tests.cpp | 23 +++++++++++++++++++++++ 5 files changed, 69 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 161a82e..c814c80 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,9 @@ public: void swap(vec& other) noexcept(is_nothrow_swappable_v); + constexpr pointer data() noexcept; + constexpr const_pointer data() const noexcept; + constexpr reference at(size_t index); constexpr const_reference at(size_t index) const; @@ -275,6 +278,9 @@ public: void swap(mat& other) noexcept(is_nothrow_swappable_v); + constexpr pointer data() noexcept; + constexpr const_pointer data() const noexcept; + constexpr reference at(size_t index); constexpr const_reference at(size_t index) const; diff --git a/headers/vmath.hpp/vmath_mat.hpp b/headers/vmath.hpp/vmath_mat.hpp index 9fa1acb..11471ae 100644 --- a/headers/vmath.hpp/vmath_mat.hpp +++ b/headers/vmath.hpp/vmath_mat.hpp @@ -204,10 +204,18 @@ namespace vmath_hpp void swap(mat& other) noexcept(std::is_nothrow_swappable_v) { for ( std::size_t i = 0; i < Size; ++i ) { using std::swap; - swap((*this)[i], other[i]); + swap(rows[i], other.rows[i]); } } + [[nodiscard]] constexpr pointer data() noexcept { + return &rows[0]; + } + + [[nodiscard]] constexpr const_pointer data() const noexcept { + return &rows[0]; + } + [[nodiscard]] constexpr reference operator[](std::size_t index) noexcept { return rows[index]; } diff --git a/headers/vmath.hpp/vmath_vec.hpp b/headers/vmath.hpp/vmath_vec.hpp index f15d325..dcb2874 100644 --- a/headers/vmath.hpp/vmath_vec.hpp +++ b/headers/vmath.hpp/vmath_vec.hpp @@ -175,6 +175,14 @@ namespace vmath_hpp } } + [[nodiscard]] constexpr pointer data() noexcept { + return &(*this)[0]; + } + + [[nodiscard]] constexpr const_pointer data() const noexcept { + return &(*this)[0]; + } + [[nodiscard]] constexpr reference at(std::size_t index) { if ( index >= Size ) { throw std::out_of_range("vec::at"); diff --git a/untests/vmath_mat_tests.cpp b/untests/vmath_mat_tests.cpp index 0f2abf9..7f7c3bb 100644 --- a/untests/vmath_mat_tests.cpp +++ b/untests/vmath_mat_tests.cpp @@ -121,6 +121,29 @@ TEST_CASE("vmath/mat") { } } + SUBCASE("data") { + { + int2x2 m2; + STATIC_REQUIRE(m2.data() == &m2[0]); + + int3x3 m3; + STATIC_REQUIRE(m3.data() == &m3[0]); + + int4x4 m4; + STATIC_REQUIRE(m4.data() == &m4[0]); + } + { + constexpr int2x2 m2; + STATIC_REQUIRE(m2.data() == &m2[0]); + + constexpr int3x3 m3; + STATIC_REQUIRE(m3.data() == &m3[0]); + + constexpr int4x4 m4; + STATIC_REQUIRE(m4.data() == &m4[0]); + } + } + SUBCASE("operator[]") { { STATIC_REQUIRE(int2x2()[0] == int2(1,0)); diff --git a/untests/vmath_vec_tests.cpp b/untests/vmath_vec_tests.cpp index 7c3d2da..71c41ae 100644 --- a/untests/vmath_vec_tests.cpp +++ b/untests/vmath_vec_tests.cpp @@ -118,6 +118,29 @@ TEST_CASE("vmath/vec") { } } + SUBCASE("data") { + { + int2 i2; + REQUIRE(i2.data() == &i2[0]); + + int3 i3; + REQUIRE(i3.data() == &i3[0]); + + int4 i4; + REQUIRE(i4.data() == &i4[0]); + } + { + constexpr int2 i2; + STATIC_REQUIRE(i2.data() == &i2[0]); + + constexpr int3 i3; + STATIC_REQUIRE(i3.data() == &i3[0]); + + constexpr int4 i4; + STATIC_REQUIRE(i4.data() == &i4[0]); + } + } + SUBCASE("operator[]") { { STATIC_REQUIRE(int2(1,2).x == 1); From a8bab6598898ed418169b01fcb8765f25722d2dd Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 3 Dec 2020 07:06:48 +0700 Subject: [PATCH 05/10] vector and matrix iterators --- headers/vmath.hpp/vmath_fwd.hpp | 2 + headers/vmath.hpp/vmath_mat.hpp | 24 ++++++++- headers/vmath.hpp/vmath_vec.hpp | 24 ++++++++- untests/vmath_mat_tests.cpp | 88 +++++++++++++++++++++++++++++---- untests/vmath_vec_tests.cpp | 82 +++++++++++++++++++++++++++--- 5 files changed, 201 insertions(+), 19 deletions(-) diff --git a/headers/vmath.hpp/vmath_fwd.hpp b/headers/vmath.hpp/vmath_fwd.hpp index 3dfcf6d..2b330ec 100644 --- a/headers/vmath.hpp/vmath_fwd.hpp +++ b/headers/vmath.hpp/vmath_fwd.hpp @@ -9,6 +9,8 @@ #include #include +#include +#include #include #include #include diff --git a/headers/vmath.hpp/vmath_mat.hpp b/headers/vmath.hpp/vmath_mat.hpp index 11471ae..65d8b4c 100644 --- a/headers/vmath.hpp/vmath_mat.hpp +++ b/headers/vmath.hpp/vmath_mat.hpp @@ -192,6 +192,11 @@ namespace vmath_hpp using reference = row_type&; using const_reference = const row_type&; + using iterator = pointer; + using const_iterator = const_pointer; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + static constexpr std::size_t size = Size; public: using base_type::mat_base; @@ -208,11 +213,26 @@ namespace vmath_hpp } } - [[nodiscard]] constexpr pointer data() noexcept { + [[nodiscard]] iterator begin() noexcept { return iterator(data()); } + [[nodiscard]] const_iterator begin() const noexcept { return const_iterator(data()); } + [[nodiscard]] iterator end() noexcept { return iterator(data() + Size); } + [[nodiscard]] const_iterator end() const noexcept { return const_iterator(data() + Size); } + + [[nodiscard]] reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } + [[nodiscard]] const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } + [[nodiscard]] reverse_iterator rend() noexcept { return reverse_iterator(begin()); } + [[nodiscard]] const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } + + [[nodiscard]] const_iterator cbegin() const noexcept { return begin(); } + [[nodiscard]] const_iterator cend() const noexcept { return end(); } + [[nodiscard]] const_reverse_iterator crbegin() const noexcept { return rbegin(); } + [[nodiscard]] const_reverse_iterator crend() const noexcept { return rend(); } + + [[nodiscard]] pointer data() noexcept { return &rows[0]; } - [[nodiscard]] constexpr const_pointer data() const noexcept { + [[nodiscard]] const_pointer data() const noexcept { return &rows[0]; } diff --git a/headers/vmath.hpp/vmath_vec.hpp b/headers/vmath.hpp/vmath_vec.hpp index dcb2874..66bb0f5 100644 --- a/headers/vmath.hpp/vmath_vec.hpp +++ b/headers/vmath.hpp/vmath_vec.hpp @@ -159,6 +159,11 @@ namespace vmath_hpp using reference = component_type&; using const_reference = const component_type&; + using iterator = pointer; + using const_iterator = const_pointer; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + static constexpr std::size_t size = Size; public: using base_type::vec_base; @@ -175,11 +180,26 @@ namespace vmath_hpp } } - [[nodiscard]] constexpr pointer data() noexcept { + [[nodiscard]] iterator begin() noexcept { return iterator(data()); } + [[nodiscard]] const_iterator begin() const noexcept { return const_iterator(data()); } + [[nodiscard]] iterator end() noexcept { return iterator(data() + Size); } + [[nodiscard]] const_iterator end() const noexcept { return const_iterator(data() + Size); } + + [[nodiscard]] reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } + [[nodiscard]] const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } + [[nodiscard]] reverse_iterator rend() noexcept { return reverse_iterator(begin()); } + [[nodiscard]] const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } + + [[nodiscard]] const_iterator cbegin() const noexcept { return begin(); } + [[nodiscard]] const_iterator cend() const noexcept { return end(); } + [[nodiscard]] const_reverse_iterator crbegin() const noexcept { return rbegin(); } + [[nodiscard]] const_reverse_iterator crend() const noexcept { return rend(); } + + [[nodiscard]] pointer data() noexcept { return &(*this)[0]; } - [[nodiscard]] constexpr const_pointer data() const noexcept { + [[nodiscard]] const_pointer data() const noexcept { return &(*this)[0]; } diff --git a/untests/vmath_mat_tests.cpp b/untests/vmath_mat_tests.cpp index 7f7c3bb..97688d1 100644 --- a/untests/vmath_mat_tests.cpp +++ b/untests/vmath_mat_tests.cpp @@ -121,26 +121,96 @@ TEST_CASE("vmath/mat") { } } + SUBCASE("iter") { + { + int2x2 m{1,2,3,4}; + + REQUIRE(*m.begin() == vec{1,2}); + REQUIRE(*(m.begin() + 1) == vec{3,4}); + REQUIRE(*(m.end() - 1) == vec{3,4}); + REQUIRE(*(m.end() - 2) == vec{1,2}); + REQUIRE(m.begin() + 2 == m.end()); + REQUIRE(m.end() - 2 == m.begin()); + + REQUIRE(*m.cbegin() == vec{1,2}); + REQUIRE(*(m.cbegin() + 1) == vec{3,4}); + REQUIRE(*(m.cend() - 1) == vec{3,4}); + REQUIRE(*(m.cend() - 2) == vec{1,2}); + REQUIRE(m.cbegin() + 2 == m.cend()); + REQUIRE(m.cend() - 2 == m.cbegin()); + + REQUIRE(*m.rbegin() == vec{3,4}); + REQUIRE(*(m.rbegin() + 1) == vec{1,2}); + REQUIRE(*(m.rend() - 1) == vec{1,2}); + REQUIRE(*(m.rend() - 2) == vec{3,4}); + REQUIRE(m.rbegin() + 2 == m.rend()); + REQUIRE(m.rend() - 2 == m.rbegin()); + + REQUIRE(*m.crbegin() == vec{3,4}); + REQUIRE(*(m.crbegin() + 1) == vec{1,2}); + REQUIRE(*(m.crend() - 1) == vec{1,2}); + REQUIRE(*(m.crend() - 2) == vec{3,4}); + REQUIRE(m.crbegin() + 2 == m.crend()); + REQUIRE(m.crend() - 2 == m.crbegin()); + + *m.begin() = {5,6}; + REQUIRE(m == int2x2{5,6,3,4}); + *m.rbegin() = {7,8}; + REQUIRE(m == int2x2{5,6,7,8}); + } + { + const int2x2 m{1,2,3,4}; + + REQUIRE(*m.begin() == vec{1,2}); + REQUIRE(*(m.begin() + 1) == vec{3,4}); + REQUIRE(*(m.end() - 1) == vec{3,4}); + REQUIRE(*(m.end() - 2) == vec{1,2}); + REQUIRE(m.begin() + 2 == m.end()); + REQUIRE(m.end() - 2 == m.begin()); + + REQUIRE(*m.cbegin() == vec{1,2}); + REQUIRE(*(m.cbegin() + 1) == vec{3,4}); + REQUIRE(*(m.cend() - 1) == vec{3,4}); + REQUIRE(*(m.cend() - 2) == vec{1,2}); + REQUIRE(m.cbegin() + 2 == m.cend()); + REQUIRE(m.cend() - 2 == m.cbegin()); + + REQUIRE(*m.rbegin() == vec{3,4}); + REQUIRE(*(m.rbegin() + 1) == vec{1,2}); + REQUIRE(*(m.rend() - 1) == vec{1,2}); + REQUIRE(*(m.rend() - 2) == vec{3,4}); + REQUIRE(m.rbegin() + 2 == m.rend()); + REQUIRE(m.rend() - 2 == m.rbegin()); + + REQUIRE(*m.crbegin() == vec{3,4}); + REQUIRE(*(m.crbegin() + 1) == vec{1,2}); + REQUIRE(*(m.crend() - 1) == vec{1,2}); + REQUIRE(*(m.crend() - 2) == vec{3,4}); + REQUIRE(m.crbegin() + 2 == m.crend()); + REQUIRE(m.crend() - 2 == m.crbegin()); + } + } + SUBCASE("data") { { int2x2 m2; - STATIC_REQUIRE(m2.data() == &m2[0]); + REQUIRE(m2.data() == &m2[0]); int3x3 m3; - STATIC_REQUIRE(m3.data() == &m3[0]); + REQUIRE(m3.data() == &m3[0]); int4x4 m4; - STATIC_REQUIRE(m4.data() == &m4[0]); + REQUIRE(m4.data() == &m4[0]); } { - constexpr int2x2 m2; - STATIC_REQUIRE(m2.data() == &m2[0]); + const int2x2 m2; + REQUIRE(m2.data() == &m2[0]); - constexpr int3x3 m3; - STATIC_REQUIRE(m3.data() == &m3[0]); + const int3x3 m3; + REQUIRE(m3.data() == &m3[0]); - constexpr int4x4 m4; - STATIC_REQUIRE(m4.data() == &m4[0]); + const int4x4 m4; + REQUIRE(m4.data() == &m4[0]); } } diff --git a/untests/vmath_vec_tests.cpp b/untests/vmath_vec_tests.cpp index 71c41ae..39e3a38 100644 --- a/untests/vmath_vec_tests.cpp +++ b/untests/vmath_vec_tests.cpp @@ -118,6 +118,76 @@ TEST_CASE("vmath/vec") { } } + SUBCASE("iter") { + { + int2 v{1,2}; + + REQUIRE(*v.begin() == 1); + REQUIRE(*(v.begin() + 1) == 2); + REQUIRE(*(v.end() - 1) == 2); + REQUIRE(*(v.end() - 2) == 1); + REQUIRE(v.begin() + 2 == v.end()); + REQUIRE(v.end() - 2 == v.begin()); + + REQUIRE(*v.cbegin() == 1); + REQUIRE(*(v.cbegin() + 1) == 2); + REQUIRE(*(v.cend() - 1) == 2); + REQUIRE(*(v.cend() - 2) == 1); + REQUIRE(v.cbegin() + 2 == v.cend()); + REQUIRE(v.cend() - 2 == v.cbegin()); + + REQUIRE(*v.rbegin() == 2); + REQUIRE(*(v.rbegin() + 1) == 1); + REQUIRE(*(v.rend() - 1) == 1); + REQUIRE(*(v.rend() - 2) == 2); + REQUIRE(v.rbegin() + 2 == v.rend()); + REQUIRE(v.rend() - 2 == v.rbegin()); + + REQUIRE(*v.crbegin() == 2); + REQUIRE(*(v.crbegin() + 1) == 1); + REQUIRE(*(v.crend() - 1) == 1); + REQUIRE(*(v.crend() - 2) == 2); + REQUIRE(v.crbegin() + 2 == v.crend()); + REQUIRE(v.crend() - 2 == v.crbegin()); + + *v.begin() = 3; + REQUIRE(v == int2{3,2}); + *v.rbegin() = 4; + REQUIRE(v == int2{3,4}); + } + { + const int2 v{1,2}; + + REQUIRE(*v.begin() == 1); + REQUIRE(*(v.begin() + 1) == 2); + REQUIRE(*(v.end() - 1) == 2); + REQUIRE(*(v.end() - 2) == 1); + REQUIRE(v.begin() + 2 == v.end()); + REQUIRE(v.end() - 2 == v.begin()); + + REQUIRE(*v.cbegin() == 1); + REQUIRE(*(v.cbegin() + 1) == 2); + REQUIRE(*(v.cend() - 1) == 2); + REQUIRE(*(v.cend() - 2) == 1); + REQUIRE(v.cbegin() + 2 == v.cend()); + REQUIRE(v.cend() - 2 == v.cbegin()); + + REQUIRE(*v.rbegin() == 2); + REQUIRE(*(v.rbegin() + 1) == 1); + REQUIRE(*(v.rend() - 1) == 1); + REQUIRE(*(v.rend() - 2) == 2); + REQUIRE(v.rbegin() + 2 == v.rend()); + REQUIRE(v.rend() - 2 == v.rbegin()); + + REQUIRE(*v.crbegin() == 2); + REQUIRE(*(v.crbegin() + 1) == 1); + REQUIRE(*(v.crend() - 1) == 1); + REQUIRE(*(v.crend() - 2) == 2); + REQUIRE(v.crbegin() + 2 == v.crend()); + REQUIRE(v.crend() - 2 == v.crbegin()); + } + } + SUBCASE("data") { { int2 i2; @@ -130,14 +200,14 @@ TEST_CASE("vmath/vec") { REQUIRE(i4.data() == &i4[0]); } { - constexpr int2 i2; - STATIC_REQUIRE(i2.data() == &i2[0]); + const int2 i2; + REQUIRE(i2.data() == &i2[0]); - constexpr int3 i3; - STATIC_REQUIRE(i3.data() == &i3[0]); + const int3 i3; + REQUIRE(i3.data() == &i3[0]); - constexpr int4 i4; - STATIC_REQUIRE(i4.data() == &i4[0]); + const int4 i4; + REQUIRE(i4.data() == &i4[0]); } } From 617ffab8396db717077b70b6fb42d36c6cc673f0 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 3 Dec 2020 07:12:18 +0700 Subject: [PATCH 06/10] update README API --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c814c80..aeaf61c 100644 --- a/README.md +++ b/README.md @@ -130,12 +130,32 @@ public: using reference = component_type&; using const_reference = const component_type&; + using iterator = pointer; + using const_iterator = const_pointer; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + static constexpr size_t size = Size; void swap(vec& other) noexcept(is_nothrow_swappable_v); - constexpr pointer data() noexcept; - constexpr const_pointer data() const noexcept; + iterator begin() noexcept; + const_iterator begin() const noexcept; + iterator end() noexcept; + const_iterator end() const noexcept; + + reverse_iterator rbegin() noexcept; + const_reverse_iterator rbegin() const noexcept; + reverse_iterator rend() noexcept; + const_reverse_iterator rend() const noexcept; + + const_iterator cbegin() const noexcept; + const_iterator cend() const noexcept; + const_reverse_iterator crbegin() const noexcept; + const_reverse_iterator crend() const noexcept; + + pointer data() noexcept; + const_pointer data() const noexcept; constexpr reference at(size_t index); constexpr const_reference at(size_t index) const; @@ -274,12 +294,32 @@ public: using reference = row_type&; using const_reference = const row_type&; + using iterator = pointer; + using const_iterator = const_pointer; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + static constexpr size_t size = Size; void swap(mat& other) noexcept(is_nothrow_swappable_v); - constexpr pointer data() noexcept; - constexpr const_pointer data() const noexcept; + iterator begin() noexcept; + const_iterator begin() const noexcept; + iterator end() noexcept; + const_iterator end() const noexcept; + + reverse_iterator rbegin() noexcept; + const_reverse_iterator rbegin() const noexcept; + reverse_iterator rend() noexcept; + const_reverse_iterator rend() const noexcept; + + const_iterator cbegin() const noexcept; + const_iterator cend() const noexcept; + const_reverse_iterator crbegin() const noexcept; + const_reverse_iterator crend() const noexcept; + + pointer data() noexcept; + const_pointer data() const noexcept; constexpr reference at(size_t index); constexpr const_reference at(size_t index) const; From 30cc68b352d957288847d2cb2f57636940d3d73b Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 3 Dec 2020 22:02:47 +0700 Subject: [PATCH 07/10] style fix --- headers/vmath.hpp/vmath_mat_fun.hpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/headers/vmath.hpp/vmath_mat_fun.hpp b/headers/vmath.hpp/vmath_mat_fun.hpp index 4b182d1..12c250b 100644 --- a/headers/vmath.hpp/vmath_mat_fun.hpp +++ b/headers/vmath.hpp/vmath_mat_fun.hpp @@ -191,8 +191,8 @@ namespace vmath_hpp template < typename T > [[nodiscard]] constexpr vec operator*(const vec& xs, const mat& ys) { return { - xs.x * ys[0][0] + xs.y * ys[1][0], - xs.x * ys[0][1] + xs.y * ys[1][1]}; + xs[0] * ys[0][0] + xs[1] * ys[1][0], + xs[0] * ys[0][1] + xs[1] * ys[1][1]}; } template < typename T > @@ -208,9 +208,9 @@ namespace vmath_hpp template < typename T > [[nodiscard]] constexpr vec operator*(const vec& xs, const mat& ys) { return { - xs.x * ys[0][0] + xs.y * ys[1][0] + xs.z * ys[2][0], - xs.x * ys[0][1] + xs.y * ys[1][1] + xs.z * ys[2][1], - xs.x * ys[0][2] + xs.y * ys[1][2] + xs.z * ys[2][2]}; + xs[0] * ys[0][0] + xs[1] * ys[1][0] + xs[2] * ys[2][0], + xs[0] * ys[0][1] + xs[1] * ys[1][1] + xs[2] * ys[2][1], + xs[0] * ys[0][2] + xs[1] * ys[1][2] + xs[2] * ys[2][2]}; } template < typename T > @@ -232,10 +232,10 @@ namespace vmath_hpp template < typename T > [[nodiscard]] constexpr vec operator*(const vec& xs, const mat& ys) { return { - xs.x * ys[0][0] + xs.y * ys[1][0] + xs.z * ys[2][0] + xs.w * ys[3][0], - xs.x * ys[0][1] + xs.y * ys[1][1] + xs.z * ys[2][1] + xs.w * ys[3][1], - xs.x * ys[0][2] + xs.y * ys[1][2] + xs.z * ys[2][2] + xs.w * ys[3][2], - xs.x * ys[0][3] + xs.y * ys[1][3] + xs.z * ys[2][3] + xs.w * ys[3][3]}; + xs[0] * ys[0][0] + xs[1] * ys[1][0] + xs[2] * ys[2][0] + xs[3] * ys[3][0], + xs[0] * ys[0][1] + xs[1] * ys[1][1] + xs[2] * ys[2][1] + xs[3] * ys[3][1], + xs[0] * ys[0][2] + xs[1] * ys[1][2] + xs[2] * ys[2][2] + xs[3] * ys[3][2], + xs[0] * ys[0][3] + xs[1] * ys[1][3] + xs[2] * ys[2][3] + xs[3] * ys[3][3]}; } template < typename T > From a2b8f76a9cbd38853ebe11f849e4adb123c76cb6 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 4 Dec 2020 00:59:27 +0700 Subject: [PATCH 08/10] generalize vector and matrix mul --- headers/vmath.hpp/vmath_mat_fun.hpp | 92 +++++++---------------------- 1 file changed, 22 insertions(+), 70 deletions(-) diff --git a/headers/vmath.hpp/vmath_mat_fun.hpp b/headers/vmath.hpp/vmath_mat_fun.hpp index 12c250b..d3931c3 100644 --- a/headers/vmath.hpp/vmath_mat_fun.hpp +++ b/headers/vmath.hpp/vmath_mat_fun.hpp @@ -48,6 +48,12 @@ namespace vmath_hpp::detail return ((init = f(std::move(init), b[Is])), ...); } + template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + A fold_impl(F&& f, A init, const vec& b, const mat& c, std::index_sequence) { + return ((init = f(std::move(init), b[Is], c[Is])), ...); + } + template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE A fold_impl(F&& f, A init, const mat& b, const mat& c, std::index_sequence) { @@ -92,6 +98,12 @@ namespace vmath_hpp::detail return impl::fold_impl(std::forward(f), std::move(init), b, std::make_index_sequence{}); } + template < typename A, typename B, typename C, std::size_t Size, typename F > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + A fold(F&& f, A init, const vec& b, const mat& c) { + return impl::fold_impl(std::forward(f), std::move(init), b, c, std::make_index_sequence{}); + } + template < typename A, typename B, typename C, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE A fold(F&& f, A init, const mat& b, const mat& c) { @@ -188,78 +200,18 @@ namespace vmath_hpp return map([x](const vec& y){ return x * y; }, ys); } - template < typename T > - [[nodiscard]] constexpr vec operator*(const vec& xs, const mat& ys) { - return { - xs[0] * ys[0][0] + xs[1] * ys[1][0], - xs[0] * ys[0][1] + xs[1] * ys[1][1]}; + template < typename T, std::size_t Size > + [[nodiscard]] constexpr vec operator*(const vec& xs, const mat& ys) { + return fold([](const vec& acc, T x, const vec& y){ + return acc + x * y; + }, vec{}, xs, ys); } - template < typename T > - [[nodiscard]] constexpr mat operator*(const mat& xs, const mat& ys) { - return { - xs[0][0] * ys[0][0] + xs[0][1] * ys[1][0], - xs[0][0] * ys[0][1] + xs[0][1] * ys[1][1], - - xs[1][0] * ys[0][0] + xs[1][1] * ys[1][0], - xs[1][0] * ys[0][1] + xs[1][1] * ys[1][1]}; - } - - template < typename T > - [[nodiscard]] constexpr vec operator*(const vec& xs, const mat& ys) { - return { - xs[0] * ys[0][0] + xs[1] * ys[1][0] + xs[2] * ys[2][0], - xs[0] * ys[0][1] + xs[1] * ys[1][1] + xs[2] * ys[2][1], - xs[0] * ys[0][2] + xs[1] * ys[1][2] + xs[2] * ys[2][2]}; - } - - template < typename T > - [[nodiscard]] constexpr mat operator*(const mat& xs, const mat& ys) { - return { - xs[0][0] * ys[0][0] + xs[0][1] * ys[1][0] + xs[0][2] * ys[2][0], - xs[0][0] * ys[0][1] + xs[0][1] * ys[1][1] + xs[0][2] * ys[2][1], - xs[0][0] * ys[0][2] + xs[0][1] * ys[1][2] + xs[0][2] * ys[2][2], - - xs[1][0] * ys[0][0] + xs[1][1] * ys[1][0] + xs[1][2] * ys[2][0], - xs[1][0] * ys[0][1] + xs[1][1] * ys[1][1] + xs[1][2] * ys[2][1], - xs[1][0] * ys[0][2] + xs[1][1] * ys[1][2] + xs[1][2] * ys[2][2], - - xs[2][0] * ys[0][0] + xs[2][1] * ys[1][0] + xs[2][2] * ys[2][0], - xs[2][0] * ys[0][1] + xs[2][1] * ys[1][1] + xs[2][2] * ys[2][1], - xs[2][0] * ys[0][2] + xs[2][1] * ys[1][2] + xs[2][2] * ys[2][2]}; - } - - template < typename T > - [[nodiscard]] constexpr vec operator*(const vec& xs, const mat& ys) { - return { - xs[0] * ys[0][0] + xs[1] * ys[1][0] + xs[2] * ys[2][0] + xs[3] * ys[3][0], - xs[0] * ys[0][1] + xs[1] * ys[1][1] + xs[2] * ys[2][1] + xs[3] * ys[3][1], - xs[0] * ys[0][2] + xs[1] * ys[1][2] + xs[2] * ys[2][2] + xs[3] * ys[3][2], - xs[0] * ys[0][3] + xs[1] * ys[1][3] + xs[2] * ys[2][3] + xs[3] * ys[3][3]}; - } - - template < typename T > - [[nodiscard]] constexpr mat operator*(const mat& xs, const mat& ys) { - return { - xs[0][0] * ys[0][0] + xs[0][1] * ys[1][0] + xs[0][2] * ys[2][0] + xs[0][3] * ys[3][0], - xs[0][0] * ys[0][1] + xs[0][1] * ys[1][1] + xs[0][2] * ys[2][1] + xs[0][3] * ys[3][1], - xs[0][0] * ys[0][2] + xs[0][1] * ys[1][2] + xs[0][2] * ys[2][2] + xs[0][3] * ys[3][2], - xs[0][0] * ys[0][3] + xs[0][1] * ys[1][3] + xs[0][2] * ys[2][3] + xs[0][3] * ys[3][3], - - xs[1][0] * ys[0][0] + xs[1][1] * ys[1][0] + xs[1][2] * ys[2][0] + xs[1][3] * ys[3][0], - xs[1][0] * ys[0][1] + xs[1][1] * ys[1][1] + xs[1][2] * ys[2][1] + xs[1][3] * ys[3][1], - xs[1][0] * ys[0][2] + xs[1][1] * ys[1][2] + xs[1][2] * ys[2][2] + xs[1][3] * ys[3][2], - xs[1][0] * ys[0][3] + xs[1][1] * ys[1][3] + xs[1][2] * ys[2][3] + xs[1][3] * ys[3][3], - - xs[2][0] * ys[0][0] + xs[2][1] * ys[1][0] + xs[2][2] * ys[2][0] + xs[2][3] * ys[3][0], - xs[2][0] * ys[0][1] + xs[2][1] * ys[1][1] + xs[2][2] * ys[2][1] + xs[2][3] * ys[3][1], - xs[2][0] * ys[0][2] + xs[2][1] * ys[1][2] + xs[2][2] * ys[2][2] + xs[2][3] * ys[3][2], - xs[2][0] * ys[0][3] + xs[2][1] * ys[1][3] + xs[2][2] * ys[2][3] + xs[2][3] * ys[3][3], - - xs[3][0] * ys[0][0] + xs[3][1] * ys[1][0] + xs[3][2] * ys[2][0] + xs[3][3] * ys[3][0], - xs[3][0] * ys[0][1] + xs[3][1] * ys[1][1] + xs[3][2] * ys[2][1] + xs[3][3] * ys[3][1], - xs[3][0] * ys[0][2] + xs[3][1] * ys[1][2] + xs[3][2] * ys[2][2] + xs[3][3] * ys[3][2], - xs[3][0] * ys[0][3] + xs[3][1] * ys[1][3] + xs[3][2] * ys[2][3] + xs[3][3] * ys[3][3]}; + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator*(const mat& xs, const mat& ys) { + return map([&ys](const vec& x){ + return x * ys; + }, xs); } // operator*= From eac4ad543748c857d278065c6fb2c3ef88d26300 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 4 Dec 2020 22:57:03 +0700 Subject: [PATCH 09/10] detail funcs renaming --- headers/vmath.hpp/vmath_ext.hpp | 8 +- headers/vmath.hpp/vmath_mat_fun.hpp | 245 +++++++++++++------- headers/vmath.hpp/vmath_vec_fun.hpp | 341 ++++++++++++++++------------ untests/vmath_mat_fun_tests.cpp | 12 +- untests/vmath_vec_fun_tests.cpp | 12 +- 5 files changed, 372 insertions(+), 246 deletions(-) diff --git a/headers/vmath.hpp/vmath_ext.hpp b/headers/vmath.hpp/vmath_ext.hpp index f9ccfa3..d150895 100644 --- a/headers/vmath.hpp/vmath_ext.hpp +++ b/headers/vmath.hpp/vmath_ext.hpp @@ -66,12 +66,12 @@ namespace vmath_hpp::detail template < typename T, size_t Size > [[nodiscard]] std::size_t hash(const vec& v) noexcept { - return fold(hash_combiner{}, std::size_t{}, v); + return fold_join(hash_combiner{}, std::size_t{}, v); } template < typename T, size_t Size > [[nodiscard]] std::size_t hash(const mat& m) noexcept { - return fold(hash_combiner{}, std::size_t{}, m); + return fold_join(hash_combiner{}, std::size_t{}, m); } } @@ -108,12 +108,12 @@ namespace vmath_hpp template < typename To, typename From, std::size_t Size > [[nodiscard]] constexpr vec cast_to(const vec& v) { - return detail::map([](From x){ return cast_to(x); }, v); + return detail::map_join([](From x){ return cast_to(x); }, v); } template < typename To, typename From, std::size_t Size > [[nodiscard]] constexpr mat cast_to(const mat& m) { - return detail::map([](const vec& v){ return cast_to(v); }, m); + return detail::map_join([](const vec& v){ return cast_to(v); }, m); } } diff --git a/headers/vmath.hpp/vmath_mat_fun.hpp b/headers/vmath.hpp/vmath_mat_fun.hpp index d3931c3..5960e2e 100644 --- a/headers/vmath.hpp/vmath_mat_fun.hpp +++ b/headers/vmath.hpp/vmath_mat_fun.hpp @@ -14,106 +14,175 @@ #include "vmath_vec.hpp" #include "vmath_vec_fun.hpp" +namespace vmath_hpp::detail::impl +{ + template < typename A, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto map_join_impl( + F&& f, + const mat& a, + std::index_sequence + ) -> mat>()))::component_type, Size> + { + return { f(a[Is])... }; + } + + template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto map_join_impl( + F&& f, + const mat& a, + const mat& b, + std::index_sequence + ) -> mat>(), + std::declval>()))::component_type, Size> + { + return { f(a[Is], b[Is])... }; + } + + template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto map_join_impl( + F&& f, + const mat& a, + const mat& b, + const mat& c, + std::index_sequence + ) -> mat>(), + std::declval>(), + std::declval>()))::component_type, Size> + { + return { f(a[Is], b[Is], c[Is])... }; + } + + template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold_join_impl( + F&& f, + A init, + const mat& b, + std::index_sequence + ) -> A { + return ((init = f(std::move(init), b[Is])), ...); + } + + template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold_join_impl( + F&& f, + A init, + const vec& b, + const mat& c, + std::index_sequence + ) -> A { + return ((init = f(std::move(init), b[Is], c[Is])), ...); + } + + template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold_join_impl( + F&& f, + A init, + const mat& b, + const mat& c, + std::index_sequence + ) -> A { + return ((init = f(std::move(init), b[Is], c[Is])), ...); + } + + template < typename A, std::size_t Size, typename F, std::size_t I, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_join_impl( + F&& f, + const mat& a, + std::index_sequence + ) -> vec { + vec init = a[I]; + return ((init = f(std::move(init), a[Is])), ...); + } +} + namespace vmath_hpp::detail { - namespace impl - { - template < typename A, std::size_t Size, typename F, std::size_t... Is > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto map_impl(F&& f, const mat& a, std::index_sequence) - -> mat>::component_type, Size> - { - return { f(a[Is])... }; - } - - template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto zip_impl(F&& f, const mat& a, const mat& b, std::index_sequence) - -> mat, vec>::component_type, Size> - { - return { f(a[Is], b[Is])... }; - } - - template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto zip_impl(F&& f, const mat& a, const mat& b, const mat& c, std::index_sequence) - -> mat, vec, vec>::component_type, Size> - { - return { f(a[Is], b[Is], c[Is])... }; - } - - template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold_impl(F&& f, A init, const mat& b, std::index_sequence) { - return ((init = f(std::move(init), b[Is])), ...); - } - - template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold_impl(F&& f, A init, const vec& b, const mat& c, std::index_sequence) { - return ((init = f(std::move(init), b[Is], c[Is])), ...); - } - - template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold_impl(F&& f, A init, const mat& b, const mat& c, std::index_sequence) { - return ((init = f(std::move(init), b[Is], c[Is])), ...); - } - - template < typename A, std::size_t Size, typename F, std::size_t I, std::size_t... Is > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - vec fold1_impl(F&& f, const mat& a, std::index_sequence) { - vec init = a[I]; - return ((init = f(std::move(init), a[Is])), ...); - } - } - template < typename A, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto map(F&& f, const mat& a) - -> mat>::component_type, Size> - { - return impl::map_impl(std::forward(f), a, std::make_index_sequence{}); + auto map_join( + F&& f, + const mat& a + ) { + return impl::map_join_impl( + std::forward(f), a, std::make_index_sequence{}); } template < typename A, typename B, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto zip(F&& f, const mat& a, const mat& b) - -> mat, vec>::component_type, Size> - { - return impl::zip_impl(std::forward(f), a, b, std::make_index_sequence{}); + auto map_join( + F&& f, + const mat& a, + const mat& b + ) { + return impl::map_join_impl( + std::forward(f), a, b, std::make_index_sequence{}); } template < typename A, typename B, typename C, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto zip(F&& f, const mat& a, const mat& b, const mat& c) - -> mat, vec, vec>::component_type, Size> - { - return impl::zip_impl(std::forward(f), a, b, c, std::make_index_sequence{}); + auto map_join( + F&& f, + const mat& a, + const mat& b, + const mat& c + ) { + return impl::map_join_impl( + std::forward(f), a, b, c, std::make_index_sequence{}); } template < typename A, typename B, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold(F&& f, A init, const mat& b) { - return impl::fold_impl(std::forward(f), std::move(init), b, std::make_index_sequence{}); + auto fold_join( + F&& f, + A init, + const mat& b + ) { + return impl::fold_join_impl( + std::forward(f), std::move(init), b, std::make_index_sequence{}); } template < typename A, typename B, typename C, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold(F&& f, A init, const vec& b, const mat& c) { - return impl::fold_impl(std::forward(f), std::move(init), b, c, std::make_index_sequence{}); + auto fold_join( + F&& f, + A init, + const vec& b, + const mat& c + ) { + return impl::fold_join_impl( + std::forward(f), std::move(init), b, c, std::make_index_sequence{}); } template < typename A, typename B, typename C, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold(F&& f, A init, const mat& b, const mat& c) { - return impl::fold_impl(std::forward(f), std::move(init), b, c, std::make_index_sequence{}); + auto fold_join( + F&& f, + A init, + const mat& b, + const mat& c + ) { + return impl::fold_join_impl( + std::forward(f), std::move(init), b, c, std::make_index_sequence{}); } template < typename A, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - vec fold1(F&& f, const mat& a) { - return impl::fold1_impl(std::forward(f), a, std::make_index_sequence{}); + auto fold1_join( + F&& f, + const mat& a + ) { + return impl::fold1_join_impl( + std::forward(f), a, std::make_index_sequence{}); } } @@ -127,24 +196,24 @@ namespace vmath_hpp template < typename T, std::size_t Size > [[nodiscard]] constexpr mat operator-(const mat& xs) { - return map([](const vec& x){ return -x; }, xs); + return map_join([](const vec& x){ return -x; }, xs); } // operator+ template < typename T, std::size_t Size > [[nodiscard]] constexpr mat operator+(const mat& xs, T y) { - return map([y](const vec& x){ return x + y; }, xs); + 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([x](const vec& y){ return x + y; }, 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 zip([](const vec& x, const vec& y){ return x + y; }, xs, ys); + return map_join([](const vec& x, const vec& y){ return x + y; }, xs, ys); } // operator+= @@ -163,17 +232,17 @@ namespace vmath_hpp template < typename T, std::size_t Size > [[nodiscard]] constexpr mat operator-(const mat& xs, T y) { - return map([y](const vec& x){ return x - y; }, xs); + 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([x](const vec& y){ return x - y; }, 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 zip([](const vec& x, const vec& y){ return x - y; }, xs, ys); + return map_join([](const vec& x, const vec& y){ return x - y; }, xs, ys); } // operator-= @@ -192,24 +261,24 @@ namespace vmath_hpp template < typename T, std::size_t Size > [[nodiscard]] constexpr mat operator*(const mat& xs, T y) { - return map([y](const vec& x){ return x * y; }, xs); + 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([x](const vec& y){ return x * y; }, ys); + return map_join([x](const vec& y){ return x * y; }, ys); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec operator*(const vec& xs, const mat& ys) { - return fold([](const vec& acc, T x, const vec& y){ + return fold_join([](const vec& acc, T x, const vec& y){ return acc + x * y; }, vec{}, xs, ys); } template < typename T, std::size_t Size > [[nodiscard]] constexpr mat operator*(const mat& xs, const mat& ys) { - return map([&ys](const vec& x){ + return map_join([&ys](const vec& x){ return x * ys; }, xs); } @@ -235,17 +304,17 @@ namespace vmath_hpp template < typename T, std::size_t Size > [[nodiscard]] constexpr mat operator/(const mat& xs, T y) { - return map([y](const vec& x){ return x / y; }, xs); + 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([x](const vec& y){ return x / y; }, 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 zip([](const vec& x, const vec& y){ return x / y; }, xs, ys); + return map_join([](const vec& x, const vec& y){ return x / y; }, xs, ys); } // operator/= @@ -264,14 +333,14 @@ namespace vmath_hpp template < typename T, std::size_t Size > [[nodiscard]] constexpr bool operator==(const mat& xs, const mat& ys) { - return fold([](bool acc, const vec& x, const vec& y){ + return fold_join([](bool acc, const vec& x, const vec& y){ return acc && (x == y); }, true, xs, ys); } template < typename T, std::size_t Size > [[nodiscard]] constexpr bool operator!=(const mat& xs, const mat& ys) { - return fold([](bool acc, const vec& x, const vec& y){ + return fold_join([](bool acc, const vec& x, const vec& y){ return acc || (x != y); }, false, xs, ys); } diff --git a/headers/vmath.hpp/vmath_vec_fun.hpp b/headers/vmath.hpp/vmath_vec_fun.hpp index 804c17f..ffbff21 100644 --- a/headers/vmath.hpp/vmath_vec_fun.hpp +++ b/headers/vmath.hpp/vmath_vec_fun.hpp @@ -11,94 +11,151 @@ #include "vmath_fun.hpp" #include "vmath_vec.hpp" +namespace vmath_hpp::detail::impl +{ + template < typename A, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto map_join_impl( + F&& f, + const vec& a, + std::index_sequence + ) -> vec())), Size> + { + return { f(a[Is])... }; + } + + template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto map_join_impl( + F&& f, + const vec& a, + const vec& b, + std::index_sequence + ) -> vec(), + std::declval())), Size> + { + return { f(a[Is], b[Is])... }; + } + + template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto map_join_impl( + F&& f, + const vec& a, + const vec& b, + const vec& c, + std::index_sequence + ) -> vec(), + std::declval(), + std::declval())), Size> + { + return { f(a[Is], b[Is], c[Is])... }; + } + + template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold_join_impl( + F&& f, + A init, + const vec& b, + std::index_sequence + ) -> A { + return ((init = f(std::move(init), b[Is])), ...); + } + + template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold_join_impl( + F&& f, + A init, + const vec& b, + const vec& c, + std::index_sequence + ) -> A { + return ((init = f(std::move(init), b[Is], c[Is])), ...); + } + + template < typename A, std::size_t Size, typename F, std::size_t I, std::size_t... Is > + [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE + auto fold1_join_impl( + F&& f, + const vec& a, + std::index_sequence + ) -> A { + A init = a[I]; + return ((init = f(std::move(init), a[Is])), ...); + } +} + namespace vmath_hpp::detail { - namespace impl - { - template < typename A, std::size_t Size, typename F, std::size_t... Is > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto map_impl(F&& f, const vec& a, std::index_sequence) - -> vec, Size> - { - return { f(a[Is])... }; - } - - template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto zip_impl(F&& f, const vec& a, const vec& b, std::index_sequence) - -> vec, Size> - { - return { f(a[Is], b[Is])... }; - } - - template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto zip_impl(F&& f, const vec& a, const vec& b, const vec& c, std::index_sequence) - -> vec, Size> - { - return { f(a[Is], b[Is], c[Is])... }; - } - - template < typename A, typename B, std::size_t Size, typename F, std::size_t... Is > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold_impl(F&& f, A init, const vec& b, std::index_sequence) { - return ((init = f(std::move(init), b[Is])), ...); - } - - template < typename A, typename B, typename C, std::size_t Size, typename F, std::size_t... Is > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold_impl(F&& f, A init, const vec& b, const vec& c, std::index_sequence) { - return ((init = f(std::move(init), b[Is], c[Is])), ...); - } - - template < typename A, std::size_t Size, typename F, std::size_t I, std::size_t... Is > - [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold1_impl(F&& f, const vec& a, std::index_sequence) { - A init = a[I]; - return ((init = f(std::move(init), a[Is])), ...); - } - } - template < typename A, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto map(F&& f, const vec& a) - -> vec, Size> - { - return impl::map_impl(std::forward(f), a, std::make_index_sequence{}); + auto map_join( + F&& f, + const vec& a + ) { + return impl::map_join_impl( + std::forward(f), a, std::make_index_sequence{}); } template < typename A, typename B, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto zip(F&& f, const vec& a, const vec& b) - -> vec, Size> - { - return impl::zip_impl(std::forward(f), a, b, std::make_index_sequence{}); + auto map_join( + F&& f, + const vec& a, + const vec& b + ) { + return impl::map_join_impl( + std::forward(f), a, b, std::make_index_sequence{}); } template < typename A, typename B, typename C, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - auto zip(F&& f, const vec& a, const vec& b, const vec& c) - -> vec, Size> - { - return impl::zip_impl(std::forward(f), a, b, c, std::make_index_sequence{}); + auto map_join( + F&& f, + const vec& a, + const vec& b, + const vec& c + ) { + return impl::map_join_impl( + std::forward(f), a, b, c, std::make_index_sequence{}); } template < typename A, typename B, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold(F&& f, A init, const vec& b) { - return impl::fold_impl(std::forward(f), std::move(init), b, std::make_index_sequence{}); + auto fold_join( + F&& f, + A init, + const vec& b + ) { + return impl::fold_join_impl( + std::forward(f), std::move(init), b, std::make_index_sequence{}); } template < typename A, typename B, typename C, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold(F&& f, A init, const vec& b, const vec& c) { - return impl::fold_impl(std::forward(f), std::move(init), b, c, std::make_index_sequence{}); + auto fold_join( + F&& f, + A init, + const vec& b, + const vec& c + ) { + return impl::fold_join_impl( + std::forward(f), std::move(init), b, c, std::make_index_sequence{}); } template < typename A, std::size_t Size, typename F > [[nodiscard]] constexpr VMATH_HPP_FORCE_INLINE - A fold1(F&& f, const vec& a) { - return impl::fold1_impl(std::forward(f), a, std::make_index_sequence{}); + auto fold1_join( + F&& f, + const vec& a + ) { + return impl::fold1_join_impl( + std::forward(f), a, std::make_index_sequence{}); } } @@ -112,24 +169,24 @@ namespace vmath_hpp template < typename T, std::size_t Size > [[nodiscard]] constexpr vec operator-(const vec& xs) { - return map([](T x){ return -x; }, xs); + return map_join([](T x){ return -x; }, xs); } // operator+ template < typename T, std::size_t Size > [[nodiscard]] constexpr vec operator+(const vec& xs, T y) { - return map([y](T x){ return x + y; }, xs); + 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([x](T y){ return x + y; }, 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 zip([](T x, T y){ return x + y; }, xs, ys); + return map_join([](T x, T y){ return x + y; }, xs, ys); } // operator+= @@ -148,17 +205,17 @@ namespace vmath_hpp template < typename T, std::size_t Size > [[nodiscard]] constexpr vec operator-(const vec& xs, T y) { - return map([y](T x){ return x - y; }, xs); + 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([x](T y){ return x - y; }, 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 zip([](T x, T y){ return x - y; }, xs, ys); + return map_join([](T x, T y){ return x - y; }, xs, ys); } // operator-= @@ -177,17 +234,17 @@ namespace vmath_hpp template < typename T, std::size_t Size > [[nodiscard]] constexpr vec operator*(const vec& xs, T y) { - return map([y](T x){ return x * y; }, xs); + 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([x](T y){ return x * y; }, 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 zip([](T x, T y){ return x * y; }, xs, ys); + return map_join([](T x, T y){ return x * y; }, xs, ys); } // operator*= @@ -206,17 +263,17 @@ namespace vmath_hpp template < typename T, std::size_t Size > [[nodiscard]] constexpr vec operator/(const vec& xs, T y) { - return map([y](T x){ return x / y; }, xs); + 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([x](T y){ return x / y; }, 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 zip([](T x, T y){ return x / y; }, xs, ys); + return map_join([](T x, T y){ return x / y; }, xs, ys); } // operator/= @@ -235,14 +292,14 @@ namespace vmath_hpp template < typename T, std::size_t Size > [[nodiscard]] constexpr bool operator==(const vec& xs, const vec& ys) { - return fold([](bool acc, T x, T y){ + return fold_join([](bool acc, T x, T y){ return acc && (x == y); }, true, xs, ys); } template < typename T, std::size_t Size > [[nodiscard]] constexpr bool operator!=(const vec& xs, const vec& ys) { - return fold([](bool acc, T x, T y){ + return fold_join([](bool acc, T x, T y){ return acc || (x != y); }, false, xs, ys); } @@ -271,77 +328,77 @@ namespace vmath_hpp { template < typename T, std::size_t Size > [[nodiscard]] constexpr vec radians(const vec& degrees) { - return map([](T x) { return radians(x); }, degrees); + return map_join([](T x) { return radians(x); }, degrees); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec degrees(const vec& radians) { - return map([](T x) { return degrees(x); }, radians); + return map_join([](T x) { return degrees(x); }, radians); } template < typename T, std::size_t Size > [[nodiscard]] vec sin(const vec& xs) { - return map([](T x) { return sin(x); }, xs); + return map_join([](T x) { return sin(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec cos(const vec& xs) { - return map([](T x) { return cos(x); }, xs); + return map_join([](T x) { return cos(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec tan(const vec& xs) { - return map([](T x) { return tan(x); }, xs); + return map_join([](T x) { return tan(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec asin(const vec& xs) { - return map([](T x) { return asin(x); }, xs); + return map_join([](T x) { return asin(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec acos(const vec& xs) { - return map([](T x) { return acos(x); }, xs); + return map_join([](T x) { return acos(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec atan(const vec& xs) { - return map([](T x) { return atan(x); }, xs); + return map_join([](T x) { return atan(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec atan2(const vec& ys, const vec& xs) { - return zip([](T y, T x) { return atan2(y, x); }, ys, xs); + return map_join([](T y, T x) { return atan2(y, x); }, ys, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec sinh(const vec& xs) { - return map([](T x) { return sinh(x); }, xs); + return map_join([](T x) { return sinh(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec cosh(const vec& xs) { - return map([](T x) { return cosh(x); }, xs); + return map_join([](T x) { return cosh(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec tanh(const vec& xs) { - return map([](T x) { return tanh(x); }, xs); + return map_join([](T x) { return tanh(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec asinh(const vec& xs) { - return map([](T x) { return asinh(x); }, xs); + return map_join([](T x) { return asinh(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec acosh(const vec& xs) { - return map([](T x) { return acosh(x); }, xs); + return map_join([](T x) { return acosh(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec atanh(const vec& xs) { - return map([](T x) { return atanh(x); }, xs); + return map_join([](T x) { return atanh(x); }, xs); } } @@ -353,37 +410,37 @@ namespace vmath_hpp { template < typename T, std::size_t Size > [[nodiscard]] vec pow(const vec& xs, const vec& ys) { - return zip([](T x, T y) { return pow(x, y); }, xs, ys); + return map_join([](T x, T y) { return pow(x, y); }, xs, ys); } template < typename T, std::size_t Size > [[nodiscard]] vec exp(const vec& xs) { - return map([](T x) { return exp(x); }, xs); + return map_join([](T x) { return exp(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec log(const vec& xs) { - return map([](T x) { return log(x); }, xs); + return map_join([](T x) { return log(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec exp2(const vec& xs) { - return map([](T x) { return exp2(x); }, xs); + return map_join([](T x) { return exp2(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec log2(const vec& xs) { - return map([](T x) { return log2(x); }, xs); + return map_join([](T x) { return log2(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec sqrt(const vec& xs) { - return map([](T x) { return sqrt(x); }, xs); + return map_join([](T x) { return sqrt(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec rsqrt(const vec& xs) { - return map([](T x) { return rsqrt(x); }, xs); + return map_join([](T x) { return rsqrt(x); }, xs); } } @@ -395,52 +452,52 @@ namespace vmath_hpp { template < typename T, std::size_t Size > [[nodiscard]] constexpr vec abs(const vec& xs) { - return map([](T x) { return abs(x); }, xs); + return map_join([](T x) { return abs(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec sign(const vec& xs) { - return map([](T x) { return sign(x); }, xs); + return map_join([](T x) { return sign(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec reciprocal(const vec& xs) { - return map([](T x) { return reciprocal(x); }, xs); + return map_join([](T x) { return reciprocal(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec floor(const vec& xs) { - return map([](T x) { return floor(x); }, xs); + return map_join([](T x) { return floor(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec trunc(const vec& xs) { - return map([](T x) { return trunc(x); }, xs); + return map_join([](T x) { return trunc(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec round(const vec& xs) { - return map([](T x) { return round(x); }, xs); + return map_join([](T x) { return round(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec ceil(const vec& xs) { - return map([](T x) { return ceil(x); }, xs); + return map_join([](T x) { return ceil(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec fract(const vec& xs) { - return map([](T x) { return fract(x); }, xs); + return map_join([](T x) { return fract(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec fmod(const vec& xs, T y) { - return map([y](T x) { return fmod(x, y); }, xs); + return map_join([y](T x) { return fmod(x, y); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec fmod(const vec& xs, const vec& ys) { - return zip([](T x, T y) { return fmod(x, y); }, xs, ys); + return map_join([](T x, T y) { return fmod(x, y); }, xs, ys); } namespace impl @@ -459,97 +516,97 @@ namespace vmath_hpp template < typename T, std::size_t Size > [[nodiscard]] constexpr T min(const vec& xs) { - return fold1([](T acc, T x){ return min(acc, x); }, xs); + return fold1_join([](T acc, T x){ return min(acc, x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec min(const vec& xs, T y) { - return map([y](T x) { return min(x, y); }, xs); + return map_join([y](T x) { return min(x, y); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec min(const vec& xs, const vec& ys) { - return zip([](T x, T y) { return min(x, y); }, xs, ys); + return map_join([](T x, T y) { return min(x, y); }, xs, ys); } template < typename T, std::size_t Size > [[nodiscard]] constexpr T max(const vec& xs) { - return fold1([](T acc, T x){ return max(acc, x); }, xs); + return fold1_join([](T acc, T x){ return max(acc, x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec max(const vec& xs, T y) { - return map([y](T x) { return max(x, y); }, xs); + return map_join([y](T x) { return max(x, y); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec max(const vec& xs, const vec& ys) { - return zip([](T x, T y) { return max(x, y); }, xs, ys); + return map_join([](T x, T y) { return max(x, y); }, xs, ys); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec clamp(const vec& xs, T min_x, T max_x) { - return map([min_x, max_x](T x) { return clamp(x, min_x, max_x); }, xs); + return map_join([min_x, max_x](T x) { return clamp(x, min_x, max_x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec clamp(const vec& xs, const vec& min_xs, const vec& max_xs) { - return zip([](T x, T min_x, T max_x) { return clamp(x, min_x, max_x); }, xs, min_xs, max_xs); + return map_join([](T x, T min_x, T max_x) { return clamp(x, min_x, max_x); }, xs, min_xs, max_xs); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec saturate(const vec& xs) { - return map([](T x) { return saturate(x); }, xs); + return map_join([](T x) { return saturate(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec lerp(const vec& xs, const vec& ys, T a) { - return zip([a](T x, T y) { return lerp(x, y, a); }, xs, ys); + return map_join([a](T x, T y) { return lerp(x, y, a); }, xs, ys); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec lerp(const vec& xs, const vec& ys, const vec& as) { - return zip([](T x, T y, T a) { return lerp(x, y, a); }, xs, ys, as); + return map_join([](T x, T y, T a) { return lerp(x, y, a); }, xs, ys, as); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec step(T edge, const vec& xs) { - return map([edge](T x) { return step(edge, x); }, xs); + return map_join([edge](T x) { return step(edge, x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec step(const vec& edges, const vec& xs) { - return zip([](T edge, T x) { return step(edge, x); }, edges, xs); + return map_join([](T edge, T x) { return step(edge, x); }, edges, xs); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec smoothstep(T edge0, T edge1, const vec& xs) { - return map([edge0, edge1](T x) { return smoothstep(edge0, edge1, x); }, xs); + return map_join([edge0, edge1](T x) { return smoothstep(edge0, edge1, x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec smoothstep(const vec& edges0, const vec& edges1, const vec& xs) { - return zip([](T edge0, T edge1, T x) { return smoothstep(edge0, edge1, x); }, edges0, edges1, xs); + return map_join([](T edge0, T edge1, T x) { return smoothstep(edge0, edge1, x); }, edges0, edges1, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec isnan(const vec& xs) { - return map([](T x) { return isnan(x); }, xs); + return map_join([](T x) { return isnan(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec isinf(const vec& xs) { - return map([](T x) { return isinf(x); }, xs); + return map_join([](T x) { return isinf(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec isfinite(const vec& xs) { - return map([](T x) { return isfinite(x); }, xs); + return map_join([](T x) { return isfinite(x); }, xs); } template < typename T, std::size_t Size > [[nodiscard]] vec fma(const vec& as, const vec& bs, const vec& cs) { - return zip([](T a, T b, T c) { return fma(a, b, c); }, as, bs, cs); + return map_join([](T a, T b, T c) { return fma(a, b, c); }, as, bs, cs); } namespace impl @@ -568,7 +625,7 @@ namespace vmath_hpp template < typename T, std::size_t Size > [[nodiscard]] vec ldexp(const vec& xs, const vec& exps) { - return zip([](T x, int exp) { return ldexp(x, exp); }, xs, exps); + return map_join([](T x, int exp) { return ldexp(x, exp); }, xs, exps); } } @@ -580,7 +637,7 @@ namespace vmath_hpp { template < typename T, std::size_t Size > [[nodiscard]] constexpr T dot(const vec& xs, const vec& ys) { - return fold([](T acc, T x, T y){ + return fold_join([](T acc, T x, T y){ return acc + (x * y); }, T(0), xs, ys); } @@ -649,51 +706,51 @@ namespace vmath_hpp { template < typename T, std::size_t Size > [[nodiscard]] constexpr vec less(const vec& xs, const vec& ys) { - return zip([](T x, T y){ return less(x, y); }, xs, ys); + return map_join([](T x, T y){ return less(x, y); }, xs, ys); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec less_equal(const vec& xs, const vec& ys) { - return zip([](T x, T y){ return less_equal(x, y); }, xs, ys); + return map_join([](T x, T y){ return less_equal(x, y); }, xs, ys); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec greater(const vec& xs, const vec& ys) { - return zip([](T x, T y){ return greater(x, y); }, xs, ys); + return map_join([](T x, T y){ return greater(x, y); }, xs, ys); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec greater_equal(const vec& xs, const vec& ys) { - return zip([](T x, T y){ return greater_equal(x, y); }, xs, ys); + return map_join([](T x, T y){ return greater_equal(x, y); }, xs, ys); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec equal_to(const vec& xs, const vec& ys) { - return zip([](T x, T y){ return equal_to(x, y); }, xs, ys); + return map_join([](T x, T y){ return equal_to(x, y); }, xs, ys); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec equal_to(const vec& xs, const vec& ys, T epsilon) { - return zip([epsilon](T x, T y){ return equal_to(x, y, epsilon); }, xs, ys); + return map_join([epsilon](T x, T y){ return equal_to(x, y, epsilon); }, xs, ys); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec not_equal_to(const vec& xs, const vec& ys) { - return zip([](T x, T y){ return not_equal_to(x, y); }, xs, ys); + return map_join([](T x, T y){ return not_equal_to(x, y); }, xs, ys); } template < typename T, std::size_t Size > [[nodiscard]] constexpr vec not_equal_to(const vec& xs, const vec& ys, T epsilon) { - return zip([epsilon](T x, T y){ return not_equal_to(x, y, epsilon); }, xs, ys); + return map_join([epsilon](T x, T y){ return not_equal_to(x, y, epsilon); }, xs, ys); } template < typename T, std::size_t Size > [[nodiscard]] constexpr bool any(const vec& xs) { - return fold([](bool acc, T x){ return acc || any(x); }, false, xs); + return fold_join([](bool acc, T x){ return acc || any(x); }, false, xs); } template < typename T, std::size_t Size > [[nodiscard]] constexpr bool all(const vec& xs) { - return fold([](bool acc, T x){ return acc && all(x); }, true, xs); + return fold_join([](bool acc, T x){ return acc && all(x); }, true, xs); } } diff --git a/untests/vmath_mat_fun_tests.cpp b/untests/vmath_mat_fun_tests.cpp index 0c0e213..c8b7069 100644 --- a/untests/vmath_mat_fun_tests.cpp +++ b/untests/vmath_mat_fun_tests.cpp @@ -32,27 +32,27 @@ namespace TEST_CASE("vmath/mat_fun") { SUBCASE("detail") { - STATIC_REQUIRE(map([](const int2& x){ + STATIC_REQUIRE(map_join([](const int2& x){ return x * 2; }, int2x2{}) == int2x2(2,0,0,2)); - STATIC_REQUIRE(zip([](const int2& x, const int2& y){ + STATIC_REQUIRE(map_join([](const int2& x, const int2& y){ return x + y; }, int2x2{}, int2x2{}) == int2x2(2,0,0,2)); - STATIC_REQUIRE(zip([](const int2& x, const int2& y, const int2& z){ + STATIC_REQUIRE(map_join([](const int2& x, const int2& y, const int2& z){ return x + y + z; }, int2x2{}, int2x2{}, int2x2{}) == int2x2(3,0,0,3)); - STATIC_REQUIRE(fold([](int acc, const int2& x){ + STATIC_REQUIRE(fold_join([](int acc, const int2& x){ return acc + x.x; }, 0, int2x2{}) == 1); - STATIC_REQUIRE(fold([](int acc, const int2& x, const int2& y){ + STATIC_REQUIRE(fold_join([](int acc, const int2& x, const int2& y){ return acc + x.x + y.x; }, 0, int2x2{}, int2x2{}) == 2); - STATIC_REQUIRE(fold1([](const int2& acc, const int2& x){ + STATIC_REQUIRE(fold1_join([](const int2& acc, const int2& x){ return acc + x; }, int2x2{}) == int2(1,1)); } diff --git a/untests/vmath_vec_fun_tests.cpp b/untests/vmath_vec_fun_tests.cpp index 79b7cbd..67faff9 100644 --- a/untests/vmath_vec_fun_tests.cpp +++ b/untests/vmath_vec_fun_tests.cpp @@ -15,27 +15,27 @@ namespace TEST_CASE("vmath/vec_fun") { SUBCASE("Detail") { - STATIC_REQUIRE(map([](const int& x){ + STATIC_REQUIRE(map_join([](const int& x){ return x * 2; }, int2{1}) == int2{2}); - STATIC_REQUIRE(zip([](const int& x, const int& y){ + STATIC_REQUIRE(map_join([](const int& x, const int& y){ return x + y; }, int2{1}, int2{1}) == int2{2}); - STATIC_REQUIRE(zip([](const int& x, const int& y, const int& z){ + STATIC_REQUIRE(map_join([](const int& x, const int& y, const int& z){ return x + y + z; }, int2{1}, int2{1}, int2{1}) == int2(3)); - STATIC_REQUIRE(fold([](int acc, const int& x){ + STATIC_REQUIRE(fold_join([](int acc, const int& x){ return acc + x; }, 0, int2{1}) == 2); - STATIC_REQUIRE(fold([](int acc, const int& x, const int& y){ + STATIC_REQUIRE(fold_join([](int acc, const int& x, const int& y){ return acc + x + y; }, 0, int2{1}, int2{1}) == 4); - STATIC_REQUIRE(fold1([](const int& acc, const int& x){ + STATIC_REQUIRE(fold1_join([](const int& acc, const int& x){ return acc + x; }, int2{1}) == 2); } From 9767efbb1aa4f5ea7fc1609debad751ff0f70a7b Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 4 Dec 2020 23:31:22 +0700 Subject: [PATCH 10/10] additional matrix ctos from matrix and vector --- README.md | 8 ++++++++ headers/vmath.hpp/vmath_mat.hpp | 23 +++++++++++++++++++++++ untests/vmath_mat_tests.cpp | 4 ++++ 3 files changed, 35 insertions(+) diff --git a/README.md b/README.md index aeaf61c..a345c56 100644 --- a/README.md +++ b/README.md @@ -248,6 +248,10 @@ public: const row_type& row1, const row_type& row2); + constexpr mat_base( + const mat_base& m, + const vec_base& v); + constexpr explicit mat_base(const mat_base& other); constexpr explicit mat_base(const mat_base& other); }; @@ -279,6 +283,10 @@ public: const row_type& row2, const row_type& row3); + constexpr mat_base( + const mat_base& m, + const vec_base& v); + constexpr explicit mat_base(const mat_base& other); constexpr explicit mat_base(const mat_base& other); }; diff --git a/headers/vmath.hpp/vmath_mat.hpp b/headers/vmath.hpp/vmath_mat.hpp index 65d8b4c..7111014 100644 --- a/headers/vmath.hpp/vmath_mat.hpp +++ b/headers/vmath.hpp/vmath_mat.hpp @@ -99,6 +99,14 @@ namespace vmath_hpp::detail const row_type& row2) : rows{row0, row1, row2} {} + constexpr mat_base( + const mat_base& m, + const vec_base& v) + : rows{ + row_type{m.rows[0], 0}, + row_type{m.rows[1], 0}, + row_type{v, 1}} {} + constexpr explicit mat_base( const mat_base& other) : rows{ @@ -158,6 +166,15 @@ namespace vmath_hpp::detail const row_type& row3) : rows{row0, row1, row2, row3} {} + constexpr mat_base( + const mat_base& m, + const vec_base& v) + : rows{ + row_type{m.rows[0], 0}, + row_type{m.rows[1], 0}, + row_type{m.rows[2], 0}, + row_type{v, 1}} {} + constexpr explicit mat_base( const mat_base& other) : rows{ @@ -281,6 +298,9 @@ namespace vmath_hpp template < typename T > mat(const vec&, const vec&, const vec&) -> mat; + template < typename T > + mat(const mat&, const vec&) -> mat; + template < typename T > mat(std::initializer_list, std::initializer_list, std::initializer_list) -> mat; @@ -292,6 +312,9 @@ namespace vmath_hpp template < typename T > mat(const vec&, const vec&, const vec&, const vec&) -> mat; + template < typename T > + mat(const mat&, const vec&) -> mat; + template < typename T > mat(std::initializer_list, std::initializer_list, std::initializer_list, std::initializer_list) -> mat; diff --git a/untests/vmath_mat_tests.cpp b/untests/vmath_mat_tests.cpp index 97688d1..fed8be3 100644 --- a/untests/vmath_mat_tests.cpp +++ b/untests/vmath_mat_tests.cpp @@ -32,10 +32,12 @@ TEST_CASE("vmath/mat") { STATIC_REQUIRE(mat{1,2,3,4,5,6,7,8,9}.size == 3); STATIC_REQUIRE(mat{{1,2,3},{4,5,6},{7,8,9}}.size == 3); STATIC_REQUIRE(mat{vec{1,2,3},vec{4,5,6},vec{7,8,9}}.size == 3); + STATIC_REQUIRE(mat{mat{1,2,3,4},vec{5,6}}.size == 3); STATIC_REQUIRE(mat{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}.size == 4); STATIC_REQUIRE(mat{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}.size == 4); STATIC_REQUIRE(mat{vec{1,2,3,4},vec{5,6,7,8},vec{9,10,11,12},vec{13,14,15,16}}.size == 4); + STATIC_REQUIRE(mat{mat{1,2,3,4,5,6,7,8,9},vec{5,6,7}}.size == 4); } SUBCASE("ctors") { @@ -74,6 +76,7 @@ TEST_CASE("vmath/mat") { STATIC_REQUIRE(int3x3(int3{2,3,4}) == int3x3(2,0,0,0,3,0,0,0,4)); STATIC_REQUIRE(int3x3(1,2,3,4,5,6,7,8,9) == int3x3(1,2,3,4,5,6,7,8,9)); STATIC_REQUIRE(int3x3({1,2,3},{4,5,6},{7,8,9}) == int3x3(1,2,3,4,5,6,7,8,9)); + STATIC_REQUIRE(int3x3(int2x2({1,2},{3,4}),int2{5,6}) == int3x3(1,2,0,3,4,0,5,6,1)); STATIC_REQUIRE(int3x3(int3x3({1,2,3},{4,5,6},{7,8,9})) == int3x3(1,2,3,4,5,6,7,8,9)); STATIC_REQUIRE(int3x3(int2x2({1,2},{3,4})) == int3x3(1,2,0,3,4,0,0,0,1)); STATIC_REQUIRE(int3x3(int4x4({1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16})) == int3x3(1,2,3,5,6,7,9,10,11)); @@ -83,6 +86,7 @@ TEST_CASE("vmath/mat") { STATIC_REQUIRE(int4x4(int4{2,3,4,5}) == int4x4(2,0,0,0,0,3,0,0,0,0,4,0,0,0,0,5)); STATIC_REQUIRE(int4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) == int4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)); STATIC_REQUIRE(int4x4({1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}) == int4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)); + STATIC_REQUIRE(int4x4(int3x3({1,2,3},{4,5,6},{7,8,9}),int3{10,11,12}) == int4x4(1,2,3,0,4,5,6,0,7,8,9,0,10,11,12,1)); STATIC_REQUIRE(int4x4(int4x4({1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16})) == int4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)); STATIC_REQUIRE(int4x4(int2x2({1,2},{3,4})) == int4x4(1,2,0,0,3,4,0,0,0,0,1,0,0,0,0,1)); STATIC_REQUIRE(int4x4(int3x3({1,2,3},{4,5,6},{7,8,9})) == int4x4(1,2,3,0,4,5,6,0,7,8,9,0,0,0,0,1));