Functions.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/Functions.h>
  7. #include <cmath>
  8. #include <cstdlib>
  9. namespace anki {
  10. /// @addtogroup math
  11. /// @{
  12. /// Just PI.
  13. constexpr F32 kPi = 3.14159265358979323846f;
  14. /// Floating point epsilon.
  15. constexpr F32 kEpsilonf = 1.0e-6f;
  16. template<typename T>
  17. inline T sin(const T rad)
  18. {
  19. return std::sin(rad);
  20. }
  21. template<typename T>
  22. inline T cos(const T rad)
  23. {
  24. return std::cos(rad);
  25. }
  26. template<typename T>
  27. inline T tan(const T rad)
  28. {
  29. return std::tan(rad);
  30. }
  31. template<typename T>
  32. inline T acos(const T x)
  33. {
  34. ANKI_ASSERT(x >= T(-1) && x <= T(1));
  35. return std::acos(x);
  36. }
  37. template<typename T>
  38. inline T asin(const T x)
  39. {
  40. return std::asin(x);
  41. }
  42. template<typename T>
  43. inline T atan(const T x)
  44. {
  45. return std::atan(x);
  46. }
  47. template<typename T>
  48. inline T atan2(const T x, const T y)
  49. {
  50. return std::atan2(x, y);
  51. }
  52. void sinCos(const F32 a, F32& sina, F32& cosa);
  53. void sinCos(const F64 a, F64& sina, F64& cosa);
  54. template<typename T>
  55. inline T sqrt(const T x) requires(std::is_floating_point<T>::value)
  56. {
  57. return std::sqrt(x);
  58. }
  59. template<typename T>
  60. inline T sqrt(const T x) requires(std::is_integral<T>::value)
  61. {
  62. return T(std::sqrt(F64(x)));
  63. }
  64. template<typename T>
  65. inline T fract(const T x)
  66. {
  67. return x - std::floor(x);
  68. }
  69. template<typename T>
  70. inline T mod(const T x, const T y)
  71. {
  72. return x - y * std::floor(x / y);
  73. }
  74. /// Like GLSL's mix.
  75. template<typename T, typename Y>
  76. inline T mix(T x, T y, Y factor)
  77. {
  78. return x * (T(1) - factor) + y * factor;
  79. }
  80. /// Like GLSL's modf
  81. template<typename T>
  82. inline T modf(T x, T& intPart)
  83. {
  84. return std::modf(x, &intPart);
  85. }
  86. /// The same as abs/fabs. For ints.
  87. template<typename T>
  88. inline constexpr T absolute(const T f) requires(std::is_integral<T>::value&& std::is_signed<T>::value)
  89. {
  90. return (f < T(0)) ? -f : f;
  91. }
  92. /// The same as abs/fabs. For floats.
  93. template<typename T>
  94. inline constexpr T absolute(const T f) requires(std::is_floating_point<T>::value&& std::is_same_v<T, double>)
  95. {
  96. return fabs(f);
  97. }
  98. /// The same as abs/fabs. For floats.
  99. template<typename T>
  100. inline constexpr T absolute(const T f) requires(std::is_floating_point<T>::value&& std::is_same_v<T, float>)
  101. {
  102. return fabsf(f);
  103. }
  104. template<typename T>
  105. inline constexpr T pow(const T x, const T exp)
  106. {
  107. return T(std::pow(x, exp));
  108. }
  109. template<typename T>
  110. inline constexpr T log2(const T x)
  111. {
  112. return T(std::log2(x));
  113. }
  114. template<typename T>
  115. inline constexpr Bool isZero(const T f, const T e = kEpsilonf) requires(std::is_floating_point<T>::value)
  116. {
  117. return absolute<T>(f) < e;
  118. }
  119. template<typename T>
  120. inline constexpr Bool isZero(const T f) requires(std::is_integral<T>::value)
  121. {
  122. return f == 0;
  123. }
  124. template<typename T>
  125. inline constexpr T toRad(const T degrees) requires(std::is_floating_point<T>::value)
  126. {
  127. return degrees * (kPi / T(180));
  128. }
  129. template<typename T>
  130. inline constexpr T toDegrees(const T rad)
  131. {
  132. return rad * (T(180) / kPi);
  133. }
  134. template<typename T>
  135. inline constexpr T saturate(T v)
  136. {
  137. return clamp<T>(v, T(0), T(1));
  138. }
  139. /// Returns 1 or -1 based on the sign
  140. template<typename T>
  141. inline constexpr T sign(T v)
  142. {
  143. return (v > T(0)) ? T(1) : T(-1);
  144. }
  145. /// Same as smoothstep in glsl
  146. template<typename T>
  147. inline constexpr T smoothstep(T edge0, T edge1, T value)
  148. {
  149. value = clamp((value - edge0) / (edge1 - edge0), T(0), T(1));
  150. return value * value * (T(3) - T(2) * value);
  151. }
  152. /// Linear interpolation between values
  153. /// @param[in] from Starting value
  154. /// @param[in] to Ending value
  155. /// @param[in] u The percentage from the from "from" value. Values from [0.0, 1.0]
  156. template<typename T>
  157. inline constexpr T linearInterpolate(const T& from, const T& to, F32 u)
  158. {
  159. return from * T(1.0f - u) + to * T(u);
  160. }
  161. /// Cosine interpolation
  162. /// @param[in] from Starting value
  163. /// @param[in] to Ending value
  164. /// @param[in] u The percentage from the from "from" value. Values from [0.0, 1.0]
  165. template<typename T>
  166. inline T cosInterpolate(const T& from, const T& to, F32 u)
  167. {
  168. const F32 u2 = (1.0f - cos<F32>(u * kPi)) / 2.0f;
  169. return from * T(1.0f - u2) + to * T(u2);
  170. }
  171. /// Cubic interpolation
  172. /// @param[in] a Point a
  173. /// @param[in] b Point b
  174. /// @param[in] c Point c
  175. /// @param[in] d Point d
  176. /// @param[in] u The percentage from the from b point to d point. Value from [0.0, 1.0]
  177. template<typename T>
  178. inline constexpr T cubicInterpolate(const T& a, const T& b, const T& c, const T& d, F32 u)
  179. {
  180. const F32 u2 = u * u;
  181. const T a0 = d - c - a + b;
  182. const T a1 = a - b - a0;
  183. const T a2 = c - a;
  184. const T a3 = b;
  185. return (a0 * u * u2 + a1 * u2 + a2 * u + a3);
  186. }
  187. /// Pack 4 color components to R10G10B10A2 SNORM format.
  188. inline U32 packColorToR10G10B10A2SNorm(F32 r, F32 g, F32 b, F32 a)
  189. {
  190. union SignedR10G10B10A10
  191. {
  192. struct
  193. {
  194. I32 m_x : 10;
  195. I32 m_y : 10;
  196. I32 m_z : 10;
  197. I32 m_w : 2;
  198. } m_unpacked;
  199. U32 m_packed;
  200. };
  201. #if ANKI_COMPILER_GCC_COMPATIBLE
  202. # pragma GCC diagnostic push
  203. # pragma GCC diagnostic ignored "-Wconversion"
  204. #endif
  205. SignedR10G10B10A10 out;
  206. out.m_unpacked.m_x = I32(round(r * 511.0f));
  207. out.m_unpacked.m_y = I32(round(g * 511.0f));
  208. out.m_unpacked.m_z = I32(round(b * 511.0f));
  209. out.m_unpacked.m_w = I32(round(a * 1.0f));
  210. #if ANKI_COMPILER_GCC_COMPATIBLE
  211. # pragma GCC diagnostic pop
  212. #endif
  213. return out.m_packed;
  214. }
  215. template<typename TVec4>
  216. inline U32 packSnorm4x8(const TVec4& v)
  217. {
  218. union
  219. {
  220. I8 in[4];
  221. U32 out;
  222. } u;
  223. const TVec4 result = (v.clamp(-1.0f, 1.0f) * 127.0f).round();
  224. u.in[0] = I8(result[0]);
  225. u.in[1] = I8(result[1]);
  226. u.in[2] = I8(result[2]);
  227. u.in[3] = I8(result[3]);
  228. return u.out;
  229. }
  230. /// Compute the abs triangle area.
  231. template<typename TVec>
  232. inline F32 computeTriangleArea(const TVec& a, const TVec& b, const TVec& c)
  233. {
  234. const TVec ab = b - a;
  235. const TVec ac = c - a;
  236. const F32 area = ab.cross(ac).length() / 2.0f;
  237. return absolute(area);
  238. }
  239. /// @}
  240. } // end namespace anki