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; |
Ytai Ben-Tsvi | f766398 | 2022-01-06 11:26:12 -0800 | [diff] [blame] | 28 | mSuppressionDeadline.reset(); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void StillnessDetector::setInput(int64_t timestamp, const Pose3f& input) { |
| 32 | mFifo.push_back(TimestampedPose{timestamp, input}); |
| 33 | discardOld(timestamp); |
| 34 | } |
| 35 | |
| 36 | bool StillnessDetector::calculate(int64_t timestamp) { |
| 37 | discardOld(timestamp); |
| 38 | |
Ytai Ben-Tsvi | 10721e5 | 2022-01-19 16:26:16 -0800 | [diff] [blame] | 39 | // Check whether all the poses in the queue are in the proximity of the new one. We want to do |
| 40 | // this before checking the overriding conditions below, in order to update the suppression |
| 41 | // deadline correctly. We always go from end to start, to find the most recent pose that |
| 42 | // violated stillness and update the suppression deadline if it has not been set or if the new |
| 43 | // one ends after the current one. |
Ytai Ben-Tsvi | f766398 | 2022-01-06 11:26:12 -0800 | [diff] [blame] | 44 | bool moved = false; |
| 45 | |
| 46 | if (!mFifo.empty()) { |
Ytai Ben-Tsvi | 10721e5 | 2022-01-19 16:26:16 -0800 | [diff] [blame] | 47 | for (auto iter = mFifo.rbegin() + 1; iter != mFifo.rend(); ++iter) { |
Ytai Ben-Tsvi | f766398 | 2022-01-06 11:26:12 -0800 | [diff] [blame] | 48 | const auto& event = *iter; |
| 49 | if (!areNear(event.pose, mFifo.back().pose)) { |
| 50 | // Enable suppression for the duration of the window. |
Ytai Ben-Tsvi | 10721e5 | 2022-01-19 16:26:16 -0800 | [diff] [blame] | 51 | int64_t deadline = event.timestamp + mOptions.windowDuration; |
| 52 | if (!mSuppressionDeadline.has_value() || mSuppressionDeadline.value() < deadline) { |
| 53 | mSuppressionDeadline = deadline; |
| 54 | } |
Ytai Ben-Tsvi | f766398 | 2022-01-06 11:26:12 -0800 | [diff] [blame] | 55 | moved = true; |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 61 | // If the window has not been full, return the default value. |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 62 | if (!mWindowFull) { |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 63 | return mOptions.defaultValue; |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Ytai Ben-Tsvi | f766398 | 2022-01-06 11:26:12 -0800 | [diff] [blame] | 66 | // Force "in motion" while the suppression deadline is active. |
| 67 | if (mSuppressionDeadline.has_value()) { |
| 68 | return false; |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Ytai Ben-Tsvi | f766398 | 2022-01-06 11:26:12 -0800 | [diff] [blame] | 71 | return !moved; |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void StillnessDetector::discardOld(int64_t timestamp) { |
| 75 | // Handle the special case of the window duration being zero (always considered full). |
| 76 | if (mOptions.windowDuration == 0) { |
| 77 | mFifo.clear(); |
| 78 | mWindowFull = true; |
| 79 | } |
| 80 | |
| 81 | // Remove any events from the queue that are older than the window. If there were any such |
| 82 | // events we consider the window full. |
| 83 | const int64_t windowStart = timestamp - mOptions.windowDuration; |
| 84 | while (!mFifo.empty() && mFifo.front().timestamp <= windowStart) { |
| 85 | mWindowFull = true; |
| 86 | mFifo.pop_front(); |
| 87 | } |
Ytai Ben-Tsvi | f766398 | 2022-01-06 11:26:12 -0800 | [diff] [blame] | 88 | |
| 89 | // Expire the suppression deadline. |
| 90 | if (mSuppressionDeadline.has_value() && mSuppressionDeadline <= timestamp) { |
| 91 | mSuppressionDeadline.reset(); |
| 92 | } |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | bool StillnessDetector::areNear(const Pose3f& pose1, const Pose3f& pose2) const { |
| 96 | // Check translation. We use the L1 norm to reduce computational load on expense of accuracy. |
| 97 | // The L1 norm is an upper bound for the actual (L2) norm, so this approach will err on the side |
| 98 | // of "not near". |
Ytai Ben-Tsvi | c6d7b83 | 2022-01-05 17:54:52 -0800 | [diff] [blame] | 99 | if ((pose1.translation() - pose2.translation()).lpNorm<1>() > mOptions.translationalThreshold) { |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
Ytai Ben-Tsvi | c6d7b83 | 2022-01-05 17:54:52 -0800 | [diff] [blame] | 103 | // Check orientation. |
| 104 | // The angle x between the quaternions is greater than that threshold iff |
| 105 | // cos(x/2) < cos(threshold/2). |
| 106 | // cos(x/2) can be efficiently calculated as the dot product of both quaternions. |
| 107 | if (pose1.rotation().dot(pose2.rotation()) < mCosHalfRotationalThreshold) { |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 108 | return false; |
| 109 | } |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | } // namespace media |
| 115 | } // namespace android |