diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2014-12-15 21:47:35 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2014-12-15 21:47:35 +0100 |
commit | 769d1340d3a76547a2252f52eb0a8615015f20e0 (patch) | |
tree | 0d676fa2f3028bca75796fd983c78bc062452ed3 /opentrack/simple-mat.hpp | |
parent | 8e01d0feed376f5e93698fb820e224014d8d9532 (diff) |
move rmat->euler and euler->rmat conversions to header
Diffstat (limited to 'opentrack/simple-mat.hpp')
-rw-r--r-- | opentrack/simple-mat.hpp | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/opentrack/simple-mat.hpp b/opentrack/simple-mat.hpp index b36b21ba..447327a8 100644 --- a/opentrack/simple-mat.hpp +++ b/opentrack/simple-mat.hpp @@ -75,7 +75,63 @@ struct Mat return ret; } + + template<int h_, int w_> using dmat = Mat<double, h_, w_>; + + // http://stackoverflow.com/a/18436193 + static dmat<3, 1> rmat_to_euler(const dmat<3, 3>& R) + { + static constexpr double pi = 3.141592653; + const double up = 90 * pi / 180.; + static constexpr double bound = 1. - 2e-4; + if (R(0, 2) > bound) + { + double roll = atan(R(1, 0) / R(2, 0)); + return dmat<3, 1>({0., up, roll}); + } + if (R(0, 2) < -bound) + { + double roll = atan(R(1, 0) / R(2, 0)); + return dmat<3, 1>({0., -up, roll}); + } + double pitch = asin(-R(0, 2)); + double roll = atan2(R(1, 2), R(2, 2)); + double yaw = atan2(R(0, 1), R(0, 0)); + return dmat<3, 1>({yaw, pitch, roll}); + } + + // tait-bryan angles, not euler + static dmat<3, 3> euler_to_rmat(const double* input) + { + static constexpr double pi = 3.141592653; + auto H = input[0] * pi / 180; + auto P = input[1] * pi / 180; + auto B = input[2] * pi / 180; + + const auto c1 = cos(H); + const auto s1 = sin(H); + const auto c2 = cos(P); + const auto s2 = sin(P); + const auto c3 = cos(B); + const auto s3 = sin(B); + + double foo[] = { + // z + c1 * c2, + c1 * s2 * s3 - c3 * s1, + s1 * s3 + c1 * c3 * s2, + // y + c2 * s1, + c1 * c3 + s1 * s2 * s3, + c3 * s1 * s2 - c1 * s3, + // x + -s2, + c2 * s3, + c2 * c3 + }; + + return dmat<3, 3>(foo); + } }; template<int h, int w> using dmat = Mat<double, h, w>; - |