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 | */ |
| 16 | |
| 17 | #include "media/Pose.h" |
| 18 | #include "media/Twist.h" |
Ytai Ben-Tsvi | 35a1fcb | 2021-08-20 16:13:19 -0700 | [diff] [blame] | 19 | #include "QuaternionUtil.h" |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 20 | |
| 21 | namespace android { |
| 22 | namespace media { |
| 23 | |
Ytai Ben-Tsvi | 35a1fcb | 2021-08-20 16:13:19 -0700 | [diff] [blame] | 24 | using Eigen::Vector3f; |
| 25 | |
| 26 | std::optional<Pose3f> Pose3f::fromVector(const std::vector<float>& vec) { |
| 27 | if (vec.size() != 6) { |
| 28 | return std::nullopt; |
| 29 | } |
| 30 | return Pose3f({vec[0], vec[1], vec[2]}, rotationVectorToQuaternion({vec[3], vec[4], vec[5]})); |
| 31 | } |
| 32 | |
| 33 | std::vector<float> Pose3f::toVector() const { |
| 34 | Eigen::Vector3f rot = quaternionToRotationVector(mRotation); |
| 35 | return {mTranslation[0], mTranslation[1], mTranslation[2], rot[0], rot[1], rot[2]}; |
| 36 | } |
| 37 | |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 38 | std::tuple<Pose3f, bool> moveWithRateLimit(const Pose3f& from, const Pose3f& to, float t, |
| 39 | float maxTranslationalVelocity, |
| 40 | float maxRotationalVelocity) { |
| 41 | // Never rate limit if both limits are set to infinity. |
| 42 | if (isinf(maxTranslationalVelocity) && isinf(maxRotationalVelocity)) { |
| 43 | return {to, false}; |
| 44 | } |
| 45 | // Always rate limit if t is 0 (required to avoid division by 0). |
| 46 | if (t == 0) { |
| 47 | return {from, true}; |
| 48 | } |
| 49 | |
| 50 | Pose3f fromToTo = from.inverse() * to; |
| 51 | Twist3f twist = differentiate(fromToTo, t); |
| 52 | float angularRotationalRatio = twist.scalarRotationalVelocity() / maxRotationalVelocity; |
| 53 | float translationalVelocityRatio = |
| 54 | twist.scalarTranslationalVelocity() / maxTranslationalVelocity; |
| 55 | float maxRatio = std::max(angularRotationalRatio, translationalVelocityRatio); |
| 56 | if (maxRatio <= 1) { |
| 57 | return {to, false}; |
| 58 | } |
| 59 | return {from * integrate(twist, t / maxRatio), true}; |
| 60 | } |
| 61 | |
| 62 | std::ostream& operator<<(std::ostream& os, const Pose3f& pose) { |
| 63 | os << "translation: " << pose.translation().transpose() |
| 64 | << " quaternion: " << pose.rotation().coeffs().transpose(); |
| 65 | return os; |
| 66 | } |
| 67 | |
| 68 | } // namespace media |
| 69 | } // namespace android |