Index: cc/base/math_util_unittest.cc |
diff --git a/cc/base/math_util_unittest.cc b/cc/base/math_util_unittest.cc |
index 6e276c25d64736057f975c17ce1b4ee6c08f7c46..91a7b6e1120a028342756839275052488eeba65c 100644 |
--- a/cc/base/math_util_unittest.cc |
+++ b/cc/base/math_util_unittest.cc |
@@ -5,6 +5,7 @@ |
#include "cc/base/math_util.h" |
#include <cmath> |
+#include <limits> |
#include "cc/test/geometry_test_utils.h" |
#include "testing/gmock/include/gmock/gmock.h" |
@@ -16,6 +17,32 @@ |
namespace cc { |
namespace { |
+const SkMScalar kApproxZero = |
+ SkFloatToMScalar(std::numeric_limits<float>::epsilon()); |
+const SkMScalar kApproxOne = 1 - kApproxZero; |
+ |
+void SetApproxIdentity(SkMatrix44* matrix) { |
+ matrix->set(0, 0, kApproxOne); |
+ matrix->set(0, 1, kApproxZero); |
+ matrix->set(0, 2, kApproxZero); |
+ matrix->set(0, 3, kApproxZero); |
+ |
+ matrix->set(1, 0, kApproxZero); |
+ matrix->set(1, 1, kApproxOne); |
+ matrix->set(1, 2, kApproxZero); |
+ matrix->set(1, 3, kApproxZero); |
+ |
+ matrix->set(2, 0, kApproxZero); |
+ matrix->set(2, 1, kApproxZero); |
+ matrix->set(2, 2, kApproxOne); |
+ matrix->set(2, 3, kApproxZero); |
+ |
+ matrix->set(3, 0, kApproxZero); |
+ matrix->set(3, 1, kApproxZero); |
+ matrix->set(3, 2, kApproxZero); |
+ matrix->set(3, 3, kApproxOne); |
+} |
+ |
TEST(MathUtilTest, ProjectionOfPerpendicularPlane) { |
// In this case, the m33() element of the transform becomes zero, which could |
// cause a divide-by-zero when projecting points/quads. |
@@ -120,5 +147,99 @@ TEST(MathUtilTest, VectorProjection) { |
projected_vector.y() / target_vector.y()); |
} |
+TEST(MathUtilTest, ExactPureTranslationMatrix) { |
+ SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor); |
+ |
+ // set translate values to values other than 0 or 1 |
+ matrix.set(0, 3, 3.4); |
+ matrix.set(1, 3, 4.4); |
+ matrix.set(2, 3, 5.6); |
+ |
+ EXPECT_TRUE(MathUtil::IsMatrixApproximatelyPureTranslation(matrix, 0)); |
+ EXPECT_TRUE( |
+ MathUtil::IsMatrixApproximatelyPureTranslation(matrix, kApproxZero)); |
+} |
+ |
+TEST(MathUtilTest, ApproximatelyPureTranslationMatrix) { |
+ SkMatrix44 matrix(SkMatrix44::kUninitialized_Constructor); |
+ SetApproxIdentity(&matrix); |
+ // Some values must be exact |
+ matrix.set(3, 0, 0); |
+ matrix.set(3, 1, 0); |
+ matrix.set(3, 2, 0); |
+ matrix.set(3, 3, 1); |
+ |
+ // set translate values to values other than 0 or 1 |
+ matrix.set(0, 3, 3.4); |
+ matrix.set(1, 3, 4.4); |
+ matrix.set(2, 3, 5.6); |
+ |
+ EXPECT_FALSE(MathUtil::IsMatrixApproximatelyPureTranslation(matrix, 0)); |
+ EXPECT_TRUE( |
+ MathUtil::IsMatrixApproximatelyPureTranslation(matrix, kApproxZero)); |
+} |
+ |
+TEST(MathUtilTest, NotApproximatelyPureTranslationMatrix) { |
+ SkMatrix44 matrix(SkMatrix44::kUninitialized_Constructor); |
+ SetApproxIdentity(&matrix); |
+ // Some values must be exact |
+ matrix.set(3, 0, 0); |
+ matrix.set(3, 1, 0); |
+ matrix.set(3, 2, 0); |
+ matrix.set(3, 3, 1); |
+ |
+ // set some values (not translate values) to values other than 0 or 1 |
+ matrix.set(0, 1, 3.4); |
+ matrix.set(3, 2, 4.4); |
+ matrix.set(2, 0, 5.6); |
+ |
+ EXPECT_FALSE(MathUtil::IsMatrixApproximatelyPureTranslation(matrix, 0)); |
+ EXPECT_FALSE( |
+ MathUtil::IsMatrixApproximatelyPureTranslation(matrix, kApproxZero)); |
+} |
+ |
+TEST(MathUtilTest, ExactIntegerTransformMatrix) { |
+ SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor); |
+ |
+ // set transform values to integers other than 0 or 1 |
+ matrix.set(0, 0, 3); |
+ matrix.set(0, 3, 4); |
+ matrix.set(1, 1, 5); |
+ matrix.set(1, 3, 6); |
+ |
+ EXPECT_TRUE(MathUtil::IsMatrixApproximatelyIntegerTransform(matrix, 0)); |
+ EXPECT_TRUE( |
+ MathUtil::IsMatrixApproximatelyIntegerTransform(matrix, kApproxZero)); |
+} |
+ |
+TEST(MathUtilTest, ApproximatelyIntegerTransformMatrix) { |
+ SkMatrix44 matrix(SkMatrix44::kUninitialized_Constructor); |
+ SetApproxIdentity(&matrix); |
+ |
+ // set transform values to approxamitely integers other than 0 or 1 |
+ matrix.set(0, 0, 3 - kApproxZero); |
+ matrix.set(0, 3, 4 + kApproxZero); |
+ matrix.set(1, 1, 5 + kApproxZero); |
+ matrix.set(1, 3, 6 - kApproxZero); |
+ |
+ EXPECT_FALSE(MathUtil::IsMatrixApproximatelyIntegerTransform(matrix, 0)); |
+ EXPECT_TRUE( |
+ MathUtil::IsMatrixApproximatelyIntegerTransform(matrix, kApproxZero)); |
+} |
+ |
+TEST(MathUtilTest, NotApproximatelyIntegerTransformMatrix) { |
+ SkMatrix44 matrix(SkMatrix44::kUninitialized_Constructor); |
+ SetApproxIdentity(&matrix); |
+ |
+ // set some values (not transform values) to values other than 0 or 1 |
+ matrix.set(0, 1, 3.4); |
+ matrix.set(3, 2, 4.4); |
+ matrix.set(2, 0, 5.6); |
+ |
+ EXPECT_FALSE(MathUtil::IsMatrixApproximatelyIntegerTransform(matrix, 0)); |
+ EXPECT_FALSE( |
+ MathUtil::IsMatrixApproximatelyIntegerTransform(matrix, kApproxZero)); |
+} |
+ |
} // namespace |
} // namespace cc |