Math.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "../../Include/Rocket/Core/Math.h"
  29. #include <time.h>
  30. #include <math.h>
  31. namespace Rocket {
  32. namespace Core {
  33. namespace Math {
  34. const float ROCKET_PI = 3.141592653f;
  35. static const float FZERO = 0.0001f;
  36. // Evaluates if a number is, or close to, zero.
  37. ROCKETCORE_API bool IsZero(float value)
  38. {
  39. return AbsoluteValue(value) < FZERO;
  40. }
  41. // Evaluates if two floating-point numbers are equal, or so similar that they could be considered
  42. // so.
  43. ROCKETCORE_API bool AreEqual(float value_0, float value_1)
  44. {
  45. return IsZero(value_1 - value_0);
  46. }
  47. // Calculates the absolute value of a number.
  48. ROCKETCORE_API float AbsoluteValue(float value)
  49. {
  50. return fabsf(value);
  51. }
  52. // Calculates the cosine of an angle.
  53. ROCKETCORE_API float Cos(float angle)
  54. {
  55. return cosf(angle);
  56. }
  57. // Calculates the arc-cosine of an value.
  58. ROCKETCORE_API float ACos(float value)
  59. {
  60. return acos(value);
  61. }
  62. // Calculates the sine of an angle.
  63. ROCKETCORE_API float Sin(float angle)
  64. {
  65. return sin(angle);
  66. }
  67. // Calculates the arc-sine of an value.
  68. ROCKETCORE_API float ASin(float angle)
  69. {
  70. return asinf(angle);
  71. }
  72. // Calculates the tangent of an angle.
  73. ROCKETCORE_API float Tan(float angle)
  74. {
  75. return tanf(angle);
  76. }
  77. // Calculates the angle of a two-dimensional line.
  78. ROCKETCORE_API float ATan2(float y, float x)
  79. {
  80. return atan2f(y, x);
  81. }
  82. // Evaluates the natural exponential function on a value.
  83. ROCKETCORE_API float Exp(float value)
  84. {
  85. return exp(value);
  86. }
  87. // Converts an angle from radians to degrees.
  88. ROCKETCORE_API float RadiansToDegrees(float angle)
  89. {
  90. return angle * (180.0f / ROCKET_PI);
  91. }
  92. // Converts an angle from degrees to radians.
  93. ROCKETCORE_API float DegreesToRadians(float angle)
  94. {
  95. return angle * (ROCKET_PI / 180.0f);
  96. }
  97. // Normalises and angle in radians
  98. ROCKETCORE_API float NormaliseAngle(float angle)
  99. {
  100. return fmodf(angle, ROCKET_PI * 2.0f);
  101. }
  102. // Calculates the square root of a value.
  103. ROCKETCORE_API float SquareRoot(float value)
  104. {
  105. return sqrtf(value);
  106. }
  107. // Rounds a floating-point value to the nearest integer.
  108. ROCKETCORE_API float RoundFloat(float value)
  109. {
  110. return roundf(value);
  111. }
  112. // Rounds a floating-point value to the nearest integer.
  113. ROCKETCORE_API int RoundToInteger(float value)
  114. {
  115. if (value > 0.0f)
  116. return RealToInteger(value + 0.5f);
  117. return RealToInteger(value - 0.5f);
  118. }
  119. // Rounds a floating-point value up to the nearest integer.
  120. ROCKETCORE_API int RoundUpToInteger(float value)
  121. {
  122. return RealToInteger(ceilf(value));
  123. }
  124. // Rounds a floating-point value down to the nearest integer.
  125. ROCKETCORE_API int RoundDownToInteger(float value)
  126. {
  127. return RealToInteger(floorf(value));
  128. }
  129. // Efficiently truncates a floating-point value into an integer.
  130. ROCKETCORE_API int RealToInteger(float value)
  131. {
  132. #if defined(ROCKET_PLATFORM_WIN32) && !defined(_M_X64) && !defined(__amd64__) && !defined(__MINGW32__)
  133. int i;
  134. _asm
  135. {
  136. mov eax, value; // loaded mem to acc
  137. rcl eax, 1; // left shift acc to remove the sign
  138. mov ebx, eax; // save the acc
  139. mov edx, 4278190080; // clear reg edx;
  140. and eax, edx; // and acc to retrieve the exponent
  141. shr eax, 24;
  142. sub eax, 7fh; // subtract 7fh(127) to get the actual power
  143. mov edx, eax; // save acc val power
  144. mov eax, ebx; // retrieve from ebx
  145. rcl eax, 8; // trim the left 8 bits that contain the power
  146. mov ebx, eax; // store
  147. mov ecx, 1fh; // subtract 17 h
  148. sub ecx, edx;
  149. mov edx, 00000000h;
  150. cmp ecx, 0;
  151. je loop2;
  152. shr eax, 1;
  153. or eax, 80000000h;
  154. loop1:
  155. shr eax, 1; // shift (total bits - power bits);
  156. sub ecx, 1;
  157. add edx, 1;
  158. cmp ecx, 0;
  159. ja loop1;
  160. loop2:
  161. mov i, eax;
  162. // check sign +/-
  163. mov eax, value;
  164. and eax, 80000000h;
  165. cmp eax, 80000000h;
  166. je putsign;
  167. }
  168. return i;
  169. putsign:
  170. return -i;
  171. #else
  172. return (int) value;
  173. #endif
  174. }
  175. // Converts the given number to a power of two, rounding up if necessary.
  176. ROCKETCORE_API int ToPowerOfTwo(int number)
  177. {
  178. // Check if the number is already a power of two.
  179. if ((number & (number - 1)) == 0)
  180. return number;
  181. // Assuming 31 useful bits in an int here ... !
  182. for (int i = 31; i >= 0; i--)
  183. {
  184. if (number & (1 << i))
  185. {
  186. if (i == 31)
  187. return 1 << 31;
  188. else
  189. return 1 << (i + 1);
  190. }
  191. }
  192. return 0;
  193. }
  194. // Converts from a hexadecimal digit to decimal.
  195. ROCKETCORE_API int HexToDecimal(char hex_digit)
  196. {
  197. if (hex_digit >= '0' && hex_digit <= '9')
  198. return hex_digit - '0';
  199. else if (hex_digit >= 'a' && hex_digit <= 'f')
  200. return 10 + (hex_digit - 'a');
  201. else if (hex_digit >= 'A' && hex_digit <= 'F')
  202. return 10 + (hex_digit - 'A');
  203. return -1;
  204. }
  205. // Generates a random floating-point value between 0 and a user-specified value.
  206. ROCKETCORE_API float RandomReal(float max_value)
  207. {
  208. return (rand() / (float) RAND_MAX) * max_value;
  209. }
  210. // Generates a random integer value between 0 and a user-specified value.
  211. ROCKETCORE_API int RandomInteger(int max_value)
  212. {
  213. return (rand() % max_value);
  214. }
  215. // Generates a random boolean value, with equal chance of true or false.
  216. ROCKETCORE_API bool RandomBool()
  217. {
  218. return RandomInteger(2) == 1;
  219. }
  220. }
  221. }
  222. }