blob: 29b036e83ec8535770f940f6aeae6d4b4adef03d [file] [log] [blame]
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -08001/*
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
23namespace android {
24namespace media {
25namespace {
26
27using Eigen::Quaternionf;
28using Eigen::Vector3f;
29using Options = StillnessDetector::Options;
30
31TEST(StillnessDetectorTest, Still) {
32 StillnessDetector detector(Options{
33 .windowDuration = 1000, .translationalThreshold = 1, .rotationalThreshold = 0.05});
34
35 const Pose3f baseline(Vector3f{1, 2, 3}, Quaternionf::UnitRandom());
36 const Pose3f withinThreshold =
37 baseline * Pose3f(Vector3f(0.3, -0.3, 0), rotateX(0.01) * rotateY(-0.01));
38
39 EXPECT_FALSE(detector.calculate(0));
40 detector.setInput(0, baseline);
41 EXPECT_FALSE(detector.calculate(0));
42 detector.setInput(300, withinThreshold);
43 EXPECT_FALSE(detector.calculate(300));
44 detector.setInput(600, baseline);
45 EXPECT_FALSE(detector.calculate(600));
46 detector.setInput(999, withinThreshold);
47 EXPECT_FALSE(detector.calculate(999));
48 detector.setInput(1000, baseline);
49 EXPECT_TRUE(detector.calculate(1000));
50}
51
52TEST(StillnessDetectorTest, ZeroDuration) {
53 StillnessDetector detector(Options{.windowDuration = 0});
54 EXPECT_TRUE(detector.calculate(0));
55 EXPECT_TRUE(detector.calculate(1000));
56}
57
58TEST(StillnessDetectorTest, NotStillTranslation) {
59 StillnessDetector detector(Options{
60 .windowDuration = 1000, .translationalThreshold = 1, .rotationalThreshold = 0.05});
61
62 const Pose3f baseline(Vector3f{1, 2, 3}, Quaternionf::UnitRandom());
63 const Pose3f withinThreshold =
64 baseline * Pose3f(Vector3f(0.3, -0.3, 0), rotateX(0.01) * rotateY(-0.01));
65 const Pose3f outsideThreshold = baseline * Pose3f(Vector3f(1, 1, 0));
66
67 EXPECT_FALSE(detector.calculate(0));
68 detector.setInput(0, baseline);
69 EXPECT_FALSE(detector.calculate(0));
70 detector.setInput(300, outsideThreshold);
71 EXPECT_FALSE(detector.calculate(300));
72 detector.setInput(600, baseline);
73 EXPECT_FALSE(detector.calculate(600));
74 detector.setInput(900, withinThreshold);
75 EXPECT_FALSE(detector.calculate(900));
76 detector.setInput(1299, baseline);
77 EXPECT_FALSE(detector.calculate(1299));
78 EXPECT_TRUE(detector.calculate(1300));
79}
80
81TEST(StillnessDetectorTest, NotStillRotation) {
82 StillnessDetector detector(Options{
83 .windowDuration = 1000, .translationalThreshold = 1, .rotationalThreshold = 0.05});
84
85 const Pose3f baseline(Vector3f{1, 2, 3}, Quaternionf::UnitRandom());
86 const Pose3f withinThreshold =
Ytai Ben-Tsvic6d7b832022-01-05 17:54:52 -080087 baseline * Pose3f(Vector3f(0.3, -0.3, 0), rotateX(0.03) * rotateY(-0.03));
88 const Pose3f outsideThreshold = baseline * Pose3f(rotateZ(0.06));
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080089 EXPECT_FALSE(detector.calculate(0));
90 detector.setInput(0, baseline);
91 EXPECT_FALSE(detector.calculate(0));
92 detector.setInput(300, outsideThreshold);
93 EXPECT_FALSE(detector.calculate(300));
94 detector.setInput(600, baseline);
95 EXPECT_FALSE(detector.calculate(600));
96 detector.setInput(900, withinThreshold);
97 EXPECT_FALSE(detector.calculate(900));
98 detector.setInput(1299, baseline);
99 EXPECT_FALSE(detector.calculate(1299));
100 EXPECT_TRUE(detector.calculate(1300));
101}
102
103TEST(StillnessDetectorTest, Reset) {
104 StillnessDetector detector(Options{
105 .windowDuration = 1000, .translationalThreshold = 1, .rotationalThreshold = 0.05});
106
107 const Pose3f baseline(Vector3f{1, 2, 3}, Quaternionf::UnitRandom());
108 const Pose3f withinThreshold =
109 baseline * Pose3f(Vector3f(0.3, -0.3, 0), rotateX(0.01) * rotateY(-0.01));
110 EXPECT_FALSE(detector.calculate(0));
111 detector.setInput(0, baseline);
112 EXPECT_FALSE(detector.calculate(0));
113 detector.reset();
114 detector.setInput(600, baseline);
115 EXPECT_FALSE(detector.calculate(600));
116 detector.setInput(900, withinThreshold);
117 EXPECT_FALSE(detector.calculate(900));
118 detector.setInput(1200, baseline);
119 EXPECT_FALSE(detector.calculate(1200));
120 detector.setInput(1599, withinThreshold);
121 EXPECT_FALSE(detector.calculate(1599));
122 detector.setInput(1600, baseline);
123 EXPECT_TRUE(detector.calculate(1600));
124}
125
126} // namespace
127} // namespace media
128} // namespace android