Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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) {
31 Matrix matrix;
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) {
49 Matrix matrix;
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, IsAligned2D) {
64 EXPECT_TRUE(Matrix().IsAligned2D());
65 EXPECT_TRUE(Matrix::MakeScale({1.0f, 1.0f, 2.0f}).IsAligned2D());
66
67 auto test = [](int index, bool expect) {
68 Matrix matrix;
69 EXPECT_TRUE(matrix.IsAligned2D());
70 matrix.m[index] = 0.5f;
71 EXPECT_EQ(matrix.IsAligned2D(), expect) << "index: " << index;
72 };
73
74 // clang-format off
75 test( 0, true); test( 1, false); test( 2, true); test( 3, false);
76 test( 4, false); test( 5, true); test( 6, true); test( 7, false);
77 test( 8, true); test( 9, true); test(10, true); test(11, true);
78 test(12, true); test(13, true); test(14, true); test(15, false);
79 // clang-format on
80
81 // True for quadrant rotations from -250 to +250 full circles
82 for (int i = -1000; i < 1000; i++) {
83 Degrees d = Degrees(i * 90);
85 EXPECT_TRUE(matrix.IsAligned2D()) << "degrees: " << d.degrees;
86 }
87
88 // False for half degree rotations from -999.5 to +1000.5 degrees
89 for (int i = -1000; i < 1000; i++) {
90 Degrees d = Degrees(i + 0.5f);
92 EXPECT_FALSE(matrix.IsAligned2D()) << "degrees: " << d.degrees;
93 }
94}
95
96TEST(MatrixTest, IsAligned) {
97 EXPECT_TRUE(Matrix().IsAligned());
98 EXPECT_TRUE(Matrix::MakeScale({1.0f, 1.0f, 2.0f}).IsAligned());
99
100 // Begin Legacy tests transferred over from geometry_unittests.cc
101 {
102 auto m = Matrix::MakeTranslation({1, 2, 3});
103 bool result = m.IsAligned();
104 ASSERT_TRUE(result);
105 }
106
107 {
108 auto m = Matrix::MakeRotationZ(Degrees{123});
109 bool result = m.IsAligned();
110 ASSERT_FALSE(result);
111 }
112 // End Legacy tests transferred over from geometry_unittests.cc
113
114 auto test = [](int index, bool expect) {
115 Matrix matrix;
116 EXPECT_TRUE(matrix.IsAligned());
117 matrix.m[index] = 0.5f;
118 EXPECT_EQ(matrix.IsAligned(), expect) << "index: " << index;
119 };
120
121 // clang-format off
122 test( 0, true); test( 1, false); test( 2, false); test( 3, false);
123 test( 4, false); test( 5, true); test( 6, false); test( 7, false);
124 test( 8, false); test( 9, false); test(10, true); test(11, false);
125 test(12, true); test(13, true); test(14, true); test(15, false);
126 // clang-format on
127
128 // True for quadrant rotations from -250 to +250 full circles
129 for (int i = -1000; i < 1000; i++) {
130 Degrees d = Degrees(i * 90);
132 EXPECT_TRUE(matrix.IsAligned()) << "degrees: " << d.degrees;
133 }
134
135 // False for half degree rotations from -999.5 to +1000.5 degrees
136 for (int i = -1000; i < 1000; i++) {
137 Degrees d = Degrees(i + 0.5f);
139 EXPECT_FALSE(matrix.IsAligned()) << "degrees: " << d.degrees;
140 }
141}
142
143TEST(MatrixTest, TransformHomogenous) {
144 Matrix matrix = Matrix::MakeColumn(
145 // clang-format off
146 2.0f, 3.0f, 5.0f, 7.0f,
147 11.0f, 13.0f, 17.0f, 19.0f,
148 23.0f, 29.0f, 31.0f, 37.0f,
149 41.0f, 43.0f, 47.0f, 53.0f
150 // clang-format on
151 );
152 EXPECT_EQ(matrix.TransformHomogenous({1.0f, -1.0f}),
153 Vector3(32.0f, 33.0f, 41.0f));
154}
155
156} // namespace testing
157} // namespace impeller
#define TEST(S, s, D, expected)
#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
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:685