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.
4
5#include "cc/base/math_util.h"
6
7#include <cmath>
8
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/rect.h"
13#include "ui/gfx/rect_f.h"
14#include "ui/gfx/transform.h"
15
16namespace cc {
17namespace {
18
19TEST(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.
22
23  gfx::Transform transform;
24  transform.MakeIdentity();
25  transform.matrix().setDouble(2, 2, 0);
26
27  gfx::RectF rect = gfx::RectF(0, 0, 1, 1);
28  gfx::RectF projected_rect = MathUtil::ProjectClippedRect(transform, rect);
29
30  EXPECT_EQ(0, projected_rect.x());
31  EXPECT_EQ(0, projected_rect.y());
32  EXPECT_TRUE(projected_rect.IsEmpty());
33}
34
35TEST(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);
40
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);
46
47  EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(-100, -100), gfx::SizeF(90, 90)),
48                       result);
49}
50
51TEST(MathUtilTest, EnclosingRectOfVerticesUsesCorrectInitialBounds) {
52  gfx::PointF vertices[3];
53  int num_vertices = 3;
54
55  vertices[0] = gfx::PointF(-10, -100);
56  vertices[1] = gfx::PointF(-100, -10);
57  vertices[2] = gfx::PointF(-30, -30);
58
59  // The bounds of the enclosing rect should be -100 to -10 for both x and y.
60  // However, if there is a bug where the initial xmin/xmax/ymin/ymax are
61  // initialized to numeric_limits<float>::min() (which is zero, not -flt_max)
62  // then the enclosing clipped rect will be computed incorrectly.
63  gfx::RectF result =
64      MathUtil::ComputeEnclosingRectOfVertices(vertices, num_vertices);
65
66  EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(-100, -100), gfx::SizeF(90, 90)),
67                       result);
68}
69
70TEST(MathUtilTest, SmallestAngleBetweenVectors) {
71  gfx::Vector2dF x(1, 0);
72  gfx::Vector2dF y(0, 1);
73  gfx::Vector2dF test_vector(0.5, 0.5);
74
75  // Orthogonal vectors are at an angle of 90 degress.
76  EXPECT_EQ(90, MathUtil::SmallestAngleBetweenVectors(x, y));
77
78  // A vector makes a zero angle with itself.
79  EXPECT_EQ(0, MathUtil::SmallestAngleBetweenVectors(x, x));
80  EXPECT_EQ(0, MathUtil::SmallestAngleBetweenVectors(y, y));
81  EXPECT_EQ(0, MathUtil::SmallestAngleBetweenVectors(test_vector, test_vector));
82
83  // Parallel but reversed vectors are at 180 degrees.
84  EXPECT_FLOAT_EQ(180, MathUtil::SmallestAngleBetweenVectors(x, -x));
85  EXPECT_FLOAT_EQ(180, MathUtil::SmallestAngleBetweenVectors(y, -y));
86  EXPECT_FLOAT_EQ(
87      180, MathUtil::SmallestAngleBetweenVectors(test_vector, -test_vector));
88
89  // The test vector is at a known angle.
90  EXPECT_FLOAT_EQ(
91      45, std::floor(MathUtil::SmallestAngleBetweenVectors(test_vector, x)));
92  EXPECT_FLOAT_EQ(
93      45, std::floor(MathUtil::SmallestAngleBetweenVectors(test_vector, y)));
94}
95
96TEST(MathUtilTest, VectorProjection) {
97  gfx::Vector2dF x(1, 0);
98  gfx::Vector2dF y(0, 1);
99  gfx::Vector2dF test_vector(0.3f, 0.7f);
100
101  // Orthogonal vectors project to a zero vector.
102  EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), MathUtil::ProjectVector(x, y));
103  EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), MathUtil::ProjectVector(y, x));
104
105  // Projecting a vector onto the orthonormal basis gives the corresponding
106  // component of the vector.
107  EXPECT_VECTOR_EQ(gfx::Vector2dF(test_vector.x(), 0),
108                   MathUtil::ProjectVector(test_vector, x));
109  EXPECT_VECTOR_EQ(gfx::Vector2dF(0, test_vector.y()),
110                   MathUtil::ProjectVector(test_vector, y));
111
112  // Finally check than an arbitrary vector projected to another one gives a
113  // vector parallel to the second vector.
114  gfx::Vector2dF target_vector(0.5, 0.2f);
115  gfx::Vector2dF projected_vector =
116      MathUtil::ProjectVector(test_vector, target_vector);
117  EXPECT_EQ(projected_vector.x() / target_vector.x(),
118            projected_vector.y() / target_vector.y());
119}
120
121}  // namespace
122}  // namespace cc
123