| 1 | /* |
| 2 | * Copyright (C) 1999 Antti Koivisto (koivisto@kde.org) |
| 3 | * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include "config.h" |
| 23 | #include "RotateTransformOperation.h" |
| 24 | |
| 25 | #include "AnimationUtilities.h" |
| 26 | #include <algorithm> |
| 27 | #include <wtf/MathExtras.h> |
| 28 | #include <wtf/text/TextStream.h> |
| 29 | |
| 30 | namespace WebCore { |
| 31 | |
| 32 | bool RotateTransformOperation::operator==(const TransformOperation& other) const |
| 33 | { |
| 34 | if (!isSameType(other)) |
| 35 | return false; |
| 36 | const RotateTransformOperation& r = downcast<RotateTransformOperation>(other); |
| 37 | return m_x == r.m_x && m_y == r.m_y && m_z == r.m_z && m_angle == r.m_angle; |
| 38 | } |
| 39 | |
| 40 | Ref<TransformOperation> RotateTransformOperation::blend(const TransformOperation* from, double progress, bool blendToIdentity) |
| 41 | { |
| 42 | if (from && !from->isSameType(*this)) |
| 43 | return *this; |
| 44 | |
| 45 | if (blendToIdentity) |
| 46 | return RotateTransformOperation::create(m_x, m_y, m_z, m_angle - m_angle * progress, type()); |
| 47 | |
| 48 | const RotateTransformOperation* fromOp = downcast<RotateTransformOperation>(from); |
| 49 | |
| 50 | // Optimize for single axis rotation |
| 51 | if (!fromOp || (fromOp->m_x == 0 && fromOp->m_y == 0 && fromOp->m_z == 1) || |
| 52 | (fromOp->m_x == 0 && fromOp->m_y == 1 && fromOp->m_z == 0) || |
| 53 | (fromOp->m_x == 1 && fromOp->m_y == 0 && fromOp->m_z == 0)) { |
| 54 | double fromAngle = fromOp ? fromOp->m_angle : 0; |
| 55 | return RotateTransformOperation::create(fromOp ? fromOp->m_x : m_x, |
| 56 | fromOp ? fromOp->m_y : m_y, |
| 57 | fromOp ? fromOp->m_z : m_z, |
| 58 | WebCore::blend(fromAngle, m_angle, progress), type()); |
| 59 | } |
| 60 | |
| 61 | const RotateTransformOperation* toOp = this; |
| 62 | |
| 63 | // Create the 2 rotation matrices |
| 64 | TransformationMatrix fromT; |
| 65 | TransformationMatrix toT; |
| 66 | fromT.rotate3d((fromOp ? fromOp->m_x : 0), |
| 67 | (fromOp ? fromOp->m_y : 0), |
| 68 | (fromOp ? fromOp->m_z : 1), |
| 69 | (fromOp ? fromOp->m_angle : 0)); |
| 70 | |
| 71 | toT.rotate3d((toOp ? toOp->m_x : 0), |
| 72 | (toOp ? toOp->m_y : 0), |
| 73 | (toOp ? toOp->m_z : 1), |
| 74 | (toOp ? toOp->m_angle : 0)); |
| 75 | |
| 76 | // Blend them |
| 77 | toT.blend(fromT, progress); |
| 78 | |
| 79 | // Extract the result as a quaternion |
| 80 | TransformationMatrix::Decomposed4Type decomp; |
| 81 | toT.decompose4(decomp); |
| 82 | |
| 83 | // Convert that to Axis/Angle form |
| 84 | double x = -decomp.quaternionX; |
| 85 | double y = -decomp.quaternionY; |
| 86 | double z = -decomp.quaternionZ; |
| 87 | double length = sqrt(x * x + y * y + z * z); |
| 88 | double angle = 0; |
| 89 | |
| 90 | if (length > 0.00001) { |
| 91 | x /= length; |
| 92 | y /= length; |
| 93 | z /= length; |
| 94 | angle = rad2deg(acos(decomp.quaternionW) * 2); |
| 95 | } else { |
| 96 | x = 0; |
| 97 | y = 0; |
| 98 | z = 1; |
| 99 | } |
| 100 | return RotateTransformOperation::create(x, y, z, angle, ROTATE_3D); |
| 101 | } |
| 102 | |
| 103 | void RotateTransformOperation::dump(TextStream& ts) const |
| 104 | { |
| 105 | ts << type() << "(" << TextStream::FormatNumberRespectingIntegers(m_x) << ", " << TextStream::FormatNumberRespectingIntegers(m_y) << ", " << TextStream::FormatNumberRespectingIntegers(m_z) << ", " << TextStream::FormatNumberRespectingIntegers(m_angle) << "deg)" ; |
| 106 | } |
| 107 | |
| 108 | } // namespace WebCore |
| 109 | |