/src/serenity/Userland/Libraries/LibGfx/AffineTransform.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/Concepts.h> |
10 | | #include <AK/Format.h> |
11 | | #include <AK/Forward.h> |
12 | | #include <LibGfx/Forward.h> |
13 | | |
14 | | namespace Gfx { |
15 | | |
16 | | class AffineTransform { |
17 | | public: |
18 | | AffineTransform() |
19 | 598k | : m_values { 1, 0, 0, 1, 0, 0 } |
20 | 598k | { |
21 | 598k | } |
22 | | |
23 | | AffineTransform(float a, float b, float c, float d, float e, float f) |
24 | 594k | : m_values { a, b, c, d, e, f } |
25 | 594k | { |
26 | 594k | } |
27 | | |
28 | | [[nodiscard]] bool is_identity() const |
29 | 597k | { |
30 | 597k | 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; |
31 | 597k | } |
32 | | |
33 | | [[nodiscard]] bool is_identity_or_translation() const |
34 | 1.46k | { |
35 | 1.46k | return m_values[0] == 1 && m_values[1] == 0 && m_values[2] == 0 && m_values[3] == 1; |
36 | 1.46k | } |
37 | | |
38 | | enum class AllowNegativeScaling { |
39 | | No, |
40 | | Yes, |
41 | | }; |
42 | | [[nodiscard]] bool is_identity_or_translation_or_scale(AllowNegativeScaling allow_negative_scaling) const |
43 | 0 | { |
44 | 0 | if (allow_negative_scaling == AllowNegativeScaling::No && (m_values[0] < 0 || m_values[3] < 0)) |
45 | 0 | return false; |
46 | 0 | return m_values[1] == 0 && m_values[2] == 0; |
47 | 0 | } |
48 | | |
49 | | void map(float unmapped_x, float unmapped_y, float& mapped_x, float& mapped_y) const; |
50 | | |
51 | | template<Arithmetic T> |
52 | | Point<T> map(Point<T>) const; |
53 | | |
54 | | template<Arithmetic T> |
55 | | Size<T> map(Size<T>) const; |
56 | | |
57 | | template<Arithmetic T> |
58 | | Rect<T> map(Rect<T> const&) const; |
59 | | |
60 | | Quad<float> map_to_quad(Rect<float> const&) const; |
61 | | |
62 | 69.2M | [[nodiscard]] ALWAYS_INLINE float a() const { return m_values[0]; } |
63 | 69.2M | [[nodiscard]] ALWAYS_INLINE float b() const { return m_values[1]; } |
64 | 69.2M | [[nodiscard]] ALWAYS_INLINE float c() const { return m_values[2]; } |
65 | 69.2M | [[nodiscard]] ALWAYS_INLINE float d() const { return m_values[3]; } |
66 | 69.3M | [[nodiscard]] ALWAYS_INLINE float e() const { return m_values[4]; } |
67 | 69.3M | [[nodiscard]] ALWAYS_INLINE float f() const { return m_values[5]; } |
68 | | |
69 | | [[nodiscard]] float x_scale() const; |
70 | | [[nodiscard]] float y_scale() const; |
71 | | [[nodiscard]] FloatPoint scale() const; |
72 | | [[nodiscard]] float x_translation() const; |
73 | | [[nodiscard]] float y_translation() const; |
74 | | [[nodiscard]] FloatPoint translation() const; |
75 | | [[nodiscard]] float rotation() const; |
76 | | |
77 | | AffineTransform& scale(float sx, float sy); |
78 | | AffineTransform& scale(FloatPoint s); |
79 | | AffineTransform& set_scale(float sx, float sy); |
80 | | AffineTransform& set_scale(FloatPoint s); |
81 | | AffineTransform& translate(float tx, float ty); |
82 | | AffineTransform& translate(FloatPoint t); |
83 | | AffineTransform& set_translation(float tx, float ty); |
84 | | AffineTransform& set_translation(FloatPoint t); |
85 | | AffineTransform& rotate_radians(float); |
86 | | AffineTransform& skew_radians(float x_radians, float y_radians); |
87 | | AffineTransform& multiply(AffineTransform const&); |
88 | | |
89 | | float determinant() const; |
90 | | Optional<AffineTransform> inverse() const; |
91 | | |
92 | | private: |
93 | | float m_values[6] { 0 }; |
94 | | }; |
95 | | |
96 | | } |
97 | | |
98 | | template<> |
99 | | struct AK::Formatter<Gfx::AffineTransform> : Formatter<FormatString> { |
100 | | ErrorOr<void> format(FormatBuilder& builder, Gfx::AffineTransform const& value) |
101 | 0 | { |
102 | 0 | return Formatter<FormatString>::format(builder, "[{} {} {} {} {} {}]"sv, value.a(), value.b(), value.c(), value.d(), value.e(), value.f()); |
103 | 0 | } |
104 | | }; |