File size: 2,253 Bytes
e3df007
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Copyright (c) Meta Platforms, Inc. and affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from animated_drawings.model.quaternions import Quaternions
from animated_drawings.model.vectors import Vectors
import numpy as np


def test_not_four_elements():
    try:
        Quaternions(np.array([0, 1, 2]))  # should fail
    except AssertionError:
        return
    assert False


def test_initialize_with_ndarray():
    q = Quaternions(np.array([1, 0, 0, 0]))  # should succeed
    assert np.array_equal(q.qs, np.array([[1, 0, 0, 0]]))


def test_from_angle_axis():
    angle = np.array([1.0])
    axis = Vectors(np.array([1.0, 1.0, 1.0]))
    q6 = Quaternions.from_angle_axis(angle, axis)
    assert np.allclose(q6.qs,  np.array(
        [[0.87758256, 0.27679646, 0.27679646, 0.27679646]]))


def test_multiple_from_angle_axis():
    angles = np.array([[1.0], [1.0]])
    axis = Vectors(
        np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], dtype=np.float32))
    q1 = Quaternions.from_angle_axis(angles, axis)
    assert np.allclose(q1.qs,  np.array([
        [0.87758256, 0.27679646, 0.27679646, 0.27679646],
        [0.87758256, 0.27679646, 0.27679646, 0.27679646]]))


def test_to_rotation_matrix():
    angles = np.array([[np.pi / 2]])
    axis = Vectors(np.array([1.0, 0.0, 0.0], dtype=np.float32))
    q1 = Quaternions.from_angle_axis(angles, axis)
    assert np.allclose(q1.to_rotation_matrix(), np.array([
        [1.000000e+00,  0.000000e+00,  0.000000e+00,  0.000000e+00],
        [0.000000e+00,  0.000000e+00, -1.000000e+00,  0.000000e+00],
        [0.000000e+00,  1.000000e+00,  0.000000e+00,  0.000000e+00],
        [0.000000e+00,  0.000000e+00,  0.000000e+00,  1.000000e+00]]))


def test_from_rotation_matrix():
    angles = np.array([[np.pi / 2]])
    axis = np.array([1.0, 1.0, 0.0], dtype=np.float32)
    q1 = Quaternions.from_angle_axis(angles, Vectors(axis))
    q2 = Quaternions.from_rotation_matrix(q1.to_rotation_matrix())
    assert np.allclose(q1.qs, q2.qs)


def test_to_euler_angles():
    # TODO add test coverage for from_euler_angles
    pass


def test_multiply():
    # TODO add test coverage for quaternion multiplication
    pass