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 #include "ui/gfx/transform_util.h" | 5 #include "ui/gfx/transform_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/logging.h" | |
10 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
11 #include "ui/gfx/point.h" | 12 #include "ui/gfx/point.h" |
13 #include "ui/gfx/point3_f.h" | |
14 #include "ui/gfx/rect.h" | |
12 | 15 |
13 namespace gfx { | 16 namespace gfx { |
14 | 17 |
15 namespace { | 18 namespace { |
16 | 19 |
17 SkMScalar Length3(SkMScalar v[3]) { | 20 SkMScalar Length3(SkMScalar v[3]) { |
18 double vd[3] = {SkMScalarToDouble(v[0]), SkMScalarToDouble(v[1]), | 21 double vd[3] = {SkMScalarToDouble(v[0]), SkMScalarToDouble(v[1]), |
19 SkMScalarToDouble(v[2])}; | 22 SkMScalarToDouble(v[2])}; |
20 return SkDoubleToMScalar( | 23 return SkDoubleToMScalar( |
21 std::sqrt(vd[0] * vd[0] + vd[1] * vd[1] + vd[2] * vd[2])); | 24 std::sqrt(vd[0] * vd[0] + vd[1] * vd[1] + vd[2] * vd[2])); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 return false; | 104 return false; |
102 | 105 |
103 SkMScalar scale = 1.0 / m.get(3, 3); | 106 SkMScalar scale = 1.0 / m.get(3, 3); |
104 for (int i = 0; i < 4; i++) | 107 for (int i = 0; i < 4; i++) |
105 for (int j = 0; j < 4; j++) | 108 for (int j = 0; j < 4; j++) |
106 m.set(i, j, m.get(i, j) * scale); | 109 m.set(i, j, m.get(i, j) * scale); |
107 | 110 |
108 return true; | 111 return true; |
109 } | 112 } |
110 | 113 |
114 SkMatrix44 BuildPerspectiveMatrix(const DecomposedTransform& decomp) { | |
115 SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor); | |
116 | |
117 for (int i = 0; i < 4; i++) | |
118 matrix.setDouble(3, i, decomp.perspective[i]); | |
119 return matrix; | |
120 } | |
121 | |
122 SkMatrix44 BuildTranslationMatrix(const DecomposedTransform& decomp) { | |
123 SkMatrix44 matrix(SkMatrix44::kUninitialized_Constructor); | |
124 // Implicitly calls matrix.setIdentity() | |
125 matrix.setTranslate(SkDoubleToMScalar(decomp.translate[0]), | |
126 SkDoubleToMScalar(decomp.translate[1]), | |
127 SkDoubleToMScalar(decomp.translate[2])); | |
128 return matrix; | |
129 } | |
130 | |
131 SkMatrix44 BuildSnappedTranslationMatrix(DecomposedTransform decomp) { | |
132 decomp.translate[0] = | |
133 std::floor(decomp.translate[0] + SkDoubleToMScalar(0.5)); | |
Ian Vollick
2013/10/23 19:03:13
nit: You might want to take a look at safe_integer
avallee
2013/10/23 19:44:15
I don't need ints here though, only the rounding b
danakj
2013/10/23 19:49:20
When I read this I thought you were saying 1.5 rou
| |
134 decomp.translate[1] = | |
135 std::floor(decomp.translate[1] + SkDoubleToMScalar(0.5)); | |
136 decomp.translate[2] = | |
137 std::floor(decomp.translate[2] + SkDoubleToMScalar(0.5)); | |
138 return BuildTranslationMatrix(decomp); | |
139 } | |
140 | |
141 SkMatrix44 BuildRotationMatrix(const DecomposedTransform& decomp) { | |
142 double x = decomp.quaternion[0]; | |
143 double y = decomp.quaternion[1]; | |
144 double z = decomp.quaternion[2]; | |
145 double w = decomp.quaternion[3]; | |
146 | |
147 SkMatrix44 matrix(SkMatrix44::kUninitialized_Constructor); | |
148 | |
149 // Implicitly calls matrix.setIdentity() | |
150 matrix.set3x3(1.0 - 2.0 * (y * y + z * z), | |
151 2.0 * (x * y + z * w), | |
152 2.0 * (x * z - y * w), | |
153 2.0 * (x * y - z * w), | |
154 1.0 - 2.0 * (x * x + z * z), | |
155 2.0 * (y * z + x * w), | |
156 2.0 * (x * z + y * w), | |
157 2.0 * (y * z - x * w), | |
158 1.0 - 2.0 * (x * x + y * y)); | |
159 return matrix; | |
160 } | |
161 | |
162 SkMatrix44 BuildSnappedRotationMatrix(const DecomposedTransform& decomp) { | |
163 // Create snapped rotation. | |
164 SkMatrix44 rotation_matrix = BuildRotationMatrix(decomp); | |
165 for (int i = 0; i < 3; ++i) { | |
166 for (int j = 0; j < 3; ++j) { | |
167 SkMScalar value = rotation_matrix.get(i, j); | |
168 // Snap values to -1, 0 or 1. | |
169 if (value < -0.5f) { | |
170 value = -1.0f; | |
171 } else if (value > 0.5f) { | |
172 value = 1.0f; | |
173 } else { | |
174 value = 0.0f; | |
175 } | |
176 rotation_matrix.set(i, j, value); | |
177 } | |
178 } | |
179 return rotation_matrix; | |
180 } | |
181 | |
182 SkMatrix44 BuildSkewMatrix(const DecomposedTransform& decomp) { | |
183 SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor); | |
184 | |
185 SkMatrix44 temp(SkMatrix44::kIdentity_Constructor); | |
186 if (decomp.skew[2]) { | |
187 temp.setDouble(1, 2, decomp.skew[2]); | |
188 matrix.preConcat(temp); | |
189 } | |
190 | |
191 if (decomp.skew[1]) { | |
192 temp.setDouble(1, 2, 0); | |
193 temp.setDouble(0, 2, decomp.skew[1]); | |
194 matrix.preConcat(temp); | |
195 } | |
196 | |
197 if (decomp.skew[0]) { | |
198 temp.setDouble(0, 2, 0); | |
199 temp.setDouble(0, 1, decomp.skew[0]); | |
200 matrix.preConcat(temp); | |
201 } | |
202 return matrix; | |
203 } | |
204 | |
205 SkMatrix44 BuildScaleMatrix(const DecomposedTransform& decomp) { | |
206 SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor); | |
207 matrix.setScale(SkDoubleToMScalar(decomp.scale[0]), | |
danakj
2013/10/23 14:57:19
This doesn't setIdentity like the other setFoo()s?
Ian Vollick
2013/10/23 19:03:13
It does. You could use the 'uninitialized' constru
avallee
2013/10/23 19:44:15
Done.
| |
208 SkDoubleToMScalar(decomp.scale[1]), | |
209 SkDoubleToMScalar(decomp.scale[2])); | |
210 return matrix; | |
211 } | |
212 | |
213 SkMatrix44 BuildSnappedScaleMatrix(DecomposedTransform decomp) { | |
214 decomp.scale[0] = std::floor(decomp.scale[0] + SkDoubleToMScalar(0.5)); | |
215 decomp.scale[1] = std::floor(decomp.scale[1] + SkDoubleToMScalar(0.5)); | |
216 decomp.scale[2] = std::floor(decomp.scale[2] + SkDoubleToMScalar(0.5)); | |
217 return BuildScaleMatrix(decomp); | |
218 } | |
219 | |
220 Transform ComposeTransform(const SkMatrix44& perspective, | |
221 const SkMatrix44& translation, | |
222 const SkMatrix44& rotation, | |
223 const SkMatrix44& skew, | |
224 const SkMatrix44& scale) { | |
225 SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor); | |
226 | |
227 matrix.preConcat(perspective); | |
228 matrix.preConcat(translation); | |
229 matrix.preConcat(rotation); | |
230 matrix.preConcat(skew); | |
231 matrix.preConcat(scale); | |
232 | |
233 Transform to_return; | |
234 to_return.matrix() = matrix; | |
235 return to_return; | |
236 } | |
237 | |
238 bool CheckViewportPointMapsWithinOnePixel(const Point& point, | |
239 const Transform& transform) { | |
240 Point3F point_original, point_transformed; | |
danakj
2013/10/23 14:57:19
From style guide "In particular, initialization sh
avallee
2013/10/23 19:44:15
Done.
| |
241 point_original = point_transformed = Point3F(point); | |
242 | |
243 // Can't use TransformRect here since it would give us the axis-aligned | |
244 // bounding rect of the 4 points in the initial rectable which is not what we | |
245 // want. | |
246 transform.TransformPoint(&point_transformed); | |
247 | |
248 if ((point_transformed - point_original).Length() > 1.f) { | |
249 // The changed distance should not be more than 1 pixel. | |
250 return false; | |
251 } | |
252 return true; | |
253 } | |
254 | |
255 bool CheckTransformsMapsIntViewportWithinOnePixel(const Rect& viewport, | |
256 const Transform& original, | |
257 const Transform& snapped) { | |
258 | |
259 Transform original_inv(Transform::kSkipInitialization); | |
260 bool invertible = true; | |
261 invertible &= original.GetInverse(&original_inv); | |
262 DCHECK(invertible) << "Non-invertible transform, cannot snap."; | |
263 | |
264 Transform combined = snapped * original_inv; | |
265 | |
266 return CheckViewportPointMapsWithinOnePixel(viewport.origin(), combined) && | |
267 CheckViewportPointMapsWithinOnePixel(viewport.top_right(), combined) && | |
268 CheckViewportPointMapsWithinOnePixel(viewport.bottom_left(), | |
269 combined) && | |
270 CheckViewportPointMapsWithinOnePixel(viewport.bottom_right(), | |
271 combined); | |
272 } | |
273 | |
111 } // namespace | 274 } // namespace |
112 | 275 |
113 Transform GetScaleTransform(const Point& anchor, float scale) { | 276 Transform GetScaleTransform(const Point& anchor, float scale) { |
114 Transform transform; | 277 Transform transform; |
115 transform.Translate(anchor.x() * (1 - scale), | 278 transform.Translate(anchor.x() * (1 - scale), |
116 anchor.y() * (1 - scale)); | 279 anchor.y() * (1 - scale)); |
117 transform.Scale(scale, scale); | 280 transform.Scale(scale, scale); |
118 return transform; | 281 return transform; |
119 } | 282 } |
120 | 283 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 if (row[0][2] > row[2][0]) | 426 if (row[0][2] > row[2][0]) |
264 decomp->quaternion[1] = -decomp->quaternion[1]; | 427 decomp->quaternion[1] = -decomp->quaternion[1]; |
265 if (row[1][0] > row[0][1]) | 428 if (row[1][0] > row[0][1]) |
266 decomp->quaternion[2] = -decomp->quaternion[2]; | 429 decomp->quaternion[2] = -decomp->quaternion[2]; |
267 | 430 |
268 return true; | 431 return true; |
269 } | 432 } |
270 | 433 |
271 // Taken from http://www.w3.org/TR/css3-transforms/. | 434 // Taken from http://www.w3.org/TR/css3-transforms/. |
272 Transform ComposeTransform(const DecomposedTransform& decomp) { | 435 Transform ComposeTransform(const DecomposedTransform& decomp) { |
273 SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor); | 436 SkMatrix44 perspective = BuildPerspectiveMatrix(decomp); |
274 for (int i = 0; i < 4; i++) | 437 SkMatrix44 translation = BuildTranslationMatrix(decomp); |
275 matrix.set(3, i, decomp.perspective[i]); | 438 SkMatrix44 rotation = BuildRotationMatrix(decomp); |
439 SkMatrix44 skew = BuildSkewMatrix(decomp); | |
440 SkMatrix44 scale = BuildScaleMatrix(decomp); | |
276 | 441 |
277 matrix.preTranslate( | 442 return ComposeTransform(perspective, translation, rotation, skew, scale); |
278 decomp.translate[0], decomp.translate[1], decomp.translate[2]); | 443 } |
279 | 444 |
280 SkMScalar x = decomp.quaternion[0]; | 445 bool SnapTransform(Transform* out, |
281 SkMScalar y = decomp.quaternion[1]; | 446 const Transform& transform, |
282 SkMScalar z = decomp.quaternion[2]; | 447 const Rect& viewport) { |
283 SkMScalar w = decomp.quaternion[3]; | 448 DecomposedTransform decomp; |
449 DecomposeTransform(&decomp, transform); | |
284 | 450 |
285 SkMatrix44 rotation_matrix(SkMatrix44::kUninitialized_Constructor); | 451 SkMatrix44 rotation_matrix = BuildSnappedRotationMatrix(decomp); |
286 rotation_matrix.set3x3(1.0 - 2.0 * (y * y + z * z), | 452 SkMatrix44 translation = BuildSnappedTranslationMatrix(decomp); |
287 2.0 * (x * y + z * w), | 453 SkMatrix44 scale = BuildSnappedScaleMatrix(decomp); |
288 2.0 * (x * z - y * w), | |
289 2.0 * (x * y - z * w), | |
290 1.0 - 2.0 * (x * x + z * z), | |
291 2.0 * (y * z + x * w), | |
292 2.0 * (x * z + y * w), | |
293 2.0 * (y * z - x * w), | |
294 1.0 - 2.0 * (x * x + y * y)); | |
295 | 454 |
296 matrix.preConcat(rotation_matrix); | 455 // Rebuild matrices for other unchanged components. |
456 SkMatrix44 perspective = BuildPerspectiveMatrix(decomp); | |
297 | 457 |
298 SkMatrix44 temp(SkMatrix44::kIdentity_Constructor); | 458 // Completely ignore the skew. |
299 if (decomp.skew[2]) { | 459 SkMatrix44 skew(SkMatrix44::kIdentity_Constructor); |
300 temp.set(1, 2, decomp.skew[2]); | 460 |
301 matrix.preConcat(temp); | 461 // Get full tranform |
462 Transform snapped = | |
463 ComposeTransform(perspective, translation, rotation_matrix, skew, scale); | |
464 | |
465 // Verify that viewport is not moved unnaturally. | |
466 bool snappable = | |
467 CheckTransformsMapsIntViewportWithinOnePixel(viewport, transform, snapped); | |
468 if (snappable) { | |
469 *out = snapped; | |
302 } | 470 } |
303 | 471 return snappable; |
304 if (decomp.skew[1]) { | |
305 temp.set(1, 2, 0); | |
306 temp.set(0, 2, decomp.skew[1]); | |
307 matrix.preConcat(temp); | |
308 } | |
309 | |
310 if (decomp.skew[0]) { | |
311 temp.set(0, 2, 0); | |
312 temp.set(0, 1, decomp.skew[0]); | |
313 matrix.preConcat(temp); | |
314 } | |
315 | |
316 matrix.preScale(decomp.scale[0], decomp.scale[1], decomp.scale[2]); | |
317 | |
318 Transform to_return; | |
319 to_return.matrix() = matrix; | |
320 return to_return; | |
321 } | 472 } |
322 | 473 |
323 std::string DecomposedTransform::ToString() const { | 474 std::string DecomposedTransform::ToString() const { |
324 return base::StringPrintf( | 475 return base::StringPrintf( |
325 "translate: %+0.4f %+0.4f %+0.4f\n" | 476 "translate: %+0.4f %+0.4f %+0.4f\n" |
326 "scale: %+0.4f %+0.4f %+0.4f\n" | 477 "scale: %+0.4f %+0.4f %+0.4f\n" |
327 "skew: %+0.4f %+0.4f %+0.4f\n" | 478 "skew: %+0.4f %+0.4f %+0.4f\n" |
328 "perspective: %+0.4f %+0.4f %+0.4f %+0.4f\n" | 479 "perspective: %+0.4f %+0.4f %+0.4f %+0.4f\n" |
329 "quaternion: %+0.4f %+0.4f %+0.4f %+0.4f\n", | 480 "quaternion: %+0.4f %+0.4f %+0.4f %+0.4f\n", |
330 translate[0], | 481 translate[0], |
331 translate[1], | 482 translate[1], |
332 translate[2], | 483 translate[2], |
333 scale[0], | 484 scale[0], |
334 scale[1], | 485 scale[1], |
335 scale[2], | 486 scale[2], |
336 skew[0], | 487 skew[0], |
337 skew[1], | 488 skew[1], |
338 skew[2], | 489 skew[2], |
339 perspective[0], | 490 perspective[0], |
340 perspective[1], | 491 perspective[1], |
341 perspective[2], | 492 perspective[2], |
342 perspective[3], | 493 perspective[3], |
343 quaternion[0], | 494 quaternion[0], |
344 quaternion[1], | 495 quaternion[1], |
345 quaternion[2], | 496 quaternion[2], |
346 quaternion[3]); | 497 quaternion[3]); |
347 } | 498 } |
348 | 499 |
349 } // namespace ui | 500 } // namespace ui |
OLD | NEW |