Bug 1876335 - use GRADLE_MAVEN_REPOSITORIES in more places. r=owlish,geckoview-review...
[gecko.git] / gfx / layers / AxisPhysicsModel.h
blob4aa6a69c11f13496a595aa305989501fdfe4ee4c
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/. */
7 #ifndef mozilla_layers_AxisPhysicsModel_h
8 #define mozilla_layers_AxisPhysicsModel_h
10 #include <sys/types.h> // for int32_t
11 #include "mozilla/TimeStamp.h" // for TimeDuration
13 namespace mozilla {
14 namespace layers {
16 /**
17 * AxisPhysicsModel encapsulates a generic 1-dimensional physically-based motion
18 * model.
20 * It performs frame-rate independent interpolation and RK4 integration for
21 * smooth animation with stable, deterministic behavior.
22 * Implementations are expected to subclass and override the Acceleration()
23 * method.
25 class AxisPhysicsModel {
26 public:
27 AxisPhysicsModel(double aInitialPosition, double aInitialVelocity);
28 virtual ~AxisPhysicsModel();
30 /**
31 * Advance the physics simulation.
32 * |aDelta| is the time since the last sample.
34 void Simulate(const TimeDuration& aDeltaTime);
36 /**
37 * Gets the raw velocity of this axis at this moment.
39 double GetVelocity() const;
41 /**
42 * Sets the raw velocity of this axis at this moment.
44 void SetVelocity(double aVelocity);
46 /**
47 * Gets the raw position of this axis at this moment.
49 double GetPosition() const;
51 /**
52 * Sets the raw position of this axis at this moment.
54 void SetPosition(double aPosition);
56 protected:
57 struct State {
58 State(double ap, double av) : p(ap), v(av){};
59 double p; // Position
60 double v; // Velocity
63 struct Derivative {
64 Derivative() : dp(0.0), dv(0.0){};
65 Derivative(double aDp, double aDv) : dp(aDp), dv(aDv){};
66 double dp; // dp / delta time = Position
67 double dv; // dv / delta time = Velocity
70 /**
71 * Acceleration must be overridden and return the number of
72 * axis-position-units / second that should be added or removed from the
73 * velocity.
75 virtual double Acceleration(const State& aState) = 0;
77 private:
78 /**
79 * Duration of fixed delta time step (seconds)
81 static const double kFixedTimestep;
83 /**
84 * 0.0 - 1.0 value indicating progress between current and next simulation
85 * sample. Normalized to units of kFixedTimestep duration.
87 double mProgress;
89 /**
90 * Sample of simulation state as it existed
91 * (1.0 - mProgress) * kFixedTimestep seconds in the past.
93 State mPrevState;
95 /**
96 * Sample of simulation state as it will be in mProgress * kFixedTimestep
97 * seconds in the future.
99 State mNextState;
102 * Perform RK4 (Runge-Kutta method) Integration to calculate the next
103 * simulation sample.
105 void Integrate(double aDeltaTime);
108 * Apply delta velocity and position represented by aDerivative over
109 * aDeltaTime seconds, calculate new acceleration, and return new deltas.
111 Derivative Evaluate(const State& aInitState, double aDeltaTime,
112 const Derivative& aDerivative);
115 * Helper function for performing linear interpolation (lerp) of double's
117 static double LinearInterpolate(double aV1, double aV2, double aBlend);
120 } // namespace layers
121 } // namespace mozilla
123 #endif