Math.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <RmlUi/Core/Math.h>
  29. #include <RmlUi/Core/Types.h>
  30. #include <doctest.h>
  31. using namespace Rml;
  32. TEST_CASE("Math.RoundedLerp")
  33. {
  34. const ColourbPremultiplied c0(0, 0, 0, 255);
  35. const ColourbPremultiplied c1(255, 0, 0, 255);
  36. const ColourbPremultiplied c2(127, 0, 0, 255);
  37. ColourbPremultiplied c;
  38. c = Math::RoundedLerp(0.0f, c0, c1);
  39. REQUIRE(c.red == c0.red);
  40. c = Math::RoundedLerp(179.f / 255.f, c0, c1);
  41. REQUIRE(c.red == 179);
  42. c = Math::RoundedLerp(50.4f / 255.f, c0, c1);
  43. REQUIRE(c.red == 50);
  44. c = Math::RoundedLerp(50.6f / 255.f, c0, c1);
  45. REQUIRE(c.red == 51);
  46. c = Math::RoundedLerp(0.0f, c1, c0);
  47. REQUIRE(c.red == c1.red);
  48. c = Math::RoundedLerp(1.0f, c0, c1);
  49. REQUIRE(c.red == c1.red);
  50. c = Math::RoundedLerp(1.0f, c1, c0);
  51. REQUIRE(c.red == c0.red);
  52. c = Math::RoundedLerp(1.0f, c0, c2);
  53. REQUIRE(c.red == c2.red);
  54. c = Math::RoundedLerp(0.0f, c2, c1);
  55. REQUIRE(c.red == c2.red);
  56. // Full-range interpolation
  57. for (int i = 0; i <= 255; i++)
  58. {
  59. const float t = float(i) / 255.f;
  60. const auto r = Math::RoundedLerp(t, c0, c1).red;
  61. REQUIRE(r == i);
  62. }
  63. // Reverse full-range interpolation
  64. for (int i = 0; i <= 255; i++)
  65. {
  66. const float t = float(i) / 255.f;
  67. const auto r = Math::RoundedLerp(t, c1, c0).red;
  68. REQUIRE(r == 255 - i);
  69. }
  70. // Half-range interpolation
  71. for (int i = 0; i <= 255; i++)
  72. {
  73. const float t = float(i) / 255.f;
  74. const auto r = Math::RoundedLerp(t, c0, c2).red;
  75. REQUIRE(r == i / 2);
  76. }
  77. // Same-color interpolation with fractional t
  78. for (int i = 0; i <= 379; i++)
  79. {
  80. const float t = float(i) / 379.f;
  81. const auto r0 = Math::RoundedLerp(t, c0, c0).red;
  82. const auto r1 = Math::RoundedLerp(t, c1, c1).red;
  83. const auto r2 = Math::RoundedLerp(t, c2, c2).red;
  84. REQUIRE(r0 == c0.red);
  85. REQUIRE(r1 == c1.red);
  86. REQUIRE(r2 == c2.red);
  87. }
  88. }
  89. TEST_CASE("Math.Clamp")
  90. {
  91. // Clamp(Value, Min, Max)
  92. CHECK(Math::Clamp(1, 1, 2) == 1);
  93. CHECK(Math::Clamp(0, 1, 2) == 1);
  94. CHECK(Math::Clamp(3, 1, 2) == 2);
  95. CHECK(Math::Clamp(0, 2, 1) == 2);
  96. CHECK(Math::Clamp(3, 2, 1) == 1);
  97. CHECK(Math::Clamp<Vector2i>({1, 1}, {1, 1}, {2, 2}) == Vector2i(1, 1));
  98. CHECK(Math::Clamp<Vector2i>({0, 0}, {1, 1}, {2, 2}) == Vector2i(1, 1));
  99. CHECK(Math::Clamp<Vector2i>({3, 3}, {1, 1}, {2, 2}) == Vector2i(2, 2));
  100. CHECK(Math::Clamp<Vector2i>({1, 3}, {1, 1}, {2, 2}) == Vector2i(1, 2));
  101. CHECK(Math::Clamp<Vector2i>({1, 1}, {2, 0}, {2, 0}) == Vector2i(2, 0));
  102. CHECK(Math::Clamp<Vector2f>({1, 1}, {1, 1}, {2, 2}) == Vector2f(1, 1));
  103. CHECK(Math::Clamp<Vector2f>({0, 0}, {1, 1}, {2, 2}) == Vector2f(1, 1));
  104. CHECK(Math::Clamp<Vector2f>({3, 3}, {1, 1}, {2, 2}) == Vector2f(2, 2));
  105. CHECK(Math::Clamp<Vector2f>({1, 3}, {1, 1}, {2, 2}) == Vector2f(1, 2));
  106. CHECK(Math::Clamp<Vector2f>({1, 1}, {2, 0}, {2, 0}) == Vector2f(2, 0));
  107. }
  108. TEST_CASE("Math.Min")
  109. {
  110. CHECK(Math::Min(1, 2) == 1);
  111. CHECK(Math::Min(2, 1) == 1);
  112. CHECK(Math::Min<Vector2i>({1, 1}, {2, 2}) == Vector2i(1, 1));
  113. CHECK(Math::Min<Vector2i>({2, 2}, {1, 1}) == Vector2i(1, 1));
  114. CHECK(Math::Min<Vector2i>({2, 1}, {1, 2}) == Vector2i(1, 1));
  115. CHECK(Math::Min<Vector2f>({1, 1}, {2, 2}) == Vector2f(1, 1));
  116. CHECK(Math::Min<Vector2f>({2, 2}, {1, 1}) == Vector2f(1, 1));
  117. CHECK(Math::Min<Vector2f>({2, 1}, {1, 2}) == Vector2f(1, 1));
  118. }
  119. TEST_CASE("Math.Max")
  120. {
  121. CHECK(Math::Max(1, 2) == 2);
  122. CHECK(Math::Max(2, 1) == 2);
  123. CHECK(Math::Max<Vector2i>({1, 1}, {2, 2}) == Vector2i(2, 2));
  124. CHECK(Math::Max<Vector2i>({2, 2}, {1, 1}) == Vector2i(2, 2));
  125. CHECK(Math::Max<Vector2i>({2, 1}, {1, 2}) == Vector2i(2, 2));
  126. CHECK(Math::Max<Vector2f>({1, 1}, {2, 2}) == Vector2f(2, 2));
  127. CHECK(Math::Max<Vector2f>({2, 2}, {1, 1}) == Vector2f(2, 2));
  128. CHECK(Math::Max<Vector2f>({2, 1}, {1, 2}) == Vector2f(2, 2));
  129. }
  130. TEST_CASE("Math.Round")
  131. {
  132. CHECK(Math::Round(-1.0f) == -1.f);
  133. CHECK(Math::Round(0.0f) == 0.f);
  134. CHECK(Math::Round(1.0f) == 1.f);
  135. CHECK(Math::Round(0.49f) == 0.f);
  136. CHECK(Math::Round(0.50f) == 1.f);
  137. CHECK(Math::Round(0.51f) == 1.f);
  138. CHECK(Math::Round(-0.49f) == 0.f);
  139. CHECK(Math::Round(-0.50f) == 0.f); // Always round half up, see commit message.
  140. CHECK(Math::Round(-0.51f) == -1.f);
  141. CHECK(Math::Round(1000.49f) == 1000.f);
  142. CHECK(Math::Round(1000.50f) == 1001.f);
  143. CHECK(Math::Round(1000.51f) == 1001.f);
  144. CHECK(Math::Round(-1000.49f) == -1000.f);
  145. CHECK(Math::Round(-1000.50f) == -1000.f);
  146. CHECK(Math::Round(-1000.51f) == -1001.f);
  147. CHECK(Math::Round(100000.49f) == 100000.f);
  148. CHECK(Math::Round(100000.50f) == 100001.f);
  149. CHECK(Math::Round(100000.51f) == 100001.f);
  150. CHECK(Math::Round(-100000.49f) == -100000.f);
  151. CHECK(Math::Round(-100000.50f) == -100000.f);
  152. CHECK(Math::Round(-100000.51f) == -100001.f);
  153. }