author | Radek Brich <radek.brich@devl.cz> |
Mon, 08 Sep 2014 20:14:24 +0200 | |
branch | pyrit |
changeset 102 | de3e9ea18f56 |
parent 94 | 4c8abb8977dc |
permissions | -rw-r--r-- |
94 | 1 |
/** |
2 |
* @file sampler.h |
|
3 |
* @brief Screen sample generation and image reconstruction |
|
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
4 |
* |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
5 |
* This file is part of Pyrit Ray Tracer. |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
6 |
* |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
7 |
* Copyright 2008 Radek Brich |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
8 |
* |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
9 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
10 |
* of this software and associated documentation files (the "Software"), to deal |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
11 |
* in the Software without restriction, including without limitation the rights |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
12 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
13 |
* copies of the Software, and to permit persons to whom the Software is |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
14 |
* furnished to do so, subject to the following conditions: |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
15 |
* |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
16 |
* The above copyright notice and this permission notice shall be included in |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
17 |
* all copies or substantial portions of the Software. |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
18 |
* |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
19 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
20 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
21 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
22 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
23 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
24 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
25 |
* THE SOFTWARE. |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
26 |
*/ |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
27 |
|
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
28 |
#ifndef SAMPLER_H |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
29 |
#define SAMPLER_H |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
30 |
|
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
31 |
#include "common.h" |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
32 |
#include "vector.h" |
88
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
33 |
#include "pixmap.h" |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
34 |
|
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
35 |
using namespace std; |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
36 |
|
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
/** |
94 | 38 |
* A structure containing sample's coordinates. |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
39 |
*/ |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
40 |
class Sample |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
41 |
{ |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
42 |
public: |
94 | 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 |
*/ |
|
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
54 |
Float x,y; |
94 | 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 |
*/ |
|
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
64 |
int sx,sy,osa_samp; |
94 | 65 |
//@} |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
66 |
}; |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
67 |
|
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
68 |
/** |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
69 |
* A abstract sampler. |
94 | 70 |
* |
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 |
|
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
73 |
* number of samples for each phase, then samples can be generated using |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
74 |
* nextSample method. The resulting colour of each sample should be returned |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
75 |
* via saveSample method. The sampler should save the results to given buffer |
94 | 76 |
* and decide if another phase is needed. When the picture is complete, |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
77 |
* initSampleSet returns zero and picture can be read from buffer. |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
78 |
*/ |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
79 |
class Sampler |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
80 |
{ |
82
930a2d3ecaed
prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
81 |
protected: |
94 | 82 |
Pixmap pixmap; ///< A pixmap for resulting image. |
83 |
bool packetable; ///< True if the generated samples are usable with ray packets. |
|
82
930a2d3ecaed
prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
84 |
public: |
94 | 85 |
/** Constructor with user provided image buffer. */ |
88
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
86 |
Sampler(Float *buffer, const int &w, const int &h): |
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
87 |
pixmap(buffer, w, h), packetable(false) {}; |
94 | 88 |
|
89 |
/** With this constructor, image buffer is allocated as specified by w,h. */ |
|
88
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
90 |
Sampler(const int &w, const int &h): |
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
91 |
pixmap(w, h), packetable(false) {}; |
94 | 92 |
|
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
93 |
virtual ~Sampler() {}; |
94 | 94 |
|
95 |
/** Switch to new user provided buffer. */ |
|
88
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
96 |
void resetBuffer(Float *buffer, int &w, int &h) |
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
97 |
{ pixmap.setData(buffer, w, h); }; |
94 | 98 |
|
99 |
/** |
|
100 |
* Initialize the sampler. |
|
101 |
* |
|
102 |
* This must be called first when starting new image. |
|
103 |
*/ |
|
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
104 |
virtual void init() = 0; |
94 | 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 |
*/ |
|
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
113 |
virtual int initSampleSet() = 0; |
94 | 114 |
|
115 |
/** |
|
116 |
* Generate next sample. |
|
117 |
* |
|
118 |
* @param[out] s The sample. |
|
119 |
* @retval false No more samples in this phase. |
|
120 |
*/ |
|
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
121 |
virtual bool nextSample(Sample *s) = 0; |
94 | 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. */ |
|
82
930a2d3ecaed
prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
132 |
bool packetableSamples() { return packetable; }; |
94 | 133 |
|
134 |
/** Get the pixmap. */ |
|
90
f6a72eb99631
rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents:
88
diff
changeset
|
135 |
const Pixmap &getPixmap() const { return pixmap; }; |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
136 |
}; |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
137 |
|
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
138 |
/** |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
139 |
* Default sampler. |
94 | 140 |
* |
48
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
141 |
* Implements basic adaptive subsampling and oversampling. |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
142 |
*/ |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
143 |
class DefaultSampler: public Sampler |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
144 |
{ |
94 | 145 |
int phase; ///< Current phase |
146 |
int subsample; ///< Subsampling mode |
|
147 |
int oversample; ///< Oversampling mode |
|
148 |
//@{ |
|
149 |
/** Properties of last generated sample. */ |
|
150 |
int sx,sy,osa_samp; |
|
151 |
//@} |
|
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
152 |
public: |
94 | 153 |
/** Constructor with user provided image buffer. */ |
88
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
154 |
DefaultSampler(Float *buffer, const int &w, const int &h): |
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
155 |
Sampler(buffer, w, h), phase(-1), subsample(0), oversample(0) {}; |
94 | 156 |
|
157 |
/** With this constructor, image buffer is allocated as specified by w,h. */ |
|
88
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
158 |
DefaultSampler(const int &w, const int &h): |
f7edb3b90816
merge pixmap handling from sampler, material.h and ccdemos's image module to new Pixmap class
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
159 |
Sampler(w, h), phase(-1), subsample(0), oversample(0) {}; |
94 | 160 |
|
161 |
/* Implement virtuals from Sampler. */ |
|
48
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
162 |
void init(); |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
163 |
int initSampleSet(); |
53
228cb8bfdd54
slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents:
50
diff
changeset
|
164 |
bool nextSample(Sample *s); |
94 | 165 |
void saveSample(const Sample &samp, const Colour &col); |
48
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
166 |
|
94 | 167 |
/** |
168 |
* Set subsampling mode. |
|
169 |
* |
|
170 |
* @param[in] sub 0,1 = no, 1+ = size of sampling grid |
|
171 |
*/ |
|
48
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
172 |
void setSubsample(int sub) { subsample = sub; }; |
94 | 173 |
/** Get subsampling mode. @see setSubsample */ |
48
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
174 |
int getSubsample() { return subsample; }; |
94 | 175 |
|
176 |
/** |
|
177 |
* Set oversampling mode. |
|
178 |
* |
|
179 |
* @param[in] osa 0 = no, 1 = 4x, 2 = 9x, 3 = 16x |
|
180 |
*/ |
|
48
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
181 |
void setOversample(int osa) { oversample = osa; }; |
94 | 182 |
/** Get oversampling mode. @see setOversample */ |
48
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
183 |
int getOversample() { return oversample; }; |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
184 |
}; |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
185 |
|
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
186 |
#endif |