Functions.h 5.2 KB

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