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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 | 95 |
96 return true; | 96 return true; |
97 } | 97 } |
98 | 98 |
99 // Returns false if the matrix cannot be normalized. | 99 // Returns false if the matrix cannot be normalized. |
100 bool Normalize(SkMatrix44& m) { | 100 bool Normalize(SkMatrix44& m) { |
101 if (m.get(3, 3) == 0.0) | 101 if (m.get(3, 3) == 0.0) |
102 // Cannot normalize. | 102 // Cannot normalize. |
103 return false; | 103 return false; |
104 | 104 |
105 SkMScalar scale = 1.0 / m.get(3, 3); | 105 SkMScalar scale = SK_MScalar1 / m.get(3, 3); |
106 for (int i = 0; i < 4; i++) | 106 for (int i = 0; i < 4; i++) |
107 for (int j = 0; j < 4; j++) | 107 for (int j = 0; j < 4; j++) |
108 m.set(i, j, m.get(i, j) * scale); | 108 m.set(i, j, m.get(i, j) * scale); |
109 | 109 |
110 return true; | 110 return true; |
111 } | 111 } |
112 | 112 |
113 SkMatrix44 BuildPerspectiveMatrix(const DecomposedTransform& decomp) { | 113 SkMatrix44 BuildPerspectiveMatrix(const DecomposedTransform& decomp) { |
114 SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor); | 114 SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor); |
115 | 115 |
(...skipping 20 matching lines...) Expand all Loading... |
136 | 136 |
137 SkMatrix44 BuildRotationMatrix(const DecomposedTransform& decomp) { | 137 SkMatrix44 BuildRotationMatrix(const DecomposedTransform& decomp) { |
138 double x = decomp.quaternion[0]; | 138 double x = decomp.quaternion[0]; |
139 double y = decomp.quaternion[1]; | 139 double y = decomp.quaternion[1]; |
140 double z = decomp.quaternion[2]; | 140 double z = decomp.quaternion[2]; |
141 double w = decomp.quaternion[3]; | 141 double w = decomp.quaternion[3]; |
142 | 142 |
143 SkMatrix44 matrix(SkMatrix44::kUninitialized_Constructor); | 143 SkMatrix44 matrix(SkMatrix44::kUninitialized_Constructor); |
144 | 144 |
145 // Implicitly calls matrix.setIdentity() | 145 // Implicitly calls matrix.setIdentity() |
146 matrix.set3x3(1.0 - 2.0 * (y * y + z * z), | 146 matrix.set3x3(SkDoubleToMScalar(1.0 - 2.0 * (y * y + z * z)), |
147 2.0 * (x * y + z * w), | 147 SkDoubleToMScalar(2.0 * (x * y + z * w)), |
148 2.0 * (x * z - y * w), | 148 SkDoubleToMScalar(2.0 * (x * z - y * w)), |
149 2.0 * (x * y - z * w), | 149 SkDoubleToMScalar(2.0 * (x * y - z * w)), |
150 1.0 - 2.0 * (x * x + z * z), | 150 SkDoubleToMScalar(1.0 - 2.0 * (x * x + z * z)), |
151 2.0 * (y * z + x * w), | 151 SkDoubleToMScalar(2.0 * (y * z + x * w)), |
152 2.0 * (x * z + y * w), | 152 SkDoubleToMScalar(2.0 * (x * z + y * w)), |
153 2.0 * (y * z - x * w), | 153 SkDoubleToMScalar(2.0 * (y * z - x * w)), |
154 1.0 - 2.0 * (x * x + y * y)); | 154 SkDoubleToMScalar(1.0 - 2.0 * (x * x + y * y))); |
155 return matrix; | 155 return matrix; |
156 } | 156 } |
157 | 157 |
158 SkMatrix44 BuildSnappedRotationMatrix(const DecomposedTransform& decomp) { | 158 SkMatrix44 BuildSnappedRotationMatrix(const DecomposedTransform& decomp) { |
159 // Create snapped rotation. | 159 // Create snapped rotation. |
160 SkMatrix44 rotation_matrix = BuildRotationMatrix(decomp); | 160 SkMatrix44 rotation_matrix = BuildRotationMatrix(decomp); |
161 for (int i = 0; i < 3; ++i) { | 161 for (int i = 0; i < 3; ++i) { |
162 for (int j = 0; j < 3; ++j) { | 162 for (int j = 0; j < 3; ++j) { |
163 SkMScalar value = rotation_matrix.get(i, j); | 163 SkMScalar value = rotation_matrix.get(i, j); |
164 // Snap values to -1, 0 or 1. | 164 // Snap values to -1, 0 or 1. |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 SkMScalar pdum3[3]; | 410 SkMScalar pdum3[3]; |
411 Cross3(pdum3, row[1], row[2]); | 411 Cross3(pdum3, row[1], row[2]); |
412 if (Dot<3>(row[0], pdum3) < 0) { | 412 if (Dot<3>(row[0], pdum3) < 0) { |
413 for (int i = 0; i < 3; i++) { | 413 for (int i = 0; i < 3; i++) { |
414 decomp->scale[i] *= -1.0; | 414 decomp->scale[i] *= -1.0; |
415 for (int j = 0; j < 3; ++j) | 415 for (int j = 0; j < 3; ++j) |
416 row[i][j] *= -1.0; | 416 row[i][j] *= -1.0; |
417 } | 417 } |
418 } | 418 } |
419 | 419 |
420 decomp->quaternion[0] = | 420 double row00 = SkMScalarToDouble(row[0][0]); |
421 0.5 * std::sqrt(std::max(1.0 + row[0][0] - row[1][1] - row[2][2], 0.0)); | 421 double row11 = SkMScalarToDouble(row[1][1]); |
422 decomp->quaternion[1] = | 422 double row22 = SkMScalarToDouble(row[2][2]); |
423 0.5 * std::sqrt(std::max(1.0 - row[0][0] + row[1][1] - row[2][2], 0.0)); | 423 decomp->quaternion[0] = SkDoubleToMScalar( |
424 decomp->quaternion[2] = | 424 0.5 * std::sqrt(std::max(1.0 + row00 - row11 - row22, 0.0))); |
425 0.5 * std::sqrt(std::max(1.0 - row[0][0] - row[1][1] + row[2][2], 0.0)); | 425 decomp->quaternion[1] = SkDoubleToMScalar( |
426 decomp->quaternion[3] = | 426 0.5 * std::sqrt(std::max(1.0 - row00 + row11 - row22, 0.0))); |
427 0.5 * std::sqrt(std::max(1.0 + row[0][0] + row[1][1] + row[2][2], 0.0)); | 427 decomp->quaternion[2] = SkDoubleToMScalar( |
| 428 0.5 * std::sqrt(std::max(1.0 - row00 - row11 + row22, 0.0))); |
| 429 decomp->quaternion[3] = SkDoubleToMScalar( |
| 430 0.5 * std::sqrt(std::max(1.0 + row00 + row11 + row22, 0.0))); |
428 | 431 |
429 if (row[2][1] > row[1][2]) | 432 if (row[2][1] > row[1][2]) |
430 decomp->quaternion[0] = -decomp->quaternion[0]; | 433 decomp->quaternion[0] = -decomp->quaternion[0]; |
431 if (row[0][2] > row[2][0]) | 434 if (row[0][2] > row[2][0]) |
432 decomp->quaternion[1] = -decomp->quaternion[1]; | 435 decomp->quaternion[1] = -decomp->quaternion[1]; |
433 if (row[1][0] > row[0][1]) | 436 if (row[1][0] > row[0][1]) |
434 decomp->quaternion[2] = -decomp->quaternion[2]; | 437 decomp->quaternion[2] = -decomp->quaternion[2]; |
435 | 438 |
436 return true; | 439 return true; |
437 } | 440 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 perspective[1], | 499 perspective[1], |
497 perspective[2], | 500 perspective[2], |
498 perspective[3], | 501 perspective[3], |
499 quaternion[0], | 502 quaternion[0], |
500 quaternion[1], | 503 quaternion[1], |
501 quaternion[2], | 504 quaternion[2], |
502 quaternion[3]); | 505 quaternion[3]); |
503 } | 506 } |
504 | 507 |
505 } // namespace ui | 508 } // namespace ui |
OLD | NEW |