diff --git a/glm/gtx/vector_angle.hpp b/glm/gtx/vector_angle.hpp index 3975ea7e..154fc793 100644 --- a/glm/gtx/vector_angle.hpp +++ b/glm/gtx/vector_angle.hpp @@ -25,50 +25,45 @@ # pragma message("GLM: GLM_GTX_vector_angle extension included") #endif -namespace glm +namespace glm{ +namespace gtx{ +//! GLM_GTX_vector_angle extension: Compute angle between vectors +namespace vector_angle { - namespace test{ - void main_gtx_vector_angle(); - }//namespace test + using namespace quaternion; + using namespace epsilon; - namespace gtx{ - //! GLM_GTX_vector_angle extension: Compute angle between vectors - namespace vector_angle - { - using namespace quaternion; - using namespace epsilon; + /// \addtogroup gtx_vector_angle + ///@{ - /// \addtogroup gtx_vector_angle - ///@{ + //! Returns the absolute angle between two vectors + //! Parameters need to be normalized. + //! From GLM_GTX_vector_angle extension + template + typename vecType::value_type angle( + vecType const & x, + vecType const & y); - //! Returns the absolute angle between x and y. - //! Parameters need to be normalized. - //! From GLM_GTX_vector_angle extension - template - typename vecType::value_type angle( - vecType const & x, - vecType const & y); + //! Returns the oriented angle between two 2d vectors + //! Parameters need to be normalized. + //! From GLM_GTX_vector_angle extension. + template + typename T orientedAngle( + detail::tvec2 const & x, + detail::tvec2 const & y); - //! Returns the oriented angle between x and y - //! Parameters need to be normalized. - //! From GLM_GTX_vector_angle extension. - template - typename vecType::value_type orientedAngle( - vecType const & x, - vecType const & y); + //! Returns the oriented angle between two 3d vectors based from a reference axis. + //! Parameters need to be normalized. + //! From GLM_GTX_vector_angle extension. + template + typename vecType orientedAngle( + detail::tvec3 const & x, + detail::tvec3 const & y, + detail::tvec3 const & ref); - //! Returns the orientation of a two vector base from a normal. - //! Parameters need to be normalized. - //! From GLM_GTX_vector_angle extension. - template class vecType, typename T> - typename vecType orientedAngleFromRef( - vecType const & x, - vecType const & y, - detail::tvec3 const & ref); - - ///@} - }//namespace vector_angle - }//namespace gtx + ///@} +}//namespace vector_angle +}//namespace gtx }//namespace glm #include "vector_angle.inl" diff --git a/glm/gtx/vector_angle.inl b/glm/gtx/vector_angle.inl index 5e8a0066..05df114b 100644 --- a/glm/gtx/vector_angle.inl +++ b/glm/gtx/vector_angle.inl @@ -29,62 +29,28 @@ GLM_FUNC_QUALIFIER valType orientedAngle detail::tvec2 const & y ) { - valType Angle = acos(dot(x, y)); - valType c = cos(Angle); - valType s = sin(Angle); - detail::tvec2 TransformedVector = detail::tvec2(c * y.x - s * y.y, s * y.x + c * y.y); - if(all(equalEpsilon(x, TransformedVector, valType(0.01)))) - return -glm::degrees(Angle); + valType Angle = glm::degrees(acos(dot(x, y))); + detail::tvec2 TransformedVector = glm::gtx::rotate_vector::rotate(x, Angle); + if(all(equalEpsilon(y, TransformedVector, valType(0.01)))) + return Angle; else - return glm::degrees(Angle); + return -Angle; } -//! \todo epsilon is hard coded to 0.01 -template +template GLM_FUNC_QUALIFIER valType orientedAngle -( - detail::tvec3 const & x, - detail::tvec3 const & y -) -{ - valType Angle = degrees(acos(dot(x, y))); - detail::tvec3 TransformedVector = glm::gtx::rotate_vector::rotate(y, Angle, glm::core::function::geometric::cross(x, y)); - if(all(equalEpsilon(x, TransformedVector, valType(0.01)))) - return -Angle; - else - return Angle; -} - -template -GLM_FUNC_QUALIFIER valType orientedAngleFromRef -( - detail::tvec2 const & x, - detail::tvec2 const & y, - detail::tvec3 const & ref -) -{ - valType Angle = glm::acos(glm::dot(x, y)); - - if(glm::dot(ref, detail::tvec3(glm::cross(x, y), valType(0))) < valType(0)) - return -glm::degrees(Angle); - else - return glm::degrees(Angle); -} - -template -GLM_FUNC_QUALIFIER valType orientedAngleFromRef ( detail::tvec3 const & x, detail::tvec3 const & y, detail::tvec3 const & ref ) { - valType Angle = glm::acos(glm::dot(x, y)); + valType Angle = glm::degrees(glm::acos(glm::dot(x, y))); if(glm::dot(ref, glm::cross(x, y)) < valType(0)) - return -glm::degrees(Angle); + return -Angle; else - return glm::degrees(Angle); + return Angle; } }//namespace vector_angle diff --git a/test/gtx/gtx_vector_angle.cpp b/test/gtx/gtx_vector_angle.cpp index 20b5bc96..300c8099 100644 --- a/test/gtx/gtx_vector_angle.cpp +++ b/test/gtx/gtx_vector_angle.cpp @@ -19,12 +19,12 @@ int test_vector_angle_calls() float AngleA = glm::angle(glm::vec3(1, 0, 0), glm::vec3(0, 1, 0)); float AngleB = glm::orientedAngle(glm::vec2(1, 0), glm::normalize(glm::vec2(1, 1))); float AngleC = glm::orientedAngle(glm::vec2(0, 1), glm::normalize(glm::vec2(1, 1))); - float AngleD = glm::orientedAngleFromRef(glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1)); + float AngleD = glm::orientedAngle(glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1)); return Error; } -int test_vector_angle_orientedAngle() +int test_orientedAngle_vec2() { int Error = 0; @@ -32,11 +32,22 @@ int test_vector_angle_orientedAngle() Error += AngleA == 45.f ? 0 : 1; float AngleB = glm::orientedAngle(glm::vec2(0, 1), glm::normalize(glm::vec2(1, 1))); Error += AngleB == -45.f ? 0 : 1; - - float AngleC = glm::orientedAngle(glm::vec3(1, 0, 0), glm::normalize(glm::vec3(1, 1, 0))); + float AngleC = glm::orientedAngle(glm::normalize(glm::vec2(1, 1)), glm::vec2(0, 1)); + Error += AngleC == 45.f ? 0 : 1; + + return Error; +} + +int test_orientedAngle_vec3() +{ + int Error = 0; + + float AngleA = glm::orientedAngle(glm::vec3(1, 0, 0), glm::normalize(glm::vec3(1, 1, 0)), glm::vec3(0, 0, 1)); + Error += AngleA == 45.f ? 0 : 1; + float AngleB = glm::orientedAngle(glm::vec3(0, 1, 0), glm::normalize(glm::vec3(1, 1, 0)), glm::vec3(0, 0, 1)); + Error += AngleB == -45.f ? 0 : 1; + float AngleC = glm::orientedAngle(glm::normalize(glm::vec3(1, 1, 0)), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1)); Error += AngleC == 45.f ? 0 : 1; - float AngleD = glm::orientedAngle(glm::vec3(0, 1, 0), glm::normalize(glm::vec3(1, 1, 0))); - Error += AngleD == -45.f ? 0 : 1; return Error; } @@ -44,7 +55,8 @@ int test_vector_angle_orientedAngle() int main() { int Error = 0; - Error += test_vector_angle_orientedAngle(); + Error += test_orientedAngle_vec2(); + Error += test_orientedAngle_vec3(); Error += test_vector_angle_calls(); return Error;