From 9e748ac687e905f55f7ee013bc5499b31c8a030d Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 11 Feb 2019 18:42:08 +0100 Subject: compat/simple-mat: more constexpr --- compat/simple-mat.hpp | 78 +++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 46 deletions(-) (limited to 'compat/simple-mat.hpp') diff --git a/compat/simple-mat.hpp b/compat/simple-mat.hpp index d42a2fe8..43d136f9 100644 --- a/compat/simple-mat.hpp +++ b/compat/simple-mat.hpp @@ -8,13 +8,11 @@ #pragma once -#include "export.hpp" - #include #include #include -namespace simple_mat_detail { +namespace simple_mat { // last param to fool SFINAE into overloading template struct equals @@ -67,17 +65,17 @@ public: // parameters w_ and h_ are rebound so that SFINAE occurs // removing them causes a compile-time error -sh 20150811 - template std::enable_if_t::value, num> - constexpr inline operator()(unsigned i) const& { return data[i][0]; } + template std::enable_if_t::value, num> + constexpr inline operator()(t i) const& { return data[(unsigned)i][0]; } - template std::enable_if_t::value, num> - constexpr inline operator()(unsigned i) const& { return data[0][i]; } + template std::enable_if_t::value, num> + constexpr inline operator()(t i) const& { return data[0][(unsigned)i]; } - template std::enable_if_t::value, num&> - constexpr inline operator()(unsigned i) & { return data[i][0]; } + template std::enable_if_t::value, num&> + constexpr inline operator()(t i) & { return data[(unsigned)i][0]; } - template std::enable_if_t::value, num&> - constexpr inline operator()(unsigned i) & { return data[0][i]; } + template std::enable_if_t::value, num&> + constexpr inline operator()(t i) & { return data[0][(unsigned)i]; } #define OTR_MAT_ASSERT_SWIZZLE static_assert(P == h_ && Q == w_) @@ -105,35 +103,25 @@ public: constexpr inline z() & { OTR_MAT_ASSERT_SWIZZLE; return operator()(2); } template std::enable_if_t::value, num&> - - template - constexpr Mat slice() const - { - return (const double*)*this; - } - - template std::enable_if_t::value, Mat> - slice() const { return ((double const*)*this) + off; } - - template std::enable_if_t::value && equals::value, - Mat> - slice() const { return ((double const*)*this) + off; } constexpr inline w() & { OTR_MAT_ASSERT_SWIZZLE; return operator()(3); } template - std::enable_if_t::value, num> - norm() const + constexpr auto norm_squared() const -> std::enable_if_t::value, num> { static_assert(P == h_ && Q == w_); const num val = dot(*this); + constexpr num eps = num(1e-4); - if (val < num(1e-4)) + if (val < eps) return num(0); else - return std::sqrt(val); + return val; } + template + inline auto norm() const { return num(std::sqrt(norm_squared())); } + template std::enable_if_t::value, num> constexpr dot(const Mat& p2) const @@ -152,7 +140,7 @@ public: constexpr cross(const Mat& b) const { static_assert(P == h_ && Q == w_); - auto& a = *this; + const auto& a = *this; return Mat(a.y()*b.z() - a.z()*b.y(), a.z()*b.x() - a.x()*b.z(), @@ -221,15 +209,10 @@ public: } template - constexpr inline num operator()(t j, u i) const& { return data[(int) j][(int) i]; } + constexpr inline num operator()(t j, u i) const& { return data[(unsigned)j][(unsigned)i]; } template - constexpr inline num& operator()(t j, u i) & { return data[(int) j][(int) i]; } - -#ifdef __GNUG__ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wmissing-braces" -#endif + constexpr inline num& operator()(t j, u i) & { return data[(unsigned)j][(unsigned)i]; } template::value>> @@ -238,10 +221,6 @@ public: static_assert(h__ == h_ && w__ == w_); } -#ifdef __GNUG__ -# pragma GCC diagnostic pop -#endif - constexpr Mat() { for (int j = 0; j < h_; j++) @@ -256,8 +235,8 @@ public: data[j][i] = mem[i*h_+j]; } - operator num*() { return reinterpret_cast(data); } - operator const num*() const { return reinterpret_cast(data); } + constexpr operator num*() & { return (num*)data; } + constexpr operator const num*() const& { return (const num*)data; } // XXX add more operators as needed, third-party dependencies mostly // not needed merely for matrix algebra -sh 20141030 @@ -288,6 +267,15 @@ public: return ret; } + + constexpr Mat& operator=(const Mat& rhs) + { + for (unsigned j = 0; j < h_; j++) + for (unsigned i = 0; i < w_; i++) + data[j][i] = rhs(j, i); + + return *this; + } }; template @@ -318,9 +306,7 @@ OTR_GENERIC_EXPORT inline void test() } #endif -} // ns simple_mat_detail +} // ns detail template -using Mat = simple_mat_detail::Mat; - - +using Mat = simple_mat::Mat; -- cgit v1.2.3