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