2012-05-11 12:24:35 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _ANDROIDFW_VELOCITY_TRACKER_H
|
|
|
|
#define _ANDROIDFW_VELOCITY_TRACKER_H
|
|
|
|
|
|
|
|
#include <androidfw/Input.h>
|
|
|
|
#include <utils/Timers.h>
|
|
|
|
#include <utils/BitSet.h>
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
2012-05-13 15:30:42 -07:00
|
|
|
class VelocityTrackerStrategy;
|
|
|
|
|
2012-05-11 12:24:35 -07:00
|
|
|
/*
|
|
|
|
* Calculates the velocity of pointer movements over time.
|
|
|
|
*/
|
|
|
|
class VelocityTracker {
|
|
|
|
public:
|
|
|
|
struct Position {
|
|
|
|
float x, y;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Estimator {
|
2012-06-01 12:39:25 -07:00
|
|
|
static const size_t MAX_DEGREE = 4;
|
2012-05-11 12:24:35 -07:00
|
|
|
|
2012-05-14 18:46:18 -07:00
|
|
|
// Estimator time base.
|
|
|
|
nsecs_t time;
|
|
|
|
|
2012-05-11 12:24:35 -07:00
|
|
|
// Polynomial coefficients describing motion in X and Y.
|
|
|
|
float xCoeff[MAX_DEGREE + 1], yCoeff[MAX_DEGREE + 1];
|
|
|
|
|
|
|
|
// Polynomial degree (number of coefficients), or zero if no information is
|
|
|
|
// available.
|
|
|
|
uint32_t degree;
|
|
|
|
|
|
|
|
// Confidence (coefficient of determination), between 0 (no fit) and 1 (perfect fit).
|
|
|
|
float confidence;
|
|
|
|
|
|
|
|
inline void clear() {
|
2012-05-14 18:46:18 -07:00
|
|
|
time = 0;
|
2012-05-11 12:24:35 -07:00
|
|
|
degree = 0;
|
|
|
|
confidence = 0;
|
|
|
|
for (size_t i = 0; i <= MAX_DEGREE; i++) {
|
|
|
|
xCoeff[i] = 0;
|
|
|
|
yCoeff[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-01 12:39:25 -07:00
|
|
|
// Creates a velocity tracker using the specified strategy.
|
|
|
|
// If strategy is NULL, uses the default strategy for the platform.
|
|
|
|
VelocityTracker(const char* strategy = NULL);
|
|
|
|
|
2012-05-13 15:30:42 -07:00
|
|
|
~VelocityTracker();
|
2012-05-11 12:24:35 -07:00
|
|
|
|
|
|
|
// Resets the velocity tracker state.
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
// Resets the velocity tracker state for specific pointers.
|
|
|
|
// Call this method when some pointers have changed and may be reusing
|
|
|
|
// an id that was assigned to a different pointer earlier.
|
|
|
|
void clearPointers(BitSet32 idBits);
|
|
|
|
|
|
|
|
// Adds movement information for a set of pointers.
|
|
|
|
// The idBits bitfield specifies the pointer ids of the pointers whose positions
|
|
|
|
// are included in the movement.
|
|
|
|
// The positions array contains position information for each pointer in order by
|
|
|
|
// increasing id. Its size should be equal to the number of one bits in idBits.
|
|
|
|
void addMovement(nsecs_t eventTime, BitSet32 idBits, const Position* positions);
|
|
|
|
|
|
|
|
// Adds movement information for all pointers in a MotionEvent, including historical samples.
|
|
|
|
void addMovement(const MotionEvent* event);
|
|
|
|
|
|
|
|
// Gets the velocity of the specified pointer id in position units per second.
|
|
|
|
// Returns false and sets the velocity components to zero if there is
|
|
|
|
// insufficient movement information for the pointer.
|
|
|
|
bool getVelocity(uint32_t id, float* outVx, float* outVy) const;
|
|
|
|
|
2012-05-13 15:30:42 -07:00
|
|
|
// Gets an estimator for the recent movements of the specified pointer id.
|
2012-05-11 12:24:35 -07:00
|
|
|
// Returns false and clears the estimator if there is no information available
|
|
|
|
// about the pointer.
|
2012-05-13 15:30:42 -07:00
|
|
|
bool getEstimator(uint32_t id, Estimator* outEstimator) const;
|
2012-05-11 12:24:35 -07:00
|
|
|
|
|
|
|
// Gets the active pointer id, or -1 if none.
|
|
|
|
inline int32_t getActivePointerId() const { return mActivePointerId; }
|
|
|
|
|
|
|
|
// Gets a bitset containing all pointer ids from the most recent movement.
|
2012-05-13 15:30:42 -07:00
|
|
|
inline BitSet32 getCurrentPointerIdBits() const { return mCurrentPointerIdBits; }
|
|
|
|
|
|
|
|
private:
|
2012-06-01 12:39:25 -07:00
|
|
|
static const char* DEFAULT_STRATEGY;
|
|
|
|
|
2012-05-14 18:46:18 -07:00
|
|
|
nsecs_t mLastEventTime;
|
2012-05-13 15:30:42 -07:00
|
|
|
BitSet32 mCurrentPointerIdBits;
|
|
|
|
int32_t mActivePointerId;
|
|
|
|
VelocityTrackerStrategy* mStrategy;
|
2012-06-01 12:39:25 -07:00
|
|
|
|
|
|
|
bool configureStrategy(const char* strategy);
|
|
|
|
|
|
|
|
static VelocityTrackerStrategy* createStrategy(const char* strategy);
|
2012-05-13 15:30:42 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implements a particular velocity tracker algorithm.
|
|
|
|
*/
|
|
|
|
class VelocityTrackerStrategy {
|
|
|
|
protected:
|
|
|
|
VelocityTrackerStrategy() { }
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~VelocityTrackerStrategy() { }
|
|
|
|
|
|
|
|
virtual void clear() = 0;
|
|
|
|
virtual void clearPointers(BitSet32 idBits) = 0;
|
|
|
|
virtual void addMovement(nsecs_t eventTime, BitSet32 idBits,
|
|
|
|
const VelocityTracker::Position* positions) = 0;
|
|
|
|
virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Velocity tracker algorithm based on least-squares linear regression.
|
|
|
|
*/
|
|
|
|
class LeastSquaresVelocityTrackerStrategy : public VelocityTrackerStrategy {
|
|
|
|
public:
|
2012-06-01 12:39:25 -07:00
|
|
|
// Degree must be no greater than Estimator::MAX_DEGREE.
|
|
|
|
LeastSquaresVelocityTrackerStrategy(uint32_t degree);
|
2012-05-13 15:30:42 -07:00
|
|
|
virtual ~LeastSquaresVelocityTrackerStrategy();
|
|
|
|
|
|
|
|
virtual void clear();
|
|
|
|
virtual void clearPointers(BitSet32 idBits);
|
|
|
|
virtual void addMovement(nsecs_t eventTime, BitSet32 idBits,
|
|
|
|
const VelocityTracker::Position* positions);
|
|
|
|
virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const;
|
2012-05-11 12:24:35 -07:00
|
|
|
|
|
|
|
private:
|
2012-05-13 15:30:42 -07:00
|
|
|
// Sample horizon.
|
|
|
|
// We don't use too much history by default since we want to react to quick
|
|
|
|
// changes in direction.
|
|
|
|
static const nsecs_t HORIZON = 100 * 1000000; // 100 ms
|
|
|
|
|
2012-05-11 12:24:35 -07:00
|
|
|
// Number of samples to keep.
|
|
|
|
static const uint32_t HISTORY_SIZE = 20;
|
|
|
|
|
|
|
|
struct Movement {
|
|
|
|
nsecs_t eventTime;
|
|
|
|
BitSet32 idBits;
|
2012-05-13 15:30:42 -07:00
|
|
|
VelocityTracker::Position positions[MAX_POINTERS];
|
2012-05-11 12:24:35 -07:00
|
|
|
|
2012-05-13 15:30:42 -07:00
|
|
|
inline const VelocityTracker::Position& getPosition(uint32_t id) const {
|
2012-05-11 12:24:35 -07:00
|
|
|
return positions[idBits.getIndexOfBit(id)];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-01 12:39:25 -07:00
|
|
|
const uint32_t mDegree;
|
2012-05-11 12:24:35 -07:00
|
|
|
uint32_t mIndex;
|
|
|
|
Movement mMovements[HISTORY_SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace android
|
|
|
|
|
|
|
|
#endif // _ANDROIDFW_VELOCITY_TRACKER_H
|