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 14 matching lines...) Expand all Loading... |
25 namespace { | 25 namespace { |
26 | 26 |
27 // Taken from SkMatrix44. | 27 // Taken from SkMatrix44. |
28 const SkMScalar kEpsilon = 1e-8f; | 28 const SkMScalar kEpsilon = 1e-8f; |
29 | 29 |
30 SkMScalar TanDegrees(double degrees) { | 30 SkMScalar TanDegrees(double degrees) { |
31 double radians = degrees * M_PI / 180; | 31 double radians = degrees * M_PI / 180; |
32 return SkDoubleToMScalar(std::tan(radians)); | 32 return SkDoubleToMScalar(std::tan(radians)); |
33 } | 33 } |
34 | 34 |
| 35 inline bool ApproximatelyZero(SkMScalar x, SkMScalar tolerance) { |
| 36 return std::abs(x) <= tolerance; |
| 37 } |
| 38 |
| 39 inline bool ApproximatelyOne(SkMScalar x, SkMScalar tolerance) { |
| 40 return std::abs(x - SkDoubleToMScalar(1.0)) <= tolerance; |
| 41 } |
| 42 |
35 } // namespace | 43 } // namespace |
36 | 44 |
37 Transform::Transform(SkMScalar col1row1, | 45 Transform::Transform(SkMScalar col1row1, |
38 SkMScalar col2row1, | 46 SkMScalar col2row1, |
39 SkMScalar col3row1, | 47 SkMScalar col3row1, |
40 SkMScalar col4row1, | 48 SkMScalar col4row1, |
41 SkMScalar col1row2, | 49 SkMScalar col1row2, |
42 SkMScalar col2row2, | 50 SkMScalar col2row2, |
43 SkMScalar col3row2, | 51 SkMScalar col3row2, |
44 SkMScalar col4row2, | 52 SkMScalar col4row2, |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 } | 211 } |
204 | 212 |
205 void Transform::PreconcatTransform(const Transform& transform) { | 213 void Transform::PreconcatTransform(const Transform& transform) { |
206 matrix_.preConcat(transform.matrix_); | 214 matrix_.preConcat(transform.matrix_); |
207 } | 215 } |
208 | 216 |
209 void Transform::ConcatTransform(const Transform& transform) { | 217 void Transform::ConcatTransform(const Transform& transform) { |
210 matrix_.postConcat(transform.matrix_); | 218 matrix_.postConcat(transform.matrix_); |
211 } | 219 } |
212 | 220 |
| 221 bool Transform::IsApproximatelyIdentityOrTranslation( |
| 222 SkMScalar tolerance) const { |
| 223 DCHECK_GE(tolerance, 0); |
| 224 return |
| 225 ApproximatelyOne(matrix_.get(0, 0), tolerance) && |
| 226 ApproximatelyZero(matrix_.get(1, 0), tolerance) && |
| 227 ApproximatelyZero(matrix_.get(2, 0), tolerance) && |
| 228 matrix_.get(3, 0) == 0 && |
| 229 ApproximatelyZero(matrix_.get(0, 1), tolerance) && |
| 230 ApproximatelyOne(matrix_.get(1, 1), tolerance) && |
| 231 ApproximatelyZero(matrix_.get(2, 1), tolerance) && |
| 232 matrix_.get(3, 1) == 0 && |
| 233 ApproximatelyZero(matrix_.get(0, 2), tolerance) && |
| 234 ApproximatelyZero(matrix_.get(1, 2), tolerance) && |
| 235 ApproximatelyOne(matrix_.get(2, 2), tolerance) && |
| 236 matrix_.get(3, 2) == 0 && |
| 237 matrix_.get(3, 3) == 1; |
| 238 } |
| 239 |
213 bool Transform::IsIdentityOrIntegerTranslation() const { | 240 bool Transform::IsIdentityOrIntegerTranslation() const { |
214 if (!IsIdentityOrTranslation()) | 241 if (!IsIdentityOrTranslation()) |
215 return false; | 242 return false; |
216 | 243 |
217 bool no_fractional_translation = | 244 bool no_fractional_translation = |
218 static_cast<int>(matrix_.get(0, 3)) == matrix_.get(0, 3) && | 245 static_cast<int>(matrix_.get(0, 3)) == matrix_.get(0, 3) && |
219 static_cast<int>(matrix_.get(1, 3)) == matrix_.get(1, 3) && | 246 static_cast<int>(matrix_.get(1, 3)) == matrix_.get(1, 3) && |
220 static_cast<int>(matrix_.get(2, 3)) == matrix_.get(2, 3); | 247 static_cast<int>(matrix_.get(2, 3)) == matrix_.get(2, 3); |
221 | 248 |
222 return no_fractional_translation; | 249 return no_fractional_translation; |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 matrix_.get(2, 1), | 546 matrix_.get(2, 1), |
520 matrix_.get(2, 2), | 547 matrix_.get(2, 2), |
521 matrix_.get(2, 3), | 548 matrix_.get(2, 3), |
522 matrix_.get(3, 0), | 549 matrix_.get(3, 0), |
523 matrix_.get(3, 1), | 550 matrix_.get(3, 1), |
524 matrix_.get(3, 2), | 551 matrix_.get(3, 2), |
525 matrix_.get(3, 3)); | 552 matrix_.get(3, 3)); |
526 } | 553 } |
527 | 554 |
528 } // namespace gfx | 555 } // namespace gfx |
OLD | NEW |