Regular sampling class definitions and test

This algorithm is a uniform sampling algorithm, inherited from the Sampler class.

Class declaration:

#pragma once
#ifndef __REGULAR_HEADER__
#define __REGULAR_HEADER__

#include "sampler.h"

class Regular :public Sampler {
public:
	Regular();
	~Regular();
	Regular(const integer samps);
	Regular(const integer samps, const integer sets);
	Regular(const Regular& reg);
	Regular& operator=(const Regular& reg);
	virtual Sampler* clone() const;
	virtual void generate_samples();
};
#endif  

Class implementation:

#include "pch.h"
#include "regular.h"

Regular::Regular() :Sampler() {
	generate_samples();
}

Regular::~Regular() {}

Regular::Regular(const integer samps) : Sampler(samps) {
	generate_samples();
}

Regular::Regular(const integer samps, const integer sets) : Sampler(samps, sets) {
	generate_samples();
}

Regular::Regular(const Regular& reg) : Sampler(reg) {
	generate_samples();
}

Regular& Regular::operator=(const Regular& reg) {
	if (this == &reg)
		return *this;
	Sampler::operator=(reg);
	return *this;
}

Sampler* Regular::clone() const {
	return new Regular(*this);
}

void Regular::generate_samples() {
	integer n = (integer)std::sqrt((ldouble)nsamples);
	for (integer p = 0; p < nsets; p++) {
		for (integer i=0;i<n;i++)
			for (integer j = 0; j < n; j++) {
				Point2 sp((j + 0.5) / n, (i + 0.5) / n); //均匀采样
				samples.push_back(sp);
			}
	}
}

 

Test sampling algorithm:

Sampler class and added to the number of samples ViewPlane category, increasing function (if not specifically stated, the modification will not be repeated portion)

void set_samples(const integer n);

void set_sampler(Sampler* const sp);

ViewPlane particular class declaration is as follows:

#pragma once
#ifndef __VIEWPLANE_HEADER__
#define __VIEWPLANE_HEADER__

#include "../../Types.h"

class Sampler;

class ViewPlane {
public:
	ViewPlane();
	ViewPlane(const ViewPlane& vp);
	void set_hres(const integer hr);
	void set_vres(const integer vr);
	void set_pixelsize(const ldouble ps);
	void set_gamma(const ldouble ga);
	void set_samples(const integer n);//新增
	void set_sampler(Sampler* const sp);//新增
	integer hres;
	integer vres;
	integer nsamples;//新增
	ldouble s;
	ldouble g;
	Sampler* sampler;//新增
};

#endif 

Need to modify the class members:

#include "pch.h"
#include "viewplane.h"
#include "../samplers/jittered.h"


ViewPlane::ViewPlane() :hres(200), vres(100), s(0.02), g(1), nsamples(16), sampler(nullptr) {}

ViewPlane::ViewPlane(const ViewPlane& vp) 
	: hres(vp.hres), vres(vp.vres), s(vp.s), g(vp.g), nsamples(vp.nsamples), sampler(vp.sampler) {}

...

void ViewPlane::set_samples(const integer n) {//和书上不同,剔除特殊性,增加兼容性
	nsamples = n;
	sampler->clear();
	sampler->set_num_samples(nsamples);
	sampler->setup_shuffled_indices();
	sampler->generate_samples();
}

void ViewPlane::set_sampler(Sampler* const sp) {
	if (sampler != nullptr) {
		delete sampler;
		sampler = nullptr;
	}
	nsamples = sp->get_num_samples();
	sampler = sp;
}

  

World class only modify build and render parts:

void World::build() {
	vp.set_hres(200);
	vp.set_vres(100);
	vp.set_sampler(new Regular());//所有采样修改都在这里测试
	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);
}

Unless otherwise render part of the statement, after the modified part will not be repeated.

void World::render() {
	Ray ray;
	ldouble x, y;
	open_window(vp.hres, vp.vres);
	Point3 sp;
	ray.o = Point3(0, 0, 1);
	for (integer r = vp.vres - 1; r >= 0; r--)//render from left-corner to right-corner
		for (integer c = 0; c < vp.hres; c++) {
			RGBColor color;
			for (integer p = 0; p < vp.nsamples; p++) {//增加样本
				sp = vp.sampler->sample_unit_square();
				x = vp.s * (c - 0.5 * vp.hres + sp.x);
				y = vp.s * (r - 0.5 * vp.vres + sp.y);
				ray.d = Point3(x, y, -1);
				color += tracer_ptr->trace_ray(ray);
			}
			color /= vp.nsamples;
			display_pixel(r, c, color);
		}
}

  

After modification, operating procedures, test results are as follows (after amplification and found a lot of rounded corners, then we test other sampling algorithm):

Guess you like

Origin www.cnblogs.com/dalgleish/p/12602750.html