From b0c1777b5745f16ab535a812c98c4e8c085d03e6 Mon Sep 17 00:00:00 2001 From: Mathias Labeyrie Date: Wed, 5 Nov 2014 10:58:56 +0100 Subject: [PATCH 1/4] More constants related to pi --- glm/gtc/constants.hpp | 20 ++++++++++++++++++++ glm/gtc/constants.inl | 24 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/glm/gtc/constants.hpp b/glm/gtc/constants.hpp index 76fc1eb4..fc721f10 100644 --- a/glm/gtc/constants.hpp +++ b/glm/gtc/constants.hpp @@ -71,6 +71,11 @@ namespace glm template GLM_FUNC_DECL genType pi(); + /// Return pi * 2. + /// @see gtc_constants + template + GLM_FUNC_DECL genType two_pi(); + /// Return square root of pi. /// @see gtc_constants template @@ -81,6 +86,11 @@ namespace glm template GLM_FUNC_DECL genType half_pi(); + /// Return pi / 2 * 3. + /// @see gtc_constants + template + GLM_FUNC_DECL genType three_half_pi(); + /// Return pi / 4. /// @see gtc_constants template @@ -91,11 +101,21 @@ namespace glm template GLM_FUNC_DECL genType one_over_pi(); + /// Return 1 / (pi * 2). + /// @see gtc_constants + template + GLM_FUNC_DECL genType one_over_two_pi(); + /// Return 2 / pi. /// @see gtc_constants template GLM_FUNC_DECL genType two_over_pi(); + /// Return 4 / pi. + /// @see gtc_constants + template + GLM_FUNC_DECL genType four_over_pi(); + /// Return 2 / sqrt(pi). /// @see gtc_constants template diff --git a/glm/gtc/constants.inl b/glm/gtc/constants.inl index a9ea0095..314ebfbc 100644 --- a/glm/gtc/constants.inl +++ b/glm/gtc/constants.inl @@ -54,6 +54,12 @@ namespace glm return genType(3.14159265358979323846264338327950288); } + template + GLM_FUNC_QUALIFIER genType two_pi() + { + return genType(6.28318530717958647692528676655900576); + } + template GLM_FUNC_QUALIFIER genType root_pi() { @@ -66,6 +72,12 @@ namespace glm return genType(1.57079632679489661923132169163975144); } + template + GLM_FUNC_QUALIFIER genType three_half_pi() + { + return genType(4.71238898038468985769396507491925432); + } + template GLM_FUNC_QUALIFIER genType quarter_pi() { @@ -78,12 +90,24 @@ namespace glm return genType(0.318309886183790671537767526745028724); } + template + GLM_FUNC_QUALIFIER genType one_over_two_pi() + { + return genType(0.159154943091895335768883763372514362); + } + template GLM_FUNC_QUALIFIER genType two_over_pi() { return genType(0.636619772367581343075535053490057448); } + template + GLM_FUNC_QUALIFIER genType four_over_pi() + { + return genType(1.273239544735162686151070106980114898); + } + template GLM_FUNC_QUALIFIER genType two_over_root_pi() { From 710e13a8e62015da18f7cbbfc9bc9d82b2807b54 Mon Sep 17 00:00:00 2001 From: Mathias Labeyrie Date: Wed, 5 Nov 2014 10:55:42 +0100 Subject: [PATCH 2/4] Much more precise fastCos and fastSin fastCos and fastSin had a max error of ~0.2 on [-pi pi]. The updated version is ~0.000007. --- glm/gtx/fast_trigonometry.hpp | 10 +++++++--- glm/gtx/fast_trigonometry.inl | 33 ++++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/glm/gtx/fast_trigonometry.hpp b/glm/gtx/fast_trigonometry.hpp index f89a6df6..17b04cfb 100644 --- a/glm/gtx/fast_trigonometry.hpp +++ b/glm/gtx/fast_trigonometry.hpp @@ -39,6 +39,7 @@ // Dependency: #include "../glm.hpp" +#include "../gtc/constants.hpp" #if(defined(GLM_MESSAGES) && !defined(GLM_EXT_INCLUDED)) # pragma message("GLM: GLM_GTX_fast_trigonometry extension included") @@ -49,14 +50,17 @@ namespace glm /// @addtogroup gtx_fast_trigonometry /// @{ - //! Faster than the common sin function but less accurate. - //! Defined between -2pi and 2pi. + //! Wrap an angle to [0 2pi[ //! From GLM_GTX_fast_trigonometry extension. template + GLM_FUNC_DECL T wrapAngle(const T& angle); + + //! Faster than the common sin function but less accurate. + //! From GLM_GTX_fast_trigonometry extension. + template GLM_FUNC_DECL T fastSin(const T& angle); //! Faster than the common cos function but less accurate. - //! Defined between -2pi and 2pi. //! From GLM_GTX_fast_trigonometry extension. template GLM_FUNC_DECL T fastCos(const T& angle); diff --git a/glm/gtx/fast_trigonometry.inl b/glm/gtx/fast_trigonometry.inl index b68e8fd5..632be5d2 100644 --- a/glm/gtx/fast_trigonometry.inl +++ b/glm/gtx/fast_trigonometry.inl @@ -9,24 +9,47 @@ namespace glm { - // sin template - GLM_FUNC_QUALIFIER T fastSin(T const & x) + GLM_FUNC_QUALIFIER T wrapAngle(T const & angle) { - return x - ((x * x * x) / T(6)) + ((x * x * x * x * x) / T(120)) - ((x * x * x * x * x * x * x) / T(5040)); + T result = angle - floor(angle * one_over_two_pi()) * two_pi(); + result = result > T(0) ? result : -result; + return result; } - VECTORIZE_VEC(fastSin) + VECTORIZE_VEC(wrapAngle) + + template + GLM_FUNC_QUALIFIER T cos_52s(T const & x) + { + T const xx(x * x); + return (T(0.9999932946) + xx * (T(-0.4999124376) + xx * (T(0.0414877472) + xx * T(-0.0012712095)))); + } + + VECTORIZE_VEC(cos_52s) // cos template GLM_FUNC_QUALIFIER T fastCos(T const & x) { - return T(1) - (x * x * T(0.5)) + (x * x * x * x * T(0.041666666666)) - (x * x * x * x * x * x * T(0.00138888888888)); + T const angle(wrapAngle(x)); + if(angle()) return cos_52s(angle); + if(angle()) return -cos_52s(pi() - angle); + if(angle<(T(3) * half_pi())) return -cos_52s(angle - pi()); + return cos_52s(two_pi() - angle); } VECTORIZE_VEC(fastCos) + // sin + template + GLM_FUNC_QUALIFIER T fastSin(T const & x) + { + return fastCos(half_pi() - x); + } + + VECTORIZE_VEC(fastSin) + // tan template GLM_FUNC_QUALIFIER T fastTan(T const & x) From b38a8b4e5ae4f8b02fb284a95a25a675923ba778 Mon Sep 17 00:00:00 2001 From: Mathias Labeyrie Date: Wed, 5 Nov 2014 13:51:16 +0100 Subject: [PATCH 3/4] rename three_half_pi() => three_over_two_pi() --- glm/gtc/constants.hpp | 2 +- glm/gtc/constants.inl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/glm/gtc/constants.hpp b/glm/gtc/constants.hpp index fc721f10..a39c9bd1 100644 --- a/glm/gtc/constants.hpp +++ b/glm/gtc/constants.hpp @@ -89,7 +89,7 @@ namespace glm /// Return pi / 2 * 3. /// @see gtc_constants template - GLM_FUNC_DECL genType three_half_pi(); + GLM_FUNC_DECL genType three_over_two_pi(); /// Return pi / 4. /// @see gtc_constants diff --git a/glm/gtc/constants.inl b/glm/gtc/constants.inl index 314ebfbc..076375c7 100644 --- a/glm/gtc/constants.inl +++ b/glm/gtc/constants.inl @@ -73,7 +73,7 @@ namespace glm } template - GLM_FUNC_QUALIFIER genType three_half_pi() + GLM_FUNC_QUALIFIER genType three_over_two_pi() { return genType(4.71238898038468985769396507491925432); } From 72a5de232099fbca32d573a76349dbb51f7851ef Mon Sep 17 00:00:00 2001 From: Mathias Labeyrie Date: Wed, 5 Nov 2014 13:57:32 +0100 Subject: [PATCH 4/4] branchless wrapAngle --- glm/gtx/fast_trigonometry.inl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/glm/gtx/fast_trigonometry.inl b/glm/gtx/fast_trigonometry.inl index 632be5d2..b22487d5 100644 --- a/glm/gtx/fast_trigonometry.inl +++ b/glm/gtx/fast_trigonometry.inl @@ -9,12 +9,10 @@ namespace glm { - template + template GLM_FUNC_QUALIFIER T wrapAngle(T const & angle) { - T result = angle - floor(angle * one_over_two_pi()) * two_pi(); - result = result > T(0) ? result : -result; - return result; + return abs(mod(angle, two_pi())); } VECTORIZE_VEC(wrapAngle)