原理参照书籍
类声明:
#pragma once #ifndef __HAMMERSLEY_HEADER__ #define __HAMMERSLEY_HEADER__ #include "sampler.h" class Hammersley :public Sampler { public: Hammersley(); ~Hammersley(); Hammersley(const integer samps); Hammersley(const integer samps, const integer sets); Hammersley(const Hammersley& ham); Hammersley& operator=(const Hammersley& ham); virtual Sampler* clone() const; virtual void generate_samples(); private: ldouble phi(integer j); }; #endif
类实现:
#include "pch.h" #include "hammersley.h" Hammersley::Hammersley() :Sampler() { generate_samples(); } Hammersley::~Hammersley() {} Hammersley::Hammersley(const integer samps) :Sampler(samps) { generate_samples(); } Hammersley::Hammersley(const integer samps, const integer sets) : Sampler(samps, sets) { generate_samples(); } Hammersley::Hammersley(const Hammersley& ham) : Sampler(ham) { generate_samples(); } Hammersley& Hammersley::operator=(const Hammersley& ham) { if (this == &ham) return *this; Sampler::operator=(ham); return *this; } Sampler* Hammersley::clone() const { return new Hammersley(*this); } void Hammersley::generate_samples() { for (integer i = 0; i < nsets; i++) for (integer j = 0; j < nsamples; j++) { Point2 sp((ldouble)j / nsamples, phi(j)); samples.push_back(sp); } } ldouble Hammersley::phi(integer j) { ldouble x = 0.0, f = 0.5; while (j) { x += f * (ldouble)(j % 2); j /= 2; f *= 0.5; } return x; }
测试效果图: