OLD | NEW |
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> |
11 | 11 |
12 #include "ui/gfx/point.h" | 12 #include "ui/gfx/point.h" |
13 #include "ui/gfx/point3_f.h" | 13 #include "ui/gfx/point3_f.h" |
14 #include "ui/gfx/vector3d_f.h" | 14 #include "ui/gfx/vector3d_f.h" |
15 #include "ui/gfx/rect.h" | 15 #include "ui/gfx/rect.h" |
16 #include "ui/gfx/safe_integer_conversions.h" | 16 #include "ui/gfx/safe_integer_conversions.h" |
17 #include "ui/gfx/skia_util.h" | 17 #include "ui/gfx/skia_util.h" |
18 #include "ui/gfx/transform_util.h" | 18 #include "ui/gfx/transform_util.h" |
19 | 19 |
20 namespace gfx { | 20 namespace gfx { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 #define MGET(m, row, col) SkMScalarToDouble(m.get(row, col)) | |
25 #define MSET(m, row, col, value) m.set(row, col, SkMScalarToDouble(value)) | |
26 | |
27 double TanDegrees(double degrees) { | 24 double TanDegrees(double degrees) { |
28 double radians = degrees * M_PI / 180; | 25 double radians = degrees * M_PI / 180; |
29 return std::tan(radians); | 26 return std::tan(radians); |
30 } | 27 } |
31 | 28 |
32 } // namespace | 29 } // namespace |
33 | 30 |
34 Transform::Transform() { | 31 Transform::Transform() { |
35 matrix_.reset(); | 32 matrix_.reset(); |
36 } | 33 } |
37 | 34 |
38 Transform::~Transform() {} | 35 Transform::~Transform() {} |
39 | 36 |
40 bool Transform::operator==(const Transform& rhs) const { | 37 bool Transform::operator==(const Transform& rhs) const { |
41 return matrix_ == rhs.matrix_; | 38 return matrix_ == rhs.matrix_; |
42 } | 39 } |
43 | 40 |
44 bool Transform::operator!=(const Transform& rhs) const { | 41 bool Transform::operator!=(const Transform& rhs) const { |
45 return !(*this == rhs); | 42 return !(*this == rhs); |
46 } | 43 } |
47 | 44 |
48 void Transform::SetRotate(double degree) { | 45 void Transform::MakeIdentity() { |
49 matrix_.setRotateDegreesAbout(SkDoubleToMScalar(0), | 46 matrix_.setIdentity(); |
50 SkDoubleToMScalar(0), | |
51 SkDoubleToMScalar(1), | |
52 SkDoubleToMScalar(degree)); | |
53 } | 47 } |
54 | 48 |
55 void Transform::SetRotateAbout(const Point3F& axis, double degree) { | 49 void Transform::Rotate(double degree) { |
56 matrix_.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), | 50 if (matrix_.isIdentity()) { |
57 SkDoubleToMScalar(axis.y()), | 51 matrix_.setRotateDegreesAbout(SkDoubleToMScalar(0), |
58 SkDoubleToMScalar(axis.z()), | 52 SkDoubleToMScalar(0), |
59 SkDoubleToMScalar(degree)); | 53 SkDoubleToMScalar(1), |
| 54 SkDoubleToMScalar(degree)); |
| 55 } else { |
| 56 SkMatrix44 rot; |
| 57 rot.setRotateDegreesAbout(SkDoubleToMScalar(0), |
| 58 SkDoubleToMScalar(0), |
| 59 SkDoubleToMScalar(1), |
| 60 SkDoubleToMScalar(degree)); |
| 61 matrix_.preConcat(rot); |
| 62 } |
60 } | 63 } |
61 | 64 |
62 void Transform::SetScaleX(double x) { | 65 void Transform::RotateAbout(const Vector3dF& axis, double degree) { |
63 MSET(matrix_, 0, 0, x); | 66 if (matrix_.isIdentity()) { |
| 67 matrix_.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), |
| 68 SkDoubleToMScalar(axis.y()), |
| 69 SkDoubleToMScalar(axis.z()), |
| 70 SkDoubleToMScalar(degree)); |
| 71 } else { |
| 72 SkMatrix44 rot; |
| 73 rot.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), |
| 74 SkDoubleToMScalar(axis.y()), |
| 75 SkDoubleToMScalar(axis.z()), |
| 76 SkDoubleToMScalar(degree)); |
| 77 matrix_.preConcat(rot); |
| 78 } |
64 } | 79 } |
65 | 80 |
66 void Transform::SetScaleY(double y) { | 81 void Transform::Scale(double x, double y) { |
67 MSET(matrix_, 1, 1, y); | 82 if (matrix_.isIdentity()) { |
| 83 matrix_.setScale(SkDoubleToMScalar(x), |
| 84 SkDoubleToMScalar(y), |
| 85 SkDoubleToMScalar(1)); |
| 86 } else { |
| 87 SkMatrix44 scale; |
| 88 scale.setScale(SkDoubleToMScalar(x), |
| 89 SkDoubleToMScalar(y), |
| 90 SkDoubleToMScalar(1)); |
| 91 matrix_.preConcat(scale); |
| 92 } |
68 } | 93 } |
69 | 94 |
70 void Transform::SetScaleZ(double z) { | 95 void Transform::Scale3d(double x, double y, double z) { |
71 MSET(matrix_, 2, 2, z); | 96 if (matrix_.isIdentity()) { |
| 97 matrix_.setScale(SkDoubleToMScalar(x), |
| 98 SkDoubleToMScalar(y), |
| 99 SkDoubleToMScalar(z)); |
| 100 } else { |
| 101 SkMatrix44 scale; |
| 102 scale.setScale(SkDoubleToMScalar(x), |
| 103 SkDoubleToMScalar(y), |
| 104 SkDoubleToMScalar(z)); |
| 105 matrix_.preConcat(scale); |
| 106 } |
72 } | 107 } |
73 | 108 |
74 void Transform::SetScale(double x, double y) { | 109 void Transform::Translate(double x, double y) { |
75 matrix_.setScale(SkDoubleToMScalar(x), | 110 if (matrix_.isIdentity()) { |
76 SkDoubleToMScalar(y), | 111 matrix_.setTranslate(SkDoubleToMScalar(x), |
77 matrix_.get(2, 2)); | 112 SkDoubleToMScalar(y), |
| 113 SkDoubleToMScalar(0)); |
| 114 } else { |
| 115 SkMatrix44 translate; |
| 116 translate.setTranslate(SkDoubleToMScalar(x), |
| 117 SkDoubleToMScalar(y), |
| 118 SkDoubleToMScalar(0)); |
| 119 matrix_.preConcat(translate); |
| 120 } |
78 } | 121 } |
79 | 122 |
80 void Transform::SetScale3d(double x, double y, double z) { | 123 void Transform::Translate3d(double x, double y, double z) { |
81 matrix_.setScale(SkDoubleToMScalar(x), | 124 if (matrix_.isIdentity()) { |
82 SkDoubleToMScalar(y), | 125 matrix_.setTranslate(SkDoubleToMScalar(x), |
83 SkDoubleToMScalar(z)); | 126 SkDoubleToMScalar(y), |
| 127 SkDoubleToMScalar(z)); |
| 128 } else { |
| 129 SkMatrix44 translate; |
| 130 translate.setTranslate(SkDoubleToMScalar(x), |
| 131 SkDoubleToMScalar(y), |
| 132 SkDoubleToMScalar(z)); |
| 133 matrix_.preConcat(translate); |
| 134 } |
84 } | 135 } |
85 | 136 |
86 void Transform::SetTranslateX(double x) { | 137 void Transform::SkewX(double angle_x) { |
87 MSET(matrix_, 0, 3, x); | 138 if (matrix_.isIdentity()) |
| 139 matrix_.setDouble(0, 1, TanDegrees(angle_x)); |
| 140 else { |
| 141 SkMatrix44 skew; |
| 142 skew.setDouble(0, 1, TanDegrees(angle_x)); |
| 143 matrix_.preConcat(skew); |
| 144 } |
88 } | 145 } |
89 | 146 |
90 void Transform::SetTranslateY(double y) { | 147 void Transform::SkewY(double angle_y) { |
91 MSET(matrix_, 1, 3, y); | 148 if (matrix_.isIdentity()) |
| 149 matrix_.setDouble(1, 0, TanDegrees(angle_y)); |
| 150 else { |
| 151 SkMatrix44 skew; |
| 152 skew.setDouble(1, 0, TanDegrees(angle_y)); |
| 153 matrix_.preConcat(skew); |
| 154 } |
92 } | 155 } |
93 | 156 |
94 void Transform::SetTranslateZ(double z) { | 157 void Transform::ApplyPerspectiveDepth(double depth) { |
95 MSET(matrix_, 2, 3, z); | 158 if (depth == 0) |
96 } | 159 return; |
97 | 160 if (matrix_.isIdentity()) |
98 void Transform::SetTranslate(double x, double y) { | 161 matrix_.setDouble(3, 2, -1.0 / depth); |
99 matrix_.setTranslate(SkDoubleToMScalar(x), | 162 else { |
100 SkDoubleToMScalar(y), | 163 SkMatrix44 m; |
101 matrix_.get(2, 3)); | 164 m.setDouble(3, 2, -1.0 / depth); |
102 } | 165 matrix_.preConcat(m); |
103 | 166 } |
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 MSET(matrix_, 0, 1, TanDegrees(angle)); | |
112 } | |
113 | |
114 void Transform::SetSkewY(double angle) { | |
115 MSET(matrix_, 1, 0, TanDegrees(angle)); | |
116 } | |
117 | |
118 void Transform::SetPerspectiveDepth(double depth) { | |
119 SkMatrix44 m; | |
120 MSET(m, 3, 2, -1.0 / depth); | |
121 matrix_ = m; | |
122 } | |
123 | |
124 void Transform::ConcatRotate(double degree) { | |
125 SkMatrix44 rot; | |
126 rot.setRotateDegreesAbout(SkDoubleToMScalar(0), | |
127 SkDoubleToMScalar(0), | |
128 SkDoubleToMScalar(1), | |
129 SkDoubleToMScalar(degree)); | |
130 matrix_.postConcat(rot); | |
131 } | |
132 | |
133 void Transform::ConcatRotateAbout(const Point3F& axis, double degree) { | |
134 SkMatrix44 rot; | |
135 rot.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), | |
136 SkDoubleToMScalar(axis.y()), | |
137 SkDoubleToMScalar(axis.z()), | |
138 SkDoubleToMScalar(degree)); | |
139 matrix_.postConcat(rot); | |
140 } | |
141 | |
142 void Transform::ConcatScale(double x, double y) { | |
143 SkMatrix44 scale; | |
144 scale.setScale(SkDoubleToMScalar(x), | |
145 SkDoubleToMScalar(y), | |
146 SkDoubleToMScalar(1)); | |
147 matrix_.postConcat(scale); | |
148 } | |
149 | |
150 void Transform::ConcatScale3d(double x, double y, double z) { | |
151 SkMatrix44 scale; | |
152 scale.setScale(SkDoubleToMScalar(x), | |
153 SkDoubleToMScalar(y), | |
154 SkDoubleToMScalar(z)); | |
155 matrix_.postConcat(scale); | |
156 } | |
157 | |
158 void Transform::ConcatTranslate(double x, double y) { | |
159 SkMatrix44 translate; | |
160 translate.setTranslate(SkDoubleToMScalar(x), | |
161 SkDoubleToMScalar(y), | |
162 SkDoubleToMScalar(0)); | |
163 matrix_.postConcat(translate); | |
164 } | |
165 | |
166 void Transform::ConcatTranslate3d(double x, double y, double z) { | |
167 SkMatrix44 translate; | |
168 translate.setTranslate(SkDoubleToMScalar(x), | |
169 SkDoubleToMScalar(y), | |
170 SkDoubleToMScalar(z)); | |
171 matrix_.postConcat(translate); | |
172 } | |
173 | |
174 void Transform::ConcatSkewX(double angle_x) { | |
175 Transform t; | |
176 t.SetSkewX(angle_x); | |
177 matrix_.postConcat(t.matrix_); | |
178 } | |
179 | |
180 void Transform::ConcatSkewY(double angle_y) { | |
181 Transform t; | |
182 t.SetSkewY(angle_y); | |
183 matrix_.postConcat(t.matrix_); | |
184 } | |
185 | |
186 void Transform::ConcatPerspectiveDepth(double depth) { | |
187 SkMatrix44 m; | |
188 MSET(m, 3, 2, -1.0 / depth); | |
189 matrix_.postConcat(m); | |
190 } | |
191 | |
192 void Transform::PreconcatRotate(double degree) { | |
193 SkMatrix44 rot; | |
194 rot.setRotateDegreesAbout(SkDoubleToMScalar(0), | |
195 SkDoubleToMScalar(0), | |
196 SkDoubleToMScalar(1), | |
197 SkDoubleToMScalar(degree)); | |
198 matrix_.preConcat(rot); | |
199 } | |
200 | |
201 void Transform::PreconcatRotateAbout(const Point3F& axis, double degree) { | |
202 SkMatrix44 rot; | |
203 rot.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), | |
204 SkDoubleToMScalar(axis.y()), | |
205 SkDoubleToMScalar(axis.z()), | |
206 SkDoubleToMScalar(degree)); | |
207 matrix_.preConcat(rot); | |
208 } | |
209 | |
210 void Transform::PreconcatScale(double x, double y) { | |
211 SkMatrix44 scale; | |
212 scale.setScale(SkDoubleToMScalar(x), | |
213 SkDoubleToMScalar(y), | |
214 SkDoubleToMScalar(1)); | |
215 matrix_.preConcat(scale); | |
216 } | |
217 | |
218 void Transform::PreconcatScale3d(double x, double y, double z) { | |
219 SkMatrix44 scale; | |
220 scale.setScale(SkDoubleToMScalar(x), | |
221 SkDoubleToMScalar(y), | |
222 SkDoubleToMScalar(z)); | |
223 matrix_.preConcat(scale); | |
224 } | |
225 | |
226 void Transform::PreconcatTranslate(double x, double y) { | |
227 SkMatrix44 translate; | |
228 translate.setTranslate(SkDoubleToMScalar(x), | |
229 SkDoubleToMScalar(y), | |
230 SkDoubleToMScalar(0)); | |
231 matrix_.preConcat(translate); | |
232 } | |
233 | |
234 void Transform::PreconcatTranslate3d(double x, double y, double z) { | |
235 SkMatrix44 translate; | |
236 translate.setTranslate(SkDoubleToMScalar(x), | |
237 SkDoubleToMScalar(y), | |
238 SkDoubleToMScalar(z)); | |
239 matrix_.preConcat(translate); | |
240 } | |
241 | |
242 void Transform::PreconcatSkewX(double angle_x) { | |
243 Transform t; | |
244 t.SetSkewX(angle_x); | |
245 matrix_.preConcat(t.matrix_); | |
246 } | |
247 | |
248 void Transform::PreconcatSkewY(double angle_y) { | |
249 Transform t; | |
250 t.SetSkewY(angle_y); | |
251 matrix_.preConcat(t.matrix_); | |
252 } | |
253 | |
254 void Transform::PreconcatPerspectiveDepth(double depth) { | |
255 SkMatrix44 m; | |
256 MSET(m, 3, 2, -1.0 / depth); | |
257 matrix_.preConcat(m); | |
258 } | 167 } |
259 | 168 |
260 void Transform::PreconcatTransform(const Transform& transform) { | 169 void Transform::PreconcatTransform(const Transform& transform) { |
261 if (!transform.matrix_.isIdentity()) { | 170 if (!transform.matrix_.isIdentity()) { |
262 matrix_.preConcat(transform.matrix_); | 171 matrix_.preConcat(transform.matrix_); |
263 } | 172 } |
264 } | 173 } |
265 | 174 |
266 void Transform::ConcatTransform(const Transform& transform) { | 175 void Transform::ConcatTransform(const Transform& transform) { |
267 if (!transform.matrix_.isIdentity()) { | 176 if (!transform.matrix_.isIdentity()) { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 !DecomposeTransform(&from_decomp, from)) | 247 !DecomposeTransform(&from_decomp, from)) |
339 return false; | 248 return false; |
340 | 249 |
341 if (!BlendDecomposedTransforms(&to_decomp, to_decomp, from_decomp, progress)) | 250 if (!BlendDecomposedTransforms(&to_decomp, to_decomp, from_decomp, progress)) |
342 return false; | 251 return false; |
343 | 252 |
344 matrix_ = ComposeTransform(to_decomp).matrix(); | 253 matrix_ = ComposeTransform(to_decomp).matrix(); |
345 return true; | 254 return true; |
346 } | 255 } |
347 | 256 |
| 257 Transform Transform::operator*(const Transform& other) const { |
| 258 Transform to_return; |
| 259 to_return.matrix_.setConcat(matrix_, other.matrix_); |
| 260 return to_return; |
| 261 } |
| 262 |
| 263 Transform& Transform::operator*=(const Transform& other) { |
| 264 matrix_.preConcat(other.matrix_); |
| 265 return *this; |
| 266 } |
| 267 |
348 void Transform::TransformPointInternal(const SkMatrix44& xform, | 268 void Transform::TransformPointInternal(const SkMatrix44& xform, |
349 Point3F& point) const { | 269 Point3F& point) const { |
350 SkMScalar p[4] = { | 270 SkMScalar p[4] = { |
351 SkDoubleToMScalar(point.x()), | 271 SkDoubleToMScalar(point.x()), |
352 SkDoubleToMScalar(point.y()), | 272 SkDoubleToMScalar(point.y()), |
353 SkDoubleToMScalar(point.z()), | 273 SkDoubleToMScalar(point.z()), |
354 SkDoubleToMScalar(1) | 274 SkDoubleToMScalar(1) |
355 }; | 275 }; |
356 | 276 |
357 xform.mapMScalars(p); | 277 xform.mapMScalars(p); |
(...skipping 13 matching lines...) Expand all Loading... |
371 SkDoubleToMScalar(0), | 291 SkDoubleToMScalar(0), |
372 SkDoubleToMScalar(1) | 292 SkDoubleToMScalar(1) |
373 }; | 293 }; |
374 | 294 |
375 xform.mapMScalars(p); | 295 xform.mapMScalars(p); |
376 | 296 |
377 point.SetPoint(ToRoundedInt(p[0]), ToRoundedInt(p[1])); | 297 point.SetPoint(ToRoundedInt(p[0]), ToRoundedInt(p[1])); |
378 } | 298 } |
379 | 299 |
380 } // namespace gfx | 300 } // namespace gfx |
OLD | NEW |