xref: /aosp_15_r20/frameworks/av/media/libheadtracking/Twist.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1*ec779b8eSAndroid Build Coastguard Worker /*
2*ec779b8eSAndroid Build Coastguard Worker  * Copyright (C) 2021 The Android Open Source Project
3*ec779b8eSAndroid Build Coastguard Worker  *
4*ec779b8eSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*ec779b8eSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*ec779b8eSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*ec779b8eSAndroid Build Coastguard Worker  *
8*ec779b8eSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*ec779b8eSAndroid Build Coastguard Worker  *
10*ec779b8eSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*ec779b8eSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*ec779b8eSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*ec779b8eSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*ec779b8eSAndroid Build Coastguard Worker  * limitations under the License.
15*ec779b8eSAndroid Build Coastguard Worker  */
16*ec779b8eSAndroid Build Coastguard Worker 
17*ec779b8eSAndroid Build Coastguard Worker #include "media/Twist.h"
18*ec779b8eSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
19*ec779b8eSAndroid Build Coastguard Worker #include "media/QuaternionUtil.h"
20*ec779b8eSAndroid Build Coastguard Worker 
21*ec779b8eSAndroid Build Coastguard Worker namespace android {
22*ec779b8eSAndroid Build Coastguard Worker namespace media {
23*ec779b8eSAndroid Build Coastguard Worker 
integrate(const Twist3f & twist,float dt)24*ec779b8eSAndroid Build Coastguard Worker Pose3f integrate(const Twist3f& twist, float dt) {
25*ec779b8eSAndroid Build Coastguard Worker     Eigen::Vector3f translation = twist.translationalVelocity() * dt;
26*ec779b8eSAndroid Build Coastguard Worker     Eigen::Vector3f rotationVector = twist.rotationalVelocity() * dt;
27*ec779b8eSAndroid Build Coastguard Worker     return Pose3f(translation, rotationVectorToQuaternion(rotationVector));
28*ec779b8eSAndroid Build Coastguard Worker }
29*ec779b8eSAndroid Build Coastguard Worker 
differentiate(const Pose3f & pose,float dt)30*ec779b8eSAndroid Build Coastguard Worker Twist3f differentiate(const Pose3f& pose, float dt) {
31*ec779b8eSAndroid Build Coastguard Worker     Eigen::Vector3f translationalVelocity = pose.translation() / dt;
32*ec779b8eSAndroid Build Coastguard Worker     Eigen::Vector3f rotationalVelocity = quaternionToRotationVector(pose.rotation()) / dt;
33*ec779b8eSAndroid Build Coastguard Worker     return Twist3f(translationalVelocity, rotationalVelocity);
34*ec779b8eSAndroid Build Coastguard Worker }
35*ec779b8eSAndroid Build Coastguard Worker 
operator <<(std::ostream & os,const Twist3f & twist)36*ec779b8eSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, const Twist3f& twist) {
37*ec779b8eSAndroid Build Coastguard Worker     os << "translation: " << twist.translationalVelocity().transpose()
38*ec779b8eSAndroid Build Coastguard Worker        << " rotation vector: " << twist.rotationalVelocity().transpose();
39*ec779b8eSAndroid Build Coastguard Worker     return os;
40*ec779b8eSAndroid Build Coastguard Worker }
41*ec779b8eSAndroid Build Coastguard Worker 
toString() const42*ec779b8eSAndroid Build Coastguard Worker std::string Twist3f::toString() const {
43*ec779b8eSAndroid Build Coastguard Worker     return base::StringPrintf("[%0.2f, %0.2f, %0.2f, %0.2f, %0.2f, %0.2f]",
44*ec779b8eSAndroid Build Coastguard Worker         mTranslationalVelocity[0], mTranslationalVelocity[1], mTranslationalVelocity[2],
45*ec779b8eSAndroid Build Coastguard Worker         mRotationalVelocity[0], mRotationalVelocity[1], mRotationalVelocity[2]);
46*ec779b8eSAndroid Build Coastguard Worker }
47*ec779b8eSAndroid Build Coastguard Worker 
48*ec779b8eSAndroid Build Coastguard Worker }  // namespace media
49*ec779b8eSAndroid Build Coastguard Worker }  // namespace android
50