/* Copyright (c) 2014-2015, 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 { // 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) }; }; 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 { num data[h_][w_]; static_assert(h_ > 0 && w_ > 0, "must have positive mat dimensions"); Mat(std::initializer_list&& xs) = delete; public: // parameters w_ and h_ are rebound so that SFINAE occurs // removing them causes a compile-time error -sh 20150811 template typename std::enable_if::value, num>::type inline operator()(int i) const { return data[i][0]; } template typename std::enable_if::value, num>::type inline operator()(int i) const { return data[0][i]; } template typename std::enable_if::value, num&>::type inline operator()(int i) { return data[i][0]; } template typename std::enable_if::value, num&>::type inline operator()(int i) { return data[0][i]; } template typename std::enable_if::value, num>::type inline x() const { return operator()(0); } template typename std::enable_if::value, num>::type inline y() const { return operator()(1); } template typename std::enable_if::value, num>::type inline z() const { return operator()(2); } template typename std::enable_if::value, num>::type inline w() const { return operator()(3); } template typename std::enable_if::value, num&>::type inline x() { return operator()(0); } template typename std::enable_if::value, num&>::type inline y() { return operator()(1); } template typename std::enable_if::value, num&>::type inline z() { return operator()(2); } template typename std::enable_if::value, num&>::type inline w() { return operator()(3); } template typename std::enable_if::value, num>::type dot(const Mat& p2) const { num ret = 0; constexpr int len = vector_len::value; for (int i = 0; i < len; i++) ret += operator()(i) * p2(i); return ret; } template typename std::enable_if::value, Mat::P, is_dim3::Q>>::type cross(const Mat& p2) const { return Mat(y() * p2.z() - p2.y() * z(), p2.x() * z() - x() * p2.z(), x() * p2.y() - y() * p2.x()); } 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; } 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; } 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; } 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; } 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 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; } inline num operator()(int j, int i) const { return data[j][i]; } inline num& operator()(int j, int i) { return data[j][i]; } template::value>::type> Mat(const ts... xs) { const std::initializer_list init = { static_cast(xs)... }; auto iter = init.begin(); for (int j = 0; j < h_; j++) for (int i = 0; i < w_; i++) data[j][i] = *iter++; } 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]; } Mat(num* mem) : Mat(const_cast(mem)) {} template::type> operator const double*() const { return reinterpret_cast(data); } // XXX add more operators as needed, third-party dependencies mostly // not needed merely for matrix algebra -sh 20141030 static Mat eye() { 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; } 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; } }; namespace euler { template using dmat = Mat; using rmat = dmat<3, 3>; using euler_t = dmat<3, 1>; euler_t euler_filter(const euler_t& rot_); rmat euler_to_rmat(const double* input); // http://stackoverflow.com/a/18436193 euler_t rmat_to_euler(const dmat<3, 3>& R); } // end ns euler template using dmat = Mat;