2 * Copyright (C) 1999 Antti Koivisto (koivisto@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
23 #include "platform/transforms/TransformOperations.h"
25 #include "platform/animation/AnimationUtilities.h"
26 #include "platform/geometry/FloatBox.h"
27 #include "platform/transforms/IdentityTransformOperation.h"
28 #include "platform/transforms/InterpolatedTransformOperation.h"
29 #include "platform/transforms/RotateTransformOperation.h"
36 TransformOperations::TransformOperations(bool makeIdentity)
39 m_operations.append(IdentityTransformOperation::create());
42 bool TransformOperations::operator==(const TransformOperations& o) const
44 if (m_operations.size() != o.m_operations.size())
47 unsigned s = m_operations.size();
48 for (unsigned i = 0; i < s; i++) {
49 if (*m_operations[i] != *o.m_operations[i])
56 bool TransformOperations::operationsMatch(const TransformOperations& other) const
58 size_t numOperations = operations().size();
59 // If the sizes of the function lists don't match, the lists don't match
60 if (numOperations != other.operations().size())
63 // If the types of each function are not the same, the lists don't match
64 for (size_t i = 0; i < numOperations; ++i) {
65 if (!operations()[i]->isSameType(*other.operations()[i]))
71 TransformOperations TransformOperations::blendByMatchingOperations(const TransformOperations& from, const double& progress) const
73 TransformOperations result;
75 unsigned fromSize = from.operations().size();
76 unsigned toSize = operations().size();
77 unsigned size = max(fromSize, toSize);
78 for (unsigned i = 0; i < size; i++) {
79 RefPtr<TransformOperation> fromOperation = (i < fromSize) ? from.operations()[i].get() : 0;
80 RefPtr<TransformOperation> toOperation = (i < toSize) ? operations()[i].get() : 0;
81 RefPtr<TransformOperation> blendedOperation = toOperation ? toOperation->blend(fromOperation.get(), progress) : (fromOperation ? fromOperation->blend(0, progress, true) : nullptr);
83 result.operations().append(blendedOperation);
85 RefPtr<TransformOperation> identityOperation = IdentityTransformOperation::create();
87 result.operations().append(toOperation ? toOperation : identityOperation);
89 result.operations().append(fromOperation ? fromOperation : identityOperation);
96 TransformOperations TransformOperations::blendByUsingMatrixInterpolation(const TransformOperations& from, double progress) const
98 TransformOperations result;
99 result.operations().append(InterpolatedTransformOperation::create(from, *this, progress));
103 TransformOperations TransformOperations::blend(const TransformOperations& from, double progress) const
105 if (from == *this || (!from.size() && !size()))
108 // If either list is empty, use blendByMatchingOperations which has special logic for this case.
109 if (!from.size() || !size() || from.operationsMatch(*this))
110 return blendByMatchingOperations(from, progress);
112 return blendByUsingMatrixInterpolation(from, progress);
115 static void findCandidatesInPlane(double px, double py, double nz, double* candidates, int* numCandidates)
117 // The angle that this point is rotated with respect to the plane nz
118 double phi = atan2(px, py);
121 candidates[0] = phi; // The element at 0deg (maximum x)
123 for (int i = 1; i < *numCandidates; ++i)
124 candidates[i] = candidates[i - 1] + M_PI_2; // every 90 deg
126 for (int i = 0; i < *numCandidates; ++i)
131 // This method returns the bounding box that contains the starting point,
132 // the ending point, and any of the extrema (in each dimension) found across
133 // the circle described by the arc. These are then filtered to points that
134 // actually reside on the arc.
135 static void boundingBoxForArc(const FloatPoint3D& point, const RotateTransformOperation& fromTransform, const RotateTransformOperation& toTransform, double minProgress, double maxProgress, FloatBox& box)
137 double candidates[6];
138 int numCandidates = 0;
140 FloatPoint3D axis(fromTransform.axis());
141 double fromDegrees = fromTransform.angle();
142 double toDegrees = toTransform.angle();
144 if (axis.dot(toTransform.axis()) < 0)
147 fromDegrees = blend(fromDegrees, toTransform.angle(), minProgress);
148 toDegrees = blend(toDegrees, fromTransform.angle(), 1.0 - maxProgress);
149 if (fromDegrees > toDegrees)
150 std::swap(fromDegrees, toDegrees);
152 TransformationMatrix fromMatrix;
153 TransformationMatrix toMatrix;
154 fromMatrix.rotate3d(fromTransform.x(), fromTransform.y(), fromTransform.z(), fromDegrees);
155 toMatrix.rotate3d(fromTransform.x(), fromTransform.y(), fromTransform.z(), toDegrees);
157 FloatPoint3D fromPoint = fromMatrix.mapPoint(point);
158 FloatPoint3D toPoint = toMatrix.mapPoint(point);
161 box.setOrigin(fromPoint);
163 box.expandTo(fromPoint);
165 box.expandTo(toPoint);
167 switch (fromTransform.type()) {
168 case TransformOperation::RotateX:
169 findCandidatesInPlane(point.y(), point.z(), fromTransform.x(), candidates, &numCandidates);
171 case TransformOperation::RotateY:
172 findCandidatesInPlane(point.z(), point.x(), fromTransform.y(), candidates, &numCandidates);
174 case TransformOperation::RotateZ:
175 findCandidatesInPlane(point.x(), point.y(), fromTransform.z(), candidates, &numCandidates);
179 FloatPoint3D normal = axis;
184 FloatPoint3D toPoint = point - origin;
185 FloatPoint3D center = origin + normal * toPoint.dot(normal);
186 FloatPoint3D v1 = point - center;
191 FloatPoint3D v2 = normal.cross(v1);
192 // v1 is the basis vector in the direction of the point.
193 // i.e. with a rotation of 0, v1 is our +x vector.
194 // v2 is a perpenticular basis vector of our plane (+y).
196 // Take the parametric equation of a circle.
197 // (x = r*cos(t); y = r*sin(t);
198 // We can treat that as a circle on the plane v1xv2
199 // From that we get the parametric equations for a circle on the
200 // plane in 3d space of
201 // x(t) = r*cos(t)*v1.x + r*sin(t)*v2.x + cx
202 // y(t) = r*cos(t)*v1.y + r*sin(t)*v2.y + cy
203 // z(t) = r*cos(t)*v1.z + r*sin(t)*v2.z + cz
204 // taking the derivative of (x, y, z) and solving for 0 gives us our
205 // maximum/minimum x, y, z values
206 // x'(t) = r*cos(t)*v2.x - r*sin(t)*v1.x = 0
207 // tan(t) = v2.x/v1.x
208 // t = atan2(v2.x, v1.x) + n*M_PI;
210 candidates[0] = atan2(v2.x(), v1.x());
211 candidates[1] = candidates[0] + M_PI;
212 candidates[2] = atan2(v2.y(), v1.y());
213 candidates[3] = candidates[2] + M_PI;
214 candidates[4] = atan2(v2.z(), v1.z());
215 candidates[5] = candidates[4] + M_PI;
221 double minRadians = deg2rad(fromDegrees);
222 double maxRadians = deg2rad(toDegrees);
223 // Once we have the candidates, we now filter them down to ones that
224 // actually live on the arc, rather than the entire circle.
225 for (int i = 0; i < numCandidates; ++i) {
226 double radians = candidates[i];
228 while (radians < minRadians)
229 radians += 2.0 * M_PI;
230 while (radians > maxRadians)
231 radians -= 2.0 * M_PI;
232 if (radians < minRadians)
235 TransformationMatrix rotation;
236 rotation.rotate3d(axis.x(), axis.y(), axis.z(), rad2deg(radians));
237 box.expandTo(rotation.mapPoint(point));
241 bool TransformOperations::blendedBoundsForBox(const FloatBox& box, const TransformOperations& from, const double& minProgress, const double& maxProgress, FloatBox* bounds) const
244 int fromSize = from.operations().size();
245 int toSize = operations().size();
246 int size = max(fromSize, toSize);
249 for (int i = size - 1; i >= 0; i--) {
250 RefPtr<TransformOperation> fromOperation = (i < fromSize) ? from.operations()[i] : nullptr;
251 RefPtr<TransformOperation> toOperation = (i < toSize) ? operations()[i] : nullptr;
252 if (fromOperation && fromOperation->type() == TransformOperation::None)
253 fromOperation = nullptr;
255 if (toOperation && toOperation->type() == TransformOperation::None)
256 toOperation = nullptr;
258 TransformOperation::OperationType interpolationType = toOperation ? toOperation->type() :
259 fromOperation ? fromOperation->type() :
260 TransformOperation::None;
261 if (fromOperation && toOperation && !fromOperation->canBlendWith(*toOperation.get()))
264 switch (interpolationType) {
265 case TransformOperation::Identity:
266 bounds->expandTo(box);
268 case TransformOperation::Translate:
269 case TransformOperation::TranslateX:
270 case TransformOperation::TranslateY:
271 case TransformOperation::TranslateZ:
272 case TransformOperation::Translate3D:
273 case TransformOperation::Scale:
274 case TransformOperation::ScaleX:
275 case TransformOperation::ScaleY:
276 case TransformOperation::ScaleZ:
277 case TransformOperation::Scale3D:
278 case TransformOperation::Skew:
279 case TransformOperation::SkewX:
280 case TransformOperation::SkewY:
281 case TransformOperation::Perspective:
283 RefPtr<TransformOperation> fromTransform;
284 RefPtr<TransformOperation> toTransform;
286 fromTransform = fromOperation->blend(toOperation.get(), 1-minProgress, false);
287 toTransform = fromOperation->blend(toOperation.get(), 1-maxProgress, false);
289 fromTransform = toOperation->blend(fromOperation.get(), minProgress, false);
290 toTransform = toOperation->blend(fromOperation.get(), maxProgress, false);
292 if (!fromTransform || !toTransform)
294 TransformationMatrix fromMatrix;
295 TransformationMatrix toMatrix;
296 fromTransform->apply(fromMatrix, FloatSize());
297 toTransform->apply(toMatrix, FloatSize());
298 FloatBox fromBox = *bounds;
299 FloatBox toBox = *bounds;
300 fromMatrix.transformBox(fromBox);
301 toMatrix.transformBox(toBox);
303 bounds->expandTo(toBox);
306 case TransformOperation::Rotate: // This is also RotateZ
307 case TransformOperation::Rotate3D:
308 case TransformOperation::RotateX:
309 case TransformOperation::RotateY:
311 RefPtr<RotateTransformOperation> identityRotation;
312 const RotateTransformOperation* fromRotation = nullptr;
313 const RotateTransformOperation* toRotation = nullptr;
315 fromRotation = static_cast<const RotateTransformOperation*>(fromOperation.get());
316 if (fromRotation->axis().isZero())
317 fromRotation = nullptr;
321 toRotation = static_cast<const RotateTransformOperation*>(toOperation.get());
322 if (toRotation->axis().isZero())
323 toRotation = nullptr;
329 if (!RotateTransformOperation::shareSameAxis(fromRotation, toRotation, &axis, &fromAngle, &toAngle)) {
334 identityRotation = RotateTransformOperation::create(axis.x(), axis.y(), axis.z(), 0, fromOperation ? fromOperation->type() : toOperation->type());
335 fromRotation = identityRotation.get();
339 if (!identityRotation)
340 identityRotation = RotateTransformOperation::create(axis.x(), axis.y(), axis.z(), 0, fromOperation ? fromOperation->type() : toOperation->type());
341 toRotation = identityRotation.get();
344 FloatBox fromBox = *bounds;
346 for (size_t i = 0; i < 2; ++i) {
347 for (size_t j = 0; j < 2; ++j) {
348 for (size_t k = 0; k < 2; ++k) {
349 FloatBox boundsForArc;
350 FloatPoint3D corner(fromBox.x(), fromBox.y(), fromBox.z());
351 corner += FloatPoint3D(i * fromBox.width(), j * fromBox.height(), k * fromBox.depth());
352 boundingBoxForArc(corner, *fromRotation, *toRotation, minProgress, maxProgress, boundsForArc);
354 *bounds = boundsForArc;
357 bounds->expandTo(boundsForArc);
364 case TransformOperation::None:
366 case TransformOperation::Matrix:
367 case TransformOperation::Matrix3D:
368 case TransformOperation::Interpolated:
376 TransformOperations TransformOperations::add(const TransformOperations& addend) const
378 TransformOperations result;
379 result.m_operations = operations();
380 result.m_operations.appendVector(addend.operations());