OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <cmath> | 5 #include <cmath> |
6 #include <limits> | 6 #include <limits> |
7 | 7 |
8 #include "cc/animation/transform_operation.h" | 8 #include "cc/animation/transform_operation.h" |
9 #include "ui/gfx/vector3d_f.h" | 9 #include "ui/gfx/vector3d_f.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 const double kAngleEpsilon = 1e-4; | 12 const double kAngleEpsilon = 1e-4; |
13 } | 13 } |
14 | 14 |
15 namespace cc { | 15 namespace cc { |
16 | 16 |
17 bool TransformOperation::IsIdentity() const { | 17 bool TransformOperation::IsIdentity() const { |
18 return matrix.IsIdentity(); | 18 return matrix.IsIdentity(); |
19 } | 19 } |
20 | 20 |
21 static bool IsOperationIdentity(const TransformOperation* operation) { | 21 static bool IsOperationIdentity(const TransformOperation* operation) { |
22 return !operation || operation->IsIdentity(); | 22 return !operation || operation->IsIdentity(); |
23 } | 23 } |
24 | 24 |
25 static bool ShareSameAxis(const TransformOperation* from, | 25 static bool ShareSameAxis(const TransformOperation* from, |
26 const TransformOperation* to, | 26 const TransformOperation* to, |
27 double& axis_x, double& axis_y, double& axis_z, | 27 double* axis_x, |
28 double& angle_from) { | 28 double* axis_y, |
| 29 double* axis_z, |
| 30 double* angle_from) { |
29 if (IsOperationIdentity(from) && IsOperationIdentity(to)) | 31 if (IsOperationIdentity(from) && IsOperationIdentity(to)) |
30 return false; | 32 return false; |
31 | 33 |
32 if (IsOperationIdentity(from) && !IsOperationIdentity(to)) { | 34 if (IsOperationIdentity(from) && !IsOperationIdentity(to)) { |
33 axis_x = to->rotate.axis.x; | 35 *axis_x = to->rotate.axis.x; |
34 axis_y = to->rotate.axis.y; | 36 *axis_y = to->rotate.axis.y; |
35 axis_z = to->rotate.axis.z; | 37 *axis_z = to->rotate.axis.z; |
36 angle_from = 0; | 38 *angle_from = 0; |
37 return true; | 39 return true; |
38 } | 40 } |
39 | 41 |
40 if (!IsOperationIdentity(from) && IsOperationIdentity(to)) { | 42 if (!IsOperationIdentity(from) && IsOperationIdentity(to)) { |
41 axis_x = from->rotate.axis.x; | 43 *axis_x = from->rotate.axis.x; |
42 axis_y = from->rotate.axis.y; | 44 *axis_y = from->rotate.axis.y; |
43 axis_z = from->rotate.axis.z; | 45 *axis_z = from->rotate.axis.z; |
44 angle_from = from->rotate.angle; | 46 *angle_from = from->rotate.angle; |
45 return true; | 47 return true; |
46 } | 48 } |
47 | 49 |
48 double length_2 = from->rotate.axis.x * from->rotate.axis.x + | 50 double length_2 = from->rotate.axis.x * from->rotate.axis.x + |
49 from->rotate.axis.y * from->rotate.axis.y + | 51 from->rotate.axis.y * from->rotate.axis.y + |
50 from->rotate.axis.z * from->rotate.axis.z; | 52 from->rotate.axis.z * from->rotate.axis.z; |
51 double other_length_2 = to->rotate.axis.x * to->rotate.axis.x + | 53 double other_length_2 = to->rotate.axis.x * to->rotate.axis.x + |
52 to->rotate.axis.y * to->rotate.axis.y + | 54 to->rotate.axis.y * to->rotate.axis.y + |
53 to->rotate.axis.z * to->rotate.axis.z; | 55 to->rotate.axis.z * to->rotate.axis.z; |
54 | 56 |
55 if (length_2 <= kAngleEpsilon || other_length_2 <= kAngleEpsilon) | 57 if (length_2 <= kAngleEpsilon || other_length_2 <= kAngleEpsilon) |
56 return false; | 58 return false; |
57 | 59 |
58 double dot = to->rotate.axis.x * from->rotate.axis.x + | 60 double dot = to->rotate.axis.x * from->rotate.axis.x + |
59 to->rotate.axis.y * from->rotate.axis.y + | 61 to->rotate.axis.y * from->rotate.axis.y + |
60 to->rotate.axis.z * from->rotate.axis.z; | 62 to->rotate.axis.z * from->rotate.axis.z; |
61 double error = std::fabs(1.0 - (dot * dot) / (length_2 * other_length_2)); | 63 double error = std::fabs(1.0 - (dot * dot) / (length_2 * other_length_2)); |
62 bool result = error < kAngleEpsilon; | 64 bool result = error < kAngleEpsilon; |
63 if (result) { | 65 if (result) { |
64 axis_x = to->rotate.axis.x; | 66 *axis_x = to->rotate.axis.x; |
65 axis_y = to->rotate.axis.y; | 67 *axis_y = to->rotate.axis.y; |
66 axis_z = to->rotate.axis.z; | 68 *axis_z = to->rotate.axis.z; |
67 // If the axes are pointing in opposite directions, we need to reverse | 69 // If the axes are pointing in opposite directions, we need to reverse |
68 // the angle. | 70 // the angle. |
69 angle_from = dot > 0 ? from->rotate.angle : -from->rotate.angle; | 71 *angle_from = dot > 0 ? from->rotate.angle : -from->rotate.angle; |
70 } | 72 } |
71 return result; | 73 return result; |
72 } | 74 } |
73 | 75 |
74 static double BlendDoubles(double from, double to, double progress) { | 76 static double BlendDoubles(double from, double to, double progress) { |
75 if (progress <= 0.0) | 77 if (progress <= 0.0) |
76 return from; | 78 return from; |
77 | 79 |
78 if (progress >= 1.0) | 80 if (progress >= 1.0) |
79 return to; | 81 return to; |
80 | 82 |
81 return from * (1 - progress) + to * progress; | 83 return from * (1 - progress) + to * progress; |
82 } | 84 } |
83 | 85 |
84 bool TransformOperation::BlendTransformOperations( | 86 bool TransformOperation::BlendTransformOperations( |
85 const TransformOperation* from, | 87 const TransformOperation* from, |
86 const TransformOperation* to, | 88 const TransformOperation* to, |
87 double progress, | 89 double progress, |
88 gfx::Transform& result) { | 90 gfx::Transform* result) { |
89 if (IsOperationIdentity(from) && IsOperationIdentity(to)) | 91 if (IsOperationIdentity(from) && IsOperationIdentity(to)) |
90 return true; | 92 return true; |
91 | 93 |
92 TransformOperation::Type interpolation_type = | 94 TransformOperation::Type interpolation_type = |
93 TransformOperation::TransformOperationIdentity; | 95 TransformOperation::TransformOperationIdentity; |
94 if (IsOperationIdentity(to)) | 96 if (IsOperationIdentity(to)) |
95 interpolation_type = from->type; | 97 interpolation_type = from->type; |
96 else | 98 else |
97 interpolation_type = to->type; | 99 interpolation_type = to->type; |
98 | 100 |
99 switch (interpolation_type) { | 101 switch (interpolation_type) { |
100 case TransformOperation::TransformOperationTranslate: { | 102 case TransformOperation::TransformOperationTranslate: { |
101 double from_x = IsOperationIdentity(from) ? 0 : from->translate.x; | 103 double from_x = IsOperationIdentity(from) ? 0 : from->translate.x; |
102 double from_y = IsOperationIdentity(from) ? 0 : from->translate.y; | 104 double from_y = IsOperationIdentity(from) ? 0 : from->translate.y; |
103 double from_z = IsOperationIdentity(from) ? 0 : from->translate.z; | 105 double from_z = IsOperationIdentity(from) ? 0 : from->translate.z; |
104 double to_x = IsOperationIdentity(to) ? 0 : to->translate.x; | 106 double to_x = IsOperationIdentity(to) ? 0 : to->translate.x; |
105 double to_y = IsOperationIdentity(to) ? 0 : to->translate.y; | 107 double to_y = IsOperationIdentity(to) ? 0 : to->translate.y; |
106 double to_z = IsOperationIdentity(to) ? 0 : to->translate.z; | 108 double to_z = IsOperationIdentity(to) ? 0 : to->translate.z; |
107 result.Translate3d(BlendDoubles(from_x, to_x, progress), | 109 result->Translate3d(BlendDoubles(from_x, to_x, progress), |
108 BlendDoubles(from_y, to_y, progress), | 110 BlendDoubles(from_y, to_y, progress), |
109 BlendDoubles(from_z, to_z, progress)); | 111 BlendDoubles(from_z, to_z, progress)); |
110 break; | 112 break; |
111 } | 113 } |
112 case TransformOperation::TransformOperationRotate: { | 114 case TransformOperation::TransformOperationRotate: { |
113 double axis_x = 0; | 115 double axis_x = 0; |
114 double axis_y = 0; | 116 double axis_y = 0; |
115 double axis_z = 1; | 117 double axis_z = 1; |
116 double from_angle = 0; | 118 double from_angle = 0; |
117 double to_angle = IsOperationIdentity(to) ? 0 : to->rotate.angle; | 119 double to_angle = IsOperationIdentity(to) ? 0 : to->rotate.angle; |
118 if (ShareSameAxis(from, to, axis_x, axis_y, axis_z, from_angle)) { | 120 if (ShareSameAxis(from, to, &axis_x, &axis_y, &axis_z, &from_angle)) |
119 result.RotateAbout(gfx::Vector3dF(axis_x, axis_y, axis_z), | 121 result->RotateAbout(gfx::Vector3dF(axis_x, axis_y, axis_z), |
120 BlendDoubles(from_angle, to_angle, progress)); | 122 BlendDoubles(from_angle, to_angle, progress)); |
121 } else { | 123 else { |
122 gfx::Transform to_matrix; | 124 gfx::Transform to_matrix; |
123 if (!IsOperationIdentity(to)) | 125 if (!IsOperationIdentity(to)) |
124 to_matrix = to->matrix; | 126 to_matrix = to->matrix; |
125 gfx::Transform from_matrix; | 127 gfx::Transform from_matrix; |
126 if (!IsOperationIdentity(from)) | 128 if (!IsOperationIdentity(from)) |
127 from_matrix = from->matrix; | 129 from_matrix = from->matrix; |
128 result = to_matrix; | 130 *result = to_matrix; |
129 if (!result.Blend(from_matrix, progress)) | 131 if (!result->Blend(from_matrix, progress)) |
130 return false; | 132 return false; |
131 } | 133 } |
132 break; | 134 break; |
133 } | 135 } |
134 case TransformOperation::TransformOperationScale: { | 136 case TransformOperation::TransformOperationScale: { |
135 double from_x = IsOperationIdentity(from) ? 1 : from->scale.x; | 137 double from_x = IsOperationIdentity(from) ? 1 : from->scale.x; |
136 double from_y = IsOperationIdentity(from) ? 1 : from->scale.y; | 138 double from_y = IsOperationIdentity(from) ? 1 : from->scale.y; |
137 double from_z = IsOperationIdentity(from) ? 1 : from->scale.z; | 139 double from_z = IsOperationIdentity(from) ? 1 : from->scale.z; |
138 double to_x = IsOperationIdentity(to) ? 1 : to->scale.x; | 140 double to_x = IsOperationIdentity(to) ? 1 : to->scale.x; |
139 double to_y = IsOperationIdentity(to) ? 1 : to->scale.y; | 141 double to_y = IsOperationIdentity(to) ? 1 : to->scale.y; |
140 double to_z = IsOperationIdentity(to) ? 1 : to->scale.z; | 142 double to_z = IsOperationIdentity(to) ? 1 : to->scale.z; |
141 result.Scale3d(BlendDoubles(from_x, to_x, progress), | 143 result->Scale3d(BlendDoubles(from_x, to_x, progress), |
142 BlendDoubles(from_y, to_y, progress), | 144 BlendDoubles(from_y, to_y, progress), |
143 BlendDoubles(from_z, to_z, progress)); | 145 BlendDoubles(from_z, to_z, progress)); |
144 break; | 146 break; |
145 } | 147 } |
146 case TransformOperation::TransformOperationSkew: { | 148 case TransformOperation::TransformOperationSkew: { |
147 double from_x = IsOperationIdentity(from) ? 0 : from->skew.x; | 149 double from_x = IsOperationIdentity(from) ? 0 : from->skew.x; |
148 double from_y = IsOperationIdentity(from) ? 0 : from->skew.y; | 150 double from_y = IsOperationIdentity(from) ? 0 : from->skew.y; |
149 double to_x = IsOperationIdentity(to) ? 0 : to->skew.x; | 151 double to_x = IsOperationIdentity(to) ? 0 : to->skew.x; |
150 double to_y = IsOperationIdentity(to) ? 0 : to->skew.y; | 152 double to_y = IsOperationIdentity(to) ? 0 : to->skew.y; |
151 result.SkewX(BlendDoubles(from_x, to_x, progress)); | 153 result->SkewX(BlendDoubles(from_x, to_x, progress)); |
152 result.SkewY(BlendDoubles(from_y, to_y, progress)); | 154 result->SkewY(BlendDoubles(from_y, to_y, progress)); |
153 break; | 155 break; |
154 } | 156 } |
155 case TransformOperation::TransformOperationPerspective: { | 157 case TransformOperation::TransformOperationPerspective: { |
156 double from_perspective_depth = IsOperationIdentity(from) ? | 158 double from_perspective_depth = IsOperationIdentity(from) ? |
157 std::numeric_limits<double>::max() : from->perspective_depth; | 159 std::numeric_limits<double>::max() : from->perspective_depth; |
158 double to_perspective_depth = IsOperationIdentity(to) ? | 160 double to_perspective_depth = IsOperationIdentity(to) ? |
159 std::numeric_limits<double>::max() : to->perspective_depth; | 161 std::numeric_limits<double>::max() : to->perspective_depth; |
160 result.ApplyPerspectiveDepth( | 162 result->ApplyPerspectiveDepth( |
161 BlendDoubles(from_perspective_depth, to_perspective_depth, progress)); | 163 BlendDoubles(from_perspective_depth, to_perspective_depth, progress)); |
162 break; | 164 break; |
163 } | 165 } |
164 case TransformOperation::TransformOperationMatrix: { | 166 case TransformOperation::TransformOperationMatrix: { |
165 gfx::Transform to_matrix; | 167 gfx::Transform to_matrix; |
166 if (!IsOperationIdentity(to)) | 168 if (!IsOperationIdentity(to)) |
167 to_matrix = to->matrix; | 169 to_matrix = to->matrix; |
168 gfx::Transform from_matrix; | 170 gfx::Transform from_matrix; |
169 if (!IsOperationIdentity(from)) | 171 if (!IsOperationIdentity(from)) |
170 from_matrix = from->matrix; | 172 from_matrix = from->matrix; |
171 result = to_matrix; | 173 *result = to_matrix; |
172 if (!result.Blend(from_matrix, progress)) | 174 if (!result->Blend(from_matrix, progress)) |
173 return false; | 175 return false; |
174 break; | 176 break; |
175 } | 177 } |
176 case TransformOperation::TransformOperationIdentity: | 178 case TransformOperation::TransformOperationIdentity: |
177 // Do nothing. | 179 // Do nothing. |
178 break; | 180 break; |
179 } | 181 } |
180 | 182 |
181 return true; | 183 return true; |
182 } | 184 } |
183 | 185 |
184 } // namespace cc | 186 } // namespace cc |
OLD | NEW |