Bug 1891710: part 2) Enable <Element-outerHTML.html> WPT for Trusted Types. r=smaug
[gecko.git] / gfx / layers / AxisPhysicsMSDModel.cpp
bloba506db8a0532cc6cf4868f5bd44eac9ecb1ce38e
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 #include "AxisPhysicsMSDModel.h"
8 #include <math.h> // for sqrt and fabs
10 namespace mozilla {
11 namespace layers {
13 /**
14 * Constructs an AxisPhysicsMSDModel with initial values for state.
16 * @param aInitialPosition sets the initial position of the simulated spring,
17 * in AppUnits.
18 * @param aInitialDestination sets the resting position of the simulated spring,
19 * in AppUnits.
20 * @param aInitialVelocity sets the initial velocity of the simulated spring,
21 * in AppUnits / second. Critically-damped and over-damped systems are
22 * guaranteed not to overshoot aInitialDestination if this is set to 0;
23 * however, it is possible to overshoot and oscillate if not set to 0 or
24 * the system is under-damped.
25 * @param aSpringConstant sets the strength of the simulated spring. Greater
26 * values of mSpringConstant result in a stiffer / stronger spring.
27 * @param aDampingRatio controls the amount of dampening force and determines
28 * if the system is under-damped, critically-damped, or over-damped.
30 AxisPhysicsMSDModel::AxisPhysicsMSDModel(double aInitialPosition,
31 double aInitialDestination,
32 double aInitialVelocity,
33 double aSpringConstant,
34 double aDampingRatio)
35 : AxisPhysicsModel(aInitialPosition, aInitialVelocity),
36 mDestination(aInitialDestination),
37 mSpringConstant(aSpringConstant),
38 mSpringConstantSqrtXTwo(sqrt(mSpringConstant) * 2.0),
39 mDampingRatio(aDampingRatio) {}
41 AxisPhysicsMSDModel::~AxisPhysicsMSDModel() = default;
43 double AxisPhysicsMSDModel::Acceleration(const State& aState) {
44 // Simulate a Mass-Damper-Spring Model; assume a unit mass
46 // Hooke’s Law: http://en.wikipedia.org/wiki/Hooke%27s_law
47 double spring_force = (mDestination - aState.p) * mSpringConstant;
48 double damp_force = -aState.v * mDampingRatio * mSpringConstantSqrtXTwo;
50 return spring_force + damp_force;
53 double AxisPhysicsMSDModel::GetDestination() const { return mDestination; }
55 void AxisPhysicsMSDModel::SetDestination(double aDestination) {
56 mDestination = aDestination;
59 bool AxisPhysicsMSDModel::IsFinished(double aSmallestVisibleIncrement) const {
60 // In order to satisfy the condition of reaching the destination, the distance
61 // between the simulation position and the destination must be less than
62 // aSmallestVisibleIncrement while the speed is simultaneously less than
63 // finishVelocity. This enables an under-damped system to overshoot the
64 // destination when desired without prematurely triggering the finished state.
65 // If finishVelocity is set too low, the animation may end long after
66 // oscillation has finished, resulting in unnecessary processing.
67 // If set too high, the animation may prematurely terminate when expected
68 // to overshoot the destination in an under-damped system.
69 // aSmallestVisibleIncrement * 2 was selected through experimentation that
70 // revealed that a critically damped system will terminate within 100ms.
71 const double finishVelocity = aSmallestVisibleIncrement * 2;
73 return fabs(mDestination - GetPosition()) < aSmallestVisibleIncrement &&
74 fabs(GetVelocity()) <= finishVelocity;
77 } // namespace layers
78 } // namespace mozilla