1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
/*
* Copyright (C) 1999 Antti Koivisto (koivisto@kde.org)
* Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "platform/transforms/TransformOperations.h"
#include "platform/animation/AnimationUtilities.h"
#include "platform/geometry/FloatBox.h"
#include "platform/transforms/IdentityTransformOperation.h"
#include "platform/transforms/InterpolatedTransformOperation.h"
#include "platform/transforms/RotateTransformOperation.h"
#include <algorithm>
using namespace std;
namespace WebCore {
TransformOperations::TransformOperations(bool makeIdentity)
{
if (makeIdentity)
m_operations.append(IdentityTransformOperation::create());
}
bool TransformOperations::operator==(const TransformOperations& o) const
{
if (m_operations.size() != o.m_operations.size())
return false;
unsigned s = m_operations.size();
for (unsigned i = 0; i < s; i++) {
if (*m_operations[i] != *o.m_operations[i])
return false;
}
return true;
}
bool TransformOperations::operationsMatch(const TransformOperations& other) const
{
size_t numOperations = operations().size();
// If the sizes of the function lists don't match, the lists don't match
if (numOperations != other.operations().size())
return false;
// If the types of each function are not the same, the lists don't match
for (size_t i = 0; i < numOperations; ++i) {
if (!operations()[i]->isSameType(*other.operations()[i]))
return false;
}
return true;
}
TransformOperations TransformOperations::blendByMatchingOperations(const TransformOperations& from, const double& progress) const
{
TransformOperations result;
unsigned fromSize = from.operations().size();
unsigned toSize = operations().size();
unsigned size = max(fromSize, toSize);
for (unsigned i = 0; i < size; i++) {
RefPtr<TransformOperation> fromOperation = (i < fromSize) ? from.operations()[i].get() : 0;
RefPtr<TransformOperation> toOperation = (i < toSize) ? operations()[i].get() : 0;
RefPtr<TransformOperation> blendedOperation = toOperation ? toOperation->blend(fromOperation.get(), progress) : (fromOperation ? fromOperation->blend(0, progress, true) : nullptr);
if (blendedOperation)
result.operations().append(blendedOperation);
else {
RefPtr<TransformOperation> identityOperation = IdentityTransformOperation::create();
if (progress > 0.5)
result.operations().append(toOperation ? toOperation : identityOperation);
else
result.operations().append(fromOperation ? fromOperation : identityOperation);
}
}
return result;
}
TransformOperations TransformOperations::blendByUsingMatrixInterpolation(const TransformOperations& from, double progress) const
{
TransformOperations result;
result.operations().append(InterpolatedTransformOperation::create(from, *this, progress));
return result;
}
TransformOperations TransformOperations::blend(const TransformOperations& from, double progress) const
{
if (from == *this || (!from.size() && !size()))
return *this;
// If either list is empty, use blendByMatchingOperations which has special logic for this case.
if (!from.size() || !size() || from.operationsMatch(*this))
return blendByMatchingOperations(from, progress);
return blendByUsingMatrixInterpolation(from, progress);
}
static void findCandidatesInPlane(double px, double py, double nz, double* candidates, int* numCandidates)
{
// The angle that this point is rotated with respect to the plane nz
double phi = atan2(px, py);
*numCandidates = 4;
candidates[0] = phi; // The element at 0deg (maximum x)
for (int i = 1; i < *numCandidates; ++i)
candidates[i] = candidates[i - 1] + M_PI_2; // every 90 deg
if (nz < 0.f) {
for (int i = 0; i < *numCandidates; ++i)
candidates[i] *= -1;
}
}
// This method returns the bounding box that contains the starting point,
// the ending point, and any of the extrema (in each dimension) found across
// the circle described by the arc. These are then filtered to points that
// actually reside on the arc.
static void boundingBoxForArc(const FloatPoint3D& point, const RotateTransformOperation& fromTransform, const RotateTransformOperation& toTransform, double minProgress, double maxProgress, FloatBox& box)
{
double candidates[6];
int numCandidates = 0;
FloatPoint3D axis(fromTransform.axis());
double fromDegrees = fromTransform.angle();
double toDegrees = toTransform.angle();
if (axis.dot(toTransform.axis()) < 0)
toDegrees *= -1;
fromDegrees = blend(fromDegrees, toTransform.angle(), minProgress);
toDegrees = blend(toDegrees, fromTransform.angle(), 1.0 - maxProgress);
if (fromDegrees > toDegrees)
std::swap(fromDegrees, toDegrees);
TransformationMatrix fromMatrix;
TransformationMatrix toMatrix;
fromMatrix.rotate3d(fromTransform.x(), fromTransform.y(), fromTransform.z(), fromDegrees);
toMatrix.rotate3d(fromTransform.x(), fromTransform.y(), fromTransform.z(), toDegrees);
FloatPoint3D fromPoint = fromMatrix.mapPoint(point);
FloatPoint3D toPoint = toMatrix.mapPoint(point);
if (box.isEmpty())
box.setOrigin(fromPoint);
else
box.expandTo(fromPoint);
box.expandTo(toPoint);
switch (fromTransform.type()) {
case TransformOperation::RotateX:
findCandidatesInPlane(point.y(), point.z(), fromTransform.x(), candidates, &numCandidates);
break;
case TransformOperation::RotateY:
findCandidatesInPlane(point.z(), point.x(), fromTransform.y(), candidates, &numCandidates);
break;
case TransformOperation::RotateZ:
findCandidatesInPlane(point.x(), point.y(), fromTransform.z(), candidates, &numCandidates);
break;
default:
{
FloatPoint3D normal = axis;
if (normal.isZero())
return;
normal.normalize();
FloatPoint3D origin;
FloatPoint3D toPoint = point - origin;
FloatPoint3D center = origin + normal * toPoint.dot(normal);
FloatPoint3D v1 = point - center;
if (v1.isZero())
return;
v1.normalize();
FloatPoint3D v2 = normal.cross(v1);
// v1 is the basis vector in the direction of the point.
// i.e. with a rotation of 0, v1 is our +x vector.
// v2 is a perpenticular basis vector of our plane (+y).
// Take the parametric equation of a circle.
// (x = r*cos(t); y = r*sin(t);
// We can treat that as a circle on the plane v1xv2
// From that we get the parametric equations for a circle on the
// plane in 3d space of
// x(t) = r*cos(t)*v1.x + r*sin(t)*v2.x + cx
// y(t) = r*cos(t)*v1.y + r*sin(t)*v2.y + cy
// z(t) = r*cos(t)*v1.z + r*sin(t)*v2.z + cz
// taking the derivative of (x, y, z) and solving for 0 gives us our
// maximum/minimum x, y, z values
// x'(t) = r*cos(t)*v2.x - r*sin(t)*v1.x = 0
// tan(t) = v2.x/v1.x
// t = atan2(v2.x, v1.x) + n*M_PI;
candidates[0] = atan2(v2.x(), v1.x());
candidates[1] = candidates[0] + M_PI;
candidates[2] = atan2(v2.y(), v1.y());
candidates[3] = candidates[2] + M_PI;
candidates[4] = atan2(v2.z(), v1.z());
candidates[5] = candidates[4] + M_PI;
numCandidates = 6;
}
break;
}
double minRadians = deg2rad(fromDegrees);
double maxRadians = deg2rad(toDegrees);
// Once we have the candidates, we now filter them down to ones that
// actually live on the arc, rather than the entire circle.
for (int i = 0; i < numCandidates; ++i) {
double radians = candidates[i];
while (radians < minRadians)
radians += 2.0 * M_PI;
while (radians > maxRadians)
radians -= 2.0 * M_PI;
if (radians < minRadians)
continue;
TransformationMatrix rotation;
rotation.rotate3d(axis.x(), axis.y(), axis.z(), rad2deg(radians));
box.expandTo(rotation.mapPoint(point));
}
}
bool TransformOperations::blendedBoundsForBox(const FloatBox& box, const TransformOperations& from, const double& minProgress, const double& maxProgress, FloatBox* bounds) const
{
int fromSize = from.operations().size();
int toSize = operations().size();
int size = max(fromSize, toSize);
*bounds = box;
for (int i = size - 1; i >= 0; i--) {
RefPtr<TransformOperation> fromOperation = (i < fromSize) ? from.operations()[i] : nullptr;
RefPtr<TransformOperation> toOperation = (i < toSize) ? operations()[i] : nullptr;
if (fromOperation && fromOperation->type() == TransformOperation::None)
fromOperation = nullptr;
if (toOperation && toOperation->type() == TransformOperation::None)
toOperation = nullptr;
TransformOperation::OperationType interpolationType = toOperation ? toOperation->type() :
fromOperation ? fromOperation->type() :
TransformOperation::None;
if (fromOperation && toOperation && !fromOperation->canBlendWith(*toOperation.get()))
return false;
switch (interpolationType) {
case TransformOperation::Identity:
bounds->expandTo(box);
continue;
case TransformOperation::Translate:
case TransformOperation::TranslateX:
case TransformOperation::TranslateY:
case TransformOperation::TranslateZ:
case TransformOperation::Translate3D:
case TransformOperation::Scale:
case TransformOperation::ScaleX:
case TransformOperation::ScaleY:
case TransformOperation::ScaleZ:
case TransformOperation::Scale3D:
case TransformOperation::Skew:
case TransformOperation::SkewX:
case TransformOperation::SkewY:
case TransformOperation::Perspective:
{
RefPtr<TransformOperation> fromTransform;
RefPtr<TransformOperation> toTransform;
if (!toOperation) {
fromTransform = fromOperation->blend(toOperation.get(), 1-minProgress, false);
toTransform = fromOperation->blend(toOperation.get(), 1-maxProgress, false);
} else {
fromTransform = toOperation->blend(fromOperation.get(), minProgress, false);
toTransform = toOperation->blend(fromOperation.get(), maxProgress, false);
}
if (!fromTransform || !toTransform)
continue;
TransformationMatrix fromMatrix;
TransformationMatrix toMatrix;
fromTransform->apply(fromMatrix, FloatSize());
toTransform->apply(toMatrix, FloatSize());
FloatBox fromBox = *bounds;
FloatBox toBox = *bounds;
fromMatrix.transformBox(fromBox);
toMatrix.transformBox(toBox);
*bounds = fromBox;
bounds->expandTo(toBox);
continue;
}
case TransformOperation::Rotate: // This is also RotateZ
case TransformOperation::Rotate3D:
case TransformOperation::RotateX:
case TransformOperation::RotateY:
{
RefPtr<RotateTransformOperation> identityRotation;
const RotateTransformOperation* fromRotation = nullptr;
const RotateTransformOperation* toRotation = nullptr;
if (fromOperation) {
fromRotation = static_cast<const RotateTransformOperation*>(fromOperation.get());
if (fromRotation->axis().isZero())
fromRotation = nullptr;
}
if (toOperation) {
toRotation = static_cast<const RotateTransformOperation*>(toOperation.get());
if (toRotation->axis().isZero())
toRotation = nullptr;
}
double fromAngle;
double toAngle;
FloatPoint3D axis;
if (!RotateTransformOperation::shareSameAxis(fromRotation, toRotation, &axis, &fromAngle, &toAngle)) {
return(false);
}
if (!fromRotation) {
identityRotation = RotateTransformOperation::create(axis.x(), axis.y(), axis.z(), 0, fromOperation ? fromOperation->type() : toOperation->type());
fromRotation = identityRotation.get();
}
if (!toRotation) {
if (!identityRotation)
identityRotation = RotateTransformOperation::create(axis.x(), axis.y(), axis.z(), 0, fromOperation ? fromOperation->type() : toOperation->type());
toRotation = identityRotation.get();
}
FloatBox fromBox = *bounds;
bool first = true;
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 2; ++j) {
for (size_t k = 0; k < 2; ++k) {
FloatBox boundsForArc;
FloatPoint3D corner(fromBox.x(), fromBox.y(), fromBox.z());
corner += FloatPoint3D(i * fromBox.width(), j * fromBox.height(), k * fromBox.depth());
boundingBoxForArc(corner, *fromRotation, *toRotation, minProgress, maxProgress, boundsForArc);
if (first) {
*bounds = boundsForArc;
first = false;
} else {
bounds->expandTo(boundsForArc);
}
}
}
}
}
continue;
case TransformOperation::None:
continue;
case TransformOperation::Matrix:
case TransformOperation::Matrix3D:
case TransformOperation::Interpolated:
return(false);
}
}
return true;
}
TransformOperations TransformOperations::add(const TransformOperations& addend) const
{
TransformOperations result;
result.m_operations = operations();
result.m_operations.appendVector(addend.operations());
return result;
}
} // namespace WebCore
|