From 90ece36fd5547f0d8c986fa5bd9b1e32854ebeae Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sun, 27 Sep 2020 16:44:13 +0300 Subject: [PATCH] Add uniform distribution inside a ball --- .../psemek/pcg/random/uniform_ball.hpp | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 libs/pcg/include/psemek/pcg/random/uniform_ball.hpp diff --git a/libs/pcg/include/psemek/pcg/random/uniform_ball.hpp b/libs/pcg/include/psemek/pcg/random/uniform_ball.hpp new file mode 100644 index 00000000..5698a1ba --- /dev/null +++ b/libs/pcg/include/psemek/pcg/random/uniform_ball.hpp @@ -0,0 +1,61 @@ +#pragma once + +#include +#include + +namespace psemek::pcg +{ + + template + struct uniform_ball_vector_distribution + { + using result_type = geom::vector; + + uniform_ball_vector_distribution(T r = T{1}) + : sphere_d_{r} + {} + + uniform_ball_vector_distribution(uniform_ball_vector_distribution const &) = default; + + template + auto operator()(RNG && rng) + { + result_type v = sphere_d_(rng); + auto r = uniform_real_distribution{}(rng); + return v * std::pow(r, T{1} / N); + } + + T radius() const { return sphere_d_.radius(); } + + private: + uniform_sphere_vector_distribution sphere_d_; + }; + + template + struct uniform_ball_point_distribution + { + using result_type = geom::vector; + + uniform_ball_point_distribution(result_type origin, T r = T{1}) + : origin_{origin} + , vector_d_{r} + {} + + uniform_ball_point_distribution(uniform_ball_point_distribution const &) = default; + + template + auto operator()(RNG && rng) + { + return origin_ + vector_d_(rng); + } + + result_type origin() const { return origin_; } + + T radius() const { return vector_d_.radius(); } + + private: + result_type origin_; + uniform_ball_vector_distribution vector_d_; + }; + +}