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 if (GetDisplacement(time_constant_, initial_speed_rate_, fling_duration_) < | |
nicholss
2017/05/22 20:16:59
It might be more readable if you assign the result
Yuwei
2017/05/22 21:18:25
Done.
| |
67 kMinDisplacement) { | |
68 StopFling(); | |
69 return; | |
70 } | |
61 | 71 |
62 velocity_ratio_x_ = velocity_x / initial_speed_rate_; | 72 velocity_ratio_x_ = velocity_x / initial_speed_rate_; |
63 velocity_ratio_y_ = velocity_y / initial_speed_rate_; | 73 velocity_ratio_y_ = velocity_y / initial_speed_rate_; |
64 | 74 |
65 previous_position_x_ = 0; | 75 previous_position_x_ = 0; |
66 previous_position_y_ = 0; | 76 previous_position_y_ = 0; |
67 } | 77 } |
68 | 78 |
69 void FlingTracker::StopFling() { | 79 void FlingTracker::StopFling() { |
70 initial_speed_rate_ = 0.f; | 80 initial_speed_rate_ = 0.f; |
71 } | 81 } |
72 | 82 |
73 bool FlingTracker::IsFlingInProgress() const { | 83 bool FlingTracker::IsFlingInProgress() const { |
74 return initial_speed_rate_ > 0; | 84 return initial_speed_rate_ > 0; |
75 } | 85 } |
76 | 86 |
77 bool FlingTracker::TrackMovement(base::TimeDelta time_elapsed, | 87 bool FlingTracker::TrackMovement(base::TimeDelta time_elapsed, |
78 float* dx, | 88 float* dx, |
79 float* dy) { | 89 float* dy) { |
80 if (!IsFlingInProgress()) { | 90 if (!IsFlingInProgress()) { |
81 return false; | 91 return false; |
82 } | 92 } |
83 | 93 |
84 float time_elapsed_ms = time_elapsed.InMilliseconds(); | 94 float time_elapsed_ms = time_elapsed.InMilliseconds(); |
85 | 95 |
86 float exp_factor = GetExpFactor(time_constant_, time_elapsed_ms); | 96 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(); | 97 StopFling(); |
92 return false; | 98 return false; |
93 } | 99 } |
94 | 100 |
95 float displacement = GetDisplacement(initial_speed_rate_, exp_factor); | 101 float displacement = |
102 GetDisplacement(time_constant_, initial_speed_rate_, time_elapsed_ms); | |
96 | 103 |
97 float position_x = displacement * velocity_ratio_x_; | 104 float position_x = displacement * velocity_ratio_x_; |
98 float position_y = displacement * velocity_ratio_y_; | 105 float position_y = displacement * velocity_ratio_y_; |
99 | 106 |
100 *dx = position_x - previous_position_x_; | 107 *dx = position_x - previous_position_x_; |
101 *dy = position_y - previous_position_y_; | 108 *dy = position_y - previous_position_y_; |
102 | 109 |
103 previous_position_x_ = position_x; | 110 previous_position_x_ = position_x; |
104 previous_position_y_ = position_y; | 111 previous_position_y_ = position_y; |
105 | 112 |
106 return true; | 113 return true; |
107 } | 114 } |
108 | 115 |
109 } // namespace remoting | 116 } // namespace remoting |
OLD | NEW |