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 #include "randompx.h"
00027 #include "lowdiscrepancypx.h"
00028 #include "linear.h"
00029 #include "tilepx.h"
00030
00031 using namespace lux;
00032
00033
00034 RandomSampler* RandomSampler::clone() const
00035 {
00036 return new RandomSampler(*this);
00037 }
00038
00039 RandomSampler::RandomSampler(int xstart, int xend,
00040 int ystart, int yend, int xs, int ys, string pixelsampler)
00041 : Sampler(xstart, xend, ystart, yend, xs * ys)
00042 {
00043 xPos = xPixelStart;
00044 yPos = yPixelStart;
00045 xPixelSamples = xs;
00046 yPixelSamples = ys;
00047
00048
00049 if(pixelsampler == "vegas")
00050 pixelSampler = new VegasPixelSampler(xstart, xend, ystart, yend);
00051 else if(pixelsampler == "lowdiscrepancy")
00052 pixelSampler = new LowdiscrepancyPixelSampler(xstart, xend, ystart, yend);
00053 else if(pixelsampler == "random")
00054 pixelSampler = new RandomPixelSampler(xstart, xend, ystart, yend);
00055 else if((pixelsampler == "tile") || (pixelsampler == "grid"))
00056 pixelSampler = new TilePixelSampler(xstart, xend, ystart, yend);
00057 else
00058 pixelSampler = new LinearPixelSampler(xstart, xend, ystart, yend);
00059
00060 TotalPixels = pixelSampler->GetTotalPixels();
00061
00062
00063 imageSamples = (float *)AllocAligned(7 * xPixelSamples * yPixelSamples *
00064 sizeof(float));
00065 lensSamples = imageSamples + 2 * xPixelSamples * yPixelSamples;
00066 timeSamples = lensSamples + 2 * xPixelSamples * yPixelSamples;
00067 wavelengthsSamples = timeSamples + xPixelSamples * yPixelSamples;
00068 singleWavelengthSamples = wavelengthsSamples + xPixelSamples * yPixelSamples;
00069
00070 samplePos = xPixelSamples * yPixelSamples;
00071 }
00072
00073 RandomSampler::~RandomSampler()
00074 {
00075 FreeAligned(imageSamples);
00076 }
00077
00078
00079 u_int RandomSampler::GetTotalSamplePos()
00080 {
00081 return TotalPixels;
00082 }
00083
00084 bool RandomSampler::GetNextSample(Sample *sample, u_int *use_pos)
00085 {
00086 sample->sampler = this;
00087
00088
00089 bool haveMoreSample = true;
00090 if (samplePos == xPixelSamples * yPixelSamples) {
00091
00092 if(!pixelSampler->GetNextPixel(xPos, yPos, use_pos)) {
00093
00094
00095 if ((film->haltSamplePerPixel > 0) && film->enoughSamplePerPixel) {
00096
00097 pixelSampler->renderingDone = true;
00098 haveMoreSample = false;
00099 }
00100 } else
00101 haveMoreSample = (!pixelSampler->renderingDone);
00102
00103 for (int i = 0; i < 7 * xPixelSamples * yPixelSamples; ++i) {
00104 imageSamples[i] = lux::random::floatValue();
00105 }
00106
00107
00108 for (int o = 0; o < 2 * xPixelSamples * yPixelSamples; o += 2) {
00109 imageSamples[o] += xPos;
00110 imageSamples[o + 1] += yPos;
00111 }
00112 samplePos = 0;
00113 }
00114
00115 if (samplePos >= xPixelSamples * yPixelSamples-1)
00116 *use_pos = -1;
00117
00118 sample->imageX = imageSamples[2*samplePos];
00119 sample->imageY = imageSamples[2*samplePos+1];
00120 sample->lensU = lensSamples[2*samplePos];
00121 sample->lensV = lensSamples[2*samplePos+1];
00122 sample->time = timeSamples[samplePos];
00123 sample->wavelengths = wavelengthsSamples[samplePos];
00124 sample->singleWavelength = singleWavelengthSamples[samplePos];
00125
00126 for (u_int i = 0; i < sample->n1D.size(); ++i) {
00127 for (u_int j = 0; j < sample->n1D[i]; ++j)
00128 sample->oneD[i][j] = lux::random::floatValue();
00129 }
00130 for (u_int i = 0; i < sample->n2D.size(); ++i) {
00131 for (u_int j = 0; j < 2*sample->n2D[i]; ++j)
00132 sample->twoD[i][j] = lux::random::floatValue();
00133 }
00134
00135 ++samplePos;
00136
00137 return haveMoreSample;
00138 }
00139
00140 float *RandomSampler::GetLazyValues(Sample *sample, u_int num, u_int pos)
00141 {
00142 float *data = sample->xD[num] + pos * sample->dxD[num];
00143 for (u_int i = 0; i < sample->dxD[num]; ++i)
00144 data[i] = lux::random::floatValue();
00145 return data;
00146 }
00147
00148 Sampler* RandomSampler::CreateSampler(const ParamSet ¶ms, const Film *film)
00149 {
00150 int xsamp = params.FindOneInt("xsamples", 2);
00151 int ysamp = params.FindOneInt("ysamples", 2);
00152 string pixelsampler = params.FindOneString("pixelsampler", "vegas");
00153 int xstart, xend, ystart, yend;
00154 film->GetSampleExtent(&xstart, &xend, &ystart, ¥d);
00155 return new RandomSampler(xstart, xend,
00156 ystart, yend,
00157 xsamp, ysamp, pixelsampler);
00158 }