vector_query.inl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /// @ref gtx_vector_query
  2. #include <cassert>
  3. namespace glm{
  4. namespace detail
  5. {
  6. template<length_t L, typename T, qualifier Q>
  7. struct compute_areCollinear{};
  8. template<typename T, qualifier Q>
  9. struct compute_areCollinear<2, T, Q>
  10. {
  11. GLM_FUNC_QUALIFIER static bool call(vec<2, T, Q> const& v0, vec<2, T, Q> const& v1, T const& epsilon)
  12. {
  13. return length(cross(vec<3, T, Q>(v0, static_cast<T>(0)), vec<3, T, Q>(v1, static_cast<T>(0)))) < epsilon;
  14. }
  15. };
  16. template<typename T, qualifier Q>
  17. struct compute_areCollinear<3, T, Q>
  18. {
  19. GLM_FUNC_QUALIFIER static bool call(vec<3, T, Q> const& v0, vec<3, T, Q> const& v1, T const& epsilon)
  20. {
  21. return length(cross(v0, v1)) < epsilon;
  22. }
  23. };
  24. template<typename T, qualifier Q>
  25. struct compute_areCollinear<4, T, Q>
  26. {
  27. GLM_FUNC_QUALIFIER static bool call(vec<4, T, Q> const& v0, vec<4, T, Q> const& v1, T const& epsilon)
  28. {
  29. return length(cross(vec<3, T, Q>(v0), vec<3, T, Q>(v1))) < epsilon;
  30. }
  31. };
  32. template<length_t L, typename T, qualifier Q>
  33. struct compute_isCompNull{};
  34. template<typename T, qualifier Q>
  35. struct compute_isCompNull<2, T, Q>
  36. {
  37. GLM_FUNC_QUALIFIER static vec<2, bool, Q> call(vec<2, T, Q> const& v, T const& epsilon)
  38. {
  39. return vec<2, bool, Q>(
  40. (abs(v.x) < epsilon),
  41. (abs(v.y) < epsilon));
  42. }
  43. };
  44. template<typename T, qualifier Q>
  45. struct compute_isCompNull<3, T, Q>
  46. {
  47. GLM_FUNC_QUALIFIER static vec<3, bool, Q> call(vec<3, T, Q> const& v, T const& epsilon)
  48. {
  49. return vec<3, bool, Q>(
  50. (abs(v.x) < epsilon),
  51. (abs(v.y) < epsilon),
  52. (abs(v.z) < epsilon));
  53. }
  54. };
  55. template<typename T, qualifier Q>
  56. struct compute_isCompNull<4, T, Q>
  57. {
  58. GLM_FUNC_QUALIFIER static vec<4, bool, Q> call(vec<4, T, Q> const& v, T const& epsilon)
  59. {
  60. return vec<4, bool, Q>(
  61. (abs(v.x) < epsilon),
  62. (abs(v.y) < epsilon),
  63. (abs(v.z) < epsilon),
  64. (abs(v.w) < epsilon));
  65. }
  66. };
  67. }//namespace detail
  68. template<length_t L, typename T, qualifier Q>
  69. GLM_FUNC_QUALIFIER bool areCollinear(vec<L, T, Q> const& v0, vec<L, T, Q> const& v1, T const& epsilon)
  70. {
  71. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'areCollinear' only accept floating-point inputs");
  72. return detail::compute_areCollinear<L, T, Q>::call(v0, v1, epsilon);
  73. }
  74. template<length_t L, typename T, qualifier Q>
  75. GLM_FUNC_QUALIFIER bool areOrthogonal(vec<L, T, Q> const& v0, vec<L, T, Q> const& v1, T const& epsilon)
  76. {
  77. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'areOrthogonal' only accept floating-point inputs");
  78. return abs(dot(v0, v1)) <= max(
  79. static_cast<T>(1),
  80. length(v0)) * max(static_cast<T>(1), length(v1)) * epsilon;
  81. }
  82. template<length_t L, typename T, qualifier Q>
  83. GLM_FUNC_QUALIFIER bool isNormalized(vec<L, T, Q> const& v, T const& epsilon)
  84. {
  85. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'isNormalized' only accept floating-point inputs");
  86. return abs(length(v) - static_cast<T>(1)) <= static_cast<T>(2) * epsilon;
  87. }
  88. template<length_t L, typename T, qualifier Q>
  89. GLM_FUNC_QUALIFIER bool isNull(vec<L, T, Q> const& v, T const& epsilon)
  90. {
  91. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'isNull' only accept floating-point inputs");
  92. return length(v) <= epsilon;
  93. }
  94. template<length_t L, typename T, qualifier Q>
  95. GLM_FUNC_QUALIFIER vec<L, bool, Q> isCompNull(vec<L, T, Q> const& v, T const& epsilon)
  96. {
  97. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'isCompNull' only accept floating-point inputs");
  98. return detail::compute_isCompNull<L, T, Q>::call(v, epsilon);
  99. }
  100. template<typename T, qualifier Q>
  101. GLM_FUNC_QUALIFIER vec<2, bool, Q> isCompNull(vec<2, T, Q> const& v, T const& epsilon)
  102. {
  103. return vec<2, bool, Q>(
  104. abs(v.x) < epsilon,
  105. abs(v.y) < epsilon);
  106. }
  107. template<typename T, qualifier Q>
  108. GLM_FUNC_QUALIFIER vec<3, bool, Q> isCompNull(vec<3, T, Q> const& v, T const& epsilon)
  109. {
  110. return vec<3, bool, Q>(
  111. abs(v.x) < epsilon,
  112. abs(v.y) < epsilon,
  113. abs(v.z) < epsilon);
  114. }
  115. template<typename T, qualifier Q>
  116. GLM_FUNC_QUALIFIER vec<4, bool, Q> isCompNull(vec<4, T, Q> const& v, T const& epsilon)
  117. {
  118. return vec<4, bool, Q>(
  119. abs(v.x) < epsilon,
  120. abs(v.y) < epsilon,
  121. abs(v.z) < epsilon,
  122. abs(v.w) < epsilon);
  123. }
  124. template<length_t L, typename T, qualifier Q>
  125. GLM_FUNC_QUALIFIER bool areOrthonormal(vec<L, T, Q> const& v0, vec<L, T, Q> const& v1, T const& epsilon)
  126. {
  127. return isNormalized(v0, epsilon) && isNormalized(v1, epsilon) && (abs(dot(v0, v1)) <= epsilon);
  128. }
  129. }//namespace glm