Collection of DPF-based plugins for packaging
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
7.4KB

  1. /// @ref core
  2. /// @file glm/detail/func_geometric.inl
  3. #include "../exponential.hpp"
  4. #include "../common.hpp"
  5. #include "type_vec2.hpp"
  6. #include "type_vec4.hpp"
  7. #include "type_float.hpp"
  8. namespace glm{
  9. namespace detail
  10. {
  11. template<length_t L, typename T, qualifier Q, bool Aligned>
  12. struct compute_length
  13. {
  14. GLM_FUNC_QUALIFIER static T call(vec<L, T, Q> const& v)
  15. {
  16. return sqrt(dot(v, v));
  17. }
  18. };
  19. template<length_t L, typename T, qualifier Q, bool Aligned>
  20. struct compute_distance
  21. {
  22. GLM_FUNC_QUALIFIER static T call(vec<L, T, Q> const& p0, vec<L, T, Q> const& p1)
  23. {
  24. return length(p1 - p0);
  25. }
  26. };
  27. template<typename V, typename T, bool Aligned>
  28. struct compute_dot{};
  29. template<typename T, qualifier Q, bool Aligned>
  30. struct compute_dot<vec<1, T, Q>, T, Aligned>
  31. {
  32. GLM_FUNC_QUALIFIER static T call(vec<1, T, Q> const& a, vec<1, T, Q> const& b)
  33. {
  34. return a.x * b.x;
  35. }
  36. };
  37. template<typename T, qualifier Q, bool Aligned>
  38. struct compute_dot<vec<2, T, Q>, T, Aligned>
  39. {
  40. GLM_FUNC_QUALIFIER static T call(vec<2, T, Q> const& a, vec<2, T, Q> const& b)
  41. {
  42. vec<2, T, Q> tmp(a * b);
  43. return tmp.x + tmp.y;
  44. }
  45. };
  46. template<typename T, qualifier Q, bool Aligned>
  47. struct compute_dot<vec<3, T, Q>, T, Aligned>
  48. {
  49. GLM_FUNC_QUALIFIER static T call(vec<3, T, Q> const& a, vec<3, T, Q> const& b)
  50. {
  51. vec<3, T, Q> tmp(a * b);
  52. return tmp.x + tmp.y + tmp.z;
  53. }
  54. };
  55. template<typename T, qualifier Q, bool Aligned>
  56. struct compute_dot<vec<4, T, Q>, T, Aligned>
  57. {
  58. GLM_FUNC_QUALIFIER static T call(vec<4, T, Q> const& a, vec<4, T, Q> const& b)
  59. {
  60. vec<4, T, Q> tmp(a * b);
  61. return (tmp.x + tmp.y) + (tmp.z + tmp.w);
  62. }
  63. };
  64. template<typename T, qualifier Q, bool Aligned>
  65. struct compute_cross
  66. {
  67. GLM_FUNC_QUALIFIER static vec<3, T, Q> call(vec<3, T, Q> const& x, vec<3, T, Q> const& y)
  68. {
  69. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'cross' accepts only floating-point inputs");
  70. return vec<3, T, Q>(
  71. x.y * y.z - y.y * x.z,
  72. x.z * y.x - y.z * x.x,
  73. x.x * y.y - y.x * x.y);
  74. }
  75. };
  76. template<length_t L, typename T, qualifier Q, bool Aligned>
  77. struct compute_normalize
  78. {
  79. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& v)
  80. {
  81. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'normalize' accepts only floating-point inputs");
  82. return v * inversesqrt(dot(v, v));
  83. }
  84. };
  85. template<length_t L, typename T, qualifier Q, bool Aligned>
  86. struct compute_faceforward
  87. {
  88. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& N, vec<L, T, Q> const& I, vec<L, T, Q> const& Nref)
  89. {
  90. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'normalize' accepts only floating-point inputs");
  91. return dot(Nref, I) < static_cast<T>(0) ? N : -N;
  92. }
  93. };
  94. template<length_t L, typename T, qualifier Q, bool Aligned>
  95. struct compute_reflect
  96. {
  97. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& I, vec<L, T, Q> const& N)
  98. {
  99. return I - N * dot(N, I) * static_cast<T>(2);
  100. }
  101. };
  102. template<length_t L, typename T, qualifier Q, bool Aligned>
  103. struct compute_refract
  104. {
  105. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& I, vec<L, T, Q> const& N, T eta)
  106. {
  107. T const dotValue(dot(N, I));
  108. T const k(static_cast<T>(1) - eta * eta * (static_cast<T>(1) - dotValue * dotValue));
  109. return (eta * I - (eta * dotValue + std::sqrt(k)) * N) * static_cast<T>(k >= static_cast<T>(0));
  110. }
  111. };
  112. }//namespace detail
  113. // length
  114. template<typename genType>
  115. GLM_FUNC_QUALIFIER genType length(genType x)
  116. {
  117. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'length' accepts only floating-point inputs");
  118. return abs(x);
  119. }
  120. template<length_t L, typename T, qualifier Q>
  121. GLM_FUNC_QUALIFIER T length(vec<L, T, Q> const& v)
  122. {
  123. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'length' accepts only floating-point inputs");
  124. return detail::compute_length<L, T, Q, detail::is_aligned<Q>::value>::call(v);
  125. }
  126. // distance
  127. template<typename genType>
  128. GLM_FUNC_QUALIFIER genType distance(genType const& p0, genType const& p1)
  129. {
  130. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'distance' accepts only floating-point inputs");
  131. return length(p1 - p0);
  132. }
  133. template<length_t L, typename T, qualifier Q>
  134. GLM_FUNC_QUALIFIER T distance(vec<L, T, Q> const& p0, vec<L, T, Q> const& p1)
  135. {
  136. return detail::compute_distance<L, T, Q, detail::is_aligned<Q>::value>::call(p0, p1);
  137. }
  138. // dot
  139. template<typename T>
  140. GLM_FUNC_QUALIFIER T dot(T x, T y)
  141. {
  142. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'dot' accepts only floating-point inputs");
  143. return x * y;
  144. }
  145. template<length_t L, typename T, qualifier Q>
  146. GLM_FUNC_QUALIFIER T dot(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
  147. {
  148. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'dot' accepts only floating-point inputs");
  149. return detail::compute_dot<vec<L, T, Q>, T, detail::is_aligned<Q>::value>::call(x, y);
  150. }
  151. // cross
  152. template<typename T, qualifier Q>
  153. GLM_FUNC_QUALIFIER vec<3, T, Q> cross(vec<3, T, Q> const& x, vec<3, T, Q> const& y)
  154. {
  155. return detail::compute_cross<T, Q, detail::is_aligned<Q>::value>::call(x, y);
  156. }
  157. // normalize
  158. template<typename genType>
  159. GLM_FUNC_QUALIFIER genType normalize(genType const& x)
  160. {
  161. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'normalize' accepts only floating-point inputs");
  162. return x < genType(0) ? genType(-1) : genType(1);
  163. }
  164. template<length_t L, typename T, qualifier Q>
  165. GLM_FUNC_QUALIFIER vec<L, T, Q> normalize(vec<L, T, Q> const& x)
  166. {
  167. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'normalize' accepts only floating-point inputs");
  168. return detail::compute_normalize<L, T, Q, detail::is_aligned<Q>::value>::call(x);
  169. }
  170. // faceforward
  171. template<typename genType>
  172. GLM_FUNC_QUALIFIER genType faceforward(genType const& N, genType const& I, genType const& Nref)
  173. {
  174. return dot(Nref, I) < static_cast<genType>(0) ? N : -N;
  175. }
  176. template<length_t L, typename T, qualifier Q>
  177. GLM_FUNC_QUALIFIER vec<L, T, Q> faceforward(vec<L, T, Q> const& N, vec<L, T, Q> const& I, vec<L, T, Q> const& Nref)
  178. {
  179. return detail::compute_faceforward<L, T, Q, detail::is_aligned<Q>::value>::call(N, I, Nref);
  180. }
  181. // reflect
  182. template<typename genType>
  183. GLM_FUNC_QUALIFIER genType reflect(genType const& I, genType const& N)
  184. {
  185. return I - N * dot(N, I) * genType(2);
  186. }
  187. template<length_t L, typename T, qualifier Q>
  188. GLM_FUNC_QUALIFIER vec<L, T, Q> reflect(vec<L, T, Q> const& I, vec<L, T, Q> const& N)
  189. {
  190. return detail::compute_reflect<L, T, Q, detail::is_aligned<Q>::value>::call(I, N);
  191. }
  192. // refract
  193. template<typename genType>
  194. GLM_FUNC_QUALIFIER genType refract(genType const& I, genType const& N, genType eta)
  195. {
  196. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'refract' accepts only floating-point inputs");
  197. genType const dotValue(dot(N, I));
  198. genType const k(static_cast<genType>(1) - eta * eta * (static_cast<genType>(1) - dotValue * dotValue));
  199. return (eta * I - (eta * dotValue + sqrt(k)) * N) * static_cast<genType>(k >= static_cast<genType>(0));
  200. }
  201. template<length_t L, typename T, qualifier Q>
  202. GLM_FUNC_QUALIFIER vec<L, T, Q> refract(vec<L, T, Q> const& I, vec<L, T, Q> const& N, T eta)
  203. {
  204. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'refract' accepts only floating-point inputs");
  205. return detail::compute_refract<L, T, Q, detail::is_aligned<Q>::value>::call(I, N, eta);
  206. }
  207. }//namespace glm
  208. #if GLM_ARCH != GLM_ARCH_PURE && GLM_HAS_UNRESTRICTED_UNIONS
  209. # include "func_geometric_simd.inl"
  210. #endif