geom/sample: make the extent surface sampler public

This commit is contained in:
Danny Robson 2018-05-01 16:02:55 +10:00
parent 8b47a2e350
commit a1aa0c6949
2 changed files with 32 additions and 28 deletions

View File

@ -23,6 +23,7 @@
#include <cstddef> #include <cstddef>
#include <random>
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
namespace util::geom { namespace util::geom {
@ -87,6 +88,37 @@ namespace util::geom {
template <typename ShapeT> template <typename ShapeT>
sampler (ShapeT const&) -> sampler<std::decay_t<ShapeT>>; sampler (ShapeT const&) -> sampler<std::decay_t<ShapeT>>;
//---------------------------------------------------------------------
template <size_t S, typename T>
class sampler<util::extent<S,T>> {
public:
sampler (util::extent<S,T> _target):
target (_target)
{ ; }
template <typename GeneratorT>
util::point<S,T>
operator() (GeneratorT &&gen) const noexcept
{
util::point<S,T> p;
for (size_t i = 0; i < S; ++i) {
if constexpr (std::is_floating_point_v<T>) {
p[i] = std::uniform_real_distribution<T> (0, target[i]) (gen);
} else {
CHECK_GE (target[i], T{1});
p[i] = std::uniform_int_distribution<T> (0, target[i] - 1) (gen);
}
}
return p;
}
private:
util::extent<S,T> target;
};
/// approximate a poisson disc sampling through mitchell's best candidate. /// approximate a poisson disc sampling through mitchell's best candidate.
/// ///
/// try to keep adding a new point to a set. each new point is the /// try to keep adding a new point to a set. each new point is the

View File

@ -8,35 +8,7 @@
#include <random> #include <random>
template <size_t S>
class util::geom::surface::sampler<util::extent<S,float>> {
public:
sampler (util::extent<S,float> _target):
target (_target)
{ ; }
template <typename GeneratorT>
util::point<S,float>
operator() (GeneratorT &&gen) const noexcept
{
util::point<S,float> p;
for (size_t i = 0; i < S; ++i)
p[i] = std::uniform_real_distribution<float> (0, target[i]) (gen);
return p;
}
private:
util::extent<S,float> target;
};
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
struct height {
float
operator() (util::point2f p) const { return std::pow (p.y / 100, 3.f); }
};
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {