2020-04-12 17:19:35 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
|
2020-04-12 17:19:35 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-12 17:19:35 +00:00
|
|
|
*/
|
|
|
|
|
2023-04-26 14:53:06 +00:00
|
|
|
#include <AK/Math.h>
|
2020-04-12 17:19:35 +00:00
|
|
|
#include <AK/Optional.h>
|
|
|
|
#include <LibGfx/AffineTransform.h>
|
2022-04-07 12:05:30 +00:00
|
|
|
#include <LibGfx/Quad.h>
|
2020-04-12 17:19:35 +00:00
|
|
|
#include <LibGfx/Rect.h>
|
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
|
|
|
float AffineTransform::x_scale() const
|
|
|
|
{
|
2022-04-13 19:56:13 +00:00
|
|
|
return AK::hypot(m_values[0], m_values[1]);
|
2020-04-12 17:19:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float AffineTransform::y_scale() const
|
|
|
|
{
|
2022-04-13 19:56:13 +00:00
|
|
|
return AK::hypot(m_values[2], m_values[3]);
|
2020-04-12 17:19:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 18:20:55 +00:00
|
|
|
FloatPoint AffineTransform::scale() const
|
|
|
|
{
|
|
|
|
return { x_scale(), y_scale() };
|
|
|
|
}
|
|
|
|
|
|
|
|
float AffineTransform::x_translation() const
|
|
|
|
{
|
|
|
|
return e();
|
|
|
|
}
|
|
|
|
|
|
|
|
float AffineTransform::y_translation() const
|
|
|
|
{
|
|
|
|
return f();
|
|
|
|
}
|
|
|
|
|
|
|
|
FloatPoint AffineTransform::translation() const
|
|
|
|
{
|
|
|
|
return { x_translation(), y_translation() };
|
|
|
|
}
|
|
|
|
|
2020-04-12 17:19:35 +00:00
|
|
|
AffineTransform& AffineTransform::scale(float sx, float sy)
|
|
|
|
{
|
|
|
|
m_values[0] *= sx;
|
|
|
|
m_values[1] *= sx;
|
|
|
|
m_values[2] *= sy;
|
|
|
|
m_values[3] *= sy;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-12-06 20:57:07 +00:00
|
|
|
AffineTransform& AffineTransform::scale(FloatPoint s)
|
2021-04-12 18:20:55 +00:00
|
|
|
{
|
|
|
|
return scale(s.x(), s.y());
|
|
|
|
}
|
|
|
|
|
|
|
|
AffineTransform& AffineTransform::set_scale(float sx, float sy)
|
|
|
|
{
|
|
|
|
m_values[0] = sx;
|
|
|
|
m_values[1] = 0;
|
|
|
|
m_values[2] = 0;
|
|
|
|
m_values[3] = sy;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-12-06 20:57:07 +00:00
|
|
|
AffineTransform& AffineTransform::set_scale(FloatPoint s)
|
2021-04-12 18:20:55 +00:00
|
|
|
{
|
|
|
|
return set_scale(s.x(), s.y());
|
|
|
|
}
|
|
|
|
|
2023-04-09 17:31:00 +00:00
|
|
|
AffineTransform& AffineTransform::skew_radians(float x_radians, float y_radians)
|
|
|
|
{
|
|
|
|
AffineTransform skew_transform(1, AK::tan(y_radians), AK::tan(x_radians), 1, 0, 0);
|
|
|
|
multiply(skew_transform);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-04-12 17:19:35 +00:00
|
|
|
AffineTransform& AffineTransform::translate(float tx, float ty)
|
|
|
|
{
|
2024-03-02 08:55:30 +00:00
|
|
|
if (is_identity_or_translation()) {
|
|
|
|
m_values[4] += tx;
|
|
|
|
m_values[5] += ty;
|
|
|
|
return *this;
|
|
|
|
}
|
2020-04-12 17:19:35 +00:00
|
|
|
m_values[4] += tx * m_values[0] + ty * m_values[2];
|
|
|
|
m_values[5] += tx * m_values[1] + ty * m_values[3];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-12-06 20:57:07 +00:00
|
|
|
AffineTransform& AffineTransform::translate(FloatPoint t)
|
2021-04-12 18:20:55 +00:00
|
|
|
{
|
|
|
|
return translate(t.x(), t.y());
|
|
|
|
}
|
|
|
|
|
|
|
|
AffineTransform& AffineTransform::set_translation(float tx, float ty)
|
|
|
|
{
|
|
|
|
m_values[4] = tx;
|
|
|
|
m_values[5] = ty;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-12-06 20:57:07 +00:00
|
|
|
AffineTransform& AffineTransform::set_translation(FloatPoint t)
|
2021-04-12 18:20:55 +00:00
|
|
|
{
|
|
|
|
return set_translation(t.x(), t.y());
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
AffineTransform& AffineTransform::multiply(AffineTransform const& other)
|
2020-06-26 16:23:38 +00:00
|
|
|
{
|
2024-03-02 09:48:15 +00:00
|
|
|
if (other.is_identity())
|
|
|
|
return *this;
|
2020-06-26 16:23:38 +00:00
|
|
|
AffineTransform result;
|
|
|
|
result.m_values[0] = other.a() * a() + other.b() * c();
|
|
|
|
result.m_values[1] = other.a() * b() + other.b() * d();
|
|
|
|
result.m_values[2] = other.c() * a() + other.d() * c();
|
|
|
|
result.m_values[3] = other.c() * b() + other.d() * d();
|
|
|
|
result.m_values[4] = other.e() * a() + other.f() * c() + e();
|
|
|
|
result.m_values[5] = other.e() * b() + other.f() * d() + f();
|
|
|
|
*this = result;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
AffineTransform& AffineTransform::rotate_radians(float radians)
|
|
|
|
{
|
2022-04-13 19:56:13 +00:00
|
|
|
float sin_angle;
|
|
|
|
float cos_angle;
|
|
|
|
AK::sincos(radians, sin_angle, cos_angle);
|
2020-06-26 16:23:38 +00:00
|
|
|
AffineTransform rotation(cos_angle, sin_angle, -sin_angle, cos_angle, 0, 0);
|
|
|
|
multiply(rotation);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-07-06 18:12:00 +00:00
|
|
|
float AffineTransform::determinant() const
|
|
|
|
{
|
|
|
|
return a() * d() - b() * c();
|
|
|
|
}
|
|
|
|
|
2022-03-18 00:17:32 +00:00
|
|
|
Optional<AffineTransform> AffineTransform::inverse() const
|
|
|
|
{
|
2023-07-06 18:12:00 +00:00
|
|
|
auto det = determinant();
|
|
|
|
if (det == 0)
|
2022-03-18 00:17:32 +00:00
|
|
|
return {};
|
|
|
|
return AffineTransform {
|
2023-07-06 18:12:00 +00:00
|
|
|
d() / det,
|
|
|
|
-b() / det,
|
|
|
|
-c() / det,
|
|
|
|
a() / det,
|
|
|
|
(c() * f() - d() * e()) / det,
|
|
|
|
(b() * e() - a() * f()) / det,
|
2022-03-18 00:17:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-12 17:19:35 +00:00
|
|
|
void AffineTransform::map(float unmapped_x, float unmapped_y, float& mapped_x, float& mapped_y) const
|
|
|
|
{
|
2022-04-07 02:00:47 +00:00
|
|
|
mapped_x = a() * unmapped_x + c() * unmapped_y + e();
|
|
|
|
mapped_y = b() * unmapped_x + d() * unmapped_y + f();
|
2020-04-12 17:19:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
template<>
|
2022-12-06 20:27:44 +00:00
|
|
|
IntPoint AffineTransform::map(IntPoint point) const
|
2020-04-12 17:19:35 +00:00
|
|
|
{
|
|
|
|
float mapped_x;
|
|
|
|
float mapped_y;
|
2021-04-12 18:20:55 +00:00
|
|
|
map(static_cast<float>(point.x()), static_cast<float>(point.y()), mapped_x, mapped_y);
|
2022-04-13 19:55:07 +00:00
|
|
|
return { round_to<int>(mapped_x), round_to<int>(mapped_y) };
|
2020-04-12 17:19:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
template<>
|
2022-12-06 20:27:44 +00:00
|
|
|
FloatPoint AffineTransform::map(FloatPoint point) const
|
2020-04-12 17:19:35 +00:00
|
|
|
{
|
|
|
|
float mapped_x;
|
|
|
|
float mapped_y;
|
|
|
|
map(point.x(), point.y(), mapped_x, mapped_y);
|
2020-07-26 04:31:47 +00:00
|
|
|
return { mapped_x, mapped_y };
|
2020-04-12 17:19:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
template<>
|
2022-12-06 21:35:32 +00:00
|
|
|
IntSize AffineTransform::map(IntSize size) const
|
2020-04-12 17:19:35 +00:00
|
|
|
{
|
2021-04-12 18:20:55 +00:00
|
|
|
return {
|
2022-04-13 19:55:07 +00:00
|
|
|
round_to<int>(static_cast<float>(size.width()) * x_scale()),
|
|
|
|
round_to<int>(static_cast<float>(size.height()) * y_scale()),
|
2021-04-12 18:20:55 +00:00
|
|
|
};
|
2020-04-12 17:19:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
template<>
|
2022-12-06 21:35:32 +00:00
|
|
|
FloatSize AffineTransform::map(FloatSize size) const
|
2020-04-12 17:19:35 +00:00
|
|
|
{
|
|
|
|
return { size.width() * x_scale(), size.height() * y_scale() };
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static T smallest_of(T p1, T p2, T p3, T p4)
|
|
|
|
{
|
|
|
|
return min(min(p1, p2), min(p3, p4));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static T largest_of(T p1, T p2, T p3, T p4)
|
|
|
|
{
|
|
|
|
return max(max(p1, p2), max(p3, p4));
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
template<>
|
2022-04-01 17:58:27 +00:00
|
|
|
FloatRect AffineTransform::map(FloatRect const& rect) const
|
2020-04-12 17:19:35 +00:00
|
|
|
{
|
2024-03-02 08:55:30 +00:00
|
|
|
if (is_identity()) {
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
if (is_identity_or_translation()) {
|
|
|
|
return rect.translated(e(), f());
|
|
|
|
}
|
2020-04-12 17:19:35 +00:00
|
|
|
FloatPoint p1 = map(rect.top_left());
|
2023-05-21 22:41:18 +00:00
|
|
|
FloatPoint p2 = map(rect.top_right());
|
|
|
|
FloatPoint p3 = map(rect.bottom_right());
|
|
|
|
FloatPoint p4 = map(rect.bottom_left());
|
2020-04-12 17:19:35 +00:00
|
|
|
float left = smallest_of(p1.x(), p2.x(), p3.x(), p4.x());
|
|
|
|
float top = smallest_of(p1.y(), p2.y(), p3.y(), p4.y());
|
|
|
|
float right = largest_of(p1.x(), p2.x(), p3.x(), p4.x());
|
|
|
|
float bottom = largest_of(p1.y(), p2.y(), p3.y(), p4.y());
|
|
|
|
return { left, top, right - left, bottom - top };
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:31:47 +00:00
|
|
|
template<>
|
2022-04-01 17:58:27 +00:00
|
|
|
IntRect AffineTransform::map(IntRect const& rect) const
|
2020-07-26 04:31:47 +00:00
|
|
|
{
|
|
|
|
return enclosing_int_rect(map(FloatRect(rect)));
|
|
|
|
}
|
|
|
|
|
2022-04-07 12:05:30 +00:00
|
|
|
Quad<float> AffineTransform::map_to_quad(Rect<float> const& rect) const
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
map(rect.top_left()),
|
|
|
|
map(rect.top_right()),
|
|
|
|
map(rect.bottom_right()),
|
|
|
|
map(rect.bottom_left()),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-26 14:53:06 +00:00
|
|
|
float AffineTransform::rotation() const
|
|
|
|
{
|
|
|
|
auto rotation = AK::atan2(b(), a());
|
|
|
|
while (rotation < -AK::Pi<float>)
|
|
|
|
rotation += 2.0f * AK::Pi<float>;
|
|
|
|
while (rotation > AK::Pi<float>)
|
|
|
|
rotation -= 2.0f * AK::Pi<float>;
|
|
|
|
return rotation;
|
|
|
|
}
|
|
|
|
|
2020-04-12 17:19:35 +00:00
|
|
|
}
|