Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pathsampler.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_BIDIR_PATHSAMPLER_H_)
21 #define __MITSUBA_BIDIR_PATHSAMPLER_H_
22 
23 #include <mitsuba/bidir/path.h>
24 #include <boost/function.hpp>
25 
27 
28 /**
29  * \brief Implements a sampling strategy that is able to produce paths using
30  * bidirectional path tracing or unidirectional volumetric path tracing.
31  *
32  * This versatile class does the heavy lifting under the hood of Mitsuba's
33  * PSSMLT implementation. It is also used to provide the Veach-MLT
34  * implementation with a luminance estimate and seed paths.
35  *
36  * \author Wenzel Jakob
37  * \ingroup libbidir
38  */
40 public:
41  /**
42  * \brief Callback type for use with \ref samplePaths()
43  *
44  * The arguments are (s, t, weight, path) where \c s denotes the number
45  * of steps from the emitter, \c is the number of steps from the sensor,
46  * and \c weight contains the importance weight associated with the sample.
47  */
48  typedef boost::function<void (int, int, Float, Path &)> PathCallback;
49 
50  /// Specifies the sampling algorithm that is internally used
51  enum ETechnique {
52  /// Bidirectional path tracing
54 
55  /// Unidirectional path tracing (via the 'volpath' plugin)
56  EUnidirectional
57  };
58 
59  /**
60  * Construct a new path sampler
61  *
62  * \param technique
63  * What path generation technique should be used (unidirectional
64  * or bidirectional path tracing?)
65  *
66  * \param scene
67  * \ref A pointer to the underlying scene
68  *
69  * \param emitterSampler
70  * Sample generator that should be used for the random walk
71  * from the emitter direction
72  *
73  * \param sensorSampler
74  * Sample generator that should be used for the random walk
75  * from the sensor direction
76  *
77  * \param directSampler
78  * Sample generator that should be used for direct sampling
79  * strategies (or \c NULL, when \c sampleDirect=\c false)
80  *
81  * \param maxDepth
82  * Maximum path depth to be visualized (-1==infinite)
83  *
84  * \param rrDepth
85  * Depth to begin using russian roulette
86  *
87  * \param excludeDirectIllum
88  * If set to true, the direct illumination component will
89  * be ignored. Note that this parameter is unrelated
90  * to the next one (\a sampleDirect) although they are
91  * named similarly.
92  *
93  * \param sampleDirect
94  * When this parameter is set to true, specialized direct
95  * sampling strategies are used for s=1 and t=1 paths.
96  *
97  * \param lightImage
98  * Denotes whether or not rendering strategies that require a 'light image'
99  * (specifically, those with <tt>t==0</tt> or <tt>t==1</tt>) are included
100  * in the rendering process.
101  */
102  PathSampler(ETechnique technique, const Scene *scene, Sampler *emitterSampler,
103  Sampler *sensorSampler, Sampler *directSampler, int maxDepth, int rrDepth,
104  bool excludeDirectIllum, bool sampleDirect, bool lightImage = true);
105 
106  /**
107  * \brief Generate a sample using the configured sampling strategy
108  *
109  * The result is stored as a series of screen-space splats (pixel position
110  * and spectral value pairs) within the parameter \c list. These can be
111  * used to implement algorithms like Bidirectional Path Tracing or Primary
112  * Sample Space MLT.
113  *
114  * \param offset
115  * Specifies the desired integer pixel position of the sample. The special
116  * value <tt>Point2i(-1)</tt> results in uniform sampling in screen space.
117  *
118  * \param list
119  * Output parameter that will receive a list of splats
120  */
121  void sampleSplats(const Point2i &offset, SplatList &list);
122 
123  /**
124  * \brief Sample a series of paths and invoke the specified callback
125  * function for each one.
126  *
127  * This function is similar to \ref sampleSplats(), but instead of
128  * returning only the contribution of the samples paths in the form of
129  * screen-space "splats", it returns the actual paths by invoking a
130  * specified callback function multiple times.
131  *
132  * This function is currently only implemented for the bidirectional
133  * sampling strategy -- i.e. it cannot be used with the unidirectional
134  * path tracer.
135  *
136  * \param offset
137  * Specifies the desired integer pixel position of the sample. The special
138  * value <tt>Point2i(-1)</tt> results in uniform sampling in screen space.
139  *
140  * \param pathCallback
141  * A callback function that will be invoked once for each
142  * path generated by the BDPT sampling strategy. The first argument
143  * specifies the path importance weight.
144  */
145  void samplePaths(const Point2i &offset, PathCallback &callback);
146 
147  /**
148  * \brief Generates a sequence of seeds that are suitable for
149  * starting a MLT Markov Chain
150  *
151  * This function additionally computes the average luminance
152  * over the image plane.
153  *
154  * \param sampleCount
155  * The number of luminance samples that will be taken
156  * \param seedCount
157  * The desired number of MLT seeds (must be > \c sampleCount)
158  * \param fineGrained
159  * This parameter only matters when the technique is set to
160  * \ref EBidirectional. It specifies whether to generate \ref PathSeed
161  * records at the granularity of entire sensor/emitter subpaths or at
162  * the granularity of their constituent sampling strategies.
163  * \param seeds
164  * A vector of resulting MLT seeds
165  * \return The average luminance over the image plane
166  */
167  Float generateSeeds(size_t sampleCount, size_t seedCount,
168  bool fineGrained, const Bitmap *importanceMap,
169  std::vector<PathSeed> &seeds);
170 
171  /**
172  * \brief Compute the average luminance over the image plane
173  * \param sampleCount
174  * The number of luminance samples that will be taken
175  */
176  Float computeAverageLuminance(size_t sampleCount);
177 
178  /**
179  * \brief Reconstruct a path from a \ref PathSeed record
180  *
181  * Given a \ref PathSeed data structure, this function rewinds
182  * the random number stream of the underlying \ref ReplayableSampler
183  * to the indicated position and recreates the associated path.
184  */
185  void reconstructPath(const PathSeed &seed,
186  const Bitmap *importanceMap, Path &result);
187 
188  /// Return the underlying memory pool
189  inline MemoryPool &getMemoryPool() { return m_pool; }
190 
192 protected:
193  /// Virtual destructor
194  virtual ~PathSampler();
195 protected:
196  ETechnique m_technique;
197  ref<const Scene> m_scene;
198  ref<SamplingIntegrator> m_integrator;
199  ref<Sampler> m_emitterSampler;
200  ref<Sampler> m_sensorSampler;
201  ref<Sampler> m_directSampler;
202  int m_maxDepth;
203  int m_rrDepth;
204  bool m_excludeDirectIllum;
205  bool m_sampleDirect;
206  bool m_lightImage;
207  int m_emitterDepth, m_sensorDepth;
208  Path m_emitterSubpath, m_sensorSubpath;
209  Path m_connectionSubpath, m_fullPath;
210  MemoryPool m_pool;
211 };
212 
213 /**
214  * \brief Stores information required to re-create a seed path (e.g. for MLT)
215  *
216  * This class makes it possible to transmit a path over the network or store
217  * it locally, while requiring very little storage to do so. This is done by
218  * describing a path using an index into a random number stream, which allows
219  * to generate it cheaply when needed.
220  */
221 struct PathSeed {
222  size_t sampleIndex; ///< Index into a rewindable random number stream
223  Float luminance; ///< Luminance value of the path (for sanity checks)
224  int s; ///< Number of steps from the luminaire
225  int t; ///< Number of steps from the eye
226 
227  inline PathSeed() { }
228 
229  inline PathSeed(size_t sampleIndex, Float luminance, int s = 0, int t = 0)
230  : sampleIndex(sampleIndex), luminance(luminance), s(s), t(t) { }
231 
232  inline PathSeed(Stream *stream) {
233  sampleIndex = stream->readSize();
234  luminance = stream->readFloat();
235  s = stream->readInt();
236  t = stream->readInt();
237  }
238 
239  void serialize(Stream *stream) const {
240  stream->writeSize(sampleIndex);
241  stream->writeFloat(luminance);
242  stream->writeInt(s);
243  stream->writeInt(t);
244  }
245 
246  std::string toString() const {
247  std::ostringstream oss;
248  oss << "PathSeed[" << endl
249  << " sampleIndex = " << sampleIndex << "," << endl
250  << " luminance = " << luminance << "," << endl
251  << " s = " << s << "," << endl
252  << " t = " << t << endl
253  << "]";
254  return oss.str();
255  }
256 };
257 
258 /**
259  * MLT work unit -- wraps a \ref PathSeed into a
260  * \ref WorkUnit instance.
261  */
262 class SeedWorkUnit : public WorkUnit {
263 public:
264  inline void set(const WorkUnit *wu) {
265  m_seed = static_cast<const SeedWorkUnit *>(wu)->m_seed;
266  m_timeout = static_cast<const SeedWorkUnit *>(wu)->m_timeout;
267  }
268 
269  inline const PathSeed &getSeed() const {
270  return m_seed;
271  }
272 
273  inline void setSeed(const PathSeed &seed) {
274  m_seed = seed;
275  }
276 
277  inline int getTimeout() const {
278  return m_timeout;
279  }
280 
281  inline void setTimeout(int timeout) {
282  m_timeout = timeout;
283  }
284 
285  inline void load(Stream *stream) {
286  m_seed = PathSeed(stream);
287  m_timeout = stream->readInt();
288  }
289 
290  inline void save(Stream *stream) const {
291  m_seed.serialize(stream);
292  stream->writeInt(m_timeout);
293  }
294 
295  inline std::string toString() const {
296  return "SeedWorkUnit[]";
297  }
298 
300 private:
301  PathSeed m_seed;
302  int m_timeout;
303 };
304 
305 /**
306  * \brief List storage for the image-space contributions ("splats") of a
307  * path sample generated using \ref PathSampler.
308  */
310  /// Represents a screen-space splat produced by a path sampling technique
311  typedef std::pair<Point2, Spectrum> Splat;
312 
313  /// A series of splats associated with the current sample
314  std::vector<Splat> splats;
315  /// Combined luminance of all splats in this sample
317  /// Total number of samples in the splat list
318  int nSamples;
319 
320  inline SplatList() : luminance(0.0f), nSamples(0) { }
321 
322  /// Appends a splat entry to the list
323  inline void append(const Point2 &samplePos, const Spectrum &value) {
324  splats.push_back(std::make_pair(samplePos, value));
325  luminance += value.getLuminance();
326  ++nSamples;
327  }
328 
329  /// Increases the contribution of an existing splat
330  inline void accum(size_t i, const Spectrum &value) {
331  splats[i].second += value;
332  luminance += value.getLuminance();
333  ++nSamples;
334  }
335 
336  /// Returns the number of contributions
337  inline size_t size() const {
338  return splats.size();
339  }
340 
341  /// Clear the splat list
342  inline void clear() {
343  luminance = 0;
344  nSamples = 0;
345  splats.clear();
346  }
347 
348  /// Return the position associated with a splat in the list
349  inline const Point2 &getPosition(size_t i) const { return splats[i].first; }
350 
351  /// Return the spectral contribution associated with a splat in the list
352  inline const Spectrum &getValue(size_t i) const { return splats[i].second; }
353 
354  /**
355  * \brief Normalize the splat list
356  *
357  * This function divides all splats so that they have unit
358  * luminance (though it leaves the \c luminance field untouched).
359  * When given an optional importance map in 2-stage MLT approaches,
360  * it divides the splat values by the associated importance
361  * map values
362  */
363  void normalize(const Bitmap *importanceMap = NULL);
364 
365  /// Return a string representation
366  std::string toString() const;
367 };
368 
370 
371 #endif /* __MITSUBA_BIDIR_PATHSAMPLER_H_ */
#define MTS_EXPORT_BIDIR
Definition: platform.h:119
List storage for the image-space contributions (&quot;splats&quot;) of a path sample generated using PathSample...
Definition: pathsampler.h:309
General-purpose bitmap class with read and write support for several common file formats.
Definition: bitmap.h:50
void append(const Point2 &samplePos, const Spectrum &value)
Appends a splat entry to the list.
Definition: pathsampler.h:323
size_t sampleIndex
Index into a rewindable random number stream.
Definition: pathsampler.h:222
int getTimeout() const
Definition: pathsampler.h:277
Float luminance
Combined luminance of all splats in this sample.
Definition: pathsampler.h:316
PathSeed(Stream *stream)
Definition: pathsampler.h:232
Implements a sampling strategy that is able to produce paths using bidirectional path tracing or unid...
Definition: pathsampler.h:39
std::pair< Point2, Spectrum > Splat
Represents a screen-space splat produced by a path sampling technique.
Definition: pathsampler.h:311
Principal scene data structure.
Definition: scene.h:49
SplatList()
Definition: pathsampler.h:320
void setTimeout(int timeout)
Definition: pathsampler.h:281
Base class of all sample generators.
Definition: sampler.h:66
Float readFloat()
Write a floating point number (configured precision) to the stream.
Definition: stream.h:424
Bidirectional path data structure.
Definition: path.h:46
int readInt()
Read a signed int (32 bit) from the stream.
Normal normalize(const Normal &n)
Definition: normal.h:73
void accum(size_t i, const Spectrum &value)
Increases the contribution of an existing splat.
Definition: pathsampler.h:330
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
int s
Number of steps from the luminaire.
Definition: pathsampler.h:224
void save(Stream *stream) const
Serialize a work unit to a binary data stream.
Definition: pathsampler.h:290
void clear()
Clear the splat list.
Definition: pathsampler.h:342
Float getLuminance() const
Return the luminance in candelas.
Abstract work unit – represents a small amount of information that encodes part of a larger processin...
Definition: sched.h:47
const Point2 & getPosition(size_t i) const
Return the position associated with a splat in the list.
Definition: pathsampler.h:349
Definition: pathsampler.h:262
int t
Number of steps from the eye.
Definition: pathsampler.h:225
void writeFloat(Float value)
Write a floating point number (configured precision) to the stream.
Definition: stream.h:271
Abstract seekable stream class.
Definition: stream.h:58
#define MTS_DECLARE_CLASS()
This macro must be used in the initial definition in classes that derive from Object.
Definition: class.h:158
Definition: fwd.h:99
Reference counting helper.
Definition: ref.h:40
void serialize(Stream *stream) const
Definition: pathsampler.h:239
int nSamples
Total number of samples in the splat list.
Definition: pathsampler.h:318
const PathSeed & getSeed() const
Definition: pathsampler.h:269
std::string toString() const
Definition: pathsampler.h:246
std::vector< Splat > splats
A series of splats associated with the current sample.
Definition: pathsampler.h:314
Float luminance
Luminance value of the path (for sanity checks)
Definition: pathsampler.h:223
Bidirectional path tracing.
Definition: pathsampler.h:53
Abstract base class, which describes integrators capable of computing samples of the scene&#39;s radiance...
Definition: integrator.h:315
Parent of all Mitsuba classes.
Definition: object.h:38
void load(Stream *stream)
Fill the work unit with content acquired from a binary data stream.
Definition: pathsampler.h:285
MemoryPool & getMemoryPool()
Return the underlying memory pool.
Definition: pathsampler.h:189
ETechnique
Specifies the sampling algorithm that is internally used.
Definition: pathsampler.h:51
PathSeed(size_t sampleIndex, Float luminance, int s=0, int t=0)
Definition: pathsampler.h:229
Discrete spectral power distribution based on a number of wavelength bins over the 360-830 nm range...
Definition: spectrum.h:663
const Spectrum & getValue(size_t i) const
Return the spectral contribution associated with a splat in the list.
Definition: pathsampler.h:352
boost::function< void(int, int, Float, Path &)> PathCallback
Callback type for use with samplePaths()
Definition: pathsampler.h:48
void set(const WorkUnit *wu)
Copy the content of another work unit of the same type.
Definition: pathsampler.h:264
std::string toString() const
Return a string representation.
Definition: pathsampler.h:295
Definition: mempool.h:29
void setSeed(const PathSeed &seed)
Definition: pathsampler.h:273
void writeSize(size_t value)
Write a size value to the stream.
Definition: stream.h:214
PathSeed()
Definition: pathsampler.h:227
#define MTS_NAMESPACE_END
Definition: platform.h:138
size_t readSize()
Read a size value from the stream.
Definition: stream.h:367
void writeInt(int value)
Write a signed int (32 bit) to the stream.
Stores information required to re-create a seed path (e.g. for MLT)
Definition: pathsampler.h:221
size_t size() const
Returns the number of contributions.
Definition: pathsampler.h:337