forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_decomposition_unittests.cc
95 lines (68 loc) · 2.89 KB
/
matrix_decomposition_unittests.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "lib/fxl/build_config.h"
#if defined(OS_WIN)
#define _USE_MATH_DEFINES
#endif
#include <cmath>
#include "flutter/flow/matrix_decomposition.h"
#include "third_party/gtest/include/gtest/gtest.h"
TEST(MatrixDecomposition, Rotation) {
SkMatrix44 matrix = SkMatrix44::I();
const auto angle = M_PI_4;
matrix.setRotateAbout(0.0, 0.0, 1.0, angle);
flow::MatrixDecomposition decomposition(matrix);
ASSERT_TRUE(decomposition.IsValid());
const auto sine = sin(angle * 0.5);
ASSERT_FLOAT_EQ(0, decomposition.rotation().fData[0]);
ASSERT_FLOAT_EQ(0, decomposition.rotation().fData[1]);
ASSERT_FLOAT_EQ(sine, decomposition.rotation().fData[2]);
ASSERT_FLOAT_EQ(cos(angle * 0.5), decomposition.rotation().fData[3]);
}
TEST(MatrixDecomposition, Scale) {
SkMatrix44 matrix = SkMatrix44::I();
const auto scale = 5.0;
matrix.setScale(scale + 0, scale + 1, scale + 2);
flow::MatrixDecomposition decomposition(matrix);
ASSERT_TRUE(decomposition.IsValid());
ASSERT_FLOAT_EQ(scale + 0, decomposition.scale().fX);
ASSERT_FLOAT_EQ(scale + 1, decomposition.scale().fY);
ASSERT_FLOAT_EQ(scale + 2, decomposition.scale().fZ);
}
TEST(MatrixDecomposition, Translate) {
SkMatrix44 matrix = SkMatrix44::I();
const auto translate = 125.0;
matrix.setTranslate(translate + 0, translate + 1, translate + 2);
flow::MatrixDecomposition decomposition(matrix);
ASSERT_TRUE(decomposition.IsValid());
ASSERT_FLOAT_EQ(translate + 0, decomposition.translation().fX);
ASSERT_FLOAT_EQ(translate + 1, decomposition.translation().fY);
ASSERT_FLOAT_EQ(translate + 2, decomposition.translation().fZ);
}
TEST(MatrixDecomposition, Combination) {
SkMatrix44 matrix = SkMatrix44::I();
const auto rotation = M_PI_4;
const auto scale = 5;
const auto translate = 125.0;
SkMatrix44 m1 = SkMatrix44::I();
m1.setRotateAbout(0, 0, 1, rotation);
SkMatrix44 m2 = SkMatrix44::I();
m2.setScale(scale);
SkMatrix44 m3 = SkMatrix44::I();
m3.setTranslate(translate, translate, translate);
SkMatrix44 combined = m3 * m2 * m1;
flow::MatrixDecomposition decomposition(combined);
ASSERT_TRUE(decomposition.IsValid());
ASSERT_FLOAT_EQ(translate, decomposition.translation().fX);
ASSERT_FLOAT_EQ(translate, decomposition.translation().fY);
ASSERT_FLOAT_EQ(translate, decomposition.translation().fZ);
ASSERT_FLOAT_EQ(scale, decomposition.scale().fX);
ASSERT_FLOAT_EQ(scale, decomposition.scale().fY);
ASSERT_FLOAT_EQ(scale, decomposition.scale().fZ);
const auto sine = sin(rotation * 0.5);
ASSERT_FLOAT_EQ(0, decomposition.rotation().fData[0]);
ASSERT_FLOAT_EQ(0, decomposition.rotation().fData[1]);
ASSERT_FLOAT_EQ(sine, decomposition.rotation().fData[2]);
ASSERT_FLOAT_EQ(cos(rotation * 0.5), decomposition.rotation().fData[3]);
}