blob: e79e54ab70854c17faf42e96cded9bf389c3b1a7 [file] [log] [blame]
Ytai Ben-Tsvicbee7d42021-06-15 00:39:31 -07001/*
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
22using Eigen::Quaternionf;
23using Eigen::Vector3f;
24
25namespace android {
26namespace media {
27namespace {
28
29TEST(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
38TEST(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
44TEST(QuaternionUtil, RoundTripFromQuaternion) {
45 Quaternionf quaternion = Quaternionf::UnitRandom();
46 EXPECT_EQ(quaternion, rotationVectorToQuaternion(quaternionToRotationVector(quaternion)));
47}
48
49TEST(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