Flutter Impeller
rstransform.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_IMPELLER_GEOMETRY_RSTRANSFORM_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_RSTRANSFORM_H_
7 
12 
13 namespace impeller {
14 
15 /// A utility struct that stores a simple transform composed of a translation,
16 /// a rotation, and a uniform scale.
17 ///
18 /// This transform is used by drawAtlas to transform the sprites.
19 /// This structure mirrors the Flutter RSTransform class.
20 struct RSTransform {
21  constexpr RSTransform()
22  : scaled_cos(1.0f),
23  scaled_sin(0.0f),
24  translate_x(0.0f),
25  translate_y(0.0f) {}
26 
35 
36  /// Constructs an RSTransform from the indicated origin, uniform scale,
37  /// and radians rotation.
38  static inline RSTransform Make(Point origin, Scalar scale, Radians radians) {
39  auto scaled_cos_sin = Matrix::CosSin(radians) * scale;
40  return {scaled_cos_sin.x, scaled_cos_sin.y, origin.x, origin.y};
41  }
42 
47 
48  /// Returns true iff the resulting transformed quad will be aligned with
49  /// the axes, even if rotated by a quadrant rotation.
50  bool IsAxisAligned() const;
51 
52  Matrix GetMatrix() const;
53 
54  /// Returns the 4 corner points of the transformed quad for a sub-image
55  /// of the indicated size in the same order as Rect::GetPoints.
56  ///
57  /// The order is UpperLeft, UpperRight, LowerLeft, LowerRight
58  void GetQuad(Scalar width, Scalar height, Quad& quad) const;
59  Quad GetQuad(Scalar width, Scalar height) const;
60  Quad GetQuad(Size size) const;
61 
62  /// Returns the bounds of the 4 corner points of the transformed quad
63  /// for a sub-image of the indicated size.
64  std::optional<Rect> GetBounds(Scalar width, Scalar height) const;
65  std::optional<Rect> GetBounds(Size size) const;
66 };
67 
68 } // namespace impeller
69 
70 namespace std {
71 
72 inline std::ostream& operator<<(std::ostream& out,
73  const impeller::RSTransform& rst) {
74  // clang-format off
75  out << "("
76  << "scos: " << rst.scaled_cos << ", "
77  << "ssin: " << rst.scaled_sin << ", "
78  << "origin: (" << rst.translate_x << ", "
79  << rst.translate_y << ")"
80  << ")";
81  // clang-format on
82  return out;
83 }
84 
85 } // namespace std
86 
87 #endif // FLUTTER_IMPELLER_GEOMETRY_RSTRANSFORM_H_
float Scalar
Definition: scalar.h:19
std::array< Point, 4 > Quad
Definition: point.h:332
Definition: comparable.h:95
std::ostream & operator<<(std::ostream &out, const impeller::Arc &a)
Definition: arc.h:141
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
static Vector2 CosSin(Radians radians)
Definition: matrix.h:618
static RSTransform Make(Point origin, Scalar scale, Radians radians)
Definition: rstransform.h:38
std::optional< Rect > GetBounds(Scalar width, Scalar height) const
Definition: rstransform.cc:49
Matrix GetMatrix() const
Definition: rstransform.cc:15
constexpr RSTransform()
Definition: rstransform.h:21
constexpr RSTransform(Scalar scaled_cos, Scalar scaled_sin, Scalar translate_x, Scalar translate_y)
Definition: rstransform.h:27
void GetQuad(Scalar width, Scalar height, Quad &quad) const
Definition: rstransform.cc:24
bool IsAxisAligned() const
Definition: rstransform.cc:11