2323
2424namespace android {
2525
26+ class VelocityTrackerStrategy ;
27+
2628/*
2729 * Calculates the velocity of pointer movements over time.
2830 */
2931class VelocityTracker {
3032public:
31- // Default polynomial degree. (used by getVelocity)
32- static const uint32_t DEFAULT_DEGREE = 2 ;
33-
34- // Default sample horizon. (used by getVelocity)
35- // We don't use too much history by default since we want to react to quick
36- // changes in direction.
37- static const nsecs_t DEFAULT_HORIZON = 100 * 1000000 ; // 100 ms
38-
3933 struct Position {
4034 float x, y;
4135 };
@@ -64,6 +58,8 @@ class VelocityTracker {
6458 };
6559
6660 VelocityTracker ();
61+ VelocityTracker (VelocityTrackerStrategy* strategy);
62+ ~VelocityTracker ();
6763
6864 // Resets the velocity tracker state.
6965 void clear ();
@@ -88,35 +84,80 @@ class VelocityTracker {
8884 // insufficient movement information for the pointer.
8985 bool getVelocity (uint32_t id, float * outVx, float * outVy) const ;
9086
91- // Gets a quadratic estimator for the movements of the specified pointer id.
87+ // Gets an estimator for the recent movements of the specified pointer id.
9288 // Returns false and clears the estimator if there is no information available
9389 // about the pointer.
94- bool getEstimator (uint32_t id, uint32_t degree, nsecs_t horizon,
95- Estimator* outEstimator) const ;
90+ bool getEstimator (uint32_t id, Estimator* outEstimator) const ;
9691
9792 // Gets the active pointer id, or -1 if none.
9893 inline int32_t getActivePointerId () const { return mActivePointerId ; }
9994
10095 // Gets a bitset containing all pointer ids from the most recent movement.
101- inline BitSet32 getCurrentPointerIdBits () const { return mMovements [mIndex ].idBits ; }
96+ inline BitSet32 getCurrentPointerIdBits () const { return mCurrentPointerIdBits ; }
97+
98+ private:
99+ BitSet32 mCurrentPointerIdBits ;
100+ int32_t mActivePointerId ;
101+ VelocityTrackerStrategy* mStrategy ;
102+ };
103+
104+
105+ /*
106+ * Implements a particular velocity tracker algorithm.
107+ */
108+ class VelocityTrackerStrategy {
109+ protected:
110+ VelocityTrackerStrategy () { }
111+
112+ public:
113+ virtual ~VelocityTrackerStrategy () { }
114+
115+ virtual void clear () = 0;
116+ virtual void clearPointers (BitSet32 idBits) = 0;
117+ virtual void addMovement (nsecs_t eventTime, BitSet32 idBits,
118+ const VelocityTracker::Position* positions) = 0;
119+ virtual bool getEstimator (uint32_t id, VelocityTracker::Estimator* outEstimator) const = 0;
120+ };
121+
122+
123+ /*
124+ * Velocity tracker algorithm based on least-squares linear regression.
125+ */
126+ class LeastSquaresVelocityTrackerStrategy : public VelocityTrackerStrategy {
127+ public:
128+ LeastSquaresVelocityTrackerStrategy ();
129+ virtual ~LeastSquaresVelocityTrackerStrategy ();
130+
131+ virtual void clear ();
132+ virtual void clearPointers (BitSet32 idBits);
133+ virtual void addMovement (nsecs_t eventTime, BitSet32 idBits,
134+ const VelocityTracker::Position* positions);
135+ virtual bool getEstimator (uint32_t id, VelocityTracker::Estimator* outEstimator) const ;
102136
103137private:
138+ // Polynomial degree. Must be less than or equal to Estimator::MAX_DEGREE.
139+ static const uint32_t DEGREE = 2 ;
140+
141+ // Sample horizon.
142+ // We don't use too much history by default since we want to react to quick
143+ // changes in direction.
144+ static const nsecs_t HORIZON = 100 * 1000000 ; // 100 ms
145+
104146 // Number of samples to keep.
105147 static const uint32_t HISTORY_SIZE = 20 ;
106148
107149 struct Movement {
108150 nsecs_t eventTime;
109151 BitSet32 idBits;
110- Position positions[MAX_POINTERS];
152+ VelocityTracker:: Position positions[MAX_POINTERS];
111153
112- inline const Position& getPosition (uint32_t id) const {
154+ inline const VelocityTracker:: Position& getPosition (uint32_t id) const {
113155 return positions[idBits.getIndexOfBit (id)];
114156 }
115157 };
116158
117159 uint32_t mIndex ;
118160 Movement mMovements [HISTORY_SIZE];
119- int32_t mActivePointerId ;
120161};
121162
122163} // namespace android
0 commit comments