blob: 5fa4e3a817e0211db8339478d77208bfc4bf196e [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;
28}
29
30void StillnessDetector::setInput(int64_t timestamp, const Pose3f& input) {
31 mFifo.push_back(TimestampedPose{timestamp, input});
32 discardOld(timestamp);
33}
34
35bool StillnessDetector::calculate(int64_t timestamp) {
36 discardOld(timestamp);
37
38 // If the window has not been full, we don't consider ourselves still.
39 if (!mWindowFull) {
40 return false;
41 }
42
43 // An empty FIFO and window full is considered still (this will happen in the unlikely case when
44 // the window duration is shorter than the gap between samples).
45 if (mFifo.empty()) {
46 return true;
47 }
48
49 // Otherwise, check whether all the poses remaining in the queue are in the proximity of the new
50 // one.
51 for (auto iter = mFifo.begin(); iter != mFifo.end() - 1; ++iter) {
52 const auto& event = *iter;
53 if (!areNear(event.pose, mFifo.back().pose)) {
54 return false;
55 }
56 }
57
58 return true;
59}
60
61void StillnessDetector::discardOld(int64_t timestamp) {
62 // Handle the special case of the window duration being zero (always considered full).
63 if (mOptions.windowDuration == 0) {
64 mFifo.clear();
65 mWindowFull = true;
66 }
67
68 // Remove any events from the queue that are older than the window. If there were any such
69 // events we consider the window full.
70 const int64_t windowStart = timestamp - mOptions.windowDuration;
71 while (!mFifo.empty() && mFifo.front().timestamp <= windowStart) {
72 mWindowFull = true;
73 mFifo.pop_front();
74 }
75}
76
77bool StillnessDetector::areNear(const Pose3f& pose1, const Pose3f& pose2) const {
78 // Check translation. We use the L1 norm to reduce computational load on expense of accuracy.
79 // The L1 norm is an upper bound for the actual (L2) norm, so this approach will err on the side
80 // of "not near".
Ytai Ben-Tsvic6d7b832022-01-05 17:54:52 -080081 if ((pose1.translation() - pose2.translation()).lpNorm<1>() > mOptions.translationalThreshold) {
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080082 return false;
83 }
84
Ytai Ben-Tsvic6d7b832022-01-05 17:54:52 -080085 // Check orientation.
86 // The angle x between the quaternions is greater than that threshold iff
87 // cos(x/2) < cos(threshold/2).
88 // cos(x/2) can be efficiently calculated as the dot product of both quaternions.
89 if (pose1.rotation().dot(pose2.rotation()) < mCosHalfRotationalThreshold) {
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080090 return false;
91 }
92
93 return true;
94}
95
96} // namespace media
97} // namespace android