| 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> |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 | 76 |
| 77 void Transform::RotateAboutXAxis(double degrees) { | 77 void Transform::RotateAboutXAxis(double degrees) { |
| 78 double radians = degrees * M_PI / 180; | 78 double radians = degrees * M_PI / 180; |
| 79 double cosTheta = std::cos(radians); | 79 double cosTheta = std::cos(radians); |
| 80 double sinTheta = std::sin(radians); | 80 double sinTheta = std::sin(radians); |
| 81 if (matrix_.isIdentity()) { | 81 if (matrix_.isIdentity()) { |
| 82 matrix_.set3x3(1, 0, 0, | 82 matrix_.set3x3(1, 0, 0, |
| 83 0, cosTheta, sinTheta, | 83 0, cosTheta, sinTheta, |
| 84 0, -sinTheta, cosTheta); | 84 0, -sinTheta, cosTheta); |
| 85 } else { | 85 } else { |
| 86 SkMatrix44 rot; | 86 SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor); |
| 87 rot.set3x3(1, 0, 0, | 87 rot.set3x3(1, 0, 0, |
| 88 0, cosTheta, sinTheta, | 88 0, cosTheta, sinTheta, |
| 89 0, -sinTheta, cosTheta); | 89 0, -sinTheta, cosTheta); |
| 90 matrix_.preConcat(rot); | 90 matrix_.preConcat(rot); |
| 91 } | 91 } |
| 92 } | 92 } |
| 93 | 93 |
| 94 void Transform::RotateAboutYAxis(double degrees) { | 94 void Transform::RotateAboutYAxis(double degrees) { |
| 95 double radians = degrees * M_PI / 180; | 95 double radians = degrees * M_PI / 180; |
| 96 double cosTheta = std::cos(radians); | 96 double cosTheta = std::cos(radians); |
| 97 double sinTheta = std::sin(radians); | 97 double sinTheta = std::sin(radians); |
| 98 if (matrix_.isIdentity()) { | 98 if (matrix_.isIdentity()) { |
| 99 // Note carefully the placement of the -sinTheta for rotation about | 99 // Note carefully the placement of the -sinTheta for rotation about |
| 100 // y-axis is different than rotation about x-axis or z-axis. | 100 // y-axis is different than rotation about x-axis or z-axis. |
| 101 matrix_.set3x3(cosTheta, 0, -sinTheta, | 101 matrix_.set3x3(cosTheta, 0, -sinTheta, |
| 102 0, 1, 0, | 102 0, 1, 0, |
| 103 sinTheta, 0, cosTheta); | 103 sinTheta, 0, cosTheta); |
| 104 } else { | 104 } else { |
| 105 SkMatrix44 rot; | 105 SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor); |
| 106 rot.set3x3(cosTheta, 0, -sinTheta, | 106 rot.set3x3(cosTheta, 0, -sinTheta, |
| 107 0, 1, 0, | 107 0, 1, 0, |
| 108 sinTheta, 0, cosTheta); | 108 sinTheta, 0, cosTheta); |
| 109 matrix_.preConcat(rot); | 109 matrix_.preConcat(rot); |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 | 112 |
| 113 void Transform::RotateAboutZAxis(double degrees) { | 113 void Transform::RotateAboutZAxis(double degrees) { |
| 114 double radians = degrees * M_PI / 180; | 114 double radians = degrees * M_PI / 180; |
| 115 double cosTheta = std::cos(radians); | 115 double cosTheta = std::cos(radians); |
| 116 double sinTheta = std::sin(radians); | 116 double sinTheta = std::sin(radians); |
| 117 if (matrix_.isIdentity()) { | 117 if (matrix_.isIdentity()) { |
| 118 matrix_.set3x3(cosTheta, sinTheta, 0, | 118 matrix_.set3x3(cosTheta, sinTheta, 0, |
| 119 -sinTheta, cosTheta, 0, | 119 -sinTheta, cosTheta, 0, |
| 120 0, 0, 1); | 120 0, 0, 1); |
| 121 } else { | 121 } else { |
| 122 SkMatrix44 rot; | 122 SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor); |
| 123 rot.set3x3(cosTheta, sinTheta, 0, | 123 rot.set3x3(cosTheta, sinTheta, 0, |
| 124 -sinTheta, cosTheta, 0, | 124 -sinTheta, cosTheta, 0, |
| 125 0, 0, 1); | 125 0, 0, 1); |
| 126 matrix_.preConcat(rot); | 126 matrix_.preConcat(rot); |
| 127 } | 127 } |
| 128 } | 128 } |
| 129 | 129 |
| 130 void Transform::RotateAbout(const Vector3dF& axis, double degrees) { | 130 void Transform::RotateAbout(const Vector3dF& axis, double degrees) { |
| 131 if (matrix_.isIdentity()) { | 131 if (matrix_.isIdentity()) { |
| 132 matrix_.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), | 132 matrix_.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), |
| 133 SkDoubleToMScalar(axis.y()), | 133 SkDoubleToMScalar(axis.y()), |
| 134 SkDoubleToMScalar(axis.z()), | 134 SkDoubleToMScalar(axis.z()), |
| 135 SkDoubleToMScalar(degrees)); | 135 SkDoubleToMScalar(degrees)); |
| 136 } else { | 136 } else { |
| 137 SkMatrix44 rot; | 137 SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor); |
| 138 rot.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), | 138 rot.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), |
| 139 SkDoubleToMScalar(axis.y()), | 139 SkDoubleToMScalar(axis.y()), |
| 140 SkDoubleToMScalar(axis.z()), | 140 SkDoubleToMScalar(axis.z()), |
| 141 SkDoubleToMScalar(degrees)); | 141 SkDoubleToMScalar(degrees)); |
| 142 matrix_.preConcat(rot); | 142 matrix_.preConcat(rot); |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 | 145 |
| 146 void Transform::Scale(double x, double y) { | 146 void Transform::Scale(double x, double y) { |
| 147 matrix_.preScale(SkDoubleToMScalar(x), SkDoubleToMScalar(y), 1); | 147 matrix_.preScale(SkDoubleToMScalar(x), SkDoubleToMScalar(y), 1); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 160 void Transform::Translate3d(double x, double y, double z) { | 160 void Transform::Translate3d(double x, double y, double z) { |
| 161 matrix_.preTranslate(SkDoubleToMScalar(x), | 161 matrix_.preTranslate(SkDoubleToMScalar(x), |
| 162 SkDoubleToMScalar(y), | 162 SkDoubleToMScalar(y), |
| 163 SkDoubleToMScalar(z)); | 163 SkDoubleToMScalar(z)); |
| 164 } | 164 } |
| 165 | 165 |
| 166 void Transform::SkewX(double angle_x) { | 166 void Transform::SkewX(double angle_x) { |
| 167 if (matrix_.isIdentity()) | 167 if (matrix_.isIdentity()) |
| 168 matrix_.setDouble(0, 1, TanDegrees(angle_x)); | 168 matrix_.setDouble(0, 1, TanDegrees(angle_x)); |
| 169 else { | 169 else { |
| 170 SkMatrix44 skew; | 170 SkMatrix44 skew(SkMatrix44::kIdentity_Constructor); |
| 171 skew.setDouble(0, 1, TanDegrees(angle_x)); | 171 skew.setDouble(0, 1, TanDegrees(angle_x)); |
| 172 matrix_.preConcat(skew); | 172 matrix_.preConcat(skew); |
| 173 } | 173 } |
| 174 } | 174 } |
| 175 | 175 |
| 176 void Transform::SkewY(double angle_y) { | 176 void Transform::SkewY(double angle_y) { |
| 177 if (matrix_.isIdentity()) | 177 if (matrix_.isIdentity()) |
| 178 matrix_.setDouble(1, 0, TanDegrees(angle_y)); | 178 matrix_.setDouble(1, 0, TanDegrees(angle_y)); |
| 179 else { | 179 else { |
| 180 SkMatrix44 skew; | 180 SkMatrix44 skew(SkMatrix44::kIdentity_Constructor); |
| 181 skew.setDouble(1, 0, TanDegrees(angle_y)); | 181 skew.setDouble(1, 0, TanDegrees(angle_y)); |
| 182 matrix_.preConcat(skew); | 182 matrix_.preConcat(skew); |
| 183 } | 183 } |
| 184 } | 184 } |
| 185 | 185 |
| 186 void Transform::ApplyPerspectiveDepth(double depth) { | 186 void Transform::ApplyPerspectiveDepth(double depth) { |
| 187 if (depth == 0) | 187 if (depth == 0) |
| 188 return; | 188 return; |
| 189 if (matrix_.isIdentity()) | 189 if (matrix_.isIdentity()) |
| 190 matrix_.setDouble(3, 2, -1.0 / depth); | 190 matrix_.setDouble(3, 2, -1.0 / depth); |
| 191 else { | 191 else { |
| 192 SkMatrix44 m; | 192 SkMatrix44 m(SkMatrix44::kIdentity_Constructor); |
| 193 m.setDouble(3, 2, -1.0 / depth); | 193 m.setDouble(3, 2, -1.0 / depth); |
| 194 matrix_.preConcat(m); | 194 matrix_.preConcat(m); |
| 195 } | 195 } |
| 196 } | 196 } |
| 197 | 197 |
| 198 void Transform::PreconcatTransform(const Transform& transform) { | 198 void Transform::PreconcatTransform(const Transform& transform) { |
| 199 matrix_.preConcat(transform.matrix_); | 199 matrix_.preConcat(transform.matrix_); |
| 200 } | 200 } |
| 201 | 201 |
| 202 void Transform::ConcatTransform(const Transform& transform) { | 202 void Transform::ConcatTransform(const Transform& transform) { |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 void Transform::TransformPoint(Point& point) const { | 314 void Transform::TransformPoint(Point& point) const { |
| 315 TransformPointInternal(matrix_, point); | 315 TransformPointInternal(matrix_, point); |
| 316 } | 316 } |
| 317 | 317 |
| 318 void Transform::TransformPoint(Point3F& point) const { | 318 void Transform::TransformPoint(Point3F& point) const { |
| 319 TransformPointInternal(matrix_, point); | 319 TransformPointInternal(matrix_, point); |
| 320 } | 320 } |
| 321 | 321 |
| 322 bool Transform::TransformPointReverse(Point& point) const { | 322 bool Transform::TransformPointReverse(Point& point) const { |
| 323 // TODO(sad): Try to avoid trying to invert the matrix. | 323 // TODO(sad): Try to avoid trying to invert the matrix. |
| 324 SkMatrix44 inverse; | 324 SkMatrix44 inverse(SkMatrix44::kUninitialized_Constructor); |
| 325 if (!matrix_.invert(&inverse)) | 325 if (!matrix_.invert(&inverse)) |
| 326 return false; | 326 return false; |
| 327 | 327 |
| 328 TransformPointInternal(inverse, point); | 328 TransformPointInternal(inverse, point); |
| 329 return true; | 329 return true; |
| 330 } | 330 } |
| 331 | 331 |
| 332 bool Transform::TransformPointReverse(Point3F& point) const { | 332 bool Transform::TransformPointReverse(Point3F& point) const { |
| 333 // TODO(sad): Try to avoid trying to invert the matrix. | 333 // TODO(sad): Try to avoid trying to invert the matrix. |
| 334 SkMatrix44 inverse; | 334 SkMatrix44 inverse(SkMatrix44::kUninitialized_Constructor); |
| 335 if (!matrix_.invert(&inverse)) | 335 if (!matrix_.invert(&inverse)) |
| 336 return false; | 336 return false; |
| 337 | 337 |
| 338 TransformPointInternal(inverse, point); | 338 TransformPointInternal(inverse, point); |
| 339 return true; | 339 return true; |
| 340 } | 340 } |
| 341 | 341 |
| 342 void Transform::TransformRect(RectF* rect) const { | 342 void Transform::TransformRect(RectF* rect) const { |
| 343 if (matrix_.isIdentity()) | 343 if (matrix_.isIdentity()) |
| 344 return; | 344 return; |
| 345 | 345 |
| 346 SkRect src = RectFToSkRect(*rect); | 346 SkRect src = RectFToSkRect(*rect); |
| 347 const SkMatrix& matrix = matrix_; | 347 const SkMatrix& matrix = matrix_; |
| 348 matrix.mapRect(&src); | 348 matrix.mapRect(&src); |
| 349 *rect = SkRectToRectF(src); | 349 *rect = SkRectToRectF(src); |
| 350 } | 350 } |
| 351 | 351 |
| 352 bool Transform::TransformRectReverse(RectF* rect) const { | 352 bool Transform::TransformRectReverse(RectF* rect) const { |
| 353 if (matrix_.isIdentity()) | 353 if (matrix_.isIdentity()) |
| 354 return true; | 354 return true; |
| 355 | 355 |
| 356 SkMatrix44 inverse; | 356 SkMatrix44 inverse(SkMatrix44::kUninitialized_Constructor); |
| 357 if (!matrix_.invert(&inverse)) | 357 if (!matrix_.invert(&inverse)) |
| 358 return false; | 358 return false; |
| 359 | 359 |
| 360 const SkMatrix& matrix = inverse; | 360 const SkMatrix& matrix = inverse; |
| 361 SkRect src = RectFToSkRect(*rect); | 361 SkRect src = RectFToSkRect(*rect); |
| 362 matrix.mapRect(&src); | 362 matrix.mapRect(&src); |
| 363 *rect = SkRectToRectF(src); | 363 *rect = SkRectToRectF(src); |
| 364 return true; | 364 return true; |
| 365 } | 365 } |
| 366 | 366 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 matrix_.getDouble(2, 1), | 442 matrix_.getDouble(2, 1), |
| 443 matrix_.getDouble(2, 2), | 443 matrix_.getDouble(2, 2), |
| 444 matrix_.getDouble(2, 3), | 444 matrix_.getDouble(2, 3), |
| 445 matrix_.getDouble(3, 0), | 445 matrix_.getDouble(3, 0), |
| 446 matrix_.getDouble(3, 1), | 446 matrix_.getDouble(3, 1), |
| 447 matrix_.getDouble(3, 2), | 447 matrix_.getDouble(3, 2), |
| 448 matrix_.getDouble(3, 3)); | 448 matrix_.getDouble(3, 3)); |
| 449 } | 449 } |
| 450 | 450 |
| 451 } // namespace gfx | 451 } // namespace gfx |
| OLD | NEW |