Math.cpp 5.9 KB

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