Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3285)

Unified Diff: cc/base/math_util.cc

Issue 27223008: Provide approximate type functions for SkMatrix44. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rename functions taking gfx::Transform. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « cc/base/math_util.h ('k') | cc/base/math_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « cc/base/math_util.h ('k') | cc/base/math_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698