00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "random.h"
00025 #include "vegas.h"
00026
00027 #include "lowdiscrepancypx.h"
00028 #include "linear.h"
00029 #include "tilepx.h"
00030 #include "scene.h"
00031 #include "hilbertpx.h"
00032 #include "dynload.h"
00033 #include "error.h"
00034
00035 using namespace lux;
00036
00037
00038 RandomSampler* RandomSampler::clone() const
00039 {
00040 return new RandomSampler(*this);
00041 }
00042
00043 RandomSampler::RandomSampler(int xstart, int xend,
00044 int ystart, int yend, int ps, string pixelsampler)
00045 : Sampler(xstart, xend, ystart, yend, ps)
00046 {
00047 xPos = xPixelStart;
00048 yPos = yPixelStart;
00049 pixelSamples = ps;
00050
00051 init = true;
00052
00053
00054 if(pixelsampler == "vegas")
00055 pixelSampler = new VegasPixelSampler(xstart, xend, ystart, yend);
00056 else if(pixelsampler == "lowdiscrepancy")
00057 pixelSampler = new LowdiscrepancyPixelSampler(xstart, xend, ystart, yend);
00058
00059
00060 else if((pixelsampler == "tile") || (pixelsampler == "grid"))
00061 pixelSampler = new TilePixelSampler(xstart, xend, ystart, yend);
00062 else if(pixelsampler == "hilbert")
00063 pixelSampler = new HilbertPixelSampler(xstart, xend, ystart, yend);
00064 else
00065 pixelSampler = new LinearPixelSampler(xstart, xend, ystart, yend);
00066
00067 TotalPixels = pixelSampler->GetTotalPixels();
00068
00069
00070 imageSamples = AllocAligned<float>(7 * pixelSamples);
00071 lensSamples = imageSamples + 2 * pixelSamples;
00072 timeSamples = lensSamples + 2 * pixelSamples;
00073 wavelengthsSamples = timeSamples + pixelSamples;
00074 singleWavelengthSamples = wavelengthsSamples + pixelSamples;
00075
00076 samplePos = pixelSamples;
00077 }
00078
00079 RandomSampler::~RandomSampler()
00080 {
00081 FreeAligned(imageSamples);
00082 }
00083
00084
00085 u_int RandomSampler::GetTotalSamplePos()
00086 {
00087 return TotalPixels;
00088 }
00089
00090 bool RandomSampler::GetNextSample(Sample *sample, u_int *use_pos)
00091 {
00092 sample->sampler = this;
00093
00094 if(init) {
00095 init = false;
00096
00097 contribBuffer = film->scene->contribPool->Next(NULL);
00098 }
00099
00100
00101 bool haveMoreSample = true;
00102 if (samplePos == pixelSamples) {
00103
00104 if(!pixelSampler->GetNextPixel(xPos, yPos, use_pos)) {
00105
00106
00107 if (film->enoughSamplePerPixel) {
00108
00109 pixelSampler->renderingDone = true;
00110 haveMoreSample = false;
00111 }
00112 } else
00113 haveMoreSample = (!pixelSampler->renderingDone);
00114
00115 for (int i = 0; i < 7 * pixelSamples; ++i) {
00116 imageSamples[i] = tspack->rng->floatValue();
00117 }
00118
00119 samplePos = 0;
00120 }
00121
00122 if (samplePos >= pixelSamples-1)
00123 *use_pos = -1;
00124
00125 sample->imageX = xPos + imageSamples[2*samplePos];
00126 sample->imageY = yPos + imageSamples[2*samplePos+1];
00127 sample->lensU = lensSamples[2*samplePos];
00128 sample->lensV = lensSamples[2*samplePos+1];
00129 sample->time = timeSamples[samplePos];
00130 sample->wavelengths = wavelengthsSamples[samplePos];
00131 sample->singleWavelength = singleWavelengthSamples[samplePos];
00132
00133 for (u_int i = 0; i < sample->n1D.size(); ++i) {
00134 for (u_int j = 0; j < sample->n1D[i]; ++j)
00135 sample->oneD[i][j] = tspack->rng->floatValue();
00136 }
00137 for (u_int i = 0; i < sample->n2D.size(); ++i) {
00138 for (u_int j = 0; j < 2*sample->n2D[i]; ++j)
00139 sample->twoD[i][j] = tspack->rng->floatValue();
00140 }
00141
00142 ++samplePos;
00143
00144 return haveMoreSample;
00145 }
00146
00147 float *RandomSampler::GetLazyValues(Sample *sample, u_int num, u_int pos)
00148 {
00149 float *data = sample->xD[num] + pos * sample->dxD[num];
00150 for (u_int i = 0; i < sample->dxD[num]; ++i)
00151 data[i] = tspack->rng->floatValue();
00152 return data;
00153 }
00154
00155 Sampler* RandomSampler::CreateSampler(const ParamSet ¶ms, const Film *film)
00156 {
00157 int nsamp = params.FindOneInt("pixelsamples", -1);
00158
00159 if (nsamp < 0) {
00160 luxError(LUX_NOERROR, LUX_WARNING,
00161 "Parameters 'xsamples' and 'ysamples' are deprecated, use 'pixelsamples' instead");
00162 int xsamp = params.FindOneInt("xsamples", 2);
00163 int ysamp = params.FindOneInt("ysamples", 2);
00164 nsamp = xsamp*ysamp;
00165 }
00166
00167 string pixelsampler = params.FindOneString("pixelsampler", "vegas");
00168 int xstart, xend, ystart, yend;
00169 film->GetSampleExtent(&xstart, &xend, &ystart, ¥d);
00170 return new RandomSampler(xstart, xend,
00171 ystart, yend,
00172 nsamp, pixelsampler);
00173 }
00174
00175 static DynamicLoader::RegisterSampler<RandomSampler> r("random");