blob: 9806352ac8b42239d9f969c1b00bb24788acff3d [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-Tsvif7663982022-01-06 11:26:12 -080039 // Check whether all the poses in the queue are in the proximity of the new
40 // one. We want to do this before checking the overriding conditions below, in order to update
41 // the suppression deadline correctly.
42 bool moved = false;
43
44 if (!mFifo.empty()) {
45 for (auto iter = mFifo.begin(); iter != mFifo.end() - 1; ++iter) {
46 const auto& event = *iter;
47 if (!areNear(event.pose, mFifo.back().pose)) {
48 // Enable suppression for the duration of the window.
49 mSuppressionDeadline = timestamp + mOptions.windowDuration;
50 moved = true;
51 break;
52 }
53 }
54 }
55
Ytai Ben-Tsvi4cb1e482022-01-06 11:22:05 -080056 // If the window has not been full, return the default value.
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080057 if (!mWindowFull) {
Ytai Ben-Tsvi4cb1e482022-01-06 11:22:05 -080058 return mOptions.defaultValue;
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080059 }
60
Ytai Ben-Tsvif7663982022-01-06 11:26:12 -080061 // Force "in motion" while the suppression deadline is active.
62 if (mSuppressionDeadline.has_value()) {
63 return false;
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080064 }
65
Ytai Ben-Tsvif7663982022-01-06 11:26:12 -080066 return !moved;
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080067}
68
69void StillnessDetector::discardOld(int64_t timestamp) {
70 // Handle the special case of the window duration being zero (always considered full).
71 if (mOptions.windowDuration == 0) {
72 mFifo.clear();
73 mWindowFull = true;
74 }
75
76 // Remove any events from the queue that are older than the window. If there were any such
77 // events we consider the window full.
78 const int64_t windowStart = timestamp - mOptions.windowDuration;
79 while (!mFifo.empty() && mFifo.front().timestamp <= windowStart) {
80 mWindowFull = true;
81 mFifo.pop_front();
82 }
Ytai Ben-Tsvif7663982022-01-06 11:26:12 -080083
84 // Expire the suppression deadline.
85 if (mSuppressionDeadline.has_value() && mSuppressionDeadline <= timestamp) {
86 mSuppressionDeadline.reset();
87 }
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080088}
89
90bool StillnessDetector::areNear(const Pose3f& pose1, const Pose3f& pose2) const {
91 // Check translation. We use the L1 norm to reduce computational load on expense of accuracy.
92 // The L1 norm is an upper bound for the actual (L2) norm, so this approach will err on the side
93 // of "not near".
Ytai Ben-Tsvic6d7b832022-01-05 17:54:52 -080094 if ((pose1.translation() - pose2.translation()).lpNorm<1>() > mOptions.translationalThreshold) {
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080095 return false;
96 }
97
Ytai Ben-Tsvic6d7b832022-01-05 17:54:52 -080098 // Check orientation.
99 // The angle x between the quaternions is greater than that threshold iff
100 // cos(x/2) < cos(threshold/2).
101 // cos(x/2) can be efficiently calculated as the dot product of both quaternions.
102 if (pose1.rotation().dot(pose2.rotation()) < mCosHalfRotationalThreshold) {
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -0800103 return false;
104 }
105
106 return true;
107}
108
109} // namespace media
110} // namespace android