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 static inline bool SkMScalarApproximatelyEqual(SkMScalar x, SkMScalar y, | |
danakj
2013/10/15 15:34:35
can you drop the SkMScalar prefix from these metho
Dominik Grewe
2013/10/15 17:04:19
Done.
| |
23 SkMScalar tolerance) { | |
24 DCHECK_GE(tolerance, 0); | |
25 return SkTAbs<SkMScalar>(x-y) <= tolerance; | |
danakj
2013/10/15 15:34:35
use std::abs()
Dominik Grewe
2013/10/15 17:04:19
Done.
| |
26 } | |
27 | |
28 static inline bool SkMScalarApproximatelyInteger(SkMScalar x, | |
danakj
2013/10/15 15:34:35
drop the anonymous namespace, or drop the "static"
Dominik Grewe
2013/10/15 17:04:19
Done.
| |
29 SkMScalar tolerance) { | |
30 SkMScalar nearestInteger = std::floor(x + SkDoubleToMScalar(0.5)); | |
danakj
2013/10/15 15:34:35
how about just "0.5f" instead of "SkDoubleToMScala
danakj
2013/10/15 15:34:35
nearest_integer
Dominik Grewe
2013/10/15 17:04:19
Done.
Dominik Grewe
2013/10/15 17:04:19
I wanted to keep it independent of whether SkMScal
| |
31 return SkMScalarApproximatelyEqual(x, nearestInteger, tolerance); | |
32 } | |
33 | |
34 } // namespace | |
20 | 35 |
21 const double MathUtil::kPiDouble = 3.14159265358979323846; | 36 const double MathUtil::kPiDouble = 3.14159265358979323846; |
22 const float MathUtil::kPiFloat = 3.14159265358979323846f; | 37 const float MathUtil::kPiFloat = 3.14159265358979323846f; |
23 | 38 |
24 static HomogeneousCoordinate ProjectHomogeneousPoint( | 39 static HomogeneousCoordinate ProjectHomogeneousPoint( |
25 const gfx::Transform& transform, | 40 const gfx::Transform& transform, |
26 gfx::PointF p) { | 41 gfx::PointF p) { |
27 // In this case, the layer we are trying to project onto is perpendicular to | 42 // 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 | 43 // 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 | 44 // 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) { | 601 scoped_ptr<base::Value> MathUtil::AsValueSafely(double value) { |
587 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( | 602 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( |
588 std::min(value, std::numeric_limits<double>::max()))); | 603 std::min(value, std::numeric_limits<double>::max()))); |
589 } | 604 } |
590 | 605 |
591 scoped_ptr<base::Value> MathUtil::AsValueSafely(float value) { | 606 scoped_ptr<base::Value> MathUtil::AsValueSafely(float value) { |
592 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( | 607 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( |
593 std::min(value, std::numeric_limits<float>::max()))); | 608 std::min(value, std::numeric_limits<float>::max()))); |
594 } | 609 } |
595 | 610 |
611 bool MathUtil::IsMatrixApproximatelyPureTranslation(const SkMatrix44& matrix, | |
danakj
2013/10/15 15:34:35
Can you make these methods take a gfx::Transform i
Dominik Grewe
2013/10/15 17:04:19
How about overloading it for both types? Would mak
| |
612 SkMScalar tolerance) { | |
613 return | |
614 SkMScalarApproximatelyEqual(matrix.get(0, 0), 1, tolerance) && | |
danakj
2013/10/15 15:34:35
1.f (don't use integer literals for floating point
Dominik Grewe
2013/10/15 17:04:19
How about "SkFloatToMScalar(1.f)" to keep it indep
| |
615 SkMScalarApproximatelyEqual(matrix.get(1, 0), 0, tolerance) && | |
616 SkMScalarApproximatelyEqual(matrix.get(2, 0), 0, tolerance) && | |
617 matrix.get(3, 0) == 0 && | |
618 SkMScalarApproximatelyEqual(matrix.get(0, 1), 0, tolerance) && | |
619 SkMScalarApproximatelyEqual(matrix.get(1, 1), 1, tolerance) && | |
620 SkMScalarApproximatelyEqual(matrix.get(2, 1), 0, tolerance) && | |
621 matrix.get(3, 1) == 0 && | |
622 SkMScalarApproximatelyEqual(matrix.get(0, 2), 0, tolerance) && | |
623 SkMScalarApproximatelyEqual(matrix.get(1, 2), 0, tolerance) && | |
624 SkMScalarApproximatelyEqual(matrix.get(2, 2), 1, tolerance) && | |
625 matrix.get(3, 2) == 0 && | |
626 matrix.get(3, 3) == 1; | |
627 } | |
628 | |
629 bool MathUtil::IsMatrixApproximatelyIntegerTransform(const SkMatrix44& matrix, | |
630 SkMScalar tolerance) { | |
631 return | |
632 SkMScalarApproximatelyInteger(matrix.get(0, 0), tolerance) && | |
633 SkMScalarApproximatelyEqual(matrix.get(0, 1), 0, tolerance) && | |
634 SkMScalarApproximatelyEqual(matrix.get(0, 2), 0, tolerance) && | |
635 SkMScalarApproximatelyInteger(matrix.get(0, 3), tolerance) && | |
636 | |
637 SkMScalarApproximatelyEqual(matrix.get(1, 0), 0, tolerance) && | |
638 SkMScalarApproximatelyInteger(matrix.get(1, 1), tolerance) && | |
639 SkMScalarApproximatelyEqual(matrix.get(1, 2), 0, tolerance) && | |
640 SkMScalarApproximatelyInteger(matrix.get(1, 3), tolerance) && | |
641 | |
642 SkMScalarApproximatelyEqual(matrix.get(2, 0), 0, tolerance) && | |
643 SkMScalarApproximatelyEqual(matrix.get(2, 1), 0, tolerance) && | |
644 | |
645 SkMScalarApproximatelyEqual(matrix.get(3, 0), 0, tolerance) && | |
646 SkMScalarApproximatelyEqual(matrix.get(3, 1), 0, tolerance) && | |
647 SkMScalarApproximatelyEqual(matrix.get(3, 2), 0, tolerance) && | |
648 SkMScalarApproximatelyEqual(matrix.get(3, 3), 1, tolerance); | |
649 } | |
650 | |
596 } // namespace cc | 651 } // namespace cc |
OLD | NEW |