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 | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 88 | detector.setInput(1299, baseline); |
| 89 | EXPECT_FALSE(detector.calculate(1299)); |
| 90 | EXPECT_TRUE(detector.calculate(1300)); |
| 91 | } |
| 92 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame^] | 93 | TEST_P(StillnessDetectorTest, NotStillRotation) { |
| 94 | StillnessDetector detector(Options{.defaultValue = mDefaultValue, |
| 95 | .windowDuration = 1000, |
| 96 | .translationalThreshold = 1, |
| 97 | .rotationalThreshold = 0.05}); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 98 | |
| 99 | const Pose3f baseline(Vector3f{1, 2, 3}, Quaternionf::UnitRandom()); |
| 100 | const Pose3f withinThreshold = |
Ytai Ben-Tsvi | c6d7b83 | 2022-01-05 17:54:52 -0800 | [diff] [blame] | 101 | baseline * Pose3f(Vector3f(0.3, -0.3, 0), rotateX(0.03) * rotateY(-0.03)); |
| 102 | const Pose3f outsideThreshold = baseline * Pose3f(rotateZ(0.06)); |
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 | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 110 | detector.setInput(900, withinThreshold); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame^] | 111 | EXPECT_EQ(mDefaultValue, detector.calculate(900)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 112 | detector.setInput(1299, baseline); |
| 113 | EXPECT_FALSE(detector.calculate(1299)); |
| 114 | EXPECT_TRUE(detector.calculate(1300)); |
| 115 | } |
| 116 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame^] | 117 | TEST_P(StillnessDetectorTest, Reset) { |
| 118 | StillnessDetector detector(Options{.defaultValue = mDefaultValue, |
| 119 | .windowDuration = 1000, |
| 120 | .translationalThreshold = 1, |
| 121 | .rotationalThreshold = 0.05}); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 122 | |
| 123 | const Pose3f baseline(Vector3f{1, 2, 3}, Quaternionf::UnitRandom()); |
| 124 | const Pose3f withinThreshold = |
| 125 | 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^] | 126 | EXPECT_EQ(mDefaultValue, detector.calculate(0)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 127 | detector.setInput(0, baseline); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame^] | 128 | EXPECT_EQ(mDefaultValue, detector.calculate(0)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 129 | detector.reset(); |
| 130 | detector.setInput(600, baseline); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame^] | 131 | EXPECT_EQ(mDefaultValue, detector.calculate(600)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 132 | detector.setInput(900, withinThreshold); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame^] | 133 | EXPECT_EQ(mDefaultValue, detector.calculate(900)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 134 | detector.setInput(1200, baseline); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame^] | 135 | EXPECT_EQ(mDefaultValue, detector.calculate(1200)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 136 | detector.setInput(1599, withinThreshold); |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame^] | 137 | EXPECT_EQ(mDefaultValue, detector.calculate(1599)); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 138 | detector.setInput(1600, baseline); |
| 139 | EXPECT_TRUE(detector.calculate(1600)); |
| 140 | } |
| 141 | |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame^] | 142 | INSTANTIATE_TEST_SUITE_P(StillnessDetectorTestParametrized, StillnessDetectorTest, |
| 143 | testing::Values(false, true)); |
| 144 | |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 145 | } // namespace |
| 146 | } // namespace media |
| 147 | } // namespace android |