1*ec779b8eSAndroid Build Coastguard Worker /*
2*ec779b8eSAndroid Build Coastguard Worker * Copyright (C) 2021 The Android Open Source Project
3*ec779b8eSAndroid Build Coastguard Worker *
4*ec779b8eSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*ec779b8eSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*ec779b8eSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*ec779b8eSAndroid Build Coastguard Worker *
8*ec779b8eSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*ec779b8eSAndroid Build Coastguard Worker *
10*ec779b8eSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*ec779b8eSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*ec779b8eSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*ec779b8eSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*ec779b8eSAndroid Build Coastguard Worker * limitations under the License.
15*ec779b8eSAndroid Build Coastguard Worker */
16*ec779b8eSAndroid Build Coastguard Worker
17*ec779b8eSAndroid Build Coastguard Worker #include "StillnessDetector.h"
18*ec779b8eSAndroid Build Coastguard Worker
19*ec779b8eSAndroid Build Coastguard Worker namespace android {
20*ec779b8eSAndroid Build Coastguard Worker namespace media {
21*ec779b8eSAndroid Build Coastguard Worker
StillnessDetector(const Options & options)22*ec779b8eSAndroid Build Coastguard Worker StillnessDetector::StillnessDetector(const Options& options)
23*ec779b8eSAndroid Build Coastguard Worker : mOptions(options), mCosHalfRotationalThreshold(cos(mOptions.rotationalThreshold / 2)) {}
24*ec779b8eSAndroid Build Coastguard Worker
reset()25*ec779b8eSAndroid Build Coastguard Worker void StillnessDetector::reset() {
26*ec779b8eSAndroid Build Coastguard Worker mFifo.clear();
27*ec779b8eSAndroid Build Coastguard Worker mWindowFull = false;
28*ec779b8eSAndroid Build Coastguard Worker mSuppressionDeadline.reset();
29*ec779b8eSAndroid Build Coastguard Worker // A "true" state indicates stillness is detected (default = true)
30*ec779b8eSAndroid Build Coastguard Worker mCurrentState = true;
31*ec779b8eSAndroid Build Coastguard Worker mPreviousState = true;
32*ec779b8eSAndroid Build Coastguard Worker }
33*ec779b8eSAndroid Build Coastguard Worker
setInput(int64_t timestamp,const Pose3f & input)34*ec779b8eSAndroid Build Coastguard Worker void StillnessDetector::setInput(int64_t timestamp, const Pose3f& input) {
35*ec779b8eSAndroid Build Coastguard Worker mFifo.push_back(TimestampedPose{timestamp, input});
36*ec779b8eSAndroid Build Coastguard Worker discardOld(timestamp);
37*ec779b8eSAndroid Build Coastguard Worker }
38*ec779b8eSAndroid Build Coastguard Worker
getPreviousState() const39*ec779b8eSAndroid Build Coastguard Worker bool StillnessDetector::getPreviousState() const {
40*ec779b8eSAndroid Build Coastguard Worker return mPreviousState;
41*ec779b8eSAndroid Build Coastguard Worker }
42*ec779b8eSAndroid Build Coastguard Worker
calculate(int64_t timestamp)43*ec779b8eSAndroid Build Coastguard Worker bool StillnessDetector::calculate(int64_t timestamp) {
44*ec779b8eSAndroid Build Coastguard Worker // Move the current stillness state to the previous state.
45*ec779b8eSAndroid Build Coastguard Worker // This allows us to detect transitions into and out of stillness.
46*ec779b8eSAndroid Build Coastguard Worker mPreviousState = mCurrentState;
47*ec779b8eSAndroid Build Coastguard Worker
48*ec779b8eSAndroid Build Coastguard Worker discardOld(timestamp);
49*ec779b8eSAndroid Build Coastguard Worker
50*ec779b8eSAndroid Build Coastguard Worker // Check whether all the poses in the queue are in the proximity of the new one. We want to do
51*ec779b8eSAndroid Build Coastguard Worker // this before checking the overriding conditions below, in order to update the suppression
52*ec779b8eSAndroid Build Coastguard Worker // deadline correctly. We always go from end to start, to find the most recent pose that
53*ec779b8eSAndroid Build Coastguard Worker // violated stillness and update the suppression deadline if it has not been set or if the new
54*ec779b8eSAndroid Build Coastguard Worker // one ends after the current one.
55*ec779b8eSAndroid Build Coastguard Worker bool moved = false;
56*ec779b8eSAndroid Build Coastguard Worker
57*ec779b8eSAndroid Build Coastguard Worker if (!mFifo.empty()) {
58*ec779b8eSAndroid Build Coastguard Worker for (auto iter = mFifo.rbegin() + 1; iter != mFifo.rend(); ++iter) {
59*ec779b8eSAndroid Build Coastguard Worker const auto& event = *iter;
60*ec779b8eSAndroid Build Coastguard Worker if (!areNear(event.pose, mFifo.back().pose)) {
61*ec779b8eSAndroid Build Coastguard Worker // Enable suppression for the duration of the window.
62*ec779b8eSAndroid Build Coastguard Worker int64_t deadline = event.timestamp + mOptions.windowDuration;
63*ec779b8eSAndroid Build Coastguard Worker if (!mSuppressionDeadline.has_value() || mSuppressionDeadline.value() < deadline) {
64*ec779b8eSAndroid Build Coastguard Worker mSuppressionDeadline = deadline;
65*ec779b8eSAndroid Build Coastguard Worker }
66*ec779b8eSAndroid Build Coastguard Worker moved = true;
67*ec779b8eSAndroid Build Coastguard Worker break;
68*ec779b8eSAndroid Build Coastguard Worker }
69*ec779b8eSAndroid Build Coastguard Worker }
70*ec779b8eSAndroid Build Coastguard Worker }
71*ec779b8eSAndroid Build Coastguard Worker
72*ec779b8eSAndroid Build Coastguard Worker // If the window has not been full, return the default value.
73*ec779b8eSAndroid Build Coastguard Worker if (!mWindowFull) {
74*ec779b8eSAndroid Build Coastguard Worker mCurrentState = mOptions.defaultValue;
75*ec779b8eSAndroid Build Coastguard Worker }
76*ec779b8eSAndroid Build Coastguard Worker // Force "in motion" while the suppression deadline is active.
77*ec779b8eSAndroid Build Coastguard Worker else if (mSuppressionDeadline.has_value()) {
78*ec779b8eSAndroid Build Coastguard Worker mCurrentState = false;
79*ec779b8eSAndroid Build Coastguard Worker }
80*ec779b8eSAndroid Build Coastguard Worker else {
81*ec779b8eSAndroid Build Coastguard Worker mCurrentState = !moved;
82*ec779b8eSAndroid Build Coastguard Worker }
83*ec779b8eSAndroid Build Coastguard Worker
84*ec779b8eSAndroid Build Coastguard Worker return mCurrentState;
85*ec779b8eSAndroid Build Coastguard Worker }
86*ec779b8eSAndroid Build Coastguard Worker
discardOld(int64_t timestamp)87*ec779b8eSAndroid Build Coastguard Worker void StillnessDetector::discardOld(int64_t timestamp) {
88*ec779b8eSAndroid Build Coastguard Worker // Handle the special case of the window duration being zero (always considered full).
89*ec779b8eSAndroid Build Coastguard Worker if (mOptions.windowDuration == 0) {
90*ec779b8eSAndroid Build Coastguard Worker mFifo.clear();
91*ec779b8eSAndroid Build Coastguard Worker mWindowFull = true;
92*ec779b8eSAndroid Build Coastguard Worker }
93*ec779b8eSAndroid Build Coastguard Worker
94*ec779b8eSAndroid Build Coastguard Worker // Remove any events from the queue that are older than the window. If there were any such
95*ec779b8eSAndroid Build Coastguard Worker // events we consider the window full.
96*ec779b8eSAndroid Build Coastguard Worker const int64_t windowStart = timestamp - mOptions.windowDuration;
97*ec779b8eSAndroid Build Coastguard Worker while (!mFifo.empty() && mFifo.front().timestamp <= windowStart) {
98*ec779b8eSAndroid Build Coastguard Worker mWindowFull = true;
99*ec779b8eSAndroid Build Coastguard Worker mFifo.pop_front();
100*ec779b8eSAndroid Build Coastguard Worker }
101*ec779b8eSAndroid Build Coastguard Worker
102*ec779b8eSAndroid Build Coastguard Worker // Expire the suppression deadline.
103*ec779b8eSAndroid Build Coastguard Worker if (mSuppressionDeadline.has_value() && mSuppressionDeadline <= timestamp) {
104*ec779b8eSAndroid Build Coastguard Worker mSuppressionDeadline.reset();
105*ec779b8eSAndroid Build Coastguard Worker }
106*ec779b8eSAndroid Build Coastguard Worker }
107*ec779b8eSAndroid Build Coastguard Worker
areNear(const Pose3f & pose1,const Pose3f & pose2) const108*ec779b8eSAndroid Build Coastguard Worker bool StillnessDetector::areNear(const Pose3f& pose1, const Pose3f& pose2) const {
109*ec779b8eSAndroid Build Coastguard Worker // Check translation. We use the L1 norm to reduce computational load on expense of accuracy.
110*ec779b8eSAndroid Build Coastguard Worker // The L1 norm is an upper bound for the actual (L2) norm, so this approach will err on the side
111*ec779b8eSAndroid Build Coastguard Worker // of "not near".
112*ec779b8eSAndroid Build Coastguard Worker if ((pose1.translation() - pose2.translation()).lpNorm<1>() > mOptions.translationalThreshold) {
113*ec779b8eSAndroid Build Coastguard Worker return false;
114*ec779b8eSAndroid Build Coastguard Worker }
115*ec779b8eSAndroid Build Coastguard Worker
116*ec779b8eSAndroid Build Coastguard Worker // Check orientation.
117*ec779b8eSAndroid Build Coastguard Worker // The angle x between the quaternions is greater than that threshold iff
118*ec779b8eSAndroid Build Coastguard Worker // cos(x/2) < cos(threshold/2).
119*ec779b8eSAndroid Build Coastguard Worker // cos(x/2) can be efficiently calculated as the dot product of both quaternions.
120*ec779b8eSAndroid Build Coastguard Worker if (pose1.rotation().dot(pose2.rotation()) < mCosHalfRotationalThreshold) {
121*ec779b8eSAndroid Build Coastguard Worker return false;
122*ec779b8eSAndroid Build Coastguard Worker }
123*ec779b8eSAndroid Build Coastguard Worker
124*ec779b8eSAndroid Build Coastguard Worker return true;
125*ec779b8eSAndroid Build Coastguard Worker }
126*ec779b8eSAndroid Build Coastguard Worker
127*ec779b8eSAndroid Build Coastguard Worker } // namespace media
128*ec779b8eSAndroid Build Coastguard Worker } // namespace android
129