blob: be7c89340b4d2d2b4eee89c3708bc1fe813aba50 [file] [log] [blame]
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -08001/*
2 * Copyright (C) 2021 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#include "StillnessDetector.h"
18
19namespace android {
20namespace media {
21
Ytai Ben-Tsvic6d7b832022-01-05 17:54:52 -080022StillnessDetector::StillnessDetector(const Options& options)
23 : mOptions(options), mCosHalfRotationalThreshold(cos(mOptions.rotationalThreshold / 2)) {}
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080024
25void StillnessDetector::reset() {
26 mFifo.clear();
27 mWindowFull = false;
Ytai Ben-Tsvif7663982022-01-06 11:26:12 -080028 mSuppressionDeadline.reset();
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080029}
30
31void StillnessDetector::setInput(int64_t timestamp, const Pose3f& input) {
32 mFifo.push_back(TimestampedPose{timestamp, input});
33 discardOld(timestamp);
34}
35
36bool StillnessDetector::calculate(int64_t timestamp) {
37 discardOld(timestamp);
38
Ytai Ben-Tsvi10721e52022-01-19 16:26:16 -080039 // Check whether all the poses in the queue are in the proximity of the new one. We want to do
40 // this before checking the overriding conditions below, in order to update the suppression
41 // deadline correctly. We always go from end to start, to find the most recent pose that
42 // violated stillness and update the suppression deadline if it has not been set or if the new
43 // one ends after the current one.
Ytai Ben-Tsvif7663982022-01-06 11:26:12 -080044 bool moved = false;
45
46 if (!mFifo.empty()) {
Ytai Ben-Tsvi10721e52022-01-19 16:26:16 -080047 for (auto iter = mFifo.rbegin() + 1; iter != mFifo.rend(); ++iter) {
Ytai Ben-Tsvif7663982022-01-06 11:26:12 -080048 const auto& event = *iter;
49 if (!areNear(event.pose, mFifo.back().pose)) {
50 // Enable suppression for the duration of the window.
Ytai Ben-Tsvi10721e52022-01-19 16:26:16 -080051 int64_t deadline = event.timestamp + mOptions.windowDuration;
52 if (!mSuppressionDeadline.has_value() || mSuppressionDeadline.value() < deadline) {
53 mSuppressionDeadline = deadline;
54 }
Ytai Ben-Tsvif7663982022-01-06 11:26:12 -080055 moved = true;
56 break;
57 }
58 }
59 }
60
Ytai Ben-Tsvi4cb1e482022-01-06 11:22:05 -080061 // If the window has not been full, return the default value.
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080062 if (!mWindowFull) {
Ytai Ben-Tsvi4cb1e482022-01-06 11:22:05 -080063 return mOptions.defaultValue;
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080064 }
65
Ytai Ben-Tsvif7663982022-01-06 11:26:12 -080066 // Force "in motion" while the suppression deadline is active.
67 if (mSuppressionDeadline.has_value()) {
68 return false;
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080069 }
70
Ytai Ben-Tsvif7663982022-01-06 11:26:12 -080071 return !moved;
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080072}
73
74void StillnessDetector::discardOld(int64_t timestamp) {
75 // Handle the special case of the window duration being zero (always considered full).
76 if (mOptions.windowDuration == 0) {
77 mFifo.clear();
78 mWindowFull = true;
79 }
80
81 // Remove any events from the queue that are older than the window. If there were any such
82 // events we consider the window full.
83 const int64_t windowStart = timestamp - mOptions.windowDuration;
84 while (!mFifo.empty() && mFifo.front().timestamp <= windowStart) {
85 mWindowFull = true;
86 mFifo.pop_front();
87 }
Ytai Ben-Tsvif7663982022-01-06 11:26:12 -080088
89 // Expire the suppression deadline.
90 if (mSuppressionDeadline.has_value() && mSuppressionDeadline <= timestamp) {
91 mSuppressionDeadline.reset();
92 }
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080093}
94
95bool StillnessDetector::areNear(const Pose3f& pose1, const Pose3f& pose2) const {
96 // Check translation. We use the L1 norm to reduce computational load on expense of accuracy.
97 // The L1 norm is an upper bound for the actual (L2) norm, so this approach will err on the side
98 // of "not near".
Ytai Ben-Tsvic6d7b832022-01-05 17:54:52 -080099 if ((pose1.translation() - pose2.translation()).lpNorm<1>() > mOptions.translationalThreshold) {
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -0800100 return false;
101 }
102
Ytai Ben-Tsvic6d7b832022-01-05 17:54:52 -0800103 // Check orientation.
104 // The angle x between the quaternions is greater than that threshold iff
105 // cos(x/2) < cos(threshold/2).
106 // cos(x/2) can be efficiently calculated as the dot product of both quaternions.
107 if (pose1.rotation().dot(pose2.rotation()) < mCosHalfRotationalThreshold) {
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -0800108 return false;
109 }
110
111 return true;
112}
113
114} // namespace media
115} // namespace android