Flutter Engine
The Flutter Engine
matrix_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
7#include "flutter/impeller/geometry/matrix.h"
8
9#include "flutter/impeller/geometry/geometry_asserts.h"
10
11namespace impeller {
12namespace testing {
13
14TEST(MatrixTest, Multiply) {
15 Matrix x(0.0, 0.0, 0.0, 1.0, //
16 1.0, 0.0, 0.0, 1.0, //
17 0.0, 1.0, 0.0, 1.0, //
18 1.0, 1.0, 0.0, 1.0);
19 Matrix translate = Matrix::MakeTranslation({10, 20, 0});
20 Matrix result = translate * x;
21 EXPECT_TRUE(MatrixNear(result, Matrix(10.0, 20.0, 0.0, 1.0, //
22 11.0, 20.0, 0.0, 1.0, //
23 10.0, 21.0, 0.0, 1.0, //
24 11.0, 21.0, 0.0, 1.0)));
25}
26
27TEST(MatrixTest, HasPerspective2D) {
28 EXPECT_FALSE(Matrix().HasPerspective2D());
29
30 auto test = [](int index, bool expect) {
32 EXPECT_FALSE(matrix.HasPerspective2D());
33 matrix.m[index] = 0.5f;
34 EXPECT_EQ(matrix.HasPerspective2D(), expect) << "index: " << index;
35 };
36
37 // clang-format off
38 test( 0, false); test( 1, false); test( 2, false); test( 3, true);
39 test( 4, false); test( 5, false); test( 6, false); test( 7, true);
40 test( 8, false); test( 9, false); test(10, false); test(11, false);
41 test(12, false); test(13, false); test(14, false); test(15, true);
42 // clang-format on
43}
44
45TEST(MatrixTest, HasPerspective) {
46 EXPECT_FALSE(Matrix().HasPerspective());
47
48 auto test = [](int index, bool expect) {
50 EXPECT_FALSE(matrix.HasPerspective());
51 matrix.m[index] = 0.5f;
52 EXPECT_EQ(matrix.HasPerspective(), expect) << "index: " << index;
53 };
54
55 // clang-format off
56 test( 0, false); test( 1, false); test( 2, false); test( 3, true);
57 test( 4, false); test( 5, false); test( 6, false); test( 7, true);
58 test( 8, false); test( 9, false); test(10, false); test(11, true);
59 test(12, false); test(13, false); test(14, false); test(15, true);
60 // clang-format on
61}
62
63TEST(MatrixTest, HasTranslation) {
64 EXPECT_TRUE(Matrix::MakeTranslation({100, 100, 0}).HasTranslation());
65 EXPECT_TRUE(Matrix::MakeTranslation({0, 100, 0}).HasTranslation());
66 EXPECT_TRUE(Matrix::MakeTranslation({100, 0, 0}).HasTranslation());
67 EXPECT_FALSE(Matrix().HasTranslation());
68}
69
70TEST(MatrixTest, IsAligned2D) {
71 EXPECT_TRUE(Matrix().IsAligned2D());
72 EXPECT_TRUE(Matrix::MakeScale({1.0f, 1.0f, 2.0f}).IsAligned2D());
73
74 auto test = [](int index, bool expect) {
76 EXPECT_TRUE(matrix.IsAligned2D());
77 matrix.m[index] = 0.5f;
78 EXPECT_EQ(matrix.IsAligned2D(), expect) << "index: " << index;
79 };
80
81 // clang-format off
82 test( 0, true); test( 1, false); test( 2, true); test( 3, false);
83 test( 4, false); test( 5, true); test( 6, true); test( 7, false);
84 test( 8, true); test( 9, true); test(10, true); test(11, true);
85 test(12, true); test(13, true); test(14, true); test(15, false);
86 // clang-format on
87
88 // True for quadrant rotations from -250 to +250 full circles
89 for (int i = -1000; i < 1000; i++) {
90 Degrees d = Degrees(i * 90);
92 EXPECT_TRUE(matrix.IsAligned2D()) << "degrees: " << d.degrees;
93 }
94
95 // False for half degree rotations from -999.5 to +1000.5 degrees
96 for (int i = -1000; i < 1000; i++) {
97 Degrees d = Degrees(i + 0.5f);
99 EXPECT_FALSE(matrix.IsAligned2D()) << "degrees: " << d.degrees;
100 }
101}
102
103TEST(MatrixTest, IsAligned) {
104 EXPECT_TRUE(Matrix().IsAligned());
105 EXPECT_TRUE(Matrix::MakeScale({1.0f, 1.0f, 2.0f}).IsAligned());
106
107 // Begin Legacy tests transferred over from geometry_unittests.cc
108 {
109 auto m = Matrix::MakeTranslation({1, 2, 3});
110 bool result = m.IsAligned();
111 ASSERT_TRUE(result);
112 }
113
114 {
115 auto m = Matrix::MakeRotationZ(Degrees{123});
116 bool result = m.IsAligned();
117 ASSERT_FALSE(result);
118 }
119 // End Legacy tests transferred over from geometry_unittests.cc
120
121 auto test = [](int index, bool expect) {
123 EXPECT_TRUE(matrix.IsAligned());
124 matrix.m[index] = 0.5f;
125 EXPECT_EQ(matrix.IsAligned(), expect) << "index: " << index;
126 };
127
128 // clang-format off
129 test( 0, true); test( 1, false); test( 2, false); test( 3, false);
130 test( 4, false); test( 5, true); test( 6, false); test( 7, false);
131 test( 8, false); test( 9, false); test(10, true); test(11, false);
132 test(12, true); test(13, true); test(14, true); test(15, false);
133 // clang-format on
134
135 // True for quadrant rotations from -250 to +250 full circles
136 for (int i = -1000; i < 1000; i++) {
137 Degrees d = Degrees(i * 90);
139 EXPECT_TRUE(matrix.IsAligned()) << "degrees: " << d.degrees;
140 }
141
142 // False for half degree rotations from -999.5 to +1000.5 degrees
143 for (int i = -1000; i < 1000; i++) {
144 Degrees d = Degrees(i + 0.5f);
146 EXPECT_FALSE(matrix.IsAligned()) << "degrees: " << d.degrees;
147 }
148}
149
150TEST(MatrixTest, TransformHomogenous) {
152 // clang-format off
153 2.0f, 3.0f, 5.0f, 7.0f,
154 11.0f, 13.0f, 17.0f, 19.0f,
155 23.0f, 29.0f, 31.0f, 37.0f,
156 41.0f, 43.0f, 47.0f, 53.0f
157 // clang-format on
158 );
159 EXPECT_EQ(matrix.TransformHomogenous({1.0f, -1.0f}),
160 Vector3(32.0f, 33.0f, 41.0f));
161}
162
163} // namespace testing
164} // namespace impeller
#define test(name)
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
GAsyncResult * result
inline ::testing::AssertionResult MatrixNear(impeller::Matrix a, impeller::Matrix b)
double x
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
TEST(AiksCanvasTest, EmptyCullRect)
SK_API sk_sp< PrecompileColorFilter > Matrix()
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
static constexpr Matrix MakeColumn(Scalar m0, Scalar m1, Scalar m2, Scalar m3, Scalar m4, Scalar m5, Scalar m6, Scalar m7, Scalar m8, Scalar m9, Scalar m10, Scalar m11, Scalar m12, Scalar m13, Scalar m14, Scalar m15)
Definition: matrix.h:69
static Matrix MakeRotationZ(Radians r)
Definition: matrix.h:213
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678