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 | |
| 22 | StillnessDetector::StillnessDetector(const Options& options) : mOptions(options) {} |
| 23 | |
| 24 | void StillnessDetector::reset() { |
| 25 | mFifo.clear(); |
| 26 | mWindowFull = false; |
| 27 | } |
| 28 | |
| 29 | void StillnessDetector::setInput(int64_t timestamp, const Pose3f& input) { |
| 30 | mFifo.push_back(TimestampedPose{timestamp, input}); |
| 31 | discardOld(timestamp); |
| 32 | } |
| 33 | |
| 34 | bool StillnessDetector::calculate(int64_t timestamp) { |
| 35 | discardOld(timestamp); |
| 36 | |
| 37 | // If the window has not been full, we don't consider ourselves still. |
| 38 | if (!mWindowFull) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | // An empty FIFO and window full is considered still (this will happen in the unlikely case when |
| 43 | // the window duration is shorter than the gap between samples). |
| 44 | if (mFifo.empty()) { |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | // Otherwise, check whether all the poses remaining in the queue are in the proximity of the new |
| 49 | // one. |
| 50 | for (auto iter = mFifo.begin(); iter != mFifo.end() - 1; ++iter) { |
| 51 | const auto& event = *iter; |
| 52 | if (!areNear(event.pose, mFifo.back().pose)) { |
| 53 | return false; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | void StillnessDetector::discardOld(int64_t timestamp) { |
| 61 | // Handle the special case of the window duration being zero (always considered full). |
| 62 | if (mOptions.windowDuration == 0) { |
| 63 | mFifo.clear(); |
| 64 | mWindowFull = true; |
| 65 | } |
| 66 | |
| 67 | // Remove any events from the queue that are older than the window. If there were any such |
| 68 | // events we consider the window full. |
| 69 | const int64_t windowStart = timestamp - mOptions.windowDuration; |
| 70 | while (!mFifo.empty() && mFifo.front().timestamp <= windowStart) { |
| 71 | mWindowFull = true; |
| 72 | mFifo.pop_front(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | bool StillnessDetector::areNear(const Pose3f& pose1, const Pose3f& pose2) const { |
| 77 | // Check translation. We use the L1 norm to reduce computational load on expense of accuracy. |
| 78 | // The L1 norm is an upper bound for the actual (L2) norm, so this approach will err on the side |
| 79 | // of "not near". |
| 80 | if ((pose1.translation() - pose2.translation()).lpNorm<1>() >= |
| 81 | mOptions.translationalThreshold) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | // Check orientation. We use the L1 norm of the imaginary components of the quaternion to reduce |
| 86 | // computational load on expense of accuracy. For small angles, those components are approx. |
| 87 | // equal to the angle of rotation and so the norm is approx. the total angle of rotation. The |
| 88 | // L1 norm is an upper bound, so this approach will err on the side of "not near". |
| 89 | if ((pose1.rotation().vec() - pose2.rotation().vec()).lpNorm<1>() >= |
| 90 | mOptions.rotationalThreshold) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | } // namespace media |
| 98 | } // namespace android |