Math.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 SnapToPixelGrid(Rectanglef& rectangle)
  156. {
  157. rectangle = Rectanglef::FromCorners(rectangle.TopLeft().Round(), rectangle.BottomRight().Round());
  158. }
  159. RMLUICORE_API void ExpandToPixelGrid(Vector2f& position, Vector2f& size)
  160. {
  161. const Vector2f bottom_right = position + size;
  162. position = Vector2f(std::floor(position.x), std::floor(position.y));
  163. size = Vector2f(std::ceil(bottom_right.x), std::ceil(bottom_right.y)) - position;
  164. }
  165. RMLUICORE_API void ExpandToPixelGrid(Rectanglef& rectangle)
  166. {
  167. const Vector2f top_left = {std::floor(rectangle.Left()), std::floor(rectangle.Top())};
  168. const Vector2f bottom_right = {std::ceil(rectangle.Right()), std::ceil(rectangle.Bottom())};
  169. rectangle = Rectanglef::FromCorners(top_left, bottom_right);
  170. }
  171. RMLUICORE_API int ToPowerOfTwo(int number)
  172. {
  173. // Check if the number is already a power of two.
  174. if ((number & (number - 1)) == 0)
  175. return number;
  176. // Assuming 31 useful bits in an int here ... !
  177. for (int i = 31; i >= 0; i--)
  178. {
  179. if (number & (1 << i))
  180. {
  181. if (i == 31)
  182. return 1 << 31;
  183. else
  184. return 1 << (i + 1);
  185. }
  186. }
  187. return 0;
  188. }
  189. RMLUICORE_API int HexToDecimal(char hex_digit)
  190. {
  191. if (hex_digit >= '0' && hex_digit <= '9')
  192. return hex_digit - '0';
  193. else if (hex_digit >= 'a' && hex_digit <= 'f')
  194. return 10 + (hex_digit - 'a');
  195. else if (hex_digit >= 'A' && hex_digit <= 'F')
  196. return 10 + (hex_digit - 'A');
  197. return -1;
  198. }
  199. RMLUICORE_API float RandomReal(float max_value)
  200. {
  201. return (rand() / (float)RAND_MAX) * max_value;
  202. }
  203. RMLUICORE_API int RandomInteger(int max_value)
  204. {
  205. return (rand() % max_value);
  206. }
  207. RMLUICORE_API bool RandomBool()
  208. {
  209. return RandomInteger(2) == 1;
  210. }
  211. template <>
  212. Vector2f Max<Vector2f>(Vector2f a, Vector2f b)
  213. {
  214. return Vector2f(Max(a.x, b.x), Max(a.y, b.y));
  215. }
  216. template <>
  217. Vector2i Max<Vector2i>(Vector2i a, Vector2i b)
  218. {
  219. return Vector2i(Max(a.x, b.x), Max(a.y, b.y));
  220. }
  221. template <>
  222. Vector2f Min<Vector2f>(Vector2f a, Vector2f b)
  223. {
  224. return Vector2f(Min(a.x, b.x), Min(a.y, b.y));
  225. }
  226. template <>
  227. Vector2i Min<Vector2i>(Vector2i a, Vector2i b)
  228. {
  229. return Vector2i(Min(a.x, b.x), Min(a.y, b.y));
  230. }
  231. template <>
  232. Vector2f Clamp(Vector2f value, Vector2f min, Vector2f max)
  233. {
  234. return Vector2f(Clamp(value.x, min.x, max.x), Clamp(value.y, min.y, max.y));
  235. }
  236. template <>
  237. Vector2i Clamp(Vector2i value, Vector2i min, Vector2i max)
  238. {
  239. return Vector2i(Clamp(value.x, min.x, max.x), Clamp(value.y, min.y, max.y));
  240. }
  241. ColourbPremultiplied RoundedLerp(float t, ColourbPremultiplied v0, ColourbPremultiplied v1)
  242. {
  243. return ColourbPremultiplied{
  244. static_cast<unsigned char>(RoundToInteger(Lerp(t, static_cast<float>(v0[0]), static_cast<float>(v1[0])))),
  245. static_cast<unsigned char>(RoundToInteger(Lerp(t, static_cast<float>(v0[1]), static_cast<float>(v1[1])))),
  246. static_cast<unsigned char>(RoundToInteger(Lerp(t, static_cast<float>(v0[2]), static_cast<float>(v1[2])))),
  247. static_cast<unsigned char>(RoundToInteger(Lerp(t, static_cast<float>(v0[3]), static_cast<float>(v1[3])))),
  248. };
  249. }
  250. } // namespace Math
  251. } // namespace Rml