Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | namespace android { |
| 20 | namespace media { |
| 21 | |
Ytai Ben-Tsvi | c6d7b83 | 2022-01-05 17:54:52 -0800 | [diff] [blame] | 22 | StillnessDetector::StillnessDetector(const Options& options) |
| 23 | : mOptions(options), mCosHalfRotationalThreshold(cos(mOptions.rotationalThreshold / 2)) {} |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 24 | |
| 25 | void StillnessDetector::reset() { |
| 26 | mFifo.clear(); |
| 27 | mWindowFull = false; |
| 28 | } |
| 29 | |
| 30 | void StillnessDetector::setInput(int64_t timestamp, const Pose3f& input) { |
| 31 | mFifo.push_back(TimestampedPose{timestamp, input}); |
| 32 | discardOld(timestamp); |
| 33 | } |
| 34 | |
| 35 | bool StillnessDetector::calculate(int64_t timestamp) { |
| 36 | discardOld(timestamp); |
| 37 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame^] | 38 | // If the window has not been full, return the default value. |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 39 | if (!mWindowFull) { |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame^] | 40 | return mOptions.defaultValue; |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame^] | 43 | // An empty FIFO and window full is considered still (this will happen when the window duration |
| 44 | // is shorter than the gap between samples, including the window size being 0). |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 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 | |
| 61 | void 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 | |
| 77 | bool 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-Tsvi | c6d7b83 | 2022-01-05 17:54:52 -0800 | [diff] [blame] | 81 | if ((pose1.translation() - pose2.translation()).lpNorm<1>() > mOptions.translationalThreshold) { |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | |
Ytai Ben-Tsvi | c6d7b83 | 2022-01-05 17:54:52 -0800 | [diff] [blame] | 85 | // 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-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | } // namespace media |
| 97 | } // namespace android |