| 
									
										
										
										
											2020-04-12 19:19:35 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-04-12 19:19:35 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/Optional.h>
 | 
					
						
							|  |  |  | #include <LibGfx/AffineTransform.h>
 | 
					
						
							|  |  |  | #include <LibGfx/Rect.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Gfx { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool AffineTransform::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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static float hypotenuse(float x, float y) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // FIXME: This won't handle overflow :(
 | 
					
						
							| 
									
										
										
										
											2021-07-05 18:56:06 +02:00
										 |  |  |     return sqrtf(x * x + y * y); | 
					
						
							| 
									
										
										
										
											2020-04-12 19:19:35 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | float AffineTransform::x_scale() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return hypotenuse(m_values[0], m_values[1]); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | float AffineTransform::y_scale() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return hypotenuse(m_values[2], m_values[3]); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-12 11:20:55 -07: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 19:19:35 +02: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-04-01 20:58:27 +03:00
										 |  |  | AffineTransform& AffineTransform::scale(FloatPoint const& s) | 
					
						
							| 
									
										
										
										
											2021-04-12 11:20:55 -07: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-04-01 20:58:27 +03:00
										 |  |  | AffineTransform& AffineTransform::set_scale(FloatPoint const& s) | 
					
						
							| 
									
										
										
										
											2021-04-12 11:20:55 -07:00
										 |  |  | { | 
					
						
							|  |  |  |     return set_scale(s.x(), s.y()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-12 19:19:35 +02:00
										 |  |  | AffineTransform& AffineTransform::translate(float tx, float ty) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     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-04-01 20:58:27 +03:00
										 |  |  | AffineTransform& AffineTransform::translate(FloatPoint const& t) | 
					
						
							| 
									
										
										
										
											2021-04-12 11:20:55 -07: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-04-01 20:58:27 +03:00
										 |  |  | AffineTransform& AffineTransform::set_translation(FloatPoint const& t) | 
					
						
							| 
									
										
										
										
											2021-04-12 11:20:55 -07:00
										 |  |  | { | 
					
						
							|  |  |  |     return set_translation(t.x(), t.y()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | AffineTransform& AffineTransform::multiply(AffineTransform const& other) | 
					
						
							| 
									
										
										
										
											2020-06-26 18:23:38 +02: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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     float sin_angle = sinf(radians); | 
					
						
							|  |  |  |     float cos_angle = cosf(radians); | 
					
						
							|  |  |  |     AffineTransform rotation(cos_angle, sin_angle, -sin_angle, cos_angle, 0, 0); | 
					
						
							|  |  |  |     multiply(rotation); | 
					
						
							|  |  |  |     return *this; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-18 01:17:32 +01:00
										 |  |  | Optional<AffineTransform> AffineTransform::inverse() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto determinant = a() * d() - b() * c(); | 
					
						
							|  |  |  |     if (determinant == 0) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     return AffineTransform { | 
					
						
							|  |  |  |         d() / determinant, | 
					
						
							|  |  |  |         -b() / determinant, | 
					
						
							|  |  |  |         -c() / determinant, | 
					
						
							|  |  |  |         a() / determinant, | 
					
						
							|  |  |  |         (c() * f() - d() * e()) / determinant, | 
					
						
							|  |  |  |         (b() * e() - a() * f()) / determinant, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-12 19:19:35 +02:00
										 |  |  | void AffineTransform::map(float unmapped_x, float unmapped_y, float& mapped_x, float& mapped_y) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-04-12 11:20:55 -07:00
										 |  |  |     mapped_x = a() * unmapped_x + b() * unmapped_y + m_values[4]; | 
					
						
							|  |  |  |     mapped_y = c() * unmapped_x + d() * unmapped_y + m_values[5]; | 
					
						
							| 
									
										
										
										
											2020-04-12 19:19:35 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-25 21:31:47 -07:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | IntPoint AffineTransform::map(IntPoint const& point) const | 
					
						
							| 
									
										
										
										
											2020-04-12 19:19:35 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     float mapped_x; | 
					
						
							|  |  |  |     float mapped_y; | 
					
						
							| 
									
										
										
										
											2021-04-12 11:20:55 -07:00
										 |  |  |     map(static_cast<float>(point.x()), static_cast<float>(point.y()), mapped_x, mapped_y); | 
					
						
							| 
									
										
										
										
											2020-07-25 21:31:47 -07:00
										 |  |  |     return { roundf(mapped_x), roundf(mapped_y) }; | 
					
						
							| 
									
										
										
										
											2020-04-12 19:19:35 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-25 21:31:47 -07:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | FloatPoint AffineTransform::map(FloatPoint const& point) const | 
					
						
							| 
									
										
										
										
											2020-04-12 19:19:35 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     float mapped_x; | 
					
						
							|  |  |  |     float mapped_y; | 
					
						
							|  |  |  |     map(point.x(), point.y(), mapped_x, mapped_y); | 
					
						
							| 
									
										
										
										
											2020-07-25 21:31:47 -07:00
										 |  |  |     return { mapped_x, mapped_y }; | 
					
						
							| 
									
										
										
										
											2020-04-12 19:19:35 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-25 21:31:47 -07:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | IntSize AffineTransform::map(IntSize const& size) const | 
					
						
							| 
									
										
										
										
											2020-04-12 19:19:35 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-04-12 11:20:55 -07:00
										 |  |  |     return { | 
					
						
							|  |  |  |         roundf(static_cast<float>(size.width()) * x_scale()), | 
					
						
							|  |  |  |         roundf(static_cast<float>(size.height()) * y_scale()), | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2020-04-12 19:19:35 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-25 21:31:47 -07:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | FloatSize AffineTransform::map(FloatSize const& size) const | 
					
						
							| 
									
										
										
										
											2020-04-12 19:19:35 +02: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-25 21:31:47 -07:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | FloatRect AffineTransform::map(FloatRect const& rect) const | 
					
						
							| 
									
										
										
										
											2020-04-12 19:19:35 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     FloatPoint p1 = map(rect.top_left()); | 
					
						
							|  |  |  |     FloatPoint p2 = map(rect.top_right().translated(1, 0)); | 
					
						
							|  |  |  |     FloatPoint p3 = map(rect.bottom_right().translated(1, 1)); | 
					
						
							|  |  |  |     FloatPoint p4 = map(rect.bottom_left().translated(0, 1)); | 
					
						
							|  |  |  |     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-25 21:31:47 -07:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | IntRect AffineTransform::map(IntRect const& rect) const | 
					
						
							| 
									
										
										
										
											2020-07-25 21:31:47 -07:00
										 |  |  | { | 
					
						
							|  |  |  |     return enclosing_int_rect(map(FloatRect(rect))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-12 19:19:35 +02:00
										 |  |  | } |