Functions.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 square(const T x)
  66. {
  67. return x * x;
  68. }
  69. template<typename T>
  70. inline T fract(const T x)
  71. {
  72. return x - std::floor(x);
  73. }
  74. template<typename T>
  75. inline T mod(const T x, const T y)
  76. {
  77. return x - y * std::floor(x / y);
  78. }
  79. /// Like GLSL's mix.
  80. template<typename T, typename Y>
  81. inline T mix(T x, T y, Y factor)
  82. {
  83. return x * (T(1) - factor) + y * factor;
  84. }
  85. /// Like GLSL's modf
  86. template<typename T>
  87. inline T modf(T x, T& intPart)
  88. {
  89. return std::modf(x, &intPart);
  90. }
  91. /// The same as abs/fabs. For ints.
  92. template<typename T>
  93. inline constexpr T absolute(const T f) requires(std::is_integral<T>::value&& std::is_signed<T>::value)
  94. {
  95. return (f < T(0)) ? -f : f;
  96. }
  97. /// The same as abs/fabs. For floats.
  98. template<typename T>
  99. inline constexpr T absolute(const T f) requires(std::is_floating_point<T>::value&& std::is_same_v<T, double>)
  100. {
  101. return fabs(f);
  102. }
  103. /// The same as abs/fabs. For floats.
  104. template<typename T>
  105. inline constexpr T absolute(const T f) requires(std::is_floating_point<T>::value&& std::is_same_v<T, float>)
  106. {
  107. return fabsf(f);
  108. }
  109. template<typename T>
  110. inline constexpr T pow(const T x, const T exp)
  111. {
  112. return T(std::pow(x, exp));
  113. }
  114. template<typename T>
  115. inline constexpr T log2(const T x)
  116. {
  117. return T(std::log2(x));
  118. }
  119. template<typename T>
  120. inline constexpr Bool isZero(const T f, const T e = kEpsilonf) requires(std::is_floating_point<T>::value)
  121. {
  122. return absolute<T>(f) < e;
  123. }
  124. template<typename T>
  125. inline constexpr Bool isZero(const T f) requires(std::is_integral<T>::value)
  126. {
  127. return f == 0;
  128. }
  129. template<typename T>
  130. inline constexpr T toRad(const T degrees) requires(std::is_floating_point<T>::value)
  131. {
  132. return degrees * (kPi / T(180));
  133. }
  134. template<typename T>
  135. inline constexpr T toDegrees(const T rad)
  136. {
  137. return rad * (T(180) / kPi);
  138. }
  139. template<typename T>
  140. inline constexpr T saturate(T v)
  141. {
  142. return clamp<T>(v, T(0), T(1));
  143. }
  144. /// Returns 1 or -1 based on the sign
  145. template<typename T>
  146. inline constexpr T sign(T v)
  147. {
  148. return (v > T(0)) ? T(1) : T(-1);
  149. }
  150. /// Same as smoothstep in glsl
  151. template<typename T>
  152. inline constexpr T smoothstep(T edge0, T edge1, T value)
  153. {
  154. value = clamp((value - edge0) / (edge1 - edge0), T(0), T(1));
  155. return value * value * (T(3) - T(2) * value);
  156. }
  157. /// Linear interpolation between values
  158. /// @param[in] from Starting value
  159. /// @param[in] to Ending value
  160. /// @param[in] u The percentage from the from "from" value. Values from [0.0, 1.0]
  161. template<typename T>
  162. inline constexpr T linearInterpolate(const T& from, const T& to, F32 u)
  163. {
  164. return from * T(1.0f - u) + to * T(u);
  165. }
  166. /// Cosine interpolation
  167. /// @param[in] from Starting value
  168. /// @param[in] to Ending value
  169. /// @param[in] u The percentage from the from "from" value. Values from [0.0, 1.0]
  170. template<typename T>
  171. inline T cosInterpolate(const T& from, const T& to, F32 u)
  172. {
  173. const F32 u2 = (1.0f - cos<F32>(u * kPi)) / 2.0f;
  174. return from * T(1.0f - u2) + to * T(u2);
  175. }
  176. /// Cubic interpolation
  177. /// @param[in] a Point a
  178. /// @param[in] b Point b
  179. /// @param[in] c Point c
  180. /// @param[in] d Point d
  181. /// @param[in] u The percentage from the from b point to d point. Value from [0.0, 1.0]
  182. template<typename T>
  183. inline constexpr T cubicInterpolate(const T& a, const T& b, const T& c, const T& d, F32 u)
  184. {
  185. const F32 u2 = u * u;
  186. const T a0 = d - c - a + b;
  187. const T a1 = a - b - a0;
  188. const T a2 = c - a;
  189. const T a3 = b;
  190. return (a0 * u * u2 + a1 * u2 + a2 * u + a3);
  191. }
  192. /// Pack 4 color components to R10G10B10A2 SNORM format.
  193. inline U32 packColorToR10G10B10A2SNorm(F32 r, F32 g, F32 b, F32 a)
  194. {
  195. union SignedR10G10B10A10
  196. {
  197. struct
  198. {
  199. I32 m_x : 10;
  200. I32 m_y : 10;
  201. I32 m_z : 10;
  202. I32 m_w : 2;
  203. } m_unpacked;
  204. U32 m_packed;
  205. };
  206. #if ANKI_COMPILER_GCC_COMPATIBLE
  207. # pragma GCC diagnostic push
  208. # pragma GCC diagnostic ignored "-Wconversion"
  209. #endif
  210. SignedR10G10B10A10 out;
  211. out.m_unpacked.m_x = I32(round(r * 511.0f));
  212. out.m_unpacked.m_y = I32(round(g * 511.0f));
  213. out.m_unpacked.m_z = I32(round(b * 511.0f));
  214. out.m_unpacked.m_w = I32(round(a * 1.0f));
  215. #if ANKI_COMPILER_GCC_COMPATIBLE
  216. # pragma GCC diagnostic pop
  217. #endif
  218. return out.m_packed;
  219. }
  220. template<typename TVec4>
  221. inline U32 packSnorm4x8(const TVec4& v)
  222. {
  223. union
  224. {
  225. I8 in[4];
  226. U32 out;
  227. } u;
  228. const TVec4 result = (v.clamp(-1.0f, 1.0f) * 127.0f).round();
  229. u.in[0] = I8(result[0]);
  230. u.in[1] = I8(result[1]);
  231. u.in[2] = I8(result[2]);
  232. u.in[3] = I8(result[3]);
  233. return u.out;
  234. }
  235. /// Compute the abs triangle area.
  236. template<typename TVec>
  237. inline F32 computeTriangleArea(const TVec& a, const TVec& b, const TVec& c)
  238. {
  239. const TVec ab = b - a;
  240. const TVec ac = c - a;
  241. const F32 area = ab.cross(ac).length() / 2.0f;
  242. return absolute(area);
  243. }
  244. /// @}
  245. } // end namespace anki
  246. // Now lay out functions that include other math headers
  247. #include <AnKi/Math/Vec.h>
  248. namespace anki {
  249. template<typename T>
  250. TVec<T, 3> sphericalToCartesian(T polar, T azimuth)
  251. {
  252. TVec<T, 3> out;
  253. out.x() = cos(polar) * sin(azimuth);
  254. out.y() = cos(polar) * cos(azimuth);
  255. out.z() = sin(polar);
  256. return out;
  257. }
  258. } // end namespace anki