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 constexpr RSTransform Make(Point origin,
39  Scalar scale,
40  Radians radians) {
41  auto scaled_cos_sin = Matrix::CosSin(radians) * scale;
42  return {scaled_cos_sin.x, scaled_cos_sin.y, origin.x, origin.y};
43  }
44 
49 
50  /// Returns true iff the resulting transformed quad will be aligned with
51  /// the axes, even if rotated by a quadrant rotation.
52  bool IsAxisAligned() const;
53 
54  Matrix GetMatrix() const;
55 
56  /// Returns the 4 corner points of the transformed quad for a sub-image
57  /// of the indicated size in the same order as Rect::GetPoints.
58  ///
59  /// The order is UpperLeft, UpperRight, LowerLeft, LowerRight
60  void GetQuad(Scalar width, Scalar height, Quad& quad) const;
61  Quad GetQuad(Scalar width, Scalar height) const;
62  Quad GetQuad(Size size) const;
63 
64  /// Returns the bounds of the 4 corner points of the transformed quad
65  /// for a sub-image of the indicated size.
66  std::optional<Rect> GetBounds(Scalar width, Scalar height) const;
67  std::optional<Rect> GetBounds(Size size) const;
68 };
69 
70 } // namespace impeller
71 
72 namespace std {
73 
74 inline std::ostream& operator<<(std::ostream& out,
75  const impeller::RSTransform& rst) {
76  // clang-format off
77  out << "("
78  << "scos: " << rst.scaled_cos << ", "
79  << "ssin: " << rst.scaled_sin << ", "
80  << "origin: (" << rst.translate_x << ", "
81  << rst.translate_y << ")"
82  << ")";
83  // clang-format on
84  return out;
85 }
86 
87 } // namespace std
88 
89 #endif // FLUTTER_IMPELLER_GEOMETRY_RSTRANSFORM_H_
float Scalar
Definition: scalar.h:18
std::array< Point, 4 > Quad
Definition: point.h:332
Definition: comparable.h:95
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
Definition: color.h:927
const Scalar scale
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
static constexpr Vector2 CosSin(Radians radians)
Definition: matrix.h:618
std::optional< Rect > GetBounds(Scalar width, Scalar height) const
Definition: rstransform.cc:49
Matrix GetMatrix() const
Definition: rstransform.cc:15
static constexpr RSTransform Make(Point origin, Scalar scale, Radians radians)
Definition: rstransform.h:38
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