Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _ANDROIDFW_VELOCITY_TRACKER_H |
| 18 | #define _ANDROIDFW_VELOCITY_TRACKER_H |
| 19 | |
| 20 | #include <androidfw/Input.h> |
| 21 | #include <utils/Timers.h> |
| 22 | #include <utils/BitSet.h> |
| 23 | |
| 24 | namespace android { |
| 25 | |
Jeff Brown | 85bd0d6 | 2012-05-13 15:30:42 -0700 | [diff] [blame] | 26 | class VelocityTrackerStrategy; |
| 27 | |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 28 | /* |
| 29 | * Calculates the velocity of pointer movements over time. |
| 30 | */ |
| 31 | class VelocityTracker { |
| 32 | public: |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 33 | struct Position { |
| 34 | float x, y; |
| 35 | }; |
| 36 | |
| 37 | struct Estimator { |
Jeff Brown | 9eb7d86 | 2012-06-01 12:39:25 -0700 | [diff] [blame] | 38 | static const size_t MAX_DEGREE = 4; |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 39 | |
Jeff Brown | 9072940 | 2012-05-14 18:46:18 -0700 | [diff] [blame] | 40 | // Estimator time base. |
| 41 | nsecs_t time; |
| 42 | |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 43 | // Polynomial coefficients describing motion in X and Y. |
| 44 | float xCoeff[MAX_DEGREE + 1], yCoeff[MAX_DEGREE + 1]; |
| 45 | |
| 46 | // Polynomial degree (number of coefficients), or zero if no information is |
| 47 | // available. |
| 48 | uint32_t degree; |
| 49 | |
| 50 | // Confidence (coefficient of determination), between 0 (no fit) and 1 (perfect fit). |
| 51 | float confidence; |
| 52 | |
| 53 | inline void clear() { |
Jeff Brown | 9072940 | 2012-05-14 18:46:18 -0700 | [diff] [blame] | 54 | time = 0; |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 55 | degree = 0; |
| 56 | confidence = 0; |
| 57 | for (size_t i = 0; i <= MAX_DEGREE; i++) { |
| 58 | xCoeff[i] = 0; |
| 59 | yCoeff[i] = 0; |
| 60 | } |
| 61 | } |
| 62 | }; |
| 63 | |
Jeff Brown | 9eb7d86 | 2012-06-01 12:39:25 -0700 | [diff] [blame] | 64 | // Creates a velocity tracker using the specified strategy. |
| 65 | // If strategy is NULL, uses the default strategy for the platform. |
| 66 | VelocityTracker(const char* strategy = NULL); |
| 67 | |
Jeff Brown | 85bd0d6 | 2012-05-13 15:30:42 -0700 | [diff] [blame] | 68 | ~VelocityTracker(); |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 69 | |
| 70 | // Resets the velocity tracker state. |
| 71 | void clear(); |
| 72 | |
| 73 | // Resets the velocity tracker state for specific pointers. |
| 74 | // Call this method when some pointers have changed and may be reusing |
| 75 | // an id that was assigned to a different pointer earlier. |
| 76 | void clearPointers(BitSet32 idBits); |
| 77 | |
| 78 | // Adds movement information for a set of pointers. |
| 79 | // The idBits bitfield specifies the pointer ids of the pointers whose positions |
| 80 | // are included in the movement. |
| 81 | // The positions array contains position information for each pointer in order by |
| 82 | // increasing id. Its size should be equal to the number of one bits in idBits. |
| 83 | void addMovement(nsecs_t eventTime, BitSet32 idBits, const Position* positions); |
| 84 | |
| 85 | // Adds movement information for all pointers in a MotionEvent, including historical samples. |
| 86 | void addMovement(const MotionEvent* event); |
| 87 | |
| 88 | // Gets the velocity of the specified pointer id in position units per second. |
| 89 | // Returns false and sets the velocity components to zero if there is |
| 90 | // insufficient movement information for the pointer. |
| 91 | bool getVelocity(uint32_t id, float* outVx, float* outVy) const; |
| 92 | |
Jeff Brown | 85bd0d6 | 2012-05-13 15:30:42 -0700 | [diff] [blame] | 93 | // Gets an estimator for the recent movements of the specified pointer id. |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 94 | // Returns false and clears the estimator if there is no information available |
| 95 | // about the pointer. |
Jeff Brown | 85bd0d6 | 2012-05-13 15:30:42 -0700 | [diff] [blame] | 96 | bool getEstimator(uint32_t id, Estimator* outEstimator) const; |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 97 | |
| 98 | // Gets the active pointer id, or -1 if none. |
| 99 | inline int32_t getActivePointerId() const { return mActivePointerId; } |
| 100 | |
| 101 | // Gets a bitset containing all pointer ids from the most recent movement. |
Jeff Brown | 85bd0d6 | 2012-05-13 15:30:42 -0700 | [diff] [blame] | 102 | inline BitSet32 getCurrentPointerIdBits() const { return mCurrentPointerIdBits; } |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 103 | |
| 104 | private: |
Jeff Brown | 9eb7d86 | 2012-06-01 12:39:25 -0700 | [diff] [blame] | 105 | static const char* DEFAULT_STRATEGY; |
| 106 | |
Jeff Brown | 9072940 | 2012-05-14 18:46:18 -0700 | [diff] [blame] | 107 | nsecs_t mLastEventTime; |
Jeff Brown | 85bd0d6 | 2012-05-13 15:30:42 -0700 | [diff] [blame] | 108 | BitSet32 mCurrentPointerIdBits; |
| 109 | int32_t mActivePointerId; |
| 110 | VelocityTrackerStrategy* mStrategy; |
Jeff Brown | 9eb7d86 | 2012-06-01 12:39:25 -0700 | [diff] [blame] | 111 | |
| 112 | bool configureStrategy(const char* strategy); |
| 113 | |
| 114 | static VelocityTrackerStrategy* createStrategy(const char* strategy); |
Jeff Brown | 85bd0d6 | 2012-05-13 15:30:42 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | |
| 118 | /* |
| 119 | * Implements a particular velocity tracker algorithm. |
| 120 | */ |
| 121 | class VelocityTrackerStrategy { |
| 122 | protected: |
| 123 | VelocityTrackerStrategy() { } |
| 124 | |
| 125 | public: |
| 126 | virtual ~VelocityTrackerStrategy() { } |
| 127 | |
| 128 | virtual void clear() = 0; |
| 129 | virtual void clearPointers(BitSet32 idBits) = 0; |
| 130 | virtual void addMovement(nsecs_t eventTime, BitSet32 idBits, |
| 131 | const VelocityTracker::Position* positions) = 0; |
| 132 | virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const = 0; |
| 133 | }; |
| 134 | |
| 135 | |
| 136 | /* |
| 137 | * Velocity tracker algorithm based on least-squares linear regression. |
| 138 | */ |
| 139 | class LeastSquaresVelocityTrackerStrategy : public VelocityTrackerStrategy { |
| 140 | public: |
Jeff Brown | 18f329e | 2012-06-03 14:18:26 -0700 | [diff] [blame] | 141 | enum Weighting { |
| 142 | // No weights applied. All data points are equally reliable. |
| 143 | WEIGHTING_NONE, |
| 144 | |
| 145 | // Weight by time delta. Data points clustered together are weighted less. |
| 146 | WEIGHTING_DELTA, |
| 147 | |
| 148 | // Weight such that points within a certain horizon are weighed more than those |
| 149 | // outside of that horizon. |
| 150 | WEIGHTING_CENTRAL, |
| 151 | |
| 152 | // Weight such that points older than a certain amount are weighed less. |
| 153 | WEIGHTING_RECENT, |
| 154 | }; |
| 155 | |
Jeff Brown | 9eb7d86 | 2012-06-01 12:39:25 -0700 | [diff] [blame] | 156 | // Degree must be no greater than Estimator::MAX_DEGREE. |
Jeff Brown | 18f329e | 2012-06-03 14:18:26 -0700 | [diff] [blame] | 157 | LeastSquaresVelocityTrackerStrategy(uint32_t degree, Weighting weighting = WEIGHTING_NONE); |
Jeff Brown | 85bd0d6 | 2012-05-13 15:30:42 -0700 | [diff] [blame] | 158 | virtual ~LeastSquaresVelocityTrackerStrategy(); |
| 159 | |
| 160 | virtual void clear(); |
| 161 | virtual void clearPointers(BitSet32 idBits); |
| 162 | virtual void addMovement(nsecs_t eventTime, BitSet32 idBits, |
| 163 | const VelocityTracker::Position* positions); |
| 164 | virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const; |
| 165 | |
| 166 | private: |
Jeff Brown | 85bd0d6 | 2012-05-13 15:30:42 -0700 | [diff] [blame] | 167 | // Sample horizon. |
| 168 | // We don't use too much history by default since we want to react to quick |
| 169 | // changes in direction. |
| 170 | static const nsecs_t HORIZON = 100 * 1000000; // 100 ms |
| 171 | |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 172 | // Number of samples to keep. |
| 173 | static const uint32_t HISTORY_SIZE = 20; |
| 174 | |
| 175 | struct Movement { |
| 176 | nsecs_t eventTime; |
| 177 | BitSet32 idBits; |
Jeff Brown | 85bd0d6 | 2012-05-13 15:30:42 -0700 | [diff] [blame] | 178 | VelocityTracker::Position positions[MAX_POINTERS]; |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 179 | |
Jeff Brown | 85bd0d6 | 2012-05-13 15:30:42 -0700 | [diff] [blame] | 180 | inline const VelocityTracker::Position& getPosition(uint32_t id) const { |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 181 | return positions[idBits.getIndexOfBit(id)]; |
| 182 | } |
| 183 | }; |
| 184 | |
Jeff Brown | 18f329e | 2012-06-03 14:18:26 -0700 | [diff] [blame] | 185 | float chooseWeight(uint32_t index) const; |
| 186 | |
Jeff Brown | 9eb7d86 | 2012-06-01 12:39:25 -0700 | [diff] [blame] | 187 | const uint32_t mDegree; |
Jeff Brown | 18f329e | 2012-06-03 14:18:26 -0700 | [diff] [blame] | 188 | const Weighting mWeighting; |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 189 | uint32_t mIndex; |
| 190 | Movement mMovements[HISTORY_SIZE]; |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
Jeff Brown | 53dd12a | 2012-06-01 13:24:04 -0700 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * Velocity tracker algorithm that uses an IIR filter. |
| 196 | */ |
| 197 | class IntegratingVelocityTrackerStrategy : public VelocityTrackerStrategy { |
| 198 | public: |
Jeff Brown | a5b0698 | 2012-06-03 22:46:07 -0700 | [diff] [blame] | 199 | // Degree must be 1 or 2. |
| 200 | IntegratingVelocityTrackerStrategy(uint32_t degree); |
Jeff Brown | 53dd12a | 2012-06-01 13:24:04 -0700 | [diff] [blame] | 201 | ~IntegratingVelocityTrackerStrategy(); |
| 202 | |
| 203 | virtual void clear(); |
| 204 | virtual void clearPointers(BitSet32 idBits); |
| 205 | virtual void addMovement(nsecs_t eventTime, BitSet32 idBits, |
| 206 | const VelocityTracker::Position* positions); |
| 207 | virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const; |
| 208 | |
| 209 | private: |
| 210 | // Current state estimate for a particular pointer. |
| 211 | struct State { |
| 212 | nsecs_t updateTime; |
Jeff Brown | a5b0698 | 2012-06-03 22:46:07 -0700 | [diff] [blame] | 213 | uint32_t degree; |
Jeff Brown | 53dd12a | 2012-06-01 13:24:04 -0700 | [diff] [blame] | 214 | |
Jeff Brown | a5b0698 | 2012-06-03 22:46:07 -0700 | [diff] [blame] | 215 | float xpos, xvel, xaccel; |
| 216 | float ypos, yvel, yaccel; |
Jeff Brown | 53dd12a | 2012-06-01 13:24:04 -0700 | [diff] [blame] | 217 | }; |
| 218 | |
Jeff Brown | a5b0698 | 2012-06-03 22:46:07 -0700 | [diff] [blame] | 219 | const uint32_t mDegree; |
Jeff Brown | 53dd12a | 2012-06-01 13:24:04 -0700 | [diff] [blame] | 220 | BitSet32 mPointerIdBits; |
| 221 | State mPointerState[MAX_POINTER_ID + 1]; |
| 222 | |
Jeff Brown | a5b0698 | 2012-06-03 22:46:07 -0700 | [diff] [blame] | 223 | void initState(State& state, nsecs_t eventTime, float xpos, float ypos) const; |
| 224 | void updateState(State& state, nsecs_t eventTime, float xpos, float ypos) const; |
| 225 | void populateEstimator(const State& state, VelocityTracker::Estimator* outEstimator) const; |
Jeff Brown | 53dd12a | 2012-06-01 13:24:04 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
Jeff Brown | 51df04b | 2012-06-03 23:14:14 -0700 | [diff] [blame] | 228 | |
| 229 | /* |
| 230 | * Velocity tracker strategy used prior to ICS. |
| 231 | */ |
| 232 | class LegacyVelocityTrackerStrategy : public VelocityTrackerStrategy { |
| 233 | public: |
| 234 | LegacyVelocityTrackerStrategy(); |
| 235 | virtual ~LegacyVelocityTrackerStrategy(); |
| 236 | |
| 237 | virtual void clear(); |
| 238 | virtual void clearPointers(BitSet32 idBits); |
| 239 | virtual void addMovement(nsecs_t eventTime, BitSet32 idBits, |
| 240 | const VelocityTracker::Position* positions); |
| 241 | virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const; |
| 242 | |
| 243 | private: |
| 244 | // Oldest sample to consider when calculating the velocity. |
| 245 | static const nsecs_t HORIZON = 200 * 1000000; // 100 ms |
| 246 | |
| 247 | // Number of samples to keep. |
| 248 | static const uint32_t HISTORY_SIZE = 20; |
| 249 | |
| 250 | // The minimum duration between samples when estimating velocity. |
| 251 | static const nsecs_t MIN_DURATION = 10 * 1000000; // 10 ms |
| 252 | |
| 253 | struct Movement { |
| 254 | nsecs_t eventTime; |
| 255 | BitSet32 idBits; |
| 256 | VelocityTracker::Position positions[MAX_POINTERS]; |
| 257 | |
| 258 | inline const VelocityTracker::Position& getPosition(uint32_t id) const { |
| 259 | return positions[idBits.getIndexOfBit(id)]; |
| 260 | } |
| 261 | }; |
| 262 | |
| 263 | uint32_t mIndex; |
| 264 | Movement mMovements[HISTORY_SIZE]; |
| 265 | }; |
| 266 | |
Jeff Brown | 8a90e6e | 2012-05-11 12:24:35 -0700 | [diff] [blame] | 267 | } // namespace android |
| 268 | |
| 269 | #endif // _ANDROIDFW_VELOCITY_TRACKER_H |