blob: 76aaf61da768a7d9724966036ed42eb4193c93ea [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"
Jeff Brown5912f952013-07-01 19:10:31 -070018
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -070019#include <array>
Siarhei Vishniakou7b9d1892017-07-05 18:58:41 -070020#include <inttypes.h>
Jeff Brown5912f952013-07-01 19:10:31 -070021#include <limits.h>
Siarhei Vishniakou7b9d1892017-07-05 18:58:41 -070022#include <math.h>
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -070023#include <optional>
Jeff Brown5912f952013-07-01 19:10:31 -070024
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070025#include <android-base/stringprintf.h>
Jeff Brown5912f952013-07-01 19:10:31 -070026#include <input/VelocityTracker.h>
27#include <utils/BitSet.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028#include <utils/Timers.h>
29
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +000030using std::literals::chrono_literals::operator""ms;
31
Jeff Brown5912f952013-07-01 19:10:31 -070032namespace android {
33
Siarhei Vishniakou276467b2022-03-17 09:43:28 -070034/**
35 * Log debug messages about velocity tracking.
36 * Enable this via "adb shell setprop log.tag.VelocityTrackerVelocity DEBUG" (requires restart)
37 */
38const bool DEBUG_VELOCITY =
39 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Velocity", ANDROID_LOG_INFO);
40
41/**
42 * Log debug messages about the progress of the algorithm itself.
43 * Enable this via "adb shell setprop log.tag.VelocityTrackerStrategy DEBUG" (requires restart)
44 */
45const bool DEBUG_STRATEGY =
46 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Strategy", ANDROID_LOG_INFO);
47
48/**
49 * Log debug messages about the 'impulse' strategy.
50 * Enable this via "adb shell setprop log.tag.VelocityTrackerImpulse DEBUG" (requires restart)
51 */
52const bool DEBUG_IMPULSE =
53 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Impulse", ANDROID_LOG_INFO);
54
Jeff Brown5912f952013-07-01 19:10:31 -070055// Nanoseconds per milliseconds.
56static const nsecs_t NANOS_PER_MS = 1000000;
57
58// Threshold for determining that a pointer has stopped moving.
59// Some input devices do not send ACTION_MOVE events in the case where a pointer has
60// stopped. We need to detect this case so that we can accurately predict the
61// velocity after the pointer starts moving again.
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +000062static const std::chrono::duration ASSUME_POINTER_STOPPED_TIME = 40ms;
Jeff Brown5912f952013-07-01 19:10:31 -070063
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +000064static std::string toString(std::chrono::nanoseconds t) {
65 std::stringstream stream;
66 stream.precision(1);
67 stream << std::fixed << std::chrono::duration<float, std::milli>(t).count() << " ms";
68 return stream.str();
69}
Jeff Brown5912f952013-07-01 19:10:31 -070070
71static float vectorDot(const float* a, const float* b, uint32_t m) {
72 float r = 0;
Siarhei Vishniakou7b9d1892017-07-05 18:58:41 -070073 for (size_t i = 0; i < m; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -070074 r += *(a++) * *(b++);
75 }
76 return r;
77}
78
79static float vectorNorm(const float* a, uint32_t m) {
80 float r = 0;
Siarhei Vishniakou7b9d1892017-07-05 18:58:41 -070081 for (size_t i = 0; i < m; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -070082 float t = *(a++);
83 r += t * t;
84 }
85 return sqrtf(r);
86}
87
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070088static std::string vectorToString(const float* a, uint32_t m) {
89 std::string str;
90 str += "[";
Siarhei Vishniakou7b9d1892017-07-05 18:58:41 -070091 for (size_t i = 0; i < m; i++) {
92 if (i) {
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++));
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 return str;
99}
100
Siarhei Vishniakoue37bcec2021-09-28 14:24:32 -0700101static std::string vectorToString(const std::vector<float>& v) {
102 return vectorToString(v.data(), v.size());
103}
104
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700105static std::string matrixToString(const float* a, uint32_t m, uint32_t n, bool rowMajor) {
106 std::string str;
107 str = "[";
Jeff Brown5912f952013-07-01 19:10:31 -0700108 for (size_t i = 0; i < m; i++) {
109 if (i) {
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700110 str += ",";
Jeff Brown5912f952013-07-01 19:10:31 -0700111 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700112 str += " [";
Jeff Brown5912f952013-07-01 19:10:31 -0700113 for (size_t j = 0; j < n; j++) {
114 if (j) {
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700115 str += ",";
Jeff Brown5912f952013-07-01 19:10:31 -0700116 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700117 str += android::base::StringPrintf(" %f", a[rowMajor ? i * n + j : j * m + i]);
Jeff Brown5912f952013-07-01 19:10:31 -0700118 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700119 str += " ]";
Jeff Brown5912f952013-07-01 19:10:31 -0700120 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700121 str += " ]";
Jeff Brown5912f952013-07-01 19:10:31 -0700122 return str;
123}
Jeff Brown5912f952013-07-01 19:10:31 -0700124
125
126// --- VelocityTracker ---
127
Chris Yef8591482020-04-17 11:49:17 -0700128VelocityTracker::VelocityTracker(const Strategy strategy)
129 : mLastEventTime(0), mCurrentPointerIdBits(0), mActivePointerId(-1) {
Jeff Brown5912f952013-07-01 19:10:31 -0700130 // Configure the strategy.
131 if (!configureStrategy(strategy)) {
Chris Yef8591482020-04-17 11:49:17 -0700132 ALOGE("Unrecognized velocity tracker strategy %" PRId32 ".", strategy);
133 if (!configureStrategy(VelocityTracker::DEFAULT_STRATEGY)) {
134 LOG_ALWAYS_FATAL("Could not create the default velocity tracker strategy '%" PRId32
135 "'!",
136 strategy);
Jeff Brown5912f952013-07-01 19:10:31 -0700137 }
138 }
139}
140
141VelocityTracker::~VelocityTracker() {
Jeff Brown5912f952013-07-01 19:10:31 -0700142}
143
Chris Yef8591482020-04-17 11:49:17 -0700144bool VelocityTracker::configureStrategy(Strategy strategy) {
145 if (strategy == VelocityTracker::Strategy::DEFAULT) {
146 mStrategy = createStrategy(VelocityTracker::DEFAULT_STRATEGY);
147 } else {
148 mStrategy = createStrategy(strategy);
149 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700150 return mStrategy != nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700151}
152
Chris Yef8591482020-04-17 11:49:17 -0700153std::unique_ptr<VelocityTrackerStrategy> VelocityTracker::createStrategy(
154 VelocityTracker::Strategy strategy) {
155 switch (strategy) {
156 case VelocityTracker::Strategy::IMPULSE:
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000157 ALOGI_IF(DEBUG_STRATEGY, "Initializing impulse strategy");
Chris Yef8591482020-04-17 11:49:17 -0700158 return std::make_unique<ImpulseVelocityTrackerStrategy>();
159
160 case VelocityTracker::Strategy::LSQ1:
161 return std::make_unique<LeastSquaresVelocityTrackerStrategy>(1);
162
163 case VelocityTracker::Strategy::LSQ2:
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000164 ALOGI_IF(DEBUG_STRATEGY && !DEBUG_IMPULSE, "Initializing lsq2 strategy");
Chris Yef8591482020-04-17 11:49:17 -0700165 return std::make_unique<LeastSquaresVelocityTrackerStrategy>(2);
166
167 case VelocityTracker::Strategy::LSQ3:
168 return std::make_unique<LeastSquaresVelocityTrackerStrategy>(3);
169
170 case VelocityTracker::Strategy::WLSQ2_DELTA:
171 return std::make_unique<
172 LeastSquaresVelocityTrackerStrategy>(2,
173 LeastSquaresVelocityTrackerStrategy::
174 WEIGHTING_DELTA);
175 case VelocityTracker::Strategy::WLSQ2_CENTRAL:
176 return std::make_unique<
177 LeastSquaresVelocityTrackerStrategy>(2,
178 LeastSquaresVelocityTrackerStrategy::
179 WEIGHTING_CENTRAL);
180 case VelocityTracker::Strategy::WLSQ2_RECENT:
181 return std::make_unique<
182 LeastSquaresVelocityTrackerStrategy>(2,
183 LeastSquaresVelocityTrackerStrategy::
184 WEIGHTING_RECENT);
185
186 case VelocityTracker::Strategy::INT1:
187 return std::make_unique<IntegratingVelocityTrackerStrategy>(1);
188
189 case VelocityTracker::Strategy::INT2:
190 return std::make_unique<IntegratingVelocityTrackerStrategy>(2);
191
192 case VelocityTracker::Strategy::LEGACY:
193 return std::make_unique<LegacyVelocityTrackerStrategy>();
194
195 default:
196 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700197 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700198 return nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700199}
200
201void VelocityTracker::clear() {
202 mCurrentPointerIdBits.clear();
203 mActivePointerId = -1;
204
205 mStrategy->clear();
206}
207
208void VelocityTracker::clearPointers(BitSet32 idBits) {
209 BitSet32 remainingIdBits(mCurrentPointerIdBits.value & ~idBits.value);
210 mCurrentPointerIdBits = remainingIdBits;
211
212 if (mActivePointerId >= 0 && idBits.hasBit(mActivePointerId)) {
213 mActivePointerId = !remainingIdBits.isEmpty() ? remainingIdBits.firstMarkedBit() : -1;
214 }
215
216 mStrategy->clearPointers(idBits);
217}
218
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500219void VelocityTracker::addMovement(nsecs_t eventTime, BitSet32 idBits,
220 const std::vector<VelocityTracker::Position>& positions) {
221 LOG_ALWAYS_FATAL_IF(idBits.count() != positions.size(),
222 "Mismatching number of pointers, idBits=%" PRIu32 ", positions=%zu",
223 idBits.count(), positions.size());
Jeff Brown5912f952013-07-01 19:10:31 -0700224 while (idBits.count() > MAX_POINTERS) {
225 idBits.clearLastMarkedBit();
226 }
227
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000228 if ((mCurrentPointerIdBits.value & idBits.value) &&
229 std::chrono::nanoseconds(eventTime - mLastEventTime) > ASSUME_POINTER_STOPPED_TIME) {
230 ALOGD_IF(DEBUG_VELOCITY, "VelocityTracker: stopped for %s, clearing state.",
231 toString(std::chrono::nanoseconds(eventTime - mLastEventTime)).c_str());
232
Jeff Brown5912f952013-07-01 19:10:31 -0700233 // We have not received any movements for too long. Assume that all pointers
234 // have stopped.
235 mStrategy->clear();
236 }
237 mLastEventTime = eventTime;
238
239 mCurrentPointerIdBits = idBits;
240 if (mActivePointerId < 0 || !idBits.hasBit(mActivePointerId)) {
241 mActivePointerId = idBits.isEmpty() ? -1 : idBits.firstMarkedBit();
242 }
243
244 mStrategy->addMovement(eventTime, idBits, positions);
245
Siarhei Vishniakoue37bcec2021-09-28 14:24:32 -0700246 if (DEBUG_VELOCITY) {
247 ALOGD("VelocityTracker: addMovement eventTime=%" PRId64
248 ", idBits=0x%08x, activePointerId=%d",
249 eventTime, idBits.value, mActivePointerId);
250 for (BitSet32 iterBits(idBits); !iterBits.isEmpty();) {
251 uint32_t id = iterBits.firstMarkedBit();
252 uint32_t index = idBits.getIndexOfBit(id);
253 iterBits.clearBit(id);
254 Estimator estimator;
255 getEstimator(id, &estimator);
256 ALOGD(" %d: position (%0.3f, %0.3f), "
257 "estimator (degree=%d, xCoeff=%s, yCoeff=%s, confidence=%f)",
258 id, positions[index].x, positions[index].y, int(estimator.degree),
259 vectorToString(estimator.xCoeff, estimator.degree + 1).c_str(),
260 vectorToString(estimator.yCoeff, estimator.degree + 1).c_str(),
261 estimator.confidence);
262 }
Jeff Brown5912f952013-07-01 19:10:31 -0700263 }
Jeff Brown5912f952013-07-01 19:10:31 -0700264}
265
266void VelocityTracker::addMovement(const MotionEvent* event) {
267 int32_t actionMasked = event->getActionMasked();
268
269 switch (actionMasked) {
270 case AMOTION_EVENT_ACTION_DOWN:
271 case AMOTION_EVENT_ACTION_HOVER_ENTER:
272 // Clear all pointers on down before adding the new movement.
273 clear();
274 break;
275 case AMOTION_EVENT_ACTION_POINTER_DOWN: {
276 // Start a new movement trace for a pointer that just went down.
277 // We do this on down instead of on up because the client may want to query the
278 // final velocity for a pointer that just went up.
279 BitSet32 downIdBits;
280 downIdBits.markBit(event->getPointerId(event->getActionIndex()));
281 clearPointers(downIdBits);
282 break;
283 }
284 case AMOTION_EVENT_ACTION_MOVE:
285 case AMOTION_EVENT_ACTION_HOVER_MOVE:
286 break;
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000287 case AMOTION_EVENT_ACTION_POINTER_UP:
288 case AMOTION_EVENT_ACTION_UP: {
289 std::chrono::nanoseconds delaySinceLastEvent(event->getEventTime() - mLastEventTime);
290 if (delaySinceLastEvent > ASSUME_POINTER_STOPPED_TIME) {
291 ALOGD_IF(DEBUG_VELOCITY,
292 "VelocityTracker: stopped for %s, clearing state upon pointer liftoff.",
293 toString(delaySinceLastEvent).c_str());
294 // We have not received any movements for too long. Assume that all pointers
295 // have stopped.
296 mStrategy->clear();
297 }
298 // These actions because they do not convey any new information about
Jeff Brown5912f952013-07-01 19:10:31 -0700299 // pointer movement. We also want to preserve the last known velocity of the pointers.
300 // Note that ACTION_UP and ACTION_POINTER_UP always report the last known position
301 // of the pointers that went up. ACTION_POINTER_UP does include the new position of
302 // pointers that remained down but we will also receive an ACTION_MOVE with this
303 // information if any of them actually moved. Since we don't know how many pointers
304 // will be going up at once it makes sense to just wait for the following ACTION_MOVE
305 // before adding the movement.
306 return;
307 }
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000308 default:
309 // Ignore all other actions.
310 return;
311 }
Jeff Brown5912f952013-07-01 19:10:31 -0700312
313 size_t pointerCount = event->getPointerCount();
314 if (pointerCount > MAX_POINTERS) {
315 pointerCount = MAX_POINTERS;
316 }
317
318 BitSet32 idBits;
319 for (size_t i = 0; i < pointerCount; i++) {
320 idBits.markBit(event->getPointerId(i));
321 }
322
323 uint32_t pointerIndex[MAX_POINTERS];
324 for (size_t i = 0; i < pointerCount; i++) {
325 pointerIndex[i] = idBits.getIndexOfBit(event->getPointerId(i));
326 }
327
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500328 std::vector<Position> positions;
329 positions.resize(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700330
331 size_t historySize = event->getHistorySize();
Siarhei Vishniakou69e4d0f2020-09-14 19:53:29 -0500332 for (size_t h = 0; h <= historySize; h++) {
333 nsecs_t eventTime = event->getHistoricalEventTime(h);
Jeff Brown5912f952013-07-01 19:10:31 -0700334 for (size_t i = 0; i < pointerCount; i++) {
335 uint32_t index = pointerIndex[i];
Siarhei Vishniakou4c3137a2018-11-13 13:33:52 -0800336 positions[index].x = event->getHistoricalX(i, h);
337 positions[index].y = event->getHistoricalY(i, h);
Jeff Brown5912f952013-07-01 19:10:31 -0700338 }
339 addMovement(eventTime, idBits, positions);
340 }
Jeff Brown5912f952013-07-01 19:10:31 -0700341}
342
343bool VelocityTracker::getVelocity(uint32_t id, float* outVx, float* outVy) const {
344 Estimator estimator;
345 if (getEstimator(id, &estimator) && estimator.degree >= 1) {
346 *outVx = estimator.xCoeff[1];
347 *outVy = estimator.yCoeff[1];
348 return true;
349 }
350 *outVx = 0;
351 *outVy = 0;
352 return false;
353}
354
355bool VelocityTracker::getEstimator(uint32_t id, Estimator* outEstimator) const {
356 return mStrategy->getEstimator(id, outEstimator);
357}
358
359
360// --- LeastSquaresVelocityTrackerStrategy ---
361
Jeff Brown5912f952013-07-01 19:10:31 -0700362LeastSquaresVelocityTrackerStrategy::LeastSquaresVelocityTrackerStrategy(
363 uint32_t degree, Weighting weighting) :
364 mDegree(degree), mWeighting(weighting) {
365 clear();
366}
367
368LeastSquaresVelocityTrackerStrategy::~LeastSquaresVelocityTrackerStrategy() {
369}
370
371void LeastSquaresVelocityTrackerStrategy::clear() {
372 mIndex = 0;
373 mMovements[0].idBits.clear();
374}
375
376void LeastSquaresVelocityTrackerStrategy::clearPointers(BitSet32 idBits) {
377 BitSet32 remainingIdBits(mMovements[mIndex].idBits.value & ~idBits.value);
378 mMovements[mIndex].idBits = remainingIdBits;
379}
380
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500381void LeastSquaresVelocityTrackerStrategy::addMovement(
382 nsecs_t eventTime, BitSet32 idBits,
383 const std::vector<VelocityTracker::Position>& positions) {
Siarhei Vishniakou346ac6a2019-04-10 09:58:05 -0700384 if (mMovements[mIndex].eventTime != eventTime) {
385 // When ACTION_POINTER_DOWN happens, we will first receive ACTION_MOVE with the coordinates
386 // of the existing pointers, and then ACTION_POINTER_DOWN with the coordinates that include
387 // the new pointer. If the eventtimes for both events are identical, just update the data
388 // for this time.
389 // We only compare against the last value, as it is likely that addMovement is called
390 // in chronological order as events occur.
391 mIndex++;
392 }
393 if (mIndex == HISTORY_SIZE) {
Jeff Brown5912f952013-07-01 19:10:31 -0700394 mIndex = 0;
395 }
396
397 Movement& movement = mMovements[mIndex];
398 movement.eventTime = eventTime;
399 movement.idBits = idBits;
400 uint32_t count = idBits.count();
401 for (uint32_t i = 0; i < count; i++) {
402 movement.positions[i] = positions[i];
403 }
404}
405
406/**
407 * Solves a linear least squares problem to obtain a N degree polynomial that fits
408 * the specified input data as nearly as possible.
409 *
410 * Returns true if a solution is found, false otherwise.
411 *
412 * The input consists of two vectors of data points X and Y with indices 0..m-1
413 * along with a weight vector W of the same size.
414 *
415 * The output is a vector B with indices 0..n that describes a polynomial
416 * that fits the data, such the sum of W[i] * W[i] * abs(Y[i] - (B[0] + B[1] X[i]
417 * + B[2] X[i]^2 ... B[n] X[i]^n)) for all i between 0 and m-1 is minimized.
418 *
419 * Accordingly, the weight vector W should be initialized by the caller with the
420 * reciprocal square root of the variance of the error in each input data point.
421 * In other words, an ideal choice for W would be W[i] = 1 / var(Y[i]) = 1 / stddev(Y[i]).
422 * The weights express the relative importance of each data point. If the weights are
423 * all 1, then the data points are considered to be of equal importance when fitting
424 * the polynomial. It is a good idea to choose weights that diminish the importance
425 * of data points that may have higher than usual error margins.
426 *
427 * Errors among data points are assumed to be independent. W is represented here
428 * as a vector although in the literature it is typically taken to be a diagonal matrix.
429 *
430 * That is to say, the function that generated the input data can be approximated
431 * by y(x) ~= B[0] + B[1] x + B[2] x^2 + ... + B[n] x^n.
432 *
433 * The coefficient of determination (R^2) is also returned to describe the goodness
434 * of fit of the model for the given data. It is a value between 0 and 1, where 1
435 * indicates perfect correspondence.
436 *
437 * This function first expands the X vector to a m by n matrix A such that
438 * A[i][0] = 1, A[i][1] = X[i], A[i][2] = X[i]^2, ..., A[i][n] = X[i]^n, then
439 * multiplies it by w[i]./
440 *
441 * Then it calculates the QR decomposition of A yielding an m by m orthonormal matrix Q
442 * and an m by n upper triangular matrix R. Because R is upper triangular (lower
443 * part is all zeroes), we can simplify the decomposition into an m by n matrix
444 * Q1 and a n by n matrix R1 such that A = Q1 R1.
445 *
446 * Finally we solve the system of linear equations given by R1 B = (Qtranspose W Y)
447 * to find B.
448 *
449 * For efficiency, we lay out A and Q column-wise in memory because we frequently
450 * operate on the column vectors. Conversely, we lay out R row-wise.
451 *
452 * http://en.wikipedia.org/wiki/Numerical_methods_for_linear_least_squares
453 * http://en.wikipedia.org/wiki/Gram-Schmidt
454 */
Siarhei Vishniakou81e8b162020-09-14 22:10:11 -0500455static bool solveLeastSquares(const std::vector<float>& x, const std::vector<float>& y,
456 const std::vector<float>& w, uint32_t n, float* outB, float* outDet) {
457 const size_t m = x.size();
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000458
459 ALOGD_IF(DEBUG_STRATEGY, "solveLeastSquares: m=%d, n=%d, x=%s, y=%s, w=%s", int(m), int(n),
460 vectorToString(x).c_str(), vectorToString(y).c_str(), vectorToString(w).c_str());
461
Siarhei Vishniakou81e8b162020-09-14 22:10:11 -0500462 LOG_ALWAYS_FATAL_IF(m != y.size() || m != w.size(), "Mismatched vector sizes");
Jeff Brown5912f952013-07-01 19:10:31 -0700463
464 // Expand the X vector to a matrix A, pre-multiplied by the weights.
465 float a[n][m]; // column-major order
466 for (uint32_t h = 0; h < m; h++) {
467 a[0][h] = w[h];
468 for (uint32_t i = 1; i < n; i++) {
469 a[i][h] = a[i - 1][h] * x[h];
470 }
471 }
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000472
473 ALOGD_IF(DEBUG_STRATEGY, " - a=%s",
474 matrixToString(&a[0][0], m, n, false /*rowMajor*/).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700475
476 // Apply the Gram-Schmidt process to A to obtain its QR decomposition.
477 float q[n][m]; // orthonormal basis, column-major order
478 float r[n][n]; // upper triangular matrix, row-major order
479 for (uint32_t j = 0; j < n; j++) {
480 for (uint32_t h = 0; h < m; h++) {
481 q[j][h] = a[j][h];
482 }
483 for (uint32_t i = 0; i < j; i++) {
484 float dot = vectorDot(&q[j][0], &q[i][0], m);
485 for (uint32_t h = 0; h < m; h++) {
486 q[j][h] -= dot * q[i][h];
487 }
488 }
489
490 float norm = vectorNorm(&q[j][0], m);
491 if (norm < 0.000001f) {
492 // vectors are linearly dependent or zero so no solution
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000493 ALOGD_IF(DEBUG_STRATEGY, " - no solution, norm=%f", norm);
Jeff Brown5912f952013-07-01 19:10:31 -0700494 return false;
495 }
496
497 float invNorm = 1.0f / norm;
498 for (uint32_t h = 0; h < m; h++) {
499 q[j][h] *= invNorm;
500 }
501 for (uint32_t i = 0; i < n; i++) {
502 r[j][i] = i < j ? 0 : vectorDot(&q[j][0], &a[i][0], m);
503 }
504 }
Siarhei Vishniakoue37bcec2021-09-28 14:24:32 -0700505 if (DEBUG_STRATEGY) {
506 ALOGD(" - q=%s", matrixToString(&q[0][0], m, n, false /*rowMajor*/).c_str());
507 ALOGD(" - r=%s", matrixToString(&r[0][0], n, n, true /*rowMajor*/).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700508
Siarhei Vishniakoue37bcec2021-09-28 14:24:32 -0700509 // calculate QR, if we factored A correctly then QR should equal A
510 float qr[n][m];
511 for (uint32_t h = 0; h < m; h++) {
512 for (uint32_t i = 0; i < n; i++) {
513 qr[i][h] = 0;
514 for (uint32_t j = 0; j < n; j++) {
515 qr[i][h] += q[j][h] * r[j][i];
516 }
Jeff Brown5912f952013-07-01 19:10:31 -0700517 }
518 }
Siarhei Vishniakoue37bcec2021-09-28 14:24:32 -0700519 ALOGD(" - qr=%s", matrixToString(&qr[0][0], m, n, false /*rowMajor*/).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700520 }
Jeff Brown5912f952013-07-01 19:10:31 -0700521
522 // Solve R B = Qt W Y to find B. This is easy because R is upper triangular.
523 // We just work from bottom-right to top-left calculating B's coefficients.
524 float wy[m];
525 for (uint32_t h = 0; h < m; h++) {
526 wy[h] = y[h] * w[h];
527 }
Dan Austin389ddba2015-09-22 14:32:03 -0700528 for (uint32_t i = n; i != 0; ) {
529 i--;
Jeff Brown5912f952013-07-01 19:10:31 -0700530 outB[i] = vectorDot(&q[i][0], wy, m);
531 for (uint32_t j = n - 1; j > i; j--) {
532 outB[i] -= r[i][j] * outB[j];
533 }
534 outB[i] /= r[i][i];
535 }
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000536
537 ALOGD_IF(DEBUG_STRATEGY, " - b=%s", vectorToString(outB, n).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700538
539 // Calculate the coefficient of determination as 1 - (SSerr / SStot) where
540 // SSerr is the residual sum of squares (variance of the error),
541 // and SStot is the total sum of squares (variance of the data) where each
542 // has been weighted.
543 float ymean = 0;
544 for (uint32_t h = 0; h < m; h++) {
545 ymean += y[h];
546 }
547 ymean /= m;
548
549 float sserr = 0;
550 float sstot = 0;
551 for (uint32_t h = 0; h < m; h++) {
552 float err = y[h] - outB[0];
553 float term = 1;
554 for (uint32_t i = 1; i < n; i++) {
555 term *= x[h];
556 err -= term * outB[i];
557 }
558 sserr += w[h] * w[h] * err * err;
559 float var = y[h] - ymean;
560 sstot += w[h] * w[h] * var * var;
561 }
562 *outDet = sstot > 0.000001f ? 1.0f - (sserr / sstot) : 1;
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000563
564 ALOGD_IF(DEBUG_STRATEGY, " - sserr=%f", sserr);
565 ALOGD_IF(DEBUG_STRATEGY, " - sstot=%f", sstot);
566 ALOGD_IF(DEBUG_STRATEGY, " - det=%f", *outDet);
567
Jeff Brown5912f952013-07-01 19:10:31 -0700568 return true;
569}
570
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100571/*
572 * Optimized unweighted second-order least squares fit. About 2x speed improvement compared to
573 * the default implementation
574 */
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700575static std::optional<std::array<float, 3>> solveUnweightedLeastSquaresDeg2(
Siarhei Vishniakou81e8b162020-09-14 22:10:11 -0500576 const std::vector<float>& x, const std::vector<float>& y) {
577 const size_t count = x.size();
578 LOG_ALWAYS_FATAL_IF(count != y.size(), "Mismatching array sizes");
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700579 // Solving y = a*x^2 + b*x + c
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100580 float sxi = 0, sxiyi = 0, syi = 0, sxi2 = 0, sxi3 = 0, sxi2yi = 0, sxi4 = 0;
581
582 for (size_t i = 0; i < count; i++) {
583 float xi = x[i];
584 float yi = y[i];
585 float xi2 = xi*xi;
586 float xi3 = xi2*xi;
587 float xi4 = xi3*xi;
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100588 float xiyi = xi*yi;
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700589 float xi2yi = xi2*yi;
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100590
591 sxi += xi;
592 sxi2 += xi2;
593 sxiyi += xiyi;
594 sxi2yi += xi2yi;
595 syi += yi;
596 sxi3 += xi3;
597 sxi4 += xi4;
598 }
599
600 float Sxx = sxi2 - sxi*sxi / count;
601 float Sxy = sxiyi - sxi*syi / count;
602 float Sxx2 = sxi3 - sxi*sxi2 / count;
603 float Sx2y = sxi2yi - sxi2*syi / count;
604 float Sx2x2 = sxi4 - sxi2*sxi2 / count;
605
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100606 float denominator = Sxx*Sx2x2 - Sxx2*Sxx2;
607 if (denominator == 0) {
608 ALOGW("division by 0 when computing velocity, Sxx=%f, Sx2x2=%f, Sxx2=%f", Sxx, Sx2x2, Sxx2);
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700609 return std::nullopt;
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100610 }
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700611 // Compute a
612 float numerator = Sx2y*Sxx - Sxy*Sxx2;
613 float a = numerator / denominator;
614
615 // Compute b
616 numerator = Sxy*Sx2x2 - Sx2y*Sxx2;
617 float b = numerator / denominator;
618
619 // Compute c
620 float c = syi/count - b * sxi/count - a * sxi2/count;
621
622 return std::make_optional(std::array<float, 3>({c, b, a}));
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100623}
624
Jeff Brown5912f952013-07-01 19:10:31 -0700625bool LeastSquaresVelocityTrackerStrategy::getEstimator(uint32_t id,
626 VelocityTracker::Estimator* outEstimator) const {
627 outEstimator->clear();
628
629 // Iterate over movement samples in reverse time order and collect samples.
Siarhei Vishniakou81e8b162020-09-14 22:10:11 -0500630 std::vector<float> x;
631 std::vector<float> y;
632 std::vector<float> w;
633 std::vector<float> time;
634
Jeff Brown5912f952013-07-01 19:10:31 -0700635 uint32_t index = mIndex;
636 const Movement& newestMovement = mMovements[mIndex];
637 do {
638 const Movement& movement = mMovements[index];
639 if (!movement.idBits.hasBit(id)) {
640 break;
641 }
642
643 nsecs_t age = newestMovement.eventTime - movement.eventTime;
644 if (age > HORIZON) {
645 break;
646 }
647
648 const VelocityTracker::Position& position = movement.getPosition(id);
Siarhei Vishniakou81e8b162020-09-14 22:10:11 -0500649 x.push_back(position.x);
650 y.push_back(position.y);
651 w.push_back(chooseWeight(index));
652 time.push_back(-age * 0.000000001f);
Jeff Brown5912f952013-07-01 19:10:31 -0700653 index = (index == 0 ? HISTORY_SIZE : index) - 1;
Siarhei Vishniakou81e8b162020-09-14 22:10:11 -0500654 } while (x.size() < HISTORY_SIZE);
Jeff Brown5912f952013-07-01 19:10:31 -0700655
Siarhei Vishniakou81e8b162020-09-14 22:10:11 -0500656 const size_t m = x.size();
Jeff Brown5912f952013-07-01 19:10:31 -0700657 if (m == 0) {
658 return false; // no data
659 }
660
661 // Calculate a least squares polynomial fit.
662 uint32_t degree = mDegree;
663 if (degree > m - 1) {
664 degree = m - 1;
665 }
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700666
667 if (degree == 2 && mWeighting == WEIGHTING_NONE) {
668 // Optimize unweighted, quadratic polynomial fit
Siarhei Vishniakou81e8b162020-09-14 22:10:11 -0500669 std::optional<std::array<float, 3>> xCoeff = solveUnweightedLeastSquaresDeg2(time, x);
670 std::optional<std::array<float, 3>> yCoeff = solveUnweightedLeastSquaresDeg2(time, y);
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700671 if (xCoeff && yCoeff) {
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100672 outEstimator->time = newestMovement.eventTime;
673 outEstimator->degree = 2;
674 outEstimator->confidence = 1;
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700675 for (size_t i = 0; i <= outEstimator->degree; i++) {
676 outEstimator->xCoeff[i] = (*xCoeff)[i];
677 outEstimator->yCoeff[i] = (*yCoeff)[i];
678 }
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100679 return true;
680 }
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700681 } else if (degree >= 1) {
682 // General case for an Nth degree polynomial fit
Jeff Brown5912f952013-07-01 19:10:31 -0700683 float xdet, ydet;
684 uint32_t n = degree + 1;
Siarhei Vishniakou81e8b162020-09-14 22:10:11 -0500685 if (solveLeastSquares(time, x, w, n, outEstimator->xCoeff, &xdet) &&
686 solveLeastSquares(time, y, w, n, outEstimator->yCoeff, &ydet)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700687 outEstimator->time = newestMovement.eventTime;
688 outEstimator->degree = degree;
689 outEstimator->confidence = xdet * ydet;
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000690
691 ALOGD_IF(DEBUG_STRATEGY, "estimate: degree=%d, xCoeff=%s, yCoeff=%s, confidence=%f",
692 int(outEstimator->degree), vectorToString(outEstimator->xCoeff, n).c_str(),
693 vectorToString(outEstimator->yCoeff, n).c_str(), outEstimator->confidence);
694
Jeff Brown5912f952013-07-01 19:10:31 -0700695 return true;
696 }
697 }
698
699 // No velocity data available for this pointer, but we do have its current position.
700 outEstimator->xCoeff[0] = x[0];
701 outEstimator->yCoeff[0] = y[0];
702 outEstimator->time = newestMovement.eventTime;
703 outEstimator->degree = 0;
704 outEstimator->confidence = 1;
705 return true;
706}
707
708float LeastSquaresVelocityTrackerStrategy::chooseWeight(uint32_t index) const {
709 switch (mWeighting) {
710 case WEIGHTING_DELTA: {
711 // Weight points based on how much time elapsed between them and the next
712 // point so that points that "cover" a shorter time span are weighed less.
713 // delta 0ms: 0.5
714 // delta 10ms: 1.0
715 if (index == mIndex) {
716 return 1.0f;
717 }
718 uint32_t nextIndex = (index + 1) % HISTORY_SIZE;
719 float deltaMillis = (mMovements[nextIndex].eventTime- mMovements[index].eventTime)
720 * 0.000001f;
721 if (deltaMillis < 0) {
722 return 0.5f;
723 }
724 if (deltaMillis < 10) {
725 return 0.5f + deltaMillis * 0.05;
726 }
727 return 1.0f;
728 }
729
730 case WEIGHTING_CENTRAL: {
731 // Weight points based on their age, weighing very recent and very old points less.
732 // age 0ms: 0.5
733 // age 10ms: 1.0
734 // age 50ms: 1.0
735 // age 60ms: 0.5
736 float ageMillis = (mMovements[mIndex].eventTime - mMovements[index].eventTime)
737 * 0.000001f;
738 if (ageMillis < 0) {
739 return 0.5f;
740 }
741 if (ageMillis < 10) {
742 return 0.5f + ageMillis * 0.05;
743 }
744 if (ageMillis < 50) {
745 return 1.0f;
746 }
747 if (ageMillis < 60) {
748 return 0.5f + (60 - ageMillis) * 0.05;
749 }
750 return 0.5f;
751 }
752
753 case WEIGHTING_RECENT: {
754 // Weight points based on their age, weighing older points less.
755 // age 0ms: 1.0
756 // age 50ms: 1.0
757 // age 100ms: 0.5
758 float ageMillis = (mMovements[mIndex].eventTime - mMovements[index].eventTime)
759 * 0.000001f;
760 if (ageMillis < 50) {
761 return 1.0f;
762 }
763 if (ageMillis < 100) {
764 return 0.5f + (100 - ageMillis) * 0.01f;
765 }
766 return 0.5f;
767 }
768
769 case WEIGHTING_NONE:
770 default:
771 return 1.0f;
772 }
773}
774
775
776// --- IntegratingVelocityTrackerStrategy ---
777
778IntegratingVelocityTrackerStrategy::IntegratingVelocityTrackerStrategy(uint32_t degree) :
779 mDegree(degree) {
780}
781
782IntegratingVelocityTrackerStrategy::~IntegratingVelocityTrackerStrategy() {
783}
784
785void IntegratingVelocityTrackerStrategy::clear() {
786 mPointerIdBits.clear();
787}
788
789void IntegratingVelocityTrackerStrategy::clearPointers(BitSet32 idBits) {
790 mPointerIdBits.value &= ~idBits.value;
791}
792
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500793void IntegratingVelocityTrackerStrategy::addMovement(
794 nsecs_t eventTime, BitSet32 idBits,
795 const std::vector<VelocityTracker::Position>& positions) {
Jeff Brown5912f952013-07-01 19:10:31 -0700796 uint32_t index = 0;
797 for (BitSet32 iterIdBits(idBits); !iterIdBits.isEmpty();) {
798 uint32_t id = iterIdBits.clearFirstMarkedBit();
799 State& state = mPointerState[id];
800 const VelocityTracker::Position& position = positions[index++];
801 if (mPointerIdBits.hasBit(id)) {
802 updateState(state, eventTime, position.x, position.y);
803 } else {
804 initState(state, eventTime, position.x, position.y);
805 }
806 }
807
808 mPointerIdBits = idBits;
809}
810
811bool IntegratingVelocityTrackerStrategy::getEstimator(uint32_t id,
812 VelocityTracker::Estimator* outEstimator) const {
813 outEstimator->clear();
814
815 if (mPointerIdBits.hasBit(id)) {
816 const State& state = mPointerState[id];
817 populateEstimator(state, outEstimator);
818 return true;
819 }
820
821 return false;
822}
823
824void IntegratingVelocityTrackerStrategy::initState(State& state,
825 nsecs_t eventTime, float xpos, float ypos) const {
826 state.updateTime = eventTime;
827 state.degree = 0;
828
829 state.xpos = xpos;
830 state.xvel = 0;
831 state.xaccel = 0;
832 state.ypos = ypos;
833 state.yvel = 0;
834 state.yaccel = 0;
835}
836
837void IntegratingVelocityTrackerStrategy::updateState(State& state,
838 nsecs_t eventTime, float xpos, float ypos) const {
839 const nsecs_t MIN_TIME_DELTA = 2 * NANOS_PER_MS;
840 const float FILTER_TIME_CONSTANT = 0.010f; // 10 milliseconds
841
842 if (eventTime <= state.updateTime + MIN_TIME_DELTA) {
843 return;
844 }
845
846 float dt = (eventTime - state.updateTime) * 0.000000001f;
847 state.updateTime = eventTime;
848
849 float xvel = (xpos - state.xpos) / dt;
850 float yvel = (ypos - state.ypos) / dt;
851 if (state.degree == 0) {
852 state.xvel = xvel;
853 state.yvel = yvel;
854 state.degree = 1;
855 } else {
856 float alpha = dt / (FILTER_TIME_CONSTANT + dt);
857 if (mDegree == 1) {
858 state.xvel += (xvel - state.xvel) * alpha;
859 state.yvel += (yvel - state.yvel) * alpha;
860 } else {
861 float xaccel = (xvel - state.xvel) / dt;
862 float yaccel = (yvel - state.yvel) / dt;
863 if (state.degree == 1) {
864 state.xaccel = xaccel;
865 state.yaccel = yaccel;
866 state.degree = 2;
867 } else {
868 state.xaccel += (xaccel - state.xaccel) * alpha;
869 state.yaccel += (yaccel - state.yaccel) * alpha;
870 }
871 state.xvel += (state.xaccel * dt) * alpha;
872 state.yvel += (state.yaccel * dt) * alpha;
873 }
874 }
875 state.xpos = xpos;
876 state.ypos = ypos;
877}
878
879void IntegratingVelocityTrackerStrategy::populateEstimator(const State& state,
880 VelocityTracker::Estimator* outEstimator) const {
881 outEstimator->time = state.updateTime;
882 outEstimator->confidence = 1.0f;
883 outEstimator->degree = state.degree;
884 outEstimator->xCoeff[0] = state.xpos;
885 outEstimator->xCoeff[1] = state.xvel;
886 outEstimator->xCoeff[2] = state.xaccel / 2;
887 outEstimator->yCoeff[0] = state.ypos;
888 outEstimator->yCoeff[1] = state.yvel;
889 outEstimator->yCoeff[2] = state.yaccel / 2;
890}
891
892
893// --- LegacyVelocityTrackerStrategy ---
894
Jeff Brown5912f952013-07-01 19:10:31 -0700895LegacyVelocityTrackerStrategy::LegacyVelocityTrackerStrategy() {
896 clear();
897}
898
899LegacyVelocityTrackerStrategy::~LegacyVelocityTrackerStrategy() {
900}
901
902void LegacyVelocityTrackerStrategy::clear() {
903 mIndex = 0;
904 mMovements[0].idBits.clear();
905}
906
907void LegacyVelocityTrackerStrategy::clearPointers(BitSet32 idBits) {
908 BitSet32 remainingIdBits(mMovements[mIndex].idBits.value & ~idBits.value);
909 mMovements[mIndex].idBits = remainingIdBits;
910}
911
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500912void LegacyVelocityTrackerStrategy::addMovement(
913 nsecs_t eventTime, BitSet32 idBits,
914 const std::vector<VelocityTracker::Position>& positions) {
Jeff Brown5912f952013-07-01 19:10:31 -0700915 if (++mIndex == HISTORY_SIZE) {
916 mIndex = 0;
917 }
918
919 Movement& movement = mMovements[mIndex];
920 movement.eventTime = eventTime;
921 movement.idBits = idBits;
922 uint32_t count = idBits.count();
923 for (uint32_t i = 0; i < count; i++) {
924 movement.positions[i] = positions[i];
925 }
926}
927
928bool LegacyVelocityTrackerStrategy::getEstimator(uint32_t id,
929 VelocityTracker::Estimator* outEstimator) const {
930 outEstimator->clear();
931
932 const Movement& newestMovement = mMovements[mIndex];
933 if (!newestMovement.idBits.hasBit(id)) {
934 return false; // no data
935 }
936
937 // Find the oldest sample that contains the pointer and that is not older than HORIZON.
938 nsecs_t minTime = newestMovement.eventTime - HORIZON;
939 uint32_t oldestIndex = mIndex;
940 uint32_t numTouches = 1;
941 do {
942 uint32_t nextOldestIndex = (oldestIndex == 0 ? HISTORY_SIZE : oldestIndex) - 1;
943 const Movement& nextOldestMovement = mMovements[nextOldestIndex];
944 if (!nextOldestMovement.idBits.hasBit(id)
945 || nextOldestMovement.eventTime < minTime) {
946 break;
947 }
948 oldestIndex = nextOldestIndex;
949 } while (++numTouches < HISTORY_SIZE);
950
951 // Calculate an exponentially weighted moving average of the velocity estimate
952 // at different points in time measured relative to the oldest sample.
953 // This is essentially an IIR filter. Newer samples are weighted more heavily
954 // than older samples. Samples at equal time points are weighted more or less
955 // equally.
956 //
957 // One tricky problem is that the sample data may be poorly conditioned.
958 // Sometimes samples arrive very close together in time which can cause us to
959 // overestimate the velocity at that time point. Most samples might be measured
960 // 16ms apart but some consecutive samples could be only 0.5sm apart because
961 // the hardware or driver reports them irregularly or in bursts.
962 float accumVx = 0;
963 float accumVy = 0;
964 uint32_t index = oldestIndex;
965 uint32_t samplesUsed = 0;
966 const Movement& oldestMovement = mMovements[oldestIndex];
967 const VelocityTracker::Position& oldestPosition = oldestMovement.getPosition(id);
968 nsecs_t lastDuration = 0;
969
970 while (numTouches-- > 1) {
971 if (++index == HISTORY_SIZE) {
972 index = 0;
973 }
974 const Movement& movement = mMovements[index];
975 nsecs_t duration = movement.eventTime - oldestMovement.eventTime;
976
977 // If the duration between samples is small, we may significantly overestimate
978 // the velocity. Consequently, we impose a minimum duration constraint on the
979 // samples that we include in the calculation.
980 if (duration >= MIN_DURATION) {
981 const VelocityTracker::Position& position = movement.getPosition(id);
982 float scale = 1000000000.0f / duration; // one over time delta in seconds
983 float vx = (position.x - oldestPosition.x) * scale;
984 float vy = (position.y - oldestPosition.y) * scale;
985 accumVx = (accumVx * lastDuration + vx * duration) / (duration + lastDuration);
986 accumVy = (accumVy * lastDuration + vy * duration) / (duration + lastDuration);
987 lastDuration = duration;
988 samplesUsed += 1;
989 }
990 }
991
992 // Report velocity.
993 const VelocityTracker::Position& newestPosition = newestMovement.getPosition(id);
994 outEstimator->time = newestMovement.eventTime;
995 outEstimator->confidence = 1;
996 outEstimator->xCoeff[0] = newestPosition.x;
997 outEstimator->yCoeff[0] = newestPosition.y;
998 if (samplesUsed) {
999 outEstimator->xCoeff[1] = accumVx;
1000 outEstimator->yCoeff[1] = accumVy;
1001 outEstimator->degree = 1;
1002 } else {
1003 outEstimator->degree = 0;
1004 }
1005 return true;
1006}
1007
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001008// --- ImpulseVelocityTrackerStrategy ---
1009
1010ImpulseVelocityTrackerStrategy::ImpulseVelocityTrackerStrategy() {
1011 clear();
1012}
1013
1014ImpulseVelocityTrackerStrategy::~ImpulseVelocityTrackerStrategy() {
1015}
1016
1017void ImpulseVelocityTrackerStrategy::clear() {
1018 mIndex = 0;
1019 mMovements[0].idBits.clear();
1020}
1021
1022void ImpulseVelocityTrackerStrategy::clearPointers(BitSet32 idBits) {
1023 BitSet32 remainingIdBits(mMovements[mIndex].idBits.value & ~idBits.value);
1024 mMovements[mIndex].idBits = remainingIdBits;
1025}
1026
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -05001027void ImpulseVelocityTrackerStrategy::addMovement(
1028 nsecs_t eventTime, BitSet32 idBits,
1029 const std::vector<VelocityTracker::Position>& positions) {
Siarhei Vishniakou346ac6a2019-04-10 09:58:05 -07001030 if (mMovements[mIndex].eventTime != eventTime) {
1031 // When ACTION_POINTER_DOWN happens, we will first receive ACTION_MOVE with the coordinates
1032 // of the existing pointers, and then ACTION_POINTER_DOWN with the coordinates that include
1033 // the new pointer. If the eventtimes for both events are identical, just update the data
1034 // for this time.
1035 // We only compare against the last value, as it is likely that addMovement is called
1036 // in chronological order as events occur.
1037 mIndex++;
1038 }
1039 if (mIndex == HISTORY_SIZE) {
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001040 mIndex = 0;
1041 }
1042
1043 Movement& movement = mMovements[mIndex];
1044 movement.eventTime = eventTime;
1045 movement.idBits = idBits;
1046 uint32_t count = idBits.count();
1047 for (uint32_t i = 0; i < count; i++) {
1048 movement.positions[i] = positions[i];
1049 }
1050}
1051
1052/**
1053 * Calculate the total impulse provided to the screen and the resulting velocity.
1054 *
1055 * The touchscreen is modeled as a physical object.
1056 * Initial condition is discussed below, but for now suppose that v(t=0) = 0
1057 *
1058 * The kinetic energy of the object at the release is E=0.5*m*v^2
1059 * Then vfinal = sqrt(2E/m). The goal is to calculate E.
1060 *
1061 * The kinetic energy at the release is equal to the total work done on the object by the finger.
1062 * The total work W is the sum of all dW along the path.
1063 *
1064 * dW = F*dx, where dx is the piece of path traveled.
1065 * Force is change of momentum over time, F = dp/dt = m dv/dt.
1066 * Then substituting:
1067 * dW = m (dv/dt) * dx = m * v * dv
1068 *
1069 * Summing along the path, we get:
1070 * W = sum(dW) = sum(m * v * dv) = m * sum(v * dv)
1071 * Since the mass stays constant, the equation for final velocity is:
1072 * vfinal = sqrt(2*sum(v * dv))
1073 *
1074 * Here,
1075 * dv : change of velocity = (v[i+1]-v[i])
1076 * dx : change of distance = (x[i+1]-x[i])
1077 * dt : change of time = (t[i+1]-t[i])
1078 * v : instantaneous velocity = dx/dt
1079 *
1080 * The final formula is:
1081 * vfinal = sqrt(2) * sqrt(sum((v[i]-v[i-1])*|v[i]|)) for all i
1082 * The absolute value is needed to properly account for the sign. If the velocity over a
1083 * particular segment descreases, then this indicates braking, which means that negative
1084 * work was done. So for two positive, but decreasing, velocities, this contribution would be
1085 * negative and will cause a smaller final velocity.
1086 *
1087 * Initial condition
1088 * There are two ways to deal with initial condition:
1089 * 1) Assume that v(0) = 0, which would mean that the screen is initially at rest.
1090 * This is not entirely accurate. We are only taking the past X ms of touch data, where X is
1091 * currently equal to 100. However, a touch event that created a fling probably lasted for longer
1092 * than that, which would mean that the user has already been interacting with the touchscreen
1093 * and it has probably already been moving.
1094 * 2) Assume that the touchscreen has already been moving at a certain velocity, calculate this
1095 * initial velocity and the equivalent energy, and start with this initial energy.
1096 * Consider an example where we have the following data, consisting of 3 points:
1097 * time: t0, t1, t2
1098 * x : x0, x1, x2
1099 * v : 0 , v1, v2
1100 * Here is what will happen in each of these scenarios:
1101 * 1) By directly applying the formula above with the v(0) = 0 boundary condition, we will get
1102 * vfinal = sqrt(2*(|v1|*(v1-v0) + |v2|*(v2-v1))). This can be simplified since v0=0
1103 * vfinal = sqrt(2*(|v1|*v1 + |v2|*(v2-v1))) = sqrt(2*(v1^2 + |v2|*(v2 - v1)))
1104 * since velocity is a real number
1105 * 2) If we treat the screen as already moving, then it must already have an energy (per mass)
1106 * equal to 1/2*v1^2. Then the initial energy should be 1/2*v1*2, and only the second segment
1107 * will contribute to the total kinetic energy (since we can effectively consider that v0=v1).
1108 * This will give the following expression for the final velocity:
1109 * vfinal = sqrt(2*(1/2*v1^2 + |v2|*(v2-v1)))
1110 * This analysis can be generalized to an arbitrary number of samples.
1111 *
1112 *
1113 * Comparing the two equations above, we see that the only mathematical difference
1114 * is the factor of 1/2 in front of the first velocity term.
1115 * This boundary condition would allow for the "proper" calculation of the case when all of the
1116 * samples are equally spaced in time and distance, which should suggest a constant velocity.
1117 *
1118 * Note that approach 2) is sensitive to the proper ordering of the data in time, since
1119 * the boundary condition must be applied to the oldest sample to be accurate.
1120 */
Siarhei Vishniakou97b5e182017-09-01 13:52:33 -07001121static float kineticEnergyToVelocity(float work) {
1122 static constexpr float sqrt2 = 1.41421356237;
1123 return (work < 0 ? -1.0 : 1.0) * sqrtf(fabsf(work)) * sqrt2;
1124}
1125
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001126static float calculateImpulseVelocity(const nsecs_t* t, const float* x, size_t count) {
1127 // The input should be in reversed time order (most recent sample at index i=0)
1128 // t[i] is in nanoseconds, but due to FP arithmetic, convert to seconds inside this function
Siarhei Vishniakou6de8f5e2018-03-02 18:48:15 -08001129 static constexpr float SECONDS_PER_NANO = 1E-9;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001130
1131 if (count < 2) {
1132 return 0; // if 0 or 1 points, velocity is zero
1133 }
1134 if (t[1] > t[0]) { // Algorithm will still work, but not perfectly
1135 ALOGE("Samples provided to calculateImpulseVelocity in the wrong order");
1136 }
1137 if (count == 2) { // if 2 points, basic linear calculation
1138 if (t[1] == t[0]) {
1139 ALOGE("Events have identical time stamps t=%" PRId64 ", setting velocity = 0", t[0]);
1140 return 0;
1141 }
Siarhei Vishniakou6de8f5e2018-03-02 18:48:15 -08001142 return (x[1] - x[0]) / (SECONDS_PER_NANO * (t[1] - t[0]));
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001143 }
1144 // Guaranteed to have at least 3 points here
1145 float work = 0;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001146 for (size_t i = count - 1; i > 0 ; i--) { // start with the oldest sample and go forward in time
1147 if (t[i] == t[i-1]) {
1148 ALOGE("Events have identical time stamps t=%" PRId64 ", skipping sample", t[i]);
1149 continue;
1150 }
Siarhei Vishniakou97b5e182017-09-01 13:52:33 -07001151 float vprev = kineticEnergyToVelocity(work); // v[i-1]
Siarhei Vishniakou6de8f5e2018-03-02 18:48:15 -08001152 float vcurr = (x[i] - x[i-1]) / (SECONDS_PER_NANO * (t[i] - t[i-1])); // v[i]
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001153 work += (vcurr - vprev) * fabsf(vcurr);
1154 if (i == count - 1) {
1155 work *= 0.5; // initial condition, case 2) above
1156 }
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001157 }
Siarhei Vishniakou97b5e182017-09-01 13:52:33 -07001158 return kineticEnergyToVelocity(work);
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001159}
1160
1161bool ImpulseVelocityTrackerStrategy::getEstimator(uint32_t id,
1162 VelocityTracker::Estimator* outEstimator) const {
1163 outEstimator->clear();
1164
1165 // Iterate over movement samples in reverse time order and collect samples.
1166 float x[HISTORY_SIZE];
1167 float y[HISTORY_SIZE];
1168 nsecs_t time[HISTORY_SIZE];
1169 size_t m = 0; // number of points that will be used for fitting
1170 size_t index = mIndex;
1171 const Movement& newestMovement = mMovements[mIndex];
1172 do {
1173 const Movement& movement = mMovements[index];
1174 if (!movement.idBits.hasBit(id)) {
1175 break;
1176 }
1177
1178 nsecs_t age = newestMovement.eventTime - movement.eventTime;
1179 if (age > HORIZON) {
1180 break;
1181 }
1182
1183 const VelocityTracker::Position& position = movement.getPosition(id);
1184 x[m] = position.x;
1185 y[m] = position.y;
1186 time[m] = movement.eventTime;
1187 index = (index == 0 ? HISTORY_SIZE : index) - 1;
1188 } while (++m < HISTORY_SIZE);
1189
1190 if (m == 0) {
1191 return false; // no data
1192 }
1193 outEstimator->xCoeff[0] = 0;
1194 outEstimator->yCoeff[0] = 0;
1195 outEstimator->xCoeff[1] = calculateImpulseVelocity(time, x, m);
1196 outEstimator->yCoeff[1] = calculateImpulseVelocity(time, y, m);
1197 outEstimator->xCoeff[2] = 0;
1198 outEstimator->yCoeff[2] = 0;
1199 outEstimator->time = newestMovement.eventTime;
1200 outEstimator->degree = 2; // similar results to 2nd degree fit
1201 outEstimator->confidence = 1;
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001202
1203 ALOGD_IF(DEBUG_STRATEGY, "velocity: (%.1f, %.1f)", outEstimator->xCoeff[1],
1204 outEstimator->yCoeff[1]);
1205
Siarhei Vishniakou276467b2022-03-17 09:43:28 -07001206 if (DEBUG_IMPULSE) {
1207 // TODO(b/134179997): delete this block once the switch to 'impulse' is complete.
1208 // Calculate the lsq2 velocity for the same inputs to allow runtime comparisons
1209 VelocityTracker lsq2(VelocityTracker::Strategy::LSQ2);
1210 BitSet32 idBits;
1211 const uint32_t pointerId = 0;
1212 idBits.markBit(pointerId);
1213 for (ssize_t i = m - 1; i >= 0; i--) {
1214 lsq2.addMovement(time[i], idBits, {{x[i], y[i]}});
1215 }
1216 float outVx = 0, outVy = 0;
1217 const bool computed = lsq2.getVelocity(pointerId, &outVx, &outVy);
1218 if (computed) {
1219 ALOGD("lsq2 velocity: (%.1f, %.1f)", outVx, outVy);
1220 } else {
1221 ALOGD("lsq2 velocity: could not compute velocity");
1222 }
Siarhei Vishniakoue37bcec2021-09-28 14:24:32 -07001223 }
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001224 return true;
1225}
1226
Jeff Brown5912f952013-07-01 19:10:31 -07001227} // namespace android