OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/base/math_util.h" | 5 #include "cc/base/math_util.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
| 8 #include <limits> |
8 | 9 |
9 #include "cc/test/geometry_test_utils.h" | 10 #include "cc/test/geometry_test_utils.h" |
10 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "ui/gfx/rect.h" | 13 #include "ui/gfx/rect.h" |
13 #include "ui/gfx/rect_f.h" | 14 #include "ui/gfx/rect_f.h" |
14 #include "ui/gfx/transform.h" | 15 #include "ui/gfx/transform.h" |
15 | 16 |
16 namespace cc { | 17 namespace cc { |
17 namespace { | 18 namespace { |
18 | 19 |
| 20 const SkMScalar kApproxZero = |
| 21 SkFloatToMScalar(std::numeric_limits<float>::epsilon()); |
| 22 const SkMScalar kApproxOne = 1 - kApproxZero; |
| 23 |
| 24 void SetApproxIdentity(SkMatrix44* matrix) { |
| 25 matrix->set(0, 0, kApproxOne); |
| 26 matrix->set(0, 1, kApproxZero); |
| 27 matrix->set(0, 2, kApproxZero); |
| 28 matrix->set(0, 3, kApproxZero); |
| 29 |
| 30 matrix->set(1, 0, kApproxZero); |
| 31 matrix->set(1, 1, kApproxOne); |
| 32 matrix->set(1, 2, kApproxZero); |
| 33 matrix->set(1, 3, kApproxZero); |
| 34 |
| 35 matrix->set(2, 0, kApproxZero); |
| 36 matrix->set(2, 1, kApproxZero); |
| 37 matrix->set(2, 2, kApproxOne); |
| 38 matrix->set(2, 3, kApproxZero); |
| 39 |
| 40 matrix->set(3, 0, kApproxZero); |
| 41 matrix->set(3, 1, kApproxZero); |
| 42 matrix->set(3, 2, kApproxZero); |
| 43 matrix->set(3, 3, kApproxOne); |
| 44 } |
| 45 |
19 TEST(MathUtilTest, ProjectionOfPerpendicularPlane) { | 46 TEST(MathUtilTest, ProjectionOfPerpendicularPlane) { |
20 // In this case, the m33() element of the transform becomes zero, which could | 47 // In this case, the m33() element of the transform becomes zero, which could |
21 // cause a divide-by-zero when projecting points/quads. | 48 // cause a divide-by-zero when projecting points/quads. |
22 | 49 |
23 gfx::Transform transform; | 50 gfx::Transform transform; |
24 transform.MakeIdentity(); | 51 transform.MakeIdentity(); |
25 transform.matrix().set(2, 2, 0); | 52 transform.matrix().set(2, 2, 0); |
26 | 53 |
27 gfx::RectF rect = gfx::RectF(0, 0, 1, 1); | 54 gfx::RectF rect = gfx::RectF(0, 0, 1, 1); |
28 gfx::RectF projected_rect = MathUtil::ProjectClippedRect(transform, rect); | 55 gfx::RectF projected_rect = MathUtil::ProjectClippedRect(transform, rect); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 | 140 |
114 // Finally check than an arbitrary vector projected to another one gives a | 141 // Finally check than an arbitrary vector projected to another one gives a |
115 // vector parallel to the second vector. | 142 // vector parallel to the second vector. |
116 gfx::Vector2dF target_vector(0.5, 0.2f); | 143 gfx::Vector2dF target_vector(0.5, 0.2f); |
117 gfx::Vector2dF projected_vector = | 144 gfx::Vector2dF projected_vector = |
118 MathUtil::ProjectVector(test_vector, target_vector); | 145 MathUtil::ProjectVector(test_vector, target_vector); |
119 EXPECT_EQ(projected_vector.x() / target_vector.x(), | 146 EXPECT_EQ(projected_vector.x() / target_vector.x(), |
120 projected_vector.y() / target_vector.y()); | 147 projected_vector.y() / target_vector.y()); |
121 } | 148 } |
122 | 149 |
| 150 TEST(MathUtilTest, ExactPureTranslationMatrix) { |
| 151 SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor); |
| 152 |
| 153 // set translate values to values other than 0 or 1 |
| 154 matrix.set(0, 3, 3.4); |
| 155 matrix.set(1, 3, 4.4); |
| 156 matrix.set(2, 3, 5.6); |
| 157 |
| 158 EXPECT_TRUE(MathUtil::IsMatrixApproximatelyPureTranslation(matrix, 0)); |
| 159 EXPECT_TRUE( |
| 160 MathUtil::IsMatrixApproximatelyPureTranslation(matrix, kApproxZero)); |
| 161 } |
| 162 |
| 163 TEST(MathUtilTest, ApproximatelyPureTranslationMatrix) { |
| 164 SkMatrix44 matrix(SkMatrix44::kUninitialized_Constructor); |
| 165 SetApproxIdentity(&matrix); |
| 166 // Some values must be exact |
| 167 matrix.set(3, 0, 0); |
| 168 matrix.set(3, 1, 0); |
| 169 matrix.set(3, 2, 0); |
| 170 matrix.set(3, 3, 1); |
| 171 |
| 172 // set translate values to values other than 0 or 1 |
| 173 matrix.set(0, 3, 3.4); |
| 174 matrix.set(1, 3, 4.4); |
| 175 matrix.set(2, 3, 5.6); |
| 176 |
| 177 EXPECT_FALSE(MathUtil::IsMatrixApproximatelyPureTranslation(matrix, 0)); |
| 178 EXPECT_TRUE( |
| 179 MathUtil::IsMatrixApproximatelyPureTranslation(matrix, kApproxZero)); |
| 180 } |
| 181 |
| 182 TEST(MathUtilTest, NotApproximatelyPureTranslationMatrix) { |
| 183 SkMatrix44 matrix(SkMatrix44::kUninitialized_Constructor); |
| 184 SetApproxIdentity(&matrix); |
| 185 // Some values must be exact |
| 186 matrix.set(3, 0, 0); |
| 187 matrix.set(3, 1, 0); |
| 188 matrix.set(3, 2, 0); |
| 189 matrix.set(3, 3, 1); |
| 190 |
| 191 // set some values (not translate values) to values other than 0 or 1 |
| 192 matrix.set(0, 1, 3.4); |
| 193 matrix.set(3, 2, 4.4); |
| 194 matrix.set(2, 0, 5.6); |
| 195 |
| 196 EXPECT_FALSE(MathUtil::IsMatrixApproximatelyPureTranslation(matrix, 0)); |
| 197 EXPECT_FALSE( |
| 198 MathUtil::IsMatrixApproximatelyPureTranslation(matrix, kApproxZero)); |
| 199 } |
| 200 |
| 201 TEST(MathUtilTest, ExactIntegerTransformMatrix) { |
| 202 SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor); |
| 203 |
| 204 // set transform values to integers other than 0 or 1 |
| 205 matrix.set(0, 0, 3); |
| 206 matrix.set(0, 3, 4); |
| 207 matrix.set(1, 1, 5); |
| 208 matrix.set(1, 3, 6); |
| 209 |
| 210 EXPECT_TRUE(MathUtil::IsMatrixApproximatelyIntegerTransform(matrix, 0)); |
| 211 EXPECT_TRUE( |
| 212 MathUtil::IsMatrixApproximatelyIntegerTransform(matrix, kApproxZero)); |
| 213 } |
| 214 |
| 215 TEST(MathUtilTest, ApproximatelyIntegerTransformMatrix) { |
| 216 SkMatrix44 matrix(SkMatrix44::kUninitialized_Constructor); |
| 217 SetApproxIdentity(&matrix); |
| 218 |
| 219 // set transform values to approxamitely integers other than 0 or 1 |
| 220 matrix.set(0, 0, 3 - kApproxZero); |
| 221 matrix.set(0, 3, 4 + kApproxZero); |
| 222 matrix.set(1, 1, 5 + kApproxZero); |
| 223 matrix.set(1, 3, 6 - kApproxZero); |
| 224 |
| 225 EXPECT_FALSE(MathUtil::IsMatrixApproximatelyIntegerTransform(matrix, 0)); |
| 226 EXPECT_TRUE( |
| 227 MathUtil::IsMatrixApproximatelyIntegerTransform(matrix, kApproxZero)); |
| 228 } |
| 229 |
| 230 TEST(MathUtilTest, NotApproximatelyIntegerTransformMatrix) { |
| 231 SkMatrix44 matrix(SkMatrix44::kUninitialized_Constructor); |
| 232 SetApproxIdentity(&matrix); |
| 233 |
| 234 // set some values (not transform values) to values other than 0 or 1 |
| 235 matrix.set(0, 1, 3.4); |
| 236 matrix.set(3, 2, 4.4); |
| 237 matrix.set(2, 0, 5.6); |
| 238 |
| 239 EXPECT_FALSE(MathUtil::IsMatrixApproximatelyIntegerTransform(matrix, 0)); |
| 240 EXPECT_FALSE( |
| 241 MathUtil::IsMatrixApproximatelyIntegerTransform(matrix, kApproxZero)); |
| 242 } |
| 243 |
123 } // namespace | 244 } // namespace |
124 } // namespace cc | 245 } // namespace cc |
OLD | NEW |