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 <gtest/gtest.h>
18 
19 #include "QuaternionUtil.h"
20 #include "TestUtil.h"
21 
22 using Eigen::Quaternionf;
23 using Eigen::Vector3f;
24 
25 namespace android {
26 namespace media {
27 namespace {
28 
TEST(QuaternionUtil,RotationVectorToQuaternion)29 TEST(QuaternionUtil, RotationVectorToQuaternion) {
30     // 90 degrees around Z.
31     Vector3f rot = {0, 0, M_PI_2};
32     Quaternionf quat = rotationVectorToQuaternion(rot);
33     ASSERT_EQ(quat * Vector3f(1, 0, 0), Vector3f(0, 1, 0));
34     ASSERT_EQ(quat * Vector3f(0, 1, 0), Vector3f(-1, 0, 0));
35     ASSERT_EQ(quat * Vector3f(0, 0, 1), Vector3f(0, 0, 1));
36 }
37 
TEST(QuaternionUtil,QuaternionToRotationVector)38 TEST(QuaternionUtil, QuaternionToRotationVector) {
39     Quaternionf quat = Quaternionf::FromTwoVectors(Vector3f(1, 0, 0), Vector3f(0, 1, 0));
40     Vector3f rot = quaternionToRotationVector(quat);
41     ASSERT_EQ(rot, Vector3f(0, 0, M_PI_2));
42 }
43 
TEST(QuaternionUtil,RoundTripFromQuaternion)44 TEST(QuaternionUtil, RoundTripFromQuaternion) {
45     Quaternionf quaternion = Quaternionf::UnitRandom();
46     EXPECT_EQ(quaternion, rotationVectorToQuaternion(quaternionToRotationVector(quaternion)));
47 }
48 
TEST(QuaternionUtil,RoundTripFromVector)49 TEST(QuaternionUtil, RoundTripFromVector) {
50     Vector3f vec{0.1, 0.2, 0.3};
51     EXPECT_EQ(vec, quaternionToRotationVector(rotationVectorToQuaternion(vec)));
52 }
53 
54 }  // namespace
55 }  // namespace media
56 }  // namespace android
57