abc_matrix_test.cc 9.1 KB

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