RavEngine
Loading...
Searching...
No Matches
test_util.h
1/*
2Copyright 2018 Google Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS-IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17// Utilities useful for testing Geometrical Acoustics related classes.
18#ifndef RESONANCE_AUDIO_GEOMETRICAL_ACOUSTICS_TEST_UTIL_H_
19#define RESONANCE_AUDIO_GEOMETRICAL_ACOUSTICS_TEST_UTIL_H_
20
21#include <cstddef>
22#include <functional>
23#include <unordered_set>
24#include <vector>
25
26#include "embree2/rtcore.h"
27#include "api/resonance_audio_api.h"
28#include "base/constants_and_types.h"
29#include "geometrical_acoustics/mesh.h"
30#include "geometrical_acoustics/path.h"
31#include "geometrical_acoustics/scene_manager.h"
32#include "platforms/common/room_effects_utils.h"
33
34namespace vraudio {
35
36// Array of |kNumReverbOctaveBands| of unit energies. Useful to set as initial
37// energies of sound sources in tests.
38static const std::array<float, kNumReverbOctaveBands> kUnitEnergies{
39 {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f}};
40
41// Validates a distribution given the cumulative distribution function (CDF).
42// For any probability density function (PDF), its CDF should be uniformly
43// distributed. So we generate some samples, gather them into bins based on
44// their CDF values, and verify that the number of samples are almost the
45// same across all bins.
46//
47// @param num_samples Number of samples to generate.
48// @param num_bins Number of bins to gather the samples.
49// @param cdf Cumulative distribution function of the distribution to test.
50void ValidateDistribution(const int num_samples, const int num_bins,
51 std::function<float()> cdf);
52
53// Adds a ground (a rectangle consisting of two triangles) to a test scene.
54//
55// @param scene Test scene to which the ground is added.
56void AddTestGround(RTCScene scene);
57
58// Builds a box scene with 8 vertices, 12 triangles, and 6 walls.
59//
60// @param min_corner Corner of the box with minimum x-, y-, z-components.
61// @param max_corner Corner of the box with maximum x-, y-, z-components.
62// @param box_vertices Output vertices of the box. Not filled if nullptr is
63// passed in.
64// @param box_triangles Output triangles of the box. Not filled if nullptr is
65// passed in.
66// @param box_wall_triangles Output wall-to-triangles mapping, with six walls
67// each having two triangles. Not filled if nullptr is passed in.
68void BuildTestBoxScene(
69 const Vertex& min_corner, const Vertex& max_corner,
70 std::vector<Vertex>* box_vertices, std::vector<Triangle>* box_triangles,
71 std::vector<std::unordered_set<unsigned int>>* box_wall_triangles);
72
73// Traces ray paths in a created test scene.
74//
75// @param num_rays Number of rays.
76// @param max_depth Maximum depth of tracing performed along a path.
77// @param energy_threshold Energy threshold below which the tracing stops.
78// @param source_position Position from which to shoot rays.
79// @param scene_vertices Vertices of the scene.
80// @param scene_triangles Triangles of the scene.
81// @param scene_triangle_groups Groups of triangles that share the same
82// material.
83// @param materials Materials for each triangle group.
84// @param scene_manager Scene manager that holds the created test scene.
85// @return Vector of traced paths.
86std::vector<Path> TracePathsInTestcene(
87 size_t num_rays, size_t max_depth, float energy_threshold,
88 const float source_position[3], const std::vector<Vertex>& scene_vertices,
89 const std::vector<Triangle>& scene_triangles,
90 const std::vector<std::unordered_set<unsigned int>>& scene_triangle_groups,
91 const std::vector<MaterialName>& materials, SceneManager* scene_manager);
92
93// Generated ray paths in uniformly distributed directions.
94//
95// @param source_position Position from which to shoot rays.
96// @param min_num_rays Minimum number of rays generated.
97// @return Vector of traced paths.
98std::vector<Path> GenerateUniformlyDistributedRayPaths(
99 const float source_position[3], size_t min_num_rays);
100
101// Compares two float vectors[3]s and expect that their components are close.
102//
103// @param actual_vector Actual vector.
104// @param expected_vector Expected vector.
105void ExpectFloat3Close(const float actual_vector[3],
106 const float expected_vector[3]);
107
108// Same as above but with a user-specified tolerance.
109//
110// @param actual_vector Actual vector.
111// @param expected_vector Expected vector.
112// @param tolerance Tolerance for comparisons.
113void ExpectFloat3Close(const float actual_vector[3],
114 const float expected_vector[3], float tolerance);
115
116// Validates a sparse float array using the indices and values of its
117// non-zero elements.
118//
119// @param actual_array Actual array to be validated.
120// @param expected_indices Indices of the non-zero elements of the expected
121// array.
122// @param expected_values Values of the non-zero elements of the expected array.
123// @param relative_error_tolerance Tolerance of relative errors for element
124// comparisons.
125void ValidateSparseFloatArray(const std::vector<float>& actual_array,
126 const std::vector<size_t>& expected_indices,
127 const std::vector<float>& expected_values,
128 float relative_error_tolerance);
129} // namespace vraudio
130
131#endif // RESONANCE_AUDIO_GEOMETRICAL_ACOUSTICS_TEST_UTIL_H_