Implement in-place sampling (without allocations) in pcg

This commit is contained in:
Nikita Lisitsa 2020-09-13 16:40:16 +03:00
parent 9191ab68cc
commit 66a72b89bd

View file

@ -5,6 +5,21 @@
namespace psemek::pcg
{
template <typename Gen, typename R>
void sample(Gen && gen, gfx::basic_pixmap<R> & result)
{
for (std::size_t j = 0; j < result.height(); ++j)
{
for (std::size_t i = 0; i < result.width(); ++i)
{
float ti = (i + 0.5f) / result.width();
float tj = (j + 0.5f) / result.height();
result(i, j) = gen(ti, tj);
}
}
}
template <typename Gen>
auto sample(Gen && gen, std::size_t width, std::size_t height)
{
@ -12,18 +27,9 @@ namespace psemek::pcg
gfx::basic_pixmap<R> result({width, height});
for (std::size_t j = 0; j < height; ++j)
{
for (std::size_t i = 0; i < width; ++i)
{
float ti = (i + 0.5f) / width;
float tj = (j + 0.5f) / height;
result(i, j) = gen(ti, tj);
}
}
sample(std::forward<Gen>(gen), result);
return result;
}
}