From 277266b9970d0cdd19734d98b953d87ad585c749 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 1 Jun 2015 17:44:07 +0200 Subject: simple-mat: implement dot and cross product --- opentrack/simple-mat.hpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'opentrack') diff --git a/opentrack/simple-mat.hpp b/opentrack/simple-mat.hpp index 649d7086..9518af90 100644 --- a/opentrack/simple-mat.hpp +++ b/opentrack/simple-mat.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include @@ -14,6 +15,23 @@ namespace { { 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 @@ -57,6 +75,25 @@ struct Mat template typename std::enable_if::value, num&>::type __inline w() { return operator()(3); } + template + typename std::enable_if::value, num>::type + __inline 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.operator ()(i); + return ret; + } + + template + typename std::enable_if::value, Mat::P, is_dim3::Q>>::type + __inline 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; -- cgit v1.2.3