1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define LOG_TAG "VelocityTracker" 18 //#define LOG_NDEBUG 0 19 20 // Log debug messages about velocity tracking. 21 #define DEBUG_VELOCITY 0 22 23 // Log debug messages about the progress of the algorithm itself. 24 #define DEBUG_STRATEGY 0 25 26 #include <inttypes.h> 27 #include <limits.h> 28 #include <math.h> 29 30 #include <android-base/stringprintf.h> 31 #include <cutils/properties.h> 32 #include <input/VelocityTracker.h> 33 #include <utils/BitSet.h> 34 #include <utils/Timers.h> 35 36 namespace android { 37 38 // Nanoseconds per milliseconds. 39 static const nsecs_t NANOS_PER_MS = 1000000; 40 41 // Threshold for determining that a pointer has stopped moving. 42 // Some input devices do not send ACTION_MOVE events in the case where a pointer has 43 // stopped. We need to detect this case so that we can accurately predict the 44 // velocity after the pointer starts moving again. 45 static const nsecs_t ASSUME_POINTER_STOPPED_TIME = 40 * NANOS_PER_MS; 46 47 48 static float vectorDot(const float* a, const float* b, uint32_t m) { 49 float r = 0; 50 for (size_t i = 0; i < m; i++) { 51 r += *(a++) * *(b++); 52 } 53 return r; 54 } 55 56 static float vectorNorm(const float* a, uint32_t m) { 57 float r = 0; 58 for (size_t i = 0; i < m; i++) { 59 float t = *(a++); 60 r += t * t; 61 } 62 return sqrtf(r); 63 } 64 65 #if DEBUG_STRATEGY || DEBUG_VELOCITY 66 static std::string vectorToString(const float* a, uint32_t m) { 67 std::string str; 68 str += "["; 69 for (size_t i = 0; i < m; i++) { 70 if (i) { 71 str += ","; 72 } 73 str += android::base::StringPrintf(" %f", *(a++)); 74 } 75 str += " ]"; 76 return str; 77 } 78 #endif 79 80 #if DEBUG_STRATEGY 81 static std::string matrixToString(const float* a, uint32_t m, uint32_t n, bool rowMajor) { 82 std::string str; 83 str = "["; 84 for (size_t i = 0; i < m; i++) { 85 if (i) { 86 str += ","; 87 } 88 str += " ["; 89 for (size_t j = 0; j < n; j++) { 90 if (j) { 91 str += ","; 92 } 93 str += android::base::StringPrintf(" %f", a[rowMajor ? i * n + j : j * m + i]); 94 } 95 str += " ]"; 96 } 97 str += " ]"; 98 return str; 99 } 100 #endif 101 102 103 // --- VelocityTracker --- 104 105 // The default velocity tracker strategy. 106 // Although other strategies are available for testing and comparison purposes, 107 // this is the strategy that applications will actually use. Be very careful 108 // when adjusting the default strategy because it can dramatically affect 109 // (often in a bad way) the user experience. 110 const char* VelocityTracker::DEFAULT_STRATEGY = "lsq2"; 111 112 VelocityTracker::VelocityTracker(const char* strategy) : 113 mLastEventTime(0), mCurrentPointerIdBits(0), mActivePointerId(-1) { 114 char value[PROPERTY_VALUE_MAX]; 115 116 // Allow the default strategy to be overridden using a system property for debugging. 117 if (!strategy) { 118 int length = property_get("debug.velocitytracker.strategy", value, NULL); 119 if (length > 0) { 120 strategy = value; 121 } else { 122 strategy = DEFAULT_STRATEGY; 123 } 124 } 125 126 // Configure the strategy. 127 if (!configureStrategy(strategy)) { 128 ALOGD("Unrecognized velocity tracker strategy name '%s'.", strategy); 129 if (!configureStrategy(DEFAULT_STRATEGY)) { 130 LOG_ALWAYS_FATAL("Could not create the default velocity tracker strategy '%s'!", 131 strategy); 132 } 133 } 134 } 135 136 VelocityTracker::~VelocityTracker() { 137 delete mStrategy; 138 } 139 140 bool VelocityTracker::configureStrategy(const char* strategy) { 141 mStrategy = createStrategy(strategy); 142 return mStrategy != NULL; 143 } 144 145 VelocityTrackerStrategy* VelocityTracker::createStrategy(const char* strategy) { 146 if (!strcmp("impulse", strategy)) { 147 // Physical model of pushing an object. Quality: VERY GOOD. 148 // Works with duplicate coordinates, unclean finger liftoff. 149 return new ImpulseVelocityTrackerStrategy(); 150 } 151 if (!strcmp("lsq1", strategy)) { 152 // 1st order least squares. Quality: POOR. 153 // Frequently underfits the touch data especially when the finger accelerates 154 // or changes direction. Often underestimates velocity. The direction 155 // is overly influenced by historical touch points. 156 return new LeastSquaresVelocityTrackerStrategy(1); 157 } 158 if (!strcmp("lsq2", strategy)) { 159 // 2nd order least squares. Quality: VERY GOOD. 160 // Pretty much ideal, but can be confused by certain kinds of touch data, 161 // particularly if the panel has a tendency to generate delayed, 162 // duplicate or jittery touch coordinates when the finger is released. 163 return new LeastSquaresVelocityTrackerStrategy(2); 164 } 165 if (!strcmp("lsq3", strategy)) { 166 // 3rd order least squares. Quality: UNUSABLE. 167 // Frequently overfits the touch data yielding wildly divergent estimates 168 // of the velocity when the finger is released. 169 return new LeastSquaresVelocityTrackerStrategy(3); 170 } 171 if (!strcmp("wlsq2-delta", strategy)) { 172 // 2nd order weighted least squares, delta weighting. Quality: EXPERIMENTAL 173 return new LeastSquaresVelocityTrackerStrategy(2, 174 LeastSquaresVelocityTrackerStrategy::WEIGHTING_DELTA); 175 } 176 if (!strcmp("wlsq2-central", strategy)) { 177 // 2nd order weighted least squares, central weighting. Quality: EXPERIMENTAL 178 return new LeastSquaresVelocityTrackerStrategy(2, 179 LeastSquaresVelocityTrackerStrategy::WEIGHTING_CENTRAL); 180 } 181 if (!strcmp("wlsq2-recent", strategy)) { 182 // 2nd order weighted least squares, recent weighting. Quality: EXPERIMENTAL 183 return new LeastSquaresVelocityTrackerStrategy(2, 184 LeastSquaresVelocityTrackerStrategy::WEIGHTING_RECENT); 185 } 186 if (!strcmp("int1", strategy)) { 187 // 1st order integrating filter. Quality: GOOD. 188 // Not as good as 'lsq2' because it cannot estimate acceleration but it is 189 // more tolerant of errors. Like 'lsq1', this strategy tends to underestimate 190 // the velocity of a fling but this strategy tends to respond to changes in 191 // direction more quickly and accurately. 192 return new IntegratingVelocityTrackerStrategy(1); 193 } 194 if (!strcmp("int2", strategy)) { 195 // 2nd order integrating filter. Quality: EXPERIMENTAL. 196 // For comparison purposes only. Unlike 'int1' this strategy can compensate 197 // for acceleration but it typically overestimates the effect. 198 return new IntegratingVelocityTrackerStrategy(2); 199 } 200 if (!strcmp("legacy", strategy)) { 201 // Legacy velocity tracker algorithm. Quality: POOR. 202 // For comparison purposes only. This algorithm is strongly influenced by 203 // old data points, consistently underestimates velocity and takes a very long 204 // time to adjust to changes in direction. 205 return new LegacyVelocityTrackerStrategy(); 206 } 207 return NULL; 208 } 209 210 void VelocityTracker::clear() { 211 mCurrentPointerIdBits.clear(); 212 mActivePointerId = -1; 213 214 mStrategy->clear(); 215 } 216 217 void VelocityTracker::clearPointers(BitSet32 idBits) { 218 BitSet32 remainingIdBits(mCurrentPointerIdBits.value & ~idBits.value); 219 mCurrentPointerIdBits = remainingIdBits; 220 221 if (mActivePointerId >= 0 && idBits.hasBit(mActivePointerId)) { 222 mActivePointerId = !remainingIdBits.isEmpty() ? remainingIdBits.firstMarkedBit() : -1; 223 } 224 225 mStrategy->clearPointers(idBits); 226 } 227 228 void VelocityTracker::addMovement(nsecs_t eventTime, BitSet32 idBits, const Position* positions) { 229 while (idBits.count() > MAX_POINTERS) { 230 idBits.clearLastMarkedBit(); 231 } 232 233 if ((mCurrentPointerIdBits.value & idBits.value) 234 && eventTime >= mLastEventTime + ASSUME_POINTER_STOPPED_TIME) { 235 #if DEBUG_VELOCITY 236 ALOGD("VelocityTracker: stopped for %0.3f ms, clearing state.", 237 (eventTime - mLastEventTime) * 0.000001f); 238 #endif 239 // We have not received any movements for too long. Assume that all pointers 240 // have stopped. 241 mStrategy->clear(); 242 } 243 mLastEventTime = eventTime; 244 245 mCurrentPointerIdBits = idBits; 246 if (mActivePointerId < 0 || !idBits.hasBit(mActivePointerId)) { 247 mActivePointerId = idBits.isEmpty() ? -1 : idBits.firstMarkedBit(); 248 } 249 250 mStrategy->addMovement(eventTime, idBits, positions); 251 252 #if DEBUG_VELOCITY 253 ALOGD("VelocityTracker: addMovement eventTime=%" PRId64 ", idBits=0x%08x, activePointerId=%d", 254 eventTime, idBits.value, mActivePointerId); 255 for (BitSet32 iterBits(idBits); !iterBits.isEmpty(); ) { 256 uint32_t id = iterBits.firstMarkedBit(); 257 uint32_t index = idBits.getIndexOfBit(id); 258 iterBits.clearBit(id); 259 Estimator estimator; 260 getEstimator(id, &estimator); 261 ALOGD(" %d: position (%0.3f, %0.3f), " 262 "estimator (degree=%d, xCoeff=%s, yCoeff=%s, confidence=%f)", 263 id, positions[index].x, positions[index].y, 264 int(estimator.degree), 265 vectorToString(estimator.xCoeff, estimator.degree + 1).c_str(), 266 vectorToString(estimator.yCoeff, estimator.degree + 1).c_str(), 267 estimator.confidence); 268 } 269 #endif 270 } 271 272 void VelocityTracker::addMovement(const MotionEvent* event) { 273 int32_t actionMasked = event->getActionMasked(); 274 275 switch (actionMasked) { 276 case AMOTION_EVENT_ACTION_DOWN: 277 case AMOTION_EVENT_ACTION_HOVER_ENTER: 278 // Clear all pointers on down before adding the new movement. 279 clear(); 280 break; 281 case AMOTION_EVENT_ACTION_POINTER_DOWN: { 282 // Start a new movement trace for a pointer that just went down. 283 // We do this on down instead of on up because the client may want to query the 284 // final velocity for a pointer that just went up. 285 BitSet32 downIdBits; 286 downIdBits.markBit(event->getPointerId(event->getActionIndex())); 287 clearPointers(downIdBits); 288 break; 289 } 290 case AMOTION_EVENT_ACTION_MOVE: 291 case AMOTION_EVENT_ACTION_HOVER_MOVE: 292 break; 293 default: 294 // Ignore all other actions because they do not convey any new information about 295 // pointer movement. We also want to preserve the last known velocity of the pointers. 296 // Note that ACTION_UP and ACTION_POINTER_UP always report the last known position 297 // of the pointers that went up. ACTION_POINTER_UP does include the new position of 298 // pointers that remained down but we will also receive an ACTION_MOVE with this 299 // information if any of them actually moved. Since we don't know how many pointers 300 // will be going up at once it makes sense to just wait for the following ACTION_MOVE 301 // before adding the movement. 302 return; 303 } 304 305 size_t pointerCount = event->getPointerCount(); 306 if (pointerCount > MAX_POINTERS) { 307 pointerCount = MAX_POINTERS; 308 } 309 310 BitSet32 idBits; 311 for (size_t i = 0; i < pointerCount; i++) { 312 idBits.markBit(event->getPointerId(i)); 313 } 314 315 uint32_t pointerIndex[MAX_POINTERS]; 316 for (size_t i = 0; i < pointerCount; i++) { 317 pointerIndex[i] = idBits.getIndexOfBit(event->getPointerId(i)); 318 } 319 320 nsecs_t eventTime; 321 Position positions[pointerCount]; 322 323 size_t historySize = event->getHistorySize(); 324 for (size_t h = 0; h < historySize; h++) { 325 eventTime = event->getHistoricalEventTime(h); 326 for (size_t i = 0; i < pointerCount; i++) { 327 uint32_t index = pointerIndex[i]; 328 positions[index].x = event->getHistoricalRawX(i, h); 329 positions[index].y = event->getHistoricalRawY(i, h); 330 } 331 addMovement(eventTime, idBits, positions); 332 } 333 334 eventTime = event->getEventTime(); 335 for (size_t i = 0; i < pointerCount; i++) { 336 uint32_t index = pointerIndex[i]; 337 positions[index].x = event->getRawX(i); 338 positions[index].y = event->getRawY(i); 339 } 340 addMovement(eventTime, idBits, positions); 341 } 342 343 bool 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 355 bool VelocityTracker::getEstimator(uint32_t id, Estimator* outEstimator) const { 356 return mStrategy->getEstimator(id, outEstimator); 357 } 358 359 360 // --- LeastSquaresVelocityTrackerStrategy --- 361 362 LeastSquaresVelocityTrackerStrategy::LeastSquaresVelocityTrackerStrategy( 363 uint32_t degree, Weighting weighting) : 364 mDegree(degree), mWeighting(weighting) { 365 clear(); 366 } 367 368 LeastSquaresVelocityTrackerStrategy::~LeastSquaresVelocityTrackerStrategy() { 369 } 370 371 void LeastSquaresVelocityTrackerStrategy::clear() { 372 mIndex = 0; 373 mMovements[0].idBits.clear(); 374 } 375 376 void LeastSquaresVelocityTrackerStrategy::clearPointers(BitSet32 idBits) { 377 BitSet32 remainingIdBits(mMovements[mIndex].idBits.value & ~idBits.value); 378 mMovements[mIndex].idBits = remainingIdBits; 379 } 380 381 void LeastSquaresVelocityTrackerStrategy::addMovement(nsecs_t eventTime, BitSet32 idBits, 382 const VelocityTracker::Position* positions) { 383 if (++mIndex == HISTORY_SIZE) { 384 mIndex = 0; 385 } 386 387 Movement& movement = mMovements[mIndex]; 388 movement.eventTime = eventTime; 389 movement.idBits = idBits; 390 uint32_t count = idBits.count(); 391 for (uint32_t i = 0; i < count; i++) { 392 movement.positions[i] = positions[i]; 393 } 394 } 395 396 /** 397 * Solves a linear least squares problem to obtain a N degree polynomial that fits 398 * the specified input data as nearly as possible. 399 * 400 * Returns true if a solution is found, false otherwise. 401 * 402 * The input consists of two vectors of data points X and Y with indices 0..m-1 403 * along with a weight vector W of the same size. 404 * 405 * The output is a vector B with indices 0..n that describes a polynomial 406 * that fits the data, such the sum of W[i] * W[i] * abs(Y[i] - (B[0] + B[1] X[i] 407 * + B[2] X[i]^2 ... B[n] X[i]^n)) for all i between 0 and m-1 is minimized. 408 * 409 * Accordingly, the weight vector W should be initialized by the caller with the 410 * reciprocal square root of the variance of the error in each input data point. 411 * In other words, an ideal choice for W would be W[i] = 1 / var(Y[i]) = 1 / stddev(Y[i]). 412 * The weights express the relative importance of each data point. If the weights are 413 * all 1, then the data points are considered to be of equal importance when fitting 414 * the polynomial. It is a good idea to choose weights that diminish the importance 415 * of data points that may have higher than usual error margins. 416 * 417 * Errors among data points are assumed to be independent. W is represented here 418 * as a vector although in the literature it is typically taken to be a diagonal matrix. 419 * 420 * That is to say, the function that generated the input data can be approximated 421 * by y(x) ~= B[0] + B[1] x + B[2] x^2 + ... + B[n] x^n. 422 * 423 * The coefficient of determination (R^2) is also returned to describe the goodness 424 * of fit of the model for the given data. It is a value between 0 and 1, where 1 425 * indicates perfect correspondence. 426 * 427 * This function first expands the X vector to a m by n matrix A such that 428 * A[i][0] = 1, A[i][1] = X[i], A[i][2] = X[i]^2, ..., A[i][n] = X[i]^n, then 429 * multiplies it by w[i]./ 430 * 431 * Then it calculates the QR decomposition of A yielding an m by m orthonormal matrix Q 432 * and an m by n upper triangular matrix R. Because R is upper triangular (lower 433 * part is all zeroes), we can simplify the decomposition into an m by n matrix 434 * Q1 and a n by n matrix R1 such that A = Q1 R1. 435 * 436 * Finally we solve the system of linear equations given by R1 B = (Qtranspose W Y) 437 * to find B. 438 * 439 * For efficiency, we lay out A and Q column-wise in memory because we frequently 440 * operate on the column vectors. Conversely, we lay out R row-wise. 441 * 442 * http://en.wikipedia.org/wiki/Numerical_methods_for_linear_least_squares 443 * http://en.wikipedia.org/wiki/Gram-Schmidt 444 */ 445 static bool solveLeastSquares(const float* x, const float* y, 446 const float* w, uint32_t m, uint32_t n, float* outB, float* outDet) { 447 #if DEBUG_STRATEGY 448 ALOGD("solveLeastSquares: m=%d, n=%d, x=%s, y=%s, w=%s", int(m), int(n), 449 vectorToString(x, m).c_str(), vectorToString(y, m).c_str(), 450 vectorToString(w, m).c_str()); 451 #endif 452 453 // Expand the X vector to a matrix A, pre-multiplied by the weights. 454 float a[n][m]; // column-major order 455 for (uint32_t h = 0; h < m; h++) { 456 a[0][h] = w[h]; 457 for (uint32_t i = 1; i < n; i++) { 458 a[i][h] = a[i - 1][h] * x[h]; 459 } 460 } 461 #if DEBUG_STRATEGY 462 ALOGD(" - a=%s", matrixToString(&a[0][0], m, n, false /*rowMajor*/).c_str()); 463 #endif 464 465 // Apply the Gram-Schmidt process to A to obtain its QR decomposition. 466 float q[n][m]; // orthonormal basis, column-major order 467 float r[n][n]; // upper triangular matrix, row-major order 468 for (uint32_t j = 0; j < n; j++) { 469 for (uint32_t h = 0; h < m; h++) { 470 q[j][h] = a[j][h]; 471 } 472 for (uint32_t i = 0; i < j; i++) { 473 float dot = vectorDot(&q[j][0], &q[i][0], m); 474 for (uint32_t h = 0; h < m; h++) { 475 q[j][h] -= dot * q[i][h]; 476 } 477 } 478 479 float norm = vectorNorm(&q[j][0], m); 480 if (norm < 0.000001f) { 481 // vectors are linearly dependent or zero so no solution 482 #if DEBUG_STRATEGY 483 ALOGD(" - no solution, norm=%f", norm); 484 #endif 485 return false; 486 } 487 488 float invNorm = 1.0f / norm; 489 for (uint32_t h = 0; h < m; h++) { 490 q[j][h] *= invNorm; 491 } 492 for (uint32_t i = 0; i < n; i++) { 493 r[j][i] = i < j ? 0 : vectorDot(&q[j][0], &a[i][0], m); 494 } 495 } 496 #if DEBUG_STRATEGY 497 ALOGD(" - q=%s", matrixToString(&q[0][0], m, n, false /*rowMajor*/).c_str()); 498 ALOGD(" - r=%s", matrixToString(&r[0][0], n, n, true /*rowMajor*/).c_str()); 499 500 // calculate QR, if we factored A correctly then QR should equal A 501 float qr[n][m]; 502 for (uint32_t h = 0; h < m; h++) { 503 for (uint32_t i = 0; i < n; i++) { 504 qr[i][h] = 0; 505 for (uint32_t j = 0; j < n; j++) { 506 qr[i][h] += q[j][h] * r[j][i]; 507 } 508 } 509 } 510 ALOGD(" - qr=%s", matrixToString(&qr[0][0], m, n, false /*rowMajor*/).c_str()); 511 #endif 512 513 // Solve R B = Qt W Y to find B. This is easy because R is upper triangular. 514 // We just work from bottom-right to top-left calculating B's coefficients. 515 float wy[m]; 516 for (uint32_t h = 0; h < m; h++) { 517 wy[h] = y[h] * w[h]; 518 } 519 for (uint32_t i = n; i != 0; ) { 520 i--; 521 outB[i] = vectorDot(&q[i][0], wy, m); 522 for (uint32_t j = n - 1; j > i; j--) { 523 outB[i] -= r[i][j] * outB[j]; 524 } 525 outB[i] /= r[i][i]; 526 } 527 #if DEBUG_STRATEGY 528 ALOGD(" - b=%s", vectorToString(outB, n).c_str()); 529 #endif 530 531 // Calculate the coefficient of determination as 1 - (SSerr / SStot) where 532 // SSerr is the residual sum of squares (variance of the error), 533 // and SStot is the total sum of squares (variance of the data) where each 534 // has been weighted. 535 float ymean = 0; 536 for (uint32_t h = 0; h < m; h++) { 537 ymean += y[h]; 538 } 539 ymean /= m; 540 541 float sserr = 0; 542 float sstot = 0; 543 for (uint32_t h = 0; h < m; h++) { 544 float err = y[h] - outB[0]; 545 float term = 1; 546 for (uint32_t i = 1; i < n; i++) { 547 term *= x[h]; 548 err -= term * outB[i]; 549 } 550 sserr += w[h] * w[h] * err * err; 551 float var = y[h] - ymean; 552 sstot += w[h] * w[h] * var * var; 553 } 554 *outDet = sstot > 0.000001f ? 1.0f - (sserr / sstot) : 1; 555 #if DEBUG_STRATEGY 556 ALOGD(" - sserr=%f", sserr); 557 ALOGD(" - sstot=%f", sstot); 558 ALOGD(" - det=%f", *outDet); 559 #endif 560 return true; 561 } 562 563 /* 564 * Optimized unweighted second-order least squares fit. About 2x speed improvement compared to 565 * the default implementation 566 */ 567 static float solveUnweightedLeastSquaresDeg2(const float* x, const float* y, size_t count) { 568 float sxi = 0, sxiyi = 0, syi = 0, sxi2 = 0, sxi3 = 0, sxi2yi = 0, sxi4 = 0; 569 570 for (size_t i = 0; i < count; i++) { 571 float xi = x[i]; 572 float yi = y[i]; 573 float xi2 = xi*xi; 574 float xi3 = xi2*xi; 575 float xi4 = xi3*xi; 576 float xi2yi = xi2*yi; 577 float xiyi = xi*yi; 578 579 sxi += xi; 580 sxi2 += xi2; 581 sxiyi += xiyi; 582 sxi2yi += xi2yi; 583 syi += yi; 584 sxi3 += xi3; 585 sxi4 += xi4; 586 } 587 588 float Sxx = sxi2 - sxi*sxi / count; 589 float Sxy = sxiyi - sxi*syi / count; 590 float Sxx2 = sxi3 - sxi*sxi2 / count; 591 float Sx2y = sxi2yi - sxi2*syi / count; 592 float Sx2x2 = sxi4 - sxi2*sxi2 / count; 593 594 float numerator = Sxy*Sx2x2 - Sx2y*Sxx2; 595 float denominator = Sxx*Sx2x2 - Sxx2*Sxx2; 596 if (denominator == 0) { 597 ALOGW("division by 0 when computing velocity, Sxx=%f, Sx2x2=%f, Sxx2=%f", Sxx, Sx2x2, Sxx2); 598 return 0; 599 } 600 return numerator/denominator; 601 } 602 603 bool LeastSquaresVelocityTrackerStrategy::getEstimator(uint32_t id, 604 VelocityTracker::Estimator* outEstimator) const { 605 outEstimator->clear(); 606 607 // Iterate over movement samples in reverse time order and collect samples. 608 float x[HISTORY_SIZE]; 609 float y[HISTORY_SIZE]; 610 float w[HISTORY_SIZE]; 611 float time[HISTORY_SIZE]; 612 uint32_t m = 0; 613 uint32_t index = mIndex; 614 const Movement& newestMovement = mMovements[mIndex]; 615 do { 616 const Movement& movement = mMovements[index]; 617 if (!movement.idBits.hasBit(id)) { 618 break; 619 } 620 621 nsecs_t age = newestMovement.eventTime - movement.eventTime; 622 if (age > HORIZON) { 623 break; 624 } 625 626 const VelocityTracker::Position& position = movement.getPosition(id); 627 x[m] = position.x; 628 y[m] = position.y; 629 w[m] = chooseWeight(index); 630 time[m] = -age * 0.000000001f; 631 index = (index == 0 ? HISTORY_SIZE : index) - 1; 632 } while (++m < HISTORY_SIZE); 633 634 if (m == 0) { 635 return false; // no data 636 } 637 638 // Calculate a least squares polynomial fit. 639 uint32_t degree = mDegree; 640 if (degree > m - 1) { 641 degree = m - 1; 642 } 643 if (degree >= 1) { 644 if (degree == 2 && mWeighting == WEIGHTING_NONE) { // optimize unweighted, degree=2 fit 645 outEstimator->time = newestMovement.eventTime; 646 outEstimator->degree = 2; 647 outEstimator->confidence = 1; 648 outEstimator->xCoeff[0] = 0; // only slope is calculated, set rest of coefficients = 0 649 outEstimator->yCoeff[0] = 0; 650 outEstimator->xCoeff[1] = solveUnweightedLeastSquaresDeg2(time, x, m); 651 outEstimator->yCoeff[1] = solveUnweightedLeastSquaresDeg2(time, y, m); 652 outEstimator->xCoeff[2] = 0; 653 outEstimator->yCoeff[2] = 0; 654 return true; 655 } 656 657 float xdet, ydet; 658 uint32_t n = degree + 1; 659 if (solveLeastSquares(time, x, w, m, n, outEstimator->xCoeff, &xdet) 660 && solveLeastSquares(time, y, w, m, n, outEstimator->yCoeff, &ydet)) { 661 outEstimator->time = newestMovement.eventTime; 662 outEstimator->degree = degree; 663 outEstimator->confidence = xdet * ydet; 664 #if DEBUG_STRATEGY 665 ALOGD("estimate: degree=%d, xCoeff=%s, yCoeff=%s, confidence=%f", 666 int(outEstimator->degree), 667 vectorToString(outEstimator->xCoeff, n).c_str(), 668 vectorToString(outEstimator->yCoeff, n).c_str(), 669 outEstimator->confidence); 670 #endif 671 return true; 672 } 673 } 674 675 // No velocity data available for this pointer, but we do have its current position. 676 outEstimator->xCoeff[0] = x[0]; 677 outEstimator->yCoeff[0] = y[0]; 678 outEstimator->time = newestMovement.eventTime; 679 outEstimator->degree = 0; 680 outEstimator->confidence = 1; 681 return true; 682 } 683 684 float LeastSquaresVelocityTrackerStrategy::chooseWeight(uint32_t index) const { 685 switch (mWeighting) { 686 case WEIGHTING_DELTA: { 687 // Weight points based on how much time elapsed between them and the next 688 // point so that points that "cover" a shorter time span are weighed less. 689 // delta 0ms: 0.5 690 // delta 10ms: 1.0 691 if (index == mIndex) { 692 return 1.0f; 693 } 694 uint32_t nextIndex = (index + 1) % HISTORY_SIZE; 695 float deltaMillis = (mMovements[nextIndex].eventTime- mMovements[index].eventTime) 696 * 0.000001f; 697 if (deltaMillis < 0) { 698 return 0.5f; 699 } 700 if (deltaMillis < 10) { 701 return 0.5f + deltaMillis * 0.05; 702 } 703 return 1.0f; 704 } 705 706 case WEIGHTING_CENTRAL: { 707 // Weight points based on their age, weighing very recent and very old points less. 708 // age 0ms: 0.5 709 // age 10ms: 1.0 710 // age 50ms: 1.0 711 // age 60ms: 0.5 712 float ageMillis = (mMovements[mIndex].eventTime - mMovements[index].eventTime) 713 * 0.000001f; 714 if (ageMillis < 0) { 715 return 0.5f; 716 } 717 if (ageMillis < 10) { 718 return 0.5f + ageMillis * 0.05; 719 } 720 if (ageMillis < 50) { 721 return 1.0f; 722 } 723 if (ageMillis < 60) { 724 return 0.5f + (60 - ageMillis) * 0.05; 725 } 726 return 0.5f; 727 } 728 729 case WEIGHTING_RECENT: { 730 // Weight points based on their age, weighing older points less. 731 // age 0ms: 1.0 732 // age 50ms: 1.0 733 // age 100ms: 0.5 734 float ageMillis = (mMovements[mIndex].eventTime - mMovements[index].eventTime) 735 * 0.000001f; 736 if (ageMillis < 50) { 737 return 1.0f; 738 } 739 if (ageMillis < 100) { 740 return 0.5f + (100 - ageMillis) * 0.01f; 741 } 742 return 0.5f; 743 } 744 745 case WEIGHTING_NONE: 746 default: 747 return 1.0f; 748 } 749 } 750 751 752 // --- IntegratingVelocityTrackerStrategy --- 753 754 IntegratingVelocityTrackerStrategy::IntegratingVelocityTrackerStrategy(uint32_t degree) : 755 mDegree(degree) { 756 } 757 758 IntegratingVelocityTrackerStrategy::~IntegratingVelocityTrackerStrategy() { 759 } 760 761 void IntegratingVelocityTrackerStrategy::clear() { 762 mPointerIdBits.clear(); 763 } 764 765 void IntegratingVelocityTrackerStrategy::clearPointers(BitSet32 idBits) { 766 mPointerIdBits.value &= ~idBits.value; 767 } 768 769 void IntegratingVelocityTrackerStrategy::addMovement(nsecs_t eventTime, BitSet32 idBits, 770 const VelocityTracker::Position* positions) { 771 uint32_t index = 0; 772 for (BitSet32 iterIdBits(idBits); !iterIdBits.isEmpty();) { 773 uint32_t id = iterIdBits.clearFirstMarkedBit(); 774 State& state = mPointerState[id]; 775 const VelocityTracker::Position& position = positions[index++]; 776 if (mPointerIdBits.hasBit(id)) { 777 updateState(state, eventTime, position.x, position.y); 778 } else { 779 initState(state, eventTime, position.x, position.y); 780 } 781 } 782 783 mPointerIdBits = idBits; 784 } 785 786 bool IntegratingVelocityTrackerStrategy::getEstimator(uint32_t id, 787 VelocityTracker::Estimator* outEstimator) const { 788 outEstimator->clear(); 789 790 if (mPointerIdBits.hasBit(id)) { 791 const State& state = mPointerState[id]; 792 populateEstimator(state, outEstimator); 793 return true; 794 } 795 796 return false; 797 } 798 799 void IntegratingVelocityTrackerStrategy::initState(State& state, 800 nsecs_t eventTime, float xpos, float ypos) const { 801 state.updateTime = eventTime; 802 state.degree = 0; 803 804 state.xpos = xpos; 805 state.xvel = 0; 806 state.xaccel = 0; 807 state.ypos = ypos; 808 state.yvel = 0; 809 state.yaccel = 0; 810 } 811 812 void IntegratingVelocityTrackerStrategy::updateState(State& state, 813 nsecs_t eventTime, float xpos, float ypos) const { 814 const nsecs_t MIN_TIME_DELTA = 2 * NANOS_PER_MS; 815 const float FILTER_TIME_CONSTANT = 0.010f; // 10 milliseconds 816 817 if (eventTime <= state.updateTime + MIN_TIME_DELTA) { 818 return; 819 } 820 821 float dt = (eventTime - state.updateTime) * 0.000000001f; 822 state.updateTime = eventTime; 823 824 float xvel = (xpos - state.xpos) / dt; 825 float yvel = (ypos - state.ypos) / dt; 826 if (state.degree == 0) { 827 state.xvel = xvel; 828 state.yvel = yvel; 829 state.degree = 1; 830 } else { 831 float alpha = dt / (FILTER_TIME_CONSTANT + dt); 832 if (mDegree == 1) { 833 state.xvel += (xvel - state.xvel) * alpha; 834 state.yvel += (yvel - state.yvel) * alpha; 835 } else { 836 float xaccel = (xvel - state.xvel) / dt; 837 float yaccel = (yvel - state.yvel) / dt; 838 if (state.degree == 1) { 839 state.xaccel = xaccel; 840 state.yaccel = yaccel; 841 state.degree = 2; 842 } else { 843 state.xaccel += (xaccel - state.xaccel) * alpha; 844 state.yaccel += (yaccel - state.yaccel) * alpha; 845 } 846 state.xvel += (state.xaccel * dt) * alpha; 847 state.yvel += (state.yaccel * dt) * alpha; 848 } 849 } 850 state.xpos = xpos; 851 state.ypos = ypos; 852 } 853 854 void IntegratingVelocityTrackerStrategy::populateEstimator(const State& state, 855 VelocityTracker::Estimator* outEstimator) const { 856 outEstimator->time = state.updateTime; 857 outEstimator->confidence = 1.0f; 858 outEstimator->degree = state.degree; 859 outEstimator->xCoeff[0] = state.xpos; 860 outEstimator->xCoeff[1] = state.xvel; 861 outEstimator->xCoeff[2] = state.xaccel / 2; 862 outEstimator->yCoeff[0] = state.ypos; 863 outEstimator->yCoeff[1] = state.yvel; 864 outEstimator->yCoeff[2] = state.yaccel / 2; 865 } 866 867 868 // --- LegacyVelocityTrackerStrategy --- 869 870 LegacyVelocityTrackerStrategy::LegacyVelocityTrackerStrategy() { 871 clear(); 872 } 873 874 LegacyVelocityTrackerStrategy::~LegacyVelocityTrackerStrategy() { 875 } 876 877 void LegacyVelocityTrackerStrategy::clear() { 878 mIndex = 0; 879 mMovements[0].idBits.clear(); 880 } 881 882 void LegacyVelocityTrackerStrategy::clearPointers(BitSet32 idBits) { 883 BitSet32 remainingIdBits(mMovements[mIndex].idBits.value & ~idBits.value); 884 mMovements[mIndex].idBits = remainingIdBits; 885 } 886 887 void LegacyVelocityTrackerStrategy::addMovement(nsecs_t eventTime, BitSet32 idBits, 888 const VelocityTracker::Position* positions) { 889 if (++mIndex == HISTORY_SIZE) { 890 mIndex = 0; 891 } 892 893 Movement& movement = mMovements[mIndex]; 894 movement.eventTime = eventTime; 895 movement.idBits = idBits; 896 uint32_t count = idBits.count(); 897 for (uint32_t i = 0; i < count; i++) { 898 movement.positions[i] = positions[i]; 899 } 900 } 901 902 bool LegacyVelocityTrackerStrategy::getEstimator(uint32_t id, 903 VelocityTracker::Estimator* outEstimator) const { 904 outEstimator->clear(); 905 906 const Movement& newestMovement = mMovements[mIndex]; 907 if (!newestMovement.idBits.hasBit(id)) { 908 return false; // no data 909 } 910 911 // Find the oldest sample that contains the pointer and that is not older than HORIZON. 912 nsecs_t minTime = newestMovement.eventTime - HORIZON; 913 uint32_t oldestIndex = mIndex; 914 uint32_t numTouches = 1; 915 do { 916 uint32_t nextOldestIndex = (oldestIndex == 0 ? HISTORY_SIZE : oldestIndex) - 1; 917 const Movement& nextOldestMovement = mMovements[nextOldestIndex]; 918 if (!nextOldestMovement.idBits.hasBit(id) 919 || nextOldestMovement.eventTime < minTime) { 920 break; 921 } 922 oldestIndex = nextOldestIndex; 923 } while (++numTouches < HISTORY_SIZE); 924 925 // Calculate an exponentially weighted moving average of the velocity estimate 926 // at different points in time measured relative to the oldest sample. 927 // This is essentially an IIR filter. Newer samples are weighted more heavily 928 // than older samples. Samples at equal time points are weighted more or less 929 // equally. 930 // 931 // One tricky problem is that the sample data may be poorly conditioned. 932 // Sometimes samples arrive very close together in time which can cause us to 933 // overestimate the velocity at that time point. Most samples might be measured 934 // 16ms apart but some consecutive samples could be only 0.5sm apart because 935 // the hardware or driver reports them irregularly or in bursts. 936 float accumVx = 0; 937 float accumVy = 0; 938 uint32_t index = oldestIndex; 939 uint32_t samplesUsed = 0; 940 const Movement& oldestMovement = mMovements[oldestIndex]; 941 const VelocityTracker::Position& oldestPosition = oldestMovement.getPosition(id); 942 nsecs_t lastDuration = 0; 943 944 while (numTouches-- > 1) { 945 if (++index == HISTORY_SIZE) { 946 index = 0; 947 } 948 const Movement& movement = mMovements[index]; 949 nsecs_t duration = movement.eventTime - oldestMovement.eventTime; 950 951 // If the duration between samples is small, we may significantly overestimate 952 // the velocity. Consequently, we impose a minimum duration constraint on the 953 // samples that we include in the calculation. 954 if (duration >= MIN_DURATION) { 955 const VelocityTracker::Position& position = movement.getPosition(id); 956 float scale = 1000000000.0f / duration; // one over time delta in seconds 957 float vx = (position.x - oldestPosition.x) * scale; 958 float vy = (position.y - oldestPosition.y) * scale; 959 accumVx = (accumVx * lastDuration + vx * duration) / (duration + lastDuration); 960 accumVy = (accumVy * lastDuration + vy * duration) / (duration + lastDuration); 961 lastDuration = duration; 962 samplesUsed += 1; 963 } 964 } 965 966 // Report velocity. 967 const VelocityTracker::Position& newestPosition = newestMovement.getPosition(id); 968 outEstimator->time = newestMovement.eventTime; 969 outEstimator->confidence = 1; 970 outEstimator->xCoeff[0] = newestPosition.x; 971 outEstimator->yCoeff[0] = newestPosition.y; 972 if (samplesUsed) { 973 outEstimator->xCoeff[1] = accumVx; 974 outEstimator->yCoeff[1] = accumVy; 975 outEstimator->degree = 1; 976 } else { 977 outEstimator->degree = 0; 978 } 979 return true; 980 } 981 982 // --- ImpulseVelocityTrackerStrategy --- 983 984 ImpulseVelocityTrackerStrategy::ImpulseVelocityTrackerStrategy() { 985 clear(); 986 } 987 988 ImpulseVelocityTrackerStrategy::~ImpulseVelocityTrackerStrategy() { 989 } 990 991 void ImpulseVelocityTrackerStrategy::clear() { 992 mIndex = 0; 993 mMovements[0].idBits.clear(); 994 } 995 996 void ImpulseVelocityTrackerStrategy::clearPointers(BitSet32 idBits) { 997 BitSet32 remainingIdBits(mMovements[mIndex].idBits.value & ~idBits.value); 998 mMovements[mIndex].idBits = remainingIdBits; 999 } 1000 1001 void ImpulseVelocityTrackerStrategy::addMovement(nsecs_t eventTime, BitSet32 idBits, 1002 const VelocityTracker::Position* positions) { 1003 if (++mIndex == HISTORY_SIZE) { 1004 mIndex = 0; 1005 } 1006 1007 Movement& movement = mMovements[mIndex]; 1008 movement.eventTime = eventTime; 1009 movement.idBits = idBits; 1010 uint32_t count = idBits.count(); 1011 for (uint32_t i = 0; i < count; i++) { 1012 movement.positions[i] = positions[i]; 1013 } 1014 } 1015 1016 /** 1017 * Calculate the total impulse provided to the screen and the resulting velocity. 1018 * 1019 * The touchscreen is modeled as a physical object. 1020 * Initial condition is discussed below, but for now suppose that v(t=0) = 0 1021 * 1022 * The kinetic energy of the object at the release is E=0.5*m*v^2 1023 * Then vfinal = sqrt(2E/m). The goal is to calculate E. 1024 * 1025 * The kinetic energy at the release is equal to the total work done on the object by the finger. 1026 * The total work W is the sum of all dW along the path. 1027 * 1028 * dW = F*dx, where dx is the piece of path traveled. 1029 * Force is change of momentum over time, F = dp/dt = m dv/dt. 1030 * Then substituting: 1031 * dW = m (dv/dt) * dx = m * v * dv 1032 * 1033 * Summing along the path, we get: 1034 * W = sum(dW) = sum(m * v * dv) = m * sum(v * dv) 1035 * Since the mass stays constant, the equation for final velocity is: 1036 * vfinal = sqrt(2*sum(v * dv)) 1037 * 1038 * Here, 1039 * dv : change of velocity = (v[i+1]-v[i]) 1040 * dx : change of distance = (x[i+1]-x[i]) 1041 * dt : change of time = (t[i+1]-t[i]) 1042 * v : instantaneous velocity = dx/dt 1043 * 1044 * The final formula is: 1045 * vfinal = sqrt(2) * sqrt(sum((v[i]-v[i-1])*|v[i]|)) for all i 1046 * The absolute value is needed to properly account for the sign. If the velocity over a 1047 * particular segment descreases, then this indicates braking, which means that negative 1048 * work was done. So for two positive, but decreasing, velocities, this contribution would be 1049 * negative and will cause a smaller final velocity. 1050 * 1051 * Initial condition 1052 * There are two ways to deal with initial condition: 1053 * 1) Assume that v(0) = 0, which would mean that the screen is initially at rest. 1054 * This is not entirely accurate. We are only taking the past X ms of touch data, where X is 1055 * currently equal to 100. However, a touch event that created a fling probably lasted for longer 1056 * than that, which would mean that the user has already been interacting with the touchscreen 1057 * and it has probably already been moving. 1058 * 2) Assume that the touchscreen has already been moving at a certain velocity, calculate this 1059 * initial velocity and the equivalent energy, and start with this initial energy. 1060 * Consider an example where we have the following data, consisting of 3 points: 1061 * time: t0, t1, t2 1062 * x : x0, x1, x2 1063 * v : 0 , v1, v2 1064 * Here is what will happen in each of these scenarios: 1065 * 1) By directly applying the formula above with the v(0) = 0 boundary condition, we will get 1066 * vfinal = sqrt(2*(|v1|*(v1-v0) + |v2|*(v2-v1))). This can be simplified since v0=0 1067 * vfinal = sqrt(2*(|v1|*v1 + |v2|*(v2-v1))) = sqrt(2*(v1^2 + |v2|*(v2 - v1))) 1068 * since velocity is a real number 1069 * 2) If we treat the screen as already moving, then it must already have an energy (per mass) 1070 * equal to 1/2*v1^2. Then the initial energy should be 1/2*v1*2, and only the second segment 1071 * will contribute to the total kinetic energy (since we can effectively consider that v0=v1). 1072 * This will give the following expression for the final velocity: 1073 * vfinal = sqrt(2*(1/2*v1^2 + |v2|*(v2-v1))) 1074 * This analysis can be generalized to an arbitrary number of samples. 1075 * 1076 * 1077 * Comparing the two equations above, we see that the only mathematical difference 1078 * is the factor of 1/2 in front of the first velocity term. 1079 * This boundary condition would allow for the "proper" calculation of the case when all of the 1080 * samples are equally spaced in time and distance, which should suggest a constant velocity. 1081 * 1082 * Note that approach 2) is sensitive to the proper ordering of the data in time, since 1083 * the boundary condition must be applied to the oldest sample to be accurate. 1084 */ 1085 static float kineticEnergyToVelocity(float work) { 1086 static constexpr float sqrt2 = 1.41421356237; 1087 return (work < 0 ? -1.0 : 1.0) * sqrtf(fabsf(work)) * sqrt2; 1088 } 1089 1090 static float calculateImpulseVelocity(const nsecs_t* t, const float* x, size_t count) { 1091 // The input should be in reversed time order (most recent sample at index i=0) 1092 // t[i] is in nanoseconds, but due to FP arithmetic, convert to seconds inside this function 1093 static constexpr float SECONDS_PER_NANO = 1E-9; 1094 1095 if (count < 2) { 1096 return 0; // if 0 or 1 points, velocity is zero 1097 } 1098 if (t[1] > t[0]) { // Algorithm will still work, but not perfectly 1099 ALOGE("Samples provided to calculateImpulseVelocity in the wrong order"); 1100 } 1101 if (count == 2) { // if 2 points, basic linear calculation 1102 if (t[1] == t[0]) { 1103 ALOGE("Events have identical time stamps t=%" PRId64 ", setting velocity = 0", t[0]); 1104 return 0; 1105 } 1106 return (x[1] - x[0]) / (SECONDS_PER_NANO * (t[1] - t[0])); 1107 } 1108 // Guaranteed to have at least 3 points here 1109 float work = 0; 1110 for (size_t i = count - 1; i > 0 ; i--) { // start with the oldest sample and go forward in time 1111 if (t[i] == t[i-1]) { 1112 ALOGE("Events have identical time stamps t=%" PRId64 ", skipping sample", t[i]); 1113 continue; 1114 } 1115 float vprev = kineticEnergyToVelocity(work); // v[i-1] 1116 float vcurr = (x[i] - x[i-1]) / (SECONDS_PER_NANO * (t[i] - t[i-1])); // v[i] 1117 work += (vcurr - vprev) * fabsf(vcurr); 1118 if (i == count - 1) { 1119 work *= 0.5; // initial condition, case 2) above 1120 } 1121 } 1122 return kineticEnergyToVelocity(work); 1123 } 1124 1125 bool ImpulseVelocityTrackerStrategy::getEstimator(uint32_t id, 1126 VelocityTracker::Estimator* outEstimator) const { 1127 outEstimator->clear(); 1128 1129 // Iterate over movement samples in reverse time order and collect samples. 1130 float x[HISTORY_SIZE]; 1131 float y[HISTORY_SIZE]; 1132 nsecs_t time[HISTORY_SIZE]; 1133 size_t m = 0; // number of points that will be used for fitting 1134 size_t index = mIndex; 1135 const Movement& newestMovement = mMovements[mIndex]; 1136 do { 1137 const Movement& movement = mMovements[index]; 1138 if (!movement.idBits.hasBit(id)) { 1139 break; 1140 } 1141 1142 nsecs_t age = newestMovement.eventTime - movement.eventTime; 1143 if (age > HORIZON) { 1144 break; 1145 } 1146 1147 const VelocityTracker::Position& position = movement.getPosition(id); 1148 x[m] = position.x; 1149 y[m] = position.y; 1150 time[m] = movement.eventTime; 1151 index = (index == 0 ? HISTORY_SIZE : index) - 1; 1152 } while (++m < HISTORY_SIZE); 1153 1154 if (m == 0) { 1155 return false; // no data 1156 } 1157 outEstimator->xCoeff[0] = 0; 1158 outEstimator->yCoeff[0] = 0; 1159 outEstimator->xCoeff[1] = calculateImpulseVelocity(time, x, m); 1160 outEstimator->yCoeff[1] = calculateImpulseVelocity(time, y, m); 1161 outEstimator->xCoeff[2] = 0; 1162 outEstimator->yCoeff[2] = 0; 1163 outEstimator->time = newestMovement.eventTime; 1164 outEstimator->degree = 2; // similar results to 2nd degree fit 1165 outEstimator->confidence = 1; 1166 #if DEBUG_STRATEGY 1167 ALOGD("velocity: (%f, %f)", outEstimator->xCoeff[1], outEstimator->yCoeff[1]); 1168 #endif 1169 return true; 1170 } 1171 1172 } // namespace android 1173