AffineTransform.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. float AffineTransform::determinant() const
  116. {
  117. return a() * d() - b() * c();
  118. }
  119. Optional<AffineTransform> AffineTransform::inverse() const
  120. {
  121. auto det = determinant();
  122. if (det == 0)
  123. return {};
  124. return AffineTransform {
  125. d() / det,
  126. -b() / det,
  127. -c() / det,
  128. a() / det,
  129. (c() * f() - d() * e()) / det,
  130. (b() * e() - a() * f()) / det,
  131. };
  132. }
  133. void AffineTransform::map(float unmapped_x, float unmapped_y, float& mapped_x, float& mapped_y) const
  134. {
  135. mapped_x = a() * unmapped_x + c() * unmapped_y + e();
  136. mapped_y = b() * unmapped_x + d() * unmapped_y + f();
  137. }
  138. template<>
  139. IntPoint AffineTransform::map(IntPoint point) const
  140. {
  141. float mapped_x;
  142. float mapped_y;
  143. map(static_cast<float>(point.x()), static_cast<float>(point.y()), mapped_x, mapped_y);
  144. return { round_to<int>(mapped_x), round_to<int>(mapped_y) };
  145. }
  146. template<>
  147. FloatPoint AffineTransform::map(FloatPoint point) const
  148. {
  149. float mapped_x;
  150. float mapped_y;
  151. map(point.x(), point.y(), mapped_x, mapped_y);
  152. return { mapped_x, mapped_y };
  153. }
  154. template<>
  155. IntSize AffineTransform::map(IntSize size) const
  156. {
  157. return {
  158. round_to<int>(static_cast<float>(size.width()) * x_scale()),
  159. round_to<int>(static_cast<float>(size.height()) * y_scale()),
  160. };
  161. }
  162. template<>
  163. FloatSize AffineTransform::map(FloatSize size) const
  164. {
  165. return { size.width() * x_scale(), size.height() * y_scale() };
  166. }
  167. template<typename T>
  168. static T smallest_of(T p1, T p2, T p3, T p4)
  169. {
  170. return min(min(p1, p2), min(p3, p4));
  171. }
  172. template<typename T>
  173. static T largest_of(T p1, T p2, T p3, T p4)
  174. {
  175. return max(max(p1, p2), max(p3, p4));
  176. }
  177. template<>
  178. FloatRect AffineTransform::map(FloatRect const& rect) const
  179. {
  180. FloatPoint p1 = map(rect.top_left());
  181. FloatPoint p2 = map(rect.top_right());
  182. FloatPoint p3 = map(rect.bottom_right());
  183. FloatPoint p4 = map(rect.bottom_left());
  184. float left = smallest_of(p1.x(), p2.x(), p3.x(), p4.x());
  185. float top = smallest_of(p1.y(), p2.y(), p3.y(), p4.y());
  186. float right = largest_of(p1.x(), p2.x(), p3.x(), p4.x());
  187. float bottom = largest_of(p1.y(), p2.y(), p3.y(), p4.y());
  188. return { left, top, right - left, bottom - top };
  189. }
  190. template<>
  191. IntRect AffineTransform::map(IntRect const& rect) const
  192. {
  193. return enclosing_int_rect(map(FloatRect(rect)));
  194. }
  195. Quad<float> AffineTransform::map_to_quad(Rect<float> const& rect) const
  196. {
  197. return {
  198. map(rect.top_left()),
  199. map(rect.top_right()),
  200. map(rect.bottom_right()),
  201. map(rect.bottom_left()),
  202. };
  203. }
  204. float AffineTransform::rotation() const
  205. {
  206. auto rotation = AK::atan2(b(), a());
  207. while (rotation < -AK::Pi<float>)
  208. rotation += 2.0f * AK::Pi<float>;
  209. while (rotation > AK::Pi<float>)
  210. rotation -= 2.0f * AK::Pi<float>;
  211. return rotation;
  212. }
  213. }