mirror of
https://github.com/g-truc/glm.git
synced 2025-04-05 13:35:03 +00:00
Fixed GLM_GTX_vector_angle implementation
This commit is contained in:
parent
bc272cd8df
commit
a1d4b1da22
3 changed files with 61 additions and 88 deletions
|
@ -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>
|
||||
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>
|
||||
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>
|
||||
typename T orientedAngle(
|
||||
detail::tvec2<T> const & x,
|
||||
detail::tvec2<T> const & y);
|
||||
|
||||
//! Returns the oriented angle between x and y
|
||||
//! Parameters need to be normalized.
|
||||
//! From GLM_GTX_vector_angle extension.
|
||||
template <typename vecType>
|
||||
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 T>
|
||||
typename vecType<T> orientedAngle(
|
||||
detail::tvec3<T> const & x,
|
||||
detail::tvec3<T> const & y,
|
||||
detail::tvec3<T> 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 <template<typename> class vecType, typename T>
|
||||
typename vecType<T> orientedAngleFromRef(
|
||||
vecType<T> const & x,
|
||||
vecType<T> const & y,
|
||||
detail::tvec3<T> const & ref);
|
||||
|
||||
///@}
|
||||
}//namespace vector_angle
|
||||
}//namespace gtx
|
||||
///@}
|
||||
}//namespace vector_angle
|
||||
}//namespace gtx
|
||||
}//namespace glm
|
||||
|
||||
#include "vector_angle.inl"
|
||||
|
|
|
@ -29,62 +29,28 @@ GLM_FUNC_QUALIFIER valType orientedAngle
|
|||
detail::tvec2<valType> const & y
|
||||
)
|
||||
{
|
||||
valType Angle = acos(dot(x, y));
|
||||
valType c = cos(Angle);
|
||||
valType s = sin(Angle);
|
||||
detail::tvec2<valType> TransformedVector = detail::tvec2<valType>(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<valType> 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 <typename valType>
|
||||
template <typename valType>
|
||||
GLM_FUNC_QUALIFIER valType orientedAngle
|
||||
(
|
||||
detail::tvec3<valType> const & x,
|
||||
detail::tvec3<valType> const & y
|
||||
)
|
||||
{
|
||||
valType Angle = degrees(acos(dot(x, y)));
|
||||
detail::tvec3<valType> 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 <typename valType>
|
||||
GLM_FUNC_QUALIFIER valType orientedAngleFromRef
|
||||
(
|
||||
detail::tvec2<valType> const & x,
|
||||
detail::tvec2<valType> const & y,
|
||||
detail::tvec3<valType> const & ref
|
||||
)
|
||||
{
|
||||
valType Angle = glm::acos(glm::dot(x, y));
|
||||
|
||||
if(glm::dot(ref, detail::tvec3<valType>(glm::cross(x, y), valType(0))) < valType(0))
|
||||
return -glm::degrees(Angle);
|
||||
else
|
||||
return glm::degrees(Angle);
|
||||
}
|
||||
|
||||
template <typename valType>
|
||||
GLM_FUNC_QUALIFIER valType orientedAngleFromRef
|
||||
(
|
||||
detail::tvec3<valType> const & x,
|
||||
detail::tvec3<valType> const & y,
|
||||
detail::tvec3<valType> 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
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue