blob: 6f88a3860b93ee5e0e174152ce6c088e7a119e6f [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "VelocityTracker"
Jeff Brown5912f952013-07-01 19:10:31 -070018
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -070019#include <array>
Siarhei Vishniakou7b9d1892017-07-05 18:58:41 -070020#include <inttypes.h>
Jeff Brown5912f952013-07-01 19:10:31 -070021#include <limits.h>
Siarhei Vishniakou7b9d1892017-07-05 18:58:41 -070022#include <math.h>
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -070023#include <optional>
Jeff Brown5912f952013-07-01 19:10:31 -070024
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070025#include <android-base/stringprintf.h>
Jeff Brown5912f952013-07-01 19:10:31 -070026#include <input/VelocityTracker.h>
27#include <utils/BitSet.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028#include <utils/Timers.h>
29
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +000030using std::literals::chrono_literals::operator""ms;
31
Jeff Brown5912f952013-07-01 19:10:31 -070032namespace android {
33
Siarhei Vishniakou276467b2022-03-17 09:43:28 -070034/**
35 * Log debug messages about velocity tracking.
36 * Enable this via "adb shell setprop log.tag.VelocityTrackerVelocity DEBUG" (requires restart)
37 */
38const bool DEBUG_VELOCITY =
39 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Velocity", ANDROID_LOG_INFO);
40
41/**
42 * Log debug messages about the progress of the algorithm itself.
43 * Enable this via "adb shell setprop log.tag.VelocityTrackerStrategy DEBUG" (requires restart)
44 */
45const bool DEBUG_STRATEGY =
46 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Strategy", ANDROID_LOG_INFO);
47
48/**
49 * Log debug messages about the 'impulse' strategy.
50 * Enable this via "adb shell setprop log.tag.VelocityTrackerImpulse DEBUG" (requires restart)
51 */
52const bool DEBUG_IMPULSE =
53 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Impulse", ANDROID_LOG_INFO);
54
Jeff Brown5912f952013-07-01 19:10:31 -070055// Nanoseconds per milliseconds.
56static const nsecs_t NANOS_PER_MS = 1000000;
57
58// Threshold for determining that a pointer has stopped moving.
59// Some input devices do not send ACTION_MOVE events in the case where a pointer has
60// stopped. We need to detect this case so that we can accurately predict the
61// velocity after the pointer starts moving again.
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +000062static const std::chrono::duration ASSUME_POINTER_STOPPED_TIME = 40ms;
Jeff Brown5912f952013-07-01 19:10:31 -070063
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +000064static std::string toString(std::chrono::nanoseconds t) {
65 std::stringstream stream;
66 stream.precision(1);
67 stream << std::fixed << std::chrono::duration<float, std::milli>(t).count() << " ms";
68 return stream.str();
69}
Jeff Brown5912f952013-07-01 19:10:31 -070070
71static float vectorDot(const float* a, const float* b, uint32_t m) {
72 float r = 0;
Siarhei Vishniakou7b9d1892017-07-05 18:58:41 -070073 for (size_t i = 0; i < m; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -070074 r += *(a++) * *(b++);
75 }
76 return r;
77}
78
79static float vectorNorm(const float* a, uint32_t m) {
80 float r = 0;
Siarhei Vishniakou7b9d1892017-07-05 18:58:41 -070081 for (size_t i = 0; i < m; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -070082 float t = *(a++);
83 r += t * t;
84 }
85 return sqrtf(r);
86}
87
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070088static std::string vectorToString(const float* a, uint32_t m) {
89 std::string str;
90 str += "[";
Siarhei Vishniakou7b9d1892017-07-05 18:58:41 -070091 for (size_t i = 0; i < m; i++) {
92 if (i) {
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070093 str += ",";
Jeff Brown5912f952013-07-01 19:10:31 -070094 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070095 str += android::base::StringPrintf(" %f", *(a++));
Jeff Brown5912f952013-07-01 19:10:31 -070096 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -070097 str += " ]";
Jeff Brown5912f952013-07-01 19:10:31 -070098 return str;
99}
100
Siarhei Vishniakoue37bcec2021-09-28 14:24:32 -0700101static std::string vectorToString(const std::vector<float>& v) {
102 return vectorToString(v.data(), v.size());
103}
104
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700105static std::string matrixToString(const float* a, uint32_t m, uint32_t n, bool rowMajor) {
106 std::string str;
107 str = "[";
Jeff Brown5912f952013-07-01 19:10:31 -0700108 for (size_t i = 0; i < m; i++) {
109 if (i) {
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700110 str += ",";
Jeff Brown5912f952013-07-01 19:10:31 -0700111 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700112 str += " [";
Jeff Brown5912f952013-07-01 19:10:31 -0700113 for (size_t j = 0; j < n; j++) {
114 if (j) {
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700115 str += ",";
Jeff Brown5912f952013-07-01 19:10:31 -0700116 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700117 str += android::base::StringPrintf(" %f", a[rowMajor ? i * n + j : j * m + i]);
Jeff Brown5912f952013-07-01 19:10:31 -0700118 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700119 str += " ]";
Jeff Brown5912f952013-07-01 19:10:31 -0700120 }
Siarhei Vishniakouec2727e2017-07-06 10:22:03 -0700121 str += " ]";
Jeff Brown5912f952013-07-01 19:10:31 -0700122 return str;
123}
Jeff Brown5912f952013-07-01 19:10:31 -0700124
125
126// --- VelocityTracker ---
127
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000128const std::set<int32_t> VelocityTracker::SUPPORTED_AXES = {AMOTION_EVENT_AXIS_X,
129 AMOTION_EVENT_AXIS_Y};
130
131const std::set<int32_t> VelocityTracker::PLANAR_AXES = {AMOTION_EVENT_AXIS_X, AMOTION_EVENT_AXIS_Y};
132
Chris Yef8591482020-04-17 11:49:17 -0700133VelocityTracker::VelocityTracker(const Strategy strategy)
134 : mLastEventTime(0), mCurrentPointerIdBits(0), mActivePointerId(-1) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000135 // Configure the strategy for each axis.
136 for (int32_t axis : SUPPORTED_AXES) {
137 configureStrategy(axis, strategy);
Jeff Brown5912f952013-07-01 19:10:31 -0700138 }
139}
140
141VelocityTracker::~VelocityTracker() {
Jeff Brown5912f952013-07-01 19:10:31 -0700142}
143
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000144void VelocityTracker::configureStrategy(int32_t axis, const Strategy strategy) {
145 std::unique_ptr<VelocityTrackerStrategy> createdStrategy;
146
Chris Yef8591482020-04-17 11:49:17 -0700147 if (strategy == VelocityTracker::Strategy::DEFAULT) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000148 createdStrategy = createStrategy(VelocityTracker::DEFAULT_STRATEGY);
Chris Yef8591482020-04-17 11:49:17 -0700149 } else {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000150 createdStrategy = createStrategy(strategy);
Chris Yef8591482020-04-17 11:49:17 -0700151 }
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000152
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 Brown5912f952013-07-01 19:10:31 -0700161}
162
Chris Yef8591482020-04-17 11:49:17 -0700163std::unique_ptr<VelocityTrackerStrategy> VelocityTracker::createStrategy(
164 VelocityTracker::Strategy strategy) {
165 switch (strategy) {
166 case VelocityTracker::Strategy::IMPULSE:
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000167 ALOGI_IF(DEBUG_STRATEGY, "Initializing impulse strategy");
Chris Yef8591482020-04-17 11:49:17 -0700168 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 Vishniakou9f26fc32022-06-17 22:13:57 +0000174 ALOGI_IF(DEBUG_STRATEGY && !DEBUG_IMPULSE, "Initializing lsq2 strategy");
Chris Yef8591482020-04-17 11:49:17 -0700175 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 Brown5912f952013-07-01 19:10:31 -0700207 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700208 return nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700209}
210
211void VelocityTracker::clear() {
212 mCurrentPointerIdBits.clear();
213 mActivePointerId = -1;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000214 for (int32_t axis : SUPPORTED_AXES) {
215 mStrategies[axis]->clear();
216 }
Jeff Brown5912f952013-07-01 19:10:31 -0700217}
218
219void 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 Wubshit384ab0f2022-09-09 16:39:18 +0000227 for (int32_t axis : SUPPORTED_AXES) {
228 mStrategies[axis]->clearPointers(idBits);
229 }
Jeff Brown5912f952013-07-01 19:10:31 -0700230}
231
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500232void VelocityTracker::addMovement(nsecs_t eventTime, BitSet32 idBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000233 const std::map<int32_t /*axis*/, std::vector<float>>& positions) {
Jeff Brown5912f952013-07-01 19:10:31 -0700234 while (idBits.count() > MAX_POINTERS) {
235 idBits.clearLastMarkedBit();
236 }
237
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000238 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 Brown5912f952013-07-01 19:10:31 -0700243 // We have not received any movements for too long. Assume that all pointers
244 // have stopped.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000245 for (const auto& [_, strategy] : mStrategies) {
246 strategy->clear();
247 }
Jeff Brown5912f952013-07-01 19:10:31 -0700248 }
249 mLastEventTime = eventTime;
250
251 mCurrentPointerIdBits = idBits;
252 if (mActivePointerId < 0 || !idBits.hasBit(mActivePointerId)) {
253 mActivePointerId = idBits.isEmpty() ? -1 : idBits.firstMarkedBit();
254 }
255
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000256 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 Brown5912f952013-07-01 19:10:31 -0700262
Siarhei Vishniakoue37bcec2021-09-28 14:24:32 -0700263 if (DEBUG_VELOCITY) {
264 ALOGD("VelocityTracker: addMovement eventTime=%" PRId64
265 ", idBits=0x%08x, activePointerId=%d",
266 eventTime, idBits.value, mActivePointerId);
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000267 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 Vishniakoue37bcec2021-09-28 14:24:32 -0700280 }
Jeff Brown5912f952013-07-01 19:10:31 -0700281 }
Jeff Brown5912f952013-07-01 19:10:31 -0700282}
283
284void VelocityTracker::addMovement(const MotionEvent* event) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000285 // Stores data about which axes to process based on the incoming motion event.
286 std::set<int32_t> axesToProcess;
Jeff Brown5912f952013-07-01 19:10:31 -0700287 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 Wubshit384ab0f2022-09-09 16:39:18 +0000294 for (int32_t axis : PLANAR_AXES) {
295 axesToProcess.insert(axis);
296 }
Jeff Brown5912f952013-07-01 19:10:31 -0700297 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 Wubshit384ab0f2022-09-09 16:39:18 +0000305 for (int32_t axis : PLANAR_AXES) {
306 axesToProcess.insert(axis);
307 }
Jeff Brown5912f952013-07-01 19:10:31 -0700308 break;
309 }
310 case AMOTION_EVENT_ACTION_MOVE:
311 case AMOTION_EVENT_ACTION_HOVER_MOVE:
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000312 for (int32_t axis : PLANAR_AXES) {
313 axesToProcess.insert(axis);
314 }
Jeff Brown5912f952013-07-01 19:10:31 -0700315 break;
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000316 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 Wubshit384ab0f2022-09-09 16:39:18 +0000325 for (int32_t axis : PLANAR_AXES) {
326 mStrategies[axis]->clear();
327 }
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000328 }
329 // These actions because they do not convey any new information about
Jeff Brown5912f952013-07-01 19:10:31 -0700330 // 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 Vishniakou9f26fc32022-06-17 22:13:57 +0000339 default:
340 // Ignore all other actions.
341 return;
342 }
Jeff Brown5912f952013-07-01 19:10:31 -0700343
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 Wubshit384ab0f2022-09-09 16:39:18 +0000359 std::map<int32_t, std::vector<float>> positions;
360 for (int32_t axis : axesToProcess) {
361 positions[axis].resize(pointerCount);
362 }
Jeff Brown5912f952013-07-01 19:10:31 -0700363
364 size_t historySize = event->getHistorySize();
Siarhei Vishniakou69e4d0f2020-09-14 19:53:29 -0500365 for (size_t h = 0; h <= historySize; h++) {
366 nsecs_t eventTime = event->getHistoricalEventTime(h);
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000367 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 Brown5912f952013-07-01 19:10:31 -0700371 }
372 addMovement(eventTime, idBits, positions);
373 }
Jeff Brown5912f952013-07-01 19:10:31 -0700374}
375
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000376std::optional<float> VelocityTracker::getVelocity(int32_t axis, uint32_t id) const {
Jeff Brown5912f952013-07-01 19:10:31 -0700377 Estimator estimator;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000378 bool validVelocity = getEstimator(axis, id, &estimator) && estimator.degree >= 1;
379 if (validVelocity) {
380 return estimator.coeff[1];
Jeff Brown5912f952013-07-01 19:10:31 -0700381 }
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000382 return {};
Jeff Brown5912f952013-07-01 19:10:31 -0700383}
384
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000385VelocityTracker::ComputedVelocity VelocityTracker::getComputedVelocity(int32_t units,
386 float maxVelocity) {
387 ComputedVelocity computedVelocity;
388 for (int32_t axis : SUPPORTED_AXES) {
389 BitSet32 copyIdBits = BitSet32(mCurrentPointerIdBits);
390 while (!copyIdBits.isEmpty()) {
391 uint32_t id = copyIdBits.clearFirstMarkedBit();
392 std::optional<float> velocity = getVelocity(axis, id);
393 if (velocity) {
394 float adjustedVelocity =
395 std::clamp(*velocity * units / 1000, -maxVelocity, maxVelocity);
396 computedVelocity.addVelocity(axis, id, adjustedVelocity);
397 }
398 }
399 }
400 return computedVelocity;
Jeff Brown5912f952013-07-01 19:10:31 -0700401}
402
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000403bool VelocityTracker::getEstimator(int32_t axis, uint32_t id, Estimator* outEstimator) const {
404 if (SUPPORTED_AXES.find(axis) == SUPPORTED_AXES.end()) {
405 return false;
406 }
407 return mStrategies.at(axis)->getEstimator(id, outEstimator);
408}
Jeff Brown5912f952013-07-01 19:10:31 -0700409
410// --- LeastSquaresVelocityTrackerStrategy ---
411
Jeff Brown5912f952013-07-01 19:10:31 -0700412LeastSquaresVelocityTrackerStrategy::LeastSquaresVelocityTrackerStrategy(
413 uint32_t degree, Weighting weighting) :
414 mDegree(degree), mWeighting(weighting) {
415 clear();
416}
417
418LeastSquaresVelocityTrackerStrategy::~LeastSquaresVelocityTrackerStrategy() {
419}
420
421void LeastSquaresVelocityTrackerStrategy::clear() {
422 mIndex = 0;
423 mMovements[0].idBits.clear();
424}
425
426void LeastSquaresVelocityTrackerStrategy::clearPointers(BitSet32 idBits) {
427 BitSet32 remainingIdBits(mMovements[mIndex].idBits.value & ~idBits.value);
428 mMovements[mIndex].idBits = remainingIdBits;
429}
430
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000431void LeastSquaresVelocityTrackerStrategy::addMovement(nsecs_t eventTime, BitSet32 idBits,
432 const std::vector<float>& positions) {
Siarhei Vishniakou346ac6a2019-04-10 09:58:05 -0700433 if (mMovements[mIndex].eventTime != eventTime) {
434 // When ACTION_POINTER_DOWN happens, we will first receive ACTION_MOVE with the coordinates
435 // of the existing pointers, and then ACTION_POINTER_DOWN with the coordinates that include
436 // the new pointer. If the eventtimes for both events are identical, just update the data
437 // for this time.
438 // We only compare against the last value, as it is likely that addMovement is called
439 // in chronological order as events occur.
440 mIndex++;
441 }
442 if (mIndex == HISTORY_SIZE) {
Jeff Brown5912f952013-07-01 19:10:31 -0700443 mIndex = 0;
444 }
445
446 Movement& movement = mMovements[mIndex];
447 movement.eventTime = eventTime;
448 movement.idBits = idBits;
449 uint32_t count = idBits.count();
450 for (uint32_t i = 0; i < count; i++) {
451 movement.positions[i] = positions[i];
452 }
453}
454
455/**
456 * Solves a linear least squares problem to obtain a N degree polynomial that fits
457 * the specified input data as nearly as possible.
458 *
459 * Returns true if a solution is found, false otherwise.
460 *
461 * The input consists of two vectors of data points X and Y with indices 0..m-1
462 * along with a weight vector W of the same size.
463 *
464 * The output is a vector B with indices 0..n that describes a polynomial
465 * that fits the data, such the sum of W[i] * W[i] * abs(Y[i] - (B[0] + B[1] X[i]
466 * + B[2] X[i]^2 ... B[n] X[i]^n)) for all i between 0 and m-1 is minimized.
467 *
468 * Accordingly, the weight vector W should be initialized by the caller with the
469 * reciprocal square root of the variance of the error in each input data point.
470 * In other words, an ideal choice for W would be W[i] = 1 / var(Y[i]) = 1 / stddev(Y[i]).
471 * The weights express the relative importance of each data point. If the weights are
472 * all 1, then the data points are considered to be of equal importance when fitting
473 * the polynomial. It is a good idea to choose weights that diminish the importance
474 * of data points that may have higher than usual error margins.
475 *
476 * Errors among data points are assumed to be independent. W is represented here
477 * as a vector although in the literature it is typically taken to be a diagonal matrix.
478 *
479 * That is to say, the function that generated the input data can be approximated
480 * by y(x) ~= B[0] + B[1] x + B[2] x^2 + ... + B[n] x^n.
481 *
482 * The coefficient of determination (R^2) is also returned to describe the goodness
483 * of fit of the model for the given data. It is a value between 0 and 1, where 1
484 * indicates perfect correspondence.
485 *
486 * This function first expands the X vector to a m by n matrix A such that
487 * A[i][0] = 1, A[i][1] = X[i], A[i][2] = X[i]^2, ..., A[i][n] = X[i]^n, then
488 * multiplies it by w[i]./
489 *
490 * Then it calculates the QR decomposition of A yielding an m by m orthonormal matrix Q
491 * and an m by n upper triangular matrix R. Because R is upper triangular (lower
492 * part is all zeroes), we can simplify the decomposition into an m by n matrix
493 * Q1 and a n by n matrix R1 such that A = Q1 R1.
494 *
495 * Finally we solve the system of linear equations given by R1 B = (Qtranspose W Y)
496 * to find B.
497 *
498 * For efficiency, we lay out A and Q column-wise in memory because we frequently
499 * operate on the column vectors. Conversely, we lay out R row-wise.
500 *
501 * http://en.wikipedia.org/wiki/Numerical_methods_for_linear_least_squares
502 * http://en.wikipedia.org/wiki/Gram-Schmidt
503 */
Siarhei Vishniakou81e8b162020-09-14 22:10:11 -0500504static bool solveLeastSquares(const std::vector<float>& x, const std::vector<float>& y,
505 const std::vector<float>& w, uint32_t n, float* outB, float* outDet) {
506 const size_t m = x.size();
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000507
508 ALOGD_IF(DEBUG_STRATEGY, "solveLeastSquares: m=%d, n=%d, x=%s, y=%s, w=%s", int(m), int(n),
509 vectorToString(x).c_str(), vectorToString(y).c_str(), vectorToString(w).c_str());
510
Siarhei Vishniakou81e8b162020-09-14 22:10:11 -0500511 LOG_ALWAYS_FATAL_IF(m != y.size() || m != w.size(), "Mismatched vector sizes");
Jeff Brown5912f952013-07-01 19:10:31 -0700512
513 // Expand the X vector to a matrix A, pre-multiplied by the weights.
514 float a[n][m]; // column-major order
515 for (uint32_t h = 0; h < m; h++) {
516 a[0][h] = w[h];
517 for (uint32_t i = 1; i < n; i++) {
518 a[i][h] = a[i - 1][h] * x[h];
519 }
520 }
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000521
522 ALOGD_IF(DEBUG_STRATEGY, " - a=%s",
523 matrixToString(&a[0][0], m, n, false /*rowMajor*/).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700524
525 // Apply the Gram-Schmidt process to A to obtain its QR decomposition.
526 float q[n][m]; // orthonormal basis, column-major order
527 float r[n][n]; // upper triangular matrix, row-major order
528 for (uint32_t j = 0; j < n; j++) {
529 for (uint32_t h = 0; h < m; h++) {
530 q[j][h] = a[j][h];
531 }
532 for (uint32_t i = 0; i < j; i++) {
533 float dot = vectorDot(&q[j][0], &q[i][0], m);
534 for (uint32_t h = 0; h < m; h++) {
535 q[j][h] -= dot * q[i][h];
536 }
537 }
538
539 float norm = vectorNorm(&q[j][0], m);
540 if (norm < 0.000001f) {
541 // vectors are linearly dependent or zero so no solution
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000542 ALOGD_IF(DEBUG_STRATEGY, " - no solution, norm=%f", norm);
Jeff Brown5912f952013-07-01 19:10:31 -0700543 return false;
544 }
545
546 float invNorm = 1.0f / norm;
547 for (uint32_t h = 0; h < m; h++) {
548 q[j][h] *= invNorm;
549 }
550 for (uint32_t i = 0; i < n; i++) {
551 r[j][i] = i < j ? 0 : vectorDot(&q[j][0], &a[i][0], m);
552 }
553 }
Siarhei Vishniakoue37bcec2021-09-28 14:24:32 -0700554 if (DEBUG_STRATEGY) {
555 ALOGD(" - q=%s", matrixToString(&q[0][0], m, n, false /*rowMajor*/).c_str());
556 ALOGD(" - r=%s", matrixToString(&r[0][0], n, n, true /*rowMajor*/).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700557
Siarhei Vishniakoue37bcec2021-09-28 14:24:32 -0700558 // calculate QR, if we factored A correctly then QR should equal A
559 float qr[n][m];
560 for (uint32_t h = 0; h < m; h++) {
561 for (uint32_t i = 0; i < n; i++) {
562 qr[i][h] = 0;
563 for (uint32_t j = 0; j < n; j++) {
564 qr[i][h] += q[j][h] * r[j][i];
565 }
Jeff Brown5912f952013-07-01 19:10:31 -0700566 }
567 }
Siarhei Vishniakoue37bcec2021-09-28 14:24:32 -0700568 ALOGD(" - qr=%s", matrixToString(&qr[0][0], m, n, false /*rowMajor*/).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700569 }
Jeff Brown5912f952013-07-01 19:10:31 -0700570
571 // Solve R B = Qt W Y to find B. This is easy because R is upper triangular.
572 // We just work from bottom-right to top-left calculating B's coefficients.
573 float wy[m];
574 for (uint32_t h = 0; h < m; h++) {
575 wy[h] = y[h] * w[h];
576 }
Dan Austin389ddba2015-09-22 14:32:03 -0700577 for (uint32_t i = n; i != 0; ) {
578 i--;
Jeff Brown5912f952013-07-01 19:10:31 -0700579 outB[i] = vectorDot(&q[i][0], wy, m);
580 for (uint32_t j = n - 1; j > i; j--) {
581 outB[i] -= r[i][j] * outB[j];
582 }
583 outB[i] /= r[i][i];
584 }
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000585
586 ALOGD_IF(DEBUG_STRATEGY, " - b=%s", vectorToString(outB, n).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700587
588 // Calculate the coefficient of determination as 1 - (SSerr / SStot) where
589 // SSerr is the residual sum of squares (variance of the error),
590 // and SStot is the total sum of squares (variance of the data) where each
591 // has been weighted.
592 float ymean = 0;
593 for (uint32_t h = 0; h < m; h++) {
594 ymean += y[h];
595 }
596 ymean /= m;
597
598 float sserr = 0;
599 float sstot = 0;
600 for (uint32_t h = 0; h < m; h++) {
601 float err = y[h] - outB[0];
602 float term = 1;
603 for (uint32_t i = 1; i < n; i++) {
604 term *= x[h];
605 err -= term * outB[i];
606 }
607 sserr += w[h] * w[h] * err * err;
608 float var = y[h] - ymean;
609 sstot += w[h] * w[h] * var * var;
610 }
611 *outDet = sstot > 0.000001f ? 1.0f - (sserr / sstot) : 1;
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000612
613 ALOGD_IF(DEBUG_STRATEGY, " - sserr=%f", sserr);
614 ALOGD_IF(DEBUG_STRATEGY, " - sstot=%f", sstot);
615 ALOGD_IF(DEBUG_STRATEGY, " - det=%f", *outDet);
616
Jeff Brown5912f952013-07-01 19:10:31 -0700617 return true;
618}
619
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100620/*
621 * Optimized unweighted second-order least squares fit. About 2x speed improvement compared to
622 * the default implementation
623 */
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700624static std::optional<std::array<float, 3>> solveUnweightedLeastSquaresDeg2(
Siarhei Vishniakou81e8b162020-09-14 22:10:11 -0500625 const std::vector<float>& x, const std::vector<float>& y) {
626 const size_t count = x.size();
627 LOG_ALWAYS_FATAL_IF(count != y.size(), "Mismatching array sizes");
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700628 // Solving y = a*x^2 + b*x + c
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100629 float sxi = 0, sxiyi = 0, syi = 0, sxi2 = 0, sxi3 = 0, sxi2yi = 0, sxi4 = 0;
630
631 for (size_t i = 0; i < count; i++) {
632 float xi = x[i];
633 float yi = y[i];
634 float xi2 = xi*xi;
635 float xi3 = xi2*xi;
636 float xi4 = xi3*xi;
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100637 float xiyi = xi*yi;
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700638 float xi2yi = xi2*yi;
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100639
640 sxi += xi;
641 sxi2 += xi2;
642 sxiyi += xiyi;
643 sxi2yi += xi2yi;
644 syi += yi;
645 sxi3 += xi3;
646 sxi4 += xi4;
647 }
648
649 float Sxx = sxi2 - sxi*sxi / count;
650 float Sxy = sxiyi - sxi*syi / count;
651 float Sxx2 = sxi3 - sxi*sxi2 / count;
652 float Sx2y = sxi2yi - sxi2*syi / count;
653 float Sx2x2 = sxi4 - sxi2*sxi2 / count;
654
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100655 float denominator = Sxx*Sx2x2 - Sxx2*Sxx2;
656 if (denominator == 0) {
657 ALOGW("division by 0 when computing velocity, Sxx=%f, Sx2x2=%f, Sxx2=%f", Sxx, Sx2x2, Sxx2);
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700658 return std::nullopt;
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100659 }
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700660 // Compute a
661 float numerator = Sx2y*Sxx - Sxy*Sxx2;
662 float a = numerator / denominator;
663
664 // Compute b
665 numerator = Sxy*Sx2x2 - Sx2y*Sxx2;
666 float b = numerator / denominator;
667
668 // Compute c
669 float c = syi/count - b * sxi/count - a * sxi2/count;
670
671 return std::make_optional(std::array<float, 3>({c, b, a}));
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100672}
673
Jeff Brown5912f952013-07-01 19:10:31 -0700674bool LeastSquaresVelocityTrackerStrategy::getEstimator(uint32_t id,
675 VelocityTracker::Estimator* outEstimator) const {
676 outEstimator->clear();
677
678 // Iterate over movement samples in reverse time order and collect samples.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000679 std::vector<float> positions;
Siarhei Vishniakou81e8b162020-09-14 22:10:11 -0500680 std::vector<float> w;
681 std::vector<float> time;
682
Jeff Brown5912f952013-07-01 19:10:31 -0700683 uint32_t index = mIndex;
684 const Movement& newestMovement = mMovements[mIndex];
685 do {
686 const Movement& movement = mMovements[index];
687 if (!movement.idBits.hasBit(id)) {
688 break;
689 }
690
691 nsecs_t age = newestMovement.eventTime - movement.eventTime;
692 if (age > HORIZON) {
693 break;
694 }
695
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000696 positions.push_back(movement.getPosition(id));
Siarhei Vishniakou81e8b162020-09-14 22:10:11 -0500697 w.push_back(chooseWeight(index));
698 time.push_back(-age * 0.000000001f);
Jeff Brown5912f952013-07-01 19:10:31 -0700699 index = (index == 0 ? HISTORY_SIZE : index) - 1;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000700 } while (positions.size() < HISTORY_SIZE);
Jeff Brown5912f952013-07-01 19:10:31 -0700701
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000702 const size_t m = positions.size();
Jeff Brown5912f952013-07-01 19:10:31 -0700703 if (m == 0) {
704 return false; // no data
705 }
706
707 // Calculate a least squares polynomial fit.
708 uint32_t degree = mDegree;
709 if (degree > m - 1) {
710 degree = m - 1;
711 }
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700712
713 if (degree == 2 && mWeighting == WEIGHTING_NONE) {
714 // Optimize unweighted, quadratic polynomial fit
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000715 std::optional<std::array<float, 3>> coeff =
716 solveUnweightedLeastSquaresDeg2(time, positions);
717 if (coeff) {
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100718 outEstimator->time = newestMovement.eventTime;
719 outEstimator->degree = 2;
720 outEstimator->confidence = 1;
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700721 for (size_t i = 0; i <= outEstimator->degree; i++) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000722 outEstimator->coeff[i] = (*coeff)[i];
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700723 }
Siarhei Vishniakou489d38e2017-06-16 17:16:25 +0100724 return true;
725 }
Siarhei Vishniakoue96bc7a2018-09-06 10:19:16 -0700726 } else if (degree >= 1) {
727 // General case for an Nth degree polynomial fit
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000728 float det;
Jeff Brown5912f952013-07-01 19:10:31 -0700729 uint32_t n = degree + 1;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000730 if (solveLeastSquares(time, positions, w, n, outEstimator->coeff, &det)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700731 outEstimator->time = newestMovement.eventTime;
732 outEstimator->degree = degree;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000733 outEstimator->confidence = det;
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000734
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000735 ALOGD_IF(DEBUG_STRATEGY, "estimate: degree=%d, coeff=%s, confidence=%f",
736 int(outEstimator->degree), vectorToString(outEstimator->coeff, n).c_str(),
737 outEstimator->confidence);
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +0000738
Jeff Brown5912f952013-07-01 19:10:31 -0700739 return true;
740 }
741 }
742
743 // No velocity data available for this pointer, but we do have its current position.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000744 outEstimator->coeff[0] = positions[0];
Jeff Brown5912f952013-07-01 19:10:31 -0700745 outEstimator->time = newestMovement.eventTime;
746 outEstimator->degree = 0;
747 outEstimator->confidence = 1;
748 return true;
749}
750
751float LeastSquaresVelocityTrackerStrategy::chooseWeight(uint32_t index) const {
752 switch (mWeighting) {
753 case WEIGHTING_DELTA: {
754 // Weight points based on how much time elapsed between them and the next
755 // point so that points that "cover" a shorter time span are weighed less.
756 // delta 0ms: 0.5
757 // delta 10ms: 1.0
758 if (index == mIndex) {
759 return 1.0f;
760 }
761 uint32_t nextIndex = (index + 1) % HISTORY_SIZE;
762 float deltaMillis = (mMovements[nextIndex].eventTime- mMovements[index].eventTime)
763 * 0.000001f;
764 if (deltaMillis < 0) {
765 return 0.5f;
766 }
767 if (deltaMillis < 10) {
768 return 0.5f + deltaMillis * 0.05;
769 }
770 return 1.0f;
771 }
772
773 case WEIGHTING_CENTRAL: {
774 // Weight points based on their age, weighing very recent and very old points less.
775 // age 0ms: 0.5
776 // age 10ms: 1.0
777 // age 50ms: 1.0
778 // age 60ms: 0.5
779 float ageMillis = (mMovements[mIndex].eventTime - mMovements[index].eventTime)
780 * 0.000001f;
781 if (ageMillis < 0) {
782 return 0.5f;
783 }
784 if (ageMillis < 10) {
785 return 0.5f + ageMillis * 0.05;
786 }
787 if (ageMillis < 50) {
788 return 1.0f;
789 }
790 if (ageMillis < 60) {
791 return 0.5f + (60 - ageMillis) * 0.05;
792 }
793 return 0.5f;
794 }
795
796 case WEIGHTING_RECENT: {
797 // Weight points based on their age, weighing older points less.
798 // age 0ms: 1.0
799 // age 50ms: 1.0
800 // age 100ms: 0.5
801 float ageMillis = (mMovements[mIndex].eventTime - mMovements[index].eventTime)
802 * 0.000001f;
803 if (ageMillis < 50) {
804 return 1.0f;
805 }
806 if (ageMillis < 100) {
807 return 0.5f + (100 - ageMillis) * 0.01f;
808 }
809 return 0.5f;
810 }
811
812 case WEIGHTING_NONE:
813 default:
814 return 1.0f;
815 }
816}
817
818
819// --- IntegratingVelocityTrackerStrategy ---
820
821IntegratingVelocityTrackerStrategy::IntegratingVelocityTrackerStrategy(uint32_t degree) :
822 mDegree(degree) {
823}
824
825IntegratingVelocityTrackerStrategy::~IntegratingVelocityTrackerStrategy() {
826}
827
828void IntegratingVelocityTrackerStrategy::clear() {
829 mPointerIdBits.clear();
830}
831
832void IntegratingVelocityTrackerStrategy::clearPointers(BitSet32 idBits) {
833 mPointerIdBits.value &= ~idBits.value;
834}
835
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000836void IntegratingVelocityTrackerStrategy::addMovement(nsecs_t eventTime, BitSet32 idBits,
837 const std::vector<float>& positions) {
Jeff Brown5912f952013-07-01 19:10:31 -0700838 uint32_t index = 0;
839 for (BitSet32 iterIdBits(idBits); !iterIdBits.isEmpty();) {
840 uint32_t id = iterIdBits.clearFirstMarkedBit();
841 State& state = mPointerState[id];
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000842 const float position = positions[index++];
Jeff Brown5912f952013-07-01 19:10:31 -0700843 if (mPointerIdBits.hasBit(id)) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000844 updateState(state, eventTime, position);
Jeff Brown5912f952013-07-01 19:10:31 -0700845 } else {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000846 initState(state, eventTime, position);
Jeff Brown5912f952013-07-01 19:10:31 -0700847 }
848 }
849
850 mPointerIdBits = idBits;
851}
852
853bool IntegratingVelocityTrackerStrategy::getEstimator(uint32_t id,
854 VelocityTracker::Estimator* outEstimator) const {
855 outEstimator->clear();
856
857 if (mPointerIdBits.hasBit(id)) {
858 const State& state = mPointerState[id];
859 populateEstimator(state, outEstimator);
860 return true;
861 }
862
863 return false;
864}
865
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000866void IntegratingVelocityTrackerStrategy::initState(State& state, nsecs_t eventTime,
867 float pos) const {
Jeff Brown5912f952013-07-01 19:10:31 -0700868 state.updateTime = eventTime;
869 state.degree = 0;
870
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000871 state.pos = pos;
872 state.accel = 0;
873 state.vel = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700874}
875
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000876void IntegratingVelocityTrackerStrategy::updateState(State& state, nsecs_t eventTime,
877 float pos) const {
Jeff Brown5912f952013-07-01 19:10:31 -0700878 const nsecs_t MIN_TIME_DELTA = 2 * NANOS_PER_MS;
879 const float FILTER_TIME_CONSTANT = 0.010f; // 10 milliseconds
880
881 if (eventTime <= state.updateTime + MIN_TIME_DELTA) {
882 return;
883 }
884
885 float dt = (eventTime - state.updateTime) * 0.000000001f;
886 state.updateTime = eventTime;
887
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000888 float vel = (pos - state.pos) / dt;
Jeff Brown5912f952013-07-01 19:10:31 -0700889 if (state.degree == 0) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000890 state.vel = vel;
Jeff Brown5912f952013-07-01 19:10:31 -0700891 state.degree = 1;
892 } else {
893 float alpha = dt / (FILTER_TIME_CONSTANT + dt);
894 if (mDegree == 1) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000895 state.vel += (vel - state.vel) * alpha;
Jeff Brown5912f952013-07-01 19:10:31 -0700896 } else {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000897 float accel = (vel - state.vel) / dt;
Jeff Brown5912f952013-07-01 19:10:31 -0700898 if (state.degree == 1) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000899 state.accel = accel;
Jeff Brown5912f952013-07-01 19:10:31 -0700900 state.degree = 2;
901 } else {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000902 state.accel += (accel - state.accel) * alpha;
Jeff Brown5912f952013-07-01 19:10:31 -0700903 }
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000904 state.vel += (state.accel * dt) * alpha;
Jeff Brown5912f952013-07-01 19:10:31 -0700905 }
906 }
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000907 state.pos = pos;
Jeff Brown5912f952013-07-01 19:10:31 -0700908}
909
910void IntegratingVelocityTrackerStrategy::populateEstimator(const State& state,
911 VelocityTracker::Estimator* outEstimator) const {
912 outEstimator->time = state.updateTime;
913 outEstimator->confidence = 1.0f;
914 outEstimator->degree = state.degree;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000915 outEstimator->coeff[0] = state.pos;
916 outEstimator->coeff[1] = state.vel;
917 outEstimator->coeff[2] = state.accel / 2;
Jeff Brown5912f952013-07-01 19:10:31 -0700918}
919
920
921// --- LegacyVelocityTrackerStrategy ---
922
Jeff Brown5912f952013-07-01 19:10:31 -0700923LegacyVelocityTrackerStrategy::LegacyVelocityTrackerStrategy() {
924 clear();
925}
926
927LegacyVelocityTrackerStrategy::~LegacyVelocityTrackerStrategy() {
928}
929
930void LegacyVelocityTrackerStrategy::clear() {
931 mIndex = 0;
932 mMovements[0].idBits.clear();
933}
934
935void LegacyVelocityTrackerStrategy::clearPointers(BitSet32 idBits) {
936 BitSet32 remainingIdBits(mMovements[mIndex].idBits.value & ~idBits.value);
937 mMovements[mIndex].idBits = remainingIdBits;
938}
939
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000940void LegacyVelocityTrackerStrategy::addMovement(nsecs_t eventTime, BitSet32 idBits,
941 const std::vector<float>& positions) {
Jeff Brown5912f952013-07-01 19:10:31 -0700942 if (++mIndex == HISTORY_SIZE) {
943 mIndex = 0;
944 }
945
946 Movement& movement = mMovements[mIndex];
947 movement.eventTime = eventTime;
948 movement.idBits = idBits;
949 uint32_t count = idBits.count();
950 for (uint32_t i = 0; i < count; i++) {
951 movement.positions[i] = positions[i];
952 }
953}
954
955bool LegacyVelocityTrackerStrategy::getEstimator(uint32_t id,
956 VelocityTracker::Estimator* outEstimator) const {
957 outEstimator->clear();
958
959 const Movement& newestMovement = mMovements[mIndex];
960 if (!newestMovement.idBits.hasBit(id)) {
961 return false; // no data
962 }
963
964 // Find the oldest sample that contains the pointer and that is not older than HORIZON.
965 nsecs_t minTime = newestMovement.eventTime - HORIZON;
966 uint32_t oldestIndex = mIndex;
967 uint32_t numTouches = 1;
968 do {
969 uint32_t nextOldestIndex = (oldestIndex == 0 ? HISTORY_SIZE : oldestIndex) - 1;
970 const Movement& nextOldestMovement = mMovements[nextOldestIndex];
971 if (!nextOldestMovement.idBits.hasBit(id)
972 || nextOldestMovement.eventTime < minTime) {
973 break;
974 }
975 oldestIndex = nextOldestIndex;
976 } while (++numTouches < HISTORY_SIZE);
977
978 // Calculate an exponentially weighted moving average of the velocity estimate
979 // at different points in time measured relative to the oldest sample.
980 // This is essentially an IIR filter. Newer samples are weighted more heavily
981 // than older samples. Samples at equal time points are weighted more or less
982 // equally.
983 //
984 // One tricky problem is that the sample data may be poorly conditioned.
985 // Sometimes samples arrive very close together in time which can cause us to
986 // overestimate the velocity at that time point. Most samples might be measured
987 // 16ms apart but some consecutive samples could be only 0.5sm apart because
988 // the hardware or driver reports them irregularly or in bursts.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000989 float accumV = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700990 uint32_t index = oldestIndex;
991 uint32_t samplesUsed = 0;
992 const Movement& oldestMovement = mMovements[oldestIndex];
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000993 float oldestPosition = oldestMovement.getPosition(id);
Jeff Brown5912f952013-07-01 19:10:31 -0700994 nsecs_t lastDuration = 0;
995
996 while (numTouches-- > 1) {
997 if (++index == HISTORY_SIZE) {
998 index = 0;
999 }
1000 const Movement& movement = mMovements[index];
1001 nsecs_t duration = movement.eventTime - oldestMovement.eventTime;
1002
1003 // If the duration between samples is small, we may significantly overestimate
1004 // the velocity. Consequently, we impose a minimum duration constraint on the
1005 // samples that we include in the calculation.
1006 if (duration >= MIN_DURATION) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00001007 float position = movement.getPosition(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001008 float scale = 1000000000.0f / duration; // one over time delta in seconds
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00001009 float v = (position - oldestPosition) * scale;
1010 accumV = (accumV * lastDuration + v * duration) / (duration + lastDuration);
Jeff Brown5912f952013-07-01 19:10:31 -07001011 lastDuration = duration;
1012 samplesUsed += 1;
1013 }
1014 }
1015
1016 // Report velocity.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00001017 float newestPosition = newestMovement.getPosition(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001018 outEstimator->time = newestMovement.eventTime;
1019 outEstimator->confidence = 1;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00001020 outEstimator->coeff[0] = newestPosition;
Jeff Brown5912f952013-07-01 19:10:31 -07001021 if (samplesUsed) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00001022 outEstimator->coeff[1] = accumV;
Jeff Brown5912f952013-07-01 19:10:31 -07001023 outEstimator->degree = 1;
1024 } else {
1025 outEstimator->degree = 0;
1026 }
1027 return true;
1028}
1029
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001030// --- ImpulseVelocityTrackerStrategy ---
1031
1032ImpulseVelocityTrackerStrategy::ImpulseVelocityTrackerStrategy() {
1033 clear();
1034}
1035
1036ImpulseVelocityTrackerStrategy::~ImpulseVelocityTrackerStrategy() {
1037}
1038
1039void ImpulseVelocityTrackerStrategy::clear() {
1040 mIndex = 0;
1041 mMovements[0].idBits.clear();
1042}
1043
1044void ImpulseVelocityTrackerStrategy::clearPointers(BitSet32 idBits) {
1045 BitSet32 remainingIdBits(mMovements[mIndex].idBits.value & ~idBits.value);
1046 mMovements[mIndex].idBits = remainingIdBits;
1047}
1048
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00001049void ImpulseVelocityTrackerStrategy::addMovement(nsecs_t eventTime, BitSet32 idBits,
1050 const std::vector<float>& positions) {
Siarhei Vishniakou346ac6a2019-04-10 09:58:05 -07001051 if (mMovements[mIndex].eventTime != eventTime) {
1052 // When ACTION_POINTER_DOWN happens, we will first receive ACTION_MOVE with the coordinates
1053 // of the existing pointers, and then ACTION_POINTER_DOWN with the coordinates that include
1054 // the new pointer. If the eventtimes for both events are identical, just update the data
1055 // for this time.
1056 // We only compare against the last value, as it is likely that addMovement is called
1057 // in chronological order as events occur.
1058 mIndex++;
1059 }
1060 if (mIndex == HISTORY_SIZE) {
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001061 mIndex = 0;
1062 }
1063
1064 Movement& movement = mMovements[mIndex];
1065 movement.eventTime = eventTime;
1066 movement.idBits = idBits;
1067 uint32_t count = idBits.count();
1068 for (uint32_t i = 0; i < count; i++) {
1069 movement.positions[i] = positions[i];
1070 }
1071}
1072
1073/**
1074 * Calculate the total impulse provided to the screen and the resulting velocity.
1075 *
1076 * The touchscreen is modeled as a physical object.
1077 * Initial condition is discussed below, but for now suppose that v(t=0) = 0
1078 *
1079 * The kinetic energy of the object at the release is E=0.5*m*v^2
1080 * Then vfinal = sqrt(2E/m). The goal is to calculate E.
1081 *
1082 * The kinetic energy at the release is equal to the total work done on the object by the finger.
1083 * The total work W is the sum of all dW along the path.
1084 *
1085 * dW = F*dx, where dx is the piece of path traveled.
1086 * Force is change of momentum over time, F = dp/dt = m dv/dt.
1087 * Then substituting:
1088 * dW = m (dv/dt) * dx = m * v * dv
1089 *
1090 * Summing along the path, we get:
1091 * W = sum(dW) = sum(m * v * dv) = m * sum(v * dv)
1092 * Since the mass stays constant, the equation for final velocity is:
1093 * vfinal = sqrt(2*sum(v * dv))
1094 *
1095 * Here,
1096 * dv : change of velocity = (v[i+1]-v[i])
1097 * dx : change of distance = (x[i+1]-x[i])
1098 * dt : change of time = (t[i+1]-t[i])
1099 * v : instantaneous velocity = dx/dt
1100 *
1101 * The final formula is:
1102 * vfinal = sqrt(2) * sqrt(sum((v[i]-v[i-1])*|v[i]|)) for all i
1103 * The absolute value is needed to properly account for the sign. If the velocity over a
1104 * particular segment descreases, then this indicates braking, which means that negative
1105 * work was done. So for two positive, but decreasing, velocities, this contribution would be
1106 * negative and will cause a smaller final velocity.
1107 *
1108 * Initial condition
1109 * There are two ways to deal with initial condition:
1110 * 1) Assume that v(0) = 0, which would mean that the screen is initially at rest.
1111 * This is not entirely accurate. We are only taking the past X ms of touch data, where X is
1112 * currently equal to 100. However, a touch event that created a fling probably lasted for longer
1113 * than that, which would mean that the user has already been interacting with the touchscreen
1114 * and it has probably already been moving.
1115 * 2) Assume that the touchscreen has already been moving at a certain velocity, calculate this
1116 * initial velocity and the equivalent energy, and start with this initial energy.
1117 * Consider an example where we have the following data, consisting of 3 points:
1118 * time: t0, t1, t2
1119 * x : x0, x1, x2
1120 * v : 0 , v1, v2
1121 * Here is what will happen in each of these scenarios:
1122 * 1) By directly applying the formula above with the v(0) = 0 boundary condition, we will get
1123 * vfinal = sqrt(2*(|v1|*(v1-v0) + |v2|*(v2-v1))). This can be simplified since v0=0
1124 * vfinal = sqrt(2*(|v1|*v1 + |v2|*(v2-v1))) = sqrt(2*(v1^2 + |v2|*(v2 - v1)))
1125 * since velocity is a real number
1126 * 2) If we treat the screen as already moving, then it must already have an energy (per mass)
1127 * equal to 1/2*v1^2. Then the initial energy should be 1/2*v1*2, and only the second segment
1128 * will contribute to the total kinetic energy (since we can effectively consider that v0=v1).
1129 * This will give the following expression for the final velocity:
1130 * vfinal = sqrt(2*(1/2*v1^2 + |v2|*(v2-v1)))
1131 * This analysis can be generalized to an arbitrary number of samples.
1132 *
1133 *
1134 * Comparing the two equations above, we see that the only mathematical difference
1135 * is the factor of 1/2 in front of the first velocity term.
1136 * This boundary condition would allow for the "proper" calculation of the case when all of the
1137 * samples are equally spaced in time and distance, which should suggest a constant velocity.
1138 *
1139 * Note that approach 2) is sensitive to the proper ordering of the data in time, since
1140 * the boundary condition must be applied to the oldest sample to be accurate.
1141 */
Siarhei Vishniakou97b5e182017-09-01 13:52:33 -07001142static float kineticEnergyToVelocity(float work) {
1143 static constexpr float sqrt2 = 1.41421356237;
1144 return (work < 0 ? -1.0 : 1.0) * sqrtf(fabsf(work)) * sqrt2;
1145}
1146
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001147static float calculateImpulseVelocity(const nsecs_t* t, const float* x, size_t count) {
1148 // The input should be in reversed time order (most recent sample at index i=0)
1149 // t[i] is in nanoseconds, but due to FP arithmetic, convert to seconds inside this function
Siarhei Vishniakou6de8f5e2018-03-02 18:48:15 -08001150 static constexpr float SECONDS_PER_NANO = 1E-9;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001151
1152 if (count < 2) {
1153 return 0; // if 0 or 1 points, velocity is zero
1154 }
1155 if (t[1] > t[0]) { // Algorithm will still work, but not perfectly
1156 ALOGE("Samples provided to calculateImpulseVelocity in the wrong order");
1157 }
1158 if (count == 2) { // if 2 points, basic linear calculation
1159 if (t[1] == t[0]) {
1160 ALOGE("Events have identical time stamps t=%" PRId64 ", setting velocity = 0", t[0]);
1161 return 0;
1162 }
Siarhei Vishniakou6de8f5e2018-03-02 18:48:15 -08001163 return (x[1] - x[0]) / (SECONDS_PER_NANO * (t[1] - t[0]));
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001164 }
1165 // Guaranteed to have at least 3 points here
1166 float work = 0;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001167 for (size_t i = count - 1; i > 0 ; i--) { // start with the oldest sample and go forward in time
1168 if (t[i] == t[i-1]) {
1169 ALOGE("Events have identical time stamps t=%" PRId64 ", skipping sample", t[i]);
1170 continue;
1171 }
Siarhei Vishniakou97b5e182017-09-01 13:52:33 -07001172 float vprev = kineticEnergyToVelocity(work); // v[i-1]
Siarhei Vishniakou6de8f5e2018-03-02 18:48:15 -08001173 float vcurr = (x[i] - x[i-1]) / (SECONDS_PER_NANO * (t[i] - t[i-1])); // v[i]
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001174 work += (vcurr - vprev) * fabsf(vcurr);
1175 if (i == count - 1) {
1176 work *= 0.5; // initial condition, case 2) above
1177 }
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001178 }
Siarhei Vishniakou97b5e182017-09-01 13:52:33 -07001179 return kineticEnergyToVelocity(work);
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001180}
1181
1182bool ImpulseVelocityTrackerStrategy::getEstimator(uint32_t id,
1183 VelocityTracker::Estimator* outEstimator) const {
1184 outEstimator->clear();
1185
1186 // Iterate over movement samples in reverse time order and collect samples.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00001187 float positions[HISTORY_SIZE];
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001188 nsecs_t time[HISTORY_SIZE];
1189 size_t m = 0; // number of points that will be used for fitting
1190 size_t index = mIndex;
1191 const Movement& newestMovement = mMovements[mIndex];
1192 do {
1193 const Movement& movement = mMovements[index];
1194 if (!movement.idBits.hasBit(id)) {
1195 break;
1196 }
1197
1198 nsecs_t age = newestMovement.eventTime - movement.eventTime;
1199 if (age > HORIZON) {
1200 break;
1201 }
1202
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00001203 positions[m] = movement.getPosition(id);
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001204 time[m] = movement.eventTime;
1205 index = (index == 0 ? HISTORY_SIZE : index) - 1;
1206 } while (++m < HISTORY_SIZE);
1207
1208 if (m == 0) {
1209 return false; // no data
1210 }
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00001211 outEstimator->coeff[0] = 0;
1212 outEstimator->coeff[1] = calculateImpulseVelocity(time, positions, m);
1213 outEstimator->coeff[2] = 0;
1214
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001215 outEstimator->time = newestMovement.eventTime;
1216 outEstimator->degree = 2; // similar results to 2nd degree fit
1217 outEstimator->confidence = 1;
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001218
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00001219 ALOGD_IF(DEBUG_STRATEGY, "velocity: %.1f", outEstimator->coeff[1]);
Siarhei Vishniakou9f26fc32022-06-17 22:13:57 +00001220
Siarhei Vishniakou276467b2022-03-17 09:43:28 -07001221 if (DEBUG_IMPULSE) {
1222 // TODO(b/134179997): delete this block once the switch to 'impulse' is complete.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00001223 // Calculate the lsq2 velocity for the same inputs to allow runtime comparisons.
1224 // X axis chosen arbitrarily for velocity comparisons.
Siarhei Vishniakou276467b2022-03-17 09:43:28 -07001225 VelocityTracker lsq2(VelocityTracker::Strategy::LSQ2);
1226 BitSet32 idBits;
1227 const uint32_t pointerId = 0;
1228 idBits.markBit(pointerId);
1229 for (ssize_t i = m - 1; i >= 0; i--) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00001230 lsq2.addMovement(time[i], idBits, {{AMOTION_EVENT_AXIS_X, {positions[i]}}});
Siarhei Vishniakou276467b2022-03-17 09:43:28 -07001231 }
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +00001232 std::optional<float> v = lsq2.getVelocity(AMOTION_EVENT_AXIS_X, pointerId);
1233 if (v) {
1234 ALOGD("lsq2 velocity: %.1f", *v);
Siarhei Vishniakou276467b2022-03-17 09:43:28 -07001235 } else {
1236 ALOGD("lsq2 velocity: could not compute velocity");
1237 }
Siarhei Vishniakoue37bcec2021-09-28 14:24:32 -07001238 }
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +01001239 return true;
1240}
1241
Jeff Brown5912f952013-07-01 19:10:31 -07001242} // namespace android