test_basis.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*************************************************************************/
  2. /* test_basis.cpp */
  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. #include "test_basis.h"
  31. #include "core/math/random_number_generator.h"
  32. #include "core/os/os.h"
  33. #include "core/ustring.h"
  34. namespace TestBasis {
  35. enum RotOrder {
  36. EulerXYZ,
  37. EulerXZY,
  38. EulerYZX,
  39. EulerYXZ,
  40. EulerZXY,
  41. EulerZYX
  42. };
  43. Vector3 deg2rad(const Vector3 &p_rotation) {
  44. return p_rotation / 180.0 * Math_PI;
  45. }
  46. Vector3 rad2deg(const Vector3 &p_rotation) {
  47. return p_rotation / Math_PI * 180.0;
  48. }
  49. Basis EulerToBasis(RotOrder mode, const Vector3 &p_rotation) {
  50. Basis ret;
  51. switch (mode) {
  52. case EulerXYZ:
  53. ret.set_euler_xyz(p_rotation);
  54. break;
  55. case EulerXZY:
  56. ret.set_euler_xzy(p_rotation);
  57. break;
  58. case EulerYZX:
  59. ret.set_euler_yzx(p_rotation);
  60. break;
  61. case EulerYXZ:
  62. ret.set_euler_yxz(p_rotation);
  63. break;
  64. case EulerZXY:
  65. ret.set_euler_zxy(p_rotation);
  66. break;
  67. case EulerZYX:
  68. ret.set_euler_zyx(p_rotation);
  69. break;
  70. default:
  71. // If you land here, Please integrate all rotation orders.
  72. CRASH_NOW_MSG("This is not unreachable.");
  73. }
  74. return ret;
  75. }
  76. Vector3 BasisToEuler(RotOrder mode, const Basis &p_rotation) {
  77. switch (mode) {
  78. case EulerXYZ:
  79. return p_rotation.get_euler_xyz();
  80. case EulerXZY:
  81. return p_rotation.get_euler_xzy();
  82. case EulerYZX:
  83. return p_rotation.get_euler_yzx();
  84. case EulerYXZ:
  85. return p_rotation.get_euler_yxz();
  86. case EulerZXY:
  87. return p_rotation.get_euler_zxy();
  88. case EulerZYX:
  89. return p_rotation.get_euler_zyx();
  90. default:
  91. // If you land here, Please integrate all rotation orders.
  92. CRASH_NOW_MSG("This is not unreachable.");
  93. return Vector3();
  94. }
  95. }
  96. String get_rot_order_name(RotOrder ro) {
  97. switch (ro) {
  98. case EulerXYZ:
  99. return "XYZ";
  100. case EulerXZY:
  101. return "XZY";
  102. case EulerYZX:
  103. return "YZX";
  104. case EulerYXZ:
  105. return "YXZ";
  106. case EulerZXY:
  107. return "ZXY";
  108. case EulerZYX:
  109. return "ZYX";
  110. default:
  111. return "[Not supported]";
  112. }
  113. }
  114. bool test_rotation(Vector3 deg_original_euler, RotOrder rot_order) {
  115. // This test:
  116. // 1. Converts the rotation vector from deg to rad.
  117. // 2. Converts euler to basis.
  118. // 3. Converts the above basis back into euler.
  119. // 4. Converts the above euler into basis again.
  120. // 5. Compares the basis obtained in step 2 with the basis of step 4
  121. //
  122. // The conversion "basis to euler", done in the step 3, may be different from
  123. // the original euler, even if the final rotation are the same.
  124. // This happens because there are more ways to represents the same rotation,
  125. // both valid, using eulers.
  126. // For this reason is necessary to convert that euler back to basis and finally
  127. // compares it.
  128. //
  129. // In this way we can assert that both functions: basis to euler / euler to basis
  130. // are correct.
  131. bool pass = true;
  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. if ((res.get_axis(0) - Vector3(1.0, 0.0, 0.0)).length() > 0.1) {
  140. OS::get_singleton()->print("Fail due to X %ls\n", String(res.get_axis(0)).c_str());
  141. pass = false;
  142. }
  143. if ((res.get_axis(1) - Vector3(0.0, 1.0, 0.0)).length() > 0.1) {
  144. OS::get_singleton()->print("Fail due to Y %ls\n", String(res.get_axis(1)).c_str());
  145. pass = false;
  146. }
  147. if ((res.get_axis(2) - Vector3(0.0, 0.0, 1.0)).length() > 0.1) {
  148. OS::get_singleton()->print("Fail due to Z %ls\n", String(res.get_axis(2)).c_str());
  149. pass = false;
  150. }
  151. if (pass) {
  152. // Double check `to_rotation` decomposing with XYZ rotation order.
  153. const Vector3 euler_xyz_from_rotation = to_rotation.get_euler_xyz();
  154. Basis rotation_from_xyz_computed_euler;
  155. rotation_from_xyz_computed_euler.set_euler_xyz(euler_xyz_from_rotation);
  156. res = to_rotation.inverse() * rotation_from_xyz_computed_euler;
  157. if ((res.get_axis(0) - Vector3(1.0, 0.0, 0.0)).length() > 0.1) {
  158. OS::get_singleton()->print("Double check with XYZ rot order failed, due to X %ls\n", String(res.get_axis(0)).c_str());
  159. pass = false;
  160. }
  161. if ((res.get_axis(1) - Vector3(0.0, 1.0, 0.0)).length() > 0.1) {
  162. OS::get_singleton()->print("Double check with XYZ rot order failed, due to Y %ls\n", String(res.get_axis(1)).c_str());
  163. pass = false;
  164. }
  165. if ((res.get_axis(2) - Vector3(0.0, 0.0, 1.0)).length() > 0.1) {
  166. OS::get_singleton()->print("Double check with XYZ rot order failed, due to Z %ls\n", String(res.get_axis(2)).c_str());
  167. pass = false;
  168. }
  169. }
  170. if (pass == false) {
  171. // Print phase only if not pass.
  172. OS *os = OS::get_singleton();
  173. os->print("Rotation order: %ls\n.", get_rot_order_name(rot_order).c_str());
  174. os->print("Original Rotation: %ls\n", String(deg_original_euler).c_str());
  175. os->print("Quaternion to rotation order: %ls\n", String(rad2deg(euler_from_rotation)).c_str());
  176. }
  177. return pass;
  178. }
  179. void test_euler_conversion() {
  180. Vector<RotOrder> rotorder_to_test;
  181. rotorder_to_test.push_back(EulerXYZ);
  182. rotorder_to_test.push_back(EulerXZY);
  183. rotorder_to_test.push_back(EulerYZX);
  184. rotorder_to_test.push_back(EulerYXZ);
  185. rotorder_to_test.push_back(EulerZXY);
  186. rotorder_to_test.push_back(EulerZYX);
  187. Vector<Vector3> vectors_to_test;
  188. // Test the special cases.
  189. vectors_to_test.push_back(Vector3(0.0, 0.0, 0.0));
  190. vectors_to_test.push_back(Vector3(0.5, 0.5, 0.5));
  191. vectors_to_test.push_back(Vector3(-0.5, -0.5, -0.5));
  192. vectors_to_test.push_back(Vector3(40.0, 40.0, 40.0));
  193. vectors_to_test.push_back(Vector3(-40.0, -40.0, -40.0));
  194. vectors_to_test.push_back(Vector3(0.0, 0.0, -90.0));
  195. vectors_to_test.push_back(Vector3(0.0, -90.0, 0.0));
  196. vectors_to_test.push_back(Vector3(-90.0, 0.0, 0.0));
  197. vectors_to_test.push_back(Vector3(0.0, 0.0, 90.0));
  198. vectors_to_test.push_back(Vector3(0.0, 90.0, 0.0));
  199. vectors_to_test.push_back(Vector3(90.0, 0.0, 0.0));
  200. vectors_to_test.push_back(Vector3(0.0, 0.0, -30.0));
  201. vectors_to_test.push_back(Vector3(0.0, -30.0, 0.0));
  202. vectors_to_test.push_back(Vector3(-30.0, 0.0, 0.0));
  203. vectors_to_test.push_back(Vector3(0.0, 0.0, 30.0));
  204. vectors_to_test.push_back(Vector3(0.0, 30.0, 0.0));
  205. vectors_to_test.push_back(Vector3(30.0, 0.0, 0.0));
  206. vectors_to_test.push_back(Vector3(0.5, 50.0, 20.0));
  207. vectors_to_test.push_back(Vector3(-0.5, -50.0, -20.0));
  208. vectors_to_test.push_back(Vector3(0.5, 0.0, 90.0));
  209. vectors_to_test.push_back(Vector3(0.5, 0.0, -90.0));
  210. vectors_to_test.push_back(Vector3(360.0, 360.0, 360.0));
  211. vectors_to_test.push_back(Vector3(-360.0, -360.0, -360.0));
  212. vectors_to_test.push_back(Vector3(-90.0, 60.0, -90.0));
  213. vectors_to_test.push_back(Vector3(90.0, 60.0, -90.0));
  214. vectors_to_test.push_back(Vector3(90.0, -60.0, -90.0));
  215. vectors_to_test.push_back(Vector3(-90.0, -60.0, -90.0));
  216. vectors_to_test.push_back(Vector3(-90.0, 60.0, 90.0));
  217. vectors_to_test.push_back(Vector3(90.0, 60.0, 90.0));
  218. vectors_to_test.push_back(Vector3(90.0, -60.0, 90.0));
  219. vectors_to_test.push_back(Vector3(-90.0, -60.0, 90.0));
  220. vectors_to_test.push_back(Vector3(60.0, 90.0, -40.0));
  221. vectors_to_test.push_back(Vector3(60.0, -90.0, -40.0));
  222. vectors_to_test.push_back(Vector3(-60.0, -90.0, -40.0));
  223. vectors_to_test.push_back(Vector3(-60.0, 90.0, 40.0));
  224. vectors_to_test.push_back(Vector3(60.0, 90.0, 40.0));
  225. vectors_to_test.push_back(Vector3(60.0, -90.0, 40.0));
  226. vectors_to_test.push_back(Vector3(-60.0, -90.0, 40.0));
  227. vectors_to_test.push_back(Vector3(-90.0, 90.0, -90.0));
  228. vectors_to_test.push_back(Vector3(90.0, 90.0, -90.0));
  229. vectors_to_test.push_back(Vector3(90.0, -90.0, -90.0));
  230. vectors_to_test.push_back(Vector3(-90.0, -90.0, -90.0));
  231. vectors_to_test.push_back(Vector3(-90.0, 90.0, 90.0));
  232. vectors_to_test.push_back(Vector3(90.0, 90.0, 90.0));
  233. vectors_to_test.push_back(Vector3(90.0, -90.0, 90.0));
  234. vectors_to_test.push_back(Vector3(20.0, 150.0, 30.0));
  235. vectors_to_test.push_back(Vector3(20.0, -150.0, 30.0));
  236. vectors_to_test.push_back(Vector3(-120.0, -150.0, 30.0));
  237. vectors_to_test.push_back(Vector3(-120.0, -150.0, -130.0));
  238. vectors_to_test.push_back(Vector3(120.0, -150.0, -130.0));
  239. vectors_to_test.push_back(Vector3(120.0, 150.0, -130.0));
  240. vectors_to_test.push_back(Vector3(120.0, 150.0, 130.0));
  241. // Add 1000 random vectors with weirds numbers.
  242. RandomNumberGenerator rng;
  243. for (int _ = 0; _ < 1000; _ += 1) {
  244. vectors_to_test.push_back(Vector3(
  245. rng.randf_range(-1800, 1800),
  246. rng.randf_range(-1800, 1800),
  247. rng.randf_range(-1800, 1800)));
  248. }
  249. bool success = true;
  250. for (int h = 0; h < rotorder_to_test.size(); h += 1) {
  251. int passed = 0;
  252. int failed = 0;
  253. for (int i = 0; i < vectors_to_test.size(); i += 1) {
  254. if (test_rotation(vectors_to_test[i], rotorder_to_test[h])) {
  255. //OS::get_singleton()->print("Success. \n\n");
  256. passed += 1;
  257. } else {
  258. OS::get_singleton()->print("FAILED FAILED FAILED. \n\n");
  259. OS::get_singleton()->print("------------>\n");
  260. OS::get_singleton()->print("------------>\n");
  261. failed += 1;
  262. success = false;
  263. }
  264. }
  265. if (failed == 0) {
  266. OS::get_singleton()->print("%i passed tests for rotation order: %ls.\n", passed, get_rot_order_name(rotorder_to_test[h]).c_str());
  267. } else {
  268. OS::get_singleton()->print("%i FAILED tests for rotation order: %ls.\n", failed, get_rot_order_name(rotorder_to_test[h]).c_str());
  269. }
  270. }
  271. if (success) {
  272. OS::get_singleton()->print("Euler conversion checks passed.\n");
  273. } else {
  274. OS::get_singleton()->print("Euler conversion checks FAILED.\n");
  275. }
  276. }
  277. MainLoop *test() {
  278. OS::get_singleton()->print("Start euler conversion checks.\n");
  279. test_euler_conversion();
  280. return nullptr;
  281. }
  282. } // namespace TestBasis