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/logging.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 | 100 |
101 return true; | 101 return true; |
102 } | 102 } |
103 | 103 |
104 // Returns false if the matrix cannot be normalized. | 104 // Returns false if the matrix cannot be normalized. |
105 bool Normalize(SkMatrix44& m) { | 105 bool Normalize(SkMatrix44& m) { |
106 if (m.get(3, 3) == 0.0) | 106 if (m.get(3, 3) == 0.0) |
107 // Cannot normalize. | 107 // Cannot normalize. |
108 return false; | 108 return false; |
109 | 109 |
110 SkMScalar scale = 1.0 / m.get(3, 3); | 110 SkMScalar scale = SK_MScalar1 / m.get(3, 3); |
111 for (int i = 0; i < 4; i++) | 111 for (int i = 0; i < 4; i++) |
112 for (int j = 0; j < 4; j++) | 112 for (int j = 0; j < 4; j++) |
113 m.set(i, j, m.get(i, j) * scale); | 113 m.set(i, j, m.get(i, j) * scale); |
114 | 114 |
115 return true; | 115 return true; |
116 } | 116 } |
117 | 117 |
118 SkMatrix44 BuildPerspectiveMatrix(const DecomposedTransform& decomp) { | 118 SkMatrix44 BuildPerspectiveMatrix(const DecomposedTransform& decomp) { |
119 SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor); | 119 SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor); |
120 | 120 |
(...skipping 20 matching lines...) Expand all Loading... |
141 | 141 |
142 SkMatrix44 BuildRotationMatrix(const DecomposedTransform& decomp) { | 142 SkMatrix44 BuildRotationMatrix(const DecomposedTransform& decomp) { |
143 double x = decomp.quaternion[0]; | 143 double x = decomp.quaternion[0]; |
144 double y = decomp.quaternion[1]; | 144 double y = decomp.quaternion[1]; |
145 double z = decomp.quaternion[2]; | 145 double z = decomp.quaternion[2]; |
146 double w = decomp.quaternion[3]; | 146 double w = decomp.quaternion[3]; |
147 | 147 |
148 SkMatrix44 matrix(SkMatrix44::kUninitialized_Constructor); | 148 SkMatrix44 matrix(SkMatrix44::kUninitialized_Constructor); |
149 | 149 |
150 // Implicitly calls matrix.setIdentity() | 150 // Implicitly calls matrix.setIdentity() |
151 matrix.set3x3(1.0 - 2.0 * (y * y + z * z), | 151 matrix.set3x3(SkDoubleToMScalar(1.0 - 2.0 * (y * y + z * z)), |
152 2.0 * (x * y + z * w), | 152 SkDoubleToMScalar(2.0 * (x * y + z * w)), |
153 2.0 * (x * z - y * w), | 153 SkDoubleToMScalar(2.0 * (x * z - y * w)), |
154 2.0 * (x * y - z * w), | 154 SkDoubleToMScalar(2.0 * (x * y - z * w)), |
155 1.0 - 2.0 * (x * x + z * z), | 155 SkDoubleToMScalar(1.0 - 2.0 * (x * x + z * z)), |
156 2.0 * (y * z + x * w), | 156 SkDoubleToMScalar(2.0 * (y * z + x * w)), |
157 2.0 * (x * z + y * w), | 157 SkDoubleToMScalar(2.0 * (x * z + y * w)), |
158 2.0 * (y * z - x * w), | 158 SkDoubleToMScalar(2.0 * (y * z - x * w)), |
159 1.0 - 2.0 * (x * x + y * y)); | 159 SkDoubleToMScalar(1.0 - 2.0 * (x * x + y * y))); |
160 return matrix; | 160 return matrix; |
161 } | 161 } |
162 | 162 |
163 SkMatrix44 BuildSnappedRotationMatrix(const DecomposedTransform& decomp) { | 163 SkMatrix44 BuildSnappedRotationMatrix(const DecomposedTransform& decomp) { |
164 // Create snapped rotation. | 164 // Create snapped rotation. |
165 SkMatrix44 rotation_matrix = BuildRotationMatrix(decomp); | 165 SkMatrix44 rotation_matrix = BuildRotationMatrix(decomp); |
166 for (int i = 0; i < 3; ++i) { | 166 for (int i = 0; i < 3; ++i) { |
167 for (int j = 0; j < 3; ++j) { | 167 for (int j = 0; j < 3; ++j) { |
168 SkMScalar value = rotation_matrix.get(i, j); | 168 SkMScalar value = rotation_matrix.get(i, j); |
169 // Snap values to -1, 0 or 1. | 169 // Snap values to -1, 0 or 1. |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 decomp->translate[i] = matrix.get(i, 3); | 366 decomp->translate[i] = matrix.get(i, 3); |
367 | 367 |
368 SkMScalar row[3][3]; | 368 SkMScalar row[3][3]; |
369 for (int i = 0; i < 3; i++) | 369 for (int i = 0; i < 3; i++) |
370 for (int j = 0; j < 3; ++j) | 370 for (int j = 0; j < 3; ++j) |
371 row[i][j] = matrix.get(j, i); | 371 row[i][j] = matrix.get(j, i); |
372 | 372 |
373 // Compute X scale factor and normalize first row. | 373 // Compute X scale factor and normalize first row. |
374 decomp->scale[0] = Length3(row[0]); | 374 decomp->scale[0] = Length3(row[0]); |
375 if (decomp->scale[0] != 0.0) | 375 if (decomp->scale[0] != 0.0) |
376 Scale3(row[0], 1.0 / decomp->scale[0]); | 376 Scale3(row[0], SK_MScalar1 / decomp->scale[0]); |
377 | 377 |
378 // Compute XY shear factor and make 2nd row orthogonal to 1st. | 378 // Compute XY shear factor and make 2nd row orthogonal to 1st. |
379 decomp->skew[0] = Dot<3>(row[0], row[1]); | 379 decomp->skew[0] = Dot<3>(row[0], row[1]); |
380 Combine<3>(row[1], row[1], row[0], 1.0, -decomp->skew[0]); | 380 Combine<3>(row[1], row[1], row[0], 1.0, -decomp->skew[0]); |
381 | 381 |
382 // Now, compute Y scale and normalize 2nd row. | 382 // Now, compute Y scale and normalize 2nd row. |
383 decomp->scale[1] = Length3(row[1]); | 383 decomp->scale[1] = Length3(row[1]); |
384 if (decomp->scale[1] != 0.0) | 384 if (decomp->scale[1] != 0.0) |
385 Scale3(row[1], 1.0 / decomp->scale[1]); | 385 Scale3(row[1], SK_MScalar1 / decomp->scale[1]); |
386 | 386 |
387 decomp->skew[0] /= decomp->scale[1]; | 387 decomp->skew[0] /= decomp->scale[1]; |
388 | 388 |
389 // Compute XZ and YZ shears, orthogonalize 3rd row | 389 // Compute XZ and YZ shears, orthogonalize 3rd row |
390 decomp->skew[1] = Dot<3>(row[0], row[2]); | 390 decomp->skew[1] = Dot<3>(row[0], row[2]); |
391 Combine<3>(row[2], row[2], row[0], 1.0, -decomp->skew[1]); | 391 Combine<3>(row[2], row[2], row[0], 1.0, -decomp->skew[1]); |
392 decomp->skew[2] = Dot<3>(row[1], row[2]); | 392 decomp->skew[2] = Dot<3>(row[1], row[2]); |
393 Combine<3>(row[2], row[2], row[1], 1.0, -decomp->skew[2]); | 393 Combine<3>(row[2], row[2], row[1], 1.0, -decomp->skew[2]); |
394 | 394 |
395 // Next, get Z scale and normalize 3rd row. | 395 // Next, get Z scale and normalize 3rd row. |
396 decomp->scale[2] = Length3(row[2]); | 396 decomp->scale[2] = Length3(row[2]); |
397 if (decomp->scale[2] != 0.0) | 397 if (decomp->scale[2] != 0.0) |
398 Scale3(row[2], 1.0 / decomp->scale[2]); | 398 Scale3(row[2], SK_MScalar1 / decomp->scale[2]); |
399 | 399 |
400 decomp->skew[1] /= decomp->scale[2]; | 400 decomp->skew[1] /= decomp->scale[2]; |
401 decomp->skew[2] /= decomp->scale[2]; | 401 decomp->skew[2] /= decomp->scale[2]; |
402 | 402 |
403 // At this point, the matrix (in rows) is orthonormal. | 403 // At this point, the matrix (in rows) is orthonormal. |
404 // Check for a coordinate system flip. If the determinant | 404 // Check for a coordinate system flip. If the determinant |
405 // is -1, then negate the matrix and the scaling factors. | 405 // is -1, then negate the matrix and the scaling factors. |
406 SkMScalar pdum3[3]; | 406 SkMScalar pdum3[3]; |
407 Cross3(pdum3, row[1], row[2]); | 407 Cross3(pdum3, row[1], row[2]); |
408 if (Dot<3>(row[0], pdum3) < 0) { | 408 if (Dot<3>(row[0], pdum3) < 0) { |
409 for (int i = 0; i < 3; i++) { | 409 for (int i = 0; i < 3; i++) { |
410 decomp->scale[i] *= -1.0; | 410 decomp->scale[i] *= -1.0; |
411 for (int j = 0; j < 3; ++j) | 411 for (int j = 0; j < 3; ++j) |
412 row[i][j] *= -1.0; | 412 row[i][j] *= -1.0; |
413 } | 413 } |
414 } | 414 } |
415 | 415 |
416 decomp->quaternion[0] = | 416 double row00 = SkMScalarToDouble(row[0][0]); |
417 0.5 * std::sqrt(std::max(1.0 + row[0][0] - row[1][1] - row[2][2], 0.0)); | 417 double row11 = SkMScalarToDouble(row[1][1]); |
418 decomp->quaternion[1] = | 418 double row22 = SkMScalarToDouble(row[2][2]); |
419 0.5 * std::sqrt(std::max(1.0 - row[0][0] + row[1][1] - row[2][2], 0.0)); | 419 decomp->quaternion[0] = SkDoubleToMScalar( |
420 decomp->quaternion[2] = | 420 0.5 * std::sqrt(std::max(1.0 + row00 - row11 - row22, 0.0))); |
421 0.5 * std::sqrt(std::max(1.0 - row[0][0] - row[1][1] + row[2][2], 0.0)); | 421 decomp->quaternion[1] = SkDoubleToMScalar( |
422 decomp->quaternion[3] = | 422 0.5 * std::sqrt(std::max(1.0 - row00 + row11 - row22, 0.0))); |
423 0.5 * std::sqrt(std::max(1.0 + row[0][0] + row[1][1] + row[2][2], 0.0)); | 423 decomp->quaternion[2] = SkDoubleToMScalar( |
| 424 0.5 * std::sqrt(std::max(1.0 - row00 - row11 + row22, 0.0))); |
| 425 decomp->quaternion[3] = SkDoubleToMScalar( |
| 426 0.5 * std::sqrt(std::max(1.0 + row00 + row11 + row22, 0.0))); |
424 | 427 |
425 if (row[2][1] > row[1][2]) | 428 if (row[2][1] > row[1][2]) |
426 decomp->quaternion[0] = -decomp->quaternion[0]; | 429 decomp->quaternion[0] = -decomp->quaternion[0]; |
427 if (row[0][2] > row[2][0]) | 430 if (row[0][2] > row[2][0]) |
428 decomp->quaternion[1] = -decomp->quaternion[1]; | 431 decomp->quaternion[1] = -decomp->quaternion[1]; |
429 if (row[1][0] > row[0][1]) | 432 if (row[1][0] > row[0][1]) |
430 decomp->quaternion[2] = -decomp->quaternion[2]; | 433 decomp->quaternion[2] = -decomp->quaternion[2]; |
431 | 434 |
432 return true; | 435 return true; |
433 } | 436 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 perspective[1], | 495 perspective[1], |
493 perspective[2], | 496 perspective[2], |
494 perspective[3], | 497 perspective[3], |
495 quaternion[0], | 498 quaternion[0], |
496 quaternion[1], | 499 quaternion[1], |
497 quaternion[2], | 500 quaternion[2], |
498 quaternion[3]); | 501 quaternion[3]); |
499 } | 502 } |
500 | 503 |
501 } // namespace ui | 504 } // namespace ui |
OLD | NEW |