Chromium Code Reviews| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "ui/gfx/quad_f.h" | 12 #include "ui/gfx/quad_f.h" |
| 13 #include "ui/gfx/rect.h" | 13 #include "ui/gfx/rect.h" |
| 14 #include "ui/gfx/rect_conversions.h" | 14 #include "ui/gfx/rect_conversions.h" |
| 15 #include "ui/gfx/rect_f.h" | 15 #include "ui/gfx/rect_f.h" |
| 16 #include "ui/gfx/transform.h" | 16 #include "ui/gfx/transform.h" |
| 17 #include "ui/gfx/vector2d_f.h" | 17 #include "ui/gfx/vector2d_f.h" |
| 18 | 18 |
| 19 namespace cc { | 19 namespace cc { |
| 20 namespace { | |
| 21 | |
| 22 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.
| |
| 23 DCHECK_GE(tolerance, 0); | |
| 24 return std::abs(x-y) <= tolerance; | |
| 25 } | |
| 26 | |
| 27 inline bool ApproximatelyInteger(SkMScalar x, SkMScalar tolerance) { | |
| 28 SkMScalar nearest_integer = std::floor(x + SkDoubleToMScalar(0.5)); | |
| 29 return ApproximatelyEqual(x, nearest_integer, tolerance); | |
| 30 } | |
| 31 | |
| 32 } // namespace | |
| 20 | 33 |
| 21 const double MathUtil::kPiDouble = 3.14159265358979323846; | 34 const double MathUtil::kPiDouble = 3.14159265358979323846; |
| 22 const float MathUtil::kPiFloat = 3.14159265358979323846f; | 35 const float MathUtil::kPiFloat = 3.14159265358979323846f; |
| 23 | 36 |
| 24 static HomogeneousCoordinate ProjectHomogeneousPoint( | 37 static HomogeneousCoordinate ProjectHomogeneousPoint( |
| 25 const gfx::Transform& transform, | 38 const gfx::Transform& transform, |
| 26 gfx::PointF p) { | 39 gfx::PointF p) { |
| 27 // In this case, the layer we are trying to project onto is perpendicular to | 40 // In this case, the layer we are trying to project onto is perpendicular to |
| 28 // ray (point p and z-axis direction) that we are trying to project. This | 41 // ray (point p and z-axis direction) that we are trying to project. This |
| 29 // happens when the layer is rotated so that it is infinitesimally thin, or | 42 // happens when the layer is rotated so that it is infinitesimally thin, or |
| (...skipping 556 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 586 scoped_ptr<base::Value> MathUtil::AsValueSafely(double value) { | 599 scoped_ptr<base::Value> MathUtil::AsValueSafely(double value) { |
| 587 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( | 600 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( |
| 588 std::min(value, std::numeric_limits<double>::max()))); | 601 std::min(value, std::numeric_limits<double>::max()))); |
| 589 } | 602 } |
| 590 | 603 |
| 591 scoped_ptr<base::Value> MathUtil::AsValueSafely(float value) { | 604 scoped_ptr<base::Value> MathUtil::AsValueSafely(float value) { |
| 592 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( | 605 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( |
| 593 std::min(value, std::numeric_limits<float>::max()))); | 606 std::min(value, std::numeric_limits<float>::max()))); |
| 594 } | 607 } |
| 595 | 608 |
| 609 bool MathUtil::IsApproximatelyPureTranslation( | |
| 610 const gfx::Transform& transform, SkMScalar tolerance) { | |
| 611 return IsMatrixApproximatelyPureTranslation(transform.matrix(), tolerance); | |
| 612 } | |
| 613 | |
| 614 bool MathUtil::IsMatrixApproximatelyPureTranslation(const SkMatrix44& matrix, | |
| 615 SkMScalar tolerance) { | |
| 616 return | |
| 617 ApproximatelyEqual(matrix.get(0, 0), 1, tolerance) && | |
| 618 ApproximatelyEqual(matrix.get(1, 0), 0, tolerance) && | |
| 619 ApproximatelyEqual(matrix.get(2, 0), 0, tolerance) && | |
| 620 matrix.get(3, 0) == 0 && | |
| 621 ApproximatelyEqual(matrix.get(0, 1), 0, tolerance) && | |
| 622 ApproximatelyEqual(matrix.get(1, 1), 1, tolerance) && | |
| 623 ApproximatelyEqual(matrix.get(2, 1), 0, tolerance) && | |
| 624 matrix.get(3, 1) == 0 && | |
| 625 ApproximatelyEqual(matrix.get(0, 2), 0, tolerance) && | |
| 626 ApproximatelyEqual(matrix.get(1, 2), 0, tolerance) && | |
| 627 ApproximatelyEqual(matrix.get(2, 2), 1, tolerance) && | |
| 628 matrix.get(3, 2) == 0 && | |
| 629 matrix.get(3, 3) == 1; | |
| 630 } | |
| 631 | |
| 632 bool MathUtil::IsApproximatelyIntegerTransform( | |
| 633 const gfx::Transform& transform, SkMScalar tolerance) { | |
| 634 return IsMatrixApproximatelyIntegerTransform(transform.matrix(), tolerance); | |
| 635 } | |
| 636 | |
| 637 bool MathUtil::IsMatrixApproximatelyIntegerTransform(const SkMatrix44& matrix, | |
| 638 SkMScalar tolerance) { | |
| 639 return | |
| 640 ApproximatelyInteger(matrix.get(0, 0), tolerance) && | |
| 641 ApproximatelyEqual(matrix.get(0, 1), 0, tolerance) && | |
| 642 ApproximatelyEqual(matrix.get(0, 2), 0, tolerance) && | |
| 643 ApproximatelyInteger(matrix.get(0, 3), tolerance) && | |
| 644 | |
| 645 ApproximatelyEqual(matrix.get(1, 0), 0, tolerance) && | |
| 646 ApproximatelyInteger(matrix.get(1, 1), tolerance) && | |
| 647 ApproximatelyEqual(matrix.get(1, 2), 0, tolerance) && | |
| 648 ApproximatelyInteger(matrix.get(1, 3), tolerance) && | |
| 649 | |
| 650 ApproximatelyEqual(matrix.get(2, 0), 0, tolerance) && | |
| 651 ApproximatelyEqual(matrix.get(2, 1), 0, tolerance) && | |
| 652 | |
| 653 ApproximatelyEqual(matrix.get(3, 0), 0, tolerance) && | |
| 654 ApproximatelyEqual(matrix.get(3, 1), 0, tolerance) && | |
| 655 ApproximatelyEqual(matrix.get(3, 2), 0, tolerance) && | |
| 656 ApproximatelyEqual(matrix.get(3, 3), 1, tolerance); | |
| 657 } | |
| 658 | |
| 596 } // namespace cc | 659 } // namespace cc |
| OLD | NEW |