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
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-10-19 16:09:12 +00:00
|
|
|
#include <AK/Concepts.h>
|
2021-04-12 18:20:55 +00:00
|
|
|
#include <AK/Format.h>
|
2020-04-12 17:19:35 +00:00
|
|
|
#include <AK/Forward.h>
|
|
|
|
#include <LibGfx/Forward.h>
|
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
|
|
|
class AffineTransform {
|
|
|
|
public:
|
|
|
|
AffineTransform()
|
|
|
|
: m_values { 1, 0, 0, 1, 0, 0 }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-26 16:23:38 +00:00
|
|
|
AffineTransform(float a, float b, float c, float d, float e, float f)
|
|
|
|
: m_values { a, b, c, d, e, f }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-02 09:40:38 +00:00
|
|
|
[[nodiscard]] bool is_identity() const
|
|
|
|
{
|
|
|
|
return m_values[0] == 1 && m_values[1] == 0 && m_values[2] == 0 && m_values[3] == 1 && m_values[4] == 0 && m_values[5] == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool is_identity_or_translation() const
|
|
|
|
{
|
|
|
|
return m_values[0] == 1 && m_values[1] == 0 && m_values[2] == 0 && m_values[3] == 1;
|
|
|
|
}
|
2020-04-12 17:19:35 +00:00
|
|
|
|
2024-05-27 18:03:52 +00:00
|
|
|
[[nodiscard]] bool is_identity_or_translation_or_scale() const
|
|
|
|
{
|
|
|
|
return m_values[1] == 0 && m_values[2] == 0;
|
|
|
|
}
|
|
|
|
|
2020-04-12 17:19:35 +00:00
|
|
|
void map(float unmapped_x, float unmapped_y, float& mapped_x, float& mapped_y) const;
|
|
|
|
|
2022-10-19 16:09:12 +00:00
|
|
|
template<Arithmetic T>
|
2022-12-06 20:27:44 +00:00
|
|
|
Point<T> map(Point<T>) const;
|
2020-04-12 17:19:35 +00:00
|
|
|
|
2022-10-19 16:09:12 +00:00
|
|
|
template<Arithmetic T>
|
2022-12-06 21:35:32 +00:00
|
|
|
Size<T> map(Size<T>) const;
|
2020-04-12 17:19:35 +00:00
|
|
|
|
2022-10-19 16:09:12 +00:00
|
|
|
template<Arithmetic T>
|
2022-04-01 17:58:27 +00:00
|
|
|
Rect<T> map(Rect<T> const&) const;
|
2020-04-12 17:19:35 +00:00
|
|
|
|
2022-04-07 12:05:30 +00:00
|
|
|
Quad<float> map_to_quad(Rect<float> const&) const;
|
|
|
|
|
2021-04-12 18:20:55 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE float a() const { return m_values[0]; }
|
|
|
|
[[nodiscard]] ALWAYS_INLINE float b() const { return m_values[1]; }
|
|
|
|
[[nodiscard]] ALWAYS_INLINE float c() const { return m_values[2]; }
|
|
|
|
[[nodiscard]] ALWAYS_INLINE float d() const { return m_values[3]; }
|
|
|
|
[[nodiscard]] ALWAYS_INLINE float e() const { return m_values[4]; }
|
|
|
|
[[nodiscard]] ALWAYS_INLINE float f() const { return m_values[5]; }
|
2020-04-12 17:19:35 +00:00
|
|
|
|
2021-04-12 18:20:55 +00:00
|
|
|
[[nodiscard]] float x_scale() const;
|
|
|
|
[[nodiscard]] float y_scale() const;
|
|
|
|
[[nodiscard]] FloatPoint scale() const;
|
|
|
|
[[nodiscard]] float x_translation() const;
|
|
|
|
[[nodiscard]] float y_translation() const;
|
|
|
|
[[nodiscard]] FloatPoint translation() const;
|
2023-04-26 14:53:06 +00:00
|
|
|
[[nodiscard]] float rotation() const;
|
2020-04-12 17:19:35 +00:00
|
|
|
|
|
|
|
AffineTransform& scale(float sx, float sy);
|
2022-12-06 20:57:07 +00:00
|
|
|
AffineTransform& scale(FloatPoint s);
|
2021-04-12 18:20:55 +00:00
|
|
|
AffineTransform& set_scale(float sx, float sy);
|
2022-12-06 20:57:07 +00:00
|
|
|
AffineTransform& set_scale(FloatPoint s);
|
2020-04-12 17:19:35 +00:00
|
|
|
AffineTransform& translate(float tx, float ty);
|
2022-12-06 20:57:07 +00:00
|
|
|
AffineTransform& translate(FloatPoint t);
|
2021-04-12 18:20:55 +00:00
|
|
|
AffineTransform& set_translation(float tx, float ty);
|
2022-12-06 20:57:07 +00:00
|
|
|
AffineTransform& set_translation(FloatPoint t);
|
2020-06-26 16:23:38 +00:00
|
|
|
AffineTransform& rotate_radians(float);
|
2023-04-09 17:31:00 +00:00
|
|
|
AffineTransform& skew_radians(float x_radians, float y_radians);
|
2022-04-01 17:58:27 +00:00
|
|
|
AffineTransform& multiply(AffineTransform const&);
|
2020-04-12 17:19:35 +00:00
|
|
|
|
2023-07-06 18:12:00 +00:00
|
|
|
float determinant() const;
|
2022-03-18 00:17:32 +00:00
|
|
|
Optional<AffineTransform> inverse() const;
|
|
|
|
|
2020-04-12 17:19:35 +00:00
|
|
|
private:
|
|
|
|
float m_values[6] { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2021-04-12 18:20:55 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
struct AK::Formatter<Gfx::AffineTransform> : Formatter<FormatString> {
|
2021-11-16 00:15:21 +00:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, Gfx::AffineTransform const& value)
|
2021-04-12 18:20:55 +00:00
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
return Formatter<FormatString>::format(builder, "[{} {} {} {} {} {}]"sv, value.a(), value.b(), value.c(), value.d(), value.e(), value.f());
|
2021-04-12 18:20:55 +00:00
|
|
|
}
|
|
|
|
};
|