test_basis.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**************************************************************************/
  2. /* test_basis.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. Vector3 deg_to_rad(const Vector3 &p_rotation) {
  37. return p_rotation / 180.0 * Math_PI;
  38. }
  39. Vector3 rad2deg(const Vector3 &p_rotation) {
  40. return p_rotation / Math_PI * 180.0;
  41. }
  42. String get_rot_order_name(EulerOrder ro) {
  43. switch (ro) {
  44. case EulerOrder::XYZ:
  45. return "XYZ";
  46. case EulerOrder::XZY:
  47. return "XZY";
  48. case EulerOrder::YZX:
  49. return "YZX";
  50. case EulerOrder::YXZ:
  51. return "YXZ";
  52. case EulerOrder::ZXY:
  53. return "ZXY";
  54. case EulerOrder::ZYX:
  55. return "ZYX";
  56. default:
  57. return "[Not supported]";
  58. }
  59. }
  60. void test_rotation(Vector3 deg_original_euler, EulerOrder rot_order) {
  61. // This test:
  62. // 1. Converts the rotation vector from deg to rad.
  63. // 2. Converts euler to basis.
  64. // 3. Converts the above basis back into euler.
  65. // 4. Converts the above euler into basis again.
  66. // 5. Compares the basis obtained in step 2 with the basis of step 4
  67. //
  68. // The conversion "basis to euler", done in the step 3, may be different from
  69. // the original euler, even if the final rotation are the same.
  70. // This happens because there are more ways to represents the same rotation,
  71. // both valid, using eulers.
  72. // For this reason is necessary to convert that euler back to basis and finally
  73. // compares it.
  74. //
  75. // In this way we can assert that both functions: basis to euler / euler to basis
  76. // are correct.
  77. // Euler to rotation
  78. const Vector3 original_euler = deg_to_rad(deg_original_euler);
  79. const Basis to_rotation = Basis::from_euler(original_euler, rot_order);
  80. // Euler from rotation
  81. const Vector3 euler_from_rotation = to_rotation.get_euler(rot_order);
  82. const Basis rotation_from_computed_euler = Basis::from_euler(euler_from_rotation, rot_order);
  83. Basis res = to_rotation.inverse() * rotation_from_computed_euler;
  84. 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))));
  85. 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))));
  86. 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))));
  87. // Double check `to_rotation` decomposing with XYZ rotation order.
  88. const Vector3 euler_xyz_from_rotation = to_rotation.get_euler(EulerOrder::XYZ);
  89. Basis rotation_from_xyz_computed_euler = Basis::from_euler(euler_xyz_from_rotation, EulerOrder::XYZ);
  90. res = to_rotation.inverse() * rotation_from_xyz_computed_euler;
  91. 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))));
  92. 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))));
  93. 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))));
  94. INFO(vformat("Rotation order: %s\n.", get_rot_order_name(rot_order)));
  95. INFO(vformat("Original Rotation: %s\n", String(deg_original_euler)));
  96. INFO(vformat("Quaternion to rotation order: %s\n", String(rad2deg(euler_from_rotation))));
  97. }
  98. TEST_CASE("[Basis] Euler conversions") {
  99. Vector<EulerOrder> euler_order_to_test;
  100. euler_order_to_test.push_back(EulerOrder::XYZ);
  101. euler_order_to_test.push_back(EulerOrder::XZY);
  102. euler_order_to_test.push_back(EulerOrder::YZX);
  103. euler_order_to_test.push_back(EulerOrder::YXZ);
  104. euler_order_to_test.push_back(EulerOrder::ZXY);
  105. euler_order_to_test.push_back(EulerOrder::ZYX);
  106. Vector<Vector3> vectors_to_test;
  107. // Test the special cases.
  108. vectors_to_test.push_back(Vector3(0.0, 0.0, 0.0));
  109. vectors_to_test.push_back(Vector3(0.5, 0.5, 0.5));
  110. vectors_to_test.push_back(Vector3(-0.5, -0.5, -0.5));
  111. vectors_to_test.push_back(Vector3(40.0, 40.0, 40.0));
  112. vectors_to_test.push_back(Vector3(-40.0, -40.0, -40.0));
  113. vectors_to_test.push_back(Vector3(0.0, 0.0, -90.0));
  114. vectors_to_test.push_back(Vector3(0.0, -90.0, 0.0));
  115. vectors_to_test.push_back(Vector3(-90.0, 0.0, 0.0));
  116. vectors_to_test.push_back(Vector3(0.0, 0.0, 90.0));
  117. vectors_to_test.push_back(Vector3(0.0, 90.0, 0.0));
  118. vectors_to_test.push_back(Vector3(90.0, 0.0, 0.0));
  119. vectors_to_test.push_back(Vector3(0.0, 0.0, -30.0));
  120. vectors_to_test.push_back(Vector3(0.0, -30.0, 0.0));
  121. vectors_to_test.push_back(Vector3(-30.0, 0.0, 0.0));
  122. vectors_to_test.push_back(Vector3(0.0, 0.0, 30.0));
  123. vectors_to_test.push_back(Vector3(0.0, 30.0, 0.0));
  124. vectors_to_test.push_back(Vector3(30.0, 0.0, 0.0));
  125. vectors_to_test.push_back(Vector3(0.5, 50.0, 20.0));
  126. vectors_to_test.push_back(Vector3(-0.5, -50.0, -20.0));
  127. vectors_to_test.push_back(Vector3(0.5, 0.0, 90.0));
  128. vectors_to_test.push_back(Vector3(0.5, 0.0, -90.0));
  129. vectors_to_test.push_back(Vector3(360.0, 360.0, 360.0));
  130. vectors_to_test.push_back(Vector3(-360.0, -360.0, -360.0));
  131. vectors_to_test.push_back(Vector3(-90.0, 60.0, -90.0));
  132. vectors_to_test.push_back(Vector3(90.0, 60.0, -90.0));
  133. vectors_to_test.push_back(Vector3(90.0, -60.0, -90.0));
  134. vectors_to_test.push_back(Vector3(-90.0, -60.0, -90.0));
  135. vectors_to_test.push_back(Vector3(-90.0, 60.0, 90.0));
  136. vectors_to_test.push_back(Vector3(90.0, 60.0, 90.0));
  137. vectors_to_test.push_back(Vector3(90.0, -60.0, 90.0));
  138. vectors_to_test.push_back(Vector3(-90.0, -60.0, 90.0));
  139. vectors_to_test.push_back(Vector3(60.0, 90.0, -40.0));
  140. vectors_to_test.push_back(Vector3(60.0, -90.0, -40.0));
  141. vectors_to_test.push_back(Vector3(-60.0, -90.0, -40.0));
  142. vectors_to_test.push_back(Vector3(-60.0, 90.0, 40.0));
  143. vectors_to_test.push_back(Vector3(60.0, 90.0, 40.0));
  144. vectors_to_test.push_back(Vector3(60.0, -90.0, 40.0));
  145. vectors_to_test.push_back(Vector3(-60.0, -90.0, 40.0));
  146. vectors_to_test.push_back(Vector3(-90.0, 90.0, -90.0));
  147. vectors_to_test.push_back(Vector3(90.0, 90.0, -90.0));
  148. vectors_to_test.push_back(Vector3(90.0, -90.0, -90.0));
  149. vectors_to_test.push_back(Vector3(-90.0, -90.0, -90.0));
  150. vectors_to_test.push_back(Vector3(-90.0, 90.0, 90.0));
  151. vectors_to_test.push_back(Vector3(90.0, 90.0, 90.0));
  152. vectors_to_test.push_back(Vector3(90.0, -90.0, 90.0));
  153. vectors_to_test.push_back(Vector3(20.0, 150.0, 30.0));
  154. vectors_to_test.push_back(Vector3(20.0, -150.0, 30.0));
  155. vectors_to_test.push_back(Vector3(-120.0, -150.0, 30.0));
  156. vectors_to_test.push_back(Vector3(-120.0, -150.0, -130.0));
  157. vectors_to_test.push_back(Vector3(120.0, -150.0, -130.0));
  158. vectors_to_test.push_back(Vector3(120.0, 150.0, -130.0));
  159. vectors_to_test.push_back(Vector3(120.0, 150.0, 130.0));
  160. for (int h = 0; h < euler_order_to_test.size(); h += 1) {
  161. for (int i = 0; i < vectors_to_test.size(); i += 1) {
  162. test_rotation(vectors_to_test[i], euler_order_to_test[h]);
  163. }
  164. }
  165. }
  166. TEST_CASE("[Stress][Basis] Euler conversions") {
  167. Vector<EulerOrder> euler_order_to_test;
  168. euler_order_to_test.push_back(EulerOrder::XYZ);
  169. euler_order_to_test.push_back(EulerOrder::XZY);
  170. euler_order_to_test.push_back(EulerOrder::YZX);
  171. euler_order_to_test.push_back(EulerOrder::YXZ);
  172. euler_order_to_test.push_back(EulerOrder::ZXY);
  173. euler_order_to_test.push_back(EulerOrder::ZYX);
  174. Vector<Vector3> vectors_to_test;
  175. // Add 1000 random vectors with weirds numbers.
  176. RandomNumberGenerator rng;
  177. for (int _ = 0; _ < 1000; _ += 1) {
  178. vectors_to_test.push_back(Vector3(
  179. rng.randf_range(-1800, 1800),
  180. rng.randf_range(-1800, 1800),
  181. rng.randf_range(-1800, 1800)));
  182. }
  183. for (int h = 0; h < euler_order_to_test.size(); h += 1) {
  184. for (int i = 0; i < vectors_to_test.size(); i += 1) {
  185. test_rotation(vectors_to_test[i], euler_order_to_test[h]);
  186. }
  187. }
  188. }
  189. TEST_CASE("[Basis] Set axis angle") {
  190. Vector3 axis;
  191. real_t angle;
  192. real_t pi = (real_t)Math_PI;
  193. // Testing the singularity when the angle is 0°.
  194. Basis identity(1, 0, 0, 0, 1, 0, 0, 0, 1);
  195. identity.get_axis_angle(axis, angle);
  196. CHECK(angle == 0);
  197. // Testing the singularity when the angle is 180°.
  198. Basis singularityPi(-1, 0, 0, 0, 1, 0, 0, 0, -1);
  199. singularityPi.get_axis_angle(axis, angle);
  200. CHECK(angle == doctest::Approx(pi));
  201. // Testing reversing the an axis (of an 30° angle).
  202. float cos30deg = Math::cos(Math::deg_to_rad((real_t)30.0));
  203. Basis z_positive(cos30deg, -0.5, 0, 0.5, cos30deg, 0, 0, 0, 1);
  204. Basis z_negative(cos30deg, 0.5, 0, -0.5, cos30deg, 0, 0, 0, 1);
  205. z_positive.get_axis_angle(axis, angle);
  206. CHECK(angle == doctest::Approx(Math::deg_to_rad((real_t)30.0)));
  207. CHECK(axis == Vector3(0, 0, 1));
  208. z_negative.get_axis_angle(axis, angle);
  209. CHECK(angle == doctest::Approx(Math::deg_to_rad((real_t)30.0)));
  210. CHECK(axis == Vector3(0, 0, -1));
  211. // Testing a rotation of 90° on x-y-z.
  212. Basis x90deg(1, 0, 0, 0, 0, -1, 0, 1, 0);
  213. x90deg.get_axis_angle(axis, angle);
  214. CHECK(angle == doctest::Approx(pi / (real_t)2));
  215. CHECK(axis == Vector3(1, 0, 0));
  216. Basis y90deg(0, 0, 1, 0, 1, 0, -1, 0, 0);
  217. y90deg.get_axis_angle(axis, angle);
  218. CHECK(axis == Vector3(0, 1, 0));
  219. Basis z90deg(0, -1, 0, 1, 0, 0, 0, 0, 1);
  220. z90deg.get_axis_angle(axis, angle);
  221. CHECK(axis == Vector3(0, 0, 1));
  222. // Regression test: checks that the method returns a small angle (not 0).
  223. Basis tiny(1, 0, 0, 0, 0.9999995, -0.001, 0, 001, 0.9999995); // The min angle possible with float is 0.001rad.
  224. tiny.get_axis_angle(axis, angle);
  225. CHECK(angle == doctest::Approx(0.001).epsilon(0.0001));
  226. // Regression test: checks that the method returns an angle which is a number (not NaN)
  227. Basis bugNan(1.00000024, 0, 0.000100001693, 0, 1, 0, -0.000100009143, 0, 1.00000024);
  228. bugNan.get_axis_angle(axis, angle);
  229. CHECK(!Math::is_nan(angle));
  230. }
  231. TEST_CASE("[Basis] Finite number checks") {
  232. const Vector3 x(0, 1, 2);
  233. const Vector3 infinite(NAN, NAN, NAN);
  234. CHECK_MESSAGE(
  235. Basis(x, x, x).is_finite(),
  236. "Basis with all components finite should be finite");
  237. CHECK_FALSE_MESSAGE(
  238. Basis(infinite, x, x).is_finite(),
  239. "Basis with one component infinite should not be finite.");
  240. CHECK_FALSE_MESSAGE(
  241. Basis(x, infinite, x).is_finite(),
  242. "Basis with one component infinite should not be finite.");
  243. CHECK_FALSE_MESSAGE(
  244. Basis(x, x, infinite).is_finite(),
  245. "Basis with one component infinite should not be finite.");
  246. CHECK_FALSE_MESSAGE(
  247. Basis(infinite, infinite, x).is_finite(),
  248. "Basis with two components infinite should not be finite.");
  249. CHECK_FALSE_MESSAGE(
  250. Basis(infinite, x, infinite).is_finite(),
  251. "Basis with two components infinite should not be finite.");
  252. CHECK_FALSE_MESSAGE(
  253. Basis(x, infinite, infinite).is_finite(),
  254. "Basis with two components infinite should not be finite.");
  255. CHECK_FALSE_MESSAGE(
  256. Basis(infinite, infinite, infinite).is_finite(),
  257. "Basis with three components infinite should not be finite.");
  258. }
  259. TEST_CASE("[Basis] Is conformal checks") {
  260. CHECK_MESSAGE(
  261. Basis().is_conformal(),
  262. "Identity Basis should be conformal.");
  263. CHECK_MESSAGE(
  264. Basis::from_euler(Vector3(1.2, 3.4, 5.6)).is_conformal(),
  265. "Basis with only rotation should be conformal.");
  266. CHECK_MESSAGE(
  267. Basis::from_scale(Vector3(-1, -1, -1)).is_conformal(),
  268. "Basis with only a flip should be conformal.");
  269. CHECK_MESSAGE(
  270. Basis::from_scale(Vector3(1.2, 1.2, 1.2)).is_conformal(),
  271. "Basis with only uniform scale should be conformal.");
  272. CHECK_MESSAGE(
  273. Basis(Vector3(3, 4, 0), Vector3(4, -3, 0.0), Vector3(0, 0, 5)).is_conformal(),
  274. "Basis with a flip, rotation, and uniform scale should be conformal.");
  275. CHECK_FALSE_MESSAGE(
  276. Basis::from_scale(Vector3(1.2, 3.4, 5.6)).is_conformal(),
  277. "Basis with non-uniform scale should not be conformal.");
  278. CHECK_FALSE_MESSAGE(
  279. Basis(Vector3(Math_SQRT12, Math_SQRT12, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)).is_conformal(),
  280. "Basis with the X axis skewed 45 degrees should not be conformal.");
  281. CHECK_MESSAGE(
  282. Basis(0, 0, 0, 0, 0, 0, 0, 0, 0).is_conformal(),
  283. "Edge case: Basis with all zeroes should return true for is_conformal (because a 0 scale is uniform).");
  284. }
  285. TEST_CASE("[Basis] Is orthogonal checks") {
  286. CHECK_MESSAGE(
  287. Basis().is_orthogonal(),
  288. "Identity Basis should be orthogonal.");
  289. CHECK_MESSAGE(
  290. Basis::from_euler(Vector3(1.2, 3.4, 5.6)).is_orthogonal(),
  291. "Basis with only rotation should be orthogonal.");
  292. CHECK_MESSAGE(
  293. Basis::from_scale(Vector3(-1, -1, -1)).is_orthogonal(),
  294. "Basis with only a flip should be orthogonal.");
  295. CHECK_MESSAGE(
  296. Basis::from_scale(Vector3(1.2, 3.4, 5.6)).is_orthogonal(),
  297. "Basis with only scale should be orthogonal.");
  298. CHECK_MESSAGE(
  299. Basis(Vector3(3, 4, 0), Vector3(4, -3, 0), Vector3(0, 0, 5)).is_orthogonal(),
  300. "Basis with a flip, rotation, and uniform scale should be orthogonal.");
  301. CHECK_FALSE_MESSAGE(
  302. Basis(Vector3(Math_SQRT12, Math_SQRT12, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)).is_orthogonal(),
  303. "Basis with the X axis skewed 45 degrees should not be orthogonal.");
  304. CHECK_MESSAGE(
  305. Basis(0, 0, 0, 0, 0, 0, 0, 0, 0).is_orthogonal(),
  306. "Edge case: Basis with all zeroes should return true for is_orthogonal, since zero vectors are orthogonal to all vectors.");
  307. }
  308. TEST_CASE("[Basis] Is orthonormal checks") {
  309. CHECK_MESSAGE(
  310. Basis().is_orthonormal(),
  311. "Identity Basis should be orthonormal.");
  312. CHECK_MESSAGE(
  313. Basis::from_euler(Vector3(1.2, 3.4, 5.6)).is_orthonormal(),
  314. "Basis with only rotation should be orthonormal.");
  315. CHECK_MESSAGE(
  316. Basis::from_scale(Vector3(-1, -1, -1)).is_orthonormal(),
  317. "Basis with only a flip should be orthonormal.");
  318. CHECK_FALSE_MESSAGE(
  319. Basis::from_scale(Vector3(1.2, 3.4, 5.6)).is_orthonormal(),
  320. "Basis with only scale should not be orthonormal.");
  321. CHECK_FALSE_MESSAGE(
  322. Basis(Vector3(3, 4, 0), Vector3(4, -3, 0), Vector3(0, 0, 5)).is_orthonormal(),
  323. "Basis with a flip, rotation, and uniform scale should not be orthonormal.");
  324. CHECK_FALSE_MESSAGE(
  325. Basis(Vector3(Math_SQRT12, Math_SQRT12, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)).is_orthonormal(),
  326. "Basis with the X axis skewed 45 degrees should not be orthonormal.");
  327. CHECK_FALSE_MESSAGE(
  328. Basis(0, 0, 0, 0, 0, 0, 0, 0, 0).is_orthonormal(),
  329. "Edge case: Basis with all zeroes should return false for is_orthonormal, since the vectors do not have a length of 1.");
  330. }
  331. TEST_CASE("[Basis] Is rotation checks") {
  332. CHECK_MESSAGE(
  333. Basis().is_rotation(),
  334. "Identity Basis should be a rotation (a rotation of zero).");
  335. CHECK_MESSAGE(
  336. Basis::from_euler(Vector3(1.2, 3.4, 5.6)).is_rotation(),
  337. "Basis with only rotation should be a rotation.");
  338. CHECK_FALSE_MESSAGE(
  339. Basis::from_scale(Vector3(-1, -1, -1)).is_rotation(),
  340. "Basis with only a flip should not be a rotation.");
  341. CHECK_FALSE_MESSAGE(
  342. Basis::from_scale(Vector3(1.2, 3.4, 5.6)).is_rotation(),
  343. "Basis with only scale should not be a rotation.");
  344. CHECK_FALSE_MESSAGE(
  345. Basis(Vector3(2, 0, 0), Vector3(0, 0.5, 0), Vector3(0, 0, 1)).is_rotation(),
  346. "Basis with a squeeze should not be a rotation.");
  347. CHECK_FALSE_MESSAGE(
  348. Basis(Vector3(Math_SQRT12, Math_SQRT12, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)).is_rotation(),
  349. "Basis with the X axis skewed 45 degrees should not be a rotation.");
  350. CHECK_FALSE_MESSAGE(
  351. Basis(0, 0, 0, 0, 0, 0, 0, 0, 0).is_rotation(),
  352. "Edge case: Basis with all zeroes should return false for is_rotation, because it is not just a rotation (has a scale of 0).");
  353. }
  354. } // namespace TestBasis
  355. #endif // TEST_BASIS_H