AffineTransform.cpp 5.9 KB

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