test_basis.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*************************************************************************/
  2. /* test_basis.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef TEST_BASIS_H
  31. #define TEST_BASIS_H
  32. #include "core/math/basis.h"
  33. #include "core/math/random_number_generator.h"
  34. #include "tests/test_macros.h"
  35. namespace TestBasis {
  36. enum RotOrder {
  37. EulerXYZ,
  38. EulerXZY,
  39. EulerYZX,
  40. EulerYXZ,
  41. EulerZXY,
  42. EulerZYX
  43. };
  44. Vector3 deg2rad(const Vector3 &p_rotation) {
  45. return p_rotation / 180.0 * Math_PI;
  46. }
  47. Vector3 rad2deg(const Vector3 &p_rotation) {
  48. return p_rotation / Math_PI * 180.0;
  49. }
  50. Basis EulerToBasis(RotOrder mode, const Vector3 &p_rotation) {
  51. Basis ret;
  52. switch (mode) {
  53. case EulerXYZ:
  54. ret.set_euler(p_rotation, Basis::EULER_ORDER_XYZ);
  55. break;
  56. case EulerXZY:
  57. ret.set_euler(p_rotation, Basis::EULER_ORDER_XZY);
  58. break;
  59. case EulerYZX:
  60. ret.set_euler(p_rotation, Basis::EULER_ORDER_YZX);
  61. break;
  62. case EulerYXZ:
  63. ret.set_euler(p_rotation, Basis::EULER_ORDER_YXZ);
  64. break;
  65. case EulerZXY:
  66. ret.set_euler(p_rotation, Basis::EULER_ORDER_ZXY);
  67. break;
  68. case EulerZYX:
  69. ret.set_euler(p_rotation, Basis::EULER_ORDER_ZYX);
  70. break;
  71. default:
  72. // If you land here, Please integrate all rotation orders.
  73. FAIL("This is not unreachable.");
  74. }
  75. return ret;
  76. }
  77. Vector3 BasisToEuler(RotOrder mode, const Basis &p_rotation) {
  78. switch (mode) {
  79. case EulerXYZ:
  80. return p_rotation.get_euler(Basis::EULER_ORDER_XYZ);
  81. case EulerXZY:
  82. return p_rotation.get_euler(Basis::EULER_ORDER_XZY);
  83. case EulerYZX:
  84. return p_rotation.get_euler(Basis::EULER_ORDER_YZX);
  85. case EulerYXZ:
  86. return p_rotation.get_euler(Basis::EULER_ORDER_YXZ);
  87. case EulerZXY:
  88. return p_rotation.get_euler(Basis::EULER_ORDER_ZXY);
  89. case EulerZYX:
  90. return p_rotation.get_euler(Basis::EULER_ORDER_ZYX);
  91. default:
  92. // If you land here, Please integrate all rotation orders.
  93. FAIL("This is not unreachable.");
  94. return Vector3();
  95. }
  96. }
  97. String get_rot_order_name(RotOrder ro) {
  98. switch (ro) {
  99. case EulerXYZ:
  100. return "XYZ";
  101. case EulerXZY:
  102. return "XZY";
  103. case EulerYZX:
  104. return "YZX";
  105. case EulerYXZ:
  106. return "YXZ";
  107. case EulerZXY:
  108. return "ZXY";
  109. case EulerZYX:
  110. return "ZYX";
  111. default:
  112. return "[Not supported]";
  113. }
  114. }
  115. void test_rotation(Vector3 deg_original_euler, RotOrder rot_order) {
  116. // This test:
  117. // 1. Converts the rotation vector from deg to rad.
  118. // 2. Converts euler to basis.
  119. // 3. Converts the above basis back into euler.
  120. // 4. Converts the above euler into basis again.
  121. // 5. Compares the basis obtained in step 2 with the basis of step 4
  122. //
  123. // The conversion "basis to euler", done in the step 3, may be different from
  124. // the original euler, even if the final rotation are the same.
  125. // This happens because there are more ways to represents the same rotation,
  126. // both valid, using eulers.
  127. // For this reason is necessary to convert that euler back to basis and finally
  128. // compares it.
  129. //
  130. // In this way we can assert that both functions: basis to euler / euler to basis
  131. // are correct.
  132. // Euler to rotation
  133. const Vector3 original_euler = deg2rad(deg_original_euler);
  134. const Basis to_rotation = EulerToBasis(rot_order, original_euler);
  135. // Euler from rotation
  136. const Vector3 euler_from_rotation = BasisToEuler(rot_order, to_rotation);
  137. const Basis rotation_from_computed_euler = EulerToBasis(rot_order, euler_from_rotation);
  138. Basis res = to_rotation.inverse() * rotation_from_computed_euler;
  139. CHECK_MESSAGE((res.get_column(0) - Vector3(1.0, 0.0, 0.0)).length() <= 0.1, vformat("Fail due to X %s\n", String(res.get_column(0))).utf8().ptr());
  140. CHECK_MESSAGE((res.get_column(1) - Vector3(0.0, 1.0, 0.0)).length() <= 0.1, vformat("Fail due to Y %s\n", String(res.get_column(1))).utf8().ptr());
  141. CHECK_MESSAGE((res.get_column(2) - Vector3(0.0, 0.0, 1.0)).length() <= 0.1, vformat("Fail due to Z %s\n", String(res.get_column(2))).utf8().ptr());
  142. // Double check `to_rotation` decomposing with XYZ rotation order.
  143. const Vector3 euler_xyz_from_rotation = to_rotation.get_euler(Basis::EULER_ORDER_XYZ);
  144. Basis rotation_from_xyz_computed_euler;
  145. rotation_from_xyz_computed_euler.set_euler(euler_xyz_from_rotation, Basis::EULER_ORDER_XYZ);
  146. res = to_rotation.inverse() * rotation_from_xyz_computed_euler;
  147. CHECK_MESSAGE((res.get_column(0) - Vector3(1.0, 0.0, 0.0)).length() <= 0.1, vformat("Double check with XYZ rot order failed, due to X %s\n", String(res.get_column(0))).utf8().ptr());
  148. CHECK_MESSAGE((res.get_column(1) - Vector3(0.0, 1.0, 0.0)).length() <= 0.1, vformat("Double check with XYZ rot order failed, due to Y %s\n", String(res.get_column(1))).utf8().ptr());
  149. CHECK_MESSAGE((res.get_column(2) - Vector3(0.0, 0.0, 1.0)).length() <= 0.1, vformat("Double check with XYZ rot order failed, due to Z %s\n", String(res.get_column(2))).utf8().ptr());
  150. INFO(vformat("Rotation order: %s\n.", get_rot_order_name(rot_order)).utf8().ptr());
  151. INFO(vformat("Original Rotation: %s\n", String(deg_original_euler)).utf8().ptr());
  152. INFO(vformat("Quaternion to rotation order: %s\n", String(rad2deg(euler_from_rotation))).utf8().ptr());
  153. }
  154. TEST_CASE("[Basis] Euler conversions") {
  155. Vector<RotOrder> rotorder_to_test;
  156. rotorder_to_test.push_back(EulerXYZ);
  157. rotorder_to_test.push_back(EulerXZY);
  158. rotorder_to_test.push_back(EulerYZX);
  159. rotorder_to_test.push_back(EulerYXZ);
  160. rotorder_to_test.push_back(EulerZXY);
  161. rotorder_to_test.push_back(EulerZYX);
  162. Vector<Vector3> vectors_to_test;
  163. // Test the special cases.
  164. vectors_to_test.push_back(Vector3(0.0, 0.0, 0.0));
  165. vectors_to_test.push_back(Vector3(0.5, 0.5, 0.5));
  166. vectors_to_test.push_back(Vector3(-0.5, -0.5, -0.5));
  167. vectors_to_test.push_back(Vector3(40.0, 40.0, 40.0));
  168. vectors_to_test.push_back(Vector3(-40.0, -40.0, -40.0));
  169. vectors_to_test.push_back(Vector3(0.0, 0.0, -90.0));
  170. vectors_to_test.push_back(Vector3(0.0, -90.0, 0.0));
  171. vectors_to_test.push_back(Vector3(-90.0, 0.0, 0.0));
  172. vectors_to_test.push_back(Vector3(0.0, 0.0, 90.0));
  173. vectors_to_test.push_back(Vector3(0.0, 90.0, 0.0));
  174. vectors_to_test.push_back(Vector3(90.0, 0.0, 0.0));
  175. vectors_to_test.push_back(Vector3(0.0, 0.0, -30.0));
  176. vectors_to_test.push_back(Vector3(0.0, -30.0, 0.0));
  177. vectors_to_test.push_back(Vector3(-30.0, 0.0, 0.0));
  178. vectors_to_test.push_back(Vector3(0.0, 0.0, 30.0));
  179. vectors_to_test.push_back(Vector3(0.0, 30.0, 0.0));
  180. vectors_to_test.push_back(Vector3(30.0, 0.0, 0.0));
  181. vectors_to_test.push_back(Vector3(0.5, 50.0, 20.0));
  182. vectors_to_test.push_back(Vector3(-0.5, -50.0, -20.0));
  183. vectors_to_test.push_back(Vector3(0.5, 0.0, 90.0));
  184. vectors_to_test.push_back(Vector3(0.5, 0.0, -90.0));
  185. vectors_to_test.push_back(Vector3(360.0, 360.0, 360.0));
  186. vectors_to_test.push_back(Vector3(-360.0, -360.0, -360.0));
  187. vectors_to_test.push_back(Vector3(-90.0, 60.0, -90.0));
  188. vectors_to_test.push_back(Vector3(90.0, 60.0, -90.0));
  189. vectors_to_test.push_back(Vector3(90.0, -60.0, -90.0));
  190. vectors_to_test.push_back(Vector3(-90.0, -60.0, -90.0));
  191. vectors_to_test.push_back(Vector3(-90.0, 60.0, 90.0));
  192. vectors_to_test.push_back(Vector3(90.0, 60.0, 90.0));
  193. vectors_to_test.push_back(Vector3(90.0, -60.0, 90.0));
  194. vectors_to_test.push_back(Vector3(-90.0, -60.0, 90.0));
  195. vectors_to_test.push_back(Vector3(60.0, 90.0, -40.0));
  196. vectors_to_test.push_back(Vector3(60.0, -90.0, -40.0));
  197. vectors_to_test.push_back(Vector3(-60.0, -90.0, -40.0));
  198. vectors_to_test.push_back(Vector3(-60.0, 90.0, 40.0));
  199. vectors_to_test.push_back(Vector3(60.0, 90.0, 40.0));
  200. vectors_to_test.push_back(Vector3(60.0, -90.0, 40.0));
  201. vectors_to_test.push_back(Vector3(-60.0, -90.0, 40.0));
  202. vectors_to_test.push_back(Vector3(-90.0, 90.0, -90.0));
  203. vectors_to_test.push_back(Vector3(90.0, 90.0, -90.0));
  204. vectors_to_test.push_back(Vector3(90.0, -90.0, -90.0));
  205. vectors_to_test.push_back(Vector3(-90.0, -90.0, -90.0));
  206. vectors_to_test.push_back(Vector3(-90.0, 90.0, 90.0));
  207. vectors_to_test.push_back(Vector3(90.0, 90.0, 90.0));
  208. vectors_to_test.push_back(Vector3(90.0, -90.0, 90.0));
  209. vectors_to_test.push_back(Vector3(20.0, 150.0, 30.0));
  210. vectors_to_test.push_back(Vector3(20.0, -150.0, 30.0));
  211. vectors_to_test.push_back(Vector3(-120.0, -150.0, 30.0));
  212. vectors_to_test.push_back(Vector3(-120.0, -150.0, -130.0));
  213. vectors_to_test.push_back(Vector3(120.0, -150.0, -130.0));
  214. vectors_to_test.push_back(Vector3(120.0, 150.0, -130.0));
  215. vectors_to_test.push_back(Vector3(120.0, 150.0, 130.0));
  216. for (int h = 0; h < rotorder_to_test.size(); h += 1) {
  217. for (int i = 0; i < vectors_to_test.size(); i += 1) {
  218. test_rotation(vectors_to_test[i], rotorder_to_test[h]);
  219. }
  220. }
  221. }
  222. TEST_CASE("[Stress][Basis] Euler conversions") {
  223. Vector<RotOrder> rotorder_to_test;
  224. rotorder_to_test.push_back(EulerXYZ);
  225. rotorder_to_test.push_back(EulerXZY);
  226. rotorder_to_test.push_back(EulerYZX);
  227. rotorder_to_test.push_back(EulerYXZ);
  228. rotorder_to_test.push_back(EulerZXY);
  229. rotorder_to_test.push_back(EulerZYX);
  230. Vector<Vector3> vectors_to_test;
  231. // Add 1000 random vectors with weirds numbers.
  232. RandomNumberGenerator rng;
  233. for (int _ = 0; _ < 1000; _ += 1) {
  234. vectors_to_test.push_back(Vector3(
  235. rng.randf_range(-1800, 1800),
  236. rng.randf_range(-1800, 1800),
  237. rng.randf_range(-1800, 1800)));
  238. }
  239. for (int h = 0; h < rotorder_to_test.size(); h += 1) {
  240. for (int i = 0; i < vectors_to_test.size(); i += 1) {
  241. test_rotation(vectors_to_test[i], rotorder_to_test[h]);
  242. }
  243. }
  244. }
  245. } // namespace TestBasis
  246. #endif // TEST_BASIS_H