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" |
| 19 | |
| 20 | namespace android { |
| 21 | namespace media { |
| 22 | |
| 23 | std::tuple<Pose3f, bool> moveWithRateLimit(const Pose3f& from, const Pose3f& to, float t, |
| 24 | float maxTranslationalVelocity, |
| 25 | float maxRotationalVelocity) { |
| 26 | // Never rate limit if both limits are set to infinity. |
| 27 | if (isinf(maxTranslationalVelocity) && isinf(maxRotationalVelocity)) { |
| 28 | return {to, false}; |
| 29 | } |
| 30 | // Always rate limit if t is 0 (required to avoid division by 0). |
| 31 | if (t == 0) { |
| 32 | return {from, true}; |
| 33 | } |
| 34 | |
| 35 | Pose3f fromToTo = from.inverse() * to; |
| 36 | Twist3f twist = differentiate(fromToTo, t); |
| 37 | float angularRotationalRatio = twist.scalarRotationalVelocity() / maxRotationalVelocity; |
| 38 | float translationalVelocityRatio = |
| 39 | twist.scalarTranslationalVelocity() / maxTranslationalVelocity; |
| 40 | float maxRatio = std::max(angularRotationalRatio, translationalVelocityRatio); |
| 41 | if (maxRatio <= 1) { |
| 42 | return {to, false}; |
| 43 | } |
| 44 | return {from * integrate(twist, t / maxRatio), true}; |
| 45 | } |
| 46 | |
| 47 | std::ostream& operator<<(std::ostream& os, const Pose3f& pose) { |
| 48 | os << "translation: " << pose.translation().transpose() |
| 49 | << " quaternion: " << pose.rotation().coeffs().transpose(); |
| 50 | return os; |
| 51 | } |
| 52 | |
| 53 | } // namespace media |
| 54 | } // namespace android |