test_basis.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*************************************************************************/
  2. /* test_basis.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/random_number_generator.h"
  33. #include "core/os/os.h"
  34. #include "core/string/ustring.h"
  35. #include "tests/test_macros.h"
  36. namespace TestBasis {
  37. enum RotOrder {
  38. EulerXYZ,
  39. EulerXZY,
  40. EulerYZX,
  41. EulerYXZ,
  42. EulerZXY,
  43. EulerZYX
  44. };
  45. Vector3 deg2rad(const Vector3 &p_rotation) {
  46. return p_rotation / 180.0 * Math_PI;
  47. }
  48. Vector3 rad2deg(const Vector3 &p_rotation) {
  49. return p_rotation / Math_PI * 180.0;
  50. }
  51. Basis EulerToBasis(RotOrder mode, const Vector3 &p_rotation) {
  52. Basis ret;
  53. switch (mode) {
  54. case EulerXYZ:
  55. ret.set_euler_xyz(p_rotation);
  56. break;
  57. case EulerXZY:
  58. ret.set_euler_xzy(p_rotation);
  59. break;
  60. case EulerYZX:
  61. ret.set_euler_yzx(p_rotation);
  62. break;
  63. case EulerYXZ:
  64. ret.set_euler_yxz(p_rotation);
  65. break;
  66. case EulerZXY:
  67. ret.set_euler_zxy(p_rotation);
  68. break;
  69. case EulerZYX:
  70. ret.set_euler_zyx(p_rotation);
  71. break;
  72. default:
  73. // If you land here, Please integrate all rotation orders.
  74. FAIL("This is not unreachable.");
  75. }
  76. return ret;
  77. }
  78. Vector3 BasisToEuler(RotOrder mode, const Basis &p_rotation) {
  79. switch (mode) {
  80. case EulerXYZ:
  81. return p_rotation.get_euler_xyz();
  82. case EulerXZY:
  83. return p_rotation.get_euler_xzy();
  84. case EulerYZX:
  85. return p_rotation.get_euler_yzx();
  86. case EulerYXZ:
  87. return p_rotation.get_euler_yxz();
  88. case EulerZXY:
  89. return p_rotation.get_euler_zxy();
  90. case EulerZYX:
  91. return p_rotation.get_euler_zyx();
  92. default:
  93. // If you land here, Please integrate all rotation orders.
  94. FAIL("This is not unreachable.");
  95. return Vector3();
  96. }
  97. }
  98. String get_rot_order_name(RotOrder ro) {
  99. switch (ro) {
  100. case EulerXYZ:
  101. return "XYZ";
  102. case EulerXZY:
  103. return "XZY";
  104. case EulerYZX:
  105. return "YZX";
  106. case EulerYXZ:
  107. return "YXZ";
  108. case EulerZXY:
  109. return "ZXY";
  110. case EulerZYX:
  111. return "ZYX";
  112. default:
  113. return "[Not supported]";
  114. }
  115. }
  116. void test_rotation(Vector3 deg_original_euler, RotOrder rot_order) {
  117. // This test:
  118. // 1. Converts the rotation vector from deg to rad.
  119. // 2. Converts euler to basis.
  120. // 3. Converts the above basis back into euler.
  121. // 4. Converts the above euler into basis again.
  122. // 5. Compares the basis obtained in step 2 with the basis of step 4
  123. //
  124. // The conversion "basis to euler", done in the step 3, may be different from
  125. // the original euler, even if the final rotation are the same.
  126. // This happens because there are more ways to represents the same rotation,
  127. // both valid, using eulers.
  128. // For this reason is necessary to convert that euler back to basis and finally
  129. // compares it.
  130. //
  131. // In this way we can assert that both functions: basis to euler / euler to basis
  132. // are correct.
  133. // Euler to rotation
  134. const Vector3 original_euler = deg2rad(deg_original_euler);
  135. const Basis to_rotation = EulerToBasis(rot_order, original_euler);
  136. // Euler from rotation
  137. const Vector3 euler_from_rotation = BasisToEuler(rot_order, to_rotation);
  138. const Basis rotation_from_computed_euler = EulerToBasis(rot_order, euler_from_rotation);
  139. Basis res = to_rotation.inverse() * rotation_from_computed_euler;
  140. CHECK_MESSAGE((res.get_axis(0) - Vector3(1.0, 0.0, 0.0)).length() <= 0.1, vformat("Fail due to X %s\n", String(res.get_axis(0))).utf8().ptr());
  141. CHECK_MESSAGE((res.get_axis(1) - Vector3(0.0, 1.0, 0.0)).length() <= 0.1, vformat("Fail due to Y %s\n", String(res.get_axis(1))).utf8().ptr());
  142. CHECK_MESSAGE((res.get_axis(2) - Vector3(0.0, 0.0, 1.0)).length() <= 0.1, vformat("Fail due to Z %s\n", String(res.get_axis(2))).utf8().ptr());
  143. // Double check `to_rotation` decomposing with XYZ rotation order.
  144. const Vector3 euler_xyz_from_rotation = to_rotation.get_euler_xyz();
  145. Basis rotation_from_xyz_computed_euler;
  146. rotation_from_xyz_computed_euler.set_euler_xyz(euler_xyz_from_rotation);
  147. res = to_rotation.inverse() * rotation_from_xyz_computed_euler;
  148. CHECK_MESSAGE((res.get_axis(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_axis(0))).utf8().ptr());
  149. CHECK_MESSAGE((res.get_axis(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_axis(1))).utf8().ptr());
  150. CHECK_MESSAGE((res.get_axis(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_axis(2))).utf8().ptr());
  151. INFO(vformat("Rotation order: %s\n.", get_rot_order_name(rot_order)).utf8().ptr());
  152. INFO(vformat("Original Rotation: %s\n", String(deg_original_euler)).utf8().ptr());
  153. INFO(vformat("Quaternion to rotation order: %s\n", String(rad2deg(euler_from_rotation))).utf8().ptr());
  154. }
  155. TEST_CASE("[Basis] Euler conversions") {
  156. Vector<RotOrder> rotorder_to_test;
  157. rotorder_to_test.push_back(EulerXYZ);
  158. rotorder_to_test.push_back(EulerXZY);
  159. rotorder_to_test.push_back(EulerYZX);
  160. rotorder_to_test.push_back(EulerYXZ);
  161. rotorder_to_test.push_back(EulerZXY);
  162. rotorder_to_test.push_back(EulerZYX);
  163. Vector<Vector3> vectors_to_test;
  164. // Test the special cases.
  165. vectors_to_test.push_back(Vector3(0.0, 0.0, 0.0));
  166. vectors_to_test.push_back(Vector3(0.5, 0.5, 0.5));
  167. vectors_to_test.push_back(Vector3(-0.5, -0.5, -0.5));
  168. vectors_to_test.push_back(Vector3(40.0, 40.0, 40.0));
  169. vectors_to_test.push_back(Vector3(-40.0, -40.0, -40.0));
  170. vectors_to_test.push_back(Vector3(0.0, 0.0, -90.0));
  171. vectors_to_test.push_back(Vector3(0.0, -90.0, 0.0));
  172. vectors_to_test.push_back(Vector3(-90.0, 0.0, 0.0));
  173. vectors_to_test.push_back(Vector3(0.0, 0.0, 90.0));
  174. vectors_to_test.push_back(Vector3(0.0, 90.0, 0.0));
  175. vectors_to_test.push_back(Vector3(90.0, 0.0, 0.0));
  176. vectors_to_test.push_back(Vector3(0.0, 0.0, -30.0));
  177. vectors_to_test.push_back(Vector3(0.0, -30.0, 0.0));
  178. vectors_to_test.push_back(Vector3(-30.0, 0.0, 0.0));
  179. vectors_to_test.push_back(Vector3(0.0, 0.0, 30.0));
  180. vectors_to_test.push_back(Vector3(0.0, 30.0, 0.0));
  181. vectors_to_test.push_back(Vector3(30.0, 0.0, 0.0));
  182. vectors_to_test.push_back(Vector3(0.5, 50.0, 20.0));
  183. vectors_to_test.push_back(Vector3(-0.5, -50.0, -20.0));
  184. vectors_to_test.push_back(Vector3(0.5, 0.0, 90.0));
  185. vectors_to_test.push_back(Vector3(0.5, 0.0, -90.0));
  186. vectors_to_test.push_back(Vector3(360.0, 360.0, 360.0));
  187. vectors_to_test.push_back(Vector3(-360.0, -360.0, -360.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(-90.0, -60.0, 90.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(-60.0, -90.0, 40.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(90.0, -90.0, 90.0));
  210. vectors_to_test.push_back(Vector3(20.0, 150.0, 30.0));
  211. vectors_to_test.push_back(Vector3(20.0, -150.0, 30.0));
  212. vectors_to_test.push_back(Vector3(-120.0, -150.0, 30.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. vectors_to_test.push_back(Vector3(120.0, 150.0, 130.0));
  217. for (int h = 0; h < rotorder_to_test.size(); h += 1) {
  218. for (int i = 0; i < vectors_to_test.size(); i += 1) {
  219. test_rotation(vectors_to_test[i], rotorder_to_test[h]);
  220. }
  221. }
  222. }
  223. TEST_CASE("[Stress][Basis] Euler conversions") {
  224. Vector<RotOrder> rotorder_to_test;
  225. rotorder_to_test.push_back(EulerXYZ);
  226. rotorder_to_test.push_back(EulerXZY);
  227. rotorder_to_test.push_back(EulerYZX);
  228. rotorder_to_test.push_back(EulerYXZ);
  229. rotorder_to_test.push_back(EulerZXY);
  230. rotorder_to_test.push_back(EulerZYX);
  231. Vector<Vector3> vectors_to_test;
  232. // Add 1000 random vectors with weirds numbers.
  233. RandomNumberGenerator rng;
  234. for (int _ = 0; _ < 1000; _ += 1) {
  235. vectors_to_test.push_back(Vector3(
  236. rng.randf_range(-1800, 1800),
  237. rng.randf_range(-1800, 1800),
  238. rng.randf_range(-1800, 1800)));
  239. }
  240. for (int h = 0; h < rotorder_to_test.size(); h += 1) {
  241. for (int i = 0; i < vectors_to_test.size(); i += 1) {
  242. test_rotation(vectors_to_test[i], rotorder_to_test[h]);
  243. }
  244. }
  245. }
  246. } // namespace TestBasis
  247. #endif