Functions.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <anki/util/StdTypes.h>
  7. #include <cmath>
  8. #include <cstdlib>
  9. namespace anki
  10. {
  11. /// @addtogroup math
  12. /// @{
  13. //==============================================================================
  14. // Math constants =
  15. //==============================================================================
  16. template<typename Scalar>
  17. constexpr Scalar getPi();
  18. template<>
  19. inline constexpr F32 getPi<F32>()
  20. {
  21. return 3.14159265358979323846f;
  22. }
  23. template<>
  24. inline constexpr F64 getPi<F64>()
  25. {
  26. return 3.14159265358979323846;
  27. }
  28. template<typename Scalar>
  29. constexpr Scalar getEpsilon()
  30. {
  31. static_assert(1, "Shouldn't instantiate");
  32. return Scalar(0);
  33. }
  34. template<>
  35. constexpr F32 getEpsilon<F32>()
  36. {
  37. return 1.0e-6f;
  38. }
  39. template<>
  40. constexpr F64 getEpsilon<F64>()
  41. {
  42. return 1.0e-6;
  43. }
  44. //==============================================================================
  45. // Math functions =
  46. //==============================================================================
  47. template<typename T>
  48. inline T fabs(const T x)
  49. {
  50. return std::fabs(x);
  51. }
  52. template<typename T>
  53. inline T sin(const T rad)
  54. {
  55. return std::sin(rad);
  56. }
  57. template<typename T>
  58. inline T cos(const T rad)
  59. {
  60. return std::cos(rad);
  61. }
  62. template<typename T>
  63. inline T tan(const T rad)
  64. {
  65. return std::tan(rad);
  66. }
  67. template<typename T>
  68. inline T acos(const T x)
  69. {
  70. return std::acos(x);
  71. }
  72. template<typename T>
  73. inline T asin(const T x)
  74. {
  75. return std::asin(x);
  76. }
  77. template<typename T>
  78. inline T atan(const T x)
  79. {
  80. return std::atan(x);
  81. }
  82. template<typename T>
  83. inline T atan2(const T x, const T y)
  84. {
  85. return std::atan2(x, y);
  86. }
  87. void sinCos(const F32 a, F32& sina, F32& cosa);
  88. void sinCos(const F64 a, F64& sina, F64& cosa);
  89. template<typename T>
  90. inline T sqrt(const T x)
  91. {
  92. return std::sqrt(x);
  93. }
  94. template<typename T>
  95. inline T fract(const T x)
  96. {
  97. return x - std::floor(x);
  98. }
  99. template<typename T>
  100. inline T mod(const T x, const T y)
  101. {
  102. return x - y * std::floor(x / y);
  103. }
  104. //==============================================================================
  105. // Other math functions =
  106. //==============================================================================
  107. template<typename T>
  108. inline T abs(const T f)
  109. {
  110. return std::fabs(f);
  111. }
  112. #define ANKI_SPECIALIZE_ABS_INT(type_) \
  113. template<> \
  114. inline type_ abs(const type_ f) \
  115. { \
  116. return std::abs(f); \
  117. }
  118. ANKI_SPECIALIZE_ABS_INT(I8)
  119. ANKI_SPECIALIZE_ABS_INT(I16)
  120. ANKI_SPECIALIZE_ABS_INT(I32)
  121. ANKI_SPECIALIZE_ABS_INT(I64)
  122. #undef ANKI_SPECIALIZE_ABS_INT
  123. template<typename T>
  124. inline Bool isZero(const T f)
  125. {
  126. return abs<T>(f) < getEpsilon<T>();
  127. }
  128. #define ANKI_SPECIALIZE_IS_ZERO_INT(type_) \
  129. template<> \
  130. inline Bool isZero(const type_ x) \
  131. { \
  132. return x == type_(0); \
  133. }
  134. ANKI_SPECIALIZE_IS_ZERO_INT(I8)
  135. ANKI_SPECIALIZE_IS_ZERO_INT(I16)
  136. ANKI_SPECIALIZE_IS_ZERO_INT(I32)
  137. ANKI_SPECIALIZE_IS_ZERO_INT(I64)
  138. ANKI_SPECIALIZE_IS_ZERO_INT(U8)
  139. ANKI_SPECIALIZE_IS_ZERO_INT(U16)
  140. ANKI_SPECIALIZE_IS_ZERO_INT(U32)
  141. ANKI_SPECIALIZE_IS_ZERO_INT(U64)
  142. #undef ANKI_SPECIALIZE_IS_ZERO_INT
  143. template<typename T>
  144. inline T toRad(const T degrees)
  145. {
  146. return degrees * (getPi<T>() / T(180));
  147. }
  148. template<typename T>
  149. inline T toDegrees(const T rad)
  150. {
  151. return rad * (T(180) / getPi<T>());
  152. }
  153. //==============================================================================
  154. // Interpolation =
  155. //==============================================================================
  156. /// Linear interpolation between values
  157. /// @param[in] from Starting value
  158. /// @param[in] to Ending value
  159. /// @param[in] u The percentage from the from "from" value. Values
  160. /// from [0.0, 1.0]
  161. template<typename Type>
  162. static Type linearInterpolate(const Type& from, const Type& to, F32 u)
  163. {
  164. return from * (1.0 - u) + to * 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
  170. /// from [0.0, 1.0]
  171. template<typename Type>
  172. static Type cosInterpolate(const Type& from, const Type& to, F32 u)
  173. {
  174. F32 u2 = (1.0 - cos<F32>(u * getPi<F32>())) / 2.0;
  175. return from * (1.0 - u2) + to * u2;
  176. }
  177. /// Cubic interpolation
  178. /// @param[in] a Point a
  179. /// @param[in] b Point b
  180. /// @param[in] c Point c
  181. /// @param[in] d Point d
  182. /// @param[in] u The percentage from the from b point to d point. Value
  183. /// from [0.0, 1.0]
  184. template<typename Type>
  185. static Type cubicInterpolate(
  186. const Type& a, const Type& b, const Type& c, const Type& d, F32 u)
  187. {
  188. F32 u2 = u * u;
  189. Type a0 = d - c - a + b;
  190. Type a1 = a - b - a0;
  191. Type a2 = c - a;
  192. Type a3 = b;
  193. return (a0 * u * u2 + a1 * u2 + a2 * u + a3);
  194. }
  195. /// @}
  196. } // end namespace anki