| 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
8 #include "flutter/impeller/geometry/point.h"
9 #include "flutter/impeller/geometry/rect.h"
10 #include "flutter/impeller/geometry/scalar.h"
11 #include "flutter/impeller/geometry/size.h"
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
27 constexpr RSTransform(Scalar scaled_cos,
28 Scalar scaled_sin,
29 Scalar translate_x,
30 Scalar translate_y)
31 : scaled_cos(scaled_cos),
32 scaled_sin(scaled_sin),
33 translate_x(translate_x),
34 translate_y(translate_y) {}
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
43 Scalar scaled_cos;
44 Scalar scaled_sin;
45 Scalar translate_x;
46 Scalar translate_y;
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_
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::Quad
std::array< Point, 4 > Quad
Definition: point.h:430
std
Definition: comparable.h:95
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Arc &a)
Definition: arc.h:141
point.h
rect.h
scalar.h
size.h
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Matrix::CosSin
static Vector2 CosSin(Radians radians)
Definition: matrix.h:685
impeller::RSTransform
Definition: rstransform.h:20
impeller::RSTransform::Make
static RSTransform Make(Point origin, Scalar scale, Radians radians)
Definition: rstransform.h:38
impeller::RSTransform::translate_y
Scalar translate_y
Definition: rstransform.h:46
impeller::RSTransform::GetBounds
std::optional< Rect > GetBounds(Scalar width, Scalar height) const
Definition: rstransform.cc:49
impeller::RSTransform::GetMatrix
Matrix GetMatrix() const
Definition: rstransform.cc:15
impeller::RSTransform::translate_x
Scalar translate_x
Definition: rstransform.h:45
impeller::RSTransform::scaled_sin
Scalar scaled_sin
Definition: rstransform.h:44
impeller::RSTransform::RSTransform
constexpr RSTransform()
Definition: rstransform.h:21
impeller::RSTransform::RSTransform
constexpr RSTransform(Scalar scaled_cos, Scalar scaled_sin, Scalar translate_x, Scalar translate_y)
Definition: rstransform.h:27
impeller::RSTransform::GetQuad
void GetQuad(Scalar width, Scalar height, Quad &quad) const
Definition: rstransform.cc:24
impeller::RSTransform::scaled_cos
Scalar scaled_cos
Definition: rstransform.h:43
impeller::RSTransform::IsAxisAligned
bool IsAxisAligned() const
Definition: rstransform.cc:11
impeller::Radians
Definition: scalar.h:44
impeller::TPoint< Scalar >
impeller::TPoint::y
Type y
Definition: point.h:32
impeller::TPoint::x
Type x
Definition: point.h:31
impeller::TSize< Scalar >