Functions.h 5.2 KB

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