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