SFML/test/Graphics/Transform.test.cpp

224 lines
12 KiB
C++
Raw Permalink Normal View History

2021-12-24 08:18:33 +08:00
#include <SFML/Graphics/Transform.hpp>
2023-04-24 20:13:52 +08:00
#include <SFML/System/Angle.hpp>
2021-12-24 08:18:33 +08:00
2023-01-18 12:51:08 +08:00
#include <catch2/catch_test_macros.hpp>
2021-12-24 08:18:33 +08:00
2022-07-05 00:20:58 +08:00
#include <GraphicsUtil.hpp>
2023-01-18 12:51:08 +08:00
#include <sstream>
#include <type_traits>
2022-07-05 00:20:58 +08:00
#include <vector>
2023-04-24 20:13:52 +08:00
#include <cassert>
2023-01-18 12:51:08 +08:00
TEST_CASE("[Graphics] sf::Transform")
2021-12-24 08:18:33 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("Type traits")
2022-06-29 02:13:32 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(std::is_copy_constructible_v<sf::Transform>);
STATIC_CHECK(std::is_copy_assignable_v<sf::Transform>);
STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::Transform>);
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::Transform>);
2022-06-29 02:13:32 +08:00
}
2021-12-24 08:18:33 +08:00
2023-01-18 12:51:08 +08:00
SECTION("Construction")
2021-12-24 08:18:33 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("Default constructor")
2021-12-24 08:18:33 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::Transform() == sf::Transform::Identity);
2021-12-24 08:18:33 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("3x3 matrix constructor")
2021-12-24 08:18:33 +08:00
{
2023-01-18 12:51:08 +08:00
constexpr sf::Transform transform(10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f);
const std::vector matrix(transform.getMatrix(), transform.getMatrix() + 16);
CHECK(matrix ==
std::vector{10.0f, 13.0f, 0.0f, 16.0f, 11.0f, 14.0f, 0.0f, 17.0f, 0.0f, 0.0f, 1.0f, 0.0f, 12.0f, 15.0f, 0.0f, 18.0f});
2021-12-24 08:18:33 +08:00
}
}
2023-01-18 12:51:08 +08:00
SECTION("Identity matrix")
2021-12-24 08:18:33 +08:00
{
2023-01-18 12:51:08 +08:00
const std::vector matrix(sf::Transform::Identity.getMatrix(), sf::Transform::Identity.getMatrix() + 16);
2022-07-05 00:20:58 +08:00
CHECK(matrix ==
2023-01-18 12:51:08 +08:00
std::vector{1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f});
2021-12-24 08:18:33 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("getInverse()")
2021-12-24 08:18:33 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::Transform::Identity.getInverse() == sf::Transform::Identity);
STATIC_CHECK(sf::Transform(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f).getInverse() ==
sf::Transform::Identity);
STATIC_CHECK(sf::Transform(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 4.0f, 3.0f, 2.0f, 1.0f).getInverse() ==
sf::Transform(0.375f, -0.5f, 0.875f, -1.0f, 1.0f, -1.0f, 0.875f, -0.5f, 0.375f));
2021-12-24 08:18:33 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("transformPoint()")
2021-12-24 08:18:33 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::Transform::Identity.transformPoint({-10.0f, -10.0f}) == sf::Vector2f(-10.0f, -10.0f));
STATIC_CHECK(sf::Transform::Identity.transformPoint({-1.0f, -1.0f}) == sf::Vector2f(-1.0f, -1.0f));
STATIC_CHECK(sf::Transform::Identity.transformPoint({-1.0f, 0.0f}) == sf::Vector2f(-1.0f, 0.0f));
STATIC_CHECK(sf::Transform::Identity.transformPoint({0.0f, 0.0f}) == sf::Vector2f(0.0f, 0.0f));
STATIC_CHECK(sf::Transform::Identity.transformPoint({0.0f, 1.0f}) == sf::Vector2f(0.0f, 1.0f));
STATIC_CHECK(sf::Transform::Identity.transformPoint({1.0f, 1.0f}) == sf::Vector2f(1.0f, 1.0f));
STATIC_CHECK(sf::Transform::Identity.transformPoint({10.0f, 10.0f}) == sf::Vector2f(10.0f, 10.0f));
constexpr sf::Transform transform(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 4.0f, 3.0f, 2.0f, 1.0f);
STATIC_CHECK(transform.transformPoint({-1.0f, -1.0f}) == sf::Vector2f(0.0f, -5.0f));
STATIC_CHECK(transform.transformPoint({0.0f, 0.0f}) == sf::Vector2f(3.0f, 4.0f));
STATIC_CHECK(transform.transformPoint({1.0f, 1.0f}) == sf::Vector2f(6.0f, 13.0f));
2021-12-24 08:18:33 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("transformRect()")
2021-12-24 08:18:33 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::Transform::Identity.transformRect({{-200.0f, -200.0f}, {-100.0f, -100.0f}}) ==
sf::FloatRect({-300.0f, -300.0f}, {100.0f, 100.0f}));
STATIC_CHECK(sf::Transform::Identity.transformRect({{0.0f, 0.0f}, {0.0f, 0.0f}}) ==
sf::FloatRect({0.0f, 0.0f}, {0.0f, 0.0f}));
STATIC_CHECK(sf::Transform::Identity.transformRect({{100.0f, 100.0f}, {200.0f, 200.0f}}) ==
sf::FloatRect({100.0f, 100.0f}, {200.0f, 200.0f}));
constexpr sf::Transform transform(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 4.0f, 3.0f, 2.0f, 1.0f);
STATIC_CHECK(transform.transformRect({{-100.0f, -100.0f}, {200.0f, 200.0f}}) ==
sf::FloatRect({-297.0f, -896.0f}, {600.0f, 1800.0f}));
STATIC_CHECK(transform.transformRect({{0.0f, 0.0f}, {0.0f, 0.0f}}) == sf::FloatRect({3.0f, 4.0f}, {0.0f, 0.0f}));
STATIC_CHECK(transform.transformRect({{100.0f, 100.0f}, {200.0f, 200.0f}}) ==
sf::FloatRect({303.0f, 904.0f}, {600.0f, 1800.0f}));
2021-12-24 08:18:33 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("combine()")
2021-12-24 08:18:33 +08:00
{
auto identity = sf::Transform::Identity;
CHECK(identity.combine(sf::Transform::Identity) == sf::Transform::Identity);
CHECK(identity.combine(sf::Transform::Identity).combine(sf::Transform::Identity) == sf::Transform::Identity);
2022-07-05 00:20:58 +08:00
sf::Transform transform(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 4.0f, 3.0f, 2.0f, 1.0f);
2021-12-24 08:18:33 +08:00
CHECK(identity.combine(transform) == transform);
CHECK(transform.combine(sf::Transform::Identity) == transform);
2022-07-05 00:20:58 +08:00
CHECK(transform.combine(transform) == sf::Transform(18.0f, 18.0f, 14.0f, 36.0f, 41.0f, 36.0f, 14.0f, 18.0f, 18.0f));
CHECK(transform.combine(sf::Transform(10.0f, 2.0f, 3.0f, 4.0f, 50.0f, 40.0f, 30.0f, 20.0f, 10.0f)) ==
sf::Transform(672.0f, 1216.0f, 914.0f, 1604.0f, 2842.0f, 2108.0f, 752.0f, 1288.0f, 942.0f));
2021-12-24 08:18:33 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("translate()")
2021-12-24 08:18:33 +08:00
{
sf::Transform transform(9, 8, 7, 6, 5, 4, 3, 2, 1);
CHECK(transform.translate({10.0f, 20.0f}) == sf::Transform(9, 8, 257, 6, 5, 164, 3, 2, 71));
CHECK(transform.translate({10.0f, 20.0f}) == sf::Transform(9, 8, 507, 6, 5, 324, 3, 2, 141));
}
2023-01-18 12:51:08 +08:00
SECTION("rotate()")
2021-12-24 08:18:33 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("Around origin")
2021-12-24 08:18:33 +08:00
{
sf::Transform transform;
transform.rotate(sf::degrees(90));
2022-07-05 00:20:58 +08:00
CHECK(transform == Approx(sf::Transform(0, -1, 0, 1, 0, 0, 0, 0, 1)));
2021-12-24 08:18:33 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("Around custom point")
2021-12-24 08:18:33 +08:00
{
sf::Transform transform;
transform.rotate(sf::degrees(90), {1.0f, 0.0f});
2022-07-05 00:20:58 +08:00
CHECK(transform == Approx(sf::Transform(0, -1, 1, 1, 0, -1, 0, 0, 1)));
2021-12-24 08:18:33 +08:00
}
}
2023-01-18 12:51:08 +08:00
SECTION("scale()")
2021-12-24 08:18:33 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("About origin")
2021-12-24 08:18:33 +08:00
{
sf::Transform transform(1, 2, 3, 4, 5, 4, 3, 2, 1);
CHECK(transform.scale({2.0f, 4.0f}) == sf::Transform(2, 8, 3, 8, 20, 4, 6, 8, 1));
CHECK(transform.scale({0.0f, 0.0f}) == sf::Transform(0, 0, 3, 0, 0, 4, 0, 0, 1));
CHECK(transform.scale({10.0f, 10.0f}) == sf::Transform(0, 0, 3, 0, 0, 4, 0, 0, 1));
}
2023-01-18 12:51:08 +08:00
SECTION("About custom point")
2021-12-24 08:18:33 +08:00
{
sf::Transform transform(1, 2, 3, 4, 5, 4, 3, 2, 1);
CHECK(transform.scale({1.0f, 2.0f}, {1.0f, 0.0f}) == sf::Transform(1, 4, 3, 4, 10, 4, 3, 4, 1));
CHECK(transform.scale({0.0f, 0.0f}, {1.0f, 0.0f}) == sf::Transform(0, 0, 4, 0, 0, 8, 0, 0, 4));
}
}
2023-01-18 12:51:08 +08:00
SECTION("Operators")
2021-12-24 08:18:33 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("operator*")
2021-12-24 08:18:33 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::Transform::Identity * sf::Transform::Identity == sf::Transform::Identity);
STATIC_CHECK(sf::Transform::Identity * sf::Transform::Identity * sf::Transform::Identity ==
sf::Transform::Identity);
constexpr sf::Transform transform(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 4.0f, 3.0f, 2.0f, 1.0f);
STATIC_CHECK(sf::Transform::Identity * transform == transform);
STATIC_CHECK(transform * sf::Transform::Identity == transform);
STATIC_CHECK(transform * transform ==
sf::Transform(18.0f, 18.0f, 14.0f, 36.0f, 41.0f, 36.0f, 14.0f, 18.0f, 18.0f));
STATIC_CHECK(transform * sf::Transform(10.0f, 2.0f, 3.0f, 4.0f, 50.0f, 40.0f, 30.0f, 20.0f, 10.0f) ==
sf::Transform(108.0f, 162.0f, 113.0f, 180.0f, 338.0f, 252.0f, 68.0f, 126.0f, 99.0f));
2021-12-24 08:18:33 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator*=")
2021-12-24 08:18:33 +08:00
{
2022-07-05 00:20:58 +08:00
sf::Transform transform(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 4.0f, 3.0f, 2.0f, 1.0f);
2021-12-24 08:18:33 +08:00
transform *= sf::Transform::Identity;
CHECK(transform == sf::Transform(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 4.0f, 3.0f, 2.0f, 1.0f));
transform *= transform;
2022-07-05 00:20:58 +08:00
CHECK(transform == sf::Transform(18.0f, 18.0f, 14.0f, 36.0f, 41.0f, 36.0f, 14.0f, 18.0f, 18.0f));
transform *= sf::Transform(10.0f, 2.0f, 3.0f, 4.0f, 50.0f, 40.0f, 30.0f, 20.0f, 10.0f);
CHECK(transform == sf::Transform(672.0f, 1216.0f, 914.0f, 1604.0f, 2842.0f, 2108.0f, 752.0f, 1288.0f, 942.0f));
2021-12-24 08:18:33 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator* with vector")
2021-12-24 08:18:33 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::Transform::Identity * sf::Vector2f(-10.0f, -10.0f) == sf::Vector2f(-10.0f, -10.0f));
STATIC_CHECK(sf::Transform::Identity * sf::Vector2f(-1.0f, -1.0f) == sf::Vector2f(-1.0f, -1.0f));
STATIC_CHECK(sf::Transform::Identity * sf::Vector2f(-1.0f, 0.0f) == sf::Vector2f(-1.0f, 0.0f));
STATIC_CHECK(sf::Transform::Identity * sf::Vector2f(0.0f, 0.0f) == sf::Vector2f(0.0f, 0.0f));
STATIC_CHECK(sf::Transform::Identity * sf::Vector2f(0.0f, 1.0f) == sf::Vector2f(0.0f, 1.0f));
STATIC_CHECK(sf::Transform::Identity * sf::Vector2f(1.0f, 1.0f) == sf::Vector2f(1.0f, 1.0f));
STATIC_CHECK(sf::Transform::Identity * sf::Vector2f(10.0f, 10.0f) == sf::Vector2f(10.0f, 10.0f));
constexpr sf::Transform transform(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 4.0f, 3.0f, 2.0f, 1.0f);
STATIC_CHECK(transform * sf::Vector2f(-1.0f, -1.0f) == sf::Vector2f(0.0f, -5.0f));
STATIC_CHECK(transform * sf::Vector2f(0.0f, 0.0f) == sf::Vector2f(3.0f, 4.0f));
STATIC_CHECK(transform * sf::Vector2f(1.0f, 1.0f) == sf::Vector2f(6.0f, 13.0f));
2021-12-24 08:18:33 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator==")
2021-12-24 08:18:33 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK(sf::Transform::Identity == sf::Transform::Identity);
STATIC_CHECK(sf::Transform() == sf::Transform());
STATIC_CHECK(sf::Transform(0, 0, 0, 0, 0, 0, 0, 0, 0) == sf::Transform(0, 0, 0, 0, 0, 0, 0, 0, 0));
STATIC_CHECK(sf::Transform(0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f) ==
sf::Transform(0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f));
STATIC_CHECK(sf::Transform(1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f) ==
sf::Transform(1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f));
2021-12-24 08:18:33 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("operator!=")
2021-12-24 08:18:33 +08:00
{
2023-01-18 12:51:08 +08:00
STATIC_CHECK_FALSE(sf::Transform::Identity != sf::Transform::Identity);
STATIC_CHECK_FALSE(sf::Transform() != sf::Transform());
STATIC_CHECK_FALSE(sf::Transform(0, 0, 0, 0, 0, 0, 0, 0, 0) != sf::Transform(0, 0, 0, 0, 0, 0, 0, 0, 0));
STATIC_CHECK_FALSE(
sf::Transform(0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f) !=
sf::Transform(0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f, 0.0001f));
STATIC_CHECK_FALSE(
sf::Transform(1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f) !=
sf::Transform(1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f, 1000.0f));
STATIC_CHECK(sf::Transform(1, 0, 0, 0, 0, 0, 0, 0, 0) != sf::Transform(0, 0, 0, 0, 0, 0, 0, 0, 0));
STATIC_CHECK(sf::Transform(0, 1, 0, 0, 0, 0, 0, 0, 0) != sf::Transform(0, 0, 0, 0, 0, 0, 0, 0, 0));
STATIC_CHECK(sf::Transform(0, 0, 1, 0, 0, 0, 0, 0, 0) != sf::Transform(0, 0, 0, 0, 0, 0, 0, 0, 0));
STATIC_CHECK(sf::Transform(0, 0, 0, 1, 0, 0, 0, 0, 0) != sf::Transform(0, 0, 0, 0, 0, 0, 0, 0, 0));
STATIC_CHECK(sf::Transform(0, 0, 0, 0, 1, 0, 0, 0, 0) != sf::Transform(0, 0, 0, 0, 0, 0, 0, 0, 0));
STATIC_CHECK(sf::Transform(0, 0, 0, 0, 0, 1, 0, 0, 0) != sf::Transform(0, 0, 0, 0, 0, 0, 0, 0, 0));
STATIC_CHECK(sf::Transform(0, 0, 0, 0, 0, 0, 1, 0, 0) != sf::Transform(0, 0, 0, 0, 0, 0, 0, 0, 0));
STATIC_CHECK(sf::Transform(0, 0, 0, 0, 0, 0, 0, 1, 0) != sf::Transform(0, 0, 0, 0, 0, 0, 0, 0, 0));
STATIC_CHECK(sf::Transform(0, 0, 0, 0, 0, 0, 0, 0, 1) != sf::Transform(0, 0, 0, 0, 0, 0, 0, 0, 0));
2021-12-24 08:18:33 +08:00
}
}
}