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 ApproximatelyZero(SkMScalar x, SkMScalar tolerance) { | |
| 23 DCHECK_GE(tolerance, 0); | |
| 24 return std::abs(x) <= tolerance; | |
| 25 } | |
| 26 | |
| 27 inline bool ApproximatelyOne(SkMScalar x, SkMScalar tolerance) { | |
| 28 DCHECK_GE(tolerance, 0); | |
| 29 return std::abs(x-SkDoubleToMScalar(1.0)) <= tolerance; | |
|
danakj
2013/10/16 19:04:14
spaces around -
| |
| 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 ApproximatelyOne(matrix.get(0, 0), tolerance) && | |
| 618 ApproximatelyZero(matrix.get(1, 0), tolerance) && | |
| 619 ApproximatelyZero(matrix.get(2, 0), tolerance) && | |
| 620 matrix.get(3, 0) == 0 && | |
| 621 ApproximatelyZero(matrix.get(0, 1), tolerance) && | |
| 622 ApproximatelyOne(matrix.get(1, 1), tolerance) && | |
| 623 ApproximatelyZero(matrix.get(2, 1), tolerance) && | |
| 624 matrix.get(3, 1) == 0 && | |
| 625 ApproximatelyZero(matrix.get(0, 2), tolerance) && | |
| 626 ApproximatelyZero(matrix.get(1, 2), tolerance) && | |
| 627 ApproximatelyOne(matrix.get(2, 2), tolerance) && | |
| 628 matrix.get(3, 2) == 0 && | |
| 629 matrix.get(3, 3) == 1; | |
| 630 } | |
| 631 | |
| 596 } // namespace cc | 632 } // namespace cc |
| OLD | NEW |