Wayverb
optimum_reflection_number.h
1 #pragma once
2 
3 #include "utilities/aligned/vector.h"
4 
5 #include "core/scene_data.h"
6 
7 #include <algorithm>
8 #include <cmath>
9 #include <numeric>
10 #include <stdexcept>
11 
12 namespace wayverb {
13 namespace raytracer {
14 
15 constexpr auto min_element(double x) { return x; }
16 constexpr auto min_element(float x) { return x; }
17 
18 template <typename T>
19 double min_absorption(const T& t) {
20  return min_element(t.absorption);
21 }
22 
23 template <typename It>
24 double min_absorption(It begin, It end) {
25  if (begin == end) {
26  throw std::runtime_error{"Can't find min absorption of empty vector."};
27  }
28  return std::accumulate(begin + 1,
29  end,
30  min_absorption(*begin),
31  [](const auto& i, const auto& j) {
32  using std::min;
33  return min(i, min_absorption(j));
34  });
35 }
36 
38 inline size_t compute_optimum_reflection_number(double absorption) {
39  return std::ceil(-6 / std::log10(1 - absorption));
40 }
41 
42 template <typename It>
43 size_t compute_optimum_reflection_number(It begin, It end) {
44  return compute_optimum_reflection_number(min_absorption(begin, end));
45 }
46 
48 template <typename Vertex, typename Surface>
49 size_t compute_optimum_reflection_number(
50  const core::generic_scene_data<Vertex, Surface>& scene) {
51  std::vector<bool> used_surfaces_hash(scene.get_surfaces().size(), false);
52  for (const auto& tri : scene.get_triangles()) {
53  if (0 <= tri.surface && tri.surface < used_surfaces_hash.size()) {
54  used_surfaces_hash[tri.surface] = true;
55  }
56  }
57 
58  util::aligned::vector<Surface> used_surfaces;
59  used_surfaces.reserve(used_surfaces_hash.size());
60  for (size_t i = 0; i != used_surfaces_hash.size(); ++i) {
61  if (used_surfaces_hash[i]) {
62  used_surfaces.emplace_back(scene.get_surfaces()[i]);
63  }
64  }
65 
66  return compute_optimum_reflection_number(std::begin(used_surfaces),
67  std::end(used_surfaces));
68 }
69 
70 } // namespace raytracer
71 } // namespace wayverb
Definition: pressure.h:22
Definition: capsule_base.h:9