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