blob: 9dfe17246ce8b029028649aecafd5716b4fcd29f [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 "PoseDriftCompensator.h"
18
19#include <cmath>
20
21#include "QuaternionUtil.h"
22
23namespace android {
24namespace media {
25
26using Eigen::Quaternionf;
27using Eigen::Vector3f;
28
29PoseDriftCompensator::PoseDriftCompensator(const Options& options) : mOptions(options) {}
30
31void PoseDriftCompensator::setInput(int64_t timestamp, const Pose3f& input) {
32 if (!mTimestamp.has_value()) {
33 // First input sample sets the output directly.
34 mOutput = input;
35 } else {
36 Pose3f prevInputToInput = mPrevInput.inverse() * input;
37 mOutput = scale(mOutput, timestamp - mTimestamp.value()) * prevInputToInput;
38 }
39 mPrevInput = input;
40 mTimestamp = timestamp;
41}
42
43void PoseDriftCompensator::recenter() {
44 mOutput = Pose3f();
45}
46
47Pose3f PoseDriftCompensator::getOutput() const {
48 return mOutput;
49}
50
51Pose3f PoseDriftCompensator::scale(const Pose3f& pose, int64_t dt) {
52 // Translation.
53 Vector3f translation = pose.translation();
54 translation *= std::expf(-static_cast<float>(dt) / mOptions.translationalDriftTimeConstant);
55
56 // Rotation.
57 Vector3f rotationVec = quaternionToRotationVector(pose.rotation());
58 rotationVec *= std::expf(-static_cast<float>(dt) / mOptions.rotationalDriftTimeConstant);
59
60 return Pose3f(translation, rotationVectorToQuaternion(rotationVec));
61}
62
63} // namespace media
64} // namespace android