File: interpolated_transform.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (298 lines) | stat: -rw-r--r-- 10,732 bytes parent folder | download | duplicates (5)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_GFX_INTERPOLATED_TRANSFORM_H_
#define UI_GFX_INTERPOLATED_TRANSFORM_H_

#include <memory>

#include "base/component_export.h"
#include "ui/gfx/geometry/decomposed_transform.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/point3_f.h"
#include "ui/gfx/geometry/transform.h"
#include "ui/gfx/geometry/vector3d_f.h"

namespace ui {

///////////////////////////////////////////////////////////////////////////////
// class InterpolatedTransform
//
// Abstract base class for transforms that animate over time. These
// interpolated transforms can be combined to allow for more sophisticated
// animations. For example, you might combine a rotation of 90 degrees between
// times 0 and 1, with a scale from 1 to 0.3 between times 0 and 0.25 and a
// scale from 0.3 to 1 from between times 0.75 and 1.
//
///////////////////////////////////////////////////////////////////////////////
class COMPONENT_EXPORT(GFX) InterpolatedTransform {
 public:
  InterpolatedTransform();
  // The interpolated transform varies only when t in (start_time, end_time).
  // If t <= start_time, Interpolate(t) will return the initial transform, and
  // if t >= end_time, Interpolate(t) will return the final transform.
  InterpolatedTransform(float start_time, float end_time);

  InterpolatedTransform(const InterpolatedTransform&) = delete;
  InterpolatedTransform& operator=(const InterpolatedTransform&) = delete;

  virtual ~InterpolatedTransform();

  // Returns the interpolated transform at time t. Note: not virtual.
  gfx::Transform Interpolate(float t) const;

  // The Intepolate ultimately returns the product of our transform at time t
  // and our child's transform at time t (if we have one).
  //
  // This function takes ownership of the passed InterpolatedTransform.
  void SetChild(std::unique_ptr<InterpolatedTransform> child);

  // If the interpolated transform is reversed, Interpolate(t) will return
  // Interpolate(1 - t)
  void SetReversed(bool reversed) { reversed_ = reversed; }
  bool Reversed() const { return reversed_; }

 protected:
  // Calculates the interpolated transform without considering our child.
  virtual gfx::Transform InterpolateButDoNotCompose(float t) const = 0;

  // If time in (start_time_, end_time_], this function linearly interpolates
  // between start_value and end_value.  More precisely it returns
  // (1 - t) * start_value + t * end_value where
  // t = (start_time_ - time) / (end_time_ - start_time_).
  // If time < start_time_ it returns start_value, and if time >= end_time_
  // it returns end_value.
  float ValueBetween(float time, float start_value, float end_value) const;

  float start_time() const { return start_time_; }
  float end_time() const { return end_time_; }

 private:
  const float start_time_;
  const float end_time_;

  // The child transform. If you consider an interpolated transform as a
  // function of t. If, without a child, we are f(t), and our child is
  // g(t), then with a child we become f'(t) = f(t) * g(t). Using a child
  // transform, we can chain collections of transforms together.
  std::unique_ptr<InterpolatedTransform> child_;

  bool reversed_;
};

///////////////////////////////////////////////////////////////////////////////
// class InterpolatedRotation
//
// Represents an animated rotation.
//
///////////////////////////////////////////////////////////////////////////////
class COMPONENT_EXPORT(GFX) InterpolatedRotation
    : public InterpolatedTransform {
 public:
  InterpolatedRotation(float start_degrees, float end_degrees);
  InterpolatedRotation(float start_degrees,
                       float end_degrees,
                       float start_time,
                       float end_time);

  InterpolatedRotation(const InterpolatedRotation&) = delete;
  InterpolatedRotation& operator=(const InterpolatedRotation&) = delete;

  ~InterpolatedRotation() override;

 protected:
  gfx::Transform InterpolateButDoNotCompose(float t) const override;

 private:
  const float start_degrees_;
  const float end_degrees_;
};

///////////////////////////////////////////////////////////////////////////////
// class InterpolatedAxisAngleRotation
//
// Represents an animated rotation.
//
///////////////////////////////////////////////////////////////////////////////
class COMPONENT_EXPORT(GFX) InterpolatedAxisAngleRotation
    : public InterpolatedTransform {
 public:
  InterpolatedAxisAngleRotation(const gfx::Vector3dF& axis,
                                float start_degrees,
                                float end_degrees);
  InterpolatedAxisAngleRotation(const gfx::Vector3dF& axis,
                                float start_degrees,
                                float end_degrees,
                                float start_time,
                                float end_time);

  InterpolatedAxisAngleRotation(const InterpolatedAxisAngleRotation&) = delete;
  InterpolatedAxisAngleRotation& operator=(
      const InterpolatedAxisAngleRotation&) = delete;

  ~InterpolatedAxisAngleRotation() override;

 protected:
  gfx::Transform InterpolateButDoNotCompose(float t) const override;

 private:
  gfx::Vector3dF axis_;
  const float start_degrees_;
  const float end_degrees_;
};

///////////////////////////////////////////////////////////////////////////////
// class InterpolatedScale
//
// Represents an animated scale.
//
///////////////////////////////////////////////////////////////////////////////
class COMPONENT_EXPORT(GFX) InterpolatedScale : public InterpolatedTransform {
 public:
  InterpolatedScale(float start_scale, float end_scale);
  InterpolatedScale(float start_scale, float end_scale,
                    float start_time, float end_time);
  InterpolatedScale(const gfx::Point3F& start_scale,
                    const gfx::Point3F& end_scale);
  InterpolatedScale(const gfx::Point3F& start_scale,
                    const gfx::Point3F& end_scale,
                    float start_time,
                    float end_time);

  InterpolatedScale(const InterpolatedScale&) = delete;
  InterpolatedScale& operator=(const InterpolatedScale&) = delete;

  ~InterpolatedScale() override;

 protected:
  gfx::Transform InterpolateButDoNotCompose(float t) const override;

 private:
  const gfx::Point3F start_scale_;
  const gfx::Point3F end_scale_;
};

class COMPONENT_EXPORT(GFX) InterpolatedTranslation
    : public InterpolatedTransform {
 public:
  InterpolatedTranslation(const gfx::PointF& start_pos,
                          const gfx::PointF& end_pos);
  InterpolatedTranslation(const gfx::PointF& start_pos,
                          const gfx::PointF& end_pos,
                          float start_time,
                          float end_time);
  InterpolatedTranslation(const gfx::Point3F& start_pos,
                          const gfx::Point3F& end_pos);
  InterpolatedTranslation(const gfx::Point3F& start_pos,
                          const gfx::Point3F& end_pos,
                          float start_time,
                          float end_time);

  InterpolatedTranslation(const InterpolatedTranslation&) = delete;
  InterpolatedTranslation& operator=(const InterpolatedTranslation&) = delete;

  ~InterpolatedTranslation() override;

 protected:
  gfx::Transform InterpolateButDoNotCompose(float t) const override;

 private:
  const gfx::Point3F start_pos_;
  const gfx::Point3F end_pos_;
};

///////////////////////////////////////////////////////////////////////////////
// class InterpolatedConstantTransform
//
// Represents a transform that is constant over time. This is only useful when
// composed with other interpolated transforms.
//
// See InterpolatedTransformAboutPivot for an example of its usage.
//
///////////////////////////////////////////////////////////////////////////////
class COMPONENT_EXPORT(GFX) InterpolatedConstantTransform
    : public InterpolatedTransform {
 public:
  explicit InterpolatedConstantTransform(const gfx::Transform& transform);

  InterpolatedConstantTransform(const InterpolatedConstantTransform&) = delete;
  InterpolatedConstantTransform& operator=(
      const InterpolatedConstantTransform&) = delete;

  ~InterpolatedConstantTransform() override;

 protected:
  gfx::Transform InterpolateButDoNotCompose(float t) const override;

 private:
  const gfx::Transform transform_;
};

///////////////////////////////////////////////////////////////////////////////
// class InterpolatedTransformAboutPivot
//
// Represents an animated transform with a transformed origin. Essentially,
// at each time, t, the interpolated transform is created by composing
// P * T * P^-1 where P is a constant transform to the new origin.
//
///////////////////////////////////////////////////////////////////////////////
class COMPONENT_EXPORT(GFX) InterpolatedTransformAboutPivot
    : public InterpolatedTransform {
 public:
  // Takes ownership of the passed transform.
  InterpolatedTransformAboutPivot(
      const gfx::Point& pivot,
      std::unique_ptr<InterpolatedTransform> transform);

  // Takes ownership of the passed transform.
  InterpolatedTransformAboutPivot(
      const gfx::Point& pivot,
      std::unique_ptr<InterpolatedTransform> transform,
      float start_time,
      float end_time);

  InterpolatedTransformAboutPivot(const InterpolatedTransformAboutPivot&) =
      delete;
  InterpolatedTransformAboutPivot& operator=(
      const InterpolatedTransformAboutPivot&) = delete;

  ~InterpolatedTransformAboutPivot() override;

 protected:
  gfx::Transform InterpolateButDoNotCompose(float t) const override;

 private:
  void Init(const gfx::Point& pivot,
            std::unique_ptr<InterpolatedTransform> transform);

  std::unique_ptr<InterpolatedTransform> transform_;
};

class COMPONENT_EXPORT(GFX) InterpolatedMatrixTransform
    : public InterpolatedTransform {
 public:
  InterpolatedMatrixTransform(const gfx::Transform& start_transform,
                              const gfx::Transform& end_transform);

  InterpolatedMatrixTransform(const gfx::Transform& start_transform,
                              const gfx::Transform& end_transform,
                              float start_time,
                              float end_time);

  ~InterpolatedMatrixTransform() override;

 protected:
  gfx::Transform InterpolateButDoNotCompose(float t) const override;

 private:
  void Init(const gfx::Transform& start_transform,
            const gfx::Transform& end_transform);

  gfx::DecomposedTransform start_decomp_;
  gfx::DecomposedTransform end_decomp_;
};

} // namespace ui

#endif  // UI_GFX_INTERPOLATED_TRANSFORM_H_