Math.cpp 7.1 KB

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