blob: 74f4beeb509d44f99e34c4ca9cf4bc2773a9dc58 [file] [log] [blame]
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -07001/*
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#include <cmath>
19
20#include "PoseDriftCompensator.h"
21#include "QuaternionUtil.h"
22#include "TestUtil.h"
23
24namespace android {
25namespace media {
26namespace {
27
28using Eigen::Quaternionf;
29using Eigen::Vector3f;
30using Options = PoseDriftCompensator::Options;
31
32TEST(PoseDriftCompensator, Initial) {
33 PoseDriftCompensator comp(Options{});
34 EXPECT_EQ(comp.getOutput(), Pose3f());
35}
36
37TEST(PoseDriftCompensator, NoDrift) {
38 Pose3f pose1({1, 2, 3}, Quaternionf::UnitRandom());
39 Pose3f pose2({4, 5, 6}, Quaternionf::UnitRandom());
40 PoseDriftCompensator comp(Options{});
41
42 comp.setInput(1000, pose1);
43 EXPECT_EQ(comp.getOutput(), pose1);
44
45 comp.setInput(2000, pose2);
46 EXPECT_EQ(comp.getOutput(), pose2);
47
48 comp.recenter();
49 EXPECT_EQ(comp.getOutput(), Pose3f());
50
51 comp.setInput(3000, pose1);
52 EXPECT_EQ(comp.getOutput(), pose2.inverse() * pose1);
53}
54
55TEST(PoseDriftCompensator, NoDriftZeroTime) {
56 Pose3f pose1({1, 2, 3}, Quaternionf::UnitRandom());
57 Pose3f pose2({4, 5, 6}, Quaternionf::UnitRandom());
58 PoseDriftCompensator comp(Options{});
59
60 comp.setInput(1000, pose1);
61 EXPECT_EQ(comp.getOutput(), pose1);
62
63 comp.setInput(1000, pose2);
64 EXPECT_EQ(comp.getOutput(), pose2);
65
66 comp.recenter();
67 EXPECT_EQ(comp.getOutput(), Pose3f());
68
69 comp.setInput(1000, pose1);
70 EXPECT_EQ(comp.getOutput(), pose2.inverse() * pose1);
71}
72
73TEST(PoseDriftCompensator, Asymptotic) {
74 Pose3f pose({1, 2, 3}, Quaternionf::UnitRandom());
75
76 PoseDriftCompensator comp(
77 Options{.translationalDriftTimeConstant = 1, .rotationalDriftTimeConstant = 1});
78
79 // Set the same pose for a long time.
80 for (int64_t t = 0; t < 1000; ++t) {
81 comp.setInput(t, pose);
82 }
83
84 // Output would have faded to approx. identity.
85 EXPECT_EQ(comp.getOutput(), Pose3f());
86}
87
88TEST(PoseDriftCompensator, Fast) {
89 Pose3f pose1({1, 2, 3}, Quaternionf::UnitRandom());
90 Pose3f pose2({4, 5, 6}, Quaternionf::UnitRandom());
91 PoseDriftCompensator comp(
92 Options{.translationalDriftTimeConstant = 1e7, .rotationalDriftTimeConstant = 1e7});
93
94 comp.setInput(0, pose1);
95 EXPECT_EQ(comp.getOutput(), pose1);
96
97 comp.setInput(1, pose2);
98 EXPECT_EQ(comp.getOutput(), pose2);
99
100 comp.recenter();
101 EXPECT_EQ(comp.getOutput(), Pose3f());
102
103 comp.setInput(2, pose1);
104 EXPECT_EQ(comp.getOutput(), pose2.inverse() * pose1);
105}
106
107TEST(PoseDriftCompensator, Drift) {
108 Pose3f pose1({1, 2, 3}, rotateZ(-M_PI * 3 / 4));
109 PoseDriftCompensator comp(
110 Options{.translationalDriftTimeConstant = 500, .rotationalDriftTimeConstant = 1000});
111
112 // Initial pose is used as is.
113 comp.setInput(1000, pose1);
114 EXPECT_EQ(comp.getOutput(), pose1);
115
116 // After 1000 ticks, our rotation should be exp(-1) and translation exp(-2) from identity.
117 comp.setInput(2000, pose1);
118 EXPECT_EQ(comp.getOutput(),
119 Pose3f(Vector3f{1, 2, 3} * std::expf(-2), rotateZ(-M_PI * 3 / 4 * std::expf(-1))));
120
121 // As long as the input stays the same, we'll continue to advance towards identity.
122 comp.setInput(3000, pose1);
123 EXPECT_EQ(comp.getOutput(),
124 Pose3f(Vector3f{1, 2, 3} * std::expf(-4), rotateZ(-M_PI * 3 / 4 * std::expf(-2))));
125
126 comp.recenter();
127 EXPECT_EQ(comp.getOutput(), Pose3f());
128}
129
130} // namespace
131} // namespace media
132} // namespace android