MathUtils.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include <math.h>
  25. #include "Assert.h"
  26. #include "Types.h"
  27. #include "Allocator.h"
  28. namespace crown
  29. {
  30. /// Math utilities.
  31. ///
  32. /// @ingroup Math
  33. namespace math
  34. {
  35. // Constants
  36. const float PI = (float)3.1415926535897932;
  37. const float TWO_PI = PI * (float)2.0;
  38. const float HALF_PI = PI * (float)0.5;
  39. const float ONEFOURTH_PI = PI * (float)0.25;
  40. const float DEG_TO_RAD = PI / (float)180.0;
  41. const float RAD_TO_DEG = (float)1.0 / DEG_TO_RAD;
  42. const float FOUR_OVER_THREE = (float)(4.0 / 3.0);
  43. const float FOUR_OVER_THREE_TIMES_PI = FOUR_OVER_THREE * PI;
  44. const float ONE_OVER_THREE = (float)(1.0 / 3.0);
  45. const float ONE_OVER_FOUR = (float)(1.0 / 4.0);
  46. const float ONE_OVER_255 = (float)(1.0 / 255.0);
  47. const float FLOAT_PRECISION = (float)1.0e-7f;
  48. const double DOUBLE_PRECISION = (float)1.0e-9;
  49. //-----------------------------------------------------------------------------
  50. inline bool equals(float a, float b, float precision = FLOAT_PRECISION)
  51. {
  52. return ((b <= (a + precision)) && (b >= (a - precision)));
  53. }
  54. //-----------------------------------------------------------------------------
  55. inline bool equals(double a, double b, double precision = DOUBLE_PRECISION)
  56. {
  57. return ((b <= (a + precision)) && (b >= (a - precision)));
  58. }
  59. //-----------------------------------------------------------------------------
  60. template <typename T>
  61. inline T min(const T& a, const T& b)
  62. {
  63. return a < b ? a : b;
  64. }
  65. //-----------------------------------------------------------------------------
  66. template <typename T>
  67. inline T min(const T& a, const T& b, const T& c)
  68. {
  69. return math::min(math::min(a, b), math::min(a, c));
  70. }
  71. //-----------------------------------------------------------------------------
  72. template <typename T>
  73. inline T min(const T& a, const T& b, const T& c, const T& d)
  74. {
  75. return math::min(math::min(a, b, c), math::min(a, b, d), math::min(a, c, d));
  76. }
  77. //-----------------------------------------------------------------------------
  78. template <typename T>
  79. inline T max(const T& a, const T& b)
  80. {
  81. return a < b ? b : a;
  82. }
  83. //-----------------------------------------------------------------------------
  84. template <typename T>
  85. inline T max(const T& a, const T& b, const T& c)
  86. {
  87. return math::max(math::max(a, b), math::max(a, c));
  88. }
  89. //-----------------------------------------------------------------------------
  90. template <typename T>
  91. inline T max(const T& a, const T& b, const T& c, const T& d)
  92. {
  93. return math::max(math::max(a, b, c), math::max(a, b, d), math::max(a, c, d));
  94. }
  95. //-----------------------------------------------------------------------------
  96. template <typename T>
  97. inline T avg(const T& a, const T& b)
  98. {
  99. return (a + b) * 0.5;
  100. }
  101. //-----------------------------------------------------------------------------
  102. template <typename T>
  103. inline T avg(const T& a, const T& b, const T& c)
  104. {
  105. return (a + b + c) * ONE_OVER_THREE;
  106. }
  107. //-----------------------------------------------------------------------------
  108. template <typename T>
  109. inline T avg(const T& a, const T& b, const T& c, const T& d)
  110. {
  111. return (a + b + c + d) * ONE_OVER_FOUR;
  112. }
  113. //-----------------------------------------------------------------------------
  114. template <typename T>
  115. inline T clamp_to_range(const T& min, const T& max, const T& value)
  116. {
  117. CE_ASSERT(min < max, "Min must be < max");
  118. if (value > max)
  119. {
  120. return max;
  121. }
  122. if (value < min)
  123. {
  124. return min;
  125. }
  126. return value;
  127. }
  128. //-----------------------------------------------------------------------------
  129. template <typename T>
  130. inline void swap(T& a, T& b)
  131. {
  132. T tmp = a;
  133. a = b;
  134. b = tmp;
  135. }
  136. //-----------------------------------------------------------------------------
  137. inline float deg_to_rad(float deg)
  138. {
  139. return deg * DEG_TO_RAD;
  140. }
  141. //-----------------------------------------------------------------------------
  142. inline float rad_to_deg(float rad)
  143. {
  144. return rad * RAD_TO_DEG;
  145. }
  146. //-----------------------------------------------------------------------------
  147. inline uint32_t next_pow_2(uint32_t x)
  148. {
  149. x--;
  150. x = (x >> 1) | x;
  151. x = (x >> 2) | x;
  152. x = (x >> 4) | x;
  153. x = (x >> 8) | x;
  154. x = (x >> 16) | x;
  155. return ++x;
  156. }
  157. //-----------------------------------------------------------------------------
  158. inline bool is_pow_2(uint32_t x)
  159. {
  160. return !(x & (x - 1)) && x;
  161. }
  162. //-----------------------------------------------------------------------------
  163. inline float ceil(float x)
  164. {
  165. return ceilf(x);
  166. }
  167. //-----------------------------------------------------------------------------
  168. inline float floor(float x)
  169. {
  170. return floorf(x);
  171. }
  172. //-----------------------------------------------------------------------------
  173. inline float sqrt(float x)
  174. {
  175. return sqrtf(x);
  176. }
  177. //-----------------------------------------------------------------------------
  178. inline float inv_sqrt(float x)
  179. {
  180. return 1.0 / sqrt(x);
  181. }
  182. //-----------------------------------------------------------------------------
  183. inline float sin(float x)
  184. {
  185. return sinf(x);
  186. }
  187. //-----------------------------------------------------------------------------
  188. inline float cos(float x)
  189. {
  190. return cosf(x);
  191. }
  192. //-----------------------------------------------------------------------------
  193. inline float asin(float x)
  194. {
  195. return asinf(x);
  196. }
  197. //-----------------------------------------------------------------------------
  198. inline float acos(float x)
  199. {
  200. return acosf(x);
  201. }
  202. //-----------------------------------------------------------------------------
  203. inline float tan(float x)
  204. {
  205. return tanf(x);
  206. }
  207. //-----------------------------------------------------------------------------
  208. inline float atan2(float y, float x)
  209. {
  210. return atan2f(y, x);
  211. }
  212. //-----------------------------------------------------------------------------
  213. inline float abs(float x)
  214. {
  215. return fabs(x);
  216. }
  217. //-----------------------------------------------------------------------------
  218. inline float fmod(float n, float d)
  219. {
  220. return ::fmod(n, d);
  221. }
  222. //-----------------------------------------------------------------------------
  223. inline bool solve_quadratic_equation(float a, float b, float c, float& x1, float& x2)
  224. {
  225. float delta = (b * b) - (4.0 * a * c);
  226. // If the equation has no float solutions
  227. if (delta < 0.0)
  228. {
  229. return false;
  230. }
  231. x1 = (-b + sqrt(delta)) / (2.0 * a);
  232. x2 = (-b - sqrt(delta)) / (2.0 * a);
  233. if (x2 > x1)
  234. {
  235. swap(x1, x2);
  236. }
  237. return true;
  238. }
  239. /// Returns the linear interpolated value between @a p0 and @a p1 at time @a t
  240. template <typename T>
  241. inline T linear(const T& p0, const T& p1, float t)
  242. {
  243. return p0 + (t * (p1 - p0));
  244. }
  245. /// Returns the cosine interpolated value between @a p0 and @a p1 at time @a t
  246. template <typename T>
  247. inline T cosine(const T& p0, const T& p1, float t)
  248. {
  249. float f = t * math::PI;
  250. float g = (1.0 - math::cos(f)) * 0.5;
  251. return p0 + (g * (p1 - p0));
  252. }
  253. /// Returns the cubic interpolated value between @a p0 and @a p1 at time @a t
  254. template <typename T>
  255. inline T cubic(const T& p0, const T& p1, float t)
  256. {
  257. float tt = t * t;
  258. float ttt = tt * t;
  259. return p0 * (2.0 * ttt - 3.0 * tt + 1.0) + p1 * (3.0 * tt - 2.0 * ttt);
  260. }
  261. /// Bezier interpolation
  262. template <typename T>
  263. inline T bezier(const T& p0, const T& p1, const T& p2, const T& p3, float t)
  264. {
  265. float u = 1.0 - t;
  266. float tt = t * t ;
  267. float uu = u * u;
  268. float uuu = uu * u;
  269. float ttt = tt * t;
  270. T tmp = (uuu * p0) +
  271. (3 * uu * t * p1) +
  272. (3 * u * tt * p2) +
  273. (ttt * p3);
  274. return tmp;
  275. }
  276. /// Catmull-Rom interpolation
  277. template <typename T>
  278. inline T catmull_rom(const T& p0, const T& p1, const T& p2, const T& p3, float t)
  279. {
  280. float tt = t * t;
  281. float ttt = tt * t;
  282. T tmp = (2.0 * p1) +
  283. ((-p0 + p2) * t) +
  284. (((2.0 * p0) - (5.0 * p1) + (4.0 * p2) - p3) * tt) +
  285. ((-p0 + (3.0 * p1) + (-3.0 * p2) + p3) * ttt);
  286. return tmp * 0.5;
  287. }
  288. /// Returns the base64-encoded @a data.
  289. inline char* base64_encode(const unsigned char* data, size_t in_len, size_t* out_len)
  290. {
  291. const char encoding_table[] =
  292. {
  293. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  294. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  295. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  296. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  297. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  298. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  299. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  300. '4', '5', '6', '7', '8', '9', '+', '/'
  301. };
  302. const int mod_table[] = { 0, 2, 1 };
  303. *out_len = 4 * ((in_len + 2) / 3);
  304. char *encoded_data = (char*) default_allocator().allocate(*out_len);
  305. if (encoded_data == NULL) return NULL;
  306. for (size_t i = 0, j = 0; i < in_len;)
  307. {
  308. uint32_t octet_a = i < in_len ? (unsigned char)data[i++] : 0;
  309. uint32_t octet_b = i < in_len ? (unsigned char)data[i++] : 0;
  310. uint32_t octet_c = i < in_len ? (unsigned char)data[i++] : 0;
  311. uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
  312. encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
  313. encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
  314. encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
  315. encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
  316. }
  317. for (int i = 0; i < mod_table[in_len % 3]; i++)
  318. {
  319. encoded_data[*out_len - 1 - i] = '=';
  320. }
  321. (*out_len) += 1;
  322. encoded_data[(*out_len) -1] = '\0';
  323. return encoded_data;
  324. }
  325. // Decodes the base64-encoded @a data.
  326. inline unsigned char* base64_decode(const char *data, size_t in_len, size_t* out_len)
  327. {
  328. CE_ASSERT(in_len % 4 == 0, "Input length not a multiple of 4");
  329. const unsigned char decoding_table[256] =
  330. {
  331. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  332. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  333. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  334. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  335. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  336. 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x3F,
  337. 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
  338. 0x3C, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  339. 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  340. 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
  341. 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
  342. 0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
  343. 0x00, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
  344. 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
  345. 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
  346. 0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00,
  347. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  348. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  349. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  350. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  351. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  352. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  353. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  354. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  355. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  356. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  357. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  358. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  359. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  360. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  361. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  362. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  363. };
  364. *out_len = in_len / 4 * 3;
  365. if (data[in_len - 1] == '=') (*out_len)--;
  366. if (data[in_len - 2] == '=') (*out_len)--;
  367. unsigned char *decoded_data = (unsigned char*) default_allocator().allocate(*out_len);
  368. if (decoded_data == NULL) return NULL;
  369. for (size_t i = 0, j = 0; i < in_len;) {
  370. uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[(unsigned char) data[i++]];
  371. uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[(unsigned char) data[i++]];
  372. uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[(unsigned char) data[i++]];
  373. uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[(unsigned char) data[i++]];
  374. uint32_t triple = (sextet_a << 3 * 6)
  375. + (sextet_b << 2 * 6)
  376. + (sextet_c << 1 * 6)
  377. + (sextet_d << 0 * 6);
  378. if (j < *out_len) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
  379. if (j < *out_len) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
  380. if (j < *out_len) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
  381. }
  382. return decoded_data;
  383. }
  384. } // namespace math
  385. } // namespace crown