Functions.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright (C) 2009-2023, 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, ANKI_ENABLE(std::is_floating_point<T>::value)>
  55. inline T sqrt(const T x)
  56. {
  57. return std::sqrt(x);
  58. }
  59. template<typename T, ANKI_ENABLE(std::is_integral<T>::value)>
  60. inline T sqrt(const T x)
  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 and floats.
  87. template<typename T, ANKI_ENABLE(std::is_floating_point<T>::value || (std::is_integral<T>::value && std::is_signed<T>::value))>
  88. inline constexpr T absolute(const T f)
  89. {
  90. return (f < T(0)) ? -f : f;
  91. }
  92. template<typename T>
  93. inline constexpr T pow(const T x, const T exp)
  94. {
  95. return T(std::pow(x, exp));
  96. }
  97. template<typename T>
  98. inline constexpr T log2(const T x)
  99. {
  100. return T(std::log2(x));
  101. }
  102. template<typename T, ANKI_ENABLE(std::is_floating_point<T>::value)>
  103. inline constexpr Bool isZero(const T f, const T e = kEpsilonf)
  104. {
  105. return absolute<T>(f) < e;
  106. }
  107. template<typename T, ANKI_ENABLE(std::is_integral<T>::value)>
  108. inline constexpr Bool isZero(const T f)
  109. {
  110. return f == 0;
  111. }
  112. template<typename T, ANKI_ENABLE(std::is_floating_point<T>::value)>
  113. inline constexpr T toRad(const T degrees)
  114. {
  115. return degrees * (kPi / T(180));
  116. }
  117. template<typename T>
  118. inline constexpr T toDegrees(const T rad)
  119. {
  120. return rad * (T(180) / kPi);
  121. }
  122. template<typename T>
  123. inline constexpr T clamp(T v, T minv, T maxv)
  124. {
  125. ANKI_ASSERT(minv <= maxv);
  126. return min<T>(max<T>(minv, v), maxv);
  127. }
  128. /// Returns 1 or -1 based on the sign
  129. template<typename T>
  130. inline constexpr T sign(T v)
  131. {
  132. return (v > T(0)) ? T(1) : T(-1);
  133. }
  134. /// Same as smoothstep in glsl
  135. template<typename T>
  136. inline constexpr T smoothstep(T edge0, T edge1, T value)
  137. {
  138. value = clamp((value - edge0) / (edge1 - edge0), T(0), T(1));
  139. return value * value * (T(3) - T(2) * value);
  140. }
  141. /// Linear interpolation between values
  142. /// @param[in] from Starting value
  143. /// @param[in] to Ending value
  144. /// @param[in] u The percentage from the from "from" value. Values from [0.0, 1.0]
  145. template<typename T>
  146. inline constexpr T linearInterpolate(const T& from, const T& to, F32 u)
  147. {
  148. return from * T(1.0f - u) + to * T(u);
  149. }
  150. /// Cosine interpolation
  151. /// @param[in] from Starting value
  152. /// @param[in] to Ending value
  153. /// @param[in] u The percentage from the from "from" value. Values from [0.0, 1.0]
  154. template<typename T>
  155. inline T cosInterpolate(const T& from, const T& to, F32 u)
  156. {
  157. const F32 u2 = (1.0f - cos<F32>(u * kPi)) / 2.0f;
  158. return from * T(1.0f - u2) + to * T(u2);
  159. }
  160. /// Cubic interpolation
  161. /// @param[in] a Point a
  162. /// @param[in] b Point b
  163. /// @param[in] c Point c
  164. /// @param[in] d Point d
  165. /// @param[in] u The percentage from the from b point to d point. Value from [0.0, 1.0]
  166. template<typename T>
  167. inline constexpr T cubicInterpolate(const T& a, const T& b, const T& c, const T& d, F32 u)
  168. {
  169. const F32 u2 = u * u;
  170. const T a0 = d - c - a + b;
  171. const T a1 = a - b - a0;
  172. const T a2 = c - a;
  173. const T a3 = b;
  174. return (a0 * u * u2 + a1 * u2 + a2 * u + a3);
  175. }
  176. /// Pack 4 color components to R10G10B10A2 SNORM format.
  177. inline U32 packColorToR10G10B10A2SNorm(F32 r, F32 g, F32 b, F32 a)
  178. {
  179. union SignedR10G10B10A10
  180. {
  181. struct
  182. {
  183. I32 m_x : 10;
  184. I32 m_y : 10;
  185. I32 m_z : 10;
  186. I32 m_w : 2;
  187. } m_unpacked;
  188. U32 m_packed;
  189. };
  190. #if ANKI_COMPILER_GCC_COMPATIBLE
  191. # pragma GCC diagnostic push
  192. # pragma GCC diagnostic ignored "-Wconversion"
  193. #endif
  194. SignedR10G10B10A10 out;
  195. out.m_unpacked.m_x = I32(round(r * 511.0f));
  196. out.m_unpacked.m_y = I32(round(g * 511.0f));
  197. out.m_unpacked.m_z = I32(round(b * 511.0f));
  198. out.m_unpacked.m_w = I32(round(a * 1.0f));
  199. #if ANKI_COMPILER_GCC_COMPATIBLE
  200. # pragma GCC diagnostic pop
  201. #endif
  202. return out.m_packed;
  203. }
  204. template<typename TVec4>
  205. inline U32 packSnorm4x8(const TVec4& v)
  206. {
  207. union
  208. {
  209. I8 in[4];
  210. U32 out;
  211. } u;
  212. const TVec4 result = (v.clamp(-1.0f, 1.0f) * 127.0f).round();
  213. u.in[0] = I8(result[0]);
  214. u.in[1] = I8(result[1]);
  215. u.in[2] = I8(result[2]);
  216. u.in[3] = I8(result[3]);
  217. return u.out;
  218. }
  219. /// Compute the abs triangle area.
  220. template<typename TVec>
  221. inline F32 computeTriangleArea(const TVec& a, const TVec& b, const TVec& c)
  222. {
  223. const TVec ab = b - a;
  224. const TVec ac = c - a;
  225. const F32 area = ab.cross(ac).getLength() / 2.0f;
  226. return absolute(area);
  227. }
  228. /// @}
  229. } // end namespace anki