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