此是随机采样算法,效果感觉一般般。
类声明:
#pragma once
#ifndef __PURERANDOM_HEADER__
#define __PURERANDOM_HEADER__
#include "sampler.h"
class PureRandom :public Sampler {
public:
PureRandom();
~PureRandom();
PureRandom(const integer samps);
PureRandom(const integer samps, const integer sets);
PureRandom(const PureRandom& pr);
PureRandom& operator=(const PureRandom& pr);
virtual Sampler* clone() const;
virtual void generate_samples();
};
#endif
类实现:
#include "pch.h"
#include "purerandom.h"
PureRandom::PureRandom() :Sampler() {
generate_samples();
}
PureRandom::~PureRandom() {}
PureRandom::PureRandom(const integer samps) :Sampler(samps) {
generate_samples();
}
PureRandom::PureRandom(const integer samps, const integer sets) : Sampler(samps, sets) {
generate_samples();
}
PureRandom::PureRandom(const PureRandom& pr) : Sampler(pr) {
generate_samples();
}
PureRandom& PureRandom::operator=(const PureRandom& pr) {
if (this == &pr)
return *this;
Sampler::operator=(pr);
return *this;
}
Sampler* PureRandom::clone() const {
return new PureRandom(*this);
}
void PureRandom::generate_samples() {
for (integer i=0;i<nsets;i++)
for (integer j = 0; j < nsamples; j++) {
Point2 sp(random_ldouble(), random_ldouble());
samples.push_back(sp);
}
}
World类需要修改build函数,之前修改的仍然有效,不再重复
void World::build() {
vp.set_hres(200);
vp.set_vres(100);
vp.set_sampler(new PureRandom());//这里使用PureRandom采样,之后都在这行代码修改,从而测试各种采样算法。
tracer_ptr = new MultiSphere(this);
Geometrics* obj = new Sphere(0, 0.5);
obj->set_color(RGBColor(1, 0, 0));
add_object(obj);
obj = new Sphere(Point3(0, -100.5, 0), 100);
obj->set_color(RGBColor(0, 0, 1));
add_object(obj);
}
测试结果和图片:
