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 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #include "QuaternionUtil.h" |
| 22 | #include "TestUtil.h" |
| 23 | |
| 24 | using android::media::Pose3f; |
| 25 | using Eigen::Quaternionf; |
| 26 | using Eigen::Vector3f; |
| 27 | |
| 28 | namespace android { |
| 29 | namespace media { |
| 30 | namespace { |
| 31 | |
| 32 | TEST(Pose, CtorDefault) { |
| 33 | Pose3f pose; |
| 34 | EXPECT_EQ(pose.translation(), Vector3f::Zero()); |
| 35 | EXPECT_EQ(pose.rotation(), Quaternionf::Identity()); |
| 36 | } |
| 37 | |
| 38 | TEST(Pose, CtorRotation) { |
| 39 | Quaternionf rot = Quaternionf::UnitRandom(); |
| 40 | Pose3f pose(rot); |
| 41 | EXPECT_EQ(pose.translation(), Vector3f::Zero()); |
| 42 | EXPECT_EQ(pose.rotation(), rot); |
| 43 | } |
| 44 | |
| 45 | TEST(Pose, CtorTranslation) { |
| 46 | Vector3f trans{1, 2, 3}; |
| 47 | Pose3f pose(trans); |
| 48 | EXPECT_EQ(pose.translation(), trans); |
| 49 | EXPECT_EQ(pose.rotation(), Quaternionf::Identity()); |
| 50 | } |
| 51 | |
| 52 | TEST(Pose, CtorTranslationRotation) { |
| 53 | Quaternionf rot = Quaternionf::UnitRandom(); |
| 54 | Vector3f trans{1, 2, 3}; |
| 55 | Pose3f pose(trans, rot); |
| 56 | EXPECT_EQ(pose.translation(), trans); |
| 57 | EXPECT_EQ(pose.rotation(), rot); |
| 58 | } |
| 59 | |
| 60 | TEST(Pose, Inverse) { |
| 61 | Pose3f pose({1, 2, 3}, Quaternionf::UnitRandom()); |
| 62 | EXPECT_EQ(pose.inverse() * pose, Pose3f()); |
| 63 | EXPECT_EQ(pose * pose.inverse(), Pose3f()); |
| 64 | } |
| 65 | |
| 66 | TEST(Pose, IsApprox) { |
| 67 | constexpr float eps = std::numeric_limits<float>::epsilon(); |
| 68 | |
| 69 | EXPECT_EQ(Pose3f({1, 2, 3}, rotationVectorToQuaternion({4, 5, 6})), |
| 70 | Pose3f({1 + eps, 2 + eps, 3 + eps}, |
| 71 | rotationVectorToQuaternion({4 + eps, 5 + eps, 6 + eps}))); |
| 72 | |
| 73 | EXPECT_NE(Pose3f({1, 2, 3}, rotationVectorToQuaternion({4, 5, 6})), |
| 74 | Pose3f({1.01, 2, 3}, rotationVectorToQuaternion({4, 5, 6}))); |
| 75 | |
| 76 | EXPECT_NE(Pose3f({1, 2, 3}, rotationVectorToQuaternion({4, 5, 6})), |
| 77 | Pose3f({1, 2, 3}, rotationVectorToQuaternion({4.01, 5, 6}))); |
| 78 | } |
| 79 | |
| 80 | TEST(Pose, Compose) { |
| 81 | Pose3f p1({1, 2, 3}, rotateZ(M_PI_2)); |
| 82 | Pose3f p2({4, 5, 6}, rotateX(M_PI_2)); |
| 83 | Pose3f p3({-4, 6, 9}, p1.rotation() * p2.rotation()); |
| 84 | EXPECT_EQ(p1 * p2, p3); |
| 85 | } |
| 86 | |
| 87 | TEST(Pose, MoveWithRateLimit_NoLimit) { |
| 88 | Pose3f from({1, 1, 1}, Quaternionf::Identity()); |
| 89 | Pose3f to({1, 1, 2}, rotateZ(M_PI_2)); |
| 90 | auto result = moveWithRateLimit(from, to, 1, 10, 10); |
| 91 | EXPECT_EQ(std::get<0>(result), to); |
| 92 | EXPECT_FALSE(std::get<1>(result)); |
| 93 | } |
| 94 | |
| 95 | TEST(Pose, MoveWithRateLimit_TranslationLimit) { |
| 96 | Pose3f from({1, 1, 1}, Quaternionf::Identity()); |
| 97 | Pose3f to({1, 1, 2}, rotateZ(M_PI_2)); |
| 98 | auto result = moveWithRateLimit(from, to, 1, 0.5f, 10); |
| 99 | Pose3f expected({1, 1, 1.5f}, rotateZ(M_PI_4)); |
| 100 | EXPECT_EQ(std::get<0>(result), expected); |
| 101 | EXPECT_TRUE(std::get<1>(result)); |
| 102 | } |
| 103 | |
| 104 | TEST(Pose, MoveWithRateLimit_RotationLimit) { |
| 105 | Pose3f from({1, 1, 1}, Quaternionf::Identity()); |
| 106 | Pose3f to({1, 1, 2}, rotateZ(M_PI_2)); |
| 107 | auto result = moveWithRateLimit(from, to, 1, 10, M_PI_4); |
| 108 | Pose3f expected({1, 1, 1.5f}, rotateZ(M_PI_4)); |
| 109 | EXPECT_EQ(std::get<0>(result), expected); |
| 110 | EXPECT_TRUE(std::get<1>(result)); |
| 111 | } |
| 112 | |
| 113 | } // namespace |
| 114 | } // namespace media |
| 115 | } // namespace android |