OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 #ifndef UI_EVENTS_GESTURE_DETECTION_VELOCITY_TRACKER_H_ | |
6 #define UI_EVENTS_GESTURE_DETECTION_VELOCITY_TRACKER_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/time/time.h" | |
11 #include "ui/events/gesture_detection/bitset_32.h" | |
12 | |
13 namespace ui { | |
14 | |
15 class MotionEvent; | |
16 class VelocityTrackerStrategy; | |
17 | |
18 namespace { | |
19 struct Estimator; | |
20 struct Position; | |
21 } | |
22 | |
23 // Port of VelocityTracker from Android | |
24 // * platform/frameworks/native/include/input/VelocityTracker.h | |
25 // * Change-Id: I4983db61b53e28479fc90d9211fafff68f7f49a6 | |
26 // * Please update the Change-Id as upstream Android changes are pulled. | |
27 class VelocityTracker { | |
28 public: | |
29 enum { MAX_POINTERS = 16, MAX_POINTER_ID = 31 }; | |
tdresser
2014/02/20 19:11:24
MAX_POINTERS should probably be pulled from somewh
jdduke (slow)
2014/02/21 02:31:56
Well, there's touchesLengthCap in Blink, and a run
| |
30 | |
31 enum Strategy { | |
tdresser
2014/02/20 19:11:24
I'm unconvinced that it makes sense to port all st
jdduke (slow)
2014/02/21 02:31:56
No reason, other than it being very little extra w
tdresser
2014/02/21 14:29:28
SGTM.
| |
32 // 1st order least squares. Quality: POOR. | |
33 // Frequently underfits the touch data especially when the finger | |
34 // accelerates or changes direction. Often underestimates velocity. The | |
35 // direction is overly influenced by historical touch points. | |
36 LSQ1, | |
37 | |
38 // 2nd order least squares. Quality: VERY GOOD. | |
39 // Pretty much ideal, but can be confused by certain kinds of touch data, | |
40 // particularly if the panel has a tendency to generate delayed, | |
41 // duplicate or jittery touch coordinates when the finger is released. | |
42 LSQ2, | |
43 | |
44 // 3rd order least squares. Quality: UNUSABLE. | |
45 // Frequently overfits the touch data yielding wildly divergent estimates | |
46 // of the velocity when the finger is released. | |
47 LSQ3, | |
48 | |
49 // 2nd order weighted least squares, delta weighting. | |
50 // Quality: EXPERIMENTAL | |
51 WLSQ2_DELTA, | |
52 | |
53 // 2nd order weighted least squares, central weighting. | |
54 // Quality: EXPERIMENTAL | |
55 WLSQ2_CENTRAL, | |
56 | |
57 // 2nd order weighted least squares, recent weighting. | |
58 // Quality: EXPERIMENTAL | |
59 WLSQ2_RECENT, | |
60 | |
61 // 1st order integrating filter. Quality: GOOD. | |
62 // Not as good as 'lsq2' because it cannot estimate acceleration but it is | |
63 // more tolerant of errors. Like 'lsq1', this strategy tends to | |
64 // underestimate | |
65 // the velocity of a fling but this strategy tends to respond to changes in | |
66 // direction more quickly and accurately. | |
67 INT1, | |
68 | |
69 // 2nd order integrating filter. Quality: EXPERIMENTAL. | |
70 // For comparison purposes only. Unlike 'int1' this strategy can compensate | |
71 // for acceleration but it typically overestimates the effect. | |
72 INT2, | |
73 STRATEGY_MAX = INT2, | |
74 | |
75 // The default velocity tracker strategy. | |
76 // Although other strategies are available for testing and comparison | |
77 // purposes, this is the strategy that applications will actually use. Be | |
78 // very careful when adjusting the default strategy because it can | |
79 // dramatically affect (often in a bad way) the user experience. | |
80 STRATEGY_DEFAULT = LSQ2, | |
81 }; | |
82 | |
83 // Creates a velocity tracker using the default strategy for the platform. | |
84 VelocityTracker(); | |
85 | |
86 // Creates a velocity tracker using the specified strategy. | |
87 // If strategy is NULL, uses the default strategy for the platform. | |
88 explicit VelocityTracker(Strategy strategy); | |
89 | |
90 ~VelocityTracker(); | |
91 | |
92 // Resets the velocity tracker state. | |
93 void Clear(); | |
94 | |
95 // Adds movement information for all pointers in a MotionEvent, including | |
96 // historical samples. | |
97 void AddMovement(const MotionEvent& event); | |
98 | |
99 // Gets the velocity of the specified pointer id in position units per second. | |
100 // Returns false and sets the velocity components to zero if there is | |
101 // insufficient movement information for the pointer. | |
102 bool GetVelocity(uint32_t id, float* outVx, float* outVy) const; | |
103 | |
104 // Gets the active pointer id, or -1 if none. | |
105 inline int32_t GetActivePointerId() const { return active_pointer_id_; } | |
106 | |
107 // Gets a bitset containing all pointer ids from the most recent movement. | |
108 inline BitSet32 GetCurrentPointerIdBits() const { | |
109 return current_pointer_id_bits_; | |
110 } | |
111 | |
112 private: | |
113 // Resets the velocity tracker state for specific pointers. | |
114 // Call this method when some pointers have changed and may be reusing | |
115 // an id that was assigned to a different pointer earlier. | |
116 void ClearPointers(BitSet32 id_bits); | |
117 | |
118 // Adds movement information for a set of pointers. | |
119 // The id_bits bitfield specifies the pointer ids of the pointers whose | |
120 // positions | |
121 // are included in the movement. | |
122 // The positions array contains position information for each pointer in order | |
123 // by | |
124 // increasing id. Its size should be equal to the number of one bits in | |
125 // id_bits. | |
126 void AddMovement(const base::TimeTicks& event_time, | |
127 BitSet32 id_bits, | |
128 const Position* positions); | |
129 | |
130 // Gets an estimator for the recent movements of the specified pointer id. | |
131 // Returns false and clears the estimator if there is no information available | |
132 // about the pointer. | |
133 bool GetEstimator(uint32_t id, Estimator* out_estimator) const; | |
134 | |
135 base::TimeTicks last_event_time_; | |
136 BitSet32 current_pointer_id_bits_; | |
137 int32_t active_pointer_id_; | |
138 scoped_ptr<VelocityTrackerStrategy> strategy_; | |
139 }; | |
140 | |
141 } // namespace ui | |
142 | |
143 #endif // UI_EVENTS_GESTURE_DETECTION_VELOCITY_TRACKER_H_ | |
OLD | NEW |