Wayverb
stochastic_histogram.h
1 #pragma once
2 
3 #include "raytracer/histogram.h"
4 #include "raytracer/simulation_parameters.h"
5 #include "raytracer/stochastic/finder.h"
6 #include "raytracer/stochastic/postprocessing.h"
7 
8 #include "core/attenuator/hrtf.h"
9 #include "core/environment.h"
10 #include "core/spatial_division/scene_buffers.h"
11 
12 namespace wayverb {
13 
14 namespace raytracer {
15 namespace reflection_processor {
16 
17 template <typename T, typename U, typename Alloc>
18 void energy_histogram_sum(const T& item,
19  double sample_rate,
20  std::vector<U, Alloc>& ret) {
21  ret[time(item) * sample_rate] += volume(item);
22 }
23 
24 template <typename T, typename U, typename Alloc, size_t Az, size_t El>
25 void energy_histogram_sum(
26  const T& item,
27  double sample_rate,
28  core::vector_look_up_table<std::vector<U, Alloc>, Az, El>& ret) {
29  using table = std::decay_t<decltype(ret)>;
30  ret.at(table::index(item.pointing))[time(item) * sample_rate] +=
31  volume(item);
32 }
33 
35  template <typename T, typename Ret>
36  void operator()(const T& item, double sample_rate, Ret& ret) const {
37  energy_histogram_sum(item, sample_rate, ret);
38  }
39 };
40 
43 template <typename Histogram>
45 public:
50  const glm::vec3& source,
51  const glm::vec3& receiver,
52  const core::environment& environment,
53  size_t total_rays,
54  size_t max_image_source_order,
55  float receiver_radius,
56  float histogram_sample_rate,
57  size_t group_items)
58  : finder_(cc,
59  group_items,
60  source,
61  receiver,
62  receiver_radius,
63  stochastic::compute_ray_energy(
64  total_rays, source, receiver, receiver_radius))
65  , receiver_{receiver}
66  , environment_{environment}
67  , max_image_source_order_{max_image_source_order}
68  , histogram_{histogram_sample_rate} {}
69 
70  template <typename It>
71  void process(It b,
72  It e,
73  const core::scene_buffers& buffers,
74  size_t step,
75  size_t /*total*/) {
76  const auto output = finder_.process(b, e, buffers);
77 
78  struct intermediate_impulse final {
79  core::bands_type volume;
80  double time;
81  glm::vec3 pointing;
82  };
83 
84  const auto intermediate = [&] {
85  util::aligned::vector<intermediate_impulse> ret;
86  ret.reserve(output.stochastic.size() + output.specular.size());
87 
88  const auto push_vector = [&](const auto& vec) {
89  for (const auto& impulse : vec) {
90  ret.emplace_back(intermediate_impulse{
91  impulse.volume,
92  impulse.distance / environment_.speed_of_sound,
93  glm::normalize(core::to_vec3{}(impulse.position) -
94  receiver_)});
95  }
96  };
97 
98  push_vector(output.stochastic);
99  if (max_image_source_order_ <= step) {
100  push_vector(output.specular);
101  }
102 
103  return ret;
104  }();
105 
106  incremental_histogram(histogram_.histogram,
107  begin(intermediate),
108  end(intermediate),
109  histogram_.sample_rate,
111  }
112 
113  Histogram get_results() const { return histogram_; }
114 
115 private:
116  stochastic::finder finder_;
117  glm::vec3 receiver_;
118  core::environment environment_;
119  size_t max_image_source_order_;
120  Histogram histogram_;
121 };
122 
124 
125 template <typename Histogram>
126 class stochastic_processor final {
127 public:
129  const glm::vec3& source,
130  const glm::vec3& receiver,
131  const core::environment& environment,
132  size_t total_rays,
133  size_t max_image_source_order,
134  float receiver_radius,
135  float histogram_sample_rate)
136  : cc_{cc}
137  , source_{source}
138  , receiver_{receiver}
139  , environment_{environment}
140  , total_rays_{total_rays}
141  , max_image_source_order_{max_image_source_order}
142  , receiver_radius_{receiver_radius}
143  , histogram_sample_rate_{histogram_sample_rate}
144  , histogram_{histogram_sample_rate} {}
145 
146  stochastic_group_processor<Histogram> get_group_processor(
147  size_t num_directions) const {
148  return {cc_,
149  source_,
150  receiver_,
151  environment_,
152  total_rays_,
153  max_image_source_order_,
154  receiver_radius_,
155  histogram_sample_rate_,
156  num_directions};
157  }
158 
159  void accumulate(const stochastic_group_processor<Histogram>& processor) {
160  sum_histograms(histogram_, processor.get_results());
161  }
162 
163  Histogram get_results() const { return histogram_; }
164 
165 private:
167  glm::vec3 source_;
168  glm::vec3 receiver_;
169  core::environment environment_;
170  size_t total_rays_;
171  size_t max_image_source_order_;
172  float receiver_radius_;
173  float histogram_sample_rate_;
174 
175  Histogram histogram_;
176 };
177 
179 
181 public:
182  make_stochastic_histogram(size_t total_rays,
183  size_t max_image_source_order,
184  float receiver_radius,
185  float histogram_sample_rate);
186 
188  const core::compute_context& cc,
189  const glm::vec3& source,
190  const glm::vec3& receiver,
191  const core::environment& environment,
193  cl_float3,
194  core::surface<core::simulation_bands>>& voxelised) const;
195 
196 private:
197  size_t total_rays_;
198  size_t max_image_source_order_;
199  float receiver_radius_;
200  float histogram_sample_rate_;
201 };
202 
204 public:
205  make_directional_histogram(size_t total_rays,
206  size_t max_image_source_order,
207  float receiver_radius,
208  float histogram_sample_rate);
209 
211  get_processor(
212  const core::compute_context& cc,
213  const glm::vec3& source,
214  const glm::vec3& receiver,
215  const core::environment& environment,
217  cl_float3,
218  core::surface<core::simulation_bands>>& voxelised) const;
219 
220 private:
221  size_t total_rays_;
222  size_t max_image_source_order_;
223  float receiver_radius_;
224  float histogram_sample_rate_;
225 };
226 
227 } // namespace reflection_processor
228 } // namespace raytracer
229 } // namespace wayverb
Definition: scene_buffers.h:12
Definition: structs.h:38
Definition: pressure.h:22
Definition: environment.h:6
Definition: capsule_base.h:9
stochastic_group_processor(const core::compute_context &cc, const glm::vec3 &source, const glm::vec3 &receiver, const core::environment &environment, size_t total_rays, size_t max_image_source_order, float receiver_radius, float histogram_sample_rate, size_t group_items)
Definition: stochastic_histogram.h:49
invariant: device is a valid device for the context
Definition: common.h:13
Definition: conversions.h:26
Definition: voxelised_scene_data.h:14