AffineTransform.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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::translate(float tx, float ty)
  68. {
  69. m_values[4] += tx * m_values[0] + ty * m_values[2];
  70. m_values[5] += tx * m_values[1] + ty * m_values[3];
  71. return *this;
  72. }
  73. AffineTransform& AffineTransform::translate(FloatPoint t)
  74. {
  75. return translate(t.x(), t.y());
  76. }
  77. AffineTransform& AffineTransform::set_translation(float tx, float ty)
  78. {
  79. m_values[4] = tx;
  80. m_values[5] = ty;
  81. return *this;
  82. }
  83. AffineTransform& AffineTransform::set_translation(FloatPoint t)
  84. {
  85. return set_translation(t.x(), t.y());
  86. }
  87. AffineTransform& AffineTransform::multiply(AffineTransform const& other)
  88. {
  89. AffineTransform result;
  90. result.m_values[0] = other.a() * a() + other.b() * c();
  91. result.m_values[1] = other.a() * b() + other.b() * d();
  92. result.m_values[2] = other.c() * a() + other.d() * c();
  93. result.m_values[3] = other.c() * b() + other.d() * d();
  94. result.m_values[4] = other.e() * a() + other.f() * c() + e();
  95. result.m_values[5] = other.e() * b() + other.f() * d() + f();
  96. *this = result;
  97. return *this;
  98. }
  99. AffineTransform& AffineTransform::rotate_radians(float radians)
  100. {
  101. float sin_angle;
  102. float cos_angle;
  103. AK::sincos(radians, sin_angle, cos_angle);
  104. AffineTransform rotation(cos_angle, sin_angle, -sin_angle, cos_angle, 0, 0);
  105. multiply(rotation);
  106. return *this;
  107. }
  108. Optional<AffineTransform> AffineTransform::inverse() const
  109. {
  110. auto determinant = a() * d() - b() * c();
  111. if (determinant == 0)
  112. return {};
  113. return AffineTransform {
  114. d() / determinant,
  115. -b() / determinant,
  116. -c() / determinant,
  117. a() / determinant,
  118. (c() * f() - d() * e()) / determinant,
  119. (b() * e() - a() * f()) / determinant,
  120. };
  121. }
  122. void AffineTransform::map(float unmapped_x, float unmapped_y, float& mapped_x, float& mapped_y) const
  123. {
  124. mapped_x = a() * unmapped_x + c() * unmapped_y + e();
  125. mapped_y = b() * unmapped_x + d() * unmapped_y + f();
  126. }
  127. template<>
  128. IntPoint AffineTransform::map(IntPoint point) const
  129. {
  130. float mapped_x;
  131. float mapped_y;
  132. map(static_cast<float>(point.x()), static_cast<float>(point.y()), mapped_x, mapped_y);
  133. return { round_to<int>(mapped_x), round_to<int>(mapped_y) };
  134. }
  135. template<>
  136. FloatPoint AffineTransform::map(FloatPoint point) const
  137. {
  138. float mapped_x;
  139. float mapped_y;
  140. map(point.x(), point.y(), mapped_x, mapped_y);
  141. return { mapped_x, mapped_y };
  142. }
  143. template<>
  144. IntSize AffineTransform::map(IntSize size) const
  145. {
  146. return {
  147. round_to<int>(static_cast<float>(size.width()) * x_scale()),
  148. round_to<int>(static_cast<float>(size.height()) * y_scale()),
  149. };
  150. }
  151. template<>
  152. FloatSize AffineTransform::map(FloatSize size) const
  153. {
  154. return { size.width() * x_scale(), size.height() * y_scale() };
  155. }
  156. template<typename T>
  157. static T smallest_of(T p1, T p2, T p3, T p4)
  158. {
  159. return min(min(p1, p2), min(p3, p4));
  160. }
  161. template<typename T>
  162. static T largest_of(T p1, T p2, T p3, T p4)
  163. {
  164. return max(max(p1, p2), max(p3, p4));
  165. }
  166. template<>
  167. FloatRect AffineTransform::map(FloatRect const& rect) const
  168. {
  169. FloatPoint p1 = map(rect.top_left());
  170. FloatPoint p2 = map(rect.top_right().translated(1, 0));
  171. FloatPoint p3 = map(rect.bottom_right().translated(1, 1));
  172. FloatPoint p4 = map(rect.bottom_left().translated(0, 1));
  173. float left = smallest_of(p1.x(), p2.x(), p3.x(), p4.x());
  174. float top = smallest_of(p1.y(), p2.y(), p3.y(), p4.y());
  175. float right = largest_of(p1.x(), p2.x(), p3.x(), p4.x());
  176. float bottom = largest_of(p1.y(), p2.y(), p3.y(), p4.y());
  177. return { left, top, right - left, bottom - top };
  178. }
  179. template<>
  180. IntRect AffineTransform::map(IntRect const& rect) const
  181. {
  182. return enclosing_int_rect(map(FloatRect(rect)));
  183. }
  184. Quad<float> AffineTransform::map_to_quad(Rect<float> const& rect) const
  185. {
  186. return {
  187. map(rect.top_left()),
  188. map(rect.top_right()),
  189. map(rect.bottom_right()),
  190. map(rect.bottom_left()),
  191. };
  192. }
  193. }