Math.cpp 5.8 KB

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