| 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 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
45 Scalar scaled_cos;
46 Scalar scaled_sin;
47 Scalar translate_x;
48 Scalar translate_y;
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_
impeller::RSTransform::Make
static constexpr RSTransform Make(Point origin, Scalar scale, Radians radians)
Definition: rstransform.h:38
impeller::RSTransform::RSTransform
constexpr RSTransform(Scalar scaled_cos, Scalar scaled_sin, Scalar translate_x, Scalar translate_y)
Definition: rstransform.h:27
point.h
impeller::TPoint::y
Type y
Definition: point.h:31
impeller::Scalar
float Scalar
Definition: scalar.h:18
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
Definition: color.h:926
impeller::RSTransform::GetQuad
void GetQuad(Scalar width, Scalar height, Quad &quad) const
Definition: rstransform.cc:24
impeller::TSize< Scalar >
impeller::Quad
std::array< Point, 4 > Quad
Definition: point.h:332
impeller::RSTransform::GetMatrix
Matrix GetMatrix() const
Definition: rstransform.cc:15
impeller::Matrix::CosSin
static constexpr Vector2 CosSin(Radians radians)
Definition: matrix.h:605
impeller::RSTransform::IsAxisAligned
bool IsAxisAligned() const
Definition: rstransform.cc:11
impeller::RSTransform::scaled_cos
Scalar scaled_cos
Definition: rstransform.h:45
impeller::RSTransform
Definition: rstransform.h:20
impeller::Radians
Definition: scalar.h:43
impeller::RSTransform::translate_x
Scalar translate_x
Definition: rstransform.h:47
impeller::RSTransform::scaled_sin
Scalar scaled_sin
Definition: rstransform.h:46
impeller::TPoint::x
Type x
Definition: point.h:30
scalar.h
impeller::RSTransform::RSTransform
constexpr RSTransform()
Definition: rstransform.h:21
std
Definition: comparable.h:95
rect.h
impeller::TPoint< Scalar >
scale
const Scalar scale
Definition: stroke_path_geometry.cc:316
impeller
Definition: allocation.cc:12
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::RSTransform::GetBounds
std::optional< Rect > GetBounds(Scalar width, Scalar height) const
Definition: rstransform.cc:49
size.h
impeller::RSTransform::translate_y
Scalar translate_y
Definition: rstransform.h:48