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 | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 86 | detector.setInput(900, withinThreshold); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 87 | EXPECT_EQ(mDefaultValue, detector.calculate(900)); |
Ytai Ben-Tsvi | f766398 | 2022-01-06 11:26:12 -0800 | [diff] [blame] | 88 | detector.setInput(1300, baseline); |
| 89 | EXPECT_FALSE(detector.calculate(1300)); |
| 90 | detector.setInput(1500, baseline); |
| 91 | EXPECT_FALSE(detector.calculate(1899)); |
| 92 | EXPECT_TRUE(detector.calculate(1900)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 95 | TEST_P(StillnessDetectorTest, NotStillRotation) { |
| 96 | StillnessDetector detector(Options{.defaultValue = mDefaultValue, |
| 97 | .windowDuration = 1000, |
| 98 | .translationalThreshold = 1, |
| 99 | .rotationalThreshold = 0.05}); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 100 | |
| 101 | const Pose3f baseline(Vector3f{1, 2, 3}, Quaternionf::UnitRandom()); |
| 102 | const Pose3f withinThreshold = |
Ytai Ben-Tsvi | c6d7b83 | 2022-01-05 17:54:52 -0800 | [diff] [blame] | 103 | baseline * Pose3f(Vector3f(0.3, -0.3, 0), rotateX(0.03) * rotateY(-0.03)); |
| 104 | const Pose3f outsideThreshold = baseline * Pose3f(rotateZ(0.06)); |
Ytai Ben-Tsvi | f766398 | 2022-01-06 11:26:12 -0800 | [diff] [blame] | 105 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 106 | EXPECT_EQ(mDefaultValue, detector.calculate(0)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 107 | detector.setInput(0, baseline); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 108 | EXPECT_EQ(mDefaultValue, detector.calculate(0)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 109 | detector.setInput(300, outsideThreshold); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 110 | EXPECT_EQ(mDefaultValue, detector.calculate(300)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 111 | detector.setInput(600, baseline); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 112 | EXPECT_EQ(mDefaultValue, detector.calculate(600)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 113 | detector.setInput(900, withinThreshold); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 114 | EXPECT_EQ(mDefaultValue, detector.calculate(900)); |
Ytai Ben-Tsvi | f766398 | 2022-01-06 11:26:12 -0800 | [diff] [blame] | 115 | detector.setInput(1300, baseline); |
| 116 | EXPECT_FALSE(detector.calculate(1300)); |
| 117 | detector.setInput(1500, baseline); |
| 118 | EXPECT_FALSE(detector.calculate(1899)); |
| 119 | EXPECT_TRUE(detector.calculate(1900)); |
| 120 | } |
| 121 | |
| 122 | TEST_P(StillnessDetectorTest, Suppression) { |
| 123 | StillnessDetector detector(Options{.defaultValue = mDefaultValue, |
| 124 | .windowDuration = 1000, |
| 125 | .translationalThreshold = 1, |
| 126 | .rotationalThreshold = 0.05}); |
| 127 | |
| 128 | const Pose3f baseline(Vector3f{1, 2, 3}, Quaternionf::UnitRandom()); |
| 129 | const Pose3f outsideThreshold = baseline * Pose3f(Vector3f(1.1, 0, 0)); |
| 130 | const Pose3f middlePoint = baseline * Pose3f(Vector3f(0.55, 0, 0)); |
| 131 | |
| 132 | detector.setInput(0, baseline); |
| 133 | detector.setInput(1000, baseline); |
| 134 | EXPECT_TRUE(detector.calculate(1000)); |
| 135 | detector.setInput(1100, outsideThreshold); |
| 136 | EXPECT_FALSE(detector.calculate(1100)); |
| 137 | detector.setInput(2000, middlePoint); |
| 138 | EXPECT_FALSE(detector.calculate(2000)); |
| 139 | EXPECT_FALSE(detector.calculate(2099)); |
| 140 | EXPECT_TRUE(detector.calculate(2100)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 143 | TEST_P(StillnessDetectorTest, Reset) { |
| 144 | StillnessDetector detector(Options{.defaultValue = mDefaultValue, |
| 145 | .windowDuration = 1000, |
| 146 | .translationalThreshold = 1, |
| 147 | .rotationalThreshold = 0.05}); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 148 | |
| 149 | const Pose3f baseline(Vector3f{1, 2, 3}, Quaternionf::UnitRandom()); |
| 150 | const Pose3f withinThreshold = |
| 151 | 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] | 152 | EXPECT_EQ(mDefaultValue, detector.calculate(0)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 153 | detector.setInput(0, baseline); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 154 | EXPECT_EQ(mDefaultValue, detector.calculate(0)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 155 | detector.reset(); |
| 156 | detector.setInput(600, baseline); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 157 | EXPECT_EQ(mDefaultValue, detector.calculate(600)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 158 | detector.setInput(900, withinThreshold); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 159 | EXPECT_EQ(mDefaultValue, detector.calculate(900)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 160 | detector.setInput(1200, baseline); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 161 | EXPECT_EQ(mDefaultValue, detector.calculate(1200)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 162 | detector.setInput(1599, withinThreshold); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 163 | EXPECT_EQ(mDefaultValue, detector.calculate(1599)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 164 | detector.setInput(1600, baseline); |
| 165 | EXPECT_TRUE(detector.calculate(1600)); |
| 166 | } |
| 167 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 168 | INSTANTIATE_TEST_SUITE_P(StillnessDetectorTestParametrized, StillnessDetectorTest, |
| 169 | testing::Values(false, true)); |
| 170 | |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 171 | } // namespace |
| 172 | } // namespace media |
| 173 | } // namespace android |