2
0

Math.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 "../../Include/RmlUi/Core/Types.h"
  30. #include <time.h>
  31. #include <math.h>
  32. #include <stdlib.h>
  33. namespace Rml {
  34. namespace Math {
  35. const float RMLUI_PI = 3.141592653f;
  36. static constexpr 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. RMLUICORE_API int AbsoluteValue(int value)
  54. {
  55. return abs(value);
  56. }
  57. RMLUICORE_API Vector2f AbsoluteValue(Vector2f value)
  58. {
  59. return {fabsf(value.x), fabsf(value.y)};
  60. }
  61. // Calculates the cosine of an angle.
  62. RMLUICORE_API float Cos(float angle)
  63. {
  64. return cosf(angle);
  65. }
  66. // Calculates the arc-cosine of an value.
  67. RMLUICORE_API float ACos(float value)
  68. {
  69. return acosf(value);
  70. }
  71. // Calculates the sine of an angle.
  72. RMLUICORE_API float Sin(float angle)
  73. {
  74. return sinf(angle);
  75. }
  76. // Calculates the arc-sine of an value.
  77. RMLUICORE_API float ASin(float angle)
  78. {
  79. return asinf(angle);
  80. }
  81. // Calculates the tangent of an angle.
  82. RMLUICORE_API float Tan(float angle)
  83. {
  84. return tanf(angle);
  85. }
  86. // Calculates the angle of a two-dimensional line.
  87. RMLUICORE_API float ATan2(float y, float x)
  88. {
  89. return atan2f(y, x);
  90. }
  91. // Evaluates the natural exponential function on a value.
  92. RMLUICORE_API float Exp(float value)
  93. {
  94. return expf(value);
  95. }
  96. // Evaluates the base-2 logarithm of an integer.
  97. RMLUICORE_API int Log2(int value)
  98. {
  99. int result = 0;
  100. while (value > 1)
  101. {
  102. value >>= 1;
  103. result++;
  104. }
  105. return result;
  106. }
  107. // Converts an angle from radians to degrees.
  108. RMLUICORE_API float RadiansToDegrees(float angle)
  109. {
  110. return angle * (180.0f / RMLUI_PI);
  111. }
  112. // Converts an angle from degrees to radians.
  113. RMLUICORE_API float DegreesToRadians(float angle)
  114. {
  115. return angle * (RMLUI_PI / 180.0f);
  116. }
  117. // Normalises an angle in radians
  118. RMLUICORE_API float NormaliseAngle(float angle)
  119. {
  120. return fmodf(angle, RMLUI_PI * 2.0f);
  121. }
  122. // Calculates the square root of a value.
  123. RMLUICORE_API float SquareRoot(float value)
  124. {
  125. return sqrtf(value);
  126. }
  127. // Rounds a floating-point value to the nearest integer.
  128. RMLUICORE_API float RoundFloat(float value)
  129. {
  130. return roundf(value);
  131. }
  132. // Rounds a floating-point value to the nearest integer.
  133. RMLUICORE_API double RoundFloat(double value)
  134. {
  135. return round(value);
  136. }
  137. RMLUICORE_API float RoundUpFloat(float value)
  138. {
  139. return ceilf(value);
  140. }
  141. RMLUICORE_API float RoundDownFloat(float value)
  142. {
  143. return floorf(value);
  144. }
  145. // Rounds a floating-point value to the nearest integer.
  146. RMLUICORE_API int RoundToInteger(float value)
  147. {
  148. if (value > 0.0f)
  149. return RealToInteger(value + 0.5f);
  150. return RealToInteger(value - 0.5f);
  151. }
  152. // Rounds a floating-point value up to the nearest integer.
  153. RMLUICORE_API int RoundUpToInteger(float value)
  154. {
  155. return RealToInteger(ceilf(value));
  156. }
  157. // Rounds a floating-point value down to the nearest integer.
  158. RMLUICORE_API int RoundDownToInteger(float value)
  159. {
  160. return RealToInteger(floorf(value));
  161. }
  162. RMLUICORE_API float DecomposeFractionalIntegral(float value, float* integral)
  163. {
  164. return modff(value, integral);
  165. }
  166. // Efficiently truncates a floating-point value into an integer.
  167. RMLUICORE_API int RealToInteger(float value)
  168. {
  169. return int(value);
  170. }
  171. RMLUICORE_API void SnapToPixelGrid(float& offset, float& width)
  172. {
  173. const float right_edge = offset + width;
  174. offset = Math::RoundFloat(offset);
  175. width = Math::RoundFloat(right_edge) - offset;
  176. }
  177. RMLUICORE_API void SnapToPixelGrid(Vector2f& position, Vector2f& size)
  178. {
  179. const Vector2f bottom_right = position + size;
  180. position = position.Round();
  181. size = bottom_right.Round() - position;
  182. }
  183. RMLUICORE_API void ExpandToPixelGrid(Vector2f& position, Vector2f& size)
  184. {
  185. const Vector2f bottom_right = position + size;
  186. position = Vector2f(std::floor(position.x), std::floor(position.y));
  187. size = Vector2f(std::ceil(bottom_right.x), std::ceil(bottom_right.y)) - position;
  188. }
  189. // Converts the given number to a power of two, rounding up if necessary.
  190. RMLUICORE_API int ToPowerOfTwo(int number)
  191. {
  192. // Check if the number is already a power of two.
  193. if ((number & (number - 1)) == 0)
  194. return number;
  195. // Assuming 31 useful bits in an int here ... !
  196. for (int i = 31; i >= 0; i--)
  197. {
  198. if (number & (1 << i))
  199. {
  200. if (i == 31)
  201. return 1 << 31;
  202. else
  203. return 1 << (i + 1);
  204. }
  205. }
  206. return 0;
  207. }
  208. // Converts from a hexadecimal digit to decimal.
  209. RMLUICORE_API int HexToDecimal(char hex_digit)
  210. {
  211. if (hex_digit >= '0' && hex_digit <= '9')
  212. return hex_digit - '0';
  213. else if (hex_digit >= 'a' && hex_digit <= 'f')
  214. return 10 + (hex_digit - 'a');
  215. else if (hex_digit >= 'A' && hex_digit <= 'F')
  216. return 10 + (hex_digit - 'A');
  217. return -1;
  218. }
  219. // Generates a random floating-point value between 0 and a user-specified value.
  220. RMLUICORE_API float RandomReal(float max_value)
  221. {
  222. return (rand() / (float) RAND_MAX) * max_value;
  223. }
  224. // Generates a random integer value between 0 and a user-specified value.
  225. RMLUICORE_API int RandomInteger(int max_value)
  226. {
  227. return (rand() % max_value);
  228. }
  229. // Generates a random boolean value, with equal chance of true or false.
  230. RMLUICORE_API bool RandomBool()
  231. {
  232. return RandomInteger(2) == 1;
  233. }
  234. template <>
  235. Vector2f Max<Vector2f>(Vector2f a, Vector2f b)
  236. {
  237. return Vector2f(Max(a.x, b.x), Max(a.y, b.y));
  238. }
  239. template <>
  240. Vector2i Max<Vector2i>(Vector2i a, Vector2i b)
  241. {
  242. return Vector2i(Max(a.x, b.x), Max(a.y, b.y));
  243. }
  244. template <>
  245. Vector2f Min<Vector2f>(Vector2f a, Vector2f b)
  246. {
  247. return Vector2f(Min(a.x, b.x), Min(a.y, b.y));
  248. }
  249. template <>
  250. Vector2i Min<Vector2i>(Vector2i a, Vector2i b)
  251. {
  252. return Vector2i(Min(a.x, b.x), Min(a.y, b.y));
  253. }
  254. template <>
  255. Vector2f Clamp(Vector2f value, Vector2f min, Vector2f max)
  256. {
  257. return Vector2f(Clamp(value.x, min.x, max.x), Clamp(value.y, min.y, max.y));
  258. }
  259. template <>
  260. Vector2i Clamp(Vector2i value, Vector2i min, Vector2i max)
  261. {
  262. return Vector2i(Clamp(value.x, min.x, max.x), Clamp(value.y, min.y, max.y));
  263. }
  264. Colourb RoundedLerp(float t, Colourb v0, Colourb v1)
  265. {
  266. return Colourb{
  267. static_cast<unsigned char>(RoundToInteger(Lerp(t,
  268. static_cast<float>(v0[0]), static_cast<float>(v1[0])))),
  269. static_cast<unsigned char>(RoundToInteger(Lerp(t,
  270. static_cast<float>(v0[1]), static_cast<float>(v1[1])))),
  271. static_cast<unsigned char>(RoundToInteger(Lerp(t,
  272. static_cast<float>(v0[2]), static_cast<float>(v1[2])))),
  273. static_cast<unsigned char>(RoundToInteger(Lerp(t,
  274. static_cast<float>(v0[3]), static_cast<float>(v1[3]))))
  275. };
  276. }
  277. }
  278. } // namespace Rml