1 // Copyright 2012 The Chromium 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.
5 #include "cc/base/math_util.h"
9 #include "cc/test/geometry_test_utils.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/gfx/geometry/rect_f.h"
14 #include "ui/gfx/transform.h"
19 TEST(MathUtilTest
, ProjectionOfPerpendicularPlane
) {
20 // In this case, the m33() element of the transform becomes zero, which could
21 // cause a divide-by-zero when projecting points/quads.
23 gfx::Transform transform
;
24 transform
.MakeIdentity();
25 transform
.matrix().set(2, 2, 0);
27 gfx::RectF rect
= gfx::RectF(0, 0, 1, 1);
28 gfx::RectF projected_rect
= MathUtil::ProjectClippedRect(transform
, rect
);
30 EXPECT_EQ(0, projected_rect
.x());
31 EXPECT_EQ(0, projected_rect
.y());
32 EXPECT_TRUE(projected_rect
.IsEmpty());
35 TEST(MathUtilTest
, EnclosingClippedRectUsesCorrectInitialBounds
) {
36 HomogeneousCoordinate
h1(-100, -100, 0, 1);
37 HomogeneousCoordinate
h2(-10, -10, 0, 1);
38 HomogeneousCoordinate
h3(10, 10, 0, -1);
39 HomogeneousCoordinate
h4(100, 100, 0, -1);
41 // The bounds of the enclosing clipped rect should be -100 to -10 for both x
42 // and y. However, if there is a bug where the initial xmin/xmax/ymin/ymax are
43 // initialized to numeric_limits<float>::min() (which is zero, not -flt_max)
44 // then the enclosing clipped rect will be computed incorrectly.
45 gfx::RectF result
= MathUtil::ComputeEnclosingClippedRect(h1
, h2
, h3
, h4
);
47 // Due to floating point math in ComputeClippedPointForEdge this result
48 // is fairly imprecise. 0.15f was empirically determined.
50 gfx::RectF(gfx::PointF(-100, -100), gfx::SizeF(90, 90)), result
, 0.15f
);
53 TEST(MathUtilTest
, EnclosingRectOfVerticesUsesCorrectInitialBounds
) {
54 gfx::PointF vertices
[3];
57 vertices
[0] = gfx::PointF(-10, -100);
58 vertices
[1] = gfx::PointF(-100, -10);
59 vertices
[2] = gfx::PointF(-30, -30);
61 // The bounds of the enclosing rect should be -100 to -10 for both x and y.
62 // However, if there is a bug where the initial xmin/xmax/ymin/ymax are
63 // initialized to numeric_limits<float>::min() (which is zero, not -flt_max)
64 // then the enclosing clipped rect will be computed incorrectly.
66 MathUtil::ComputeEnclosingRectOfVertices(vertices
, num_vertices
);
68 EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(-100, -100), gfx::SizeF(90, 90)),
72 TEST(MathUtilTest
, SmallestAngleBetweenVectors
) {
73 gfx::Vector2dF
x(1, 0);
74 gfx::Vector2dF
y(0, 1);
75 gfx::Vector2dF
test_vector(0.5, 0.5);
77 // Orthogonal vectors are at an angle of 90 degress.
78 EXPECT_EQ(90, MathUtil::SmallestAngleBetweenVectors(x
, y
));
80 // A vector makes a zero angle with itself.
81 EXPECT_EQ(0, MathUtil::SmallestAngleBetweenVectors(x
, x
));
82 EXPECT_EQ(0, MathUtil::SmallestAngleBetweenVectors(y
, y
));
83 EXPECT_EQ(0, MathUtil::SmallestAngleBetweenVectors(test_vector
, test_vector
));
85 // Parallel but reversed vectors are at 180 degrees.
86 EXPECT_FLOAT_EQ(180, MathUtil::SmallestAngleBetweenVectors(x
, -x
));
87 EXPECT_FLOAT_EQ(180, MathUtil::SmallestAngleBetweenVectors(y
, -y
));
89 180, MathUtil::SmallestAngleBetweenVectors(test_vector
, -test_vector
));
91 // The test vector is at a known angle.
93 45, std::floor(MathUtil::SmallestAngleBetweenVectors(test_vector
, x
)));
95 45, std::floor(MathUtil::SmallestAngleBetweenVectors(test_vector
, y
)));
98 TEST(MathUtilTest
, VectorProjection
) {
99 gfx::Vector2dF
x(1, 0);
100 gfx::Vector2dF
y(0, 1);
101 gfx::Vector2dF
test_vector(0.3f
, 0.7f
);
103 // Orthogonal vectors project to a zero vector.
104 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), MathUtil::ProjectVector(x
, y
));
105 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), MathUtil::ProjectVector(y
, x
));
107 // Projecting a vector onto the orthonormal basis gives the corresponding
108 // component of the vector.
109 EXPECT_VECTOR_EQ(gfx::Vector2dF(test_vector
.x(), 0),
110 MathUtil::ProjectVector(test_vector
, x
));
111 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, test_vector
.y()),
112 MathUtil::ProjectVector(test_vector
, y
));
114 // Finally check than an arbitrary vector projected to another one gives a
115 // vector parallel to the second vector.
116 gfx::Vector2dF
target_vector(0.5, 0.2f
);
117 gfx::Vector2dF projected_vector
=
118 MathUtil::ProjectVector(test_vector
, target_vector
);
119 EXPECT_EQ(projected_vector
.x() / target_vector
.x(),
120 projected_vector
.y() / target_vector
.y());
123 TEST(MathUtilTest
, MapEnclosedRectWith2dAxisAlignedTransform
) {
124 gfx::Rect
input(1, 2, 3, 4);
126 gfx::Transform transform
;
130 MathUtil::MapEnclosedRectWith2dAxisAlignedTransform(transform
, input
);
131 EXPECT_EQ(input
, output
);
133 // Integer translate.
134 transform
.Translate(2.0, 3.0);
136 MathUtil::MapEnclosedRectWith2dAxisAlignedTransform(transform
, input
);
137 EXPECT_EQ(gfx::Rect(3, 5, 3, 4), output
);
139 // Non-integer translate.
140 transform
.Translate(0.5, 0.5);
142 MathUtil::MapEnclosedRectWith2dAxisAlignedTransform(transform
, input
);
143 EXPECT_EQ(gfx::Rect(4, 6, 2, 3), output
);
146 transform
= gfx::Transform();
147 transform
.Scale(2.0, 3.0);
149 MathUtil::MapEnclosedRectWith2dAxisAlignedTransform(transform
, input
);
150 EXPECT_EQ(gfx::Rect(2, 6, 6, 12), output
);
153 transform
= gfx::Transform();
154 transform
.Translate(1.0, 2.0);
155 transform
.RotateAboutZAxis(90.0);
156 transform
.Translate(-1.0, -2.0);
158 MathUtil::MapEnclosedRectWith2dAxisAlignedTransform(transform
, input
);
159 EXPECT_EQ(gfx::Rect(-3, 2, 4, 3), output
);
162 transform
= gfx::Transform();
163 transform
.RotateAboutXAxis(90.0);
165 MathUtil::MapEnclosedRectWith2dAxisAlignedTransform(transform
, input
);
166 EXPECT_TRUE(output
.IsEmpty());
168 transform
= gfx::Transform();
169 transform
.RotateAboutXAxis(180.0);
171 MathUtil::MapEnclosedRectWith2dAxisAlignedTransform(transform
, input
);
172 EXPECT_EQ(gfx::Rect(1, -6, 3, 4), output
);
175 transform
= gfx::Transform();
176 transform
.RotateAboutYAxis(90.0);
178 MathUtil::MapEnclosedRectWith2dAxisAlignedTransform(transform
, input
);
179 EXPECT_TRUE(output
.IsEmpty());
181 transform
= gfx::Transform();
182 transform
.RotateAboutYAxis(180.0);
184 MathUtil::MapEnclosedRectWith2dAxisAlignedTransform(transform
, input
);
185 EXPECT_EQ(gfx::Rect(-4, 2, 3, 4), output
);
188 transform
= gfx::Transform();
189 transform
.ApplyPerspectiveDepth(10.0);
190 transform
.Translate3d(0.0, 0.0, 5.0);
192 MathUtil::MapEnclosedRectWith2dAxisAlignedTransform(transform
, input
);
193 EXPECT_EQ(gfx::Rect(2, 4, 6, 8), output
);