CmMath.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #pragma once
  25. #include "CmPrerequisitesUtil.h"
  26. #include "CmDegree.h"
  27. #include "CmRadian.h"
  28. namespace CamelotFramework
  29. {
  30. class CM_UTILITY_EXPORT Math
  31. {
  32. public:
  33. static Radian acos(float val);
  34. static Radian asin(float val);
  35. static Radian atan(float val) { return Radian(std::atan(val)); }
  36. static Radian atan2(float y, float x) { return Radian(std::atan2(y,x)); }
  37. static float cos(const Radian& val) { return (float)std::cos(val.valueRadians()); }
  38. static float cos(float val) { return (float)std::cos(val); }
  39. static float sin(const Radian& val) { return (float)std::sin(val.valueRadians()); }
  40. static float sin(float val) { return (float)std::sin(val); }
  41. static float tan(const Radian& val) { return (float)std::tan(val.valueRadians()); }
  42. static float tan(float val) { return (float)std::tan(val); }
  43. static float sqrt(float val) { return (float)std::sqrt(val); }
  44. static Radian sqrt(const Radian& val) { return Radian(std::sqrt(val.valueRadians())); }
  45. static Degree sqrt(const Degree& val) { return Degree(std::sqrt(val.valueDegrees())); }
  46. static float invSqrt(float val);
  47. static float sqr(float val) { return val*val; }
  48. static float pow(float base, float exponent) { return (float)std::pow(base, exponent); }
  49. static float exp(float val) { return (float)std::exp(val); }
  50. static float log(float val) { return (float)std::log(val); }
  51. static float log2(float val) { return (float)(std::log(val)/LOG2); }
  52. static float logN(float base, float val) { return (float)(std::log(val)/std::log(base)); }
  53. static float sign(float val);
  54. static Radian sign(const Radian& val) { return Radian(sign(val.valueRadians())); }
  55. static Degree sign(const Degree& val) { return Degree(sign(val.valueDegrees())); }
  56. static float abs(float val) { return float(std::fabs(val)); }
  57. static Degree abs(const Degree& val) { return Degree(std::fabs(val.valueDegrees())); }
  58. static Radian abs(const Radian& val) { return Radian(std::fabs(val.valueRadians())); }
  59. static float ceil(float val) { return (float)std::ceil(val); }
  60. static int ceilToInt(float val) { return (int)std::ceil(val); }
  61. static float round(float val) { return (float)std::floor(val + 0.5f); }
  62. static int roundToInt(float val) { return (int)std::floor(val + 0.5f); }
  63. static float floor(float val) { return (float)std::floor(val); }
  64. static int floorToInt(float val) { return (int)std::floor(val); }
  65. /**
  66. * @brief Clamp a value within an inclusive range.
  67. */
  68. template <typename T>
  69. static T clamp(T val, T minval, T maxval)
  70. {
  71. assert (minval <= maxval && "Invalid clamp range");
  72. return std::max(std::min(val, maxval), minval);
  73. }
  74. /**
  75. * @brief Clamp a value within an inclusive range [0..1].
  76. */
  77. template <typename T>
  78. static T clamp01(T val)
  79. {
  80. return std::max(std::min(val, (T)1), (T)0);
  81. }
  82. static bool isNaN(float f)
  83. {
  84. return f != f;
  85. }
  86. /**
  87. * @brief Compare 2 floats, using tolerance for inaccuracies.
  88. */
  89. static bool approxEquals(float a, float b, float tolerance = std::numeric_limits<float>::epsilon());
  90. /**
  91. * @brief Calculates the tangent space vector for a given set of positions / texture coords.
  92. */
  93. static Vector3 calculateTriTangent(const Vector3& position1, const Vector3& position2,
  94. const Vector3& position3, float u1, float v1, float u2, float v2, float u3, float v3);
  95. /************************************************************************/
  96. /* TRIG APPROXIMATIONS */
  97. /************************************************************************/
  98. /**
  99. * @brief Sine function approximation.
  100. *
  101. * @param val Angle in range [0, pi/2].
  102. *
  103. * @note Evaluates trigonometric functions using polynomial approximations.
  104. */
  105. static float fastSin0(const Radian& val) { return (float)fastASin0(val.valueRadians()); }
  106. /**
  107. * @brief Sine function approximation.
  108. *
  109. * @param val Angle in range [0, pi/2].
  110. *
  111. * @note Evaluates trigonometric functions using polynomial approximations.
  112. */
  113. static float fastSin0(float val);
  114. /**
  115. * @brief Sine function approximation.
  116. *
  117. * @param val Angle in range [0, pi/2].
  118. *
  119. * @note Evaluates trigonometric functions using polynomial approximations.
  120. * Slightly better (and slower) than "fastSin0".
  121. */
  122. static float fastSin1(const Radian& val) { return (float)fastASin1(val.valueRadians()); }
  123. /**
  124. * @brief Sine function approximation.
  125. *
  126. * @param val Angle in range [0, pi/2].
  127. *
  128. * @note Evaluates trigonometric functions using polynomial approximations.
  129. * Slightly better (and slower) than "fastSin0".
  130. */
  131. static float fastSin1(float val);
  132. /**
  133. * @brief Cosine function approximation.
  134. *
  135. * @param val Angle in range [0, pi/2].
  136. *
  137. * @note Evaluates trigonometric functions using polynomial approximations.
  138. */
  139. static float fastCos0(const Radian& val) { return (float)fastACos0(val.valueRadians()); }
  140. /**
  141. * @brief Cosine function approximation.
  142. *
  143. * @param val Angle in range [0, pi/2].
  144. *
  145. * @note Evaluates trigonometric functions using polynomial approximations.
  146. */
  147. static float fastCos0(float val);
  148. /**
  149. * @brief Cosine function approximation.
  150. *
  151. * @param val Angle in range [0, pi/2].
  152. *
  153. * @note Evaluates trigonometric functions using polynomial approximations.
  154. * Slightly better (and slower) than "fastCos0".
  155. */
  156. static float fastCos1(const Radian& val) { return (float)fastACos1(val.valueRadians()); }
  157. /**
  158. * @brief Cosine function approximation.
  159. *
  160. * @param val Angle in range [0, pi/2].
  161. *
  162. * @note Evaluates trigonometric functions using polynomial approximations.
  163. * Slightly better (and slower) than "fastCos0".
  164. */
  165. static float fastCos1(float val);
  166. /**
  167. * @brief Tangent function approximation.
  168. *
  169. * @param val Angle in range [0, pi/4].
  170. *
  171. * @note Evaluates trigonometric functions using polynomial approximations.
  172. */
  173. static float fastTan0(const Radian& val) { return (float)fastATan0(val.valueRadians()); }
  174. /**
  175. * @brief Tangent function approximation.
  176. *
  177. * @param val Angle in range [0, pi/4].
  178. *
  179. * @note Evaluates trigonometric functions using polynomial approximations.
  180. */
  181. static float fastTan0(float val);
  182. /**
  183. * @brief Tangent function approximation.
  184. *
  185. * @param val Angle in range [0, pi/4].
  186. *
  187. * @note Evaluates trigonometric functions using polynomial approximations.
  188. * Slightly better (and slower) than "fastTan0".
  189. */
  190. static float fastTan1(const Radian& val) { return (float)fastATan1(val.valueRadians()); }
  191. /**
  192. * @brief Tangent function approximation.
  193. *
  194. * @param val Angle in range [0, pi/4].
  195. *
  196. * @note Evaluates trigonometric functions using polynomial approximations.
  197. * Slightly better (and slower) than "fastTan0".
  198. */
  199. static float fastTan1(float val);
  200. /**
  201. * @brief Inverse sine function approximation.
  202. *
  203. * @param val Angle in range [0, 1].
  204. *
  205. * @note Evaluates trigonometric functions using polynomial approximations.
  206. */
  207. static float fastASin0(const Radian& val) { return (float)fastASin0(val.valueRadians()); }
  208. /**
  209. * @brief Inverse sine function approximation.
  210. *
  211. * @param val Angle in range [0, 1].
  212. *
  213. * @note Evaluates trigonometric functions using polynomial approximations.
  214. */
  215. static float fastASin0(float val);
  216. /**
  217. * @brief Inverse sine function approximation.
  218. *
  219. * @param val Angle in range [0, 1].
  220. *
  221. * @note Evaluates trigonometric functions using polynomial approximations.
  222. * Slightly better (and slower) than "fastASin0".
  223. */
  224. static float fastASin1(const Radian& val) { return (float)fastASin1(val.valueRadians()); }
  225. /**
  226. * @brief Inverse sine function approximation.
  227. *
  228. * @param val Angle in range [0, 1].
  229. *
  230. * @note Evaluates trigonometric functions using polynomial approximations.
  231. * Slightly better (and slower) than "fastASin0".
  232. */
  233. static float fastASin1(float val);
  234. /**
  235. * @brief Inverse cosine function approximation.
  236. *
  237. * @param val Angle in range [0, 1].
  238. *
  239. * @note Evaluates trigonometric functions using polynomial approximations.
  240. */
  241. static float fastACos0(const Radian& val) { return (float)fastACos0(val.valueRadians()); }
  242. /**
  243. * @brief Inverse cosine function approximation.
  244. *
  245. * @param val Angle in range [0, 1].
  246. *
  247. * @note Evaluates trigonometric functions using polynomial approximations.
  248. */
  249. static float fastACos0(float val);
  250. /**
  251. * @brief Inverse cosine function approximation.
  252. *
  253. * @param val Angle in range [0, 1].
  254. *
  255. * @note Evaluates trigonometric functions using polynomial approximations.
  256. * Slightly better (and slower) than "fastACos0".
  257. */
  258. static float fastACos1(const Radian& val) { return (float)fastACos1(val.valueRadians()); }
  259. /**
  260. * @brief Inverse cosine function approximation.
  261. *
  262. * @param val Angle in range [0, 1].
  263. *
  264. * @note Evaluates trigonometric functions using polynomial approximations.
  265. * Slightly better (and slower) than "fastACos0".
  266. */
  267. static float fastACos1(float val);
  268. /**
  269. * @brief Inverse tangent function approximation.
  270. *
  271. * @param val Angle in range [-1, 1].
  272. *
  273. * @note Evaluates trigonometric functions using polynomial approximations.
  274. */
  275. static float fastATan0(const Radian& val) { return (float)fastATan0(val.valueRadians()); }
  276. /**
  277. * @brief Inverse tangent function approximation.
  278. *
  279. * @param val Angle in range [-1, 1].
  280. *
  281. * @note Evaluates trigonometric functions using polynomial approximations.
  282. */
  283. static float fastATan0(float val);
  284. /**
  285. * @brief Inverse tangent function approximation.
  286. *
  287. * @param val Angle in range [-1, 1].
  288. *
  289. * @note Evaluates trigonometric functions using polynomial approximations.
  290. * Slightly better (and slower) than "fastATan0".
  291. */
  292. static float fastATan1(const Radian& val) { return (float)fastATan1(val.valueRadians()); }
  293. /**
  294. * @brief Inverse tangent function approximation.
  295. *
  296. * @param val Angle in range [-1, 1].
  297. *
  298. * @note Evaluates trigonometric functions using polynomial approximations.
  299. * Slightly better (and slower) than "fastATan0".
  300. */
  301. static float fastATan1(float val);
  302. static const float POS_INFINITY;
  303. static const float NEG_INFINITY;
  304. static const float PI;
  305. static const float TWO_PI;
  306. static const float HALF_PI;
  307. static const float DEG2RAD;
  308. static const float RAD2DEG;
  309. static const float LOG2;
  310. };
  311. }