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

Unified Diff: ui/gfx/transform.h

Issue 7044062: Use SkMatrix44 for the underlying implementation of ui::Transform (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Added 2D unittests Created 9 years, 6 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
« ui/gfx/point3.h ('K') | « ui/gfx/point3.h ('k') | ui/gfx/transform.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/transform.h
diff --git a/ui/gfx/transform.h b/ui/gfx/transform.h
index 3fdfc3235a36c79eab4508d7481e9c05e8a83278..3e8ba8e85c0c77b47f180487ae92e4ff94b6b7c4 100644
--- a/ui/gfx/transform.h
+++ b/ui/gfx/transform.h
@@ -6,20 +6,24 @@
#define UI_GFX_TRANSFORM_H_
#pragma once
+#include "base/template_util.h"
#include "base/basictypes.h"
#include "base/compiler_specific.h"
-#include "third_party/skia/include/core/SkMatrix.h"
+#include "third_party/skia/include/utils/SkMatrix44.h"
+#include "ui/gfx/point.h"
+
+#include <cmath>
namespace gfx {
-class Point;
class Rect;
+template <typename T> class Point3;
+typedef Point3<float> Point3f;
}
namespace ui {
-// 3x3 transformation matrix. Transform is cheap and explicitly allows
+// 4x4 transformation matrix. Transform is cheap and explicitly allows
// copy/assign.
-// TODO: make this a 4x4.
class Transform {
public:
Transform();
@@ -57,43 +61,109 @@ class Transform {
void ConcatTranslate(float x, float y);
// Applies a transformation on the current transformation
- // (i.e. 'this = this * transform;'). Returns true if the result can be
- // represented.
- bool PreconcatTransform(const Transform& transform);
+ // (i.e. 'this = this * transform;').
+ void PreconcatTransform(const Transform& transform);
// Applies a transformation on the current transformation
- // (i.e. 'this = transform * this;'). Returns true if the result can be
- // represented.
- bool ConcatTransform(const Transform& transform);
+ // (i.e. 'this = transform * this;').
+ void ConcatTransform(const Transform& transform);
// Does the transformation change anything?
bool HasChange() const;
// Applies the transformation on the point. Returns true if the point is
// transformed successfully.
- bool TransformPoint(gfx::Point* point);
+ template <class T>
+ bool TransformPoint(T& point) const;
- // Applies the reverse transformation on the point. Returns true if the point
- // is transformed successfully.
- bool TransformPointReverse(gfx::Point* point);
+ // Applies the reverse transformation on the point. Returns true if the
+ // transformation can be inverted
+ template <class T>
+ bool TransformPointReverse(T& point) const;
// Applies transformation on the rectangle. Returns true if the rectangle is
// transformed successfully.
- bool TransformRect(gfx::Rect* rect);
+ bool TransformRect(gfx::Rect* rect) const;
// Applies the reverse transformation on the rectangle. Returns true if the
// rectangle is transformed successfully.
- bool TransformRectReverse(gfx::Rect* rect);
+ bool TransformRectReverse(gfx::Rect* rect) const;
// Returns the underlying matrix.
- const SkMatrix& matrix() const { return matrix_; }
+ const SkMatrix44& matrix() const { return matrix_; }
private:
- SkMatrix matrix_;
+ template <class T>
+ void TransformPointInternal(const SkMatrix44& xform, T& point) const;
+
+ template <typename T>
+ static int SymmetricRound(const T& value);
+
+ SkMatrix44 matrix_;
// copy/assign are allowed.
};
-} // namespace ui
+template <class T>
+bool Transform::TransformPoint(T& point) const {
+ TransformPointInternal(matrix_, point);
+ return true;
+}
+
+template <class T>
+bool Transform::TransformPointReverse(T& point) const {
+ // TODO(sad): Try to avoid trying to invert the matrix.
+ SkMatrix44 inverse;
+ if (!matrix_.invert(&inverse))
+ return false;
+
+ TransformPointInternal(inverse, point);
+ return true;
+}
+
+template <class T>
+void Transform::TransformPointInternal(const SkMatrix44& xform,
+ T& point) const {
+ SkScalar p[4] = {
+ point.x(),
+ point.y(),
+ point.z(),
+ 1 };
+
+ xform.map(p);
+
+ if (p[3] != 1 && abs(p[3]) > 0) {
+ point.SetPoint(p[0]/p[3], p[1]/p[3], p[2]/p[3]);
+ } else {
+ point.SetPoint(p[0], p[1], p[2]);
+ }
+}
+
+template <>
+inline void Transform::TransformPointInternal<gfx::Point>(
+ const SkMatrix44& xform,
+ gfx::Point& point) const {
+
+ SkScalar p[4] = {
+ SkIntToScalar(point.x()),
+ SkIntToScalar(point.y()),
+ 0,
+ 1 };
+
+ xform.map(p);
+
+ point.SetPoint(SymmetricRound(p[0]),
+ SymmetricRound(p[1]));
+}
+
+template <typename T>
+int Transform::SymmetricRound(const T& x) {
+ return static_cast<int>(
+ x > 0.0f
+ ? std::floor(x + 0.5f)
+ : std::ceil(x - 0.5f));
+}
+
+}// namespace ui
#endif // UI_GFX_TRANSFORM_H_
« ui/gfx/point3.h ('K') | « ui/gfx/point3.h ('k') | ui/gfx/transform.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698