/* Copyright (c) 2014-2016, Stanislaw Halik * Permission to use, copy, modify, and/or distribute this * software for any purpose with or without fee is hereby granted, * provided that the above copyright notice and this permission * notice appear in all copies. */ #pragma once #include #include #include namespace simple_mat { // last param to fool SFINAE into overloading template struct equals { enum { value = i == j }; }; template struct maybe_add_swizzle { enum { value = (i == 1 || j == 1) && (i >= min || j >= min) && (i <= max || j <= max) }; }; template struct is_vector { enum { value = j == 1 || i == 1 }; }; template struct is_vector_pair { enum { value = (i1 == i2 && j1 == 1 && j2 == 1) || (j1 == j2 && i1 == 1 && i2 == 1) }; }; template struct vector_len { enum { value = i > j ? i : j }; }; template struct is_dim3 { enum { value = (a == 1 && c == 1 && b == 3 && d == 3) || (a == 3 && c == 3 && b == 1 && d == 1) }; enum { P = a == 1 ? 1 : 3 }; enum { Q = a == 1 ? 3 : 1 }; }; template struct is_arglist_correct { enum { value = h * w == sizeof...(ts) }; }; template class Mat { static_assert(H > 0 && W > 0, "must have positive mat dimensions"); num data[H][W]; 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()(t i) const& { return data[(unsigned)i][0]; } 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()(t i) & { return data[(unsigned)i][0]; } 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) // const variants template std::enable_if_t::value, num> constexpr inline x() const& { OTR_MAT_ASSERT_SWIZZLE; return operator()(0); } template std::enable_if_t::value, num> constexpr inline y() const& { OTR_MAT_ASSERT_SWIZZLE; return operator()(1); } template std::enable_if_t::value, num> constexpr inline z() const& { OTR_MAT_ASSERT_SWIZZLE; return operator()(2); } template std::enable_if_t::value, num> constexpr inline w() const& { OTR_MAT_ASSERT_SWIZZLE; return operator()(3); } // mutable variants template std::enable_if_t::value, num&> constexpr inline x() & { OTR_MAT_ASSERT_SWIZZLE; return operator()(0); } template std::enable_if_t::value, num&> constexpr inline y() & { OTR_MAT_ASSERT_SWIZZLE; return operator()(1); } template std::enable_if_t::value, num&> constexpr inline z() & { OTR_MAT_ASSERT_SWIZZLE; return operator()(2); } template std::enable_if_t::value, num&> constexpr inline w() & { OTR_MAT_ASSERT_SWIZZLE; return operator()(3); } template 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 < eps) return num(0); else return val; } inline auto norm() const { return num(std::sqrt(norm_squared())); } template std::enable_if_t::value, num> constexpr dot(const Mat& p2) const { static_assert(P == H && Q == W); num ret = 0; constexpr int len = vector_len::value; for (int i = 0; i < len; i++) ret += operator()(i) * p2(i); return ret; } template std::enable_if_t::value, Mat::P, is_dim3::Q>> constexpr cross(const Mat& b) const { static_assert(P == H && Q == W); const auto& a = *this; return Mat(a.y()*b.z() - a.z()*b.y(), a.z()*b.x() - a.x()*b.z(), a.x()*b.y() - a.y()*b.x()); } constexpr Mat operator+(const Mat& other) const { Mat ret; for (int j = 0; j < H; j++) for (int i = 0; i < W; i++) ret(j, i) = data[j][i] + other.data[j][i]; return ret; } constexpr Mat operator-(const Mat& other) const { Mat ret; for (int j = 0; j < H; j++) for (int i = 0; i < W; i++) ret(j, i) = data[j][i] - other.data[j][i]; return ret; } constexpr Mat operator+(const num other) const { Mat ret; for (int j = 0; j < H; j++) for (int i = 0; i < W; i++) ret(j, i) = data[j][i] + other; return ret; } constexpr Mat operator-(const num other) const { Mat ret; for (int j = 0; j < H; j++) for (int i = 0; i < W; i++) ret(j, i) = data[j][i] - other; return ret; } template constexpr Mat operator*(const Mat& other) const { Mat ret; for (int k = 0; k < H; k++) for (int i = 0; i < p; i++) { ret(k, i) = 0; for (int j = 0; j < W; j++) ret(k, i) += data[k][j] * other(j, i); } return ret; } constexpr Mat mult_elementwise(const Mat& other) const& { Mat ret; for (unsigned j = 0; j < H; j++) for (unsigned i = 0; i < W; i++) ret(j, i) = data[j][i] * other.data[j][i]; return ret; } template 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[(unsigned)j][(unsigned)i]; } #ifdef __clang__ # pragma clang diagnostic push # pragma clang diagnostic ignored "-Wmissing-braces" #endif template::value>> constexpr Mat(const ts... xs) : data{static_cast(xs)...} { static_assert(h2 == H && w2 == W); } #ifdef __clang__ # pragma clang diagnostic pop #endif constexpr Mat() { for (int j = 0; j < H; j++) for (int i = 0; i < W; i++) data[j][i] = num(0); } Mat(const num* mem) { for (int j = 0; j < H; j++) for (int i = 0; i < W; i++) data[j][i] = mem[i*H+j]; } 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 template static std::enable_if_t> eye() { static_assert(H == H_); Mat ret; for (int j = 0; j < H; j++) for (int i = 0; i < W; i++) ret.data[j][i] = 0; for (int i = 0; i < H; i++) ret.data[i][i] = 1; return ret; } constexpr Mat t() const { Mat ret; for (int j = 0; j < H; j++) for (int i = 0; i < W; i++) ret(i, j) = data[j][i]; 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; } }; } // ns simple_mat template using Mat = simple_mat::Mat; template constexpr Mat operator*(num scalar, const Mat& mat) { return mat * scalar; } template constexpr Mat operator*(const Mat& self, num other) { Mat ret; for (int j = 0; j < H; j++) for (int i = 0; i < W; i++) ret(j, i) = self(j, i) * other; return ret; }