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