Flutter Impeller
rstransform_unittests.cc
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 #include "gtest/gtest.h"
6 
8 
10 
11 namespace impeller {
12 namespace testing {
13 
14 TEST(RSTransformTest, Construction) {
15  RSTransform transform = RSTransform::Make({10.0f, 12.0f}, 2.0f, Degrees(90));
16 
17  EXPECT_EQ(transform.scaled_cos, 0.0f);
18  EXPECT_EQ(transform.scaled_sin, 2.0f);
19  EXPECT_EQ(transform.translate_x, 10.0f);
20  EXPECT_EQ(transform.translate_y, 12.0f);
21 
22  EXPECT_EQ(transform.GetBounds(20.0f, 30.0f),
23  // relative corners are at
24  // 0, 0
25  // 0, 40
26  // -60, 0
27  // -60, 40
28  // then add 10, 12 to all values
29  Rect::MakeLTRB(10 + -2 * 30, 12 + 0, 10 + 0, 12 + 40));
30 }
31 
32 TEST(RSTransformTest, CompareToMatrix) {
33  for (int tx = 0; tx <= 100; tx += 10) {
34  for (int ty = 0; ty <= 100; ty += 10) {
35  Point origin(tx, ty);
36  for (int scale = 1; scale <= 20; scale += 5) {
37  // Overshoot a full circle by 30 degrees
38  for (int degrees = 0; degrees <= 390; degrees += 45) {
39  auto matrix = Matrix::MakeTranslation(origin) *
40  Matrix::MakeRotationZ(Degrees(degrees)) *
41  Matrix::MakeScale(Vector2(scale, scale));
42  auto rst = RSTransform::Make(origin, scale, Degrees(degrees));
43  EXPECT_MATRIX_NEAR(rst.GetMatrix(), matrix);
44  for (int w = 10; w <= 100; w += 10) {
45  for (int h = 10; h <= 100; h += 10) {
46  Quad q = rst.GetQuad(w, h);
47  auto points = Rect::MakeWH(w, h).GetTransformedPoints(matrix);
48  for (int i = 0; i < 4; i++) {
49  EXPECT_NEAR(q[i].x, points[i].x, kEhCloseEnough);
50  EXPECT_NEAR(q[i].y, points[i].y, kEhCloseEnough);
51  }
52  }
53  }
54  }
55  }
56  }
57  }
58 }
59 
60 } // namespace testing
61 } // namespace impeller
int32_t x
#define EXPECT_MATRIX_NEAR(a, b)
TEST(AllocationSizeTest, CanCreateTypedAllocations)
Point Vector2
Definition: point.h:331
constexpr float kEhCloseEnough
Definition: constants.h:57
std::array< Point, 4 > Quad
Definition: point.h:332
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
static Matrix MakeRotationZ(Radians r)
Definition: matrix.h:223
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
static RSTransform Make(Point origin, Scalar scale, Radians radians)
Definition: rstransform.h:38
constexpr static TRect MakeWH(Type width, Type height)
Definition: rect.h:140
constexpr std::array< TPoint< T >, 4 > GetTransformedPoints(const Matrix &transform) const
Definition: rect.h:430
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129
std::vector< Point > points