AffineTransform.cpp 5.9 KB

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