From b8fe8831289a13a4b0aaf70231bd2271454d1dc0 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 4 Oct 2019 00:25:17 +0700 Subject: [PATCH] approximately and contains_nan for matrix --- headers/enduro2d/math/mat2.hpp | 64 +++++++++++++++++++--------- headers/enduro2d/math/mat3.hpp | 70 +++++++++++++++++++++---------- headers/enduro2d/math/mat4.hpp | 76 ++++++++++++++++++++++++---------- 3 files changed, 147 insertions(+), 63 deletions(-) diff --git a/headers/enduro2d/math/mat2.hpp b/headers/enduro2d/math/mat2.hpp index d4c206cf..e2e28349 100644 --- a/headers/enduro2d/math/mat2.hpp +++ b/headers/enduro2d/math/mat2.hpp @@ -28,18 +28,20 @@ namespace e2d {1, 0}, {0, 1}}; public: - static const mat2& zero() noexcept; - static const mat2& identity() noexcept; + static constexpr mat2 zero() noexcept; + static constexpr mat2 identity() noexcept; public: - mat2() noexcept = default; - mat2(const mat2& other) noexcept = default; - mat2& operator=(const mat2& other) noexcept = default; + constexpr mat2() noexcept = default; + constexpr mat2(const mat2& other) noexcept = default; + constexpr mat2& operator=(const mat2& other) noexcept = default; - mat2(const vec2& row0, - const vec2& row1) noexcept; + constexpr mat2( + const vec2& row0, + const vec2& row1) noexcept; - mat2(T m11, T m12, - T m21, T m22) noexcept; + constexpr mat2( + T m11, T m12, + T m21, T m22) noexcept; template < typename To > mat2 cast_to() const noexcept; @@ -61,29 +63,27 @@ namespace e2d namespace e2d { template < typename T > - const mat2& mat2::zero() noexcept { - static const mat2 zero{ + constexpr mat2 mat2::zero() noexcept { + return { 0, 0, 0, 0}; - return zero; } template < typename T > - const mat2& mat2::identity() noexcept { - static const mat2 identity{ + constexpr mat2 mat2::identity() noexcept { + return { 1, 0, 0, 1}; - return identity; } template < typename T > - mat2::mat2( + constexpr mat2::mat2( const vec2& row0, const vec2& row1) noexcept : rows{row0, row1} {} template < typename T > - mat2::mat2( + constexpr mat2::mat2( T m11, T m12, T m21, T m22) noexcept : rows{{m11, m12}, @@ -147,7 +147,7 @@ namespace e2d // template < typename T > - mat2 make_mat2( + constexpr mat2 make_mat2( const vec2& row0, const vec2& row1) noexcept { @@ -155,7 +155,7 @@ namespace e2d } template < typename T > - mat2 make_mat2( + constexpr mat2 make_mat2( T m11, T m12, T m21, T m22) noexcept { @@ -311,6 +311,21 @@ namespace e2d::math -sn, cs}; } + // + // approximately + // + + template < typename T > + bool approximately( + const mat2& l, + const mat2& r, + T precision = math::default_precision()) noexcept + { + return + math::approximately(l.rows[0], r.rows[0], precision) && + math::approximately(l.rows[1], r.rows[1], precision); + } + // // inversed // @@ -349,4 +364,15 @@ namespace e2d::math mm[0], mm[2], mm[1], mm[3]}; } + + // + // contains_nan + // + + template < typename T > + bool contains_nan(const mat2& v) noexcept { + return + math::contains_nan(v.rows[0]) || + math::contains_nan(v.rows[1]); + } } diff --git a/headers/enduro2d/math/mat3.hpp b/headers/enduro2d/math/mat3.hpp index 3260b990..038bdc04 100644 --- a/headers/enduro2d/math/mat3.hpp +++ b/headers/enduro2d/math/mat3.hpp @@ -30,20 +30,22 @@ namespace e2d {0, 1, 0}, {0, 0, 1}}; public: - static const mat3& zero() noexcept; - static const mat3& identity() noexcept; + static constexpr mat3 zero() noexcept; + static constexpr mat3 identity() noexcept; public: - mat3() noexcept = default; - mat3(const mat3& other) noexcept = default; - mat3& operator=(const mat3& other) noexcept = default; + constexpr mat3() noexcept = default; + constexpr mat3(const mat3& other) noexcept = default; + constexpr mat3& operator=(const mat3& other) noexcept = default; - mat3(const vec3& row0, - const vec3& row1, - const vec3& row2) noexcept; + constexpr mat3( + const vec3& row0, + const vec3& row1, + const vec3& row2) noexcept; - mat3(T m11, T m12, T m13, - T m21, T m22, T m23, - T m31, T m32, T m33) noexcept; + constexpr mat3( + T m11, T m12, T m13, + T m21, T m22, T m23, + T m31, T m32, T m33) noexcept; template < typename To > mat3 cast_to() const noexcept; @@ -65,32 +67,30 @@ namespace e2d namespace e2d { template < typename T > - const mat3& mat3::zero() noexcept { - static const mat3 zero{ + constexpr mat3 mat3::zero() noexcept { + return { 0, 0, 0, 0, 0, 0, 0, 0, 0}; - return zero; } template < typename T > - const mat3& mat3::identity() noexcept { - static const mat3 identity{ + constexpr mat3 mat3::identity() noexcept { + return { 1, 0, 0, 0, 1, 0, 0, 0, 1}; - return identity; } template < typename T > - mat3::mat3( + constexpr mat3::mat3( const vec3& row0, const vec3& row1, const vec3& row2) noexcept : rows{row0, row1, row2} {} template < typename T > - mat3::mat3( + constexpr mat3::mat3( T m11, T m12, T m13, T m21, T m22, T m23, T m31, T m32, T m33) noexcept @@ -157,7 +157,7 @@ namespace e2d // template < typename T > - mat3 make_mat3( + constexpr mat3 make_mat3( const vec3& row0, const vec3& row1, const vec3& row2) noexcept @@ -166,7 +166,7 @@ namespace e2d } template < typename T > - mat3 make_mat3( + constexpr mat3 make_mat3( T m11, T m12, T m13, T m21, T m22, T m23, T m31, T m32, T m33) noexcept @@ -423,6 +423,22 @@ namespace e2d::math T(2) * (xz + yw), T(2) * (yz - xw), T(1) - T(2) * (xx + yy)}; } + // + // approximately + // + + template < typename T > + bool approximately( + const mat3& l, + const mat3& r, + T precision = math::default_precision()) noexcept + { + return + math::approximately(l.rows[0], r.rows[0], precision) && + math::approximately(l.rows[1], r.rows[1], precision) && + math::approximately(l.rows[2], r.rows[2], precision); + } + // // inversed // @@ -477,4 +493,16 @@ namespace e2d::math mm[1], mm[4], mm[7], mm[2], mm[5], mm[8]}; } + + // + // contains_nan + // + + template < typename T > + bool contains_nan(const mat3& v) noexcept { + return + math::contains_nan(v.rows[0]) || + math::contains_nan(v.rows[1]) || + math::contains_nan(v.rows[2]); + } } diff --git a/headers/enduro2d/math/mat4.hpp b/headers/enduro2d/math/mat4.hpp index bf120285..4bb7ca97 100644 --- a/headers/enduro2d/math/mat4.hpp +++ b/headers/enduro2d/math/mat4.hpp @@ -33,22 +33,24 @@ namespace e2d {0, 0, 1, 0}, {0, 0, 0, 1}}; public: - static const mat4& zero() noexcept; - static const mat4& identity() noexcept; + static constexpr mat4 zero() noexcept; + static constexpr mat4 identity() noexcept; public: - mat4() noexcept = default; - mat4(const mat4& other) noexcept = default; - mat4& operator=(const mat4& other) noexcept = default; + constexpr mat4() noexcept = default; + constexpr mat4(const mat4& other) noexcept = default; + constexpr mat4& operator=(const mat4& other) noexcept = default; - mat4(const vec4& row0, - const vec4& row1, - const vec4& row2, - const vec4& row3) noexcept; + constexpr mat4( + const vec4& row0, + const vec4& row1, + const vec4& row2, + const vec4& row3) noexcept; - mat4(T m11, T m12, T m13, T m14, - T m21, T m22, T m23, T m24, - T m31, T m32, T m33, T m34, - T m41, T m42, T m43, T m44) noexcept; + constexpr mat4( + T m11, T m12, T m13, T m14, + T m21, T m22, T m23, T m24, + T m31, T m32, T m33, T m34, + T m41, T m42, T m43, T m44) noexcept; template < typename To > mat4 cast_to() const noexcept; @@ -70,27 +72,25 @@ namespace e2d namespace e2d { template < typename T > - const mat4& mat4::zero() noexcept { - static const mat4 zero{ + constexpr mat4 mat4::zero() noexcept { + return { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - return zero; } template < typename T > - const mat4& mat4::identity() noexcept { - static const mat4 identity{ + constexpr mat4 mat4::identity() noexcept { + return { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; - return identity; } template < typename T > - mat4::mat4( + constexpr mat4::mat4( const vec4& row0, const vec4& row1, const vec4& row2, @@ -98,7 +98,7 @@ namespace e2d : rows{row0, row1, row2, row3} {} template < typename T > - mat4::mat4( + constexpr mat4::mat4( T m11, T m12, T m13, T m14, T m21, T m22, T m23, T m24, T m31, T m32, T m33, T m34, @@ -168,7 +168,7 @@ namespace e2d // template < typename T > - mat4 make_mat4( + constexpr mat4 make_mat4( const vec4& row0, const vec4& row1, const vec4& row2, @@ -178,7 +178,7 @@ namespace e2d } template < typename T > - mat4 make_mat4( + constexpr mat4 make_mat4( T m11, T m12, T m13, T m14, T m21, T m22, T m23, T m24, T m31, T m32, T m33, T m34, @@ -647,6 +647,23 @@ namespace e2d::math T(0), T(0), tz, T(0)}; } + // + // approximately + // + + template < typename T > + bool approximately( + const mat4& l, + const mat4& r, + T precision = math::default_precision()) noexcept + { + return + math::approximately(l.rows[0], r.rows[0], precision) && + math::approximately(l.rows[1], r.rows[1], precision) && + math::approximately(l.rows[2], r.rows[2], precision) && + math::approximately(l.rows[3], r.rows[3], precision); + } + // // inversed // @@ -733,4 +750,17 @@ namespace e2d::math mm[2], mm[6], mm[10], mm[14], mm[3], mm[7], mm[11], mm[15]}; } + + // + // contains_nan + // + + template < typename T > + bool contains_nan(const mat4& v) noexcept { + return + math::contains_nan(v.rows[0]) || + math::contains_nan(v.rows[1]) || + math::contains_nan(v.rows[2]) || + math::contains_nan(v.rows[3]); + } }