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 <gtest/gtest.h> |
| 18 | |
| 19 | #include "QuaternionUtil.h" |
| 20 | #include "StillnessDetector.h" |
| 21 | #include "TestUtil.h" |
| 22 | |
| 23 | namespace android { |
| 24 | namespace media { |
| 25 | namespace { |
| 26 | |
| 27 | using Eigen::Quaternionf; |
| 28 | using Eigen::Vector3f; |
| 29 | using Options = StillnessDetector::Options; |
| 30 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 31 | class StillnessDetectorTest : public testing::TestWithParam<bool> { |
| 32 | public: |
| 33 | void SetUp() override { mDefaultValue = GetParam(); } |
| 34 | |
| 35 | protected: |
| 36 | bool mDefaultValue; |
| 37 | }; |
| 38 | |
| 39 | TEST_P(StillnessDetectorTest, Still) { |
| 40 | StillnessDetector detector(Options{.defaultValue = mDefaultValue, |
| 41 | .windowDuration = 1000, |
| 42 | .translationalThreshold = 1, |
| 43 | .rotationalThreshold = 0.05}); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 44 | |
| 45 | const Pose3f baseline(Vector3f{1, 2, 3}, Quaternionf::UnitRandom()); |
| 46 | const Pose3f withinThreshold = |
| 47 | baseline * Pose3f(Vector3f(0.3, -0.3, 0), rotateX(0.01) * rotateY(-0.01)); |
| 48 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 49 | EXPECT_EQ(mDefaultValue, detector.calculate(0)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 50 | detector.setInput(0, baseline); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 51 | EXPECT_EQ(mDefaultValue, detector.calculate(0)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 52 | detector.setInput(300, withinThreshold); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 53 | EXPECT_EQ(mDefaultValue, detector.calculate(300)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 54 | detector.setInput(600, baseline); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 55 | EXPECT_EQ(mDefaultValue, detector.calculate(600)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 56 | detector.setInput(999, withinThreshold); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 57 | EXPECT_EQ(mDefaultValue, detector.calculate(999)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 58 | detector.setInput(1000, baseline); |
| 59 | EXPECT_TRUE(detector.calculate(1000)); |
| 60 | } |
| 61 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 62 | TEST_P(StillnessDetectorTest, ZeroDuration) { |
| 63 | StillnessDetector detector(Options{.defaultValue = mDefaultValue, .windowDuration = 0}); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 64 | EXPECT_TRUE(detector.calculate(0)); |
| 65 | EXPECT_TRUE(detector.calculate(1000)); |
| 66 | } |
| 67 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 68 | TEST_P(StillnessDetectorTest, NotStillTranslation) { |
| 69 | StillnessDetector detector(Options{.defaultValue = mDefaultValue, |
| 70 | .windowDuration = 1000, |
| 71 | .translationalThreshold = 1, |
| 72 | .rotationalThreshold = 0.05}); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 73 | |
| 74 | const Pose3f baseline(Vector3f{1, 2, 3}, Quaternionf::UnitRandom()); |
| 75 | const Pose3f withinThreshold = |
| 76 | baseline * Pose3f(Vector3f(0.3, -0.3, 0), rotateX(0.01) * rotateY(-0.01)); |
| 77 | const Pose3f outsideThreshold = baseline * Pose3f(Vector3f(1, 1, 0)); |
| 78 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 79 | EXPECT_EQ(mDefaultValue, detector.calculate(0)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 80 | detector.setInput(0, baseline); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 81 | EXPECT_EQ(mDefaultValue, detector.calculate(0)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 82 | detector.setInput(300, outsideThreshold); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 83 | EXPECT_EQ(mDefaultValue, detector.calculate(300)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 84 | detector.setInput(600, baseline); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 85 | EXPECT_EQ(mDefaultValue, detector.calculate(600)); |
Ytai Ben-Tsvi | 10721e5 | 2022-01-19 16:26:16 -0800 | [diff] [blame] | 86 | detector.setInput(1299, withinThreshold); |
| 87 | EXPECT_FALSE(detector.calculate(1299)); |
Ytai Ben-Tsvi | f766398 | 2022-01-06 11:26:12 -0800 | [diff] [blame] | 88 | detector.setInput(1300, baseline); |
Ytai Ben-Tsvi | 10721e5 | 2022-01-19 16:26:16 -0800 | [diff] [blame] | 89 | EXPECT_TRUE(detector.calculate(1300)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 92 | TEST_P(StillnessDetectorTest, NotStillRotation) { |
| 93 | StillnessDetector detector(Options{.defaultValue = mDefaultValue, |
| 94 | .windowDuration = 1000, |
| 95 | .translationalThreshold = 1, |
| 96 | .rotationalThreshold = 0.05}); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 97 | |
| 98 | const Pose3f baseline(Vector3f{1, 2, 3}, Quaternionf::UnitRandom()); |
| 99 | const Pose3f withinThreshold = |
Ytai Ben-Tsvi | c6d7b83 | 2022-01-05 17:54:52 -0800 | [diff] [blame] | 100 | baseline * Pose3f(Vector3f(0.3, -0.3, 0), rotateX(0.03) * rotateY(-0.03)); |
| 101 | const Pose3f outsideThreshold = baseline * Pose3f(rotateZ(0.06)); |
Ytai Ben-Tsvi | f766398 | 2022-01-06 11:26:12 -0800 | [diff] [blame] | 102 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 103 | EXPECT_EQ(mDefaultValue, detector.calculate(0)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 104 | detector.setInput(0, baseline); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 105 | EXPECT_EQ(mDefaultValue, detector.calculate(0)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 106 | detector.setInput(300, outsideThreshold); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 107 | EXPECT_EQ(mDefaultValue, detector.calculate(300)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 108 | detector.setInput(600, baseline); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 109 | EXPECT_EQ(mDefaultValue, detector.calculate(600)); |
Ytai Ben-Tsvi | 10721e5 | 2022-01-19 16:26:16 -0800 | [diff] [blame] | 110 | detector.setInput(1299, withinThreshold); |
| 111 | EXPECT_FALSE(detector.calculate(1299)); |
Ytai Ben-Tsvi | f766398 | 2022-01-06 11:26:12 -0800 | [diff] [blame] | 112 | detector.setInput(1300, baseline); |
Ytai Ben-Tsvi | 10721e5 | 2022-01-19 16:26:16 -0800 | [diff] [blame] | 113 | EXPECT_TRUE(detector.calculate(1300)); |
Ytai Ben-Tsvi | f766398 | 2022-01-06 11:26:12 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEST_P(StillnessDetectorTest, Suppression) { |
| 117 | StillnessDetector detector(Options{.defaultValue = mDefaultValue, |
| 118 | .windowDuration = 1000, |
| 119 | .translationalThreshold = 1, |
| 120 | .rotationalThreshold = 0.05}); |
| 121 | |
| 122 | const Pose3f baseline(Vector3f{1, 2, 3}, Quaternionf::UnitRandom()); |
| 123 | const Pose3f outsideThreshold = baseline * Pose3f(Vector3f(1.1, 0, 0)); |
| 124 | const Pose3f middlePoint = baseline * Pose3f(Vector3f(0.55, 0, 0)); |
| 125 | |
| 126 | detector.setInput(0, baseline); |
| 127 | detector.setInput(1000, baseline); |
| 128 | EXPECT_TRUE(detector.calculate(1000)); |
| 129 | detector.setInput(1100, outsideThreshold); |
| 130 | EXPECT_FALSE(detector.calculate(1100)); |
Ytai Ben-Tsvi | 10721e5 | 2022-01-19 16:26:16 -0800 | [diff] [blame] | 131 | detector.setInput(1500, middlePoint); |
| 132 | EXPECT_FALSE(detector.calculate(1500)); |
| 133 | EXPECT_FALSE(detector.calculate(1999)); |
| 134 | EXPECT_TRUE(detector.calculate(2000)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 137 | TEST_P(StillnessDetectorTest, Reset) { |
| 138 | StillnessDetector detector(Options{.defaultValue = mDefaultValue, |
| 139 | .windowDuration = 1000, |
| 140 | .translationalThreshold = 1, |
| 141 | .rotationalThreshold = 0.05}); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 142 | |
| 143 | const Pose3f baseline(Vector3f{1, 2, 3}, Quaternionf::UnitRandom()); |
| 144 | const Pose3f withinThreshold = |
| 145 | baseline * Pose3f(Vector3f(0.3, -0.3, 0), rotateX(0.01) * rotateY(-0.01)); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 146 | EXPECT_EQ(mDefaultValue, detector.calculate(0)); |
Ytai Ben-Tsvi | 10721e5 | 2022-01-19 16:26:16 -0800 | [diff] [blame] | 147 | detector.setInput(300, baseline); |
| 148 | EXPECT_EQ(mDefaultValue, detector.calculate(300)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 149 | detector.reset(); |
| 150 | detector.setInput(600, baseline); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 151 | EXPECT_EQ(mDefaultValue, detector.calculate(600)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 152 | detector.setInput(900, withinThreshold); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 153 | EXPECT_EQ(mDefaultValue, detector.calculate(900)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 154 | detector.setInput(1200, baseline); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 155 | EXPECT_EQ(mDefaultValue, detector.calculate(1200)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 156 | detector.setInput(1599, withinThreshold); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 157 | EXPECT_EQ(mDefaultValue, detector.calculate(1599)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 158 | detector.setInput(1600, baseline); |
| 159 | EXPECT_TRUE(detector.calculate(1600)); |
| 160 | } |
| 161 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 162 | INSTANTIATE_TEST_SUITE_P(StillnessDetectorTestParametrized, StillnessDetectorTest, |
| 163 | testing::Values(false, true)); |
| 164 | |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 165 | } // namespace |
| 166 | } // namespace media |
| 167 | } // namespace android |