Math.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. return fmodf(angle, RMLUI_PI * 2.0f);
  101. }
  102. RMLUICORE_API float SquareRoot(float value)
  103. {
  104. return sqrtf(value);
  105. }
  106. RMLUICORE_API float Round(float value)
  107. {
  108. return roundf(value);
  109. }
  110. RMLUICORE_API double Round(double value)
  111. {
  112. return round(value);
  113. }
  114. RMLUICORE_API float RoundUp(float value)
  115. {
  116. return ceilf(value);
  117. }
  118. RMLUICORE_API float RoundDown(float value)
  119. {
  120. return floorf(value);
  121. }
  122. RMLUICORE_API int RoundToInteger(float value)
  123. {
  124. if (value > 0.0f)
  125. return int(value + 0.5f);
  126. return int(value - 0.5f);
  127. }
  128. RMLUICORE_API int RoundUpToInteger(float value)
  129. {
  130. return int(ceilf(value));
  131. }
  132. RMLUICORE_API int RoundDownToInteger(float value)
  133. {
  134. return int(floorf(value));
  135. }
  136. RMLUICORE_API float DecomposeFractionalIntegral(float value, float* integral)
  137. {
  138. return modff(value, integral);
  139. }
  140. RMLUICORE_API void SnapToPixelGrid(float& offset, float& width)
  141. {
  142. const float right_edge = offset + width;
  143. offset = Math::Round(offset);
  144. width = Math::Round(right_edge) - offset;
  145. }
  146. RMLUICORE_API void SnapToPixelGrid(Vector2f& position, Vector2f& size)
  147. {
  148. const Vector2f bottom_right = position + size;
  149. position = position.Round();
  150. size = bottom_right.Round() - position;
  151. }
  152. RMLUICORE_API void ExpandToPixelGrid(Vector2f& position, Vector2f& size)
  153. {
  154. const Vector2f bottom_right = position + size;
  155. position = Vector2f(std::floor(position.x), std::floor(position.y));
  156. size = Vector2f(std::ceil(bottom_right.x), std::ceil(bottom_right.y)) - position;
  157. }
  158. RMLUICORE_API int ToPowerOfTwo(int number)
  159. {
  160. // Check if the number is already a power of two.
  161. if ((number & (number - 1)) == 0)
  162. return number;
  163. // Assuming 31 useful bits in an int here ... !
  164. for (int i = 31; i >= 0; i--)
  165. {
  166. if (number & (1 << i))
  167. {
  168. if (i == 31)
  169. return 1 << 31;
  170. else
  171. return 1 << (i + 1);
  172. }
  173. }
  174. return 0;
  175. }
  176. RMLUICORE_API int HexToDecimal(char hex_digit)
  177. {
  178. if (hex_digit >= '0' && hex_digit <= '9')
  179. return hex_digit - '0';
  180. else if (hex_digit >= 'a' && hex_digit <= 'f')
  181. return 10 + (hex_digit - 'a');
  182. else if (hex_digit >= 'A' && hex_digit <= 'F')
  183. return 10 + (hex_digit - 'A');
  184. return -1;
  185. }
  186. RMLUICORE_API float RandomReal(float max_value)
  187. {
  188. return (rand() / (float)RAND_MAX) * max_value;
  189. }
  190. RMLUICORE_API int RandomInteger(int max_value)
  191. {
  192. return (rand() % max_value);
  193. }
  194. RMLUICORE_API bool RandomBool()
  195. {
  196. return RandomInteger(2) == 1;
  197. }
  198. template <>
  199. Vector2f Max<Vector2f>(Vector2f a, Vector2f b)
  200. {
  201. return Vector2f(Max(a.x, b.x), Max(a.y, b.y));
  202. }
  203. template <>
  204. Vector2i Max<Vector2i>(Vector2i a, Vector2i b)
  205. {
  206. return Vector2i(Max(a.x, b.x), Max(a.y, b.y));
  207. }
  208. template <>
  209. Vector2f Min<Vector2f>(Vector2f a, Vector2f b)
  210. {
  211. return Vector2f(Min(a.x, b.x), Min(a.y, b.y));
  212. }
  213. template <>
  214. Vector2i Min<Vector2i>(Vector2i a, Vector2i b)
  215. {
  216. return Vector2i(Min(a.x, b.x), Min(a.y, b.y));
  217. }
  218. template <>
  219. Vector2f Clamp(Vector2f value, Vector2f min, Vector2f max)
  220. {
  221. return Vector2f(Clamp(value.x, min.x, max.x), Clamp(value.y, min.y, max.y));
  222. }
  223. template <>
  224. Vector2i Clamp(Vector2i value, Vector2i min, Vector2i max)
  225. {
  226. return Vector2i(Clamp(value.x, min.x, max.x), Clamp(value.y, min.y, max.y));
  227. }
  228. Colourb RoundedLerp(float t, Colourb v0, Colourb v1)
  229. {
  230. return Colourb{
  231. static_cast<unsigned char>(RoundToInteger(Lerp(t, static_cast<float>(v0[0]), static_cast<float>(v1[0])))),
  232. static_cast<unsigned char>(RoundToInteger(Lerp(t, static_cast<float>(v0[1]), static_cast<float>(v1[1])))),
  233. static_cast<unsigned char>(RoundToInteger(Lerp(t, static_cast<float>(v0[2]), static_cast<float>(v1[2])))),
  234. static_cast<unsigned char>(RoundToInteger(Lerp(t, static_cast<float>(v0[3]), static_cast<float>(v1[3])))),
  235. };
  236. }
  237. } // namespace Math
  238. } // namespace Rml