Alembic: Renamed create_rotation_matrix to create_swapped_rotation_matrix and more:
[blender.git] / tests / gtests / alembic / abc_matrix_test.cc
1 #include "testing/testing.h"
2
3 // Keep first since utildefines defines AT which conflicts with fucking STL
4 #include "intern/abc_util.h"
5
6 extern "C" {
7 #include "BLI_utildefines.h"
8 #include "BLI_math.h"
9 }
10
11
12 #define EXPECT_M3_NEAR(a, b, eps) {\
13         EXPECT_V3_NEAR(a[0], b[0], eps); \
14         EXPECT_V3_NEAR(a[1], b[1], eps); \
15         EXPECT_V3_NEAR(a[2], b[2], eps); \
16 }
17
18 TEST(abc_matrix, CreateRotationMatrixY_YfromZ) {
19         // Input variables
20         float rot_x_mat[3][3];
21         float rot_y_mat[3][3];
22         float rot_z_mat[3][3];
23         float euler[3] = {0.f, M_PI_4, 0.f};
24
25         // Construct expected matrices
26         float unit[3][3];
27         float rot_z_min_quart_pi[3][3];  // rotation of -pi/4 radians over z-axis
28
29         unit_m3(unit);
30         unit_m3(rot_z_min_quart_pi);
31         rot_z_min_quart_pi[0][0] = M_SQRT1_2;
32         rot_z_min_quart_pi[0][1] = -M_SQRT1_2;
33         rot_z_min_quart_pi[1][0] = M_SQRT1_2;
34         rot_z_min_quart_pi[1][1] = M_SQRT1_2;
35
36         // Run tests
37         create_swapped_rotation_matrix(rot_x_mat, rot_y_mat, rot_z_mat, euler,
38                                        ABC_YUP_FROM_ZUP);
39
40         EXPECT_M3_NEAR(rot_x_mat, unit, 1e-5f);
41         EXPECT_M3_NEAR(rot_y_mat, unit, 1e-5f);
42         EXPECT_M3_NEAR(rot_z_mat, rot_z_min_quart_pi, 1e-5f);
43 }
44
45 TEST(abc_matrix, CreateRotationMatrixZ_YfromZ) {
46         // Input variables
47         float rot_x_mat[3][3];
48         float rot_y_mat[3][3];
49         float rot_z_mat[3][3];
50         float euler[3] = {0.f, 0.f, M_PI_4};
51
52         // Construct expected matrices
53         float unit[3][3];
54         float rot_y_quart_pi[3][3];  // rotation of pi/4 radians over y-axis
55
56         unit_m3(unit);
57         unit_m3(rot_y_quart_pi);
58         rot_y_quart_pi[0][0] = M_SQRT1_2;
59         rot_y_quart_pi[0][2] = -M_SQRT1_2;
60         rot_y_quart_pi[2][0] = M_SQRT1_2;
61         rot_y_quart_pi[2][2] = M_SQRT1_2;
62
63         // Run tests
64         create_swapped_rotation_matrix(rot_x_mat, rot_y_mat, rot_z_mat, euler,
65                                        ABC_YUP_FROM_ZUP);
66
67         EXPECT_M3_NEAR(rot_x_mat, unit, 1e-5f);
68         EXPECT_M3_NEAR(rot_y_mat, rot_y_quart_pi, 1e-5f);
69         EXPECT_M3_NEAR(rot_z_mat, unit, 1e-5f);
70 }
71
72 TEST(abc_matrix, CreateRotationMatrixXYZ_YfromZ) {
73         // Input variables
74         float rot_x_mat[3][3];
75         float rot_y_mat[3][3];
76         float rot_z_mat[3][3];
77         // in degrees: X=10, Y=20, Z=30
78         float euler[3] = {0.1745329201221466f, 0.3490658104419708f, 0.5235987901687622f};
79
80         // Construct expected matrices
81         float rot_x_p10[3][3];  // rotation of +10 degrees over x-axis
82         float rot_y_p30[3][3];  // rotation of +30 degrees over y-axis
83         float rot_z_m20[3][3];  // rotation of -20 degrees over z-axis
84
85         unit_m3(rot_x_p10);
86         rot_x_p10[1][1] =  0.9848077297210693f;
87         rot_x_p10[1][2] =  0.1736481785774231f;
88         rot_x_p10[2][1] = -0.1736481785774231f;
89         rot_x_p10[2][2] =  0.9848077297210693f;
90
91         unit_m3(rot_y_p30);
92         rot_y_p30[0][0] =  0.8660253882408142f;
93         rot_y_p30[0][2] = -0.5f;
94         rot_y_p30[2][0] =  0.5f;
95         rot_y_p30[2][2] =  0.8660253882408142f;
96
97         unit_m3(rot_z_m20);
98         rot_z_m20[0][0] =  0.9396926164627075f;
99         rot_z_m20[0][1] = -0.3420201241970062f;
100         rot_z_m20[1][0] =  0.3420201241970062f;
101         rot_z_m20[1][1] =  0.9396926164627075f;
102
103         // Run tests
104         create_swapped_rotation_matrix(rot_x_mat, rot_y_mat, rot_z_mat, euler,
105                                        ABC_YUP_FROM_ZUP);
106
107         EXPECT_M3_NEAR(rot_x_mat, rot_x_p10, 1e-5f);
108         EXPECT_M3_NEAR(rot_y_mat, rot_y_p30, 1e-5f);
109         EXPECT_M3_NEAR(rot_z_mat, rot_z_m20, 1e-5f);
110 }
111
112 TEST(abc_matrix, CreateRotationMatrixXYZ_ZfromY) {
113         // Input variables
114         float rot_x_mat[3][3];
115         float rot_y_mat[3][3];
116         float rot_z_mat[3][3];
117         // in degrees: X=10, Y=20, Z=30
118         float euler[3] = {0.1745329201221466f, 0.3490658104419708f, 0.5235987901687622f};
119
120         // Construct expected matrices
121         float rot_x_p10[3][3];  // rotation of +10 degrees over x-axis
122         float rot_y_m30[3][3];  // rotation of -30 degrees over y-axis
123         float rot_z_p20[3][3];  // rotation of +20 degrees over z-axis
124
125         unit_m3(rot_x_p10);
126         rot_x_p10[1][1] =  0.9848077297210693f;
127         rot_x_p10[1][2] =  0.1736481785774231f;
128         rot_x_p10[2][1] = -0.1736481785774231f;
129         rot_x_p10[2][2] =  0.9848077297210693f;
130
131         unit_m3(rot_y_m30);
132         rot_y_m30[0][0] =  0.8660253882408142f;
133         rot_y_m30[0][2] =  0.5f;
134         rot_y_m30[2][0] = -0.5f;
135         rot_y_m30[2][2] =  0.8660253882408142f;
136
137         unit_m3(rot_z_p20);
138         rot_z_p20[0][0] =  0.9396926164627075f;
139         rot_z_p20[0][1] =  0.3420201241970062f;
140         rot_z_p20[1][0] = -0.3420201241970062f;
141         rot_z_p20[1][1] =  0.9396926164627075f;
142
143         // Run tests
144         create_swapped_rotation_matrix(rot_x_mat, rot_y_mat, rot_z_mat, euler,
145                                        ABC_ZUP_FROM_YUP);
146
147         EXPECT_M3_NEAR(rot_x_mat, rot_x_p10, 1e-5f);
148         EXPECT_M3_NEAR(rot_y_mat, rot_y_m30, 1e-5f);
149         EXPECT_M3_NEAR(rot_z_mat, rot_z_p20, 1e-5f);
150 }