summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/transforms/rotate_transform_operation.cc
blob: 4910951d0e30363d611ca72fda48f561fa097bdb (plain)
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
/*
 * 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 "third_party/blink/renderer/platform/transforms/rotate_transform_operation.h"

#include "third_party/blink/renderer/platform/geometry/blend.h"

namespace blink {
namespace {
TransformOperation::OperationType GetTypeForRotation(const Rotation& rotation) {
  float x = rotation.axis.X();
  float y = rotation.axis.Y();
  float z = rotation.axis.Z();
  if (x && !y && !z)
    return TransformOperation::kRotateX;
  if (y && !x && !z)
    return TransformOperation::kRotateY;
  if (z && !x && !y)
    return TransformOperation::kRotateZ;
  return TransformOperation::kRotate3D;
}
}  // namespace

bool RotateTransformOperation::operator==(
    const TransformOperation& other) const {
  if (!IsSameType(other))
    return false;
  const Rotation& other_rotation = ToRotateTransformOperation(other).rotation_;
  return rotation_.axis == other_rotation.axis &&
         rotation_.angle == other_rotation.angle;
}

bool RotateTransformOperation::GetCommonAxis(const RotateTransformOperation* a,
                                             const RotateTransformOperation* b,
                                             FloatPoint3D& result_axis,
                                             double& result_angle_a,
                                             double& result_angle_b) {
  return Rotation::GetCommonAxis(a ? a->rotation_ : Rotation(),
                                 b ? b->rotation_ : Rotation(), result_axis,
                                 result_angle_a, result_angle_b);
}

scoped_refptr<TransformOperation> RotateTransformOperation::Accumulate(
    const TransformOperation& other) {
  DCHECK(IsMatchingOperationType(other.GetType()));
  Rotation new_rotation =
      Rotation::Add(rotation_, ToRotateTransformOperation(other).rotation_);
  return RotateTransformOperation::Create(new_rotation,
                                          GetTypeForRotation(new_rotation));
}

scoped_refptr<TransformOperation> RotateTransformOperation::Blend(
    const TransformOperation* from,
    double progress,
    bool blend_to_identity) {
  if (from && !IsMatchingOperationType(from->GetType()))
    return this;

  if (blend_to_identity)
    return RotateTransformOperation::Create(
        Rotation(Axis(), Angle() * (1 - progress)), type_);

  // Optimize for single axis rotation
  if (!from)
    return RotateTransformOperation::Create(
        Rotation(Axis(), Angle() * progress), type_);

  // Apply spherical linear interpolation. Rotate around a common axis if
  // possible. Otherwise, convert rotations to 4x4 matrix representations and
  // interpolate the matrix decompositions. The 'from' and 'to' transforms can
  // be of different types (based on axis), but must both have equivalent
  // rotate3d representations.
  DCHECK(from->PrimitiveType() == OperationType::kRotate3D);
  OperationType type =
      from->IsSameType(*this) ? type_ : OperationType::kRotate3D;
  const RotateTransformOperation& from_rotate =
      ToRotateTransformOperation(*from);
  return RotateTransformOperation::Create(
      Rotation::Slerp(from_rotate.rotation_, rotation_, progress), type);
}

bool RotateTransformOperation::CanBlendWith(
    const TransformOperation& other) const {
  return other.IsSameType(*this);
}

RotateAroundOriginTransformOperation::RotateAroundOriginTransformOperation(
    double angle,
    double origin_x,
    double origin_y)
    : RotateTransformOperation(Rotation(FloatPoint3D(0, 0, 1), angle),
                               kRotateAroundOrigin),
      origin_x_(origin_x),
      origin_y_(origin_y) {}

void RotateAroundOriginTransformOperation::Apply(
    TransformationMatrix& transform,
    const FloatSize& box_size) const {
  transform.Translate(origin_x_, origin_y_);
  RotateTransformOperation::Apply(transform, box_size);
  transform.Translate(-origin_x_, -origin_y_);
}

bool RotateAroundOriginTransformOperation::operator==(
    const TransformOperation& other) const {
  if (!IsSameType(other))
    return false;
  const RotateAroundOriginTransformOperation& other_rotate =
      ToRotateAroundOriginTransformOperation(other);
  const Rotation& other_rotation = other_rotate.rotation_;
  return rotation_.axis == other_rotation.axis &&
         rotation_.angle == other_rotation.angle &&
         origin_x_ == other_rotate.origin_x_ &&
         origin_y_ == other_rotate.origin_y_;
}

scoped_refptr<TransformOperation> RotateAroundOriginTransformOperation::Blend(
    const TransformOperation* from,
    double progress,
    bool blend_to_identity) {
  if (from && !from->IsSameType(*this))
    return this;
  if (blend_to_identity) {
    return RotateAroundOriginTransformOperation::Create(
        Angle() * (1 - progress), origin_x_, origin_y_);
  }
  if (!from) {
    return RotateAroundOriginTransformOperation::Create(Angle() * progress,
                                                        origin_x_, origin_y_);
  }
  const RotateAroundOriginTransformOperation& from_rotate =
      ToRotateAroundOriginTransformOperation(*from);
  return RotateAroundOriginTransformOperation::Create(
      blink::Blend(from_rotate.Angle(), Angle(), progress),
      blink::Blend(from_rotate.origin_x_, origin_x_, progress),
      blink::Blend(from_rotate.origin_y_, origin_y_, progress));
}

scoped_refptr<TransformOperation> RotateAroundOriginTransformOperation::Zoom(
    double factor) {
  return Create(Angle(), origin_x_ * factor, origin_y_ * factor);
}

}  // namespace blink