blob: be1a33d28660458ac6093550c74d4a1e2894daec [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
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#define LOG_TAG "VelocityTracker"
18//#define LOG_NDEBUG 0
19
20// Log debug messages about velocity tracking.
21#define DEBUG_VELOCITY 0
22
23// Log debug messages about the progress of the algorithm itself.
24#define DEBUG_STRATEGY 0
25
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -070026#include <array>
Siarhei Vishniakou7b9d1892017-07-05 18:58:41 -070027#include <inttypes.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028#include <limits.h>
Siarhei Vishniakou7b9d1892017-07-05 18:58:41 -070029#include <math.h>
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -070030#include <optional>
Jeff Brown5912f952013-07-01 19:10:31 -070031
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070032#include <android-base/stringprintf.h>
Jeff Brown5912f952013-07-01 19:10:31 -070033#include <cutils/properties.h>
34#include <input/VelocityTracker.h>
35#include <utils/BitSet.h>
Jeff Brown5912f952013-07-01 19:10:31 -070036#include <utils/Timers.h>
37
38namespace android {
39
40// Nanoseconds per milliseconds.
41static const nsecs_t NANOS_PER_MS = 1000000;
42
43// Threshold for determining that a pointer has stopped moving.
44// Some input devices do not send ACTION_MOVE events in the case where a pointer has
45// stopped. We need to detect this case so that we can accurately predict the
46// velocity after the pointer starts moving again.
47static const nsecs_t ASSUME_POINTER_STOPPED_TIME = 40 * NANOS_PER_MS;
48
49
50static float vectorDot(const float* a, const float* b, uint32_t m) {
51 float r = 0;
Siarhei Vishniakou7b9d1892017-07-05 18:58:41 -070052 for (size_t i = 0; i < m; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -070053 r += *(a++) * *(b++);
54 }
55 return r;
56}
57
58static float vectorNorm(const float* a, uint32_t m) {
59 float r = 0;
Siarhei Vishniakou7b9d1892017-07-05 18:58:41 -070060 for (size_t i = 0; i < m; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -070061 float t = *(a++);
62 r += t * t;
63 }
64 return sqrtf(r);
65}
66
67#if DEBUG_STRATEGY || DEBUG_VELOCITY
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070068static std::string vectorToString(const float* a, uint32_t m) {
69 std::string str;
70 str += "[";
Siarhei Vishniakou7b9d1892017-07-05 18:58:41 -070071 for (size_t i = 0; i < m; i++) {
72 if (i) {
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070073 str += ",";
Jeff Brown5912f952013-07-01 19:10:31 -070074 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070075 str += android::base::StringPrintf(" %f", *(a++));
Jeff Brown5912f952013-07-01 19:10:31 -070076 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070077 str += " ]";
Jeff Brown5912f952013-07-01 19:10:31 -070078 return str;
79}
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010080#endif
Jeff Brown5912f952013-07-01 19:10:31 -070081
Siarhei Vishniakoud4b607e2017-06-13 12:21:59 +010082#if DEBUG_STRATEGY
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070083static std::string matrixToString(const float* a, uint32_t m, uint32_t n, bool rowMajor) {
84 std::string str;
85 str = "[";
Jeff Brown5912f952013-07-01 19:10:31 -070086 for (size_t i = 0; i < m; i++) {
87 if (i) {
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070088 str += ",";
Jeff Brown5912f952013-07-01 19:10:31 -070089 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070090 str += " [";
Jeff Brown5912f952013-07-01 19:10:31 -070091 for (size_t j = 0; j < n; j++) {
92 if (j) {
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070093 str += ",";
Jeff Brown5912f952013-07-01 19:10:31 -070094 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070095 str += android::base::StringPrintf(" %f", a[rowMajor ? i * n + j : j * m + i]);
Jeff Brown5912f952013-07-01 19:10:31 -070096 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070097 str += " ]";
Jeff Brown5912f952013-07-01 19:10:31 -070098 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070099 str += " ]";
Jeff Brown5912f952013-07-01 19:10:31 -0700100 return str;
101}
102#endif
103
104
105// --- VelocityTracker ---
106
Chris Yef8591482020-04-17 11:49:17 -0700107VelocityTracker::VelocityTracker(const Strategy strategy)
108 : mLastEventTime(0), mCurrentPointerIdBits(0), mActivePointerId(-1) {
Jeff Brown5912f952013-07-01 19:10:31 -0700109 // Configure the strategy.
110 if (!configureStrategy(strategy)) {
Chris Yef8591482020-04-17 11:49:17 -0700111 ALOGE("Unrecognized velocity tracker strategy %" PRId32 ".", strategy);
112 if (!configureStrategy(VelocityTracker::DEFAULT_STRATEGY)) {
113 LOG_ALWAYS_FATAL("Could not create the default velocity tracker strategy '%" PRId32
114 "'!",
115 strategy);
Jeff Brown5912f952013-07-01 19:10:31 -0700116 }
117 }
118}
119
120VelocityTracker::~VelocityTracker() {
Jeff Brown5912f952013-07-01 19:10:31 -0700121}
122
Chris Yef8591482020-04-17 11:49:17 -0700123bool VelocityTracker::configureStrategy(Strategy strategy) {
124 if (strategy == VelocityTracker::Strategy::DEFAULT) {
125 mStrategy = createStrategy(VelocityTracker::DEFAULT_STRATEGY);
126 } else {
127 mStrategy = createStrategy(strategy);
128 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700129 return mStrategy != nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700130}
131
Chris Yef8591482020-04-17 11:49:17 -0700132std::unique_ptr<VelocityTrackerStrategy> VelocityTracker::createStrategy(
133 VelocityTracker::Strategy strategy) {
134 switch (strategy) {
135 case VelocityTracker::Strategy::IMPULSE:
136 return std::make_unique<ImpulseVelocityTrackerStrategy>();
137
138 case VelocityTracker::Strategy::LSQ1:
139 return std::make_unique<LeastSquaresVelocityTrackerStrategy>(1);
140
141 case VelocityTracker::Strategy::LSQ2:
142 return std::make_unique<LeastSquaresVelocityTrackerStrategy>(2);
143
144 case VelocityTracker::Strategy::LSQ3:
145 return std::make_unique<LeastSquaresVelocityTrackerStrategy>(3);
146
147 case VelocityTracker::Strategy::WLSQ2_DELTA:
148 return std::make_unique<
149 LeastSquaresVelocityTrackerStrategy>(2,
150 LeastSquaresVelocityTrackerStrategy::
151 WEIGHTING_DELTA);
152 case VelocityTracker::Strategy::WLSQ2_CENTRAL:
153 return std::make_unique<
154 LeastSquaresVelocityTrackerStrategy>(2,
155 LeastSquaresVelocityTrackerStrategy::
156 WEIGHTING_CENTRAL);
157 case VelocityTracker::Strategy::WLSQ2_RECENT:
158 return std::make_unique<
159 LeastSquaresVelocityTrackerStrategy>(2,
160 LeastSquaresVelocityTrackerStrategy::
161 WEIGHTING_RECENT);
162
163 case VelocityTracker::Strategy::INT1:
164 return std::make_unique<IntegratingVelocityTrackerStrategy>(1);
165
166 case VelocityTracker::Strategy::INT2:
167 return std::make_unique<IntegratingVelocityTrackerStrategy>(2);
168
169 case VelocityTracker::Strategy::LEGACY:
170 return std::make_unique<LegacyVelocityTrackerStrategy>();
171
172 default:
173 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700174 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700175 return nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700176}
177
178void VelocityTracker::clear() {
179 mCurrentPointerIdBits.clear();
180 mActivePointerId = -1;
181
182 mStrategy->clear();
183}
184
185void VelocityTracker::clearPointers(BitSet32 idBits) {
186 BitSet32 remainingIdBits(mCurrentPointerIdBits.value & ~idBits.value);
187 mCurrentPointerIdBits = remainingIdBits;
188
189 if (mActivePointerId >= 0 && idBits.hasBit(mActivePointerId)) {
190 mActivePointerId = !remainingIdBits.isEmpty() ? remainingIdBits.firstMarkedBit() : -1;
191 }
192
193 mStrategy->clearPointers(idBits);
194}
195
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500196void VelocityTracker::addMovement(nsecs_t eventTime, BitSet32 idBits,
197 const std::vector<VelocityTracker::Position>& positions) {
198 LOG_ALWAYS_FATAL_IF(idBits.count() != positions.size(),
199 "Mismatching number of pointers, idBits=%" PRIu32 ", positions=%zu",
200 idBits.count(), positions.size());
Jeff Brown5912f952013-07-01 19:10:31 -0700201 while (idBits.count() > MAX_POINTERS) {
202 idBits.clearLastMarkedBit();
203 }
204
205 if ((mCurrentPointerIdBits.value & idBits.value)
206 && eventTime >= mLastEventTime + ASSUME_POINTER_STOPPED_TIME) {
207#if DEBUG_VELOCITY
208 ALOGD("VelocityTracker: stopped for %0.3f ms, clearing state.",
209 (eventTime - mLastEventTime) * 0.000001f);
210#endif
211 // We have not received any movements for too long. Assume that all pointers
212 // have stopped.
213 mStrategy->clear();
214 }
215 mLastEventTime = eventTime;
216
217 mCurrentPointerIdBits = idBits;
218 if (mActivePointerId < 0 || !idBits.hasBit(mActivePointerId)) {
219 mActivePointerId = idBits.isEmpty() ? -1 : idBits.firstMarkedBit();
220 }
221
222 mStrategy->addMovement(eventTime, idBits, positions);
223
224#if DEBUG_VELOCITY
Siarhei Vishniakou7b9d1892017-07-05 18:58:41 -0700225 ALOGD("VelocityTracker: addMovement eventTime=%" PRId64 ", idBits=0x%08x, activePointerId=%d",
Jeff Brown5912f952013-07-01 19:10:31 -0700226 eventTime, idBits.value, mActivePointerId);
227 for (BitSet32 iterBits(idBits); !iterBits.isEmpty(); ) {
228 uint32_t id = iterBits.firstMarkedBit();
229 uint32_t index = idBits.getIndexOfBit(id);
230 iterBits.clearBit(id);
231 Estimator estimator;
232 getEstimator(id, &estimator);
233 ALOGD(" %d: position (%0.3f, %0.3f), "
234 "estimator (degree=%d, xCoeff=%s, yCoeff=%s, confidence=%f)",
235 id, positions[index].x, positions[index].y,
236 int(estimator.degree),
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700237 vectorToString(estimator.xCoeff, estimator.degree + 1).c_str(),
238 vectorToString(estimator.yCoeff, estimator.degree + 1).c_str(),
Jeff Brown5912f952013-07-01 19:10:31 -0700239 estimator.confidence);
240 }
241#endif
242}
243
244void VelocityTracker::addMovement(const MotionEvent* event) {
245 int32_t actionMasked = event->getActionMasked();
246
247 switch (actionMasked) {
248 case AMOTION_EVENT_ACTION_DOWN:
249 case AMOTION_EVENT_ACTION_HOVER_ENTER:
250 // Clear all pointers on down before adding the new movement.
251 clear();
252 break;
253 case AMOTION_EVENT_ACTION_POINTER_DOWN: {
254 // Start a new movement trace for a pointer that just went down.
255 // We do this on down instead of on up because the client may want to query the
256 // final velocity for a pointer that just went up.
257 BitSet32 downIdBits;
258 downIdBits.markBit(event->getPointerId(event->getActionIndex()));
259 clearPointers(downIdBits);
260 break;
261 }
262 case AMOTION_EVENT_ACTION_MOVE:
263 case AMOTION_EVENT_ACTION_HOVER_MOVE:
264 break;
265 default:
266 // Ignore all other actions because they do not convey any new information about
267 // pointer movement. We also want to preserve the last known velocity of the pointers.
268 // Note that ACTION_UP and ACTION_POINTER_UP always report the last known position
269 // of the pointers that went up. ACTION_POINTER_UP does include the new position of
270 // pointers that remained down but we will also receive an ACTION_MOVE with this
271 // information if any of them actually moved. Since we don't know how many pointers
272 // will be going up at once it makes sense to just wait for the following ACTION_MOVE
273 // before adding the movement.
274 return;
275 }
276
277 size_t pointerCount = event->getPointerCount();
278 if (pointerCount > MAX_POINTERS) {
279 pointerCount = MAX_POINTERS;
280 }
281
282 BitSet32 idBits;
283 for (size_t i = 0; i < pointerCount; i++) {
284 idBits.markBit(event->getPointerId(i));
285 }
286
287 uint32_t pointerIndex[MAX_POINTERS];
288 for (size_t i = 0; i < pointerCount; i++) {
289 pointerIndex[i] = idBits.getIndexOfBit(event->getPointerId(i));
290 }
291
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500292 std::vector<Position> positions;
293 positions.resize(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700294
295 size_t historySize = event->getHistorySize();
Siarhei Vishniakou69e4d0f2020-09-14 19:53:29 -0500296 for (size_t h = 0; h <= historySize; h++) {
297 nsecs_t eventTime = event->getHistoricalEventTime(h);
Jeff Brown5912f952013-07-01 19:10:31 -0700298 for (size_t i = 0; i < pointerCount; i++) {
299 uint32_t index = pointerIndex[i];
Siarhei Vishniakou4c3137a2018-11-13 13:33:52 -0800300 positions[index].x = event->getHistoricalX(i, h);
301 positions[index].y = event->getHistoricalY(i, h);
Jeff Brown5912f952013-07-01 19:10:31 -0700302 }
303 addMovement(eventTime, idBits, positions);
304 }
Jeff Brown5912f952013-07-01 19:10:31 -0700305}
306
307bool VelocityTracker::getVelocity(uint32_t id, float* outVx, float* outVy) const {
308 Estimator estimator;
309 if (getEstimator(id, &estimator) && estimator.degree >= 1) {
310 *outVx = estimator.xCoeff[1];
311 *outVy = estimator.yCoeff[1];
312 return true;
313 }
314 *outVx = 0;
315 *outVy = 0;
316 return false;
317}
318
319bool VelocityTracker::getEstimator(uint32_t id, Estimator* outEstimator) const {
320 return mStrategy->getEstimator(id, outEstimator);
321}
322
323
324// --- LeastSquaresVelocityTrackerStrategy ---
325
Jeff Brown5912f952013-07-01 19:10:31 -0700326LeastSquaresVelocityTrackerStrategy::LeastSquaresVelocityTrackerStrategy(
327 uint32_t degree, Weighting weighting) :
328 mDegree(degree), mWeighting(weighting) {
329 clear();
330}
331
332LeastSquaresVelocityTrackerStrategy::~LeastSquaresVelocityTrackerStrategy() {
333}
334
335void LeastSquaresVelocityTrackerStrategy::clear() {
336 mIndex = 0;
337 mMovements[0].idBits.clear();
338}
339
340void LeastSquaresVelocityTrackerStrategy::clearPointers(BitSet32 idBits) {
341 BitSet32 remainingIdBits(mMovements[mIndex].idBits.value & ~idBits.value);
342 mMovements[mIndex].idBits = remainingIdBits;
343}
344
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500345void LeastSquaresVelocityTrackerStrategy::addMovement(
346 nsecs_t eventTime, BitSet32 idBits,
347 const std::vector<VelocityTracker::Position>& positions) {
Siarhei Vishniakou346ac6a2019-04-10 09:58:05 -0700348 if (mMovements[mIndex].eventTime != eventTime) {
349 // When ACTION_POINTER_DOWN happens, we will first receive ACTION_MOVE with the coordinates
350 // of the existing pointers, and then ACTION_POINTER_DOWN with the coordinates that include
351 // the new pointer. If the eventtimes for both events are identical, just update the data
352 // for this time.
353 // We only compare against the last value, as it is likely that addMovement is called
354 // in chronological order as events occur.
355 mIndex++;
356 }
357 if (mIndex == HISTORY_SIZE) {
Jeff Brown5912f952013-07-01 19:10:31 -0700358 mIndex = 0;
359 }
360
361 Movement& movement = mMovements[mIndex];
362 movement.eventTime = eventTime;
363 movement.idBits = idBits;
364 uint32_t count = idBits.count();
365 for (uint32_t i = 0; i < count; i++) {
366 movement.positions[i] = positions[i];
367 }
368}
369
370/**
371 * Solves a linear least squares problem to obtain a N degree polynomial that fits
372 * the specified input data as nearly as possible.
373 *
374 * Returns true if a solution is found, false otherwise.
375 *
376 * The input consists of two vectors of data points X and Y with indices 0..m-1
377 * along with a weight vector W of the same size.
378 *
379 * The output is a vector B with indices 0..n that describes a polynomial
380 * that fits the data, such the sum of W[i] * W[i] * abs(Y[i] - (B[0] + B[1] X[i]
381 * + B[2] X[i]^2 ... B[n] X[i]^n)) for all i between 0 and m-1 is minimized.
382 *
383 * Accordingly, the weight vector W should be initialized by the caller with the
384 * reciprocal square root of the variance of the error in each input data point.
385 * In other words, an ideal choice for W would be W[i] = 1 / var(Y[i]) = 1 / stddev(Y[i]).
386 * The weights express the relative importance of each data point. If the weights are
387 * all 1, then the data points are considered to be of equal importance when fitting
388 * the polynomial. It is a good idea to choose weights that diminish the importance
389 * of data points that may have higher than usual error margins.
390 *
391 * Errors among data points are assumed to be independent. W is represented here
392 * as a vector although in the literature it is typically taken to be a diagonal matrix.
393 *
394 * That is to say, the function that generated the input data can be approximated
395 * by y(x) ~= B[0] + B[1] x + B[2] x^2 + ... + B[n] x^n.
396 *
397 * The coefficient of determination (R^2) is also returned to describe the goodness
398 * of fit of the model for the given data. It is a value between 0 and 1, where 1
399 * indicates perfect correspondence.
400 *
401 * This function first expands the X vector to a m by n matrix A such that
402 * A[i][0] = 1, A[i][1] = X[i], A[i][2] = X[i]^2, ..., A[i][n] = X[i]^n, then
403 * multiplies it by w[i]./
404 *
405 * Then it calculates the QR decomposition of A yielding an m by m orthonormal matrix Q
406 * and an m by n upper triangular matrix R. Because R is upper triangular (lower
407 * part is all zeroes), we can simplify the decomposition into an m by n matrix
408 * Q1 and a n by n matrix R1 such that A = Q1 R1.
409 *
410 * Finally we solve the system of linear equations given by R1 B = (Qtranspose W Y)
411 * to find B.
412 *
413 * For efficiency, we lay out A and Q column-wise in memory because we frequently
414 * operate on the column vectors. Conversely, we lay out R row-wise.
415 *
416 * http://en.wikipedia.org/wiki/Numerical_methods_for_linear_least_squares
417 * http://en.wikipedia.org/wiki/Gram-Schmidt
418 */
419static bool solveLeastSquares(const float* x, const float* y,
420 const float* w, uint32_t m, uint32_t n, float* outB, float* outDet) {
421#if DEBUG_STRATEGY
422 ALOGD("solveLeastSquares: m=%d, n=%d, x=%s, y=%s, w=%s", int(m), int(n),
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700423 vectorToString(x, m).c_str(), vectorToString(y, m).c_str(),
424 vectorToString(w, m).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700425#endif
426
427 // Expand the X vector to a matrix A, pre-multiplied by the weights.
428 float a[n][m]; // column-major order
429 for (uint32_t h = 0; h < m; h++) {
430 a[0][h] = w[h];
431 for (uint32_t i = 1; i < n; i++) {
432 a[i][h] = a[i - 1][h] * x[h];
433 }
434 }
435#if DEBUG_STRATEGY
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700436 ALOGD(" - a=%s", matrixToString(&a[0][0], m, n, false /*rowMajor*/).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700437#endif
438
439 // Apply the Gram-Schmidt process to A to obtain its QR decomposition.
440 float q[n][m]; // orthonormal basis, column-major order
441 float r[n][n]; // upper triangular matrix, row-major order
442 for (uint32_t j = 0; j < n; j++) {
443 for (uint32_t h = 0; h < m; h++) {
444 q[j][h] = a[j][h];
445 }
446 for (uint32_t i = 0; i < j; i++) {
447 float dot = vectorDot(&q[j][0], &q[i][0], m);
448 for (uint32_t h = 0; h < m; h++) {
449 q[j][h] -= dot * q[i][h];
450 }
451 }
452
453 float norm = vectorNorm(&q[j][0], m);
454 if (norm < 0.000001f) {
455 // vectors are linearly dependent or zero so no solution
456#if DEBUG_STRATEGY
457 ALOGD(" - no solution, norm=%f", norm);
458#endif
459 return false;
460 }
461
462 float invNorm = 1.0f / norm;
463 for (uint32_t h = 0; h < m; h++) {
464 q[j][h] *= invNorm;
465 }
466 for (uint32_t i = 0; i < n; i++) {
467 r[j][i] = i < j ? 0 : vectorDot(&q[j][0], &a[i][0], m);
468 }
469 }
470#if DEBUG_STRATEGY
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700471 ALOGD(" - q=%s", matrixToString(&q[0][0], m, n, false /*rowMajor*/).c_str());
472 ALOGD(" - r=%s", matrixToString(&r[0][0], n, n, true /*rowMajor*/).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700473
474 // calculate QR, if we factored A correctly then QR should equal A
475 float qr[n][m];
476 for (uint32_t h = 0; h < m; h++) {
477 for (uint32_t i = 0; i < n; i++) {
478 qr[i][h] = 0;
479 for (uint32_t j = 0; j < n; j++) {
480 qr[i][h] += q[j][h] * r[j][i];
481 }
482 }
483 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700484 ALOGD(" - qr=%s", matrixToString(&qr[0][0], m, n, false /*rowMajor*/).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700485#endif
486
487 // Solve R B = Qt W Y to find B. This is easy because R is upper triangular.
488 // We just work from bottom-right to top-left calculating B's coefficients.
489 float wy[m];
490 for (uint32_t h = 0; h < m; h++) {
491 wy[h] = y[h] * w[h];
492 }
Dan Austin389ddba2015-09-22 14:32:03 -0700493 for (uint32_t i = n; i != 0; ) {
494 i--;
Jeff Brown5912f952013-07-01 19:10:31 -0700495 outB[i] = vectorDot(&q[i][0], wy, m);
496 for (uint32_t j = n - 1; j > i; j--) {
497 outB[i] -= r[i][j] * outB[j];
498 }
499 outB[i] /= r[i][i];
500 }
501#if DEBUG_STRATEGY
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700502 ALOGD(" - b=%s", vectorToString(outB, n).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700503#endif
504
505 // Calculate the coefficient of determination as 1 - (SSerr / SStot) where
506 // SSerr is the residual sum of squares (variance of the error),
507 // and SStot is the total sum of squares (variance of the data) where each
508 // has been weighted.
509 float ymean = 0;
510 for (uint32_t h = 0; h < m; h++) {
511 ymean += y[h];
512 }
513 ymean /= m;
514
515 float sserr = 0;
516 float sstot = 0;
517 for (uint32_t h = 0; h < m; h++) {
518 float err = y[h] - outB[0];
519 float term = 1;
520 for (uint32_t i = 1; i < n; i++) {
521 term *= x[h];
522 err -= term * outB[i];
523 }
524 sserr += w[h] * w[h] * err * err;
525 float var = y[h] - ymean;
526 sstot += w[h] * w[h] * var * var;
527 }
528 *outDet = sstot > 0.000001f ? 1.0f - (sserr / sstot) : 1;
529#if DEBUG_STRATEGY
530 ALOGD(" - sserr=%f", sserr);
531 ALOGD(" - sstot=%f", sstot);
532 ALOGD(" - det=%f", *outDet);
533#endif
534 return true;
535}
536
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100537/*
538 * Optimized unweighted second-order least squares fit. About 2x speed improvement compared to
539 * the default implementation
540 */
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700541static std::optional<std::array<float, 3>> solveUnweightedLeastSquaresDeg2(
542 const float* x, const float* y, size_t count) {
543 // Solving y = a*x^2 + b*x + c
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100544 float sxi = 0, sxiyi = 0, syi = 0, sxi2 = 0, sxi3 = 0, sxi2yi = 0, sxi4 = 0;
545
546 for (size_t i = 0; i < count; i++) {
547 float xi = x[i];
548 float yi = y[i];
549 float xi2 = xi*xi;
550 float xi3 = xi2*xi;
551 float xi4 = xi3*xi;
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100552 float xiyi = xi*yi;
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700553 float xi2yi = xi2*yi;
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100554
555 sxi += xi;
556 sxi2 += xi2;
557 sxiyi += xiyi;
558 sxi2yi += xi2yi;
559 syi += yi;
560 sxi3 += xi3;
561 sxi4 += xi4;
562 }
563
564 float Sxx = sxi2 - sxi*sxi / count;
565 float Sxy = sxiyi - sxi*syi / count;
566 float Sxx2 = sxi3 - sxi*sxi2 / count;
567 float Sx2y = sxi2yi - sxi2*syi / count;
568 float Sx2x2 = sxi4 - sxi2*sxi2 / count;
569
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100570 float denominator = Sxx*Sx2x2 - Sxx2*Sxx2;
571 if (denominator == 0) {
572 ALOGW("division by 0 when computing velocity, Sxx=%f, Sx2x2=%f, Sxx2=%f", Sxx, Sx2x2, Sxx2);
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700573 return std::nullopt;
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100574 }
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700575 // Compute a
576 float numerator = Sx2y*Sxx - Sxy*Sxx2;
577 float a = numerator / denominator;
578
579 // Compute b
580 numerator = Sxy*Sx2x2 - Sx2y*Sxx2;
581 float b = numerator / denominator;
582
583 // Compute c
584 float c = syi/count - b * sxi/count - a * sxi2/count;
585
586 return std::make_optional(std::array<float, 3>({c, b, a}));
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100587}
588
Jeff Brown5912f952013-07-01 19:10:31 -0700589bool LeastSquaresVelocityTrackerStrategy::getEstimator(uint32_t id,
590 VelocityTracker::Estimator* outEstimator) const {
591 outEstimator->clear();
592
593 // Iterate over movement samples in reverse time order and collect samples.
594 float x[HISTORY_SIZE];
595 float y[HISTORY_SIZE];
596 float w[HISTORY_SIZE];
597 float time[HISTORY_SIZE];
598 uint32_t m = 0;
599 uint32_t index = mIndex;
600 const Movement& newestMovement = mMovements[mIndex];
601 do {
602 const Movement& movement = mMovements[index];
603 if (!movement.idBits.hasBit(id)) {
604 break;
605 }
606
607 nsecs_t age = newestMovement.eventTime - movement.eventTime;
608 if (age > HORIZON) {
609 break;
610 }
611
612 const VelocityTracker::Position& position = movement.getPosition(id);
613 x[m] = position.x;
614 y[m] = position.y;
615 w[m] = chooseWeight(index);
616 time[m] = -age * 0.000000001f;
617 index = (index == 0 ? HISTORY_SIZE : index) - 1;
618 } while (++m < HISTORY_SIZE);
619
620 if (m == 0) {
621 return false; // no data
622 }
623
624 // Calculate a least squares polynomial fit.
625 uint32_t degree = mDegree;
626 if (degree > m - 1) {
627 degree = m - 1;
628 }
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700629
630 if (degree == 2 && mWeighting == WEIGHTING_NONE) {
631 // Optimize unweighted, quadratic polynomial fit
632 std::optional<std::array<float, 3>> xCoeff = solveUnweightedLeastSquaresDeg2(time, x, m);
633 std::optional<std::array<float, 3>> yCoeff = solveUnweightedLeastSquaresDeg2(time, y, m);
634 if (xCoeff && yCoeff) {
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100635 outEstimator->time = newestMovement.eventTime;
636 outEstimator->degree = 2;
637 outEstimator->confidence = 1;
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700638 for (size_t i = 0; i <= outEstimator->degree; i++) {
639 outEstimator->xCoeff[i] = (*xCoeff)[i];
640 outEstimator->yCoeff[i] = (*yCoeff)[i];
641 }
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100642 return true;
643 }
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700644 } else if (degree >= 1) {
645 // General case for an Nth degree polynomial fit
Jeff Brown5912f952013-07-01 19:10:31 -0700646 float xdet, ydet;
647 uint32_t n = degree + 1;
648 if (solveLeastSquares(time, x, w, m, n, outEstimator->xCoeff, &xdet)
649 && solveLeastSquares(time, y, w, m, n, outEstimator->yCoeff, &ydet)) {
650 outEstimator->time = newestMovement.eventTime;
651 outEstimator->degree = degree;
652 outEstimator->confidence = xdet * ydet;
653#if DEBUG_STRATEGY
654 ALOGD("estimate: degree=%d, xCoeff=%s, yCoeff=%s, confidence=%f",
655 int(outEstimator->degree),
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700656 vectorToString(outEstimator->xCoeff, n).c_str(),
657 vectorToString(outEstimator->yCoeff, n).c_str(),
Jeff Brown5912f952013-07-01 19:10:31 -0700658 outEstimator->confidence);
659#endif
660 return true;
661 }
662 }
663
664 // No velocity data available for this pointer, but we do have its current position.
665 outEstimator->xCoeff[0] = x[0];
666 outEstimator->yCoeff[0] = y[0];
667 outEstimator->time = newestMovement.eventTime;
668 outEstimator->degree = 0;
669 outEstimator->confidence = 1;
670 return true;
671}
672
673float LeastSquaresVelocityTrackerStrategy::chooseWeight(uint32_t index) const {
674 switch (mWeighting) {
675 case WEIGHTING_DELTA: {
676 // Weight points based on how much time elapsed between them and the next
677 // point so that points that "cover" a shorter time span are weighed less.
678 // delta 0ms: 0.5
679 // delta 10ms: 1.0
680 if (index == mIndex) {
681 return 1.0f;
682 }
683 uint32_t nextIndex = (index + 1) % HISTORY_SIZE;
684 float deltaMillis = (mMovements[nextIndex].eventTime- mMovements[index].eventTime)
685 * 0.000001f;
686 if (deltaMillis < 0) {
687 return 0.5f;
688 }
689 if (deltaMillis < 10) {
690 return 0.5f + deltaMillis * 0.05;
691 }
692 return 1.0f;
693 }
694
695 case WEIGHTING_CENTRAL: {
696 // Weight points based on their age, weighing very recent and very old points less.
697 // age 0ms: 0.5
698 // age 10ms: 1.0
699 // age 50ms: 1.0
700 // age 60ms: 0.5
701 float ageMillis = (mMovements[mIndex].eventTime - mMovements[index].eventTime)
702 * 0.000001f;
703 if (ageMillis < 0) {
704 return 0.5f;
705 }
706 if (ageMillis < 10) {
707 return 0.5f + ageMillis * 0.05;
708 }
709 if (ageMillis < 50) {
710 return 1.0f;
711 }
712 if (ageMillis < 60) {
713 return 0.5f + (60 - ageMillis) * 0.05;
714 }
715 return 0.5f;
716 }
717
718 case WEIGHTING_RECENT: {
719 // Weight points based on their age, weighing older points less.
720 // age 0ms: 1.0
721 // age 50ms: 1.0
722 // age 100ms: 0.5
723 float ageMillis = (mMovements[mIndex].eventTime - mMovements[index].eventTime)
724 * 0.000001f;
725 if (ageMillis < 50) {
726 return 1.0f;
727 }
728 if (ageMillis < 100) {
729 return 0.5f + (100 - ageMillis) * 0.01f;
730 }
731 return 0.5f;
732 }
733
734 case WEIGHTING_NONE:
735 default:
736 return 1.0f;
737 }
738}
739
740
741// --- IntegratingVelocityTrackerStrategy ---
742
743IntegratingVelocityTrackerStrategy::IntegratingVelocityTrackerStrategy(uint32_t degree) :
744 mDegree(degree) {
745}
746
747IntegratingVelocityTrackerStrategy::~IntegratingVelocityTrackerStrategy() {
748}
749
750void IntegratingVelocityTrackerStrategy::clear() {
751 mPointerIdBits.clear();
752}
753
754void IntegratingVelocityTrackerStrategy::clearPointers(BitSet32 idBits) {
755 mPointerIdBits.value &= ~idBits.value;
756}
757
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500758void IntegratingVelocityTrackerStrategy::addMovement(
759 nsecs_t eventTime, BitSet32 idBits,
760 const std::vector<VelocityTracker::Position>& positions) {
Jeff Brown5912f952013-07-01 19:10:31 -0700761 uint32_t index = 0;
762 for (BitSet32 iterIdBits(idBits); !iterIdBits.isEmpty();) {
763 uint32_t id = iterIdBits.clearFirstMarkedBit();
764 State& state = mPointerState[id];
765 const VelocityTracker::Position& position = positions[index++];
766 if (mPointerIdBits.hasBit(id)) {
767 updateState(state, eventTime, position.x, position.y);
768 } else {
769 initState(state, eventTime, position.x, position.y);
770 }
771 }
772
773 mPointerIdBits = idBits;
774}
775
776bool IntegratingVelocityTrackerStrategy::getEstimator(uint32_t id,
777 VelocityTracker::Estimator* outEstimator) const {
778 outEstimator->clear();
779
780 if (mPointerIdBits.hasBit(id)) {
781 const State& state = mPointerState[id];
782 populateEstimator(state, outEstimator);
783 return true;
784 }
785
786 return false;
787}
788
789void IntegratingVelocityTrackerStrategy::initState(State& state,
790 nsecs_t eventTime, float xpos, float ypos) const {
791 state.updateTime = eventTime;
792 state.degree = 0;
793
794 state.xpos = xpos;
795 state.xvel = 0;
796 state.xaccel = 0;
797 state.ypos = ypos;
798 state.yvel = 0;
799 state.yaccel = 0;
800}
801
802void IntegratingVelocityTrackerStrategy::updateState(State& state,
803 nsecs_t eventTime, float xpos, float ypos) const {
804 const nsecs_t MIN_TIME_DELTA = 2 * NANOS_PER_MS;
805 const float FILTER_TIME_CONSTANT = 0.010f; // 10 milliseconds
806
807 if (eventTime <= state.updateTime + MIN_TIME_DELTA) {
808 return;
809 }
810
811 float dt = (eventTime - state.updateTime) * 0.000000001f;
812 state.updateTime = eventTime;
813
814 float xvel = (xpos - state.xpos) / dt;
815 float yvel = (ypos - state.ypos) / dt;
816 if (state.degree == 0) {
817 state.xvel = xvel;
818 state.yvel = yvel;
819 state.degree = 1;
820 } else {
821 float alpha = dt / (FILTER_TIME_CONSTANT + dt);
822 if (mDegree == 1) {
823 state.xvel += (xvel - state.xvel) * alpha;
824 state.yvel += (yvel - state.yvel) * alpha;
825 } else {
826 float xaccel = (xvel - state.xvel) / dt;
827 float yaccel = (yvel - state.yvel) / dt;
828 if (state.degree == 1) {
829 state.xaccel = xaccel;
830 state.yaccel = yaccel;
831 state.degree = 2;
832 } else {
833 state.xaccel += (xaccel - state.xaccel) * alpha;
834 state.yaccel += (yaccel - state.yaccel) * alpha;
835 }
836 state.xvel += (state.xaccel * dt) * alpha;
837 state.yvel += (state.yaccel * dt) * alpha;
838 }
839 }
840 state.xpos = xpos;
841 state.ypos = ypos;
842}
843
844void IntegratingVelocityTrackerStrategy::populateEstimator(const State& state,
845 VelocityTracker::Estimator* outEstimator) const {
846 outEstimator->time = state.updateTime;
847 outEstimator->confidence = 1.0f;
848 outEstimator->degree = state.degree;
849 outEstimator->xCoeff[0] = state.xpos;
850 outEstimator->xCoeff[1] = state.xvel;
851 outEstimator->xCoeff[2] = state.xaccel / 2;
852 outEstimator->yCoeff[0] = state.ypos;
853 outEstimator->yCoeff[1] = state.yvel;
854 outEstimator->yCoeff[2] = state.yaccel / 2;
855}
856
857
858// --- LegacyVelocityTrackerStrategy ---
859
Jeff Brown5912f952013-07-01 19:10:31 -0700860LegacyVelocityTrackerStrategy::LegacyVelocityTrackerStrategy() {
861 clear();
862}
863
864LegacyVelocityTrackerStrategy::~LegacyVelocityTrackerStrategy() {
865}
866
867void LegacyVelocityTrackerStrategy::clear() {
868 mIndex = 0;
869 mMovements[0].idBits.clear();
870}
871
872void LegacyVelocityTrackerStrategy::clearPointers(BitSet32 idBits) {
873 BitSet32 remainingIdBits(mMovements[mIndex].idBits.value & ~idBits.value);
874 mMovements[mIndex].idBits = remainingIdBits;
875}
876
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500877void LegacyVelocityTrackerStrategy::addMovement(
878 nsecs_t eventTime, BitSet32 idBits,
879 const std::vector<VelocityTracker::Position>& positions) {
Jeff Brown5912f952013-07-01 19:10:31 -0700880 if (++mIndex == HISTORY_SIZE) {
881 mIndex = 0;
882 }
883
884 Movement& movement = mMovements[mIndex];
885 movement.eventTime = eventTime;
886 movement.idBits = idBits;
887 uint32_t count = idBits.count();
888 for (uint32_t i = 0; i < count; i++) {
889 movement.positions[i] = positions[i];
890 }
891}
892
893bool LegacyVelocityTrackerStrategy::getEstimator(uint32_t id,
894 VelocityTracker::Estimator* outEstimator) const {
895 outEstimator->clear();
896
897 const Movement& newestMovement = mMovements[mIndex];
898 if (!newestMovement.idBits.hasBit(id)) {
899 return false; // no data
900 }
901
902 // Find the oldest sample that contains the pointer and that is not older than HORIZON.
903 nsecs_t minTime = newestMovement.eventTime - HORIZON;
904 uint32_t oldestIndex = mIndex;
905 uint32_t numTouches = 1;
906 do {
907 uint32_t nextOldestIndex = (oldestIndex == 0 ? HISTORY_SIZE : oldestIndex) - 1;
908 const Movement& nextOldestMovement = mMovements[nextOldestIndex];
909 if (!nextOldestMovement.idBits.hasBit(id)
910 || nextOldestMovement.eventTime < minTime) {
911 break;
912 }
913 oldestIndex = nextOldestIndex;
914 } while (++numTouches < HISTORY_SIZE);
915
916 // Calculate an exponentially weighted moving average of the velocity estimate
917 // at different points in time measured relative to the oldest sample.
918 // This is essentially an IIR filter. Newer samples are weighted more heavily
919 // than older samples. Samples at equal time points are weighted more or less
920 // equally.
921 //
922 // One tricky problem is that the sample data may be poorly conditioned.
923 // Sometimes samples arrive very close together in time which can cause us to
924 // overestimate the velocity at that time point. Most samples might be measured
925 // 16ms apart but some consecutive samples could be only 0.5sm apart because
926 // the hardware or driver reports them irregularly or in bursts.
927 float accumVx = 0;
928 float accumVy = 0;
929 uint32_t index = oldestIndex;
930 uint32_t samplesUsed = 0;
931 const Movement& oldestMovement = mMovements[oldestIndex];
932 const VelocityTracker::Position& oldestPosition = oldestMovement.getPosition(id);
933 nsecs_t lastDuration = 0;
934
935 while (numTouches-- > 1) {
936 if (++index == HISTORY_SIZE) {
937 index = 0;
938 }
939 const Movement& movement = mMovements[index];
940 nsecs_t duration = movement.eventTime - oldestMovement.eventTime;
941
942 // If the duration between samples is small, we may significantly overestimate
943 // the velocity. Consequently, we impose a minimum duration constraint on the
944 // samples that we include in the calculation.
945 if (duration >= MIN_DURATION) {
946 const VelocityTracker::Position& position = movement.getPosition(id);
947 float scale = 1000000000.0f / duration; // one over time delta in seconds
948 float vx = (position.x - oldestPosition.x) * scale;
949 float vy = (position.y - oldestPosition.y) * scale;
950 accumVx = (accumVx * lastDuration + vx * duration) / (duration + lastDuration);
951 accumVy = (accumVy * lastDuration + vy * duration) / (duration + lastDuration);
952 lastDuration = duration;
953 samplesUsed += 1;
954 }
955 }
956
957 // Report velocity.
958 const VelocityTracker::Position& newestPosition = newestMovement.getPosition(id);
959 outEstimator->time = newestMovement.eventTime;
960 outEstimator->confidence = 1;
961 outEstimator->xCoeff[0] = newestPosition.x;
962 outEstimator->yCoeff[0] = newestPosition.y;
963 if (samplesUsed) {
964 outEstimator->xCoeff[1] = accumVx;
965 outEstimator->yCoeff[1] = accumVy;
966 outEstimator->degree = 1;
967 } else {
968 outEstimator->degree = 0;
969 }
970 return true;
971}
972
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100973// --- ImpulseVelocityTrackerStrategy ---
974
975ImpulseVelocityTrackerStrategy::ImpulseVelocityTrackerStrategy() {
976 clear();
977}
978
979ImpulseVelocityTrackerStrategy::~ImpulseVelocityTrackerStrategy() {
980}
981
982void ImpulseVelocityTrackerStrategy::clear() {
983 mIndex = 0;
984 mMovements[0].idBits.clear();
985}
986
987void ImpulseVelocityTrackerStrategy::clearPointers(BitSet32 idBits) {
988 BitSet32 remainingIdBits(mMovements[mIndex].idBits.value & ~idBits.value);
989 mMovements[mIndex].idBits = remainingIdBits;
990}
991
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500992void ImpulseVelocityTrackerStrategy::addMovement(
993 nsecs_t eventTime, BitSet32 idBits,
994 const std::vector<VelocityTracker::Position>& positions) {
Siarhei Vishniakou346ac6a2019-04-10 09:58:05 -0700995 if (mMovements[mIndex].eventTime != eventTime) {
996 // When ACTION_POINTER_DOWN happens, we will first receive ACTION_MOVE with the coordinates
997 // of the existing pointers, and then ACTION_POINTER_DOWN with the coordinates that include
998 // the new pointer. If the eventtimes for both events are identical, just update the data
999 // for this time.
1000 // We only compare against the last value, as it is likely that addMovement is called
1001 // in chronological order as events occur.
1002 mIndex++;
1003 }
1004 if (mIndex == HISTORY_SIZE) {
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001005 mIndex = 0;
1006 }
1007
1008 Movement& movement = mMovements[mIndex];
1009 movement.eventTime = eventTime;
1010 movement.idBits = idBits;
1011 uint32_t count = idBits.count();
1012 for (uint32_t i = 0; i < count; i++) {
1013 movement.positions[i] = positions[i];
1014 }
1015}
1016
1017/**
1018 * Calculate the total impulse provided to the screen and the resulting velocity.
1019 *
1020 * The touchscreen is modeled as a physical object.
1021 * Initial condition is discussed below, but for now suppose that v(t=0) = 0
1022 *
1023 * The kinetic energy of the object at the release is E=0.5*m*v^2
1024 * Then vfinal = sqrt(2E/m). The goal is to calculate E.
1025 *
1026 * The kinetic energy at the release is equal to the total work done on the object by the finger.
1027 * The total work W is the sum of all dW along the path.
1028 *
1029 * dW = F*dx, where dx is the piece of path traveled.
1030 * Force is change of momentum over time, F = dp/dt = m dv/dt.
1031 * Then substituting:
1032 * dW = m (dv/dt) * dx = m * v * dv
1033 *
1034 * Summing along the path, we get:
1035 * W = sum(dW) = sum(m * v * dv) = m * sum(v * dv)
1036 * Since the mass stays constant, the equation for final velocity is:
1037 * vfinal = sqrt(2*sum(v * dv))
1038 *
1039 * Here,
1040 * dv : change of velocity = (v[i+1]-v[i])
1041 * dx : change of distance = (x[i+1]-x[i])
1042 * dt : change of time = (t[i+1]-t[i])
1043 * v : instantaneous velocity = dx/dt
1044 *
1045 * The final formula is:
1046 * vfinal = sqrt(2) * sqrt(sum((v[i]-v[i-1])*|v[i]|)) for all i
1047 * The absolute value is needed to properly account for the sign. If the velocity over a
1048 * particular segment descreases, then this indicates braking, which means that negative
1049 * work was done. So for two positive, but decreasing, velocities, this contribution would be
1050 * negative and will cause a smaller final velocity.
1051 *
1052 * Initial condition
1053 * There are two ways to deal with initial condition:
1054 * 1) Assume that v(0) = 0, which would mean that the screen is initially at rest.
1055 * This is not entirely accurate. We are only taking the past X ms of touch data, where X is
1056 * currently equal to 100. However, a touch event that created a fling probably lasted for longer
1057 * than that, which would mean that the user has already been interacting with the touchscreen
1058 * and it has probably already been moving.
1059 * 2) Assume that the touchscreen has already been moving at a certain velocity, calculate this
1060 * initial velocity and the equivalent energy, and start with this initial energy.
1061 * Consider an example where we have the following data, consisting of 3 points:
1062 * time: t0, t1, t2
1063 * x : x0, x1, x2
1064 * v : 0 , v1, v2
1065 * Here is what will happen in each of these scenarios:
1066 * 1) By directly applying the formula above with the v(0) = 0 boundary condition, we will get
1067 * vfinal = sqrt(2*(|v1|*(v1-v0) + |v2|*(v2-v1))). This can be simplified since v0=0
1068 * vfinal = sqrt(2*(|v1|*v1 + |v2|*(v2-v1))) = sqrt(2*(v1^2 + |v2|*(v2 - v1)))
1069 * since velocity is a real number
1070 * 2) If we treat the screen as already moving, then it must already have an energy (per mass)
1071 * equal to 1/2*v1^2. Then the initial energy should be 1/2*v1*2, and only the second segment
1072 * will contribute to the total kinetic energy (since we can effectively consider that v0=v1).
1073 * This will give the following expression for the final velocity:
1074 * vfinal = sqrt(2*(1/2*v1^2 + |v2|*(v2-v1)))
1075 * This analysis can be generalized to an arbitrary number of samples.
1076 *
1077 *
1078 * Comparing the two equations above, we see that the only mathematical difference
1079 * is the factor of 1/2 in front of the first velocity term.
1080 * This boundary condition would allow for the "proper" calculation of the case when all of the
1081 * samples are equally spaced in time and distance, which should suggest a constant velocity.
1082 *
1083 * Note that approach 2) is sensitive to the proper ordering of the data in time, since
1084 * the boundary condition must be applied to the oldest sample to be accurate.
1085 */
Siarhei Vishniakou97b5e182017-09-01 13:52:33 -07001086static float kineticEnergyToVelocity(float work) {
1087 static constexpr float sqrt2 = 1.41421356237;
1088 return (work < 0 ? -1.0 : 1.0) * sqrtf(fabsf(work)) * sqrt2;
1089}
1090
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001091static float calculateImpulseVelocity(const nsecs_t* t, const float* x, size_t count) {
1092 // The input should be in reversed time order (most recent sample at index i=0)
1093 // t[i] is in nanoseconds, but due to FP arithmetic, convert to seconds inside this function
Siarhei Vishniakou6de8f5e2018-03-02 18:48:15 -08001094 static constexpr float SECONDS_PER_NANO = 1E-9;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001095
1096 if (count < 2) {
1097 return 0; // if 0 or 1 points, velocity is zero
1098 }
1099 if (t[1] > t[0]) { // Algorithm will still work, but not perfectly
1100 ALOGE("Samples provided to calculateImpulseVelocity in the wrong order");
1101 }
1102 if (count == 2) { // if 2 points, basic linear calculation
1103 if (t[1] == t[0]) {
1104 ALOGE("Events have identical time stamps t=%" PRId64 ", setting velocity = 0", t[0]);
1105 return 0;
1106 }
Siarhei Vishniakou6de8f5e2018-03-02 18:48:15 -08001107 return (x[1] - x[0]) / (SECONDS_PER_NANO * (t[1] - t[0]));
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001108 }
1109 // Guaranteed to have at least 3 points here
1110 float work = 0;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001111 for (size_t i = count - 1; i > 0 ; i--) { // start with the oldest sample and go forward in time
1112 if (t[i] == t[i-1]) {
1113 ALOGE("Events have identical time stamps t=%" PRId64 ", skipping sample", t[i]);
1114 continue;
1115 }
Siarhei Vishniakou97b5e182017-09-01 13:52:33 -07001116 float vprev = kineticEnergyToVelocity(work); // v[i-1]
Siarhei Vishniakou6de8f5e2018-03-02 18:48:15 -08001117 float vcurr = (x[i] - x[i-1]) / (SECONDS_PER_NANO * (t[i] - t[i-1])); // v[i]
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001118 work += (vcurr - vprev) * fabsf(vcurr);
1119 if (i == count - 1) {
1120 work *= 0.5; // initial condition, case 2) above
1121 }
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001122 }
Siarhei Vishniakou97b5e182017-09-01 13:52:33 -07001123 return kineticEnergyToVelocity(work);
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001124}
1125
1126bool ImpulseVelocityTrackerStrategy::getEstimator(uint32_t id,
1127 VelocityTracker::Estimator* outEstimator) const {
1128 outEstimator->clear();
1129
1130 // Iterate over movement samples in reverse time order and collect samples.
1131 float x[HISTORY_SIZE];
1132 float y[HISTORY_SIZE];
1133 nsecs_t time[HISTORY_SIZE];
1134 size_t m = 0; // number of points that will be used for fitting
1135 size_t index = mIndex;
1136 const Movement& newestMovement = mMovements[mIndex];
1137 do {
1138 const Movement& movement = mMovements[index];
1139 if (!movement.idBits.hasBit(id)) {
1140 break;
1141 }
1142
1143 nsecs_t age = newestMovement.eventTime - movement.eventTime;
1144 if (age > HORIZON) {
1145 break;
1146 }
1147
1148 const VelocityTracker::Position& position = movement.getPosition(id);
1149 x[m] = position.x;
1150 y[m] = position.y;
1151 time[m] = movement.eventTime;
1152 index = (index == 0 ? HISTORY_SIZE : index) - 1;
1153 } while (++m < HISTORY_SIZE);
1154
1155 if (m == 0) {
1156 return false; // no data
1157 }
1158 outEstimator->xCoeff[0] = 0;
1159 outEstimator->yCoeff[0] = 0;
1160 outEstimator->xCoeff[1] = calculateImpulseVelocity(time, x, m);
1161 outEstimator->yCoeff[1] = calculateImpulseVelocity(time, y, m);
1162 outEstimator->xCoeff[2] = 0;
1163 outEstimator->yCoeff[2] = 0;
1164 outEstimator->time = newestMovement.eventTime;
1165 outEstimator->degree = 2; // similar results to 2nd degree fit
1166 outEstimator->confidence = 1;
1167#if DEBUG_STRATEGY
1168 ALOGD("velocity: (%f, %f)", outEstimator->xCoeff[1], outEstimator->yCoeff[1]);
1169#endif
1170 return true;
1171}
1172
Jeff Brown5912f952013-07-01 19:10:31 -07001173} // namespace android