include/sampler.h
branchpyrit
changeset 94 4c8abb8977dc
parent 93 96d65f841791
equal deleted inserted replaced
93:96d65f841791 94:4c8abb8977dc
     1 /*
     1 /**
     2  * sampler.h: screen sample generation and image reconstruction
     2  * @file  sampler.h
       
     3  * @brief Screen sample generation and image reconstruction
     3  *
     4  *
     4  * This file is part of Pyrit Ray Tracer.
     5  * This file is part of Pyrit Ray Tracer.
     5  *
     6  *
     6  * Copyright 2008  Radek Brich
     7  * Copyright 2008  Radek Brich
     7  *
     8  *
    32 #include "pixmap.h"
    33 #include "pixmap.h"
    33 
    34 
    34 using namespace std;
    35 using namespace std;
    35 
    36 
    36 /**
    37 /**
    37  * sample
    38  * A structure containing sample's coordinates.
    38  */
    39  */
    39 class Sample
    40 class Sample
    40 {
    41 {
    41 	friend class Sampler;
       
    42 public:
    42 public:
       
    43 	//@{
       
    44 	/**
       
    45 	 * Generated coordinates.
       
    46 	 *
       
    47 	 * These are coordinates for use by Camera.
       
    48 	 * Middle of the screen plane has [0,0],
       
    49 	 * top of the screen has y=-0.5, left pixels has x<0
       
    50 	 * and the rest of samples are aligned to regular grid,
       
    51 	 * so the vertical size is always one and horizontal size
       
    52 	 * can be wider depending on aspect ratio.
       
    53 	 */
    43 	Float x,y;
    54 	Float x,y;
       
    55 	//@}
       
    56 
       
    57 	//@{
       
    58 	/**
       
    59 	 * Auxiliary coordinates used internally by sampler.
       
    60 	 *
       
    61 	 * Screen coordinates sx, sy are integer coordinates of screen pixels,
       
    62 	 * osa_samp is used by oversampling to remember current sample.
       
    63 	 */
    44 	int sx,sy,osa_samp;
    64 	int sx,sy,osa_samp;
       
    65 	//@}
    45 };
    66 };
    46 
    67 
    47 /**
    68 /**
    48  * A abstract sampler.
    69  * A abstract sampler.
    49  * It generates screen samples in coordinates between [-1..1] for height
    70  *
    50  * and [-w/h..w/h] for width. It works in phases: initSampleSet returns
    71  * It generates screen samples in coordinates between [-0.5..0.5] for height
       
    72  * and [-w/h/2..w/h/2] for width. It works in phases: initSampleSet returns
    51  * number of samples for each phase, then samples can be generated using
    73  * number of samples for each phase, then samples can be generated using
    52  * nextSample method. The resulting colour of each sample should be returned
    74  * nextSample method. The resulting colour of each sample should be returned
    53  * via saveSample method. The sampler should save the results to given buffer
    75  * via saveSample method. The sampler should save the results to given buffer
    54  * and decide if other phase is needed. When the picture is complete,
    76  * and decide if another phase is needed. When the picture is complete,
    55  * initSampleSet returns zero and picture can be read from buffer.
    77  * initSampleSet returns zero and picture can be read from buffer.
    56  */
    78  */
    57 class Sampler
    79 class Sampler
    58 {
    80 {
    59 protected:
    81 protected:
    60 	Pixmap pixmap;
    82 	Pixmap pixmap;    ///< A pixmap for resulting image.
    61 	bool packetable;
    83 	bool packetable;  ///< True if the generated samples are usable with ray packets.
    62 public:
    84 public:
       
    85 	/** Constructor with user provided image buffer. */
    63 	Sampler(Float *buffer, const int &w, const int &h):
    86 	Sampler(Float *buffer, const int &w, const int &h):
    64 		pixmap(buffer, w, h), packetable(false) {};
    87 		pixmap(buffer, w, h), packetable(false) {};
       
    88 
       
    89 	/** With this constructor, image buffer is allocated as specified by w,h. */
    65 	Sampler(const int &w, const int &h):
    90 	Sampler(const int &w, const int &h):
    66 		pixmap(w, h), packetable(false) {};
    91 		pixmap(w, h), packetable(false) {};
       
    92 
    67 	virtual ~Sampler() {};
    93 	virtual ~Sampler() {};
       
    94 
       
    95 	/** Switch to new user provided buffer. */
    68 	void resetBuffer(Float *buffer, int &w, int &h)
    96 	void resetBuffer(Float *buffer, int &w, int &h)
    69 		{ pixmap.setData(buffer, w, h); };
    97 		{ pixmap.setData(buffer, w, h); };
       
    98 
       
    99 	/**
       
   100 	 * Initialize the sampler.
       
   101 	 *
       
   102 	 * This must be called first when starting new image.
       
   103 	 */
    70 	virtual void init() = 0;
   104 	virtual void init() = 0;
       
   105 
       
   106 	/**
       
   107 	 * Prepare for next phase.
       
   108 	 *
       
   109 	 * Must be called before reading samples.
       
   110 	 * @retval N approximate number of samples to be generated in the phase
       
   111 	 * @retval 0 last phase, image is complete
       
   112 	 */
    71 	virtual int initSampleSet() = 0;
   113 	virtual int initSampleSet() = 0;
       
   114 
       
   115 	/**
       
   116 	 * Generate next sample.
       
   117 	 *
       
   118 	 * @param[out] s  The sample.
       
   119 	 * @retval false  No more samples in this phase.
       
   120 	 */
    72 	virtual bool nextSample(Sample *s) = 0;
   121 	virtual bool nextSample(Sample *s) = 0;
    73 	virtual void saveSample(Sample &samp, Colour &col) = 0;
   122 
       
   123 	/**
       
   124 	 * Save the color of the sample.
       
   125 	 *
       
   126 	 * @param[in] samp  A sample.
       
   127 	 * @param[in] col   Color resulted from ray tracing the sample.
       
   128 	 */
       
   129 	virtual void saveSample(const Sample &samp, const Colour &col) = 0;
       
   130 
       
   131 	/** True if generated samples are usable with ray packets. */
    74 	bool packetableSamples() { return packetable; };
   132 	bool packetableSamples() { return packetable; };
       
   133 
       
   134 	/** Get the pixmap. */
    75 	const Pixmap &getPixmap() const { return pixmap; };
   135 	const Pixmap &getPixmap() const { return pixmap; };
    76 };
   136 };
    77 
   137 
    78 /**
   138 /**
    79  * Default sampler.
   139  * Default sampler.
       
   140  *
    80  * Implements basic adaptive subsampling and oversampling.
   141  * Implements basic adaptive subsampling and oversampling.
    81  */
   142  */
    82 class DefaultSampler: public Sampler
   143 class DefaultSampler: public Sampler
    83 {
   144 {
    84 	int phase;
   145 	int phase;      ///< Current phase
    85 	int subsample;  // 0,1 = no, 1+ = size of sampling grid
   146 	int subsample;  ///< Subsampling mode
    86 	int oversample; // 0 = no, 1 = 4x, 2 = 9x, 3 = 16x
   147 	int oversample; ///< Oversampling mode
    87 	int sx,sy,osa_samp; // current sample properties
   148 	//@{
       
   149 	/** Properties of last generated sample. */
       
   150 	int sx,sy,osa_samp;
       
   151 	//@}
    88 public:
   152 public:
       
   153 	/** Constructor with user provided image buffer. */
    89 	DefaultSampler(Float *buffer, const int &w, const int &h):
   154 	DefaultSampler(Float *buffer, const int &w, const int &h):
    90 		Sampler(buffer, w, h), phase(-1), subsample(0), oversample(0) {};
   155 		Sampler(buffer, w, h), phase(-1), subsample(0), oversample(0) {};
       
   156 
       
   157 	/** With this constructor, image buffer is allocated as specified by w,h. */
    91 	DefaultSampler(const int &w, const int &h):
   158 	DefaultSampler(const int &w, const int &h):
    92 		Sampler(w, h), phase(-1), subsample(0), oversample(0) {};
   159 		Sampler(w, h), phase(-1), subsample(0), oversample(0) {};
       
   160 
       
   161 	/* Implement virtuals from Sampler. */
    93 	void init();
   162 	void init();
    94 	int initSampleSet();
   163 	int initSampleSet();
    95 	bool nextSample(Sample *s);
   164 	bool nextSample(Sample *s);
    96 	void saveSample(Sample &samp, Colour &col);
   165 	void saveSample(const Sample &samp, const Colour &col);
    97 
   166 
       
   167 	/**
       
   168 	  * Set subsampling mode.
       
   169 	  *
       
   170 	  * @param[in] sub  0,1 = no, 1+ = size of sampling grid
       
   171 	  */
    98 	void setSubsample(int sub) { subsample = sub; };
   172 	void setSubsample(int sub) { subsample = sub; };
       
   173 	/** Get subsampling mode. @see setSubsample */
    99 	int getSubsample() { return subsample; };
   174 	int getSubsample() { return subsample; };
       
   175 
       
   176 	/**
       
   177 	  * Set oversampling mode.
       
   178 	  *
       
   179 	  * @param[in] osa  0 = no, 1 = 4x, 2 = 9x, 3 = 16x
       
   180 	  */
   100 	void setOversample(int osa) { oversample = osa; };
   181 	void setOversample(int osa) { oversample = osa; };
       
   182 	/** Get oversampling mode. @see setOversample */
   101 	int getOversample() { return oversample; };
   183 	int getOversample() { return oversample; };
   102 };
   184 };
   103 
   185 
   104 #endif
   186 #endif