RavEngine
Loading...
Searching...
No Matches
reflection_kernel.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#ifndef RESONANCE_AUDIO_GEOMETRICAL_ACOUSTICS_REFLECTION_KERNEL_H_
18#define RESONANCE_AUDIO_GEOMETRICAL_ACOUSTICS_REFLECTION_KERNEL_H_
19
20#include <array>
21#include <functional>
22#include <utility>
23
24#include "Eigen/Core"
25#include "base/constants_and_types.h"
26#include "base/logging.h"
27#include "geometrical_acoustics/acoustic_ray.h"
28
29namespace vraudio {
30
31// A class modeling reflections of acoustic rays. It takes in an incident ray
32// and computes the corresponding reflected ray. NOTE: not to be confused with
33// vraudio:Reflection, which models the reflection properties of a room.
34//
35// This class handles specular and diffuse reflections, randomly chosen based
36// on the scattering coefficient. Besides, a certain portion of the energy is
37// absorbed based on the absorption coefficient.
38//
39
41 public:
42 // Constructor.
43 //
44 // @param absorption_coefficients Fraction of energy being absorbed for each
45 // frequency band. All values must be in [0.0, 1.0].
46 // @param scattering_coefficient Fraction of energy being scattered
47 // (diffusely reflected). Must be in [0.0, 1.0].
48 // @param random_number_generator Random number generator to randomly select
49 // reflection types as well as ray directions if needed. It should
50 // implement operator() that returns a random value in [0.0, 1.0).
52 const std::array<float, kNumReverbOctaveBands>& absorption_coefficients,
53 float scattering_coefficient,
54 std::function<float()> random_number_generator)
55 : scattering_coefficient_(scattering_coefficient),
56 random_number_generator_(std::move(random_number_generator)) {
57 for (size_t i = 0; i < kNumReverbOctaveBands; ++i) {
58 reflection_coefficients_[i] = 1.0f - absorption_coefficients[i];
59 CHECK(reflection_coefficients_[i] >= 0.0f &&
60 reflection_coefficients_[i] <= 1.0f);
61 }
62 CHECK(scattering_coefficient_ >= 0.0f && scattering_coefficient_ <= 1.0f);
63 }
64
65 // Reflects a ray.
66 //
67 // @param incident_ray Incident (incoming) ray.
68 // @return Exitant (outgoing) ray, whose origin is on the intersection point,
69 // with direction and energy calculated based on the type of reflection
70 // and the incident ray.
71 AcousticRay Reflect(const AcousticRay& incident_ray) const;
72
73 // Reflects an incident ray to create a diffuse-rain ray, whose |direction|
74 // is from the reflection point to the listener position, and whose |t_far|
75 // is the distance between the reflection point and the listener position.
76 // All the other data are the copied from the provided
77 // |reference_reflected_ray|.
78 //
79 // @param incident_ray Incident ray.
80 // @param reflected_ray Reference reflected ray, from which the output
81 // diffuse-rain ray copies all the data except for |direction| and
82 // |t_far|.
83 // @param listener_position Listener position.
84 // @param direction_pdf Output probability density function that a reflected
85 // ray is in the direction of the diffuse-rain ray. Will be used to
86 // modulate the energy contribution from the diffuse-rain ray.
87 // @param diffuse_rain_ray Output diffuse-rain ray.
88 void ReflectDiffuseRain(const AcousticRay& incident_ray,
89 const AcousticRay& reference_reflected_ray,
90 const Eigen::Vector3f& listener_position,
91 float* direction_pdf,
92 AcousticRay* diffuse_rain_ray) const;
93
94 private:
95 std::array<float, kNumReverbOctaveBands> reflection_coefficients_;
96 const float scattering_coefficient_;
97
98 // Randon number generator for sampling reflection types and ray directions.
99 std::function<float()> random_number_generator_;
100};
101
102} // namespace vraudio
103
104#endif // RESONANCE_AUDIO_GEOMETRICAL_ACOUSTICS_REFLECTION_KERNEL_H_
Definition reflection_kernel.h:40