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 | |
| 26 | /* |
| 27 | * Calculates the velocity of pointer movements over time. |
| 28 | */ |
| 29 | class VelocityTracker { |
| 30 | public: |
| 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 | |
| 39 | struct Position { |
| 40 | float x, y; |
| 41 | }; |
| 42 | |
| 43 | struct Estimator { |
| 44 | static const size_t MAX_DEGREE = 2; |
| 45 | |
| 46 | // Polynomial coefficients describing motion in X and Y. |
| 47 | float xCoeff[MAX_DEGREE + 1], yCoeff[MAX_DEGREE + 1]; |
| 48 | |
| 49 | // Polynomial degree (number of coefficients), or zero if no information is |
| 50 | // available. |
| 51 | uint32_t degree; |
| 52 | |
| 53 | // Confidence (coefficient of determination), between 0 (no fit) and 1 (perfect fit). |
| 54 | float confidence; |
| 55 | |
| 56 | inline void clear() { |
| 57 | degree = 0; |
| 58 | confidence = 0; |
| 59 | for (size_t i = 0; i <= MAX_DEGREE; i++) { |
| 60 | xCoeff[i] = 0; |
| 61 | yCoeff[i] = 0; |
| 62 | } |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | VelocityTracker(); |
| 67 | |
| 68 | // Resets the velocity tracker state. |
| 69 | void clear(); |
| 70 | |
| 71 | // Resets the velocity tracker state for specific pointers. |
| 72 | // Call this method when some pointers have changed and may be reusing |
| 73 | // an id that was assigned to a different pointer earlier. |
| 74 | void clearPointers(BitSet32 idBits); |
| 75 | |
| 76 | // Adds movement information for a set of pointers. |
| 77 | // The idBits bitfield specifies the pointer ids of the pointers whose positions |
| 78 | // are included in the movement. |
| 79 | // The positions array contains position information for each pointer in order by |
| 80 | // increasing id. Its size should be equal to the number of one bits in idBits. |
| 81 | void addMovement(nsecs_t eventTime, BitSet32 idBits, const Position* positions); |
| 82 | |
| 83 | // Adds movement information for all pointers in a MotionEvent, including historical samples. |
| 84 | void addMovement(const MotionEvent* event); |
| 85 | |
| 86 | // Gets the velocity of the specified pointer id in position units per second. |
| 87 | // Returns false and sets the velocity components to zero if there is |
| 88 | // insufficient movement information for the pointer. |
| 89 | bool getVelocity(uint32_t id, float* outVx, float* outVy) const; |
| 90 | |
| 91 | // Gets a quadratic estimator for the movements of the specified pointer id. |
| 92 | // Returns false and clears the estimator if there is no information available |
| 93 | // about the pointer. |
| 94 | bool getEstimator(uint32_t id, uint32_t degree, nsecs_t horizon, |
| 95 | Estimator* outEstimator) const; |
| 96 | |
| 97 | // Gets the active pointer id, or -1 if none. |
| 98 | inline int32_t getActivePointerId() const { return mActivePointerId; } |
| 99 | |
| 100 | // Gets a bitset containing all pointer ids from the most recent movement. |
| 101 | inline BitSet32 getCurrentPointerIdBits() const { return mMovements[mIndex].idBits; } |
| 102 | |
| 103 | private: |
| 104 | // Number of samples to keep. |
| 105 | static const uint32_t HISTORY_SIZE = 20; |
| 106 | |
| 107 | struct Movement { |
| 108 | nsecs_t eventTime; |
| 109 | BitSet32 idBits; |
| 110 | Position positions[MAX_POINTERS]; |
| 111 | |
| 112 | inline const Position& getPosition(uint32_t id) const { |
| 113 | return positions[idBits.getIndexOfBit(id)]; |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | uint32_t mIndex; |
| 118 | Movement mMovements[HISTORY_SIZE]; |
| 119 | int32_t mActivePointerId; |
| 120 | }; |
| 121 | |
| 122 | } // namespace android |
| 123 | |
| 124 | #endif // _ANDROIDFW_VELOCITY_TRACKER_H |