test_vector3.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /**************************************************************************/
  2. /* test_vector3.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_VECTOR3_H
  31. #define TEST_VECTOR3_H
  32. #include "core/math/vector3.h"
  33. #include "tests/test_macros.h"
  34. #define Math_SQRT13 0.57735026918962576450914878050196
  35. #define Math_SQRT3 1.7320508075688772935274463415059
  36. namespace TestVector3 {
  37. TEST_CASE("[Vector3] Constructor methods") {
  38. const Vector3 vector_empty = Vector3();
  39. const Vector3 vector_zero = Vector3(0.0, 0.0, 0.0);
  40. CHECK_MESSAGE(
  41. vector_empty == vector_zero,
  42. "Vector3 Constructor with no inputs should return a zero Vector3.");
  43. }
  44. TEST_CASE("[Vector3] Angle methods") {
  45. const Vector3 vector_x = Vector3(1, 0, 0);
  46. const Vector3 vector_y = Vector3(0, 1, 0);
  47. const Vector3 vector_yz = Vector3(0, 1, 1);
  48. CHECK_MESSAGE(
  49. vector_x.angle_to(vector_y) == doctest::Approx((real_t)Math_TAU / 4),
  50. "Vector3 angle_to should work as expected.");
  51. CHECK_MESSAGE(
  52. vector_x.angle_to(vector_yz) == doctest::Approx((real_t)Math_TAU / 4),
  53. "Vector3 angle_to should work as expected.");
  54. CHECK_MESSAGE(
  55. vector_yz.angle_to(vector_x) == doctest::Approx((real_t)Math_TAU / 4),
  56. "Vector3 angle_to should work as expected.");
  57. CHECK_MESSAGE(
  58. vector_y.angle_to(vector_yz) == doctest::Approx((real_t)Math_TAU / 8),
  59. "Vector3 angle_to should work as expected.");
  60. CHECK_MESSAGE(
  61. vector_x.signed_angle_to(vector_y, vector_y) == doctest::Approx((real_t)Math_TAU / 4),
  62. "Vector3 signed_angle_to edge case should be positive.");
  63. CHECK_MESSAGE(
  64. vector_x.signed_angle_to(vector_yz, vector_y) == doctest::Approx((real_t)Math_TAU / -4),
  65. "Vector3 signed_angle_to should work as expected.");
  66. CHECK_MESSAGE(
  67. vector_yz.signed_angle_to(vector_x, vector_y) == doctest::Approx((real_t)Math_TAU / 4),
  68. "Vector3 signed_angle_to should work as expected.");
  69. }
  70. TEST_CASE("[Vector3] Axis methods") {
  71. Vector3 vector = Vector3(1.2, 3.4, 5.6);
  72. CHECK_MESSAGE(
  73. vector.max_axis_index() == Vector3::Axis::AXIS_Z,
  74. "Vector3 max_axis_index should work as expected.");
  75. CHECK_MESSAGE(
  76. vector.min_axis_index() == Vector3::Axis::AXIS_X,
  77. "Vector3 min_axis_index should work as expected.");
  78. CHECK_MESSAGE(
  79. vector[vector.max_axis_index()] == (real_t)5.6,
  80. "Vector3 array operator should work as expected.");
  81. CHECK_MESSAGE(
  82. vector[vector.min_axis_index()] == (real_t)1.2,
  83. "Vector3 array operator should work as expected.");
  84. vector[Vector3::Axis::AXIS_Y] = 3.7;
  85. CHECK_MESSAGE(
  86. vector[Vector3::Axis::AXIS_Y] == (real_t)3.7,
  87. "Vector3 array operator setter should work as expected.");
  88. }
  89. TEST_CASE("[Vector3] Interpolation methods") {
  90. const Vector3 vector1 = Vector3(1, 2, 3);
  91. const Vector3 vector2 = Vector3(4, 5, 6);
  92. CHECK_MESSAGE(
  93. vector1.lerp(vector2, 0.5) == Vector3(2.5, 3.5, 4.5),
  94. "Vector3 lerp should work as expected.");
  95. CHECK_MESSAGE(
  96. vector1.lerp(vector2, 1.0 / 3.0).is_equal_approx(Vector3(2, 3, 4)),
  97. "Vector3 lerp should work as expected.");
  98. CHECK_MESSAGE(
  99. vector1.normalized().slerp(vector2.normalized(), 0.5).is_equal_approx(Vector3(0.363866806030273438, 0.555698215961456299, 0.747529566287994385)),
  100. "Vector3 slerp should work as expected.");
  101. CHECK_MESSAGE(
  102. vector1.normalized().slerp(vector2.normalized(), 1.0 / 3.0).is_equal_approx(Vector3(0.332119762897491455, 0.549413740634918213, 0.766707837581634521)),
  103. "Vector3 slerp should work as expected.");
  104. CHECK_MESSAGE(
  105. Vector3(5, 0, 0).slerp(Vector3(0, 3, 4), 0.5).is_equal_approx(Vector3(3.535533905029296875, 2.121320486068725586, 2.828427314758300781)),
  106. "Vector3 slerp with non-normalized values should work as expected.");
  107. CHECK_MESSAGE(
  108. Vector3(1, 1, 1).slerp(Vector3(2, 2, 2), 0.5).is_equal_approx(Vector3(1.5, 1.5, 1.5)),
  109. "Vector3 slerp with colinear inputs should behave as expected.");
  110. CHECK_MESSAGE(
  111. Vector3().slerp(Vector3(), 0.5) == Vector3(),
  112. "Vector3 slerp with both inputs as zero vectors should return a zero vector.");
  113. CHECK_MESSAGE(
  114. Vector3().slerp(Vector3(1, 1, 1), 0.5) == Vector3(0.5, 0.5, 0.5),
  115. "Vector3 slerp with one input as zero should behave like a regular lerp.");
  116. CHECK_MESSAGE(
  117. Vector3(1, 1, 1).slerp(Vector3(), 0.5) == Vector3(0.5, 0.5, 0.5),
  118. "Vector3 slerp with one input as zero should behave like a regular lerp.");
  119. CHECK_MESSAGE(
  120. Vector3(4, 6, 2).slerp(Vector3(8, 10, 3), 0.5).is_equal_approx(Vector3(5.90194219811429941053, 8.06758688849378394534, 2.558307894718317120038)),
  121. "Vector3 slerp should work as expected.");
  122. CHECK_MESSAGE(
  123. vector1.slerp(vector2, 0.5).length() == doctest::Approx((real_t)6.25831088708303172),
  124. "Vector3 slerp with different length input should return a vector with an interpolated length.");
  125. CHECK_MESSAGE(
  126. vector1.angle_to(vector1.slerp(vector2, 0.5)) * 2 == doctest::Approx(vector1.angle_to(vector2)),
  127. "Vector3 slerp with different length input should return a vector with an interpolated angle.");
  128. CHECK_MESSAGE(
  129. vector1.cubic_interpolate(vector2, Vector3(), Vector3(7, 7, 7), 0.5) == Vector3(2.375, 3.5, 4.625),
  130. "Vector3 cubic_interpolate should work as expected.");
  131. CHECK_MESSAGE(
  132. vector1.cubic_interpolate(vector2, Vector3(), Vector3(7, 7, 7), 1.0 / 3.0).is_equal_approx(Vector3(1.851851940155029297, 2.962963104248046875, 4.074074268341064453)),
  133. "Vector3 cubic_interpolate should work as expected.");
  134. CHECK_MESSAGE(
  135. Vector3(1, 0, 0).move_toward(Vector3(10, 0, 0), 3) == Vector3(4, 0, 0),
  136. "Vector3 move_toward should work as expected.");
  137. }
  138. TEST_CASE("[Vector3] Length methods") {
  139. const Vector3 vector1 = Vector3(10, 10, 10);
  140. const Vector3 vector2 = Vector3(20, 30, 40);
  141. CHECK_MESSAGE(
  142. vector1.length_squared() == 300,
  143. "Vector3 length_squared should work as expected and return exact result.");
  144. CHECK_MESSAGE(
  145. vector1.length() == doctest::Approx(10 * (real_t)Math_SQRT3),
  146. "Vector3 length should work as expected.");
  147. CHECK_MESSAGE(
  148. vector2.length_squared() == 2900,
  149. "Vector3 length_squared should work as expected and return exact result.");
  150. CHECK_MESSAGE(
  151. vector2.length() == doctest::Approx((real_t)53.8516480713450403125),
  152. "Vector3 length should work as expected.");
  153. CHECK_MESSAGE(
  154. vector1.distance_squared_to(vector2) == 1400,
  155. "Vector3 distance_squared_to should work as expected and return exact result.");
  156. CHECK_MESSAGE(
  157. vector1.distance_to(vector2) == doctest::Approx((real_t)37.41657386773941385584),
  158. "Vector3 distance_to should work as expected.");
  159. }
  160. TEST_CASE("[Vector3] Limiting methods") {
  161. const Vector3 vector = Vector3(10, 10, 10);
  162. CHECK_MESSAGE(
  163. vector.limit_length().is_equal_approx(Vector3(Math_SQRT13, Math_SQRT13, Math_SQRT13)),
  164. "Vector3 limit_length should work as expected.");
  165. CHECK_MESSAGE(
  166. vector.limit_length(5).is_equal_approx(5 * Vector3(Math_SQRT13, Math_SQRT13, Math_SQRT13)),
  167. "Vector3 limit_length should work as expected.");
  168. CHECK_MESSAGE(
  169. Vector3(-5, 5, 15).clamp(Vector3(), vector) == Vector3(0, 5, 10),
  170. "Vector3 clamp should work as expected.");
  171. CHECK_MESSAGE(
  172. vector.clamp(Vector3(0, 10, 15), Vector3(5, 10, 20)) == Vector3(5, 10, 15),
  173. "Vector3 clamp should work as expected.");
  174. }
  175. TEST_CASE("[Vector3] Normalization methods") {
  176. CHECK_MESSAGE(
  177. Vector3(1, 0, 0).is_normalized() == true,
  178. "Vector3 is_normalized should return true for a normalized vector.");
  179. CHECK_MESSAGE(
  180. Vector3(1, 1, 1).is_normalized() == false,
  181. "Vector3 is_normalized should return false for a non-normalized vector.");
  182. CHECK_MESSAGE(
  183. Vector3(1, 0, 0).normalized() == Vector3(1, 0, 0),
  184. "Vector3 normalized should return the same vector for a normalized vector.");
  185. CHECK_MESSAGE(
  186. Vector3(1, 1, 0).normalized().is_equal_approx(Vector3(Math_SQRT12, Math_SQRT12, 0)),
  187. "Vector3 normalized should work as expected.");
  188. CHECK_MESSAGE(
  189. Vector3(1, 1, 1).normalized().is_equal_approx(Vector3(Math_SQRT13, Math_SQRT13, Math_SQRT13)),
  190. "Vector3 normalized should work as expected.");
  191. Vector3 vector = Vector3(3.2, -5.4, 6);
  192. vector.normalize();
  193. CHECK_MESSAGE(
  194. vector == Vector3(3.2, -5.4, 6).normalized(),
  195. "Vector3 normalize should convert same way as Vector3 normalized.");
  196. CHECK_MESSAGE(
  197. vector.is_equal_approx(Vector3(0.368522751763902980457, -0.621882143601586279522, 0.6909801595573180883585)),
  198. "Vector3 normalize should work as expected.");
  199. }
  200. TEST_CASE("[Vector3] Operators") {
  201. const Vector3 decimal1 = Vector3(2.3, 4.9, 7.8);
  202. const Vector3 decimal2 = Vector3(1.2, 3.4, 5.6);
  203. const Vector3 power1 = Vector3(0.75, 1.5, 0.625);
  204. const Vector3 power2 = Vector3(0.5, 0.125, 0.25);
  205. const Vector3 int1 = Vector3(4, 5, 9);
  206. const Vector3 int2 = Vector3(1, 2, 3);
  207. CHECK_MESSAGE(
  208. (decimal1 + decimal2).is_equal_approx(Vector3(3.5, 8.3, 13.4)),
  209. "Vector3 addition should behave as expected.");
  210. CHECK_MESSAGE(
  211. (power1 + power2) == Vector3(1.25, 1.625, 0.875),
  212. "Vector3 addition with powers of two should give exact results.");
  213. CHECK_MESSAGE(
  214. (int1 + int2) == Vector3(5, 7, 12),
  215. "Vector3 addition with integers should give exact results.");
  216. CHECK_MESSAGE(
  217. (decimal1 - decimal2).is_equal_approx(Vector3(1.1, 1.5, 2.2)),
  218. "Vector3 subtraction should behave as expected.");
  219. CHECK_MESSAGE(
  220. (power1 - power2) == Vector3(0.25, 1.375, 0.375),
  221. "Vector3 subtraction with powers of two should give exact results.");
  222. CHECK_MESSAGE(
  223. (int1 - int2) == Vector3(3, 3, 6),
  224. "Vector3 subtraction with integers should give exact results.");
  225. CHECK_MESSAGE(
  226. (decimal1 * decimal2).is_equal_approx(Vector3(2.76, 16.66, 43.68)),
  227. "Vector3 multiplication should behave as expected.");
  228. CHECK_MESSAGE(
  229. (power1 * power2) == Vector3(0.375, 0.1875, 0.15625),
  230. "Vector3 multiplication with powers of two should give exact results.");
  231. CHECK_MESSAGE(
  232. (int1 * int2) == Vector3(4, 10, 27),
  233. "Vector3 multiplication with integers should give exact results.");
  234. CHECK_MESSAGE(
  235. (decimal1 / decimal2).is_equal_approx(Vector3(1.91666666666666666, 1.44117647058823529, 1.39285714285714286)),
  236. "Vector3 division should behave as expected.");
  237. CHECK_MESSAGE(
  238. (power1 / power2) == Vector3(1.5, 12.0, 2.5),
  239. "Vector3 division with powers of two should give exact results.");
  240. CHECK_MESSAGE(
  241. (int1 / int2) == Vector3(4, 2.5, 3),
  242. "Vector3 division with integers should give exact results.");
  243. CHECK_MESSAGE(
  244. (decimal1 * 2).is_equal_approx(Vector3(4.6, 9.8, 15.6)),
  245. "Vector3 multiplication should behave as expected.");
  246. CHECK_MESSAGE(
  247. (power1 * 2) == Vector3(1.5, 3, 1.25),
  248. "Vector3 multiplication with powers of two should give exact results.");
  249. CHECK_MESSAGE(
  250. (int1 * 2) == Vector3(8, 10, 18),
  251. "Vector3 multiplication with integers should give exact results.");
  252. CHECK_MESSAGE(
  253. (decimal1 / 2).is_equal_approx(Vector3(1.15, 2.45, 3.9)),
  254. "Vector3 division should behave as expected.");
  255. CHECK_MESSAGE(
  256. (power1 / 2) == Vector3(0.375, 0.75, 0.3125),
  257. "Vector3 division with powers of two should give exact results.");
  258. CHECK_MESSAGE(
  259. (int1 / 2) == Vector3(2, 2.5, 4.5),
  260. "Vector3 division with integers should give exact results.");
  261. CHECK_MESSAGE(
  262. ((Vector3i)decimal1) == Vector3i(2, 4, 7),
  263. "Vector3 cast to Vector3i should work as expected.");
  264. CHECK_MESSAGE(
  265. ((Vector3i)decimal2) == Vector3i(1, 3, 5),
  266. "Vector3 cast to Vector3i should work as expected.");
  267. CHECK_MESSAGE(
  268. Vector3(Vector3i(1, 2, 3)) == Vector3(1, 2, 3),
  269. "Vector3 constructed from Vector3i should work as expected.");
  270. CHECK_MESSAGE(
  271. ((String)decimal1) == "(2.3, 4.9, 7.8)",
  272. "Vector3 cast to String should work as expected.");
  273. CHECK_MESSAGE(
  274. ((String)decimal2) == "(1.2, 3.4, 5.6)",
  275. "Vector3 cast to String should work as expected.");
  276. CHECK_MESSAGE(
  277. ((String)Vector3(9.7, 9.8, 9.9)) == "(9.7, 9.8, 9.9)",
  278. "Vector3 cast to String should work as expected.");
  279. #ifdef REAL_T_IS_DOUBLE
  280. CHECK_MESSAGE(
  281. ((String)Vector3(Math_E, Math_SQRT2, Math_SQRT3)) == "(2.71828182845905, 1.4142135623731, 1.73205080756888)",
  282. "Vector3 cast to String should print the correct amount of digits for real_t = double.");
  283. #else
  284. CHECK_MESSAGE(
  285. ((String)Vector3(Math_E, Math_SQRT2, Math_SQRT3)) == "(2.718282, 1.414214, 1.732051)",
  286. "Vector3 cast to String should print the correct amount of digits for real_t = float.");
  287. #endif // REAL_T_IS_DOUBLE
  288. }
  289. TEST_CASE("[Vector3] Other methods") {
  290. const Vector3 vector = Vector3(1.2, 3.4, 5.6);
  291. CHECK_MESSAGE(
  292. vector.direction_to(Vector3()).is_equal_approx(-vector.normalized()),
  293. "Vector3 direction_to should work as expected.");
  294. CHECK_MESSAGE(
  295. Vector3(1, 1, 1).direction_to(Vector3(2, 2, 2)).is_equal_approx(Vector3(Math_SQRT13, Math_SQRT13, Math_SQRT13)),
  296. "Vector3 direction_to should work as expected.");
  297. CHECK_MESSAGE(
  298. vector.inverse().is_equal_approx(Vector3(1 / 1.2, 1 / 3.4, 1 / 5.6)),
  299. "Vector3 inverse should work as expected.");
  300. CHECK_MESSAGE(
  301. vector.posmod(2).is_equal_approx(Vector3(1.2, 1.4, 1.6)),
  302. "Vector3 posmod should work as expected.");
  303. CHECK_MESSAGE(
  304. (-vector).posmod(2).is_equal_approx(Vector3(0.8, 0.6, 0.4)),
  305. "Vector3 posmod should work as expected.");
  306. CHECK_MESSAGE(
  307. vector.posmodv(Vector3(1, 2, 3)).is_equal_approx(Vector3(0.2, 1.4, 2.6)),
  308. "Vector3 posmodv should work as expected.");
  309. CHECK_MESSAGE(
  310. (-vector).posmodv(Vector3(2, 3, 4)).is_equal_approx(Vector3(0.8, 2.6, 2.4)),
  311. "Vector3 posmodv should work as expected.");
  312. CHECK_MESSAGE(
  313. vector.rotated(Vector3(0, 1, 0), Math_TAU).is_equal_approx(vector),
  314. "Vector3 rotated should work as expected.");
  315. CHECK_MESSAGE(
  316. vector.rotated(Vector3(0, 1, 0), Math_TAU / 4).is_equal_approx(Vector3(5.6, 3.4, -1.2)),
  317. "Vector3 rotated should work as expected.");
  318. CHECK_MESSAGE(
  319. vector.rotated(Vector3(1, 0, 0), Math_TAU / 3).is_equal_approx(Vector3(1.2, -6.54974226119285642, 0.1444863728670914)),
  320. "Vector3 rotated should work as expected.");
  321. CHECK_MESSAGE(
  322. vector.rotated(Vector3(0, 0, 1), Math_TAU / 2).is_equal_approx(vector.rotated(Vector3(0, 0, 1), Math_TAU / -2)),
  323. "Vector3 rotated should work as expected.");
  324. CHECK_MESSAGE(
  325. vector.snapped(Vector3(1, 1, 1)) == Vector3(1, 3, 6),
  326. "Vector3 snapped to integers should be the same as rounding.");
  327. CHECK_MESSAGE(
  328. vector.snapped(Vector3(0.25, 0.25, 0.25)) == Vector3(1.25, 3.5, 5.5),
  329. "Vector3 snapped to 0.25 should give exact results.");
  330. CHECK_MESSAGE(
  331. Vector3(1.2, 2.5, 2.0).is_equal_approx(vector.min(Vector3(3.0, 2.5, 2.0))),
  332. "Vector3 min should return expected value.");
  333. CHECK_MESSAGE(
  334. Vector3(5.3, 3.4, 5.6).is_equal_approx(vector.max(Vector3(5.3, 2.0, 3.0))),
  335. "Vector3 max should return expected value.");
  336. }
  337. TEST_CASE("[Vector3] Plane methods") {
  338. const Vector3 vector = Vector3(1.2, 3.4, 5.6);
  339. const Vector3 vector_y = Vector3(0, 1, 0);
  340. const Vector3 vector_normal = Vector3(0.88763458893247992491, 0.26300284116517923701, 0.37806658417494515320);
  341. CHECK_MESSAGE(
  342. vector.bounce(vector_y) == Vector3(1.2, -3.4, 5.6),
  343. "Vector3 bounce on a plane with normal of the Y axis should.");
  344. CHECK_MESSAGE(
  345. vector.bounce(vector_normal).is_equal_approx(Vector3(-6.0369629829775736287, 1.25571467171034855444, 2.517589840583626047)),
  346. "Vector3 bounce with normal should return expected value.");
  347. CHECK_MESSAGE(
  348. vector.reflect(vector_y) == Vector3(-1.2, 3.4, -5.6),
  349. "Vector3 reflect on a plane with normal of the Y axis should.");
  350. CHECK_MESSAGE(
  351. vector.reflect(vector_normal).is_equal_approx(Vector3(6.0369629829775736287, -1.25571467171034855444, -2.517589840583626047)),
  352. "Vector3 reflect with normal should return expected value.");
  353. CHECK_MESSAGE(
  354. vector.project(vector_y) == Vector3(0, 3.4, 0),
  355. "Vector3 projected on the Y axis should only give the Y component.");
  356. CHECK_MESSAGE(
  357. vector.project(vector_normal).is_equal_approx(Vector3(3.61848149148878681437, 1.0721426641448257227776, 1.54120507970818697649)),
  358. "Vector3 projected on a normal should return expected value.");
  359. CHECK_MESSAGE(
  360. vector.slide(vector_y) == Vector3(1.2, 0, 5.6),
  361. "Vector3 slide on a plane with normal of the Y axis should set the Y to zero.");
  362. CHECK_MESSAGE(
  363. vector.slide(vector_normal).is_equal_approx(Vector3(-2.41848149148878681437, 2.32785733585517427722237, 4.0587949202918130235)),
  364. "Vector3 slide with normal should return expected value.");
  365. // There's probably a better way to test these ones?
  366. #ifdef MATH_CHECKS
  367. const Vector3 vector_non_normal = Vector3(5.4, 1.6, 2.3);
  368. ERR_PRINT_OFF;
  369. CHECK_MESSAGE(
  370. vector.bounce(vector_non_normal).is_equal_approx(Vector3()),
  371. "Vector3 bounce should return empty Vector3 with non-normalized input.");
  372. CHECK_MESSAGE(
  373. vector.reflect(vector_non_normal).is_equal_approx(Vector3()),
  374. "Vector3 reflect should return empty Vector3 with non-normalized input.");
  375. CHECK_MESSAGE(
  376. vector.slide(vector_non_normal).is_equal_approx(Vector3()),
  377. "Vector3 slide should return empty Vector3 with non-normalized input.");
  378. ERR_PRINT_ON;
  379. #endif // MATH_CHECKS
  380. }
  381. TEST_CASE("[Vector3] Rounding methods") {
  382. const Vector3 vector1 = Vector3(1.2, 3.4, 5.6);
  383. const Vector3 vector2 = Vector3(1.2, -3.4, -5.6);
  384. CHECK_MESSAGE(
  385. vector1.abs() == vector1,
  386. "Vector3 abs should work as expected.");
  387. CHECK_MESSAGE(
  388. vector2.abs() == vector1,
  389. "Vector3 abs should work as expected.");
  390. CHECK_MESSAGE(
  391. vector1.ceil() == Vector3(2, 4, 6),
  392. "Vector3 ceil should work as expected.");
  393. CHECK_MESSAGE(
  394. vector2.ceil() == Vector3(2, -3, -5),
  395. "Vector3 ceil should work as expected.");
  396. CHECK_MESSAGE(
  397. vector1.floor() == Vector3(1, 3, 5),
  398. "Vector3 floor should work as expected.");
  399. CHECK_MESSAGE(
  400. vector2.floor() == Vector3(1, -4, -6),
  401. "Vector3 floor should work as expected.");
  402. CHECK_MESSAGE(
  403. vector1.round() == Vector3(1, 3, 6),
  404. "Vector3 round should work as expected.");
  405. CHECK_MESSAGE(
  406. vector2.round() == Vector3(1, -3, -6),
  407. "Vector3 round should work as expected.");
  408. CHECK_MESSAGE(
  409. vector1.sign() == Vector3(1, 1, 1),
  410. "Vector3 sign should work as expected.");
  411. CHECK_MESSAGE(
  412. vector2.sign() == Vector3(1, -1, -1),
  413. "Vector3 sign should work as expected.");
  414. }
  415. TEST_CASE("[Vector3] Linear algebra methods") {
  416. const Vector3 vector_x = Vector3(1, 0, 0);
  417. const Vector3 vector_y = Vector3(0, 1, 0);
  418. const Vector3 vector_z = Vector3(0, 0, 1);
  419. const Vector3 a = Vector3(3.5, 8.5, 2.3);
  420. const Vector3 b = Vector3(5.2, 4.6, 7.8);
  421. CHECK_MESSAGE(
  422. vector_x.cross(vector_y) == vector_z,
  423. "Vector3 cross product of X and Y should give Z.");
  424. CHECK_MESSAGE(
  425. vector_y.cross(vector_x) == -vector_z,
  426. "Vector3 cross product of Y and X should give negative Z.");
  427. CHECK_MESSAGE(
  428. vector_y.cross(vector_z) == vector_x,
  429. "Vector3 cross product of Y and Z should give X.");
  430. CHECK_MESSAGE(
  431. vector_z.cross(vector_x) == vector_y,
  432. "Vector3 cross product of Z and X should give Y.");
  433. CHECK_MESSAGE(
  434. a.cross(b).is_equal_approx(Vector3(55.72, -15.34, -28.1)),
  435. "Vector3 cross should return expected value.");
  436. CHECK_MESSAGE(
  437. Vector3(-a.x, a.y, -a.z).cross(Vector3(b.x, -b.y, b.z)).is_equal_approx(Vector3(55.72, 15.34, -28.1)),
  438. "Vector2 cross should return expected value.");
  439. CHECK_MESSAGE(
  440. vector_x.dot(vector_y) == 0.0,
  441. "Vector3 dot product of perpendicular vectors should be zero.");
  442. CHECK_MESSAGE(
  443. vector_x.dot(vector_x) == 1.0,
  444. "Vector3 dot product of identical unit vectors should be one.");
  445. CHECK_MESSAGE(
  446. (vector_x * 10).dot(vector_x * 10) == 100.0,
  447. "Vector3 dot product of same direction vectors should behave as expected.");
  448. CHECK_MESSAGE(
  449. a.dot(b) == doctest::Approx((real_t)75.24),
  450. "Vector3 dot should return expected value.");
  451. CHECK_MESSAGE(
  452. Vector3(-a.x, a.y, -a.z).dot(Vector3(b.x, -b.y, b.z)) == doctest::Approx((real_t)-75.24),
  453. "Vector3 dot should return expected value.");
  454. }
  455. TEST_CASE("[Vector3] Finite number checks") {
  456. const double infinite[] = { NAN, INFINITY, -INFINITY };
  457. CHECK_MESSAGE(
  458. Vector3(0, 1, 2).is_finite(),
  459. "Vector3(0, 1, 2) should be finite");
  460. for (double x : infinite) {
  461. CHECK_FALSE_MESSAGE(
  462. Vector3(x, 1, 2).is_finite(),
  463. "Vector3 with one component infinite should not be finite.");
  464. CHECK_FALSE_MESSAGE(
  465. Vector3(0, x, 2).is_finite(),
  466. "Vector3 with one component infinite should not be finite.");
  467. CHECK_FALSE_MESSAGE(
  468. Vector3(0, 1, x).is_finite(),
  469. "Vector3 with one component infinite should not be finite.");
  470. }
  471. for (double x : infinite) {
  472. for (double y : infinite) {
  473. CHECK_FALSE_MESSAGE(
  474. Vector3(x, y, 2).is_finite(),
  475. "Vector3 with two components infinite should not be finite.");
  476. CHECK_FALSE_MESSAGE(
  477. Vector3(x, 1, y).is_finite(),
  478. "Vector3 with two components infinite should not be finite.");
  479. CHECK_FALSE_MESSAGE(
  480. Vector3(0, x, y).is_finite(),
  481. "Vector3 with two components infinite should not be finite.");
  482. }
  483. }
  484. for (double x : infinite) {
  485. for (double y : infinite) {
  486. for (double z : infinite) {
  487. CHECK_FALSE_MESSAGE(
  488. Vector3(x, y, z).is_finite(),
  489. "Vector3 with three components infinite should not be finite.");
  490. }
  491. }
  492. }
  493. }
  494. } // namespace TestVector3
  495. #endif // TEST_VECTOR3_H