RavEngine
Loading...
Searching...
No Matches
scene_manager.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_SCENE_MANAGER_H_
18#define RESONANCE_AUDIO_GEOMETRICAL_ACOUSTICS_SCENE_MANAGER_H_
19
20#include <functional>
21#include <unordered_map>
22#include <unordered_set>
23#include <vector>
24
25#include "embree2/rtcore.h"
26#include "embree2/rtcore_ray.h"
27#include "base/logging.h"
28#include "geometrical_acoustics/acoustic_listener.h"
29#include "geometrical_acoustics/mesh.h"
30#include "geometrical_acoustics/reflection_kernel.h"
31
32namespace vraudio {
33
34// A class to manage a scene modeling an acoustic environment and a scene
35// modeling listeners.
37 public:
39 virtual ~SceneManager();
40
41 // Builds a scene with triangles describing the acoustic environment.
42 //
43 // @param vertex_buffer A vector of vertices.
44 // @param triangle_buffer A vector of triangles. The triangles should be
45 // right-handed, i.e. if a triangle is defined by {v0, v1, v2},
46 // then its normal is along the right-handed cross product of (v1 - v0)
47 // and (v2 - v0).
48 void BuildScene(const std::vector<Vertex>& vertex_buffer,
49 const std::vector<Triangle>& triangle_buffer);
50
51 // Associates a reflection kernel to a set of triangles.
52 //
53 // @param reflection Reflection kernel.
54 // @param triangle_indices Indices of triangles to be associated with
55 // the reflection .
56 // @return True on success.
57 bool AssociateReflectionKernelToTriangles(
58 const ReflectionKernel& reflection_kernel,
59 const std::unordered_set<unsigned int>& triangle_indices);
60
61 // Gets the reflection kernel associated to a triangle.
62 //
63 // @param triangle_index The index of the triangle to get the reflection
64 // kernel from.
65 // @return The reflection kernel associated to the triangle; a default
66 // reflection if no reflection kernel is associated to the triangle.
67 const ReflectionKernel& GetAssociatedReflectionKernel(
68 unsigned int triangle_index) const {
69 if (triangle_to_reflection_map_.count(triangle_index) == 0) {
70 return kDefaultReflection;
71 }
72 return reflections_.at(triangle_to_reflection_map_.at(triangle_index));
73 }
74
75 RTCScene scene() const { return scene_; }
76 bool is_scene_committed() const { return is_scene_committed_; }
77 size_t num_vertices() const { return num_vertices_; }
78 size_t num_triangles() const { return num_triangles_; }
79
80 // Builds a scene with only listener spheres, one sphere per listener.
81 //
82 // @param listeners Vector of AcousticListeners.
83 // @param listener_sphere_radius Radius of listener spheres (m).
84 void BuildListenerScene(const std::vector<AcousticListener>& listeners,
85 float listener_sphere_radius);
86
87 // Gets the listener index associated to a sphere id.
88 //
89 // @param sphere_id Sphere id.
90 // @return listener_index Listener index associated to the sphere id.
91 size_t GetListenerIndexFromSphereId(unsigned int sphere_id) {
92 DCHECK_GT(sphere_to_listener_map_.count(sphere_id), 0);
93 return sphere_to_listener_map_.at(sphere_id);
94 }
95
96 RTCScene listener_scene() const { return listener_scene_; }
97 bool is_listener_scene_committed() const {
98 return is_listener_scene_committed_;
99 }
100
101 private:
102 // Perfect absorption. No randomness needed.
103 const std::array<float, kNumReverbOctaveBands>
104 kPerfectReflectionCoefficients = {
105 {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f}};
106 const ReflectionKernel kDefaultReflection = ReflectionKernel(
107 kPerfectReflectionCoefficients, 0.0f, []() { return 0.5f; });
108
109 // Embree device.
110 RTCDevice device_;
111
112 // Scene modeling an acoustic environment.
113 RTCScene scene_;
114 bool is_scene_committed_ = false;
115 size_t num_vertices_ = 0;
116 size_t num_triangles_ = 0;
117 std::vector<ReflectionKernel> reflections_;
118 std::unordered_map<unsigned int, size_t> triangle_to_reflection_map_;
119
120 // Scene with only listener spheres.
121 RTCScene listener_scene_;
122 bool is_listener_scene_committed_ = false;
123
124 // Map from a sphere's id to the index of a listener.
125 std::unordered_map<unsigned int, size_t> sphere_to_listener_map_;
126};
127
128} // namespace vraudio
129
130#endif // RESONANCE_AUDIO_GEOMETRICAL_ACOUSTICS_SCENE_MANAGER_H_
Definition reflection_kernel.h:40
Definition scene_manager.h:36