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/interpolated_transform.h" | 5 #include "ui/gfx/interpolated_transform.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #ifndef M_PI | 9 #ifndef M_PI |
10 #define M_PI 3.14159265358979323846 | 10 #define M_PI 3.14159265358979323846 |
11 #endif | 11 #endif |
12 | 12 |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "ui/base/animation/tween.h" | 14 #include "ui/base/animation/tween.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 static const float EPSILON = 1e-6f; | 18 static const float EPSILON = 1e-6f; |
19 | 19 |
20 bool IsAMultipleOfNinetyDegrees(float degrees) | |
James Cook
2012/04/24 17:18:18
nit: Maybe just IsMultipleOfNinetyDegrees()?
| |
21 { | |
22 float remainder = fabs(fmod(degrees, 90.0f)); | |
23 return remainder < EPSILON || 90.0f - remainder < EPSILON; | |
24 } | |
25 | |
26 // Returns false if |degrees| is not a multiple of ninety degrees or if | |
27 // |rotation| is NULL. It does not affect |rotation| in this case. Otherwise | |
28 // *rotation is set to be the appropriate sanitized rotation matrix. That is, | |
29 // the rotation matrix corresponding to |degrees| which has entries that are all | |
30 // either 0, 1 or -1. | |
31 bool MassageRotationIfMultipleOfNinetyDegrees(ui::Transform* rotation, | |
32 float degrees) | |
33 { | |
34 if (!IsAMultipleOfNinetyDegrees(degrees) || !rotation) | |
35 return false; | |
36 | |
37 ui::Transform transform; | |
38 transform.SetRotate(degrees); | |
39 SkMatrix44& m = transform.matrix(); | |
40 | |
41 // A rotation that is a multiple of ninety degrees will have entries that are | |
42 // either 0, 1, or -1. | |
43 for (int row = 0; row < 4; ++row) { | |
44 for (int col = 0; col < 4; ++col) { | |
45 float entry = m.get(row, col); | |
46 if (fabs(entry) < EPSILON) | |
piman
2012/04/24 17:15:43
I worry that we may miss this, especially for larg
| |
47 entry = 0; | |
48 else if (fabs(entry + 1) < EPSILON) | |
49 entry = -1; | |
50 else if (fabs(entry - 1) < EPSILON) | |
51 entry = 1; | |
52 else | |
53 NOTREACHED(); | |
54 m.set(row, col, entry); | |
55 } | |
56 } | |
57 | |
58 *rotation = transform; | |
59 return true; | |
60 } | |
61 | |
20 } // namespace | 62 } // namespace |
21 | 63 |
22 namespace ui { | 64 namespace ui { |
23 | 65 |
24 /////////////////////////////////////////////////////////////////////////////// | 66 /////////////////////////////////////////////////////////////////////////////// |
25 // InterpolatedTransform | 67 // InterpolatedTransform |
26 // | 68 // |
27 | 69 |
28 InterpolatedTransform::InterpolatedTransform() | 70 InterpolatedTransform::InterpolatedTransform() |
29 : start_time_(0.0f), | 71 : start_time_(0.0f), |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 float end_time) | 192 float end_time) |
151 : InterpolatedTransform(start_time, end_time), | 193 : InterpolatedTransform(start_time, end_time), |
152 start_degrees_(start_degrees), | 194 start_degrees_(start_degrees), |
153 end_degrees_(end_degrees) { | 195 end_degrees_(end_degrees) { |
154 } | 196 } |
155 | 197 |
156 InterpolatedRotation::~InterpolatedRotation() {} | 198 InterpolatedRotation::~InterpolatedRotation() {} |
157 | 199 |
158 ui::Transform InterpolatedRotation::InterpolateButDoNotCompose(float t) const { | 200 ui::Transform InterpolatedRotation::InterpolateButDoNotCompose(float t) const { |
159 ui::Transform result; | 201 ui::Transform result; |
160 result.SetRotate(ValueBetween(t, start_degrees_, end_degrees_)); | 202 float interpolated_degrees = ValueBetween(t, start_degrees_, end_degrees_); |
203 result.SetRotate(interpolated_degrees); | |
204 MassageRotationIfMultipleOfNinetyDegrees(&result, interpolated_degrees); | |
James Cook
2012/04/24 17:18:18
Do we want to always do this or just when t == 0 o
| |
161 return result; | 205 return result; |
162 } | 206 } |
163 | 207 |
164 /////////////////////////////////////////////////////////////////////////////// | 208 /////////////////////////////////////////////////////////////////////////////// |
165 // InterpolatedAxisAngleRotation | 209 // InterpolatedAxisAngleRotation |
166 // | 210 // |
167 | 211 |
168 InterpolatedAxisAngleRotation::InterpolatedAxisAngleRotation( | 212 InterpolatedAxisAngleRotation::InterpolatedAxisAngleRotation( |
169 gfx::Point3f axis, | 213 gfx::Point3f axis, |
170 float start_degrees, | 214 float start_degrees, |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
261 } | 305 } |
262 | 306 |
263 InterpolatedTranslation::~InterpolatedTranslation() {} | 307 InterpolatedTranslation::~InterpolatedTranslation() {} |
264 | 308 |
265 ui::Transform | 309 ui::Transform |
266 InterpolatedTranslation::InterpolateButDoNotCompose(float t) const { | 310 InterpolatedTranslation::InterpolateButDoNotCompose(float t) const { |
267 ui::Transform result; | 311 ui::Transform result; |
268 // TODO(vollick) 3d xforms. | 312 // TODO(vollick) 3d xforms. |
269 result.SetTranslate(ValueBetween(t, start_pos_.x(), end_pos_.x()), | 313 result.SetTranslate(ValueBetween(t, start_pos_.x(), end_pos_.x()), |
270 ValueBetween(t, start_pos_.y(), end_pos_.y())); | 314 ValueBetween(t, start_pos_.y(), end_pos_.y())); |
271 | |
272 return result; | 315 return result; |
273 } | 316 } |
274 | 317 |
275 /////////////////////////////////////////////////////////////////////////////// | 318 /////////////////////////////////////////////////////////////////////////////// |
276 // InterpolatedConstantTransform | 319 // InterpolatedConstantTransform |
277 // | 320 // |
278 | 321 |
279 InterpolatedConstantTransform::InterpolatedConstantTransform( | 322 InterpolatedConstantTransform::InterpolatedConstantTransform( |
280 const ui::Transform& transform) | 323 const ui::Transform& transform) |
281 : InterpolatedTransform(), | 324 : InterpolatedTransform(), |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
389 | 432 |
390 rotation->SetChild(translation.release()); | 433 rotation->SetChild(translation.release()); |
391 scale->SetChild(rotation.release()); | 434 scale->SetChild(rotation.release()); |
392 transform_.reset(scale.release()); | 435 transform_.reset(scale.release()); |
393 } else { | 436 } else { |
394 transform_.reset(new InterpolatedConstantTransform(end_transform)); | 437 transform_.reset(new InterpolatedConstantTransform(end_transform)); |
395 } | 438 } |
396 } | 439 } |
397 | 440 |
398 } // namespace ui | 441 } // namespace ui |
OLD | NEW |