Chromium Code Reviews| Index: cc/base/math_util.cc |
| diff --git a/cc/base/math_util.cc b/cc/base/math_util.cc |
| index eeb1523db46a080aff9850caaf126c1c3f8b0509..e7f17f368e520c204d4c34541d9bb4169544670f 100644 |
| --- a/cc/base/math_util.cc |
| +++ b/cc/base/math_util.cc |
| @@ -17,6 +17,19 @@ |
| #include "ui/gfx/vector2d_f.h" |
| namespace cc { |
| +namespace { |
| + |
| +inline bool ApproximatelyEqual(SkMScalar x, SkMScalar y, SkMScalar tolerance) { |
|
shawnsingh
2013/10/16 11:43:34
I'm wondering, did you have a strong reason not to
Dominik Grewe
2013/10/16 12:46:23
The main motivation was to avoid code duplication.
|
| + DCHECK_GE(tolerance, 0); |
| + return std::abs(x-y) <= tolerance; |
| +} |
| + |
| +inline bool ApproximatelyInteger(SkMScalar x, SkMScalar tolerance) { |
| + SkMScalar nearest_integer = std::floor(x + SkDoubleToMScalar(0.5)); |
| + return ApproximatelyEqual(x, nearest_integer, tolerance); |
| +} |
| + |
| +} // namespace |
| const double MathUtil::kPiDouble = 3.14159265358979323846; |
| const float MathUtil::kPiFloat = 3.14159265358979323846f; |
| @@ -593,4 +606,54 @@ scoped_ptr<base::Value> MathUtil::AsValueSafely(float value) { |
| std::min(value, std::numeric_limits<float>::max()))); |
| } |
| +bool MathUtil::IsApproximatelyPureTranslation( |
| + const gfx::Transform& transform, SkMScalar tolerance) { |
| + return IsMatrixApproximatelyPureTranslation(transform.matrix(), tolerance); |
| +} |
| + |
| +bool MathUtil::IsMatrixApproximatelyPureTranslation(const SkMatrix44& matrix, |
| + SkMScalar tolerance) { |
| + return |
| + ApproximatelyEqual(matrix.get(0, 0), 1, tolerance) && |
| + ApproximatelyEqual(matrix.get(1, 0), 0, tolerance) && |
| + ApproximatelyEqual(matrix.get(2, 0), 0, tolerance) && |
| + matrix.get(3, 0) == 0 && |
| + ApproximatelyEqual(matrix.get(0, 1), 0, tolerance) && |
| + ApproximatelyEqual(matrix.get(1, 1), 1, tolerance) && |
| + ApproximatelyEqual(matrix.get(2, 1), 0, tolerance) && |
| + matrix.get(3, 1) == 0 && |
| + ApproximatelyEqual(matrix.get(0, 2), 0, tolerance) && |
| + ApproximatelyEqual(matrix.get(1, 2), 0, tolerance) && |
| + ApproximatelyEqual(matrix.get(2, 2), 1, tolerance) && |
| + matrix.get(3, 2) == 0 && |
| + matrix.get(3, 3) == 1; |
| +} |
| + |
| +bool MathUtil::IsApproximatelyIntegerTransform( |
| + const gfx::Transform& transform, SkMScalar tolerance) { |
| + return IsMatrixApproximatelyIntegerTransform(transform.matrix(), tolerance); |
| +} |
| + |
| +bool MathUtil::IsMatrixApproximatelyIntegerTransform(const SkMatrix44& matrix, |
| + SkMScalar tolerance) { |
| + return |
| + ApproximatelyInteger(matrix.get(0, 0), tolerance) && |
| + ApproximatelyEqual(matrix.get(0, 1), 0, tolerance) && |
| + ApproximatelyEqual(matrix.get(0, 2), 0, tolerance) && |
| + ApproximatelyInteger(matrix.get(0, 3), tolerance) && |
| + |
| + ApproximatelyEqual(matrix.get(1, 0), 0, tolerance) && |
| + ApproximatelyInteger(matrix.get(1, 1), tolerance) && |
| + ApproximatelyEqual(matrix.get(1, 2), 0, tolerance) && |
| + ApproximatelyInteger(matrix.get(1, 3), tolerance) && |
| + |
| + ApproximatelyEqual(matrix.get(2, 0), 0, tolerance) && |
| + ApproximatelyEqual(matrix.get(2, 1), 0, tolerance) && |
| + |
| + ApproximatelyEqual(matrix.get(3, 0), 0, tolerance) && |
| + ApproximatelyEqual(matrix.get(3, 1), 0, tolerance) && |
| + ApproximatelyEqual(matrix.get(3, 2), 0, tolerance) && |
| + ApproximatelyEqual(matrix.get(3, 3), 1, tolerance); |
| +} |
| + |
| } // namespace cc |