Math.cpp 6.2 KB

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