-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDirectPathServiceTest.cpp
More file actions
657 lines (541 loc) · 21.8 KB
/
Copy pathDirectPathServiceTest.cpp
File metadata and controls
657 lines (541 loc) · 21.8 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
//
// ██╗██████╗ ██╗ ██╗██████╗ ███████╗
// ██║██╔══██╗ ██║ ██║██╔══██╗██╔════╝ ** JPLSpatial **
// ██║██████╔╝ ██║ ██║██████╔╝███████╗
// ██ ██║██╔═══╝ ██║ ██║██╔══██╗╚════██║ https://github.com/Jaytheway/JPLSpatial
// ╚█████╔╝██║ ███████╗██║██████╔╝███████║
// ╚════╝ ╚═╝ ╚══════╝╚═╝╚═════╝ ╚══════╝
//
// Copyright Jaroslav Pevno, JPLSpatial is offered under the terms of the ISC license:
//
// Permission to use, copy, modify, and/or distribute this software for any purpose with or
// without fee is hereby granted, provided that the above copyright notice and this permission
// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "JPLSpatial/Core.h"
#include "JPLSpatial/Services/DirectPathService.h"
#include "JPLSpatial/Math/Math.h"
#include "JPLSpatial/Math/MinimalVec3.h"
#include "JPLSpatial/Math/MinimalQuat.h"
#include "JPLSpatial/Math/Position.h"
#include "../Utility/TestMemoryLeakDetector.h"
#include <gtest/gtest.h>
#include <array>
#include <cmath>
#include <random>
#include <numbers>
#include <vector>
#include <string>
namespace JPL
{
class ProcessAngleAttenuationTest : public ::testing::Test
{
protected:
using Vec3 = MinimalVec3;
static JPL_INLINE Position<Vec3> GetForwardFacingIdentity()
{
return Position<Vec3>{
.Location = Vec3(0, 0, 0),
.Orientation = OrientationData<Vec3>::IdentityForward()
};
}
TestLeakDetector mLeakDetector;
void SetUp() override
{
mLeakDetector.SetUp();
}
void TearDown() override
{
mLeakDetector.TearDown();
}
};
class ProcessDirectPath : public ProcessAngleAttenuationTest
{
protected:
enum class EFacing { Forward, Backward, Left, Right };
struct ListenerTestCase
{
std::string_view Description;
JPL::Position<Vec3> Position;
};
static OrientationData<Vec3> OrientForward(const Vec3& forward)
{
return OrientationData<Vec3>{.Up = Vec3(0, 1, 0), .Forward = forward };
};
static ListenerTestCase GetListenerCaseFor(EFacing direction)
{
switch (direction)
{
case ProcessDirectPath::EFacing::Forward:
return {
.Description = "Listener at origin, facing forwawrd",
.Position = {.Location = Vec3(0, 0, 0), .Orientation = OrientForward(Vec3(0, 0, -1)) }
};
case ProcessDirectPath::EFacing::Backward:
return {
.Description = "Listener at origin, facing backward",
.Position = {.Location = Vec3(0, 0, 0), .Orientation = OrientForward(Vec3(0, 0, 1)) }
};
case ProcessDirectPath::EFacing::Left:
return {
.Description = "Listener at origin, facing left",
.Position = {.Location = Vec3(0, 0, 0), .Orientation = OrientForward(Vec3(-1, 0, 0)) }
};
case ProcessDirectPath::EFacing::Right:
return {
.Description = "Listener at origin, facing right",
.Position = {.Location = Vec3(0, 0, 0), .Orientation = OrientForward(Vec3(1, 0, 0)) }
};
default:
return {
.Description = "Listener at origin, facing forwawrd",
.Position = {.Location = Vec3(0, 0, 0), .Orientation = OrientForward(Vec3(0, 0, -1)) }
};
}
}
TestLeakDetector mLeakDetector;
void SetUp() override
{
mLeakDetector.SetUp();
}
void TearDown() override
{
mLeakDetector.TearDown();
}
};
TEST_F(ProcessAngleAttenuationTest, InsideInnerCone)
{
// Inputs
Vec3 position(0.0f, 0.0f, -1.0f); // Directly in front (since forward is -Z)
Position<Vec3> referencePoint = GetForwardFacingIdentity();
AttenuationCone cone;
cone.InnerAngle = Math::ToRadians(60.0f);
cone.OuterAngle = Math::ToRadians(120.0f);
float coneOuterGain = 0.5f;
// Expected Output
float expectedAngularGain = 1.0f;
// Compute using original function
float angularGainOriginal = DirectPathService::ProcessAngleAttenuation(position, referencePoint, cone);
angularGainOriginal = std::lerp(1.0f, coneOuterGain, angularGainOriginal);
// Assertions
static constexpr float tolerance = 1e-6f;
EXPECT_NEAR(angularGainOriginal, expectedAngularGain, tolerance);
}
TEST_F(ProcessAngleAttenuationTest, BetweenInnerAndOuterCone)
{
// Inputs
// Position at 45 degrees off the forward vector (-Z)
float angle = Math::ToRadians(45.0f);
Vec3 position(std::sin(angle), 0.0f, -std::cos(angle));
Position<Vec3> referencePoint = GetForwardFacingIdentity();
AttenuationCone cone;
cone.InnerAngle = Math::ToRadians(60.0f);
cone.OuterAngle = Math::ToRadians(120.0f);
float coneOuterGain = 0.5f;
// Compute using original function
float angularGainOriginal = DirectPathService::ProcessAngleAttenuation(position, referencePoint, cone);
angularGainOriginal = std::lerp(1.0f, coneOuterGain, angularGainOriginal);
// Assertions
EXPECT_TRUE(angularGainOriginal > coneOuterGain);
EXPECT_TRUE(angularGainOriginal < 1.0f);
}
TEST_F(ProcessAngleAttenuationTest, OutsideOuterCone)
{
// Inputs
Vec3 position(0.0f, 0.0f, 1.0f); // Directly behind (since forward is -Z)
Position<Vec3> referencePoint = GetForwardFacingIdentity();
AttenuationCone cone;
cone.InnerAngle = Math::ToRadians(60.0f);
cone.OuterAngle = Math::ToRadians(120.0f);
float coneOuterGain = 0.5f;
// Expected Output
float expectedAngularGain = coneOuterGain;
// Compute using original function
float angularGain = DirectPathService::ProcessAngleAttenuation(position, referencePoint, cone);
angularGain = std::lerp(1.0f, coneOuterGain, angularGain);
// Assertions
static constexpr float tolerance = 1e-6f;
EXPECT_NEAR(angularGain, expectedAngularGain, tolerance);
}
TEST_F(ProcessAngleAttenuationTest, InnerAngleEqualsOuterAngle)
{
// Inputs
Vec3 position(1.0f, 0.0f, -1.0f); // Arbitrary position in front-left
Position<Vec3> referencePoint = GetForwardFacingIdentity();
AttenuationCone cone;
cone.InnerAngle = Math::ToRadians(45.0f);
cone.OuterAngle = Math::ToRadians(45.0f);
float coneOuterGain = 0.5f;
// Compute using original function
float angularGainOriginal = DirectPathService::ProcessAngleAttenuation(position, referencePoint, cone);
angularGainOriginal = std::lerp(1.0f, coneOuterGain, angularGainOriginal);
// Assertions
static constexpr float tolerance = 1e-6f;
EXPECT_NEAR(angularGainOriginal, coneOuterGain, tolerance);
}
TEST_F(ProcessAngleAttenuationTest, AnglesZero)
{
// Inputs
Vec3 position(0.0f, 0.0f, -1.0f); // Directly in front (-Z)
Position<Vec3> referencePoint = GetForwardFacingIdentity();
AttenuationCone cone;
cone.InnerAngle = 0.0f;
cone.OuterAngle = 0.0f;
float coneOuterGain = 0.5f;
// Expected Output: Since angles are zero, angularGain should be 1.0f when directly in front
float expectedAngularGain = coneOuterGain;
// Compute using original function
float angularGainOriginal = DirectPathService::ProcessAngleAttenuation(position, referencePoint, cone);
angularGainOriginal = std::lerp(1.0f, coneOuterGain, angularGainOriginal);
// Assertions
static constexpr float tolerance = 1e-6f;
EXPECT_NEAR(angularGainOriginal, expectedAngularGain, tolerance);
}
TEST_F(ProcessAngleAttenuationTest, AnglesFullCircle)
{
// Inputs
Vec3 position(10.0f, 0.0f, 5.0f); // Arbitrary position
Position<Vec3> referencePoint = GetForwardFacingIdentity();
AttenuationCone cone;
cone.InnerAngle = Math::ToRadians(360.0f);
cone.OuterAngle = Math::ToRadians(360.0f);
float coneOuterGain = 0.5f;
// Expected Output: angularGain should be 1.0f
float expectedAngularGain = 1.0f;
// Compute using original function
float angularGainOriginal = DirectPathService::ProcessAngleAttenuation(position, referencePoint, cone);
angularGainOriginal = std::lerp(1.0f, coneOuterGain, angularGainOriginal);
// Assertions
static constexpr float tolerance = 1e-6f;
EXPECT_NEAR(angularGainOriginal, expectedAngularGain, tolerance);
}
TEST_F(ProcessAngleAttenuationTest, NegativeAngles)
{
// Inputs
Vec3 position(0.0f, 1.0f, -1.0f); // Arbitrary position in front-up
Position<Vec3> referencePoint = GetForwardFacingIdentity();
AttenuationCone cone;
cone.InnerAngle = Math::ToRadians(-90.0f);
cone.OuterAngle = Math::ToRadians(-180.0f);
float coneOuterGain = 0.5f;
float expectedAngularGain = 1.0f;
// Compute using original function
float angularGainOriginal = DirectPathService::ProcessAngleAttenuation(position, referencePoint, cone);
angularGainOriginal = std::lerp(1.0f, coneOuterGain, angularGainOriginal);
// Since negative angles may not be valid, ensure both functions handle them consistently
static constexpr float tolerance = 1e-6f;
EXPECT_NEAR(angularGainOriginal, expectedAngularGain, tolerance);
}
#if 0 // TODO: investigate if we need this
TEST_F(ProcessAngleAttenuationTest, OuterAngleLessThanInnerAngle)
{
// Inputs
Vec3 position(-1.0f, 0.0f, -1.0f); // Arbitrary position in front-left
Position<Vec3> referencePoint = GetForwardFacingIdentity();
AttenuationCone cone;
cone.InnerAngle = Math::ToRadians(120.0f);
cone.OuterAngle = Math::ToRadians(60.0f); // Outer angle greater than inner angle
float coneOuterGain = 0.5f;
// Compute using original function
float angularGainOriginal = DirectPathService::ProcessAngleAttenuation(position, referencePoint, cone);
angularGainOriginal = std::lerp(1.0f, coneOuterGain, angularGainOriginal);
// Assertions
static constexpr float tolerance = 1e-6f;
//EXPECT_NEAR(angularGainOriginal, angularGainSIMD, tolerance);
}
#endif
TEST_F(ProcessAngleAttenuationTest, DAtCutoffValues)
{
// Inputs
Position<Vec3> referencePoint = GetForwardFacingIdentity();
AttenuationCone cone;
cone.InnerAngle = Math::ToRadians(60.0f);
cone.OuterAngle = Math::ToRadians(90.0f);
float coneOuterGain = 0.5f;
// Compute cutoff values
float cutoffInner = std::cos(cone.InnerAngle * 0.5f);
float cutoffOuter = std::cos(cone.OuterAngle * 0.5f);
// For angle θ, position vector is:
// position = [sin(θ), 0, -cos(θ)] (since forward is -Z)
// Angle corresponding to cutoffInner
float angleInner = std::acos(cutoffInner);
Vec3 positionInner(std::sin(angleInner), 0.0f, -std::cos(angleInner));
// Angle corresponding to cutoffOuter
float angleOuter = std::acos(cutoffOuter);
Vec3 positionOuter(std::sin(angleOuter), 0.0f, -std::cos(angleOuter));
// Compute using original function for cutoffInner
float angularGainOriginalInner = DirectPathService::ProcessAngleAttenuation(positionInner, referencePoint, cone);
angularGainOriginalInner = std::lerp(1.0f, coneOuterGain, angularGainOriginalInner);
// Compute using original function for cutoffOuter
float angularGainOriginalOuter = DirectPathService::ProcessAngleAttenuation(positionOuter, referencePoint, cone);
angularGainOriginalOuter = std::lerp(1.0f, coneOuterGain, angularGainOriginalOuter);
// Assertions
static constexpr float tolerance = 1e-6f;
EXPECT_NEAR(angularGainOriginalInner, 1.0f, tolerance);
EXPECT_NEAR(angularGainOriginalOuter, coneOuterGain, tolerance);
}
TEST_F(ProcessAngleAttenuationTest, AzimuthOverload)
{
std::mt19937 rng(42); // Seed for reproducibility
std::uniform_real_distribution<float> angleDist(0.0f, std::numbers::pi_v<float> *2.0f);
std::uniform_real_distribution<float> azimuthDist(-std::numbers::pi_v<float>, std::numbers::pi_v<float>);
std::uniform_real_distribution<float> gainDist(0.0f, 1.0f);
const int numTests = 1000;
static constexpr float toleranse = 1e-5f;
// Random reference point
Position<Vec3> referencePoint = GetForwardFacingIdentity();
for (int i = 0; i < numTests; ++i)
{
// Random positions around the listener
float azimuth = azimuthDist(rng);
Vec3 position(std::sin(azimuth), 0.0f, -std::cos(azimuth));
// Random cone angles and gain
AttenuationCone cone{
.InnerAngle = angleDist(rng),
.OuterAngle = angleDist(rng)
};
float coneOuterGain = gainDist(rng);
// Ensure innerAngle <= outerAngle
if (cone.InnerAngle > cone.OuterAngle)
std::swap(cone.InnerAngle, cone.OuterAngle);
// Compute using azimuth overload
float angularGainAzimuth = DirectPathService::ProcessAngleAttenuation(azimuth, cone);
angularGainAzimuth = std::lerp(1.0f, coneOuterGain, angularGainAzimuth);
// Compute using original function
float angularGainOriginal = DirectPathService::ProcessAngleAttenuation(position, referencePoint, cone);
angularGainOriginal = std::lerp(1.0f, coneOuterGain, angularGainOriginal);
EXPECT_NEAR(angularGainAzimuth, angularGainOriginal, toleranse) << "Mismatch at iteration " << i;
}
}
#if 0
TEST_F(ProcessAngleAttenuationTest, SIMDvsBranches_PerformanceTest)
{
std::mt19937 rng(42); // Seed for reproducibility
std::uniform_real_distribution<float> angleDist(0.0f, std::numbers::pi_v<float> *2.0f);
std::uniform_real_distribution<float> gainDist(0.0f, 1.0f);
const int numTests = 5000;
static constexpr float toleranse = 1e-5f;
// Random reference point
Position<Vec3> referencePoint = GetForwardFacingIdentity();
float branchedTime = 0.0f;
{
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < numTests; ++i)
{
// Random positions around the listener
const Vec3 position = Vec3::sRandom(rng) * 10.0f;
// Random cone angles and gain
AttenuationCone cone{
.InnerAngle = angleDist(rng),
.OuterAngle = angleDist(rng),
.OuterGain = gainDist(rng)
};
// Ensure innerAngle <= outerAngle
if (cone.InnerAngle > cone.OuterAngle)
std::swap(cone.InnerAngle, cone.OuterAngle);
// Compute using original function
volatile float angularGainOriginal = ProcessAngleAttenuationOriginal(position, referencePoint, cone);
auto l = [angularGainOriginal] {};
}
branchedTime = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start).count() * 0.001f;
}
float SIMDTime = 0.0f;
{
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < numTests; ++i)
{
// Random positions around the listener
const Vec3 position = Vec3::sRandom(rng) * 10.0f;
// Random reference point
Position<Vec3> referencePoint = GetForwardFacingIdentity();
// Random cone angles and gain
AttenuationCone cone{
.InnerAngle = angleDist(rng),
.OuterAngle = angleDist(rng),
.OuterGain = gainDist(rng)
};
// Ensure innerAngle <= outerAngle
if (cone.InnerAngle > cone.OuterAngle)
std::swap(cone.InnerAngle, cone.OuterAngle);
// Compute using SIMD function
volatile float angularGainSIMD = DirectPathService::ProcessAngleAttenuation(position, referencePoint, cone);
//angularGainSIMD =
auto l = [angularGainSIMD] {};
}
SIMDTime = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start).count() * 0.001f;
}
EXPECT_TRUE(branchedTime > SIMDTime)
<< "SIMD is slower than branched version: "
<< "SIMD time: " << SIMDTime
<< " | Branched time: " << branchedTime;
}
#endif
TEST_F(ProcessAngleAttenuationTest, AzimuthOverload2)
{
// Inputs
// Position at 45 degrees off the forward vector (-Z)
float azimuth = Math::ToRadians(45.0f);
Vec3 position(std::sin(azimuth), 0.0f, -std::cos(azimuth));
Position<Vec3> referencePoint = GetForwardFacingIdentity();
AttenuationCone cone;
cone.InnerAngle = Math::ToRadians(60.0f);
cone.OuterAngle = Math::ToRadians(120.0f);
float coneOuterGain = 0.5f;
// Compute using original function
float angularGainAzimuth = DirectPathService::ProcessAngleAttenuation(azimuth, cone);
angularGainAzimuth = std::lerp(1.0f, coneOuterGain, angularGainAzimuth);
float angularGainOriginal = DirectPathService::ProcessAngleAttenuation(position, referencePoint, cone);
angularGainOriginal = std::lerp(1.0f, coneOuterGain, angularGainOriginal);
// Assertions
static constexpr float tolerance = 1e-6f;
EXPECT_TRUE(angularGainOriginal > coneOuterGain);
EXPECT_TRUE(angularGainOriginal < 1.0f);
EXPECT_NEAR(angularGainAzimuth, angularGainOriginal, tolerance);
}
TEST_F(ProcessDirectPath, ProcessDirectPath_ComputesCorrectDotProducts)
{
struct DirectPathTestCase
{
std::string_view Description;
Vec3 SourcePosition;
ListenerTestCase ListenerCase;
float ExpectedDot;
float ExpectedInvDot;
};
const auto testCases = std::to_array(
{
// Listener facing forward
DirectPathTestCase{
.Description = "Source in world forward",
.SourcePosition = Vec3(0.0f, 0.0f, -5.0f),
.ListenerCase = GetListenerCaseFor(EFacing::Forward),
.ExpectedDot = 1.0f,
.ExpectedInvDot = -1.0f,
},
DirectPathTestCase{
.Description = "Source above the listener",
.SourcePosition = Vec3(0.0f, 5.0f, 0.0f),
.ListenerCase = GetListenerCaseFor(EFacing::Forward),
.ExpectedDot = 0.0f,
.ExpectedInvDot = 0.0f,
},
DirectPathTestCase{
.Description = "Source in world left",
.SourcePosition = Vec3(-5.0f, 0.0f, 0.0f),
.ListenerCase = GetListenerCaseFor(EFacing::Forward),
.ExpectedDot = 0.0f,
.ExpectedInvDot = 0.0f,
},
DirectPathTestCase{
.Description = "Source on top of the listener",
.SourcePosition = Vec3(0.0f, 0.0f, 0.0f),
.ListenerCase = GetListenerCaseFor(EFacing::Forward),
.ExpectedDot = 1.0f,
.ExpectedInvDot = -1.0f,
},
// Listener facing backward
DirectPathTestCase{
.Description = "Source in world forward",
.SourcePosition = Vec3(0.0f, 0.0f, -5.0f),
.ListenerCase = GetListenerCaseFor(EFacing::Backward),
.ExpectedDot = -1.0f,
.ExpectedInvDot = -1.0f,
},
DirectPathTestCase{
.Description = "Source above the listener",
.SourcePosition = Vec3(0.0f, 5.0f, 0.0f),
.ListenerCase = GetListenerCaseFor(EFacing::Backward),
.ExpectedDot = 0.0f,
.ExpectedInvDot = 0.0f,
},
DirectPathTestCase{
.Description = "Source in world left",
.SourcePosition = Vec3(-5.0f, 0.0f, 0.0f),
.ListenerCase = GetListenerCaseFor(EFacing::Backward),
.ExpectedDot = 0.0f,
.ExpectedInvDot = 0.0f,
},
DirectPathTestCase{
.Description = "Source on top of the listener",
.SourcePosition = Vec3(0.0f, 0.0f, 0.0f),
.ListenerCase = GetListenerCaseFor(EFacing::Backward),
.ExpectedDot = 1.0f,
.ExpectedInvDot = 1.0f,
},
// Listener offset, facing fowrard
DirectPathTestCase{
.Description = "Source in world forward",
.SourcePosition = Vec3(0.0f, 0.0f, -10.0f),
.ListenerCase = {
.Description = "Listener moved forward, facing forwawrd",
.Position = {.Location = Vec3(0, 0, -5), .Orientation = OrientForward(Vec3(0, 0, -1)) }
},
.ExpectedDot = 1.0f,
.ExpectedInvDot = -1.0f,
},
DirectPathTestCase{
.Description = "Source in world diagonal fowrard-right",
.SourcePosition = Vec3(10.0f, 0.0f, -10.0f),
.ListenerCase = {
.Description = "Listener left of the source, facing forwawrd",
.Position = {.Location = Vec3(0, 0, -10), .Orientation = OrientForward(Vec3(0, 0, -1)) }
},
.ExpectedDot = 0.0f,
.ExpectedInvDot = 0.0f,
},
DirectPathTestCase{
.Description = "Source in world diagonal forward-left",
.SourcePosition = Vec3(-10.0f, 0.0f, -10.0f),
.ListenerCase = {
.Description = "Listener diagonal forward-right of the source, facing forwawrd",
.Position = {.Location = Vec3(0, 0, -20), .Orientation = OrientForward(Vec3(0, 0, -1)) }
},
.ExpectedDot = -0.7071f,
.ExpectedInvDot = 0.7071f,
},
// Listener offset, facing source
DirectPathTestCase{
.Description = "Source in world diagonal forward-right",
.SourcePosition = Vec3(10.0f, 0.0f, -10.0f),
.ListenerCase = {
.Description = "Listener diagonal back-left of the source, facing source",
.Position = {.Location = Vec3(5, 0, -5), .Orientation = OrientForward(Normalized(Vec3(1, 0, -1))) }
},
.ExpectedDot = 1.0f,
.ExpectedInvDot = -0.7071f,
},
DirectPathTestCase{
.Description = "Source in world diagonal forward-left",
.SourcePosition = Vec3(-10.0f, 0.0f, -10.0f),
.ListenerCase = {
.Description = "Listener diagonal fowrard-right of the source, facing source",
.Position = {.Location = Vec3(0, 0, -20), .Orientation = OrientForward(Normalized(Vec3(-1, 0, 1))) }
},
.ExpectedDot = 1.0f,
.ExpectedInvDot = 0.7071f,
}
});
static constexpr float tolerance = 1e-5f;
for (const DirectPathTestCase& testCase : testCases)
{
const Position<Vec3>& listenerPosition = testCase.ListenerCase.Position;
//! Note: SCOPED_TRACE allocate and messes up our leak detector,
//! just print the description on failed EXPECT/ASSERT as a workaround
// SCOPED_TRACE(testCase.ListenerCase.Description);
// SCOPED_TRACE(testCase.Description);
const Position<Vec3> sourcePosition{
.Location = testCase.SourcePosition,
.Orientation = OrientationData<Vec3>::IdentityForward()
};
const DirectPathResult<Vec3> result = DirectPathService::ProcessDirectPath(sourcePosition, listenerPosition);
EXPECT_NEAR(result.DirectionDot, testCase.ExpectedDot, tolerance)
<< testCase.ListenerCase.Description << " " << testCase.Description;
EXPECT_NEAR(result.InvDirectionDot, testCase.ExpectedInvDot, tolerance)
<< testCase.ListenerCase.Description << " " << testCase.Description;
}
}
} // namespace JPL