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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
// Copyright (c) 2012 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 "ui/gfx/interpolated_transform.h"
#include "base/basictypes.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/rect.h"
namespace {
void CheckApproximatelyEqual(const gfx::Transform& lhs,
const gfx::Transform& rhs) {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
EXPECT_FLOAT_EQ(lhs.matrix().get(i, j), rhs.matrix().get(i, j));
}
}
}
float NormalizeAngle(float angle) {
while (angle < 0.0f) {
angle += 360.0f;
}
while (angle > 360.0f) {
angle -= 360.0f;
}
return angle;
}
} // namespace
TEST(InterpolatedTransformTest, InterpolatedRotation) {
ui::InterpolatedRotation interpolated_rotation(0, 100);
ui::InterpolatedRotation interpolated_rotation_diff_start_end(
0, 100, 100, 200);
for (int i = 0; i <= 100; ++i) {
gfx::Transform rotation;
rotation.SetRotate(i);
gfx::Transform interpolated = interpolated_rotation.Interpolate(i / 100.0f);
CheckApproximatelyEqual(rotation, interpolated);
interpolated = interpolated_rotation_diff_start_end.Interpolate(i + 100);
CheckApproximatelyEqual(rotation, interpolated);
}
}
TEST(InterpolatedTransformTest, InterpolatedScale) {
ui::InterpolatedScale interpolated_scale(gfx::Point3f(0, 0, 0),
gfx::Point3f(100, 100, 100));
ui::InterpolatedScale interpolated_scale_diff_start_end(
gfx::Point3f(0, 0, 0), gfx::Point3f(100, 100, 100), 100, 200);
for (int i = 0; i <= 100; ++i) {
gfx::Transform scale;
scale.SetScale(i, i);
gfx::Transform interpolated = interpolated_scale.Interpolate(i / 100.0f);
CheckApproximatelyEqual(scale, interpolated);
interpolated = interpolated_scale_diff_start_end.Interpolate(i + 100);
CheckApproximatelyEqual(scale, interpolated);
}
}
TEST(InterpolatedTransformTest, InterpolatedTranslate) {
ui::InterpolatedTranslation interpolated_xform(gfx::Point(0, 0),
gfx::Point(100, 100));
ui::InterpolatedTranslation interpolated_xform_diff_start_end(
gfx::Point(0, 0), gfx::Point(100, 100), 100, 200);
for (int i = 0; i <= 100; ++i) {
gfx::Transform xform;
xform.SetTranslate(i, i);
gfx::Transform interpolated = interpolated_xform.Interpolate(i / 100.0f);
CheckApproximatelyEqual(xform, interpolated);
interpolated = interpolated_xform_diff_start_end.Interpolate(i + 100);
CheckApproximatelyEqual(xform, interpolated);
}
}
TEST(InterpolatedTransformTest, InterpolatedRotationAboutPivot) {
gfx::Point pivot(100, 100);
gfx::Point above_pivot(100, 200);
ui::InterpolatedRotation rot(0, 90);
ui::InterpolatedTransformAboutPivot interpolated_xform(
pivot,
new ui::InterpolatedRotation(0, 90));
gfx::Transform result = interpolated_xform.Interpolate(0.0f);
CheckApproximatelyEqual(gfx::Transform(), result);
result = interpolated_xform.Interpolate(1.0f);
gfx::Point expected_result = pivot;
result.TransformPoint(pivot);
EXPECT_EQ(expected_result, pivot);
expected_result = gfx::Point(0, 100);
result.TransformPoint(above_pivot);
EXPECT_EQ(expected_result, above_pivot);
}
TEST(InterpolatedTransformTest, InterpolatedScaleAboutPivot) {
gfx::Point pivot(100, 100);
gfx::Point above_pivot(100, 200);
ui::InterpolatedTransformAboutPivot interpolated_xform(
pivot,
new ui::InterpolatedScale(gfx::Point3f(1, 1, 1), gfx::Point3f(2, 2, 2)));
gfx::Transform result = interpolated_xform.Interpolate(0.0f);
CheckApproximatelyEqual(gfx::Transform(), result);
result = interpolated_xform.Interpolate(1.0f);
gfx::Point expected_result = pivot;
result.TransformPoint(pivot);
EXPECT_EQ(expected_result, pivot);
expected_result = gfx::Point(100, 300);
result.TransformPoint(above_pivot);
EXPECT_EQ(expected_result, above_pivot);
}
TEST(InterpolatedTransformTest, FactorTRS) {
for (int degrees = 0; degrees < 360; ++degrees) {
// build a transformation matrix.
gfx::Transform transform;
transform.SetScale(degrees + 1, 2 * degrees + 1);
transform.ConcatRotate(degrees);
transform.ConcatTranslate(degrees * 2, -degrees * 3);
// factor the matrix
gfx::Point translation;
float rotation;
gfx::Point3f scale;
bool success = ui::InterpolatedTransform::FactorTRS(transform,
&translation,
&rotation,
&scale);
EXPECT_TRUE(success);
EXPECT_FLOAT_EQ(translation.x(), degrees * 2);
EXPECT_FLOAT_EQ(translation.y(), -degrees * 3);
EXPECT_FLOAT_EQ(NormalizeAngle(rotation), degrees);
EXPECT_FLOAT_EQ(scale.x(), degrees + 1);
EXPECT_FLOAT_EQ(scale.y(), 2 * degrees + 1);
}
}
ui::InterpolatedTransform* GetScreenRotation(int degrees, bool reversed) {
gfx::Point old_pivot;
gfx::Point new_pivot;
int width = 1920;
int height = 180;
switch (degrees) {
case 90:
new_pivot = gfx::Point(width, 0);
break;
case -90:
new_pivot = gfx::Point(0, height);
break;
case 180:
case 360:
new_pivot = old_pivot = gfx::Point(width / 2, height / 2);
break;
}
scoped_ptr<ui::InterpolatedTransform> rotation(
new ui::InterpolatedTransformAboutPivot(
old_pivot,
new ui::InterpolatedRotation(reversed ? degrees : 0,
reversed ? 0 : degrees)));
scoped_ptr<ui::InterpolatedTransform> translation(
new ui::InterpolatedTranslation(
gfx::Point(0, 0),
gfx::Point(new_pivot.x() - old_pivot.x(),
new_pivot.y() - old_pivot.y())));
float scale_factor = 0.9f;
scoped_ptr<ui::InterpolatedTransform> scale_down(
new ui::InterpolatedScale(1.0f, scale_factor, 0.0f, 0.5f));
scoped_ptr<ui::InterpolatedTransform> scale_up(
new ui::InterpolatedScale(1.0f, 1.0f / scale_factor, 0.5f, 1.0f));
scoped_ptr<ui::InterpolatedTransform> to_return(
new ui::InterpolatedConstantTransform(gfx::Transform()));
scale_up->SetChild(scale_down.release());
translation->SetChild(scale_up.release());
rotation->SetChild(translation.release());
to_return->SetChild(rotation.release());
to_return->SetReversed(reversed);
return to_return.release();
}
TEST(InterpolatedTransformTest, ScreenRotationEndsCleanly) {
for (int i = 0; i < 2; ++i) {
for (int degrees = -360; degrees <= 360; degrees += 90) {
const bool reversed = i == 1;
scoped_ptr<ui::InterpolatedTransform> screen_rotation(
GetScreenRotation(degrees, reversed));
gfx::Transform interpolated = screen_rotation->Interpolate(1.0f);
SkMatrix44& m = interpolated.matrix();
// Upper-left 3x3 matrix should all be 0, 1 or -1.
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
float entry = m.get(row, col);
EXPECT_TRUE(entry == 0 || entry == 1 || entry == -1);
}
}
}
}
}
ui::InterpolatedTransform* GetMaximize() {
gfx::Rect target_bounds(0, 0, 1920, 1080);
gfx::Rect initial_bounds(30, 1000, 192, 108);
float scale_x = static_cast<float>(
target_bounds.height()) / initial_bounds.width();
float scale_y = static_cast<float>(
target_bounds.width()) / initial_bounds.height();
scoped_ptr<ui::InterpolatedTransform> scale(
new ui::InterpolatedScale(gfx::Point3f(1, 1, 1),
gfx::Point3f(scale_x, scale_y, 1)));
scoped_ptr<ui::InterpolatedTransform> translation(
new ui::InterpolatedTranslation(
gfx::Point(),
gfx::Point(target_bounds.x() - initial_bounds.x(),
target_bounds.y() - initial_bounds.y())));
scoped_ptr<ui::InterpolatedTransform> rotation(
new ui::InterpolatedRotation(0, 4.0f));
scoped_ptr<ui::InterpolatedTransform> rotation_about_pivot(
new ui::InterpolatedTransformAboutPivot(
gfx::Point(initial_bounds.width() * 0.5,
initial_bounds.height() * 0.5),
rotation.release()));
scale->SetChild(translation.release());
rotation_about_pivot->SetChild(scale.release());
rotation_about_pivot->SetReversed(true);
return rotation_about_pivot.release();
}
TEST(InterpolatedTransformTest, MaximizeEndsCleanly) {
scoped_ptr<ui::InterpolatedTransform> maximize(GetMaximize());
gfx::Transform interpolated = maximize->Interpolate(1.0f);
SkMatrix44& m = interpolated.matrix();
// Upper-left 3x3 matrix should all be 0, 1 or -1.
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
float entry = m.get(row, col);
EXPECT_TRUE(entry == 0 || entry == 1 || entry == -1);
}
}
}
|