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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #ifndef UI_GFX_TRANSFORM_H_ 5 #ifndef UI_GFX_TRANSFORM_H_
6 #define UI_GFX_TRANSFORM_H_ 6 #define UI_GFX_TRANSFORM_H_
7 #pragma once 7 #pragma once
8 8
9 #include "base/template_util.h"
9 #include "base/basictypes.h" 10 #include "base/basictypes.h"
10 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
11 #include "third_party/skia/include/core/SkMatrix.h" 12 #include "third_party/skia/include/utils/SkMatrix44.h"
13 #include "ui/gfx/point.h"
14
15 #include <cmath>
12 16
13 namespace gfx { 17 namespace gfx {
14 class Point;
15 class Rect; 18 class Rect;
19 template <typename T> class Point3;
20 typedef Point3<float> Point3f;
16 } 21 }
17 22
18 namespace ui { 23 namespace ui {
19 24
20 // 3x3 transformation matrix. Transform is cheap and explicitly allows 25 // 4x4 transformation matrix. Transform is cheap and explicitly allows
21 // copy/assign. 26 // copy/assign.
22 // TODO: make this a 4x4.
23 class Transform { 27 class Transform {
24 public: 28 public:
25 Transform(); 29 Transform();
26 ~Transform(); 30 ~Transform();
27 31
28 // NOTE: The 'Set' functions overwrite the previously set transformation 32 // NOTE: The 'Set' functions overwrite the previously set transformation
29 // parameters. The 'Concat' functions apply a transformation (e.g. rotation, 33 // parameters. The 'Concat' functions apply a transformation (e.g. rotation,
30 // scale, translate) on top of the existing transforms, instead of overwriting 34 // scale, translate) on top of the existing transforms, instead of overwriting
31 // them. 35 // them.
32 36
(...skipping 17 matching lines...) Expand all
50 // Applies rotation on the current transformation. 54 // Applies rotation on the current transformation.
51 void ConcatRotate(float degree); 55 void ConcatRotate(float degree);
52 56
53 // Applies scaling on current transform. 57 // Applies scaling on current transform.
54 void ConcatScale(float x, float y); 58 void ConcatScale(float x, float y);
55 59
56 // Applies translation on current transform. 60 // Applies translation on current transform.
57 void ConcatTranslate(float x, float y); 61 void ConcatTranslate(float x, float y);
58 62
59 // Applies a transformation on the current transformation 63 // Applies a transformation on the current transformation
60 // (i.e. 'this = this * transform;'). Returns true if the result can be 64 // (i.e. 'this = this * transform;').
61 // represented. 65 void PreconcatTransform(const Transform& transform);
62 bool PreconcatTransform(const Transform& transform);
63 66
64 // Applies a transformation on the current transformation 67 // Applies a transformation on the current transformation
65 // (i.e. 'this = transform * this;'). Returns true if the result can be 68 // (i.e. 'this = transform * this;').
66 // represented. 69 void ConcatTransform(const Transform& transform);
67 bool ConcatTransform(const Transform& transform);
68 70
69 // Does the transformation change anything? 71 // Does the transformation change anything?
70 bool HasChange() const; 72 bool HasChange() const;
71 73
72 // Applies the transformation on the point. Returns true if the point is 74 // Applies the transformation on the point. Returns true if the point is
73 // transformed successfully. 75 // transformed successfully.
74 bool TransformPoint(gfx::Point* point); 76 template <class T>
77 bool TransformPoint(T& point) const;
75 78
76 // Applies the reverse transformation on the point. Returns true if the point 79 // Applies the reverse transformation on the point. Returns true if the
77 // is transformed successfully. 80 // transformation can be inverted
78 bool TransformPointReverse(gfx::Point* point); 81 template <class T>
82 bool TransformPointReverse(T& point) const;
79 83
80 // Applies transformation on the rectangle. Returns true if the rectangle is 84 // Applies transformation on the rectangle. Returns true if the rectangle is
81 // transformed successfully. 85 // transformed successfully.
82 bool TransformRect(gfx::Rect* rect); 86 bool TransformRect(gfx::Rect* rect) const;
83 87
84 // Applies the reverse transformation on the rectangle. Returns true if the 88 // Applies the reverse transformation on the rectangle. Returns true if the
85 // rectangle is transformed successfully. 89 // rectangle is transformed successfully.
86 bool TransformRectReverse(gfx::Rect* rect); 90 bool TransformRectReverse(gfx::Rect* rect) const;
87 91
88 // Returns the underlying matrix. 92 // Returns the underlying matrix.
89 const SkMatrix& matrix() const { return matrix_; } 93 const SkMatrix44& matrix() const { return matrix_; }
90 94
91 private: 95 private:
92 SkMatrix matrix_; 96 template <class T>
97 void TransformPointInternal(const SkMatrix44& xform, T& point) const;
98
99 template <typename T>
100 static int SymmetricRound(const T& value);
101
102 SkMatrix44 matrix_;
93 103
94 // copy/assign are allowed. 104 // copy/assign are allowed.
95 }; 105 };
96 106
97 } // namespace ui 107 template <class T>
108 bool Transform::TransformPoint(T& point) const {
109 TransformPointInternal(matrix_, point);
110 return true;
111 }
112
113 template <class T>
114 bool Transform::TransformPointReverse(T& point) const {
115 // TODO(sad): Try to avoid trying to invert the matrix.
116 SkMatrix44 inverse;
117 if (!matrix_.invert(&inverse))
118 return false;
119
120 TransformPointInternal(inverse, point);
121 return true;
122 }
123
124 template <class T>
125 void Transform::TransformPointInternal(const SkMatrix44& xform,
126 T& point) const {
127 SkScalar p[4] = {
128 point.x(),
129 point.y(),
130 point.z(),
131 1 };
132
133 xform.map(p);
134
135 if (p[3] != 1 && abs(p[3]) > 0) {
136 point.SetPoint(p[0]/p[3], p[1]/p[3], p[2]/p[3]);
137 } else {
138 point.SetPoint(p[0], p[1], p[2]);
139 }
140 }
141
142 template <>
143 inline void Transform::TransformPointInternal<gfx::Point>(
144 const SkMatrix44& xform,
145 gfx::Point& point) const {
146
147 SkScalar p[4] = {
148 SkIntToScalar(point.x()),
149 SkIntToScalar(point.y()),
150 0,
151 1 };
152
153 xform.map(p);
154
155 point.SetPoint(SymmetricRound(p[0]),
156 SymmetricRound(p[1]));
157 }
158
159 template <typename T>
160 int Transform::SymmetricRound(const T& x) {
161 return static_cast<int>(
162 x > 0.0f
163 ? std::floor(x + 0.5f)
164 : std::ceil(x - 0.5f));
165 }
166
167 }// namespace ui
98 168
99 #endif // UI_GFX_TRANSFORM_H_ 169 #endif // UI_GFX_TRANSFORM_H_
OLDNEW
« 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