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

Unified Diff: ui/gfx/transform_util.cc

Issue 23444049: Implement transform snapping for gfx::Transforms. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Snap *ALL* the components of the 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 | « ui/gfx/transform_util.h ('k') | ui/gfx/transform_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/transform_util.cc
diff --git a/ui/gfx/transform_util.cc b/ui/gfx/transform_util.cc
index 916b0b0bf8562294eed85196f60c4653cfb9a2b2..177fa774c51eeb11658221d8e24403c276673ecd 100644
--- a/ui/gfx/transform_util.cc
+++ b/ui/gfx/transform_util.cc
@@ -7,8 +7,11 @@
#include <algorithm>
#include <cmath>
+#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "ui/gfx/point.h"
+#include "ui/gfx/point3_f.h"
+#include "ui/gfx/rect.h"
namespace gfx {
@@ -108,6 +111,162 @@ bool Normalize(SkMatrix44& m) {
return true;
}
+void BuildPerspectiveMatrix(SkMatrix44* matrix,
+ const DecomposedTransform& decomp) {
+ matrix->setIdentity();
+
+ for (int i = 0; i < 4; i++)
+ matrix->setDouble(3, i, decomp.perspective[i]);
+}
+
+void BuildTranslationMatrix(SkMatrix44 * matrix,
+ const DecomposedTransform& decomp) {
+ matrix->setTranslate(SkDoubleToMScalar(decomp.translate[0]),
danakj 2013/10/21 15:28:42 I find it a bit odd reading this that you have all
avallee 2013/10/21 15:55:55 Removed the pointers, makes it easier to read. Ad
+ SkDoubleToMScalar(decomp.translate[1]),
+ SkDoubleToMScalar(decomp.translate[2]));
+}
+
+SkMatrix44 BuildSnappedTranslationMatrix(DecomposedTransform decomp) {
+ SkMatrix44 translation_matrix(SkMatrix44::kUninitialized_Constructor);
+ decomp.translate[0] =
+ std::floor(decomp.translate[0] + SkDoubleToMScalar(0.5));
+ decomp.translate[1] =
+ std::floor(decomp.translate[1] + SkDoubleToMScalar(0.5));
+ decomp.translate[2] =
+ std::floor(decomp.translate[2] + SkDoubleToMScalar(0.5));
+ BuildTranslationMatrix(&translation_matrix, decomp);
+ return translation_matrix;
+}
+
+void BuildRotationMatrix(SkMatrix44* matrix,
+ const DecomposedTransform& decomp) {
+ double x = decomp.quaternion[0];
+ double y = decomp.quaternion[1];
+ double z = decomp.quaternion[2];
+ double w = decomp.quaternion[3];
+
+ matrix->set3x3(1.0 - 2.0 * (y * y + z * z),
+ 2.0 * (x * y + z * w),
+ 2.0 * (x * z - y * w),
+ 2.0 * (x * y - z * w),
+ 1.0 - 2.0 * (x * x + z * z),
+ 2.0 * (y * z + x * w),
+ 2.0 * (x * z + y * w),
+ 2.0 * (y * z - x * w),
+ 1.0 - 2.0 * (x * x + y * y));
+}
+
+SkMatrix44 BuildSnappedRotationMatrix(const DecomposedTransform& decomp) {
+ // Create snapped rotation.
+ SkMatrix44 rotation_matrix(SkMatrix44::kUninitialized_Constructor);
+ BuildRotationMatrix(&rotation_matrix, decomp);
+ for (int i = 0; i < 3; ++i) {
+ for (int j = 0; j < 3; ++j) {
+ SkMScalar value = rotation_matrix.get(i, j);
+ // Snap values to -1, 0 or 1.
+ if (value < -0.5f) {
+ value = -1.0f;
+ } else if (value > 0.5f) {
+ value = 1.0f;
+ } else {
+ value = 0.0f;
+ }
+ rotation_matrix.set(i, j, value);
+ }
+ }
+ return rotation_matrix;
+}
+
+void BuildSkewMatrix(SkMatrix44* matrix, const DecomposedTransform& decomp) {
+ matrix->setIdentity();
+
+ SkMatrix44 temp(SkMatrix44::kIdentity_Constructor);
+ if (decomp.skew[2]) {
+ temp.setDouble(1, 2, decomp.skew[2]);
+ matrix->preConcat(temp);
+ }
+
+ if (decomp.skew[1]) {
+ temp.setDouble(1, 2, 0);
+ temp.setDouble(0, 2, decomp.skew[1]);
+ matrix->preConcat(temp);
+ }
+
+ if (decomp.skew[0]) {
+ temp.setDouble(0, 2, 0);
+ temp.setDouble(0, 1, decomp.skew[0]);
+ matrix->preConcat(temp);
+ }
+}
+
+void BuildScaleMatrix(SkMatrix44* matrix, const DecomposedTransform& decomp) {
+ matrix->setIdentity();
+ matrix->setScale(SkDoubleToMScalar(decomp.scale[0]),
+ SkDoubleToMScalar(decomp.scale[1]),
+ SkDoubleToMScalar(decomp.scale[2]));
+}
+
+SkMatrix44 BuildSnappedScaleMatrix(DecomposedTransform decomp) {
+ SkMatrix44 scale_matrix(SkMatrix44::kUninitialized_Constructor);
+ decomp.scale[0] = std::floor(decomp.scale[0] + SkDoubleToMScalar(0.5));
+ decomp.scale[1] = std::floor(decomp.scale[1] + SkDoubleToMScalar(0.5));
+ decomp.scale[2] = std::floor(decomp.scale[2] + SkDoubleToMScalar(0.5));
+ BuildScaleMatrix(&scale_matrix, decomp);
+ return scale_matrix;
+}
+
+Transform ComposeTransform(const SkMatrix44& perspective,
+ const SkMatrix44& translation,
+ const SkMatrix44& rotation,
+ const SkMatrix44& skew,
+ const SkMatrix44& scale) {
+ SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor);
+
+ matrix.preConcat(perspective);
+ matrix.preConcat(translation);
+ matrix.preConcat(rotation);
+ matrix.preConcat(skew);
+ matrix.preConcat(scale);
+
+ Transform to_return;
+ to_return.matrix() = matrix;
+ return to_return;
+}
+
+bool CheckViewportPointMapsWithinAPixel(const Point& point,
danakj 2013/10/21 15:28:42 bikeshed: WithinOnePixel
avallee 2013/10/21 15:55:55 Yeah, was mostly shortening to make the call sits
+ const Transform& transform) {
danakj 2013/10/21 15:28:42 git cl format
+ Point3F point_original, point_transformed;
+ point_original = point_transformed = Point3F(point);
+
+ // Can't use TransformRect here since it would give us the axis-aligned
+ // bounding rect of the 4 points in the initial rectable which is not what we
+ // want.
+ transform.TransformPoint(&point_transformed);
+
+ if ((point_transformed - point_original).Length() > 1.f) {
+ // The changed distance should not be more than 1 pixel.
+ return false;
+ }
+ return true;
+}
+
+bool CheckTransformsMapsIntViewportWithinAPixel(const Rect& viewport,
+ const Transform& original,
+ const Transform& snapped) {
+
+ Transform original_inv(Transform::kSkipInitialization);
+ bool invertible = true;
+ invertible &= original.GetInverse(&original_inv);
+ DCHECK(invertible) << "Non-invertible transform, cannot snap.";
+
+ Transform combined = snapped * original_inv;
+
+ return CheckViewportPointMapsWithinAPixel(viewport.origin(), combined) &&
+ CheckViewportPointMapsWithinAPixel(viewport.top_right(), combined) &&
+ CheckViewportPointMapsWithinAPixel(viewport.bottom_left(), combined) &&
+ CheckViewportPointMapsWithinAPixel(viewport.bottom_right(), combined);
+}
+
} // namespace
Transform GetScaleTransform(const Point& anchor, float scale) {
@@ -270,54 +429,52 @@ bool DecomposeTransform(DecomposedTransform* decomp,
// Taken from http://www.w3.org/TR/css3-transforms/.
Transform ComposeTransform(const DecomposedTransform& decomp) {
- SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor);
- for (int i = 0; i < 4; i++)
- matrix.set(3, i, decomp.perspective[i]);
+ SkMatrix44 perspective(SkMatrix44::kUninitialized_Constructor);
+ BuildPerspectiveMatrix(&perspective, decomp);
- matrix.preTranslate(
- decomp.translate[0], decomp.translate[1], decomp.translate[2]);
+ SkMatrix44 translation(SkMatrix44::kUninitialized_Constructor);
+ BuildTranslationMatrix(&translation, decomp);
- SkMScalar x = decomp.quaternion[0];
- SkMScalar y = decomp.quaternion[1];
- SkMScalar z = decomp.quaternion[2];
- SkMScalar w = decomp.quaternion[3];
+ SkMatrix44 rotation(SkMatrix44::kUninitialized_Constructor);
+ BuildRotationMatrix(&rotation, decomp);
- SkMatrix44 rotation_matrix(SkMatrix44::kUninitialized_Constructor);
- rotation_matrix.set3x3(1.0 - 2.0 * (y * y + z * z),
- 2.0 * (x * y + z * w),
- 2.0 * (x * z - y * w),
- 2.0 * (x * y - z * w),
- 1.0 - 2.0 * (x * x + z * z),
- 2.0 * (y * z + x * w),
- 2.0 * (x * z + y * w),
- 2.0 * (y * z - x * w),
- 1.0 - 2.0 * (x * x + y * y));
-
- matrix.preConcat(rotation_matrix);
+ SkMatrix44 skew(SkMatrix44::kUninitialized_Constructor);
+ BuildSkewMatrix(&skew, decomp);
- SkMatrix44 temp(SkMatrix44::kIdentity_Constructor);
- if (decomp.skew[2]) {
- temp.set(1, 2, decomp.skew[2]);
- matrix.preConcat(temp);
- }
+ SkMatrix44 scale(SkMatrix44::kUninitialized_Constructor);
+ BuildScaleMatrix(&scale, decomp);
- if (decomp.skew[1]) {
- temp.set(1, 2, 0);
- temp.set(0, 2, decomp.skew[1]);
- matrix.preConcat(temp);
- }
+ return ComposeTransform(perspective, translation, rotation, skew, scale);
+}
- if (decomp.skew[0]) {
- temp.set(0, 2, 0);
- temp.set(0, 1, decomp.skew[0]);
- matrix.preConcat(temp);
- }
+bool SnapTransform(Transform* out,
+ const Transform& transform,
+ const Rect& viewport) {
+ DecomposedTransform decomp;
+ DecomposeTransform(&decomp, transform);
- matrix.preScale(decomp.scale[0], decomp.scale[1], decomp.scale[2]);
+ SkMatrix44 rotation_matrix = BuildSnappedRotationMatrix(decomp);
+ SkMatrix44 translation = BuildSnappedTranslationMatrix(decomp);
+ SkMatrix44 scale = BuildSnappedScaleMatrix(decomp);
- Transform to_return;
- to_return.matrix() = matrix;
- return to_return;
+ // Rebuild matrices for other unchanged components.
+ SkMatrix44 perspective(SkMatrix44::kUninitialized_Constructor);
+ BuildPerspectiveMatrix(&perspective, decomp);
+
+ // Completely ignore the skew.
+ SkMatrix44 skew(SkMatrix44::kIdentity_Constructor);
+
+ // Get full tranform
+ Transform snapped =
+ ComposeTransform(perspective, translation, rotation_matrix, skew, scale);
+
+ // Verify that viewport is not moved unnaturally.
+ bool snappable =
+ CheckTransformsMapsIntViewportWithinAPixel(viewport, transform, snapped);
+ if (snappable) {
+ *out = snapped;
+ }
+ return snappable;
}
std::string DecomposedTransform::ToString() const {
« no previous file with comments | « ui/gfx/transform_util.h ('k') | ui/gfx/transform_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698