Blender  V3.3
abc_matrix_test.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include "testing/testing.h"
4 
5 /* Keep first since utildefines defines AT which conflicts with STL. */
7 
8 #include "BLI_math.h"
9 #include "BLI_utildefines.h"
10 
11 namespace blender::io::alembic {
12 
13 TEST(abc_matrix, CreateRotationMatrixY_YfromZ)
14 {
15  /* Input variables */
16  float rot_x_mat[3][3];
17  float rot_y_mat[3][3];
18  float rot_z_mat[3][3];
19  float euler[3] = {0.0f, M_PI_4, 0.0f};
20 
21  /* Construct expected matrices */
22  float unit[3][3];
23  float rot_z_min_quart_pi[3][3]; /* rotation of -pi/4 radians over z-axis */
24 
25  unit_m3(unit);
26  unit_m3(rot_z_min_quart_pi);
27  rot_z_min_quart_pi[0][0] = M_SQRT1_2;
28  rot_z_min_quart_pi[0][1] = -M_SQRT1_2;
29  rot_z_min_quart_pi[1][0] = M_SQRT1_2;
30  rot_z_min_quart_pi[1][1] = M_SQRT1_2;
31 
32  /* Run tests */
33  create_swapped_rotation_matrix(rot_x_mat, rot_y_mat, rot_z_mat, euler, ABC_YUP_FROM_ZUP);
34 
35  EXPECT_M3_NEAR(rot_x_mat, unit, 1e-5f);
36  EXPECT_M3_NEAR(rot_y_mat, unit, 1e-5f);
37  EXPECT_M3_NEAR(rot_z_mat, rot_z_min_quart_pi, 1e-5f);
38 }
39 
40 TEST(abc_matrix, CreateRotationMatrixZ_YfromZ)
41 {
42  /* Input variables */
43  float rot_x_mat[3][3];
44  float rot_y_mat[3][3];
45  float rot_z_mat[3][3];
46  float euler[3] = {0.0f, 0.0f, M_PI_4};
47 
48  /* Construct expected matrices */
49  float unit[3][3];
50  float rot_y_quart_pi[3][3]; /* rotation of pi/4 radians over y-axis */
51 
52  unit_m3(unit);
53  unit_m3(rot_y_quart_pi);
54  rot_y_quart_pi[0][0] = M_SQRT1_2;
55  rot_y_quart_pi[0][2] = -M_SQRT1_2;
56  rot_y_quart_pi[2][0] = M_SQRT1_2;
57  rot_y_quart_pi[2][2] = M_SQRT1_2;
58 
59  /* Run tests */
60  create_swapped_rotation_matrix(rot_x_mat, rot_y_mat, rot_z_mat, euler, ABC_YUP_FROM_ZUP);
61 
62  EXPECT_M3_NEAR(rot_x_mat, unit, 1e-5f);
63  EXPECT_M3_NEAR(rot_y_mat, rot_y_quart_pi, 1e-5f);
64  EXPECT_M3_NEAR(rot_z_mat, unit, 1e-5f);
65 }
66 
67 TEST(abc_matrix, CreateRotationMatrixXYZ_YfromZ)
68 {
69  /* Input variables */
70  float rot_x_mat[3][3];
71  float rot_y_mat[3][3];
72  float rot_z_mat[3][3];
73  /* in degrees: X=10, Y=20, Z=30 */
74  float euler[3] = {0.17453292012214f, 0.34906581044197f, 0.52359879016876f};
75 
76  /* Construct expected matrices */
77  float rot_x_p10[3][3]; /* rotation of +10 degrees over x-axis */
78  float rot_y_p30[3][3]; /* rotation of +30 degrees over y-axis */
79  float rot_z_m20[3][3]; /* rotation of -20 degrees over z-axis */
80 
81  unit_m3(rot_x_p10);
82  rot_x_p10[1][1] = 0.9848077297210693f;
83  rot_x_p10[1][2] = 0.1736481785774231f;
84  rot_x_p10[2][1] = -0.1736481785774231f;
85  rot_x_p10[2][2] = 0.9848077297210693f;
86 
87  unit_m3(rot_y_p30);
88  rot_y_p30[0][0] = 0.8660253882408142f;
89  rot_y_p30[0][2] = -0.5f;
90  rot_y_p30[2][0] = 0.5f;
91  rot_y_p30[2][2] = 0.8660253882408142f;
92 
93  unit_m3(rot_z_m20);
94  rot_z_m20[0][0] = 0.9396926164627075f;
95  rot_z_m20[0][1] = -0.3420201241970062f;
96  rot_z_m20[1][0] = 0.3420201241970062f;
97  rot_z_m20[1][1] = 0.9396926164627075f;
98 
99  /* Run tests */
100  create_swapped_rotation_matrix(rot_x_mat, rot_y_mat, rot_z_mat, euler, ABC_YUP_FROM_ZUP);
101 
102  EXPECT_M3_NEAR(rot_x_mat, rot_x_p10, 1e-5f);
103  EXPECT_M3_NEAR(rot_y_mat, rot_y_p30, 1e-5f);
104  EXPECT_M3_NEAR(rot_z_mat, rot_z_m20, 1e-5f);
105 }
106 
107 TEST(abc_matrix, CreateRotationMatrixXYZ_ZfromY)
108 {
109  /* Input variables */
110  float rot_x_mat[3][3];
111  float rot_y_mat[3][3];
112  float rot_z_mat[3][3];
113  /* in degrees: X=10, Y=20, Z=30 */
114  float euler[3] = {0.1745329201221466f, 0.3490658104419708f, 0.5235987901687622f};
115 
116  /* Construct expected matrices */
117  float rot_x_p10[3][3]; /* rotation of +10 degrees over x-axis */
118  float rot_y_m30[3][3]; /* rotation of -30 degrees over y-axis */
119  float rot_z_p20[3][3]; /* rotation of +20 degrees over z-axis */
120 
121  unit_m3(rot_x_p10);
122  rot_x_p10[1][1] = 0.9848077297210693f;
123  rot_x_p10[1][2] = 0.1736481785774231f;
124  rot_x_p10[2][1] = -0.1736481785774231f;
125  rot_x_p10[2][2] = 0.9848077297210693f;
126 
127  unit_m3(rot_y_m30);
128  rot_y_m30[0][0] = 0.8660253882408142f;
129  rot_y_m30[0][2] = 0.5f;
130  rot_y_m30[2][0] = -0.5f;
131  rot_y_m30[2][2] = 0.8660253882408142f;
132 
133  unit_m3(rot_z_p20);
134  rot_z_p20[0][0] = 0.9396926164627075f;
135  rot_z_p20[0][1] = 0.3420201241970062f;
136  rot_z_p20[1][0] = -0.3420201241970062f;
137  rot_z_p20[1][1] = 0.9396926164627075f;
138 
139  /* Run tests */
140  create_swapped_rotation_matrix(rot_x_mat, rot_y_mat, rot_z_mat, euler, ABC_ZUP_FROM_YUP);
141 
142  EXPECT_M3_NEAR(rot_x_mat, rot_x_p10, 1e-5f);
143  EXPECT_M3_NEAR(rot_y_mat, rot_y_m30, 1e-5f);
144  EXPECT_M3_NEAR(rot_z_mat, rot_z_p20, 1e-5f);
145 }
146 
147 TEST(abc_matrix, CopyM44AxisSwap_YfromZ)
148 {
149  float result[4][4];
150 
151  /* Construct an input matrix that performs a rotation like the tests
152  * above. This matrix was created by rotating a cube in Blender over
153  * (X=10, Y=20, Z=30 degrees in XYZ order) and translating over (1, 2, 3) */
154  float input[4][4] = {
155  {0.81379765272f, 0.4698463380336f, -0.342020124197f, 0.0f},
156  {-0.44096961617f, 0.8825641274452f, 0.163175910711f, 0.0f},
157  {0.37852230668f, 0.0180283170193f, 0.925416588783f, 0.0f},
158  {1.0f, 2.0f, 3.0f, 1.0f},
159  };
160 
162 
163  /* Check the resulting rotation & translation. */
164  float trans[4] = {1.0f, 3.0f, -2.0f, 1.0f};
165  EXPECT_V4_NEAR(trans, result[3], 1e-5f);
166 
167  /* This matrix was created by rotating a cube in Blender over
168  * (X=10, Y=30, Z=-20 degrees in XZY order) and translating over (1, 3, -2) */
169  float expect[4][4] = {
170  {0.813797652721f, -0.342020124197f, -0.469846338033f, 0.0f},
171  {0.378522306680f, 0.925416588783f, -0.018028317019f, 0.0f},
172  {0.440969616174f, -0.163175910711f, 0.882564127445f, 0.0f},
173  {1.0f, 3.0f, -2.0f, 1.0f},
174  };
175  EXPECT_M4_NEAR(expect, result, 1e-5f);
176 }
177 
178 TEST(abc_matrix, CopyM44AxisSwapWithScale_YfromZ)
179 {
180  float result[4][4];
181 
182  /* Construct an input matrix that performs a rotation like the tests
183  * above. This matrix was created by rotating a cube in Blender over
184  * (X=10, Y=20, Z=30 degrees in XYZ order), translating over (1, 2, 3),
185  * and scaling by (4, 5, 6). */
186  float input[4][4] = {
187  {3.25519061088f, 1.8793853521347f, -1.368080496788f, 0.0f},
188  {-2.20484805107f, 4.4128208160400f, 0.815879583358f, 0.0f},
189  {2.27113389968f, 0.1081698983907f, 5.552499771118f, 0.0f},
190  {1.0f, 2.0f, 3.0f, 1.0f},
191  };
192 
194 
195  /* This matrix was created by rotating a cube in Blender over
196  * (X=10, Y=30, Z=-20 degrees in XZY order), translating over (1, 3, -2)
197  * and scaling over (4, 6, 5). */
198  float expect[4][4] = {
199  {3.255190610885f, -1.368080496788f, -1.879385352134f, 0.0f},
200  {2.271133899688f, 5.552499771118f, -0.108169898390f, 0.0f},
201  {2.204848051071f, -0.815879583358f, 4.412820816040f, 0.0f},
202  {1.0f, 3.0f, -2.0f, 1.0f},
203  };
204  EXPECT_M4_NEAR(expect, result, 1e-5f);
205 }
206 
207 TEST(abc_matrix, CopyM44AxisSwap_ZfromY)
208 {
209  float result[4][4];
210 
211  /* This matrix was created by rotating a cube in Blender over
212  * (X=10, Y=30, Z=-20 degrees in XZY order) and translating over (1, 3, -2) */
213  float input[4][4] = {
214  {0.813797652721f, -0.342020124197f, -0.469846338033f, 0.0f},
215  {0.378522306680f, 0.925416588783f, -0.018028317019f, 0.0f},
216  {0.440969616174f, -0.163175910711f, 0.882564127445f, 0.0f},
217  {1.0f, 3.0f, -2.0f, 1.0f},
218  };
219 
221 
222  /* This matrix was created by rotating a cube in Blender over
223  * (X=10, Y=20, Z=30 degrees in XYZ order) and translating over (1, 2, 3) */
224  float expect[4][4] = {
225  {0.813797652721f, 0.469846338033f, -0.342020124197f, 0.0f},
226  {-0.44096961617f, 0.882564127445f, 0.163175910711f, 0.0f},
227  {0.378522306680f, 0.018028317019f, 0.925416588783f, 0.0f},
228  {1.0f, 2.0f, 3.0f, 1.0f},
229  };
230 
231  EXPECT_M4_NEAR(expect, result, 1e-5f);
232 }
233 
234 TEST(abc_matrix, CopyM44AxisSwapWithScale_ZfromY)
235 {
236  float result[4][4];
237 
238  /* This matrix was created by rotating a cube in Blender over
239  * (X=10, Y=30, Z=-20 degrees in XZY order), translating over (1, 3, -2)
240  * and scaling over (4, 6, 5). */
241  float input[4][4] = {
242  {3.2551906108f, -1.36808049678f, -1.879385352134f, 0.0f},
243  {2.2711338996f, 5.55249977111f, -0.108169898390f, 0.0f},
244  {2.2048480510f, -0.81587958335f, 4.412820816040f, 0.0f},
245  {1.0f, 3.0f, -2.0f, 1.0f},
246  };
247 
249 
250  /* This matrix was created by rotating a cube in Blender over
251  * (X=10, Y=20, Z=30 degrees in XYZ order), translating over (1, 2, 3),
252  * and scaling by (4, 5, 6). */
253  float expect[4][4] = {
254  {3.25519061088f, 1.879385352134f, -1.36808049678f, 0.0f},
255  {-2.2048480510f, 4.412820816040f, 0.81587958335f, 0.0f},
256  {2.27113389968f, 0.108169898390f, 5.55249977111f, 0.0f},
257  {1.0f, 2.0f, 3.0f, 1.0f},
258  };
259 
260  EXPECT_M4_NEAR(expect, result, 1e-5f);
261 }
262 
263 TEST(abc_matrix, CopyM44AxisSwapWithScale_gimbal_ZfromY)
264 {
265  float result[4][4];
266 
267  /* This matrix represents a rotation over (-90, -0, -0) degrees,
268  * and a translation over (-0, -0.1, -0). It is in Y=up. */
269  float input[4][4] = {
270  {1.000f, 0.000f, 0.000f, 0.000f},
271  {0.000f, 0.000f, -1.000f, 0.000f},
272  {0.000f, 1.000f, 0.000f, 0.000f},
273  {-0.000f, -0.100f, -0.000f, 1.000f},
274  };
275 
277 
278  /* Since the rotation is only over the X-axis, it should not change.
279  * The translation does change. */
280  float expect[4][4] = {
281  {1.000f, 0.000f, 0.000f, 0.000f},
282  {0.000f, 0.000f, -1.000f, 0.000f},
283  {0.000f, 1.000f, 0.000f, 0.000f},
284  {-0.000f, 0.000f, -0.100f, 1.000f},
285  };
286 
287  EXPECT_M4_NEAR(expect, result, 1e-5f);
288 }
289 
290 } // namespace blender::io::alembic
#define M_SQRT1_2
Definition: BLI_math_base.h:32
#define M_PI_4
Definition: BLI_math_base.h:26
void unit_m3(float m[3][3])
Definition: math_matrix.c:40
ccl_global KernelShaderEvalInput * input
void create_swapped_rotation_matrix(float rot_x_mat[3][3], float rot_y_mat[3][3], float rot_z_mat[3][3], const float euler[3], AbcAxisSwapMode mode)
void copy_m44_axis_swap(float dst_mat[4][4], float src_mat[4][4], AbcAxisSwapMode mode)
TEST(abc_matrix, CreateRotationMatrixY_YfromZ)