Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/layers/AxisPhysicsModel.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_layers_AxisPhysicsModel_h
8
#define mozilla_layers_AxisPhysicsModel_h
9
10
#include <sys/types.h>                  // for int32_t
11
#include "mozilla/TimeStamp.h"          // for TimeDuration
12
13
namespace mozilla {
14
namespace layers {
15
16
17
/**
18
 * AxisPhysicsModel encapsulates a generic 1-dimensional physically-based motion
19
 * model.
20
 *
21
 * It performs frame-rate independent interpolation and RK4 integration for
22
 * smooth animation with stable, deterministic behavior.
23
 * Implementations are expected to subclass and override the Acceleration()
24
 * method.
25
 */
26
class AxisPhysicsModel {
27
public:
28
  AxisPhysicsModel(double aInitialPosition, double aInitialVelocity);
29
  ~AxisPhysicsModel();
30
31
  /**
32
   * Advance the physics simulation.
33
   * |aDelta| is the time since the last sample.
34
   */
35
  void Simulate(const TimeDuration& aDeltaTime);
36
37
  /**
38
   * Gets the raw velocity of this axis at this moment.
39
   */
40
  double GetVelocity() const;
41
42
  /**
43
   * Sets the raw velocity of this axis at this moment.
44
   */
45
  void SetVelocity(double aVelocity);
46
47
  /**
48
   * Gets the raw position of this axis at this moment.
49
   */
50
  double GetPosition() const;
51
52
  /**
53
   * Sets the raw position of this axis at this moment.
54
   */
55
  void SetPosition(double aPosition);
56
57
protected:
58
59
  struct State
60
  {
61
0
    State(double ap, double av) : p(ap), v(av) {};
62
    double p; // Position
63
    double v; // Velocity
64
  };
65
66
  struct Derivative
67
  {
68
0
    Derivative() : dp(0.0), dv(0.0) {};
69
0
    Derivative(double aDp, double aDv) : dp(aDp), dv(aDv) {};
70
    double dp; // dp / delta time = Position
71
    double dv; // dv / delta time = Velocity
72
  };
73
74
  /**
75
   * Acceleration must be overridden and return the number of
76
   * axis-position-units / second that should be added or removed from the
77
   * velocity.
78
   */
79
  virtual double Acceleration(const State &aState) = 0;
80
81
private:
82
83
  /**
84
   * Duration of fixed delta time step (seconds)
85
   */
86
  static const double kFixedTimestep;
87
88
  /**
89
   * 0.0 - 1.0 value indicating progress between current and next simulation
90
   * sample.  Normalized to units of kFixedTimestep duration.
91
   */
92
  double mProgress;
93
94
  /**
95
   * Sample of simulation state as it existed
96
   * (1.0 - mProgress) * kFixedTimestep seconds in the past.
97
   */
98
  State mPrevState;
99
100
  /**
101
   * Sample of simulation state as it will be in mProgress * kFixedTimestep
102
   * seconds in the future.
103
   */
104
  State mNextState;
105
106
  /**
107
   * Perform RK4 (Runge-Kutta method) Integration to calculate the next
108
   * simulation sample.
109
   */
110
  void Integrate(double aDeltaTime);
111
112
  /**
113
   * Apply delta velocity and position represented by aDerivative over
114
   * aDeltaTime seconds, calculate new acceleration, and return new deltas.
115
   */
116
  Derivative Evaluate(const State &aInitState, double aDeltaTime,
117
                      const Derivative &aDerivative);
118
119
  /**
120
   * Helper function for performing linear interpolation (lerp) of double's
121
   */
122
  static double LinearInterpolate(double aV1, double aV2, double aBlend);
123
124
};
125
126
127
} // namespace layers
128
} // namespace mozilla
129
130
#endif