AffineTransform.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2020, 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/Rect.h>
  9. namespace Gfx {
  10. bool AffineTransform::is_identity() const
  11. {
  12. 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;
  13. }
  14. static float hypotenuse(float x, float y)
  15. {
  16. // FIXME: This won't handle overflow :(
  17. return sqrtf(x * x + y * y);
  18. }
  19. float AffineTransform::x_scale() const
  20. {
  21. return hypotenuse(m_values[0], m_values[1]);
  22. }
  23. float AffineTransform::y_scale() const
  24. {
  25. return hypotenuse(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(const 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(const 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(const 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(const FloatPoint& t)
  84. {
  85. return set_translation(t.x(), t.y());
  86. }
  87. AffineTransform& AffineTransform::multiply(const AffineTransform& 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 = sinf(radians);
  102. float cos_angle = cosf(radians);
  103. AffineTransform rotation(cos_angle, sin_angle, -sin_angle, cos_angle, 0, 0);
  104. multiply(rotation);
  105. return *this;
  106. }
  107. void AffineTransform::map(float unmapped_x, float unmapped_y, float& mapped_x, float& mapped_y) const
  108. {
  109. mapped_x = a() * unmapped_x + b() * unmapped_y + m_values[4];
  110. mapped_y = c() * unmapped_x + d() * unmapped_y + m_values[5];
  111. }
  112. template<>
  113. IntPoint AffineTransform::map(const IntPoint& point) const
  114. {
  115. float mapped_x;
  116. float mapped_y;
  117. map(static_cast<float>(point.x()), static_cast<float>(point.y()), mapped_x, mapped_y);
  118. return { roundf(mapped_x), roundf(mapped_y) };
  119. }
  120. template<>
  121. FloatPoint AffineTransform::map(const FloatPoint& point) const
  122. {
  123. float mapped_x;
  124. float mapped_y;
  125. map(point.x(), point.y(), mapped_x, mapped_y);
  126. return { mapped_x, mapped_y };
  127. }
  128. template<>
  129. IntSize AffineTransform::map(const IntSize& size) const
  130. {
  131. return {
  132. roundf(static_cast<float>(size.width()) * x_scale()),
  133. roundf(static_cast<float>(size.height()) * y_scale()),
  134. };
  135. }
  136. template<>
  137. FloatSize AffineTransform::map(const FloatSize& size) const
  138. {
  139. return { size.width() * x_scale(), size.height() * y_scale() };
  140. }
  141. template<typename T>
  142. static T smallest_of(T p1, T p2, T p3, T p4)
  143. {
  144. return min(min(p1, p2), min(p3, p4));
  145. }
  146. template<typename T>
  147. static T largest_of(T p1, T p2, T p3, T p4)
  148. {
  149. return max(max(p1, p2), max(p3, p4));
  150. }
  151. template<>
  152. FloatRect AffineTransform::map(const FloatRect& rect) const
  153. {
  154. FloatPoint p1 = map(rect.top_left());
  155. FloatPoint p2 = map(rect.top_right().translated(1, 0));
  156. FloatPoint p3 = map(rect.bottom_right().translated(1, 1));
  157. FloatPoint p4 = map(rect.bottom_left().translated(0, 1));
  158. float left = smallest_of(p1.x(), p2.x(), p3.x(), p4.x());
  159. float top = smallest_of(p1.y(), p2.y(), p3.y(), p4.y());
  160. float right = largest_of(p1.x(), p2.x(), p3.x(), p4.x());
  161. float bottom = largest_of(p1.y(), p2.y(), p3.y(), p4.y());
  162. return { left, top, right - left, bottom - top };
  163. }
  164. template<>
  165. IntRect AffineTransform::map(const IntRect& rect) const
  166. {
  167. return enclosing_int_rect(map(FloatRect(rect)));
  168. }
  169. }