norm.inl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /// @ref gtx_norm
  2. #include "../detail/qualifier.hpp"
  3. namespace glm{
  4. namespace detail
  5. {
  6. template<length_t L, typename T, qualifier Q, bool Aligned>
  7. struct compute_length2
  8. {
  9. GLM_FUNC_QUALIFIER static T call(vec<L, T, Q> const& v)
  10. {
  11. return dot(v, v);
  12. }
  13. };
  14. }//namespace detail
  15. template<typename genType>
  16. GLM_FUNC_QUALIFIER genType length2(genType x)
  17. {
  18. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'length2' accepts only floating-point inputs");
  19. return x * x;
  20. }
  21. template<length_t L, typename T, qualifier Q>
  22. GLM_FUNC_QUALIFIER T length2(vec<L, T, Q> const& v)
  23. {
  24. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'length2' accepts only floating-point inputs");
  25. return detail::compute_length2<L, T, Q, detail::is_aligned<Q>::value>::call(v);
  26. }
  27. template<typename T>
  28. GLM_FUNC_QUALIFIER T distance2(T p0, T p1)
  29. {
  30. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'distance2' accepts only floating-point inputs");
  31. return length2(p1 - p0);
  32. }
  33. template<length_t L, typename T, qualifier Q>
  34. GLM_FUNC_QUALIFIER T distance2(vec<L, T, Q> const& p0, vec<L, T, Q> const& p1)
  35. {
  36. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'distance2' accepts only floating-point inputs");
  37. return length2(p1 - p0);
  38. }
  39. template<typename T, qualifier Q>
  40. GLM_FUNC_QUALIFIER T l1Norm(vec<3, T, Q> const& a, vec<3, T, Q> const& b)
  41. {
  42. return abs(b.x - a.x) + abs(b.y - a.y) + abs(b.z - a.z);
  43. }
  44. template<typename T, qualifier Q>
  45. GLM_FUNC_QUALIFIER T l1Norm(vec<3, T, Q> const& v)
  46. {
  47. return abs(v.x) + abs(v.y) + abs(v.z);
  48. }
  49. template<typename T, qualifier Q>
  50. GLM_FUNC_QUALIFIER T l2Norm(vec<3, T, Q> const& a, vec<3, T, Q> const& b
  51. )
  52. {
  53. return length(b - a);
  54. }
  55. template<typename T, qualifier Q>
  56. GLM_FUNC_QUALIFIER T l2Norm(vec<3, T, Q> const& v)
  57. {
  58. return length(v);
  59. }
  60. template<typename T, qualifier Q>
  61. GLM_FUNC_QUALIFIER T lxNorm(vec<3, T, Q> const& x, vec<3, T, Q> const& y, unsigned int Depth)
  62. {
  63. return pow(pow(y.x - x.x, T(Depth)) + pow(y.y - x.y, T(Depth)) + pow(y.z - x.z, T(Depth)), T(1) / T(Depth));
  64. }
  65. template<typename T, qualifier Q>
  66. GLM_FUNC_QUALIFIER T lxNorm(vec<3, T, Q> const& v, unsigned int Depth)
  67. {
  68. return pow(pow(v.x, T(Depth)) + pow(v.y, T(Depth)) + pow(v.z, T(Depth)), T(1) / T(Depth));
  69. }
  70. }//namespace glm