AffineTransform.cpp 5.6 KB

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