summaryrefslogtreecommitdiffhomepage
path: root/compat/simple-mat.hpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2017-10-02 07:52:41 +0200
committerStanislaw Halik <sthalik@misaki.pl>2017-10-02 07:54:24 +0200
commit146cf358925c433e1b9af022cab8e915160467f5 (patch)
treeed53feac997c3cb16de84ff4d6451385811679c6 /compat/simple-mat.hpp
parent84c3f6ba94aed8b1132359e39430296e3624c6a9 (diff)
compat/simple-mat: add constexpr
Diffstat (limited to 'compat/simple-mat.hpp')
-rw-r--r--compat/simple-mat.hpp78
1 files changed, 41 insertions, 37 deletions
diff --git a/compat/simple-mat.hpp b/compat/simple-mat.hpp
index d983f79b..3b2d89eb 100644
--- a/compat/simple-mat.hpp
+++ b/compat/simple-mat.hpp
@@ -14,7 +14,7 @@
#include <utility>
#include <cmath>
-namespace {
+namespace simple_mat_detail {
// last param to fool SFINAE into overloading
template<int i, int j, int>
struct equals
@@ -49,7 +49,6 @@ namespace {
{
enum { value = h * w == sizeof...(ts) };
};
-}
template<typename num, int h_, int w_>
class Mat
@@ -59,54 +58,54 @@ class Mat
public:
template<int Q = w_> std::enable_if_t<equals<Q, 1, 0>::value, num>
- inline operator()(int i) const { return data[i][0]; }
+ constexpr inline operator()(int i) const { return data[i][0]; }
template<int P = h_> std::enable_if_t<equals<P, 1, 1>::value, num>
- inline operator()(int i) const { return data[0][i]; }
+ constexpr inline operator()(int i) const { return data[0][i]; }
template<int Q = w_> std::enable_if_t<equals<Q, 1, 2>::value, num&>
- inline operator()(int i) { return data[i][0]; }
+ constexpr inline operator()(int i) { return data[i][0]; }
template<int P = h_> std::enable_if_t<equals<P, 1, 3>::value, num&>
- inline operator()(int i) { return data[0][i]; }
+ constexpr inline operator()(int i) { return data[0][i]; }
template<int Q = w_> std::enable_if_t<equals<Q, 1, 0>::value, num>
- inline operator()(unsigned i) const { return data[i][0]; }
+ constexpr inline operator()(unsigned i) const { return data[i][0]; }
template<int P = h_> std::enable_if_t<equals<P, 1, 1>::value, num>
- inline operator()(unsigned i) const { return data[0][i]; }
+ constexpr inline operator()(unsigned i) const { return data[0][i]; }
template<int Q = w_> std::enable_if_t<equals<Q, 1, 2>::value, num&>
- inline operator()(unsigned i) { return data[i][0]; }
+ constexpr inline operator()(unsigned i) { return data[i][0]; }
template<int P = h_> std::enable_if_t<equals<P, 1, 3>::value, num&>
- inline operator()(unsigned i) { return data[0][i]; }
+ constexpr inline operator()(unsigned i) { return data[0][i]; }
#define OPENTRACK_ASSERT_SWIZZLE static_assert(P == h_ && Q == w_, "")
template<int P = h_, int Q = w_> std::enable_if_t<maybe_add_swizzle<P, Q, 1>::value, num>
- x() const { OPENTRACK_ASSERT_SWIZZLE; return operator()(0); }
+ constexpr inline x() const { OPENTRACK_ASSERT_SWIZZLE; return operator()(0); }
template<int P = h_, int Q = w_> std::enable_if_t<maybe_add_swizzle<P, Q, 2>::value, num>
- y() const { OPENTRACK_ASSERT_SWIZZLE; return operator()(1); }
+ constexpr inline y() const { OPENTRACK_ASSERT_SWIZZLE; return operator()(1); }
template<int P = h_, int Q = w_> std::enable_if_t<maybe_add_swizzle<P, Q, 3>::value, num>
- z() const { OPENTRACK_ASSERT_SWIZZLE; return operator()(2); }
+ constexpr inline z() const { OPENTRACK_ASSERT_SWIZZLE; return operator()(2); }
template<int P = h_, int Q = w_> std::enable_if_t<maybe_add_swizzle<P, Q, 4>::value, num>
- w() const { OPENTRACK_ASSERT_SWIZZLE; return operator()(3); }
+ constexpr inline w() const { OPENTRACK_ASSERT_SWIZZLE; return operator()(3); }
template<int P = h_, int Q = w_> std::enable_if_t<maybe_add_swizzle<P, Q, 1>::value, num&>
- x() { OPENTRACK_ASSERT_SWIZZLE; return operator()(0); }
+ constexpr inline x() { OPENTRACK_ASSERT_SWIZZLE; return operator()(0); }
template<int P = h_, int Q = w_> std::enable_if_t<maybe_add_swizzle<P, Q, 2>::value, num&>
- y() { OPENTRACK_ASSERT_SWIZZLE; return operator()(1); }
+ constexpr inline y() { OPENTRACK_ASSERT_SWIZZLE; return operator()(1); }
template<int P = h_, int Q = w_> std::enable_if_t<maybe_add_swizzle<P, Q, 3>::value, num&>
- z() { OPENTRACK_ASSERT_SWIZZLE; return operator()(2); }
+ constexpr inline z() { OPENTRACK_ASSERT_SWIZZLE; return operator()(2); }
template<int P = h_, int Q = w_> std::enable_if_t<maybe_add_swizzle<P, Q, 4>::value, num&>
- w() { OPENTRACK_ASSERT_SWIZZLE; return operator()(3); }
+ constexpr inline w() { OPENTRACK_ASSERT_SWIZZLE; return operator()(3); }
// parameters w_ and h_ are rebound so that SFINAE occurs
// removing them causes a compile-time error -sh 20150811
@@ -119,7 +118,7 @@ public:
const num val = dot(*this);
- if (std::fabs(val) < num(1e-4))
+ if (val < num(1e-4))
return num(0);
else
return std::sqrt(val);
@@ -127,12 +126,12 @@ public:
template<int R, int S, int P = h_, int Q = w_>
std::enable_if_t<is_vector_pair<R, S, P, Q>::value, num>
- dot(const Mat<num, R, S>& p2) const
+ constexpr dot(const Mat<num, R, S>& p2) const
{
static_assert(P == h_ && Q == w_, "");
num ret = 0;
- static constexpr int len = vector_len<R, S>::value;
+ constexpr int len = vector_len<R, S>::value;
for (int i = 0; i < len; i++)
ret += operator()(i) * p2(i);
return ret;
@@ -140,17 +139,17 @@ public:
template<int R, int S, int P = h_, int Q = w_>
std::enable_if_t<is_dim3<P, Q, R, S>::value, Mat<num, is_dim3<P, Q, R, S>::P, is_dim3<P, Q, R, S>::Q>>
- cross(const Mat<num, R, S>& b) const
+ constexpr cross(const Mat<num, R, S>& b) const
{
static_assert(P == h_ && Q == w_, "");
- decltype(*this)& a = *this;
+ auto& a = *this;
return Mat<num, R, S>(a.y()*b.z() - a.z()*b.y(),
a.z()*b.x() - a.x()*b.z(),
a.x()*b.y() - a.y()*b.x());
}
- Mat<num, h_, w_> operator+(const Mat<num, h_, w_>& other) const
+ constexpr Mat<num, h_, w_> operator+(const Mat<num, h_, w_>& other) const
{
Mat<num, h_, w_> ret;
for (int j = 0; j < h_; j++)
@@ -159,7 +158,7 @@ public:
return ret;
}
- Mat<num, h_, w_> operator-(const Mat<num, h_, w_>& other) const
+ constexpr Mat<num, h_, w_> operator-(const Mat<num, h_, w_>& other) const
{
Mat<num, h_, w_> ret;
for (int j = 0; j < h_; j++)
@@ -168,7 +167,7 @@ public:
return ret;
}
- Mat<num, h_, w_> operator+(const num& other) const
+ constexpr Mat<num, h_, w_> operator+(const num& other) const
{
Mat<num, h_, w_> ret;
for (int j = 0; j < h_; j++)
@@ -177,7 +176,7 @@ public:
return ret;
}
- Mat<num, h_, w_> operator-(const num& other) const
+ constexpr Mat<num, h_, w_> operator-(const num& other) const
{
Mat<num, h_, w_> ret;
for (int j = 0; j < h_; j++)
@@ -187,7 +186,7 @@ public:
}
template<int p>
- Mat<num, h_, p> operator*(const Mat<num, w_, p>& other) const
+ constexpr Mat<num, h_, p> operator*(const Mat<num, w_, p>& other) const
{
Mat<num, h_, p> ret;
for (int k = 0; k < h_; k++)
@@ -200,11 +199,11 @@ public:
return ret;
}
- inline num operator()(int j, int i) const { return data[j][i]; }
- inline num& operator()(int j, int i) { return data[j][i]; }
+ constexpr inline num operator()(int j, int i) const { return data[j][i]; }
+ constexpr inline num& operator()(int j, int i) { return data[j][i]; }
- inline num operator()(unsigned j, unsigned i) const { return data[j][i]; }
- inline num& operator()(unsigned j, unsigned i) { return data[j][i]; }
+ constexpr inline num operator()(unsigned j, unsigned i) const { return data[j][i]; }
+ constexpr inline num& operator()(unsigned j, unsigned i) { return data[j][i]; }
#ifdef __GNUG__
# pragma GCC diagnostic push
@@ -213,7 +212,7 @@ public:
template<typename... ts, int h__ = h_, int w__ = w_,
typename = std::enable_if_t<is_arglist_correct<num, h__, w__, ts...>::value>>
- Mat(const ts... xs) : data{static_cast<num>(xs)...}
+ constexpr Mat(const ts... xs) : data{static_cast<num>(xs)...}
{
static_assert(h__ == h_ && w__ == w_, "");
}
@@ -222,7 +221,7 @@ public:
# pragma GCC diagnostic pop
#endif
- Mat()
+ constexpr Mat()
{
for (int j = 0; j < h_; j++)
for (int i = 0; i < w_; i++)
@@ -258,7 +257,7 @@ public:
return ret;
}
- Mat<num, w_, h_> t() const
+ constexpr Mat<num, w_, h_> t() const
{
Mat<num, w_, h_> ret;
@@ -271,13 +270,13 @@ public:
};
template<typename num, int h, int w>
-Mat<num, h, w> operator*(num scalar, const Mat<num, h, w>& mat)
+constexpr Mat<num, h, w> operator*(num scalar, const Mat<num, h, w>& mat)
{
return mat * scalar;
}
template<typename num, int h_, int w_>
-Mat<num, h_, w_> operator*(const Mat<num, h_, w_>& self, num other)
+constexpr Mat<num, h_, w_> operator*(const Mat<num, h_, w_>& self, num other)
{
Mat<num, h_, w_> ret;
for (int j = 0; j < h_; j++)
@@ -285,3 +284,8 @@ Mat<num, h_, w_> operator*(const Mat<num, h_, w_>& self, num other)
ret(j, i) = self(j, i) * other;
return ret;
}
+
+} // ns simple_mat_detail
+
+template<typename num, int h, int w>
+using Mat = simple_mat_detail::Mat<num, h, w>;