blob: 523208411feec55ef42c7642f62005c6bd04ed20 [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();
Robert Daltonec0c3282022-08-26 10:33:34 +000029 // A "true" state indicates stillness is detected (default = true)
30 mCurrentState = true;
31 mPreviousState = true;
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080032}
33
34void StillnessDetector::setInput(int64_t timestamp, const Pose3f& input) {
35 mFifo.push_back(TimestampedPose{timestamp, input});
36 discardOld(timestamp);
37}
38
Robert Daltonec0c3282022-08-26 10:33:34 +000039bool StillnessDetector::getPreviousState() const {
40 return mPreviousState;
41}
42
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080043bool StillnessDetector::calculate(int64_t timestamp) {
Robert Daltonec0c3282022-08-26 10:33:34 +000044 // Move the current stillness state to the previous state.
45 // This allows us to detect transitions into and out of stillness.
46 mPreviousState = mCurrentState;
47
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080048 discardOld(timestamp);
49
Ytai Ben-Tsvi10721e52022-01-19 16:26:16 -080050 // Check whether all the poses in the queue are in the proximity of the new one. We want to do
51 // this before checking the overriding conditions below, in order to update the suppression
52 // deadline correctly. We always go from end to start, to find the most recent pose that
53 // violated stillness and update the suppression deadline if it has not been set or if the new
54 // one ends after the current one.
Ytai Ben-Tsvif7663982022-01-06 11:26:12 -080055 bool moved = false;
56
57 if (!mFifo.empty()) {
Ytai Ben-Tsvi10721e52022-01-19 16:26:16 -080058 for (auto iter = mFifo.rbegin() + 1; iter != mFifo.rend(); ++iter) {
Ytai Ben-Tsvif7663982022-01-06 11:26:12 -080059 const auto& event = *iter;
60 if (!areNear(event.pose, mFifo.back().pose)) {
61 // Enable suppression for the duration of the window.
Ytai Ben-Tsvi10721e52022-01-19 16:26:16 -080062 int64_t deadline = event.timestamp + mOptions.windowDuration;
63 if (!mSuppressionDeadline.has_value() || mSuppressionDeadline.value() < deadline) {
64 mSuppressionDeadline = deadline;
65 }
Ytai Ben-Tsvif7663982022-01-06 11:26:12 -080066 moved = true;
67 break;
68 }
69 }
70 }
71
Ytai Ben-Tsvi4cb1e482022-01-06 11:22:05 -080072 // If the window has not been full, return the default value.
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080073 if (!mWindowFull) {
Robert Daltonec0c3282022-08-26 10:33:34 +000074 mCurrentState = mOptions.defaultValue;
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080075 }
Ytai Ben-Tsvif7663982022-01-06 11:26:12 -080076 // Force "in motion" while the suppression deadline is active.
Robert Daltonec0c3282022-08-26 10:33:34 +000077 else if (mSuppressionDeadline.has_value()) {
78 mCurrentState = false;
79 }
80 else {
81 mCurrentState = !moved;
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080082 }
83
Robert Daltonec0c3282022-08-26 10:33:34 +000084 return mCurrentState;
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080085}
86
87void StillnessDetector::discardOld(int64_t timestamp) {
88 // Handle the special case of the window duration being zero (always considered full).
89 if (mOptions.windowDuration == 0) {
90 mFifo.clear();
91 mWindowFull = true;
92 }
93
94 // Remove any events from the queue that are older than the window. If there were any such
95 // events we consider the window full.
96 const int64_t windowStart = timestamp - mOptions.windowDuration;
97 while (!mFifo.empty() && mFifo.front().timestamp <= windowStart) {
98 mWindowFull = true;
99 mFifo.pop_front();
100 }
Ytai Ben-Tsvif7663982022-01-06 11:26:12 -0800101
102 // Expire the suppression deadline.
103 if (mSuppressionDeadline.has_value() && mSuppressionDeadline <= timestamp) {
104 mSuppressionDeadline.reset();
105 }
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -0800106}
107
108bool StillnessDetector::areNear(const Pose3f& pose1, const Pose3f& pose2) const {
109 // Check translation. We use the L1 norm to reduce computational load on expense of accuracy.
110 // The L1 norm is an upper bound for the actual (L2) norm, so this approach will err on the side
111 // of "not near".
Ytai Ben-Tsvic6d7b832022-01-05 17:54:52 -0800112 if ((pose1.translation() - pose2.translation()).lpNorm<1>() > mOptions.translationalThreshold) {
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -0800113 return false;
114 }
115
Ytai Ben-Tsvic6d7b832022-01-05 17:54:52 -0800116 // Check orientation.
117 // The angle x between the quaternions is greater than that threshold iff
118 // cos(x/2) < cos(threshold/2).
119 // cos(x/2) can be efficiently calculated as the dot product of both quaternions.
120 if (pose1.rotation().dot(pose2.rotation()) < mCosHalfRotationalThreshold) {
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -0800121 return false;
122 }
123
124 return true;
125}
126
127} // namespace media
128} // namespace android