Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/layers/AxisPhysicsMSDModel.cpp
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
#include "AxisPhysicsMSDModel.h"
8
#include <math.h>                       // for sqrt and fabs
9
10
namespace mozilla {
11
namespace layers {
12
13
/**
14
 * Constructs an AxisPhysicsMSDModel with initial values for state.
15
 *
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.
29
 */
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)
40
0
{
41
0
}
42
43
AxisPhysicsMSDModel::~AxisPhysicsMSDModel()
44
0
{
45
0
}
46
47
double
48
AxisPhysicsMSDModel::Acceleration(const State &aState)
49
0
{
50
0
  // Simulate a Mass-Damper-Spring Model; assume a unit mass
51
0
52
0
  // Hooke’s Law: http://en.wikipedia.org/wiki/Hooke%27s_law
53
0
  double spring_force = (mDestination - aState.p) * mSpringConstant;
54
0
  double damp_force = -aState.v * mDampingRatio * mSpringConstantSqrtXTwo;
55
0
56
0
  return spring_force + damp_force;
57
0
}
58
59
60
double
61
AxisPhysicsMSDModel::GetDestination() const
62
0
{
63
0
  return mDestination;
64
0
}
65
66
void
67
AxisPhysicsMSDModel::SetDestination(double aDestination)
68
0
{
69
0
  mDestination = aDestination;
70
0
}
71
72
bool
73
AxisPhysicsMSDModel::IsFinished(double aSmallestVisibleIncrement)
74
0
{
75
0
  // In order to satisfy the condition of reaching the destination, the distance
76
0
  // between the simulation position and the destination must be less than
77
0
  // aSmallestVisibleIncrement while the speed is simultaneously less than
78
0
  // finishVelocity.  This enables an under-damped system to overshoot the
79
0
  // destination when desired without prematurely triggering the finished state.
80
0
  // If finishVelocity is set too low, the animation may end long after
81
0
  // oscillation has finished, resulting in unnecessary processing.
82
0
  // If set too high, the animation may prematurely terminate when expected
83
0
  // to overshoot the destination in an under-damped system.
84
0
  // aSmallestVisibleIncrement * 2 was selected through experimentation that
85
0
  // revealed that a critically damped system will terminate within 100ms.
86
0
  const double finishVelocity = aSmallestVisibleIncrement * 2;
87
0
88
0
  return fabs(mDestination - GetPosition ()) < aSmallestVisibleIncrement
89
0
    && fabs(GetVelocity()) <= finishVelocity;
90
0
}
91
92
} // namespace layers
93
} // namespace mozilla