From 17816021b9f53e667867ac8c097208b3ddbdd6a9 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 13 Mar 2018 14:36:38 +1100 Subject: [PATCH] coord/ops: add std::{cos,sin} overloads for coords --- coord/ops.hpp | 26 ++++++++++++++++---------- maths.hpp | 10 ++++++++++ quaternion.cpp | 7 +++---- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/coord/ops.hpp b/coord/ops.hpp index d516372d..8c55da9e 100644 --- a/coord/ops.hpp +++ b/coord/ops.hpp @@ -1404,11 +1404,11 @@ namespace std { /////////////////////////////////////////////////////////////////////////////// -#include - #include "../hash.hpp" + namespace std { + //------------------------------------------------------------------------- template < std::size_t S, typename T, @@ -1427,6 +1427,8 @@ namespace std { } }; + + //------------------------------------------------------------------------- template < typename CoordT, typename = std::enable_if_t< @@ -1435,16 +1437,20 @@ namespace std { > auto cos (CoordT val) { - CoordT out; + return ::util::apply (::util::cos, val); + } - std::transform ( - std::begin (val), - std::end (val), - std::begin (out), - [] (auto i) { return ::std::cos (i); } - ); - return out; + //------------------------------------------------------------------------- + template < + typename CoordT, + typename = std::enable_if_t< + ::util::is_coord_v, void + > + > + auto sin (CoordT val) + { + return ::util::apply (::util::sin, val); } }; diff --git a/maths.hpp b/maths.hpp index b23fa55d..1794c076 100644 --- a/maths.hpp +++ b/maths.hpp @@ -508,6 +508,16 @@ namespace util { } + //------------------------------------------------------------------------- + // thin wrappers around std trig identities. + // + // we have these because it's a little easier to qualify templates when + // passing function objects as compared to explicitly disambiguating raw + // functions (ie, with casts or typedefs). + template ValueT cos (ValueT theta) { return ::std::cos (theta); } + template ValueT sin (ValueT theta) { return ::std::sin (theta); } + template ValueT tan (ValueT theta) { return ::std::tan (theta); } + /////////////////////////////////////////////////////////////////////////////// // combinatorics diff --git a/quaternion.cpp b/quaternion.cpp index 3d0d7975..0aa2dfdd 100644 --- a/quaternion.cpp +++ b/quaternion.cpp @@ -19,8 +19,7 @@ #include "debug.hpp" #include "vector.hpp" - -#include +#include "coord/ops.hpp" /////////////////////////////////////////////////////////////////////////////// @@ -50,8 +49,8 @@ quaternion::from_euler (vector<3,T> angles) { auto half = angles / 2; - auto c = cos (half); - auto s = sin (half); + auto c = std::cos (half); + auto s = std::sin (half); return { c.x * c.y * c.z - s.x * s.y * s.z,