Chromium Code Reviews| Index: remoting/client/fling_tracker.cc |
| diff --git a/remoting/client/fling_tracker.cc b/remoting/client/fling_tracker.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f2967410ae30d3275bea612187516fedc41b7034 |
| --- /dev/null |
| +++ b/remoting/client/fling_tracker.cc |
| @@ -0,0 +1,109 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "remoting/client/fling_tracker.h" |
| + |
| +#include <cmath> |
| + |
| +namespace remoting { |
| + |
| +namespace { |
| + |
| +// TODO(yuweih): May need to tweak these numbers to get better smoothness. |
| + |
| +// Stop flinging if the speed drops below this. |
| +// 1px per 16ms. i.e. 1px/frame. |
| +// TODO(yuweih): The screen unit may not be in pixel. This needs to be |
| +// normalized with the DPI. |
| +const float kMinTrackSpeed = 0.0625f; |
| + |
| +// The minimum fling duration (ms) needed to trigger the fling animation. This |
| +// is to prevent unintentional fling with low velocity. |
| +const float kMinFlingTime = 500.f; |
| + |
| +float GetExpFactor(float time_constant, float time_delta) { |
| + return std::exp(-time_delta / time_constant); |
| +} |
| + |
| +float GetDisplacement(float initial_speed_rate, float exp_factor) { |
| + // x = v0 * (1 - e^(-t / T)) |
| + // This comes from a solution to the linear drag equation F=-kv |
| + return initial_speed_rate * (1.f - exp_factor); |
| +} |
| + |
| +float GetSpeed(float initial_speed_rate, |
| + float time_constant, |
| + float exp_factor) { |
| + // v = (1 / T) * v0 * e^(-t / T). |
| + // Derivative of the displacement. |
| + return (1.f / time_constant) * initial_speed_rate * exp_factor; |
| +} |
| + |
| +} // namespace |
| + |
| +FlingTracker::FlingTracker(float time_constant) |
| + : time_constant_(time_constant) {} |
| + |
| +FlingTracker::~FlingTracker() {} |
| + |
| +void FlingTracker::StartFling(float velocity_x, float velocity_y) { |
| + start_time_ = base::TimeTicks::Now(); |
| + |
| + initial_speed_rate_ = |
| + std::sqrt(velocity_x * velocity_x + velocity_y * velocity_y); |
| + |
| + if (GetSpeed(initial_speed_rate_, time_constant_, |
| + GetExpFactor(time_constant_, kMinFlingTime)) < kMinTrackSpeed) { |
| + StopFling(); |
| + return; |
| + } |
| + |
| + velocity_ratio_x_ = velocity_x / initial_speed_rate_; |
| + velocity_ratio_y_ = velocity_y / initial_speed_rate_; |
| + |
| + previous_position_x_ = 0; |
| + previous_position_y_ = 0; |
| +} |
| + |
| +void FlingTracker::StopFling() { |
| + initial_speed_rate_ = 0.f; |
| +} |
| + |
| +bool FlingTracker::IsFlingInProgress() const { |
| + return initial_speed_rate_ > 0; |
| +} |
| + |
| +bool FlingTracker::TrackMovement(base::TimeDelta time_delta, |
| + float* dx, |
| + float* dy) { |
| + if (!IsFlingInProgress()) { |
| + return false; |
| + } |
| + |
| + float time_delta_ms = time_delta.InMilliseconds(); |
| + |
| + float exp_factor = GetExpFactor(time_constant_, time_delta_ms); |
| + |
| + float speed = GetSpeed(initial_speed_rate_, time_constant_, exp_factor); |
| + |
| + if (speed < kMinTrackSpeed) { |
| + StopFling(); |
| + return false; |
| + } |
| + |
| + float displacement = GetDisplacement(initial_speed_rate_, exp_factor); |
| + |
| + float position_x = displacement * velocity_ratio_x_; |
| + float position_y = displacement * velocity_ratio_y_; |
| + |
| + *dx = position_x - previous_position_x_; |
|
nicholss
2017/05/11 15:13:50
It seems to me you also do not need previous_posit
Yuwei
2017/05/11 18:26:27
I just renamed |time_delta| to |time_elapsed|. It
|
| + *dy = position_y - previous_position_y_; |
| + |
| + previous_position_x_ = position_x; |
| + previous_position_y_ = position_y; |
| + |
| + return true; |
| +} |
| + |
| +} // namespace remoting |