Functions.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #ifndef ANKI_MATH_FUNCTIONS_H
  2. #define ANKI_MATH_FUNCTIONS_H
  3. #include "anki/util/StdTypes.h"
  4. #include <cmath>
  5. namespace anki {
  6. //==============================================================================
  7. // Math constants =
  8. //==============================================================================
  9. template<typename Scalar>
  10. constexpr Scalar getPi();
  11. template<>
  12. inline constexpr F32 getPi<F32>()
  13. {
  14. return 3.14159265358979323846f;
  15. }
  16. template<>
  17. inline constexpr F64 getPi<F64>()
  18. {
  19. return 3.14159265358979323846;
  20. }
  21. template<typename Scalar>
  22. constexpr Scalar getEpsilon();
  23. template<>
  24. constexpr F32 getEpsilon<F32>()
  25. {
  26. return 1.0e-6f;
  27. }
  28. template<>
  29. constexpr F64 getEpsilon<F64>()
  30. {
  31. return 1.0e-6;
  32. }
  33. //==============================================================================
  34. // Math functions =
  35. //==============================================================================
  36. template<typename T>
  37. inline T fabs(const T x)
  38. {
  39. return std::fabs(x);
  40. }
  41. template<typename T>
  42. inline T sin(const T rad)
  43. {
  44. return std::sin(rad);
  45. }
  46. template<typename T>
  47. inline T cos(const T rad)
  48. {
  49. return std::cos(rad);
  50. }
  51. template<typename T>
  52. inline T tan(const T rad)
  53. {
  54. return std::tan(rad);
  55. }
  56. template<typename T>
  57. inline T acos(const T x)
  58. {
  59. return std::acos(x);
  60. }
  61. template<typename T>
  62. inline T asin(const T x)
  63. {
  64. return std::asin(x);
  65. }
  66. template<typename T>
  67. inline T atan(const T x)
  68. {
  69. return std::atan(x);
  70. }
  71. template<typename T>
  72. inline T atan2(const T x, const T y)
  73. {
  74. return std::atan2(x, y);
  75. }
  76. void sinCos(const F32 a, F32& sina, F32& cosa);
  77. void sinCos(const F64 a, F64& sina, F64& cosa);
  78. template<typename T>
  79. inline T sqrt(const T x)
  80. {
  81. return std::sqrt(x);
  82. }
  83. //==============================================================================
  84. // Other math functions =
  85. //==============================================================================
  86. template<typename T>
  87. inline Bool isZero(const T f)
  88. {
  89. return fabs(f) < getEpsilon<T>();
  90. }
  91. template<typename T>
  92. inline T toRad(const T degrees)
  93. {
  94. return degrees * (getPi<T>() / T(180.0));
  95. }
  96. template<typename T>
  97. inline T toDegrees(const T rad)
  98. {
  99. return rad * (T(180.0) / getPi<T>());
  100. }
  101. //==============================================================================
  102. // Interpolation =
  103. //==============================================================================
  104. /// Linear interpolation between values
  105. /// @param[in] from Starting value
  106. /// @param[in] to Ending value
  107. /// @param[in] u The percentage from the from "from" value. Values
  108. /// from [0.0, 1.0]
  109. template<typename Type>
  110. static Type linearInterpolate(const Type& from, const Type& to, F32 u)
  111. {
  112. return from * (1.0 - u) + to * u;
  113. }
  114. /// Cosine interpolation
  115. /// @param[in] from Starting value
  116. /// @param[in] to Ending value
  117. /// @param[in] u The percentage from the from "from" value. Values
  118. /// from [0.0, 1.0]
  119. template<typename Type>
  120. static Type cosInterpolate(const Type& from, const Type& to, F32 u)
  121. {
  122. F32 u2 = (1.0 - cos<F32>(u * getPi<F32>())) / 2.0;
  123. return from * (1.0 - u2) + to * u2;
  124. }
  125. /// Cubic interpolation
  126. /// @param[in] a Point a
  127. /// @param[in] b Point b
  128. /// @param[in] c Point c
  129. /// @param[in] d Point d
  130. /// @param[in] u The percentage from the from b point to d point. Value
  131. /// from [0.0, 1.0]
  132. template<typename Type>
  133. static Type cubicInterpolate(
  134. const Type& a, const Type& b, const Type& c,
  135. const Type& d, F32 u)
  136. {
  137. F32 u2 = u * u;
  138. Type a0 = d - c - a + b;
  139. Type a1 = a - b - a0;
  140. Type a2 = c - a;
  141. Type a3 = b;
  142. return(a0 * u * u2 + a1 * u2 + a2 * u + a3);
  143. }
  144. } // end namespace anki
  145. #endif