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

Side by Side Diff: ui/gfx/transform.cc

Issue 11418040: gfx::Transform API clean-up (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years 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
« no previous file with comments | « ui/gfx/transform.h ('k') | ui/gfx/transform_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // MSVC++ requires this to be set before any other includes to get M_PI. 5 // MSVC++ requires this to be set before any other includes to get M_PI.
6 #define _USE_MATH_DEFINES 6 #define _USE_MATH_DEFINES
7 7
8 #include "ui/gfx/transform.h" 8 #include "ui/gfx/transform.h"
9 9
10 #include <cmath> 10 #include <cmath>
(...skipping 27 matching lines...) Expand all
38 Transform::~Transform() {} 38 Transform::~Transform() {}
39 39
40 bool Transform::operator==(const Transform& rhs) const { 40 bool Transform::operator==(const Transform& rhs) const {
41 return matrix_ == rhs.matrix_; 41 return matrix_ == rhs.matrix_;
42 } 42 }
43 43
44 bool Transform::operator!=(const Transform& rhs) const { 44 bool Transform::operator!=(const Transform& rhs) const {
45 return !(*this == rhs); 45 return !(*this == rhs);
46 } 46 }
47 47
48 void Transform::SetRotate(double degree) { 48 void Transform::MakeIdentity() {
49 matrix_.setRotateDegreesAbout(SkDoubleToMScalar(0), 49 matrix_.setIdentity();
50 SkDoubleToMScalar(0),
51 SkDoubleToMScalar(1),
52 SkDoubleToMScalar(degree));
53 } 50 }
54 51
55 void Transform::SetRotateAbout(const Point3F& axis, double degree) { 52 void Transform::Rotate(double degree) {
56 matrix_.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), 53 if (matrix_.isIdentity()) {
57 SkDoubleToMScalar(axis.y()), 54 matrix_.setRotateDegreesAbout(SkDoubleToMScalar(0),
58 SkDoubleToMScalar(axis.z()), 55 SkDoubleToMScalar(0),
59 SkDoubleToMScalar(degree)); 56 SkDoubleToMScalar(1),
57 SkDoubleToMScalar(degree));
58 } else {
59 SkMatrix44 rot;
60 rot.setRotateDegreesAbout(SkDoubleToMScalar(0),
61 SkDoubleToMScalar(0),
62 SkDoubleToMScalar(1),
63 SkDoubleToMScalar(degree));
64 matrix_.preConcat(rot);
65 }
60 } 66 }
61 67
62 void Transform::SetScaleX(double x) { 68 void Transform::RotateAbout(const Vector3dF& axis, double degree) {
63 matrix_.setDouble(0, 0, x); 69 if (matrix_.isIdentity()) {
70 matrix_.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()),
71 SkDoubleToMScalar(axis.y()),
72 SkDoubleToMScalar(axis.z()),
73 SkDoubleToMScalar(degree));
74 } else {
75 SkMatrix44 rot;
76 rot.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()),
77 SkDoubleToMScalar(axis.y()),
78 SkDoubleToMScalar(axis.z()),
79 SkDoubleToMScalar(degree));
80 matrix_.preConcat(rot);
81 }
64 } 82 }
65 83
66 void Transform::SetScaleY(double y) { 84 void Transform::Scale(double x, double y) {
67 matrix_.setDouble(1, 1, y); 85 if (matrix_.isIdentity()) {
86 matrix_.setScale(SkDoubleToMScalar(x),
87 SkDoubleToMScalar(y),
88 SkDoubleToMScalar(1));
89 } else {
90 SkMatrix44 scale;
91 scale.setScale(SkDoubleToMScalar(x),
92 SkDoubleToMScalar(y),
93 SkDoubleToMScalar(1));
94 matrix_.preConcat(scale);
95 }
68 } 96 }
69 97
70 void Transform::SetScaleZ(double z) { 98 void Transform::Scale3d(double x, double y, double z) {
71 matrix_.setDouble(2, 2, z); 99 if (matrix_.isIdentity()) {
100 matrix_.setScale(SkDoubleToMScalar(x),
101 SkDoubleToMScalar(y),
102 SkDoubleToMScalar(z));
103 } else {
104 SkMatrix44 scale;
105 scale.setScale(SkDoubleToMScalar(x),
106 SkDoubleToMScalar(y),
107 SkDoubleToMScalar(z));
108 matrix_.preConcat(scale);
109 }
72 } 110 }
73 111
74 void Transform::SetScale(double x, double y) { 112 void Transform::Translate(double x, double y) {
75 matrix_.setScale(SkDoubleToMScalar(x), 113 if (matrix_.isIdentity()) {
76 SkDoubleToMScalar(y), 114 matrix_.setTranslate(SkDoubleToMScalar(x),
77 matrix_.get(2, 2)); 115 SkDoubleToMScalar(y),
116 SkDoubleToMScalar(0));
117 } else {
118 SkMatrix44 translate;
119 translate.setTranslate(SkDoubleToMScalar(x),
120 SkDoubleToMScalar(y),
121 SkDoubleToMScalar(0));
122 matrix_.preConcat(translate);
123 }
78 } 124 }
79 125
80 void Transform::SetScale3d(double x, double y, double z) { 126 void Transform::Translate3d(double x, double y, double z) {
81 matrix_.setScale(SkDoubleToMScalar(x), 127 if (matrix_.isIdentity()) {
82 SkDoubleToMScalar(y), 128 matrix_.setTranslate(SkDoubleToMScalar(x),
83 SkDoubleToMScalar(z)); 129 SkDoubleToMScalar(y),
130 SkDoubleToMScalar(z));
131 } else {
132 SkMatrix44 translate;
133 translate.setTranslate(SkDoubleToMScalar(x),
134 SkDoubleToMScalar(y),
135 SkDoubleToMScalar(z));
136 matrix_.preConcat(translate);
137 }
84 } 138 }
85 139
86 void Transform::SetTranslateX(double x) { 140 void Transform::SkewX(double angle_x) {
87 matrix_.setDouble(0, 3, x); 141 if (matrix_.isIdentity())
142 matrix_.setDouble(0, 1, TanDegrees(angle_x));
143 else {
144 SkMatrix44 skew;
145 skew.setDouble(0, 1, TanDegrees(angle_x));
146 matrix_.preConcat(skew);
147 }
88 } 148 }
89 149
90 void Transform::SetTranslateY(double y) { 150 void Transform::SkewY(double angle_y) {
91 matrix_.setDouble(1, 3, y); 151 if (matrix_.isIdentity())
152 matrix_.setDouble(1, 0, TanDegrees(angle_y));
153 else {
154 SkMatrix44 skew;
155 skew.setDouble(1, 0, TanDegrees(angle_y));
156 matrix_.preConcat(skew);
157 }
92 } 158 }
93 159
94 void Transform::SetTranslateZ(double z) { 160 void Transform::ApplyPerspectiveDepth(double depth) {
95 matrix_.setDouble(2, 3, z);
96 }
97
98 void Transform::SetTranslate(double x, double y) {
99 matrix_.setTranslate(SkDoubleToMScalar(x),
100 SkDoubleToMScalar(y),
101 matrix_.get(2, 3));
102 }
103
104 void Transform::SetTranslate3d(double x, double y, double z) {
105 matrix_.setTranslate(SkDoubleToMScalar(x),
106 SkDoubleToMScalar(y),
107 SkDoubleToMScalar(z));
108 }
109
110 void Transform::SetSkewX(double angle) {
111 matrix_.setDouble(0, 1, TanDegrees(angle));
112 }
113
114 void Transform::SetSkewY(double angle) {
115 matrix_.setDouble(1, 0, TanDegrees(angle));
116 }
117
118 void Transform::SetPerspectiveDepth(double depth) {
119 if (depth == 0) 161 if (depth == 0)
120 return; 162 return;
121 163 if (matrix_.isIdentity())
122 SkMatrix44 m; 164 matrix_.setDouble(3, 2, -1.0 / depth);
123 m.setDouble(3, 2, -1.0 / depth); 165 else {
124 matrix_ = m; 166 SkMatrix44 m;
125 } 167 m.setDouble(3, 2, -1.0 / depth);
126 168 matrix_.preConcat(m);
127 void Transform::ConcatRotate(double degree) { 169 }
128 SkMatrix44 rot;
129 rot.setRotateDegreesAbout(SkDoubleToMScalar(0),
130 SkDoubleToMScalar(0),
131 SkDoubleToMScalar(1),
132 SkDoubleToMScalar(degree));
133 matrix_.postConcat(rot);
134 }
135
136 void Transform::ConcatRotateAbout(const Point3F& axis, double degree) {
137 SkMatrix44 rot;
138 rot.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()),
139 SkDoubleToMScalar(axis.y()),
140 SkDoubleToMScalar(axis.z()),
141 SkDoubleToMScalar(degree));
142 matrix_.postConcat(rot);
143 }
144
145 void Transform::ConcatScale(double x, double y) {
146 SkMatrix44 scale;
147 scale.setScale(SkDoubleToMScalar(x),
148 SkDoubleToMScalar(y),
149 SkDoubleToMScalar(1));
150 matrix_.postConcat(scale);
151 }
152
153 void Transform::ConcatScale3d(double x, double y, double z) {
154 SkMatrix44 scale;
155 scale.setScale(SkDoubleToMScalar(x),
156 SkDoubleToMScalar(y),
157 SkDoubleToMScalar(z));
158 matrix_.postConcat(scale);
159 }
160
161 void Transform::ConcatTranslate(double x, double y) {
162 SkMatrix44 translate;
163 translate.setTranslate(SkDoubleToMScalar(x),
164 SkDoubleToMScalar(y),
165 SkDoubleToMScalar(0));
166 matrix_.postConcat(translate);
167 }
168
169 void Transform::ConcatTranslate3d(double x, double y, double z) {
170 SkMatrix44 translate;
171 translate.setTranslate(SkDoubleToMScalar(x),
172 SkDoubleToMScalar(y),
173 SkDoubleToMScalar(z));
174 matrix_.postConcat(translate);
175 }
176
177 void Transform::ConcatSkewX(double angle_x) {
178 Transform t;
179 t.SetSkewX(angle_x);
180 matrix_.postConcat(t.matrix_);
181 }
182
183 void Transform::ConcatSkewY(double angle_y) {
184 Transform t;
185 t.SetSkewY(angle_y);
186 matrix_.postConcat(t.matrix_);
187 }
188
189 void Transform::ConcatPerspectiveDepth(double depth) {
190 if (depth == 0)
191 return;
192
193 SkMatrix44 m;
194 m.setDouble(3, 2, -1.0 / depth);
195 matrix_.postConcat(m);
196 }
197
198 void Transform::PreconcatRotate(double degree) {
199 SkMatrix44 rot;
200 rot.setRotateDegreesAbout(SkDoubleToMScalar(0),
201 SkDoubleToMScalar(0),
202 SkDoubleToMScalar(1),
203 SkDoubleToMScalar(degree));
204 matrix_.preConcat(rot);
205 }
206
207 void Transform::PreconcatRotateAbout(const Point3F& axis, double degree) {
208 SkMatrix44 rot;
209 rot.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()),
210 SkDoubleToMScalar(axis.y()),
211 SkDoubleToMScalar(axis.z()),
212 SkDoubleToMScalar(degree));
213 matrix_.preConcat(rot);
214 }
215
216 void Transform::PreconcatScale(double x, double y) {
217 SkMatrix44 scale;
218 scale.setScale(SkDoubleToMScalar(x),
219 SkDoubleToMScalar(y),
220 SkDoubleToMScalar(1));
221 matrix_.preConcat(scale);
222 }
223
224 void Transform::PreconcatScale3d(double x, double y, double z) {
225 SkMatrix44 scale;
226 scale.setScale(SkDoubleToMScalar(x),
227 SkDoubleToMScalar(y),
228 SkDoubleToMScalar(z));
229 matrix_.preConcat(scale);
230 }
231
232 void Transform::PreconcatTranslate(double x, double y) {
233 SkMatrix44 translate;
234 translate.setTranslate(SkDoubleToMScalar(x),
235 SkDoubleToMScalar(y),
236 SkDoubleToMScalar(0));
237 matrix_.preConcat(translate);
238 }
239
240 void Transform::PreconcatTranslate3d(double x, double y, double z) {
241 SkMatrix44 translate;
242 translate.setTranslate(SkDoubleToMScalar(x),
243 SkDoubleToMScalar(y),
244 SkDoubleToMScalar(z));
245 matrix_.preConcat(translate);
246 }
247
248 void Transform::PreconcatSkewX(double angle_x) {
249 Transform t;
250 t.SetSkewX(angle_x);
251 matrix_.preConcat(t.matrix_);
252 }
253
254 void Transform::PreconcatSkewY(double angle_y) {
255 Transform t;
256 t.SetSkewY(angle_y);
257 matrix_.preConcat(t.matrix_);
258 }
259
260 void Transform::PreconcatPerspectiveDepth(double depth) {
261 if (depth == 0)
262 return;
263
264 SkMatrix44 m;
265 m.setDouble(3, 2, -1.0 / depth);
266 matrix_.preConcat(m);
267 } 170 }
268 171
269 void Transform::PreconcatTransform(const Transform& transform) { 172 void Transform::PreconcatTransform(const Transform& transform) {
270 if (!transform.matrix_.isIdentity()) { 173 if (!transform.matrix_.isIdentity()) {
271 matrix_.preConcat(transform.matrix_); 174 matrix_.preConcat(transform.matrix_);
272 } 175 }
273 } 176 }
274 177
275 void Transform::ConcatTransform(const Transform& transform) { 178 void Transform::ConcatTransform(const Transform& transform) {
276 if (!transform.matrix_.isIdentity()) { 179 if (!transform.matrix_.isIdentity()) {
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 !DecomposeTransform(&from_decomp, from)) 258 !DecomposeTransform(&from_decomp, from))
356 return false; 259 return false;
357 260
358 if (!BlendDecomposedTransforms(&to_decomp, to_decomp, from_decomp, progress)) 261 if (!BlendDecomposedTransforms(&to_decomp, to_decomp, from_decomp, progress))
359 return false; 262 return false;
360 263
361 matrix_ = ComposeTransform(to_decomp).matrix(); 264 matrix_ = ComposeTransform(to_decomp).matrix();
362 return true; 265 return true;
363 } 266 }
364 267
268 Transform Transform::operator*(const Transform& other) const {
269 Transform to_return;
270 to_return.matrix_.setConcat(matrix_, other.matrix_);
271 return to_return;
272 }
273
274 Transform& Transform::operator*=(const Transform& other) {
275 matrix_.preConcat(other.matrix_);
276 return *this;
277 }
278
365 void Transform::TransformPointInternal(const SkMatrix44& xform, 279 void Transform::TransformPointInternal(const SkMatrix44& xform,
366 Point3F& point) const { 280 Point3F& point) const {
367 SkMScalar p[4] = { 281 SkMScalar p[4] = {
368 SkDoubleToMScalar(point.x()), 282 SkDoubleToMScalar(point.x()),
369 SkDoubleToMScalar(point.y()), 283 SkDoubleToMScalar(point.y()),
370 SkDoubleToMScalar(point.z()), 284 SkDoubleToMScalar(point.z()),
371 SkDoubleToMScalar(1) 285 SkDoubleToMScalar(1)
372 }; 286 };
373 287
374 xform.mapMScalars(p); 288 xform.mapMScalars(p);
(...skipping 13 matching lines...) Expand all
388 SkDoubleToMScalar(0), 302 SkDoubleToMScalar(0),
389 SkDoubleToMScalar(1) 303 SkDoubleToMScalar(1)
390 }; 304 };
391 305
392 xform.mapMScalars(p); 306 xform.mapMScalars(p);
393 307
394 point.SetPoint(ToRoundedInt(p[0]), ToRoundedInt(p[1])); 308 point.SetPoint(ToRoundedInt(p[0]), ToRoundedInt(p[1]));
395 } 309 }
396 310
397 } // namespace gfx 311 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/transform.h ('k') | ui/gfx/transform_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698