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