AffineTransform.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Optional.h>
  7. #include <LibGfx/AffineTransform.h>
  8. #include <LibGfx/Quad.h>
  9. #include <LibGfx/Rect.h>
  10. namespace Gfx {
  11. bool AffineTransform::is_identity() const
  12. {
  13. 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;
  14. }
  15. bool AffineTransform::is_identity_or_translation() const
  16. {
  17. return a() == 1 && b() == 0 && c() == 0 && d() == 1;
  18. }
  19. static float hypotenuse(float x, float y)
  20. {
  21. // FIXME: This won't handle overflow :(
  22. return sqrtf(x * x + y * y);
  23. }
  24. float AffineTransform::x_scale() const
  25. {
  26. return hypotenuse(m_values[0], m_values[1]);
  27. }
  28. float AffineTransform::y_scale() const
  29. {
  30. return hypotenuse(m_values[2], m_values[3]);
  31. }
  32. FloatPoint AffineTransform::scale() const
  33. {
  34. return { x_scale(), y_scale() };
  35. }
  36. float AffineTransform::x_translation() const
  37. {
  38. return e();
  39. }
  40. float AffineTransform::y_translation() const
  41. {
  42. return f();
  43. }
  44. FloatPoint AffineTransform::translation() const
  45. {
  46. return { x_translation(), y_translation() };
  47. }
  48. AffineTransform& AffineTransform::scale(float sx, float sy)
  49. {
  50. m_values[0] *= sx;
  51. m_values[1] *= sx;
  52. m_values[2] *= sy;
  53. m_values[3] *= sy;
  54. return *this;
  55. }
  56. AffineTransform& AffineTransform::scale(FloatPoint const& s)
  57. {
  58. return scale(s.x(), s.y());
  59. }
  60. AffineTransform& AffineTransform::set_scale(float sx, float sy)
  61. {
  62. m_values[0] = sx;
  63. m_values[1] = 0;
  64. m_values[2] = 0;
  65. m_values[3] = sy;
  66. return *this;
  67. }
  68. AffineTransform& AffineTransform::set_scale(FloatPoint const& s)
  69. {
  70. return set_scale(s.x(), s.y());
  71. }
  72. AffineTransform& AffineTransform::translate(float tx, float ty)
  73. {
  74. m_values[4] += tx * m_values[0] + ty * m_values[2];
  75. m_values[5] += tx * m_values[1] + ty * m_values[3];
  76. return *this;
  77. }
  78. AffineTransform& AffineTransform::translate(FloatPoint const& t)
  79. {
  80. return translate(t.x(), t.y());
  81. }
  82. AffineTransform& AffineTransform::set_translation(float tx, float ty)
  83. {
  84. m_values[4] = tx;
  85. m_values[5] = ty;
  86. return *this;
  87. }
  88. AffineTransform& AffineTransform::set_translation(FloatPoint const& t)
  89. {
  90. return set_translation(t.x(), t.y());
  91. }
  92. AffineTransform& AffineTransform::multiply(AffineTransform const& other)
  93. {
  94. AffineTransform result;
  95. result.m_values[0] = other.a() * a() + other.b() * c();
  96. result.m_values[1] = other.a() * b() + other.b() * d();
  97. result.m_values[2] = other.c() * a() + other.d() * c();
  98. result.m_values[3] = other.c() * b() + other.d() * d();
  99. result.m_values[4] = other.e() * a() + other.f() * c() + e();
  100. result.m_values[5] = other.e() * b() + other.f() * d() + f();
  101. *this = result;
  102. return *this;
  103. }
  104. AffineTransform& AffineTransform::rotate_radians(float radians)
  105. {
  106. float sin_angle = sinf(radians);
  107. float cos_angle = cosf(radians);
  108. AffineTransform rotation(cos_angle, sin_angle, -sin_angle, cos_angle, 0, 0);
  109. multiply(rotation);
  110. return *this;
  111. }
  112. Optional<AffineTransform> AffineTransform::inverse() const
  113. {
  114. auto determinant = a() * d() - b() * c();
  115. if (determinant == 0)
  116. return {};
  117. return AffineTransform {
  118. d() / determinant,
  119. -b() / determinant,
  120. -c() / determinant,
  121. a() / determinant,
  122. (c() * f() - d() * e()) / determinant,
  123. (b() * e() - a() * f()) / determinant,
  124. };
  125. }
  126. void AffineTransform::map(float unmapped_x, float unmapped_y, float& mapped_x, float& mapped_y) const
  127. {
  128. mapped_x = a() * unmapped_x + c() * unmapped_y + e();
  129. mapped_y = b() * unmapped_x + d() * unmapped_y + f();
  130. }
  131. template<>
  132. IntPoint AffineTransform::map(IntPoint const& point) const
  133. {
  134. float mapped_x;
  135. float mapped_y;
  136. map(static_cast<float>(point.x()), static_cast<float>(point.y()), mapped_x, mapped_y);
  137. return { roundf(mapped_x), roundf(mapped_y) };
  138. }
  139. template<>
  140. FloatPoint AffineTransform::map(FloatPoint const& point) const
  141. {
  142. float mapped_x;
  143. float mapped_y;
  144. map(point.x(), point.y(), mapped_x, mapped_y);
  145. return { mapped_x, mapped_y };
  146. }
  147. template<>
  148. IntSize AffineTransform::map(IntSize const& size) const
  149. {
  150. return {
  151. roundf(static_cast<float>(size.width()) * x_scale()),
  152. roundf(static_cast<float>(size.height()) * y_scale()),
  153. };
  154. }
  155. template<>
  156. FloatSize AffineTransform::map(FloatSize const& size) const
  157. {
  158. return { size.width() * x_scale(), size.height() * y_scale() };
  159. }
  160. template<typename T>
  161. static T smallest_of(T p1, T p2, T p3, T p4)
  162. {
  163. return min(min(p1, p2), min(p3, p4));
  164. }
  165. template<typename T>
  166. static T largest_of(T p1, T p2, T p3, T p4)
  167. {
  168. return max(max(p1, p2), max(p3, p4));
  169. }
  170. template<>
  171. FloatRect AffineTransform::map(FloatRect const& rect) const
  172. {
  173. FloatPoint p1 = map(rect.top_left());
  174. FloatPoint p2 = map(rect.top_right().translated(1, 0));
  175. FloatPoint p3 = map(rect.bottom_right().translated(1, 1));
  176. FloatPoint p4 = map(rect.bottom_left().translated(0, 1));
  177. float left = smallest_of(p1.x(), p2.x(), p3.x(), p4.x());
  178. float top = smallest_of(p1.y(), p2.y(), p3.y(), p4.y());
  179. float right = largest_of(p1.x(), p2.x(), p3.x(), p4.x());
  180. float bottom = largest_of(p1.y(), p2.y(), p3.y(), p4.y());
  181. return { left, top, right - left, bottom - top };
  182. }
  183. template<>
  184. IntRect AffineTransform::map(IntRect const& rect) const
  185. {
  186. return enclosing_int_rect(map(FloatRect(rect)));
  187. }
  188. Quad<float> AffineTransform::map_to_quad(Rect<float> const& rect) const
  189. {
  190. return {
  191. map(rect.top_left()),
  192. map(rect.top_right()),
  193. map(rect.bottom_right()),
  194. map(rect.bottom_left()),
  195. };
  196. }
  197. }