Skip to content
Snippets Groups Projects
StratifiedSampler.cpp 733 B
Newer Older
Postea's avatar
Postea committed
#include "StratifiedSampler.h"
Postea's avatar
Postea committed
namespace util {
Yoel's avatar
Yoel committed
StratifiedSampler::StratifiedSampler(const std::shared_ptr<Sampler>& sampler,
                                     size_t n)
    : sampler(sampler), n(std::ceil(std::sqrt(n))){};
Postea's avatar
Postea committed

Yoel's avatar
Yoel committed
Vec3 StratifiedSampler::color(float x, float y) const {
	Vec3 color(0, 0, 0);
	for (int xi = 0; xi < n; xi++) {
		for (int yi = 0; yi < n; yi++) {
Yoel's avatar
Yoel committed
			float rx = dis0to1(gen);
			float ry = dis0to1(gen);
			float xs = x + (xi + rx) / n;
			float ys = y + (yi + ry) / n;
Yoel's avatar
Yoel committed
			color = color + sampler->color(xs, ys);
		}
	}
	// if (color != Vec3 (16, 16, 16) && color != Vec3 (0, 0, 8))
	// std::cout << color << " and " << color / (n_ * n_) << n_ << std::endl;
	return color / (n * n);
Yoel's avatar
Yoel committed
}  // namespace util