test_vector2.h 16 KB

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