19#ifndef RESONANCE_AUDIO_GEOMETRICAL_ACOUSTICS_SAMPLING_H_
20#define RESONANCE_AUDIO_GEOMETRICAL_ACOUSTICS_SAMPLING_H_
25#include "base/constants_and_types.h"
26#include "base/logging.h"
36inline Eigen::Vector3f UniformSampleSphere(
float u,
float v) {
37 DCHECK(u >= 0.0f && u <= 1.0f);
38 DCHECK(v >= 0.0f && v <= 1.0f);
39 const float cos_theta = 1.0f - 2.0f * v;
40 const float sin_theta = 2.0f * std::sqrt(v * (1.0f - v));
41 const float phi = kTwoPi * u;
42 return Eigen::Vector3f(std::cos(phi) * sin_theta, std::sin(phi) * sin_theta,
56inline Eigen::Vector3f StratifiedSampleSphere(
float u,
float v,
57 size_t sqrt_num_samples,
58 size_t sample_index) {
59 DCHECK(u >= 0.0f && u <= 1.0f);
60 DCHECK(v >= 0.0f && v <= 1.0f);
65 const float cell_x =
static_cast<float>(sample_index / sqrt_num_samples);
66 const float cell_y =
static_cast<float>(sample_index % sqrt_num_samples);
71 const float cell_width = 1.0f /
static_cast<float>(sqrt_num_samples);
72 return UniformSampleSphere((cell_x + u) * cell_width,
73 (cell_y + v) * cell_width);
81static Eigen::Vector3f CosineSampleHemisphere(
float u,
float v) {
82 DCHECK(u >= 0.0f && u <= 1.0f);
83 DCHECK(v >= 0.0f && v <= 1.0f);
84 const float cos_theta = std::sqrt(1.0f - v);
85 const float sin_theta = std::sqrt(v);
86 const float phi = kTwoPi * u;
87 return Eigen::Vector3f(std::cos(phi) * sin_theta, std::sin(phi) * sin_theta,
98static Eigen::Vector3f CosineSampleHemisphere(
99 float u,
float v,
const Eigen::Vector3f& unit_normal) {
100 Eigen::Vector3f local_vector = CosineSampleHemisphere(u, v);
102 return Eigen::Quaternionf::FromTwoVectors(Eigen::Vector3f::UnitZ(),
115static float CosineSampleHemispherePdf(
const Eigen::Vector3f& unit_normal,
116 const Eigen::Vector3f& unit_direction) {
117 const float cos_theta = unit_normal.dot(unit_direction);
121 return cos_theta >= 0.0f ? cos_theta / kPi : 0.0f;