Math.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <Rocket/Core/Math.h>
  29. #include <time.h>
  30. #include <math.h>
  31. namespace Rocket {
  32. namespace Core {
  33. namespace Math {
  34. const float PI = 3.141592653f;
  35. const float PI_BY_TWO = PI * 0.5f;
  36. const float TWO_PI_BY_THREE = PI * 1.5f;
  37. const float TWO_PI = PI * 2;
  38. static const float FZERO = 0.0001f;
  39. // Evaluates if a number is, or close to, zero.
  40. ROCKETCORE_API bool IsZero(float value)
  41. {
  42. return AbsoluteValue(value) < FZERO;
  43. }
  44. // Evaluates if two floating-point numbers are equal, or so similar that they could be considered
  45. // so.
  46. ROCKETCORE_API bool AreEqual(float value_0, float value_1)
  47. {
  48. return IsZero(value_1 - value_0);
  49. }
  50. // Calculates the absolute value of a number.
  51. ROCKETCORE_API float AbsoluteValue(float value)
  52. {
  53. return fabsf(value);
  54. }
  55. // Calculates the cosine of an angle.
  56. ROCKETCORE_API float Cos(float angle)
  57. {
  58. return cosf(angle);
  59. }
  60. // Calculates the arc-cosine of an value.
  61. ROCKETCORE_API float ACos(float value)
  62. {
  63. return acos(value);
  64. }
  65. // Calculates the sine of an angle.
  66. ROCKETCORE_API float Sin(float angle)
  67. {
  68. return sin(angle);
  69. }
  70. // Calculates the arc-sine of an value.
  71. ROCKETCORE_API float ASin(float angle)
  72. {
  73. return asinf(angle);
  74. }
  75. // Calculates the tangent of an angle.
  76. ROCKETCORE_API float Tan(float angle)
  77. {
  78. return tanf(angle);
  79. }
  80. // Calculates the angle of a two-dimensional line.
  81. ROCKETCORE_API float ATan2(float y, float x)
  82. {
  83. return atan2f(y, x);
  84. }
  85. // Converts an angle from radians to degrees.
  86. ROCKETCORE_API float RadiansToDegrees(float angle)
  87. {
  88. return angle * (180.0f / PI);
  89. }
  90. // Converts an angle from degrees to radians.
  91. ROCKETCORE_API float DegreesToRadians(float angle)
  92. {
  93. return angle * (PI / 180.0f);
  94. }
  95. // Normalises and angle in radians
  96. ROCKETCORE_API float NormaliseAngle(float angle)
  97. {
  98. return fmodf(angle, TWO_PI);
  99. }
  100. // Calculates the square root of a value.
  101. ROCKETCORE_API float SquareRoot(float value)
  102. {
  103. return sqrtf(value);
  104. }
  105. // Rounds a floating-point value to the nearest integer.
  106. ROCKETCORE_API int Round(float value)
  107. {
  108. if (value > 0.0f)
  109. return RealToInteger(value + 0.5f);
  110. return RealToInteger(value - 0.5f);
  111. }
  112. // Rounds a floating-point value up to the nearest integer.
  113. ROCKETCORE_API int RoundUp(float value)
  114. {
  115. return RealToInteger(ceilf(value));
  116. }
  117. // Rounds a floating-point value down to the nearest integer.
  118. ROCKETCORE_API int RoundDown(float value)
  119. {
  120. return RealToInteger(floorf(value));
  121. }
  122. // Efficiently truncates a floating-point value into an integer.
  123. ROCKETCORE_API int RealToInteger(float value)
  124. {
  125. #if defined(ROCKET_PLATFORM_WIN32) && !defined(_M_X64) && !defined(__amd64__) && !defined(__MINGW32__)
  126. int i;
  127. _asm
  128. {
  129. mov eax, value; // loaded mem to acc
  130. rcl eax, 1; // left shift acc to remove the sign
  131. mov ebx, eax; // save the acc
  132. mov edx, 4278190080; // clear reg edx;
  133. and eax, edx; // and acc to retrieve the exponent
  134. shr eax, 24;
  135. sub eax, 7fh; // subtract 7fh(127) to get the actual power
  136. mov edx, eax; // save acc val power
  137. mov eax, ebx; // retrieve from ebx
  138. rcl eax, 8; // trim the left 8 bits that contain the power
  139. mov ebx, eax; // store
  140. mov ecx, 1fh; // subtract 17 h
  141. sub ecx, edx;
  142. mov edx, 00000000h;
  143. cmp ecx, 0;
  144. je loop2;
  145. shr eax, 1;
  146. or eax, 80000000h;
  147. loop1:
  148. shr eax, 1; // shift (total bits - power bits);
  149. sub ecx, 1;
  150. add edx, 1;
  151. cmp ecx, 0;
  152. ja loop1;
  153. loop2:
  154. mov i, eax;
  155. // check sign +/-
  156. mov eax, value;
  157. and eax, 80000000h;
  158. cmp eax, 80000000h;
  159. je putsign;
  160. }
  161. return i;
  162. putsign:
  163. return -i;
  164. #else
  165. return (int) value;
  166. #endif
  167. }
  168. // Converts the given number to a power of two, rounding up if necessary.
  169. ROCKETCORE_API int ToPowerOfTwo(int number)
  170. {
  171. // Check if the number is already a power of two.
  172. if ((number & (number - 1)) == 0)
  173. return number;
  174. // Assuming 31 useful bits in an int here ... !
  175. for (int i = 31; i >= 0; i--)
  176. {
  177. if (number & (1 << i))
  178. {
  179. if (i == 31)
  180. return 1 << 31;
  181. else
  182. return 1 << (i + 1);
  183. }
  184. }
  185. return 0;
  186. }
  187. // Converts from a hexadecimal digit to decimal.
  188. ROCKETCORE_API int HexToDecimal(char hex_digit)
  189. {
  190. if (hex_digit >= '0' && hex_digit <= '9')
  191. return hex_digit - '0';
  192. else if (hex_digit >= 'a' && hex_digit <= 'f')
  193. return 10 + (hex_digit - 'a');
  194. else if (hex_digit >= 'A' && hex_digit <= 'F')
  195. return 10 + (hex_digit - 'A');
  196. return -1;
  197. }
  198. // Generates a random floating-point value between 0 and a user-specified value.
  199. ROCKETCORE_API float RandomReal(float max_value)
  200. {
  201. return (rand() / (float) RAND_MAX) * max_value;
  202. }
  203. // Generates a random integer value between 0 and a user-specified value.
  204. ROCKETCORE_API int RandomInteger(int max_value)
  205. {
  206. return (rand() % max_value);
  207. }
  208. // Generates a random boolean value, with equal chance of true or false.
  209. ROCKETCORE_API bool RandomBool()
  210. {
  211. return RandomInteger(2) == 1;
  212. }
  213. }
  214. }
  215. }