From 56bd959f9e5ce37ff1578127eb64e9274031371d Mon Sep 17 00:00:00 2001 From: Janis Born Date: Thu, 19 Dec 2013 16:16:28 +0100 Subject: [PATCH] fix vector angle functions possibly returning NaN --- glm/gtx/vector_angle.inl | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/glm/gtx/vector_angle.inl b/glm/gtx/vector_angle.inl index 39e04f22..737e1cef 100644 --- a/glm/gtx/vector_angle.inl +++ b/glm/gtx/vector_angle.inl @@ -18,10 +18,12 @@ namespace glm { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'angle' only accept floating-point inputs"); + genType Dot = clamp(dot(x, y), genType(0), genType(1)); + #ifdef GLM_FORCE_RADIANS - return acos(dot(x, y)); + return acos(Dot); #else - return degrees(acos(dot(x, y))); + return degrees(acos(Dot)); #endif } @@ -34,10 +36,12 @@ namespace glm { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'angle' only accept floating-point inputs"); + T Dot = clamp(dot(x, y), T(0), T(1)); + #ifdef GLM_FORCE_RADIANS - return acos(dot(x, y)); + return acos(Dot); #else - return degrees(acos(dot(x, y))); + return degrees(acos(Dot)); #endif } @@ -51,10 +55,12 @@ namespace glm { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'orientedAngle' only accept floating-point inputs"); + T Dot = clamp(dot(x, y), T(0), T(1)); + #ifdef GLM_FORCE_RADIANS - T const Angle(acos(dot(x, y))); + T const Angle(acos(Dot)); #else - T const Angle(degrees(acos(dot(x, y)))); + T const Angle(degrees(acos(Dot))); #endif detail::tvec2 const TransformedVector(glm::rotate(x, Angle)); if(all(epsilonEqual(y, TransformedVector, T(0.01)))) @@ -73,10 +79,12 @@ namespace glm { GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'orientedAngle' only accept floating-point inputs"); + T Dot = clamp(dot(x, y), T(0), T(1)); + #ifdef GLM_FORCE_RADIANS - T const Angle(acos(dot(x, y))); + T const Angle(acos(Dot)); #else - T const Angle(degrees(acos(dot(x, y)))); + T const Angle(degrees(acos(Dot))); #endif if(dot(ref, cross(x, y)) < T(0))