Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/client/fling_tracker.h" | |
| 6 | |
| 7 #include <cmath> | |
| 8 | |
| 9 namespace remoting { | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 // TODO(yuweih): May need to tweak these numbers to get better smoothness. | |
| 14 | |
| 15 // Stop flinging if the speed drops below this. | |
| 16 // 1px per 16ms. i.e. 1px/frame. | |
| 17 // TODO(yuweih): The screen unit may not be in pixel. This needs to be | |
| 18 // normalized with the DPI. | |
| 19 const float kMinTrackSpeed = 0.0625f; | |
| 20 | |
| 21 // The minimum fling duration (ms) needed to trigger the fling animation. This | |
| 22 // is to prevent unintentional fling with low velocity. | |
| 23 const float kMinFlingTime = 500.f; | |
| 24 | |
| 25 float GetExpFactor(float time_constant, float time_delta) { | |
| 26 return std::exp(-time_delta / time_constant); | |
| 27 } | |
| 28 | |
| 29 float GetDisplacement(float initial_speed_rate, float exp_factor) { | |
| 30 // x = v0 * (1 - e^(-t / T)) | |
| 31 // This comes from a solution to the linear drag equation F=-kv | |
| 32 return initial_speed_rate * (1.f - exp_factor); | |
| 33 } | |
| 34 | |
| 35 float GetSpeed(float initial_speed_rate, | |
| 36 float time_constant, | |
| 37 float exp_factor) { | |
| 38 // v = (1 / T) * v0 * e^(-t / T). | |
| 39 // Derivative of the displacement. | |
| 40 return (1.f / time_constant) * initial_speed_rate * exp_factor; | |
| 41 } | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 FlingTracker::FlingTracker(float time_constant) | |
| 46 : time_constant_(time_constant) {} | |
| 47 | |
| 48 FlingTracker::~FlingTracker() {} | |
| 49 | |
| 50 void FlingTracker::StartFling(float velocity_x, float velocity_y) { | |
| 51 start_time_ = base::TimeTicks::Now(); | |
| 52 | |
| 53 initial_speed_rate_ = | |
| 54 std::sqrt(velocity_x * velocity_x + velocity_y * velocity_y); | |
| 55 | |
| 56 if (GetSpeed(initial_speed_rate_, time_constant_, | |
| 57 GetExpFactor(time_constant_, kMinFlingTime)) < kMinTrackSpeed) { | |
| 58 StopFling(); | |
| 59 return; | |
| 60 } | |
| 61 | |
| 62 velocity_ratio_x_ = velocity_x / initial_speed_rate_; | |
| 63 velocity_ratio_y_ = velocity_y / initial_speed_rate_; | |
| 64 | |
| 65 previous_position_x_ = 0; | |
| 66 previous_position_y_ = 0; | |
| 67 } | |
| 68 | |
| 69 void FlingTracker::StopFling() { | |
| 70 initial_speed_rate_ = 0.f; | |
| 71 } | |
| 72 | |
| 73 bool FlingTracker::IsFlingInProgress() const { | |
| 74 return initial_speed_rate_ > 0; | |
| 75 } | |
| 76 | |
| 77 bool FlingTracker::TrackMovement(base::TimeDelta time_delta, | |
| 78 float* dx, | |
| 79 float* dy) { | |
| 80 if (!IsFlingInProgress()) { | |
| 81 return false; | |
| 82 } | |
| 83 | |
| 84 float time_delta_ms = time_delta.InMilliseconds(); | |
| 85 | |
| 86 float exp_factor = GetExpFactor(time_constant_, time_delta_ms); | |
| 87 | |
| 88 float speed = GetSpeed(initial_speed_rate_, time_constant_, exp_factor); | |
| 89 | |
| 90 if (speed < kMinTrackSpeed) { | |
| 91 StopFling(); | |
| 92 return false; | |
| 93 } | |
| 94 | |
| 95 float displacement = GetDisplacement(initial_speed_rate_, exp_factor); | |
| 96 | |
| 97 float position_x = displacement * velocity_ratio_x_; | |
| 98 float position_y = displacement * velocity_ratio_y_; | |
| 99 | |
| 100 *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
| |
| 101 *dy = position_y - previous_position_y_; | |
| 102 | |
| 103 previous_position_x_ = position_x; | |
| 104 previous_position_y_ = position_y; | |
| 105 | |
| 106 return true; | |
| 107 } | |
| 108 | |
| 109 } // namespace remoting | |
| OLD | NEW |