test_vector2.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*************************************************************************/
  2. /* test_vector2.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_VECTOR2_H
  31. #define TEST_VECTOR2_H
  32. #include "core/math/vector2.h"
  33. #include "tests/test_macros.h"
  34. namespace TestVector2 {
  35. TEST_CASE("[Vector2] Angle methods") {
  36. const Vector2 vector_x = Vector2(1, 0);
  37. const Vector2 vector_y = Vector2(0, 1);
  38. CHECK_MESSAGE(
  39. Math::is_equal_approx(vector_x.angle_to(vector_y), (real_t)Math_TAU / 4),
  40. "Vector2 angle_to should work as expected.");
  41. CHECK_MESSAGE(
  42. Math::is_equal_approx(vector_y.angle_to(vector_x), (real_t)-Math_TAU / 4),
  43. "Vector2 angle_to should work as expected.");
  44. CHECK_MESSAGE(
  45. Math::is_equal_approx(vector_x.angle_to_point(vector_y), (real_t)Math_TAU * 3 / 8),
  46. "Vector2 angle_to_point should work as expected.");
  47. CHECK_MESSAGE(
  48. Math::is_equal_approx(vector_y.angle_to_point(vector_x), (real_t)-Math_TAU / 8),
  49. "Vector2 angle_to_point should work as expected.");
  50. }
  51. TEST_CASE("[Vector2] Axis methods") {
  52. Vector2 vector = Vector2(1.2, 3.4);
  53. CHECK_MESSAGE(
  54. vector.max_axis_index() == Vector2::Axis::AXIS_Y,
  55. "Vector2 max_axis_index should work as expected.");
  56. CHECK_MESSAGE(
  57. vector.min_axis_index() == Vector2::Axis::AXIS_X,
  58. "Vector2 min_axis_index should work as expected.");
  59. CHECK_MESSAGE(
  60. vector[vector.min_axis_index()] == (real_t)1.2,
  61. "Vector2 array operator should work as expected.");
  62. vector[Vector2::Axis::AXIS_Y] = 3.7;
  63. CHECK_MESSAGE(
  64. vector[Vector2::Axis::AXIS_Y] == (real_t)3.7,
  65. "Vector2 array operator setter should work as expected.");
  66. }
  67. TEST_CASE("[Vector2] Interpolation methods") {
  68. const Vector2 vector1 = Vector2(1, 2);
  69. const Vector2 vector2 = Vector2(4, 5);
  70. CHECK_MESSAGE(
  71. vector1.lerp(vector2, 0.5) == Vector2(2.5, 3.5),
  72. "Vector2 lerp should work as expected.");
  73. CHECK_MESSAGE(
  74. vector1.lerp(vector2, 1.0 / 3.0).is_equal_approx(Vector2(2, 3)),
  75. "Vector2 lerp should work as expected.");
  76. CHECK_MESSAGE(
  77. vector1.normalized().slerp(vector2.normalized(), 0.5).is_equal_approx(Vector2(0.538953602313995361, 0.84233558177947998)),
  78. "Vector2 slerp should work as expected.");
  79. CHECK_MESSAGE(
  80. vector1.normalized().slerp(vector2.normalized(), 1.0 / 3.0).is_equal_approx(Vector2(0.508990883827209473, 0.860771894454956055)),
  81. "Vector2 slerp should work as expected.");
  82. CHECK_MESSAGE(
  83. Vector2(5, 0).slerp(Vector2(0, 5), 0.5).is_equal_approx(Vector2(5, 5) * Math_SQRT12),
  84. "Vector2 slerp with non-normalized values should work as expected.");
  85. CHECK_MESSAGE(
  86. Vector2().slerp(Vector2(), 0.5) == Vector2(),
  87. "Vector2 slerp with both inputs as zero vectors should return a zero vector.");
  88. CHECK_MESSAGE(
  89. Vector2().slerp(Vector2(1, 1), 0.5) == Vector2(0.5, 0.5),
  90. "Vector2 slerp with one input as zero should behave like a regular lerp.");
  91. CHECK_MESSAGE(
  92. Vector2(1, 1).slerp(Vector2(), 0.5) == Vector2(0.5, 0.5),
  93. "Vector2 slerp with one input as zero should behave like a regular lerp.");
  94. CHECK_MESSAGE(
  95. Math::is_equal_approx(vector1.slerp(vector2, 0.5).length(), (real_t)4.31959610746631919),
  96. "Vector2 slerp with different length input should return a vector with an interpolated length.");
  97. CHECK_MESSAGE(
  98. Math::is_equal_approx(vector1.angle_to(vector1.slerp(vector2, 0.5)) * 2, vector1.angle_to(vector2)),
  99. "Vector2 slerp with different length input should return a vector with an interpolated angle.");
  100. CHECK_MESSAGE(
  101. vector1.cubic_interpolate(vector2, Vector2(), Vector2(7, 7), 0.5) == Vector2(2.375, 3.5),
  102. "Vector2 cubic_interpolate should work as expected.");
  103. CHECK_MESSAGE(
  104. vector1.cubic_interpolate(vector2, Vector2(), Vector2(7, 7), 1.0 / 3.0).is_equal_approx(Vector2(1.851851940155029297, 2.962963104248046875)),
  105. "Vector2 cubic_interpolate should work as expected.");
  106. CHECK_MESSAGE(
  107. Vector2(1, 0).move_toward(Vector2(10, 0), 3) == Vector2(4, 0),
  108. "Vector2 move_toward should work as expected.");
  109. }
  110. TEST_CASE("[Vector2] Length methods") {
  111. const Vector2 vector1 = Vector2(10, 10);
  112. const Vector2 vector2 = Vector2(20, 30);
  113. CHECK_MESSAGE(
  114. vector1.length_squared() == 200,
  115. "Vector2 length_squared should work as expected and return exact result.");
  116. CHECK_MESSAGE(
  117. Math::is_equal_approx(vector1.length(), 10 * (real_t)Math_SQRT2),
  118. "Vector2 length should work as expected.");
  119. CHECK_MESSAGE(
  120. vector2.length_squared() == 1300,
  121. "Vector2 length_squared should work as expected and return exact result.");
  122. CHECK_MESSAGE(
  123. Math::is_equal_approx(vector2.length(), (real_t)36.05551275463989293119),
  124. "Vector2 length should work as expected.");
  125. CHECK_MESSAGE(
  126. vector1.distance_squared_to(vector2) == 500,
  127. "Vector2 distance_squared_to should work as expected and return exact result.");
  128. CHECK_MESSAGE(
  129. Math::is_equal_approx(vector1.distance_to(vector2), (real_t)22.36067977499789696409),
  130. "Vector2 distance_to should work as expected.");
  131. }
  132. TEST_CASE("[Vector2] Limiting methods") {
  133. const Vector2 vector = Vector2(10, 10);
  134. CHECK_MESSAGE(
  135. vector.limit_length().is_equal_approx(Vector2(Math_SQRT12, Math_SQRT12)),
  136. "Vector2 limit_length should work as expected.");
  137. CHECK_MESSAGE(
  138. vector.limit_length(5).is_equal_approx(5 * Vector2(Math_SQRT12, Math_SQRT12)),
  139. "Vector2 limit_length should work as expected.");
  140. CHECK_MESSAGE(
  141. Vector2(-5, 15).clamp(Vector2(), vector).is_equal_approx(Vector2(0, 10)),
  142. "Vector2 clamp should work as expected.");
  143. CHECK_MESSAGE(
  144. vector.clamp(Vector2(0, 15), Vector2(5, 20)).is_equal_approx(Vector2(5, 15)),
  145. "Vector2 clamp should work as expected.");
  146. }
  147. TEST_CASE("[Vector2] Normalization methods") {
  148. CHECK_MESSAGE(
  149. Vector2(1, 0).is_normalized() == true,
  150. "Vector2 is_normalized should return true for a normalized vector.");
  151. CHECK_MESSAGE(
  152. Vector2(1, 1).is_normalized() == false,
  153. "Vector2 is_normalized should return false for a non-normalized vector.");
  154. CHECK_MESSAGE(
  155. Vector2(1, 0).normalized() == Vector2(1, 0),
  156. "Vector2 normalized should return the same vector for a normalized vector.");
  157. CHECK_MESSAGE(
  158. Vector2(1, 1).normalized().is_equal_approx(Vector2(Math_SQRT12, Math_SQRT12)),
  159. "Vector2 normalized should work as expected.");
  160. }
  161. TEST_CASE("[Vector2] Operators") {
  162. const Vector2 decimal1 = Vector2(2.3, 4.9);
  163. const Vector2 decimal2 = Vector2(1.2, 3.4);
  164. const Vector2 power1 = Vector2(0.75, 1.5);
  165. const Vector2 power2 = Vector2(0.5, 0.125);
  166. const Vector2 int1 = Vector2(4, 5);
  167. const Vector2 int2 = Vector2(1, 2);
  168. CHECK_MESSAGE(
  169. (decimal1 + decimal2).is_equal_approx(Vector2(3.5, 8.3)),
  170. "Vector2 addition should behave as expected.");
  171. CHECK_MESSAGE(
  172. (power1 + power2) == Vector2(1.25, 1.625),
  173. "Vector2 addition with powers of two should give exact results.");
  174. CHECK_MESSAGE(
  175. (int1 + int2) == Vector2(5, 7),
  176. "Vector2 addition with integers should give exact results.");
  177. CHECK_MESSAGE(
  178. (decimal1 - decimal2).is_equal_approx(Vector2(1.1, 1.5)),
  179. "Vector2 subtraction should behave as expected.");
  180. CHECK_MESSAGE(
  181. (power1 - power2) == Vector2(0.25, 1.375),
  182. "Vector2 subtraction with powers of two should give exact results.");
  183. CHECK_MESSAGE(
  184. (int1 - int2) == Vector2(3, 3),
  185. "Vector2 subtraction with integers should give exact results.");
  186. CHECK_MESSAGE(
  187. (decimal1 * decimal2).is_equal_approx(Vector2(2.76, 16.66)),
  188. "Vector2 multiplication should behave as expected.");
  189. CHECK_MESSAGE(
  190. (power1 * power2) == Vector2(0.375, 0.1875),
  191. "Vector2 multiplication with powers of two should give exact results.");
  192. CHECK_MESSAGE(
  193. (int1 * int2) == Vector2(4, 10),
  194. "Vector2 multiplication with integers should give exact results.");
  195. CHECK_MESSAGE(
  196. (decimal1 / decimal2).is_equal_approx(Vector2(1.91666666666666666, 1.44117647058823529)),
  197. "Vector2 division should behave as expected.");
  198. CHECK_MESSAGE(
  199. (power1 / power2) == Vector2(1.5, 12.0),
  200. "Vector2 division with powers of two should give exact results.");
  201. CHECK_MESSAGE(
  202. (int1 / int2) == Vector2(4, 2.5),
  203. "Vector2 division with integers should give exact results.");
  204. CHECK_MESSAGE(
  205. (decimal1 * 2).is_equal_approx(Vector2(4.6, 9.8)),
  206. "Vector2 multiplication should behave as expected.");
  207. CHECK_MESSAGE(
  208. (power1 * 2) == Vector2(1.5, 3),
  209. "Vector2 multiplication with powers of two should give exact results.");
  210. CHECK_MESSAGE(
  211. (int1 * 2) == Vector2(8, 10),
  212. "Vector2 multiplication with integers should give exact results.");
  213. CHECK_MESSAGE(
  214. (decimal1 / 2).is_equal_approx(Vector2(1.15, 2.45)),
  215. "Vector2 division should behave as expected.");
  216. CHECK_MESSAGE(
  217. (power1 / 2) == Vector2(0.375, 0.75),
  218. "Vector2 division with powers of two should give exact results.");
  219. CHECK_MESSAGE(
  220. (int1 / 2) == Vector2(2, 2.5),
  221. "Vector2 division with integers should give exact results.");
  222. CHECK_MESSAGE(
  223. ((Vector2i)decimal1) == Vector2i(2, 4),
  224. "Vector2 cast to Vector2i should work as expected.");
  225. CHECK_MESSAGE(
  226. ((Vector2i)decimal2) == Vector2i(1, 3),
  227. "Vector2 cast to Vector2i should work as expected.");
  228. CHECK_MESSAGE(
  229. Vector2(Vector2i(1, 2)) == Vector2(1, 2),
  230. "Vector2 constructed from Vector2i should work as expected.");
  231. CHECK_MESSAGE(
  232. ((String)decimal1) == "(2.3, 4.9)",
  233. "Vector2 cast to String should work as expected.");
  234. CHECK_MESSAGE(
  235. ((String)decimal2) == "(1.2, 3.4)",
  236. "Vector2 cast to String should work as expected.");
  237. CHECK_MESSAGE(
  238. ((String)Vector2(9.8, 9.9)) == "(9.8, 9.9)",
  239. "Vector2 cast to String should work as expected.");
  240. #ifdef REAL_T_IS_DOUBLE
  241. CHECK_MESSAGE(
  242. ((String)Vector2(Math_PI, Math_TAU)) == "(3.14159265358979, 6.28318530717959)",
  243. "Vector2 cast to String should print the correct amount of digits for real_t = double.");
  244. #else
  245. CHECK_MESSAGE(
  246. ((String)Vector2(Math_PI, Math_TAU)) == "(3.141593, 6.283185)",
  247. "Vector2 cast to String should print the correct amount of digits for real_t = float.");
  248. #endif // REAL_T_IS_DOUBLE
  249. }
  250. TEST_CASE("[Vector2] Other methods") {
  251. const Vector2 vector = Vector2(1.2, 3.4);
  252. CHECK_MESSAGE(
  253. Math::is_equal_approx(vector.aspect(), (real_t)1.2 / (real_t)3.4),
  254. "Vector2 aspect should work as expected.");
  255. CHECK_MESSAGE(
  256. vector.direction_to(Vector2()).is_equal_approx(-vector.normalized()),
  257. "Vector2 direction_to should work as expected.");
  258. CHECK_MESSAGE(
  259. Vector2(1, 1).direction_to(Vector2(2, 2)).is_equal_approx(Vector2(Math_SQRT12, Math_SQRT12)),
  260. "Vector2 direction_to should work as expected.");
  261. CHECK_MESSAGE(
  262. vector.posmod(2).is_equal_approx(Vector2(1.2, 1.4)),
  263. "Vector2 posmod should work as expected.");
  264. CHECK_MESSAGE(
  265. (-vector).posmod(2).is_equal_approx(Vector2(0.8, 0.6)),
  266. "Vector2 posmod should work as expected.");
  267. CHECK_MESSAGE(
  268. vector.posmodv(Vector2(1, 2)).is_equal_approx(Vector2(0.2, 1.4)),
  269. "Vector2 posmodv should work as expected.");
  270. CHECK_MESSAGE(
  271. (-vector).posmodv(Vector2(2, 3)).is_equal_approx(Vector2(0.8, 2.6)),
  272. "Vector2 posmodv should work as expected.");
  273. CHECK_MESSAGE(
  274. vector.rotated(Math_TAU / 4).is_equal_approx(Vector2(-3.4, 1.2)),
  275. "Vector2 rotated should work as expected.");
  276. CHECK_MESSAGE(
  277. vector.snapped(Vector2(1, 1)) == Vector2(1, 3),
  278. "Vector2 snapped to integers should be the same as rounding.");
  279. CHECK_MESSAGE(
  280. Vector2(3.4, 5.6).snapped(Vector2(1, 1)) == Vector2(3, 6),
  281. "Vector2 snapped to integers should be the same as rounding.");
  282. CHECK_MESSAGE(
  283. vector.snapped(Vector2(0.25, 0.25)) == Vector2(1.25, 3.5),
  284. "Vector2 snapped to 0.25 should give exact results.");
  285. }
  286. TEST_CASE("[Vector2] Plane methods") {
  287. const Vector2 vector = Vector2(1.2, 3.4);
  288. const Vector2 vector_y = Vector2(0, 1);
  289. CHECK_MESSAGE(
  290. vector.bounce(vector_y) == Vector2(1.2, -3.4),
  291. "Vector2 bounce on a plane with normal of the Y axis should.");
  292. CHECK_MESSAGE(
  293. vector.reflect(vector_y) == Vector2(-1.2, 3.4),
  294. "Vector2 reflect on a plane with normal of the Y axis should.");
  295. CHECK_MESSAGE(
  296. vector.project(vector_y) == Vector2(0, 3.4),
  297. "Vector2 projected on the X axis should only give the Y component.");
  298. CHECK_MESSAGE(
  299. vector.slide(vector_y) == Vector2(1.2, 0),
  300. "Vector2 slide on a plane with normal of the Y axis should set the Y to zero.");
  301. }
  302. TEST_CASE("[Vector2] Rounding methods") {
  303. const Vector2 vector1 = Vector2(1.2, 5.6);
  304. const Vector2 vector2 = Vector2(1.2, -5.6);
  305. CHECK_MESSAGE(
  306. vector1.abs() == vector1,
  307. "Vector2 abs should work as expected.");
  308. CHECK_MESSAGE(
  309. vector2.abs() == vector1,
  310. "Vector2 abs should work as expected.");
  311. CHECK_MESSAGE(
  312. vector1.ceil() == Vector2(2, 6),
  313. "Vector2 ceil should work as expected.");
  314. CHECK_MESSAGE(
  315. vector2.ceil() == Vector2(2, -5),
  316. "Vector2 ceil should work as expected.");
  317. CHECK_MESSAGE(
  318. vector1.floor() == Vector2(1, 5),
  319. "Vector2 floor should work as expected.");
  320. CHECK_MESSAGE(
  321. vector2.floor() == Vector2(1, -6),
  322. "Vector2 floor should work as expected.");
  323. CHECK_MESSAGE(
  324. vector1.round() == Vector2(1, 6),
  325. "Vector2 round should work as expected.");
  326. CHECK_MESSAGE(
  327. vector2.round() == Vector2(1, -6),
  328. "Vector2 round should work as expected.");
  329. CHECK_MESSAGE(
  330. vector1.sign() == Vector2(1, 1),
  331. "Vector2 sign should work as expected.");
  332. CHECK_MESSAGE(
  333. vector2.sign() == Vector2(1, -1),
  334. "Vector2 sign should work as expected.");
  335. }
  336. TEST_CASE("[Vector2] Linear algebra methods") {
  337. const Vector2 vector_x = Vector2(1, 0);
  338. const Vector2 vector_y = Vector2(0, 1);
  339. CHECK_MESSAGE(
  340. vector_x.cross(vector_y) == 1,
  341. "Vector2 cross product of X and Y should give 1.");
  342. CHECK_MESSAGE(
  343. vector_y.cross(vector_x) == -1,
  344. "Vector2 cross product of Y and X should give negative 1.");
  345. CHECK_MESSAGE(
  346. vector_x.dot(vector_y) == 0.0,
  347. "Vector2 dot product of perpendicular vectors should be zero.");
  348. CHECK_MESSAGE(
  349. vector_x.dot(vector_x) == 1.0,
  350. "Vector2 dot product of identical unit vectors should be one.");
  351. CHECK_MESSAGE(
  352. (vector_x * 10).dot(vector_x * 10) == 100.0,
  353. "Vector2 dot product of same direction vectors should behave as expected.");
  354. }
  355. } // namespace TestVector2
  356. #endif // TEST_VECTOR2_H