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

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: 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
Index: cc/base/math_util.cc
diff --git a/cc/base/math_util.cc b/cc/base/math_util.cc
index eeb1523db46a080aff9850caaf126c1c3f8b0509..ee7b3cafecdfd8472b284a75d1e14792910b43b3 100644
--- a/cc/base/math_util.cc
+++ b/cc/base/math_util.cc
@@ -17,6 +17,21 @@
#include "ui/gfx/vector2d_f.h"
namespace cc {
+namespace {
+
+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.
+ SkMScalar tolerance) {
+ DCHECK_GE(tolerance, 0);
+ 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.
+}
+
+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.
+ SkMScalar tolerance) {
+ 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
+ return SkMScalarApproximatelyEqual(x, nearestInteger, tolerance);
+}
+
+} // namespace
const double MathUtil::kPiDouble = 3.14159265358979323846;
const float MathUtil::kPiFloat = 3.14159265358979323846f;
@@ -593,4 +608,44 @@ scoped_ptr<base::Value> MathUtil::AsValueSafely(float value) {
std::min(value, std::numeric_limits<float>::max())));
}
+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
+ SkMScalar tolerance) {
+ return
+ 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
+ SkMScalarApproximatelyEqual(matrix.get(1, 0), 0, tolerance) &&
+ SkMScalarApproximatelyEqual(matrix.get(2, 0), 0, tolerance) &&
+ matrix.get(3, 0) == 0 &&
+ SkMScalarApproximatelyEqual(matrix.get(0, 1), 0, tolerance) &&
+ SkMScalarApproximatelyEqual(matrix.get(1, 1), 1, tolerance) &&
+ SkMScalarApproximatelyEqual(matrix.get(2, 1), 0, tolerance) &&
+ matrix.get(3, 1) == 0 &&
+ SkMScalarApproximatelyEqual(matrix.get(0, 2), 0, tolerance) &&
+ SkMScalarApproximatelyEqual(matrix.get(1, 2), 0, tolerance) &&
+ SkMScalarApproximatelyEqual(matrix.get(2, 2), 1, tolerance) &&
+ matrix.get(3, 2) == 0 &&
+ matrix.get(3, 3) == 1;
+}
+
+bool MathUtil::IsMatrixApproximatelyIntegerTransform(const SkMatrix44& matrix,
+ SkMScalar tolerance) {
+ return
+ SkMScalarApproximatelyInteger(matrix.get(0, 0), tolerance) &&
+ SkMScalarApproximatelyEqual(matrix.get(0, 1), 0, tolerance) &&
+ SkMScalarApproximatelyEqual(matrix.get(0, 2), 0, tolerance) &&
+ SkMScalarApproximatelyInteger(matrix.get(0, 3), tolerance) &&
+
+ SkMScalarApproximatelyEqual(matrix.get(1, 0), 0, tolerance) &&
+ SkMScalarApproximatelyInteger(matrix.get(1, 1), tolerance) &&
+ SkMScalarApproximatelyEqual(matrix.get(1, 2), 0, tolerance) &&
+ SkMScalarApproximatelyInteger(matrix.get(1, 3), tolerance) &&
+
+ SkMScalarApproximatelyEqual(matrix.get(2, 0), 0, tolerance) &&
+ SkMScalarApproximatelyEqual(matrix.get(2, 1), 0, tolerance) &&
+
+ SkMScalarApproximatelyEqual(matrix.get(3, 0), 0, tolerance) &&
+ SkMScalarApproximatelyEqual(matrix.get(3, 1), 0, tolerance) &&
+ SkMScalarApproximatelyEqual(matrix.get(3, 2), 0, tolerance) &&
+ SkMScalarApproximatelyEqual(matrix.get(3, 3), 1, tolerance);
+}
+
} // namespace cc
« cc/base/math_util.h ('K') | « 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