blob: e03725bd6f8f0cc2ad100cfd85bd01f958b505af [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 */
Shunkai Yao0324a392022-08-30 03:14:50 +000016#include <android-base/stringprintf.h>
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -070017
18#include "media/Pose.h"
Andy Hung9d197e72023-02-03 19:18:33 -080019#include "media/QuaternionUtil.h"
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -070020#include "media/Twist.h"
21
22namespace android {
23namespace media {
24
Shunkai Yao0324a392022-08-30 03:14:50 +000025using android::base::StringAppendF;
Ytai Ben-Tsvi35a1fcb2021-08-20 16:13:19 -070026using Eigen::Vector3f;
27
28std::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
35std::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
Shunkai Yao0324a392022-08-30 03:14:50 +000040std::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
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -070053std::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).
Ytai Ben-Tsvic2d28402022-01-14 14:55:16 -080061 if (t == 0 || maxTranslationalVelocity == 0 || maxRotationalVelocity == 0) {
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -070062 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
77std::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