blob: df0a05fe366802d2bdb45429617730d5b43bc42c [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
Ytai Ben-Tsvib2a87e52021-08-20 10:17:05 -070042 // First pose sets the baseline.
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -070043 comp.setInput(1000, pose1);
Ytai Ben-Tsvib2a87e52021-08-20 10:17:05 -070044 EXPECT_EQ(comp.getOutput(), Pose3f());
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -070045
46 comp.setInput(2000, pose2);
Ytai Ben-Tsvib2a87e52021-08-20 10:17:05 -070047 EXPECT_EQ(comp.getOutput(), pose1.inverse() * pose2);
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -070048
Ytai Ben-Tsvib2a87e52021-08-20 10:17:05 -070049 // Recentering resets the baseline.
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -070050 comp.recenter();
51 EXPECT_EQ(comp.getOutput(), Pose3f());
52
53 comp.setInput(3000, pose1);
Ytai Ben-Tsvib2a87e52021-08-20 10:17:05 -070054 EXPECT_EQ(comp.getOutput(), Pose3f());
55
56 comp.setInput(4000, pose2);
57 EXPECT_EQ(comp.getOutput(), pose1.inverse() * pose2);
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -070058}
59
60TEST(PoseDriftCompensator, NoDriftZeroTime) {
61 Pose3f pose1({1, 2, 3}, Quaternionf::UnitRandom());
62 Pose3f pose2({4, 5, 6}, Quaternionf::UnitRandom());
63 PoseDriftCompensator comp(Options{});
64
65 comp.setInput(1000, pose1);
Ytai Ben-Tsvib2a87e52021-08-20 10:17:05 -070066 EXPECT_EQ(comp.getOutput(), Pose3f());
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -070067
68 comp.setInput(1000, pose2);
Ytai Ben-Tsvib2a87e52021-08-20 10:17:05 -070069 EXPECT_EQ(comp.getOutput(), pose1.inverse() * pose2);
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -070070
71 comp.recenter();
72 EXPECT_EQ(comp.getOutput(), Pose3f());
73
74 comp.setInput(1000, pose1);
Ytai Ben-Tsvib2a87e52021-08-20 10:17:05 -070075 EXPECT_EQ(comp.getOutput(), Pose3f());
76
77 comp.setInput(1000, pose2);
78 EXPECT_EQ(comp.getOutput(), pose1.inverse() * pose2);
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -070079}
80
81TEST(PoseDriftCompensator, Asymptotic) {
82 Pose3f pose({1, 2, 3}, Quaternionf::UnitRandom());
83
84 PoseDriftCompensator comp(
85 Options{.translationalDriftTimeConstant = 1, .rotationalDriftTimeConstant = 1});
86
87 // Set the same pose for a long time.
88 for (int64_t t = 0; t < 1000; ++t) {
89 comp.setInput(t, pose);
90 }
91
92 // Output would have faded to approx. identity.
93 EXPECT_EQ(comp.getOutput(), Pose3f());
94}
95
96TEST(PoseDriftCompensator, Fast) {
97 Pose3f pose1({1, 2, 3}, Quaternionf::UnitRandom());
98 Pose3f pose2({4, 5, 6}, Quaternionf::UnitRandom());
99 PoseDriftCompensator comp(
100 Options{.translationalDriftTimeConstant = 1e7, .rotationalDriftTimeConstant = 1e7});
101
102 comp.setInput(0, pose1);
Ytai Ben-Tsvib2a87e52021-08-20 10:17:05 -0700103 EXPECT_EQ(comp.getOutput(), Pose3f());
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -0700104
105 comp.setInput(1, pose2);
Ytai Ben-Tsvib2a87e52021-08-20 10:17:05 -0700106 EXPECT_EQ(comp.getOutput(), pose1.inverse() * pose2);
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -0700107
108 comp.recenter();
109 EXPECT_EQ(comp.getOutput(), Pose3f());
110
111 comp.setInput(2, pose1);
Ytai Ben-Tsvib2a87e52021-08-20 10:17:05 -0700112 EXPECT_EQ(comp.getOutput(), Pose3f());
113
114 comp.setInput(3, pose2);
115 EXPECT_EQ(comp.getOutput(), pose1.inverse() * pose2);
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -0700116}
117
118TEST(PoseDriftCompensator, Drift) {
119 Pose3f pose1({1, 2, 3}, rotateZ(-M_PI * 3 / 4));
120 PoseDriftCompensator comp(
121 Options{.translationalDriftTimeConstant = 500, .rotationalDriftTimeConstant = 1000});
122
Ytai Ben-Tsvib2a87e52021-08-20 10:17:05 -0700123 // Establish a baseline.
124 comp.setInput(1000, Pose3f());
125
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -0700126 // Initial pose is used as is.
127 comp.setInput(1000, pose1);
128 EXPECT_EQ(comp.getOutput(), pose1);
129
130 // After 1000 ticks, our rotation should be exp(-1) and translation exp(-2) from identity.
131 comp.setInput(2000, pose1);
132 EXPECT_EQ(comp.getOutput(),
133 Pose3f(Vector3f{1, 2, 3} * std::expf(-2), rotateZ(-M_PI * 3 / 4 * std::expf(-1))));
134
135 // As long as the input stays the same, we'll continue to advance towards identity.
136 comp.setInput(3000, pose1);
137 EXPECT_EQ(comp.getOutput(),
138 Pose3f(Vector3f{1, 2, 3} * std::expf(-4), rotateZ(-M_PI * 3 / 4 * std::expf(-2))));
139
140 comp.recenter();
141 EXPECT_EQ(comp.getOutput(), Pose3f());
142}
143
144} // namespace
145} // namespace media
146} // namespace android