Math.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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-2023 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 <math.h>
  31. #include <stdlib.h>
  32. #include <time.h>
  33. namespace Rml {
  34. namespace Math {
  35. static constexpr float FZERO = 0.0001f;
  36. RMLUICORE_API bool IsCloseToZero(float value)
  37. {
  38. return Absolute(value) < FZERO;
  39. }
  40. RMLUICORE_API float Absolute(float value)
  41. {
  42. return fabsf(value);
  43. }
  44. RMLUICORE_API int Absolute(int value)
  45. {
  46. return abs(value);
  47. }
  48. RMLUICORE_API Vector2f Absolute(Vector2f value)
  49. {
  50. return {fabsf(value.x), fabsf(value.y)};
  51. }
  52. RMLUICORE_API float Cos(float angle)
  53. {
  54. return cosf(angle);
  55. }
  56. RMLUICORE_API float ACos(float value)
  57. {
  58. return acosf(value);
  59. }
  60. RMLUICORE_API float Sin(float angle)
  61. {
  62. return sinf(angle);
  63. }
  64. RMLUICORE_API float ASin(float angle)
  65. {
  66. return asinf(angle);
  67. }
  68. RMLUICORE_API float Tan(float angle)
  69. {
  70. return tanf(angle);
  71. }
  72. RMLUICORE_API float ATan2(float y, float x)
  73. {
  74. return atan2f(y, x);
  75. }
  76. RMLUICORE_API float Exp(float value)
  77. {
  78. return expf(value);
  79. }
  80. RMLUICORE_API int Log2(int value)
  81. {
  82. int result = 0;
  83. while (value > 1)
  84. {
  85. value >>= 1;
  86. result++;
  87. }
  88. return result;
  89. }
  90. RMLUICORE_API float RadiansToDegrees(float angle)
  91. {
  92. return angle * (180.0f / RMLUI_PI);
  93. }
  94. RMLUICORE_API float DegreesToRadians(float angle)
  95. {
  96. return angle * (RMLUI_PI / 180.0f);
  97. }
  98. RMLUICORE_API float NormaliseAngle(float angle)
  99. {
  100. float result = fmodf(angle, RMLUI_PI * 2.0f);
  101. if (result < 0.f)
  102. result += RMLUI_PI * 2.0f;
  103. return result;
  104. }
  105. RMLUICORE_API float SquareRoot(float value)
  106. {
  107. return sqrtf(value);
  108. }
  109. RMLUICORE_API float Round(float value)
  110. {
  111. return roundf(value);
  112. }
  113. RMLUICORE_API double Round(double value)
  114. {
  115. return round(value);
  116. }
  117. RMLUICORE_API float RoundUp(float value)
  118. {
  119. return ceilf(value);
  120. }
  121. RMLUICORE_API float RoundDown(float value)
  122. {
  123. return floorf(value);
  124. }
  125. RMLUICORE_API int RoundToInteger(float value)
  126. {
  127. if (value > 0.0f)
  128. return int(value + 0.5f);
  129. return int(value - 0.5f);
  130. }
  131. RMLUICORE_API int RoundUpToInteger(float value)
  132. {
  133. return int(ceilf(value));
  134. }
  135. RMLUICORE_API int RoundDownToInteger(float value)
  136. {
  137. return int(floorf(value));
  138. }
  139. RMLUICORE_API float DecomposeFractionalIntegral(float value, float* integral)
  140. {
  141. return modff(value, integral);
  142. }
  143. RMLUICORE_API void SnapToPixelGrid(float& offset, float& width)
  144. {
  145. const float right_edge = offset + width;
  146. offset = Math::Round(offset);
  147. width = Math::Round(right_edge) - offset;
  148. }
  149. RMLUICORE_API void SnapToPixelGrid(Vector2f& position, Vector2f& size)
  150. {
  151. const Vector2f bottom_right = position + size;
  152. position = position.Round();
  153. size = bottom_right.Round() - position;
  154. }
  155. RMLUICORE_API void ExpandToPixelGrid(Vector2f& position, Vector2f& size)
  156. {
  157. const Vector2f bottom_right = position + size;
  158. position = Vector2f(std::floor(position.x), std::floor(position.y));
  159. size = Vector2f(std::ceil(bottom_right.x), std::ceil(bottom_right.y)) - position;
  160. }
  161. RMLUICORE_API void ExpandToPixelGrid(Rectanglef& rectangle)
  162. {
  163. const Vector2f top_left = {std::floor(rectangle.Left()), std::floor(rectangle.Top())};
  164. const Vector2f bottom_right = {std::ceil(rectangle.Right()), std::ceil(rectangle.Bottom())};
  165. rectangle = Rectanglef::FromCorners(top_left, bottom_right);
  166. }
  167. RMLUICORE_API int ToPowerOfTwo(int number)
  168. {
  169. // Check if the number is already a power of two.
  170. if ((number & (number - 1)) == 0)
  171. return number;
  172. // Assuming 31 useful bits in an int here ... !
  173. for (int i = 31; i >= 0; i--)
  174. {
  175. if (number & (1 << i))
  176. {
  177. if (i == 31)
  178. return 1 << 31;
  179. else
  180. return 1 << (i + 1);
  181. }
  182. }
  183. return 0;
  184. }
  185. RMLUICORE_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. RMLUICORE_API float RandomReal(float max_value)
  196. {
  197. return (rand() / (float)RAND_MAX) * max_value;
  198. }
  199. RMLUICORE_API int RandomInteger(int max_value)
  200. {
  201. return (rand() % max_value);
  202. }
  203. RMLUICORE_API bool RandomBool()
  204. {
  205. return RandomInteger(2) == 1;
  206. }
  207. template <>
  208. Vector2f Max<Vector2f>(Vector2f a, Vector2f b)
  209. {
  210. return Vector2f(Max(a.x, b.x), Max(a.y, b.y));
  211. }
  212. template <>
  213. Vector2i Max<Vector2i>(Vector2i a, Vector2i b)
  214. {
  215. return Vector2i(Max(a.x, b.x), Max(a.y, b.y));
  216. }
  217. template <>
  218. Vector2f Min<Vector2f>(Vector2f a, Vector2f b)
  219. {
  220. return Vector2f(Min(a.x, b.x), Min(a.y, b.y));
  221. }
  222. template <>
  223. Vector2i Min<Vector2i>(Vector2i a, Vector2i b)
  224. {
  225. return Vector2i(Min(a.x, b.x), Min(a.y, b.y));
  226. }
  227. template <>
  228. Vector2f Clamp(Vector2f value, Vector2f min, Vector2f max)
  229. {
  230. return Vector2f(Clamp(value.x, min.x, max.x), Clamp(value.y, min.y, max.y));
  231. }
  232. template <>
  233. Vector2i Clamp(Vector2i value, Vector2i min, Vector2i max)
  234. {
  235. return Vector2i(Clamp(value.x, min.x, max.x), Clamp(value.y, min.y, max.y));
  236. }
  237. Colourb RoundedLerp(float t, Colourb v0, Colourb v1)
  238. {
  239. return Colourb{
  240. static_cast<unsigned char>(RoundToInteger(Lerp(t, static_cast<float>(v0[0]), static_cast<float>(v1[0])))),
  241. static_cast<unsigned char>(RoundToInteger(Lerp(t, static_cast<float>(v0[1]), static_cast<float>(v1[1])))),
  242. static_cast<unsigned char>(RoundToInteger(Lerp(t, static_cast<float>(v0[2]), static_cast<float>(v1[2])))),
  243. static_cast<unsigned char>(RoundToInteger(Lerp(t, static_cast<float>(v0[3]), static_cast<float>(v1[3])))),
  244. };
  245. }
  246. } // namespace Math
  247. } // namespace Rml