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