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)); | |
shawnsingh
2013/10/16 19:05:37
nice way to check that we are not accidentally an
| |
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 | |
123 } // namespace | 201 } // namespace |
124 } // namespace cc | 202 } // namespace cc |
OLD | NEW |