Math.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // Converts an angle from radians to degrees.
  83. ROCKETCORE_API float RadiansToDegrees(float angle)
  84. {
  85. return angle * (180.0f / ROCKET_PI);
  86. }
  87. // Converts an angle from degrees to radians.
  88. ROCKETCORE_API float DegreesToRadians(float angle)
  89. {
  90. return angle * (ROCKET_PI / 180.0f);
  91. }
  92. // Normalises and angle in radians
  93. ROCKETCORE_API float NormaliseAngle(float angle)
  94. {
  95. return fmodf(angle, ROCKET_PI * 2.0f);
  96. }
  97. // Calculates the square root of a value.
  98. ROCKETCORE_API float SquareRoot(float value)
  99. {
  100. return sqrtf(value);
  101. }
  102. // Rounds a floating-point value to the nearest integer.
  103. ROCKETCORE_API float RoundFloat(float value)
  104. {
  105. return roundf(value);
  106. }
  107. // Rounds a floating-point value to the nearest integer.
  108. ROCKETCORE_API int RoundToInteger(float value)
  109. {
  110. if (value > 0.0f)
  111. return RealToInteger(value + 0.5f);
  112. return RealToInteger(value - 0.5f);
  113. }
  114. // Rounds a floating-point value up to the nearest integer.
  115. ROCKETCORE_API int RoundUpToInteger(float value)
  116. {
  117. return RealToInteger(ceilf(value));
  118. }
  119. // Rounds a floating-point value down to the nearest integer.
  120. ROCKETCORE_API int RoundDownToInteger(float value)
  121. {
  122. return RealToInteger(floorf(value));
  123. }
  124. // Efficiently truncates a floating-point value into an integer.
  125. ROCKETCORE_API int RealToInteger(float value)
  126. {
  127. #if defined(ROCKET_PLATFORM_WIN32) && !defined(_M_X64) && !defined(__amd64__) && !defined(__MINGW32__)
  128. int i;
  129. _asm
  130. {
  131. mov eax, value; // loaded mem to acc
  132. rcl eax, 1; // left shift acc to remove the sign
  133. mov ebx, eax; // save the acc
  134. mov edx, 4278190080; // clear reg edx;
  135. and eax, edx; // and acc to retrieve the exponent
  136. shr eax, 24;
  137. sub eax, 7fh; // subtract 7fh(127) to get the actual power
  138. mov edx, eax; // save acc val power
  139. mov eax, ebx; // retrieve from ebx
  140. rcl eax, 8; // trim the left 8 bits that contain the power
  141. mov ebx, eax; // store
  142. mov ecx, 1fh; // subtract 17 h
  143. sub ecx, edx;
  144. mov edx, 00000000h;
  145. cmp ecx, 0;
  146. je loop2;
  147. shr eax, 1;
  148. or eax, 80000000h;
  149. loop1:
  150. shr eax, 1; // shift (total bits - power bits);
  151. sub ecx, 1;
  152. add edx, 1;
  153. cmp ecx, 0;
  154. ja loop1;
  155. loop2:
  156. mov i, eax;
  157. // check sign +/-
  158. mov eax, value;
  159. and eax, 80000000h;
  160. cmp eax, 80000000h;
  161. je putsign;
  162. }
  163. return i;
  164. putsign:
  165. return -i;
  166. #else
  167. return (int) value;
  168. #endif
  169. }
  170. // Converts the given number to a power of two, rounding up if necessary.
  171. ROCKETCORE_API int ToPowerOfTwo(int number)
  172. {
  173. // Check if the number is already a power of two.
  174. if ((number & (number - 1)) == 0)
  175. return number;
  176. // Assuming 31 useful bits in an int here ... !
  177. for (int i = 31; i >= 0; i--)
  178. {
  179. if (number & (1 << i))
  180. {
  181. if (i == 31)
  182. return 1 << 31;
  183. else
  184. return 1 << (i + 1);
  185. }
  186. }
  187. return 0;
  188. }
  189. // Converts from a hexadecimal digit to decimal.
  190. ROCKETCORE_API int HexToDecimal(char hex_digit)
  191. {
  192. if (hex_digit >= '0' && hex_digit <= '9')
  193. return hex_digit - '0';
  194. else if (hex_digit >= 'a' && hex_digit <= 'f')
  195. return 10 + (hex_digit - 'a');
  196. else if (hex_digit >= 'A' && hex_digit <= 'F')
  197. return 10 + (hex_digit - 'A');
  198. return -1;
  199. }
  200. // Generates a random floating-point value between 0 and a user-specified value.
  201. ROCKETCORE_API float RandomReal(float max_value)
  202. {
  203. return (rand() / (float) RAND_MAX) * max_value;
  204. }
  205. // Generates a random integer value between 0 and a user-specified value.
  206. ROCKETCORE_API int RandomInteger(int max_value)
  207. {
  208. return (rand() % max_value);
  209. }
  210. // Generates a random boolean value, with equal chance of true or false.
  211. ROCKETCORE_API bool RandomBool()
  212. {
  213. return RandomInteger(2) == 1;
  214. }
  215. }
  216. }
  217. }