Functions.h 5.5 KB

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