Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 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 | */ |
Shunkai Yao | 0324a39 | 2022-08-30 03:14:50 +0000 | [diff] [blame] | 16 | #include <android-base/stringprintf.h> |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 17 | |
| 18 | #include "media/Pose.h" |
Andy Hung | 9d197e7 | 2023-02-03 19:18:33 -0800 | [diff] [blame] | 19 | #include "media/QuaternionUtil.h" |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 20 | #include "media/Twist.h" |
| 21 | |
| 22 | namespace android { |
| 23 | namespace media { |
| 24 | |
Shunkai Yao | 0324a39 | 2022-08-30 03:14:50 +0000 | [diff] [blame] | 25 | using android::base::StringAppendF; |
Ytai Ben-Tsvi | 35a1fcb | 2021-08-20 16:13:19 -0700 | [diff] [blame] | 26 | using Eigen::Vector3f; |
| 27 | |
| 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 | |
| 35 | 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 | |
Shunkai Yao | 0324a39 | 2022-08-30 03:14:50 +0000 | [diff] [blame] | 40 | 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 | |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 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). |
Ytai Ben-Tsvi | c2d2840 | 2022-01-14 14:55:16 -0800 | [diff] [blame] | 61 | if (t == 0 || maxTranslationalVelocity == 0 || maxRotationalVelocity == 0) { |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 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 | |
| 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 |