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