| OLD | NEW |
| (Empty) |
| 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/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 | |
| 16 namespace cc { | |
| 17 namespace { | |
| 18 | |
| 19 TEST(MathUtilTest, verifyProjectionOfPerpendicularPlane) | |
| 20 { | |
| 21 // In this case, the m33() element of the transform becomes zero, which coul
d cause a | |
| 22 // divide-by-zero when projecting points/quads. | |
| 23 | |
| 24 gfx::Transform transform; | |
| 25 transform.MakeIdentity(); | |
| 26 transform.matrix().setDouble(2, 2, 0); | |
| 27 | |
| 28 gfx::RectF rect = gfx::RectF(0, 0, 1, 1); | |
| 29 gfx::RectF projectedRect = MathUtil::projectClippedRect(transform, rect); | |
| 30 | |
| 31 EXPECT_EQ(0, projectedRect.x()); | |
| 32 EXPECT_EQ(0, projectedRect.y()); | |
| 33 EXPECT_TRUE(projectedRect.IsEmpty()); | |
| 34 } | |
| 35 | |
| 36 TEST(MathUtilTest, verifyEnclosingClippedRectUsesCorrectInitialBounds) | |
| 37 { | |
| 38 HomogeneousCoordinate h1(-100, -100, 0, 1); | |
| 39 HomogeneousCoordinate h2(-10, -10, 0, 1); | |
| 40 HomogeneousCoordinate h3(10, 10, 0, -1); | |
| 41 HomogeneousCoordinate h4(100, 100, 0, -1); | |
| 42 | |
| 43 // The bounds of the enclosing clipped rect should be -100 to -10 for both x
and y. | |
| 44 // However, if there is a bug where the initial xmin/xmax/ymin/ymax are init
ialized to | |
| 45 // numeric_limits<float>::min() (which is zero, not -flt_max) then the enclo
sing | |
| 46 // clipped rect will be computed incorrectly. | |
| 47 gfx::RectF result = MathUtil::computeEnclosingClippedRect(h1, h2, h3, h4); | |
| 48 | |
| 49 EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(-100, -100), gfx::SizeF(90, 90))
, result); | |
| 50 } | |
| 51 | |
| 52 TEST(MathUtilTest, verifyEnclosingRectOfVerticesUsesCorrectInitialBounds) | |
| 53 { | |
| 54 gfx::PointF vertices[3]; | |
| 55 int numVertices = 3; | |
| 56 | |
| 57 vertices[0] = gfx::PointF(-10, -100); | |
| 58 vertices[1] = gfx::PointF(-100, -10); | |
| 59 vertices[2] = gfx::PointF(-30, -30); | |
| 60 | |
| 61 // The bounds of the enclosing rect should be -100 to -10 for both x and y.
However, | |
| 62 // if there is a bug where the initial xmin/xmax/ymin/ymax are initialized t
o | |
| 63 // numeric_limits<float>::min() (which is zero, not -flt_max) then the enclo
sing | |
| 64 // clipped rect will be computed incorrectly. | |
| 65 gfx::RectF result = MathUtil::computeEnclosingRectOfVertices(vertices, numVe
rtices); | |
| 66 | |
| 67 EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(-100, -100), gfx::SizeF(90, 90))
, result); | |
| 68 } | |
| 69 | |
| 70 TEST(MathUtilTest, smallestAngleBetweenVectors) | |
| 71 { | |
| 72 gfx::Vector2dF x(1, 0); | |
| 73 gfx::Vector2dF y(0, 1); | |
| 74 gfx::Vector2dF testVector(0.5, 0.5); | |
| 75 | |
| 76 // Orthogonal vectors are at an angle of 90 degress. | |
| 77 EXPECT_EQ(90, MathUtil::smallestAngleBetweenVectors(x, y)); | |
| 78 | |
| 79 // A vector makes a zero angle with itself. | |
| 80 EXPECT_EQ(0, MathUtil::smallestAngleBetweenVectors(x, x)); | |
| 81 EXPECT_EQ(0, MathUtil::smallestAngleBetweenVectors(y, y)); | |
| 82 EXPECT_EQ(0, MathUtil::smallestAngleBetweenVectors(testVector, testVector)); | |
| 83 | |
| 84 // Parallel but reversed vectors are at 180 degrees. | |
| 85 EXPECT_FLOAT_EQ(180, MathUtil::smallestAngleBetweenVectors(x, -x)); | |
| 86 EXPECT_FLOAT_EQ(180, MathUtil::smallestAngleBetweenVectors(y, -y)); | |
| 87 EXPECT_FLOAT_EQ(180, MathUtil::smallestAngleBetweenVectors(testVector, -test
Vector)); | |
| 88 | |
| 89 // The test vector is at a known angle. | |
| 90 EXPECT_FLOAT_EQ(45, std::floor(MathUtil::smallestAngleBetweenVectors(testVec
tor, x))); | |
| 91 EXPECT_FLOAT_EQ(45, std::floor(MathUtil::smallestAngleBetweenVectors(testVec
tor, y))); | |
| 92 } | |
| 93 | |
| 94 TEST(MathUtilTest, vectorProjection) | |
| 95 { | |
| 96 gfx::Vector2dF x(1, 0); | |
| 97 gfx::Vector2dF y(0, 1); | |
| 98 gfx::Vector2dF testVector(0.3f, 0.7f); | |
| 99 | |
| 100 // Orthogonal vectors project to a zero vector. | |
| 101 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), MathUtil::projectVector(x, y)); | |
| 102 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), MathUtil::projectVector(y, x)); | |
| 103 | |
| 104 // Projecting a vector onto the orthonormal basis gives the corresponding co
mponent of the | |
| 105 // vector. | |
| 106 EXPECT_VECTOR_EQ(gfx::Vector2dF(testVector.x(), 0), MathUtil::projectVector(
testVector, x)); | |
| 107 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, testVector.y()), MathUtil::projectVector(
testVector, y)); | |
| 108 | |
| 109 // Finally check than an arbitrary vector projected to another one gives a v
ector parallel to | |
| 110 // the second vector. | |
| 111 gfx::Vector2dF targetVector(0.5, 0.2f); | |
| 112 gfx::Vector2dF projectedVector = MathUtil::projectVector(testVector, targetV
ector); | |
| 113 EXPECT_EQ(projectedVector.x() / targetVector.x(), | |
| 114 projectedVector.y() / targetVector.y()); | |
| 115 } | |
| 116 | |
| 117 } // namespace | |
| 118 } // namespace cc | |
| OLD | NEW |