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> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "ui/gfx/point.h" | 14 #include "ui/gfx/point.h" |
15 #include "ui/gfx/point3_f.h" | 15 #include "ui/gfx/point3_f.h" |
16 #include "ui/gfx/rect.h" | 16 #include "ui/gfx/rect.h" |
17 #include "ui/gfx/safe_integer_conversions.h" | 17 #include "ui/gfx/safe_integer_conversions.h" |
18 #include "ui/gfx/skia_util.h" | 18 #include "ui/gfx/skia_util.h" |
19 #include "ui/gfx/transform_util.h" | 19 #include "ui/gfx/transform_util.h" |
20 #include "ui/gfx/vector3d_f.h" | 20 #include "ui/gfx/vector3d_f.h" |
21 | 21 |
22 namespace gfx { | 22 namespace gfx { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 // Taken from SkMatrix44. | 26 // Taken from SkMatrix44. |
27 const double kEpsilon = 1e-8; | 27 const SkMScalar kEpsilon = 1e-8; |
28 | 28 |
29 double TanDegrees(double degrees) { | 29 SkMScalar TanDegrees(double degrees) { |
30 double radians = degrees * M_PI / 180; | 30 SkMScalar radians = degrees * M_PI / 180; |
31 return std::tan(radians); | 31 return std::tan(radians); |
32 } | 32 } |
33 | 33 |
34 } // namespace | 34 } // namespace |
35 | 35 |
36 Transform::Transform( | 36 Transform::Transform(SkMScalar col1row1, |
37 double col1row1, double col2row1, double col3row1, double col4row1, | 37 SkMScalar col2row1, |
38 double col1row2, double col2row2, double col3row2, double col4row2, | 38 SkMScalar col3row1, |
39 double col1row3, double col2row3, double col3row3, double col4row3, | 39 SkMScalar col4row1, |
40 double col1row4, double col2row4, double col3row4, double col4row4) | 40 SkMScalar col1row2, |
41 : matrix_(SkMatrix44::kUninitialized_Constructor) | 41 SkMScalar col2row2, |
42 { | 42 SkMScalar col3row2, |
43 matrix_.setDouble(0, 0, col1row1); | 43 SkMScalar col4row2, |
44 matrix_.setDouble(1, 0, col1row2); | 44 SkMScalar col1row3, |
45 matrix_.setDouble(2, 0, col1row3); | 45 SkMScalar col2row3, |
46 matrix_.setDouble(3, 0, col1row4); | 46 SkMScalar col3row3, |
47 SkMScalar col4row3, | |
48 SkMScalar col1row4, | |
49 SkMScalar col2row4, | |
50 SkMScalar col3row4, | |
51 SkMScalar col4row4) | |
52 : matrix_(SkMatrix44::kUninitialized_Constructor) { | |
53 matrix_.set(0, 0, col1row1); | |
54 matrix_.set(1, 0, col1row2); | |
55 matrix_.set(2, 0, col1row3); | |
56 matrix_.set(3, 0, col1row4); | |
47 | 57 |
48 matrix_.setDouble(0, 1, col2row1); | 58 matrix_.set(0, 1, col2row1); |
49 matrix_.setDouble(1, 1, col2row2); | 59 matrix_.set(1, 1, col2row2); |
50 matrix_.setDouble(2, 1, col2row3); | 60 matrix_.set(2, 1, col2row3); |
51 matrix_.setDouble(3, 1, col2row4); | 61 matrix_.set(3, 1, col2row4); |
52 | 62 |
53 matrix_.setDouble(0, 2, col3row1); | 63 matrix_.set(0, 2, col3row1); |
54 matrix_.setDouble(1, 2, col3row2); | 64 matrix_.set(1, 2, col3row2); |
55 matrix_.setDouble(2, 2, col3row3); | 65 matrix_.set(2, 2, col3row3); |
56 matrix_.setDouble(3, 2, col3row4); | 66 matrix_.set(3, 2, col3row4); |
57 | 67 |
58 matrix_.setDouble(0, 3, col4row1); | 68 matrix_.set(0, 3, col4row1); |
59 matrix_.setDouble(1, 3, col4row2); | 69 matrix_.set(1, 3, col4row2); |
60 matrix_.setDouble(2, 3, col4row3); | 70 matrix_.set(2, 3, col4row3); |
61 matrix_.setDouble(3, 3, col4row4); | 71 matrix_.set(3, 3, col4row4); |
62 } | 72 } |
63 | 73 |
64 Transform::Transform( | 74 Transform::Transform(SkMScalar col1row1, |
65 double col1row1, double col2row1, | 75 SkMScalar col2row1, |
66 double col1row2, double col2row2, | 76 SkMScalar col1row2, |
67 double x_translation, double y_translation) | 77 SkMScalar col2row2, |
68 : matrix_(SkMatrix44::kIdentity_Constructor) | 78 SkMScalar x_translation, |
69 { | 79 SkMScalar y_translation) |
70 matrix_.setDouble(0, 0, col1row1); | 80 : matrix_(SkMatrix44::kIdentity_Constructor) { |
71 matrix_.setDouble(1, 0, col1row2); | 81 matrix_.set(0, 0, col1row1); |
72 matrix_.setDouble(0, 1, col2row1); | 82 matrix_.set(1, 0, col1row2); |
73 matrix_.setDouble(1, 1, col2row2); | 83 matrix_.set(0, 1, col2row1); |
74 matrix_.setDouble(0, 3, x_translation); | 84 matrix_.set(1, 1, col2row2); |
75 matrix_.setDouble(1, 3, y_translation); | 85 matrix_.set(0, 3, x_translation); |
86 matrix_.set(1, 3, y_translation); | |
76 } | 87 } |
77 | 88 |
78 void Transform::RotateAboutXAxis(double degrees) { | 89 void Transform::RotateAboutXAxis(double degrees) { |
79 double radians = degrees * M_PI / 180; | 90 double radians = degrees * M_PI / 180; |
80 double cosTheta = std::cos(radians); | 91 SkMScalar cosTheta = SkDoubleToMScalar(std::cos(radians)); |
81 double sinTheta = std::sin(radians); | 92 SkMScalar sinTheta = SkDoubleToMScalar(std::sin(radians)); |
82 if (matrix_.isIdentity()) { | 93 if (matrix_.isIdentity()) { |
83 matrix_.set3x3(1, 0, 0, | 94 matrix_.set3x3(1, 0, 0, |
84 0, cosTheta, sinTheta, | 95 0, cosTheta, sinTheta, |
85 0, -sinTheta, cosTheta); | 96 0, -sinTheta, cosTheta); |
86 } else { | 97 } else { |
87 SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor); | 98 SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor); |
88 rot.set3x3(1, 0, 0, | 99 rot.set3x3(1, 0, 0, |
89 0, cosTheta, sinTheta, | 100 0, cosTheta, sinTheta, |
90 0, -sinTheta, cosTheta); | 101 0, -sinTheta, cosTheta); |
91 matrix_.preConcat(rot); | 102 matrix_.preConcat(rot); |
92 } | 103 } |
93 } | 104 } |
94 | 105 |
95 void Transform::RotateAboutYAxis(double degrees) { | 106 void Transform::RotateAboutYAxis(double degrees) { |
96 double radians = degrees * M_PI / 180; | 107 double radians = degrees * M_PI / 180; |
97 double cosTheta = std::cos(radians); | 108 SkMScalar cosTheta = SkDoubleToMScalar(std::cos(radians)); |
98 double sinTheta = std::sin(radians); | 109 SkMScalar sinTheta = SkDoubleToMScalar(std::sin(radians)); |
99 if (matrix_.isIdentity()) { | 110 if (matrix_.isIdentity()) { |
100 // Note carefully the placement of the -sinTheta for rotation about | 111 // Note carefully the placement of the -sinTheta for rotation about |
101 // y-axis is different than rotation about x-axis or z-axis. | 112 // y-axis is different than rotation about x-axis or z-axis. |
102 matrix_.set3x3(cosTheta, 0, -sinTheta, | 113 matrix_.set3x3(cosTheta, 0, -sinTheta, |
103 0, 1, 0, | 114 0, 1, 0, |
104 sinTheta, 0, cosTheta); | 115 sinTheta, 0, cosTheta); |
105 } else { | 116 } else { |
106 SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor); | 117 SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor); |
107 rot.set3x3(cosTheta, 0, -sinTheta, | 118 rot.set3x3(cosTheta, 0, -sinTheta, |
108 0, 1, 0, | 119 0, 1, 0, |
109 sinTheta, 0, cosTheta); | 120 sinTheta, 0, cosTheta); |
110 matrix_.preConcat(rot); | 121 matrix_.preConcat(rot); |
111 } | 122 } |
112 } | 123 } |
113 | 124 |
114 void Transform::RotateAboutZAxis(double degrees) { | 125 void Transform::RotateAboutZAxis(double degrees) { |
115 double radians = degrees * M_PI / 180; | 126 double radians = (degrees / 180.0) * M_PI; |
enne (OOO)
2013/09/07 00:01:45
This needed to stay a double for precision when ro
| |
116 double cosTheta = std::cos(radians); | 127 SkMScalar cosTheta = SkDoubleToScalar(std::cos(radians)); |
117 double sinTheta = std::sin(radians); | 128 SkMScalar sinTheta = SkDoubleToScalar(std::sin(radians)); |
118 if (matrix_.isIdentity()) { | 129 if (matrix_.isIdentity()) { |
119 matrix_.set3x3(cosTheta, sinTheta, 0, | 130 matrix_.set3x3(cosTheta, sinTheta, 0, |
120 -sinTheta, cosTheta, 0, | 131 -sinTheta, cosTheta, 0, |
121 0, 0, 1); | 132 0, 0, 1); |
122 } else { | 133 } else { |
123 SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor); | 134 SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor); |
124 rot.set3x3(cosTheta, sinTheta, 0, | 135 rot.set3x3(cosTheta, sinTheta, 0, |
125 -sinTheta, cosTheta, 0, | 136 -sinTheta, cosTheta, 0, |
126 0, 0, 1); | 137 0, 0, 1); |
127 matrix_.preConcat(rot); | 138 matrix_.preConcat(rot); |
128 } | 139 } |
129 } | 140 } |
130 | 141 |
131 void Transform::RotateAbout(const Vector3dF& axis, double degrees) { | 142 void Transform::RotateAbout(const Vector3dF& axis, double degrees) { |
132 if (matrix_.isIdentity()) { | 143 if (matrix_.isIdentity()) { |
133 matrix_.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), | 144 matrix_.setRotateDegreesAbout(SkFloatToMScalar(axis.x()), |
134 SkDoubleToMScalar(axis.y()), | 145 SkFloatToMScalar(axis.y()), |
135 SkDoubleToMScalar(axis.z()), | 146 SkFloatToMScalar(axis.z()), |
136 SkDoubleToMScalar(degrees)); | 147 degrees); |
137 } else { | 148 } else { |
138 SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor); | 149 SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor); |
139 rot.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), | 150 rot.setRotateDegreesAbout(SkFloatToMScalar(axis.x()), |
140 SkDoubleToMScalar(axis.y()), | 151 SkFloatToMScalar(axis.y()), |
141 SkDoubleToMScalar(axis.z()), | 152 SkFloatToMScalar(axis.z()), |
142 SkDoubleToMScalar(degrees)); | 153 degrees); |
143 matrix_.preConcat(rot); | 154 matrix_.preConcat(rot); |
144 } | 155 } |
145 } | 156 } |
146 | 157 |
147 void Transform::Scale(double x, double y) { | 158 void Transform::Scale(SkMScalar x, SkMScalar y) { matrix_.preScale(x, y, 1); } |
148 matrix_.preScale(SkDoubleToMScalar(x), SkDoubleToMScalar(y), 1); | 159 |
160 void Transform::Scale3d(SkMScalar x, SkMScalar y, SkMScalar z) { | |
161 matrix_.preScale(x, y, z); | |
149 } | 162 } |
150 | 163 |
151 void Transform::Scale3d(double x, double y, double z) { | 164 void Transform::Translate(SkMScalar x, SkMScalar y) { |
152 matrix_.preScale(SkDoubleToMScalar(x), | 165 matrix_.preTranslate(x, y, 0); |
153 SkDoubleToMScalar(y), | |
154 SkDoubleToMScalar(z)); | |
155 } | 166 } |
156 | 167 |
157 void Transform::Translate(double x, double y) { | 168 void Transform::Translate3d(SkMScalar x, SkMScalar y, SkMScalar z) { |
158 matrix_.preTranslate(SkDoubleToMScalar(x), SkDoubleToMScalar(y), 0); | 169 matrix_.preTranslate(x, y, z); |
159 } | 170 } |
160 | 171 |
161 void Transform::Translate3d(double x, double y, double z) { | 172 void Transform::SkewX(SkMScalar angle_x) { |
162 matrix_.preTranslate(SkDoubleToMScalar(x), | |
163 SkDoubleToMScalar(y), | |
164 SkDoubleToMScalar(z)); | |
165 } | |
166 | |
167 void Transform::SkewX(double angle_x) { | |
168 if (matrix_.isIdentity()) | 173 if (matrix_.isIdentity()) |
169 matrix_.setDouble(0, 1, TanDegrees(angle_x)); | 174 matrix_.set(0, 1, TanDegrees(angle_x)); |
170 else { | 175 else { |
171 SkMatrix44 skew(SkMatrix44::kIdentity_Constructor); | 176 SkMatrix44 skew(SkMatrix44::kIdentity_Constructor); |
172 skew.setDouble(0, 1, TanDegrees(angle_x)); | 177 skew.set(0, 1, TanDegrees(angle_x)); |
173 matrix_.preConcat(skew); | 178 matrix_.preConcat(skew); |
174 } | 179 } |
175 } | 180 } |
176 | 181 |
177 void Transform::SkewY(double angle_y) { | 182 void Transform::SkewY(SkMScalar angle_y) { |
178 if (matrix_.isIdentity()) | 183 if (matrix_.isIdentity()) |
179 matrix_.setDouble(1, 0, TanDegrees(angle_y)); | 184 matrix_.set(1, 0, TanDegrees(angle_y)); |
180 else { | 185 else { |
181 SkMatrix44 skew(SkMatrix44::kIdentity_Constructor); | 186 SkMatrix44 skew(SkMatrix44::kIdentity_Constructor); |
182 skew.setDouble(1, 0, TanDegrees(angle_y)); | 187 skew.set(1, 0, TanDegrees(angle_y)); |
183 matrix_.preConcat(skew); | 188 matrix_.preConcat(skew); |
184 } | 189 } |
185 } | 190 } |
186 | 191 |
187 void Transform::ApplyPerspectiveDepth(double depth) { | 192 void Transform::ApplyPerspectiveDepth(SkMScalar depth) { |
188 if (depth == 0) | 193 if (depth == 0) |
189 return; | 194 return; |
190 if (matrix_.isIdentity()) | 195 if (matrix_.isIdentity()) |
191 matrix_.setDouble(3, 2, -1.0 / depth); | 196 matrix_.set(3, 2, -1.0 / depth); |
192 else { | 197 else { |
193 SkMatrix44 m(SkMatrix44::kIdentity_Constructor); | 198 SkMatrix44 m(SkMatrix44::kIdentity_Constructor); |
194 m.setDouble(3, 2, -1.0 / depth); | 199 m.set(3, 2, -1.0 / depth); |
195 matrix_.preConcat(m); | 200 matrix_.preConcat(m); |
196 } | 201 } |
197 } | 202 } |
198 | 203 |
199 void Transform::PreconcatTransform(const Transform& transform) { | 204 void Transform::PreconcatTransform(const Transform& transform) { |
200 matrix_.preConcat(transform.matrix_); | 205 matrix_.preConcat(transform.matrix_); |
201 } | 206 } |
202 | 207 |
203 void Transform::ConcatTransform(const Transform& transform) { | 208 void Transform::ConcatTransform(const Transform& transform) { |
204 matrix_.postConcat(transform.matrix_); | 209 matrix_.postConcat(transform.matrix_); |
205 } | 210 } |
206 | 211 |
207 bool Transform::IsIdentityOrIntegerTranslation() const { | 212 bool Transform::IsIdentityOrIntegerTranslation() const { |
208 if (!IsIdentityOrTranslation()) | 213 if (!IsIdentityOrTranslation()) |
209 return false; | 214 return false; |
210 | 215 |
211 bool no_fractional_translation = | 216 bool no_fractional_translation = |
212 static_cast<int>(matrix_.getDouble(0, 3)) == matrix_.getDouble(0, 3) && | 217 static_cast<int>(matrix_.get(0, 3)) == matrix_.get(0, 3) && |
213 static_cast<int>(matrix_.getDouble(1, 3)) == matrix_.getDouble(1, 3) && | 218 static_cast<int>(matrix_.get(1, 3)) == matrix_.get(1, 3) && |
214 static_cast<int>(matrix_.getDouble(2, 3)) == matrix_.getDouble(2, 3); | 219 static_cast<int>(matrix_.get(2, 3)) == matrix_.get(2, 3); |
215 | 220 |
216 return no_fractional_translation; | 221 return no_fractional_translation; |
217 } | 222 } |
218 | 223 |
219 bool Transform::IsBackFaceVisible() const { | 224 bool Transform::IsBackFaceVisible() const { |
220 // Compute whether a layer with a forward-facing normal of (0, 0, 1, 0) | 225 // Compute whether a layer with a forward-facing normal of (0, 0, 1, 0) |
221 // would have its back face visible after applying the transform. | 226 // would have its back face visible after applying the transform. |
222 if (matrix_.isIdentity()) | 227 if (matrix_.isIdentity()) |
223 return false; | 228 return false; |
224 | 229 |
(...skipping 12 matching lines...) Expand all Loading... | |
237 // | 242 // |
238 | 243 |
239 double determinant = matrix_.determinant(); | 244 double determinant = matrix_.determinant(); |
240 | 245 |
241 // If matrix was not invertible, then just assume back face is not visible. | 246 // If matrix was not invertible, then just assume back face is not visible. |
242 if (std::abs(determinant) <= kEpsilon) | 247 if (std::abs(determinant) <= kEpsilon) |
243 return false; | 248 return false; |
244 | 249 |
245 // Compute the cofactor of the 3rd row, 3rd column. | 250 // Compute the cofactor of the 3rd row, 3rd column. |
246 double cofactor_part_1 = | 251 double cofactor_part_1 = |
247 matrix_.getDouble(0, 0) * | 252 matrix_.get(0, 0) * matrix_.get(1, 1) * matrix_.get(3, 3); |
248 matrix_.getDouble(1, 1) * | |
249 matrix_.getDouble(3, 3); | |
250 | 253 |
251 double cofactor_part_2 = | 254 double cofactor_part_2 = |
252 matrix_.getDouble(0, 1) * | 255 matrix_.get(0, 1) * matrix_.get(1, 3) * matrix_.get(3, 0); |
253 matrix_.getDouble(1, 3) * | |
254 matrix_.getDouble(3, 0); | |
255 | 256 |
256 double cofactor_part_3 = | 257 double cofactor_part_3 = |
257 matrix_.getDouble(0, 3) * | 258 matrix_.get(0, 3) * matrix_.get(1, 0) * matrix_.get(3, 1); |
258 matrix_.getDouble(1, 0) * | |
259 matrix_.getDouble(3, 1); | |
260 | 259 |
261 double cofactor_part_4 = | 260 double cofactor_part_4 = |
262 matrix_.getDouble(0, 0) * | 261 matrix_.get(0, 0) * matrix_.get(1, 3) * matrix_.get(3, 1); |
263 matrix_.getDouble(1, 3) * | |
264 matrix_.getDouble(3, 1); | |
265 | 262 |
266 double cofactor_part_5 = | 263 double cofactor_part_5 = |
267 matrix_.getDouble(0, 1) * | 264 matrix_.get(0, 1) * matrix_.get(1, 0) * matrix_.get(3, 3); |
268 matrix_.getDouble(1, 0) * | |
269 matrix_.getDouble(3, 3); | |
270 | 265 |
271 double cofactor_part_6 = | 266 double cofactor_part_6 = |
272 matrix_.getDouble(0, 3) * | 267 matrix_.get(0, 3) * matrix_.get(1, 1) * matrix_.get(3, 0); |
273 matrix_.getDouble(1, 1) * | |
274 matrix_.getDouble(3, 0); | |
275 | 268 |
276 double cofactor33 = | 269 double cofactor33 = |
277 cofactor_part_1 + | 270 cofactor_part_1 + |
278 cofactor_part_2 + | 271 cofactor_part_2 + |
279 cofactor_part_3 - | 272 cofactor_part_3 - |
280 cofactor_part_4 - | 273 cofactor_part_4 - |
281 cofactor_part_5 - | 274 cofactor_part_5 - |
282 cofactor_part_6; | 275 cofactor_part_6; |
283 | 276 |
284 // Technically the transformed z component is cofactor33 / determinant. But | 277 // Technically the transformed z component is cofactor33 / determinant. But |
(...skipping 25 matching lines...) Expand all Loading... | |
310 // anyway. For the inner 2x2 portion, the only effects that keep a rect axis | 303 // anyway. For the inner 2x2 portion, the only effects that keep a rect axis |
311 // aligned are (1) swapping axes and (2) scaling axes. This can be checked by | 304 // aligned are (1) swapping axes and (2) scaling axes. This can be checked by |
312 // verifying only 1 element of every column and row is non-zero. Degenerate | 305 // verifying only 1 element of every column and row is non-zero. Degenerate |
313 // cases that project the x or y dimension to zero are considered to preserve | 306 // cases that project the x or y dimension to zero are considered to preserve |
314 // axis alignment. | 307 // axis alignment. |
315 // | 308 // |
316 // If the matrix does have perspective component that is affected by x or y | 309 // If the matrix does have perspective component that is affected by x or y |
317 // values: The current implementation conservatively assumes that axis | 310 // values: The current implementation conservatively assumes that axis |
318 // alignment is not preserved. | 311 // alignment is not preserved. |
319 | 312 |
320 bool has_x_or_y_perspective = matrix_.getDouble(3, 0) != 0 || | 313 bool has_x_or_y_perspective = |
321 matrix_.getDouble(3, 1) != 0; | 314 matrix_.get(3, 0) != 0 || matrix_.get(3, 1) != 0; |
322 | 315 |
323 int num_non_zero_in_row_0 = 0; | 316 int num_non_zero_in_row_0 = 0; |
324 int num_non_zero_in_row_1 = 0; | 317 int num_non_zero_in_row_1 = 0; |
325 int num_non_zero_in_col_0 = 0; | 318 int num_non_zero_in_col_0 = 0; |
326 int num_non_zero_in_col_1 = 0; | 319 int num_non_zero_in_col_1 = 0; |
327 | 320 |
328 if (std::abs(matrix_.getDouble(0, 0)) > kEpsilon) { | 321 if (std::abs(matrix_.get(0, 0)) > kEpsilon) { |
329 num_non_zero_in_row_0++; | 322 num_non_zero_in_row_0++; |
330 num_non_zero_in_col_0++; | 323 num_non_zero_in_col_0++; |
331 } | 324 } |
332 | 325 |
333 if (std::abs(matrix_.getDouble(0, 1)) > kEpsilon) { | 326 if (std::abs(matrix_.get(0, 1)) > kEpsilon) { |
334 num_non_zero_in_row_0++; | 327 num_non_zero_in_row_0++; |
335 num_non_zero_in_col_1++; | 328 num_non_zero_in_col_1++; |
336 } | 329 } |
337 | 330 |
338 if (std::abs(matrix_.getDouble(1, 0)) > kEpsilon) { | 331 if (std::abs(matrix_.get(1, 0)) > kEpsilon) { |
339 num_non_zero_in_row_1++; | 332 num_non_zero_in_row_1++; |
340 num_non_zero_in_col_0++; | 333 num_non_zero_in_col_0++; |
341 } | 334 } |
342 | 335 |
343 if (std::abs(matrix_.getDouble(1, 1)) > kEpsilon) { | 336 if (std::abs(matrix_.get(1, 1)) > kEpsilon) { |
344 num_non_zero_in_row_1++; | 337 num_non_zero_in_row_1++; |
345 num_non_zero_in_col_1++; | 338 num_non_zero_in_col_1++; |
346 } | 339 } |
347 | 340 |
348 return | 341 return |
349 num_non_zero_in_row_0 <= 1 && | 342 num_non_zero_in_row_0 <= 1 && |
350 num_non_zero_in_row_1 <= 1 && | 343 num_non_zero_in_row_1 <= 1 && |
351 num_non_zero_in_col_0 <= 1 && | 344 num_non_zero_in_col_0 <= 1 && |
352 num_non_zero_in_col_1 <= 1 && | 345 num_non_zero_in_col_1 <= 1 && |
353 !has_x_or_y_perspective; | 346 !has_x_or_y_perspective; |
354 } | 347 } |
355 | 348 |
356 void Transform::Transpose() { | 349 void Transform::Transpose() { |
357 matrix_.transpose(); | 350 matrix_.transpose(); |
358 } | 351 } |
359 | 352 |
360 void Transform::FlattenTo2d() { | 353 void Transform::FlattenTo2d() { |
361 matrix_.setDouble(2, 0, 0.0); | 354 matrix_.set(2, 0, 0.0); |
362 matrix_.setDouble(2, 1, 0.0); | 355 matrix_.set(2, 1, 0.0); |
363 matrix_.setDouble(0, 2, 0.0); | 356 matrix_.set(0, 2, 0.0); |
364 matrix_.setDouble(1, 2, 0.0); | 357 matrix_.set(1, 2, 0.0); |
365 matrix_.setDouble(2, 2, 1.0); | 358 matrix_.set(2, 2, 1.0); |
366 matrix_.setDouble(3, 2, 0.0); | 359 matrix_.set(3, 2, 0.0); |
367 matrix_.setDouble(2, 3, 0.0); | 360 matrix_.set(2, 3, 0.0); |
368 } | 361 } |
369 | 362 |
370 Vector2dF Transform::To2dTranslation() const { | 363 Vector2dF Transform::To2dTranslation() const { |
371 DCHECK(IsIdentityOrTranslation()); | 364 DCHECK(IsIdentityOrTranslation()); |
372 // Ensure that this translation is truly 2d. | 365 // Ensure that this translation is truly 2d. |
373 const double translate_z = matrix_.getDouble(2, 3); | 366 const double translate_z = matrix_.get(2, 3); |
374 DCHECK_EQ(0.0, translate_z); | 367 DCHECK_EQ(0.0, translate_z); |
375 return gfx::Vector2dF(matrix_.getDouble(0, 3), matrix_.getDouble(1, 3)); | 368 return gfx::Vector2dF(matrix_.get(0, 3), matrix_.get(1, 3)); |
376 } | 369 } |
377 | 370 |
378 void Transform::TransformPoint(Point& point) const { | 371 void Transform::TransformPoint(Point& point) const { |
379 TransformPointInternal(matrix_, point); | 372 TransformPointInternal(matrix_, point); |
380 } | 373 } |
381 | 374 |
382 void Transform::TransformPoint(Point3F& point) const { | 375 void Transform::TransformPoint(Point3F& point) const { |
383 TransformPointInternal(matrix_, point); | 376 TransformPointInternal(matrix_, point); |
384 } | 377 } |
385 | 378 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
421 if (!matrix_.invert(&inverse)) | 414 if (!matrix_.invert(&inverse)) |
422 return false; | 415 return false; |
423 | 416 |
424 const SkMatrix& matrix = inverse; | 417 const SkMatrix& matrix = inverse; |
425 SkRect src = RectFToSkRect(*rect); | 418 SkRect src = RectFToSkRect(*rect); |
426 matrix.mapRect(&src); | 419 matrix.mapRect(&src); |
427 *rect = SkRectToRectF(src); | 420 *rect = SkRectToRectF(src); |
428 return true; | 421 return true; |
429 } | 422 } |
430 | 423 |
431 bool Transform::Blend(const Transform& from, double progress) { | 424 bool Transform::Blend(const Transform& from, SkMScalar progress) { |
432 DecomposedTransform to_decomp; | 425 DecomposedTransform to_decomp; |
433 DecomposedTransform from_decomp; | 426 DecomposedTransform from_decomp; |
434 if (!DecomposeTransform(&to_decomp, *this) || | 427 if (!DecomposeTransform(&to_decomp, *this) || |
435 !DecomposeTransform(&from_decomp, from)) | 428 !DecomposeTransform(&from_decomp, from)) |
436 return false; | 429 return false; |
437 | 430 |
438 if (!BlendDecomposedTransforms(&to_decomp, to_decomp, from_decomp, progress)) | 431 if (!BlendDecomposedTransforms(&to_decomp, to_decomp, from_decomp, progress)) |
439 return false; | 432 return false; |
440 | 433 |
441 matrix_ = ComposeTransform(to_decomp).matrix(); | 434 matrix_ = ComposeTransform(to_decomp).matrix(); |
442 return true; | 435 return true; |
443 } | 436 } |
444 | 437 |
445 void Transform::TransformPointInternal(const SkMatrix44& xform, | 438 void Transform::TransformPointInternal(const SkMatrix44& xform, |
446 Point3F& point) const { | 439 Point3F& point) const { |
447 if (xform.isIdentity()) | 440 if (xform.isIdentity()) |
448 return; | 441 return; |
449 | 442 |
450 SkMScalar p[4] = { | 443 SkMScalar p[4] = {SkFloatToMScalar(point.x()), SkFloatToMScalar(point.y()), |
451 SkDoubleToMScalar(point.x()), | 444 SkFloatToMScalar(point.z()), 1}; |
452 SkDoubleToMScalar(point.y()), | |
453 SkDoubleToMScalar(point.z()), | |
454 SkDoubleToMScalar(1) | |
455 }; | |
456 | 445 |
457 xform.mapMScalars(p); | 446 xform.mapMScalars(p); |
458 | 447 |
459 if (p[3] != 1 && abs(p[3]) > 0) { | 448 if (p[3] != 1 && abs(p[3]) > 0) { |
460 point.SetPoint(p[0] / p[3], p[1] / p[3], p[2]/ p[3]); | 449 point.SetPoint(p[0] / p[3], p[1] / p[3], p[2]/ p[3]); |
461 } else { | 450 } else { |
462 point.SetPoint(p[0], p[1], p[2]); | 451 point.SetPoint(p[0], p[1], p[2]); |
463 } | 452 } |
464 } | 453 } |
465 | 454 |
466 void Transform::TransformPointInternal(const SkMatrix44& xform, | 455 void Transform::TransformPointInternal(const SkMatrix44& xform, |
467 Point& point) const { | 456 Point& point) const { |
468 if (xform.isIdentity()) | 457 if (xform.isIdentity()) |
469 return; | 458 return; |
470 | 459 |
471 SkMScalar p[4] = { | 460 SkMScalar p[4] = {SkFloatToMScalar(point.x()), SkFloatToMScalar(point.y()), 0, |
472 SkDoubleToMScalar(point.x()), | 461 1}; |
473 SkDoubleToMScalar(point.y()), | |
474 SkDoubleToMScalar(0), | |
475 SkDoubleToMScalar(1) | |
476 }; | |
477 | 462 |
478 xform.mapMScalars(p); | 463 xform.mapMScalars(p); |
479 | 464 |
480 point.SetPoint(ToRoundedInt(p[0]), ToRoundedInt(p[1])); | 465 point.SetPoint(ToRoundedInt(p[0]), ToRoundedInt(p[1])); |
481 } | 466 } |
482 | 467 |
483 std::string Transform::ToString() const { | 468 std::string Transform::ToString() const { |
484 return base::StringPrintf( | 469 return base::StringPrintf( |
485 "[ %+0.4f %+0.4f %+0.4f %+0.4f \n" | 470 "[ %+0.4f %+0.4f %+0.4f %+0.4f \n" |
486 " %+0.4f %+0.4f %+0.4f %+0.4f \n" | 471 " %+0.4f %+0.4f %+0.4f %+0.4f \n" |
487 " %+0.4f %+0.4f %+0.4f %+0.4f \n" | 472 " %+0.4f %+0.4f %+0.4f %+0.4f \n" |
488 " %+0.4f %+0.4f %+0.4f %+0.4f ]\n", | 473 " %+0.4f %+0.4f %+0.4f %+0.4f ]\n", |
489 matrix_.getDouble(0, 0), | 474 matrix_.get(0, 0), |
490 matrix_.getDouble(0, 1), | 475 matrix_.get(0, 1), |
491 matrix_.getDouble(0, 2), | 476 matrix_.get(0, 2), |
492 matrix_.getDouble(0, 3), | 477 matrix_.get(0, 3), |
493 matrix_.getDouble(1, 0), | 478 matrix_.get(1, 0), |
494 matrix_.getDouble(1, 1), | 479 matrix_.get(1, 1), |
495 matrix_.getDouble(1, 2), | 480 matrix_.get(1, 2), |
496 matrix_.getDouble(1, 3), | 481 matrix_.get(1, 3), |
497 matrix_.getDouble(2, 0), | 482 matrix_.get(2, 0), |
498 matrix_.getDouble(2, 1), | 483 matrix_.get(2, 1), |
499 matrix_.getDouble(2, 2), | 484 matrix_.get(2, 2), |
500 matrix_.getDouble(2, 3), | 485 matrix_.get(2, 3), |
501 matrix_.getDouble(3, 0), | 486 matrix_.get(3, 0), |
502 matrix_.getDouble(3, 1), | 487 matrix_.get(3, 1), |
503 matrix_.getDouble(3, 2), | 488 matrix_.get(3, 2), |
504 matrix_.getDouble(3, 3)); | 489 matrix_.get(3, 3)); |
505 } | 490 } |
506 | 491 |
507 } // namespace gfx | 492 } // namespace gfx |
OLD | NEW |