• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #include <android-base/stringprintf.h>
17 
18 #include "media/Pose.h"
19 #include "media/QuaternionUtil.h"
20 #include "media/Twist.h"
21 
22 namespace android {
23 namespace media {
24 
25 using android::base::StringAppendF;
26 using Eigen::Vector3f;
27 
fromVector(const std::vector<float> & vec)28 std::optional<Pose3f> Pose3f::fromVector(const std::vector<float>& vec) {
29     if (vec.size() != 6) {
30         return std::nullopt;
31     }
32     return Pose3f({vec[0], vec[1], vec[2]}, rotationVectorToQuaternion({vec[3], vec[4], vec[5]}));
33 }
34 
toVector() const35 std::vector<float> Pose3f::toVector() const {
36     Eigen::Vector3f rot = quaternionToRotationVector(mRotation);
37     return {mTranslation[0], mTranslation[1], mTranslation[2], rot[0], rot[1], rot[2]};
38 }
39 
toString() const40 std::string Pose3f::toString() const {
41     const auto& vec = this->toVector();
42     std::string ss = "[";
43     for (auto f = vec.begin(); f != vec.end(); ++f) {
44         if (f != vec.begin()) {
45             ss.append(", ");
46         }
47         StringAppendF(&ss, "%0.2f", *f);
48     }
49     ss.append("]");
50     return ss;
51 }
52 
moveWithRateLimit(const Pose3f & from,const Pose3f & to,float t,float maxTranslationalVelocity,float maxRotationalVelocity)53 std::tuple<Pose3f, bool> moveWithRateLimit(const Pose3f& from, const Pose3f& to, float t,
54                                            float maxTranslationalVelocity,
55                                            float maxRotationalVelocity) {
56     // Never rate limit if both limits are set to infinity.
57     if (isinf(maxTranslationalVelocity) && isinf(maxRotationalVelocity)) {
58         return {to, false};
59     }
60     // Always rate limit if t is 0 (required to avoid division by 0).
61     if (t == 0 || maxTranslationalVelocity == 0 || maxRotationalVelocity == 0) {
62         return {from, true};
63     }
64 
65     Pose3f fromToTo = from.inverse() * to;
66     Twist3f twist = differentiate(fromToTo, t);
67     float angularRotationalRatio = twist.scalarRotationalVelocity() / maxRotationalVelocity;
68     float translationalVelocityRatio =
69             twist.scalarTranslationalVelocity() / maxTranslationalVelocity;
70     float maxRatio = std::max(angularRotationalRatio, translationalVelocityRatio);
71     if (maxRatio <= 1) {
72         return {to, false};
73     }
74     return {from * integrate(twist, t / maxRatio), true};
75 }
76 
operator <<(std::ostream & os,const Pose3f & pose)77 std::ostream& operator<<(std::ostream& os, const Pose3f& pose) {
78     os << "translation: " << pose.translation().transpose()
79        << " quaternion: " << pose.rotation().coeffs().transpose();
80     return os;
81 }
82 
83 }  // namespace media
84 }  // namespace android
85