Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
irrcache.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_RENDER_IRRCACHE_H_)
21 #define __MITSUBA_RENDER_IRRCACHE_H_
22 
23 #include <mitsuba/render/scene.h>
24 #include <mitsuba/core/octree.h>
25 
27 
28 /* 3 (X, Y, and Z) components for each spectral sample */
31 
32 /**
33  * \brief Utility data structure for hemispherical sampling and
34  * translational/rotational gradient computation.
35  *
36  * Uses the improved translational gradients proposed in
37  * the paper "Improved radiance gradient computation" by
38  * Krivanek J., Gautron P., Bouatouch K., Pattanaik S.
39  * (Proceedings of SCCG 2005)
40  *
41  * \author Wenzel Jakob
42  * \ingroup librender
43  */
45 public:
46  struct SampleEntry {
52  };
53 
54  /**
55  * Allocate storage for a hemispherical sample with M
56  * elevation and N azimuthal samples.
57  */
59 
60  /// Return the elevational resolution
61  inline uint32_t getM() const { return m_M; }
62 
63  /// Return the azimuthal resolution
64  inline uint32_t getN() const { return m_N; }
65 
66  /// Access a cell by index
67  inline SampleEntry &operator() (uint32_t j, uint32_t k) {
68  return m_entries[j*m_N + k];
69  }
70 
71  /// Access a cell by index (const version)
72  inline const SampleEntry &operator() (uint32_t j, uint32_t k) const {
73  return m_entries[j*m_N + k];
74  }
75 
76  /// Generate a set of projected solid angle-distributed directions
77  void generateDirections(const Intersection &its);
78 
79  /// Compute mean distance, gradients etc.
80  void process(const Intersection &its);
81 
82  /// Return the irradiance gradient with respect to rotation
83  inline const RotationalGradient &getRotationalGradient() const {
84  return m_rGrad;
85  }
86 
87  /// Return the irradiance gradient with respect to translation
89  return m_tGrad;
90  }
91 
92  /// Return the average distance over all cells (harmonic mean)
93  inline Float getHarmonicMeanDistance() const {
94  return m_hMean;
95  }
96 
97  /// Return the minimum distance over all cells
98  inline Float getMinimumDistance() const {
99  return m_hMin;
100  }
101 
102  /// Return the minimum distance over all cells (>10 deg in elevation)
104  return m_hMinRestricted;
105  }
106 
107  /// Return the computed irradiance
108  inline const Spectrum &getIrradiance() const {
109  return m_E;
110  }
111 
113 protected:
114  /// Free all memory
115  virtual ~HemisphereSampler();
116 private:
117  uint32_t m_M, m_N;
118  SampleEntry *m_entries;
119  Vector *m_uk, *m_vk, *m_vkMinus;
120  Spectrum m_E;
121  RotationalGradient m_rGrad;
122  TranslationalGradient m_tGrad;
123  Float m_hMean, m_hMin, m_hMinRestricted;
124  ref<Random> m_random;
125 };
126 
127 
128 /** \brief Irradiance cache data structure based on "A Ray Tracing Solution
129  * for Diffuse Interreflection" by Greg J. Ward, Francis M. Rubinstein and
130  * Robert D. Clear (Computer Graphics, Volume 22, Number 4, August 1988)
131  *
132  * with extensions from
133  * "Irradiance Gradients" by Greg J. Ward and Paul S. Heckbert
134  * (1992 Eurographics Workshop on Rendering).
135  *
136  * Includes optimizations proposed by Jaroslav Krivanek et al. in
137  * "Making Radiance and Irradiance Caching Practical: Adaptive Caching and
138  * Neighbor Clamping" (in EGSR 2006),
139  *
140  * and
141  *
142  * "An Approximate Global Illumination System for Computer Generated Films"
143  * by E. Tabellion and A. Lamorlette (SIGGRAPH 2004)
144  *
145  * \author Wenzel Jakob
146  * \ingroup librender
147  */
149 public:
150  struct Record;
151 
152  /* ===================================================================== */
153  /* Public access methods */
154  /* ===================================================================== */
155 
156  /**
157  * Create an empty irradiance of the given size
158  */
159  IrradianceCache(const AABB &aabb);
160 
161  /**
162  * Unserialize an irradiance cache from a binary data stream
163  */
164  IrradianceCache(Stream *stream, InstanceManager *manager);
165 
166  /**
167  * Set the quality parameter \kappa from the
168  * Tabellion and Lamorlette paper. Once samples
169  * have been stored, this should only be decreased.
170  */
171  inline void setQuality(Float quality) { m_kappa = quality; }
172 
173  /**
174  * Set the influence region cutoff values of samples in the
175  * cache. Will be multiplied by the scene size
176  */
177  void clampInfluence(Float min, Float max);
178 
179  /**
180  * Minimal influence region falloff with increasing distance
181  */
182  inline void clampScreen(bool active) { m_clampScreen = active; }
183 
184  /**
185  * Enable neighbor clamping?
186  */
187  inline void clampNeighbor(bool active) { m_clampNeighbor = active; }
188 
189  /**
190  * Enable/disable irradiance gradients
191  */
192  inline void useGradients(bool active) { m_useGradients = active; }
193 
194  /**
195  * Add a sample to the irradiance cache
196  *
197  * \param ray
198  * Ray differentials (if they exist)
199  * \param its
200  * The position/normal of the surface in question
201  * \param sample
202  * Record containing all hemispherical samples and
203  * derived gradient information
204  */
205  Record *put(const RayDifferential &ray, const Intersection &its,
206  const HemisphereSampler &hs);
207 
208  /**
209  * Use the irradiance cache to interpolate/extrapolate an
210  * irradiance value for the given position and surface normal
211  * Returns false on a cache miss
212  */
213  bool get(const Intersection &its, Spectrum &E) const;
214 
215  /// Manually insert an irradiance record
216  void insert(Record *rec);
217 
218  /**
219  * Serialize an irradiance cache to a binary data stream
220  */
221  void serialize(Stream *stream, InstanceManager *manager) const;
222 
223  /// Return a string representation
224  std::string toString() const;
225 
226  /* ===================================================================== */
227  /* Internal data structures */
228  /* ===================================================================== */
229 
230  struct Record {
231  /* Sample position */
233  /* Normal vector of the associated surface */
235  /* Minimum intersection distance */
237  /* Unclamped distance - must be stored for
238  neighbor clamping */
240  /* Minimum/Maximum distance - must be
241  stored for neighbor clamping. */
242  Float R0_min, R0_max;
243  /* Irradiance value */
245  /* Rotational gradient for improved interpolation */
247  /* Translational gradient for improved interpolation */
249 
250  /// Dummy constructor
251  inline Record() { }
252 
253  /// Copy constructor
254  inline Record(const Record *rec)
255  : p(rec->p), n(rec->n), R0(rec->R0), originalR0(rec->originalR0),
256  R0_min(rec->R0_min), R0_max(rec->R0_max), E(rec->E) {
257  for (int i=0; i<3; ++i) {
258  tGrad[i] = rec->tGrad[i];
259  rGrad[i] = rec->rGrad[i];
260  }
261  }
262 
263  /// Unserialize from a binary data stream
264  inline Record(Stream *stream) {
265  p = Point(stream);
266  n = Normal(stream);
267  R0 = stream->readFloat();
268  originalR0 = stream->readFloat();
269  R0_min = stream->readFloat();
270  R0_max = stream->readFloat();
271  E = Spectrum(stream);
272  for (int i=0; i<3; ++i)
273  rGrad[i] = Spectrum(stream);
274  for (int i=0; i<3; ++i)
275  tGrad[i] = Spectrum(stream);
276  }
277 
278  /// Serialize to a binary data stream
279  inline void serialize(Stream *stream) const {
280  p.serialize(stream);
281  n.serialize(stream);
282  stream->writeFloat(R0);
283  stream->writeFloat(originalR0);
284  stream->writeFloat(R0_min);
285  stream->writeFloat(R0_max);
286  E.serialize(stream);
287  for (int i=0; i<3; ++i)
288  rGrad[i].serialize(stream);
289  for (int i=0; i<3; ++i)
290  tGrad[i].serialize(stream);
291  }
292 
293  /**
294  * Calculate contribution of this sample if it were used
295  * to calculate an interpolated value at the given position
296  */
297  inline Float getWeight(const Point &p2, const Normal &n2, Float kappa) const {
298  Float dp = dot(n, n2);
299 
300  /* Quickly discard opposite-facing samples */
301  if (dp < 0.0f)
302  return 0.0f;
303  else if (dp > 1.0f)
304  dp = 1.0f;
305 
306  /* Reject illuminance values 'in front' of P2 */
307  if (dot(p2 - p, n + n2) < -0.05f)
308  return 0.0f;
309 
310  /* Ad-hoc weight function (Tabellion & Lamorlette) */
311  Float ePI = (p-p2).length() / (.5f * R0);
312  Float eNI = std::sqrt(1.0f - std::abs(dp))
313  / 0.12326f;
314  Float weight = 1 - kappa*std::max(ePI, eNI);
315 
316  if (weight < 0)
317  return 0.0f;
318 
319  return weight;
320  }
321  };
322 
324 protected:
325  /// Release all memory
326  virtual ~IrradianceCache();
327 protected:
328  /* ===================================================================== */
329  /* Protected attributes */
330  /* ===================================================================== */
331 
332  DynamicOctree<Record *> m_octree;
333  std::vector<Record *> m_records;
334  Float m_kappa;
335  Float m_sceneSize;
336  Float m_minDist, m_maxDist;
337  bool m_clampScreen, m_clampNeighbor, m_useGradients;
338  ref<Mutex> m_mutex;
339 };
340 
342 
343 #endif /* __MITSUBA_RENDER_IRRCACHE_H_ */
Float getMinimumDistance() const
Return the minimum distance over all cells.
Definition: irrcache.h:98
Generic multiple-reference octree with support for parallel dynamic updates.
Definition: octree.h:253
Spectrum TranslationalGradient[3]
Definition: irrcache.h:30
Three-dimensional normal data structure.
Definition: normal.h:39
Random number generator based on SIMD-oriented Fast Mersenne Twister
Definition: random.h:88
Vector d
Definition: irrcache.h:47
const Spectrum & getIrradiance() const
Return the computed irradiance.
Definition: irrcache.h:108
Record(const Record *rec)
Copy constructor.
Definition: irrcache.h:254
Spectrum L
Definition: irrcache.h:48
const RotationalGradient & getTranslationalGradient() const
Return the irradiance gradient with respect to translation.
Definition: irrcache.h:88
Float getWeight(const Point &p2, const Normal &n2, Float kappa) const
Definition: irrcache.h:297
Float sinTheta
Definition: irrcache.h:51
Base class of all reference-counted objects with serialization support.
Definition: serialization.h:35
TranslationalGradient tGrad
Definition: irrcache.h:248
Point p
Definition: irrcache.h:232
Float dist
Definition: irrcache.h:49
Record()
Dummy constructor.
Definition: irrcache.h:251
uint32_t getM() const
Return the elevational resolution.
Definition: irrcache.h:61
Float readFloat()
Write a floating point number (configured precision) to the stream.
Definition: stream.h:424
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
Float R0_min
Definition: irrcache.h:242
void setQuality(Float quality)
Definition: irrcache.h:171
Ray differential – enhances the basic ray class with information about the rays of adjacent pixels on...
Definition: ray.h:140
TPoint3< Float > Point
Definition: fwd.h:136
Axis-aligned bounding box data structure in three dimensions.
Definition: aabb.h:437
Spectrum E
Definition: irrcache.h:244
uint32_t getN() const
Return the azimuthal resolution.
Definition: irrcache.h:64
void serialize(Stream *stream) const
Serialize to a binary data stream.
Definition: irrcache.h:279
void clampNeighbor(bool active)
Definition: irrcache.h:187
void useGradients(bool active)
Definition: irrcache.h:192
Float getMinimumDistanceRestricted() const
Return the minimum distance over all cells (&gt;10 deg in elevation)
Definition: irrcache.h:103
Definition: irrcache.h:230
RotationalGradient rGrad
Definition: irrcache.h:246
void writeFloat(Float value)
Write a floating point number (configured precision) to the stream.
Definition: stream.h:271
Record(Stream *stream)
Unserialize from a binary data stream.
Definition: irrcache.h:264
void clampScreen(bool active)
Definition: irrcache.h:182
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: irrcache.h:46
Normal n
Definition: irrcache.h:234
Reference counting helper.
Definition: ref.h:40
Float originalR0
Definition: irrcache.h:239
Spectrum RotationalGradient[3]
Definition: irrcache.h:29
Definition: fwd.h:96
Float cosTheta
Definition: irrcache.h:50
Utility data structure for hemispherical sampling and translational/rotational gradient computation...
Definition: irrcache.h:44
const RotationalGradient & getRotationalGradient() const
Return the irradiance gradient with respect to rotation.
Definition: irrcache.h:83
Container for all information related to a surface intersection.
Definition: shape.h:36
Definition: fwd.h:100
Parent of all Mitsuba classes.
Definition: object.h:38
Coordinates the serialization and unserialization of object graphs.
Definition: serialization.h:65
#define MTS_EXPORT_RENDER
Definition: platform.h:109
Discrete spectral power distribution based on a number of wavelength bins over the 360-830 nm range...
Definition: spectrum.h:663
T dot(const TQuaternion< T > &q1, const TQuaternion< T > &q2)
Definition: quat.h:348
virtual std::string toString() const
Return a human-readable string representation of the object&#39;s contents.
#define MTS_NAMESPACE_END
Definition: platform.h:138
Thin wrapper around the recursive boost thread lock.
Definition: lock.h:34
Float getHarmonicMeanDistance() const
Return the average distance over all cells (harmonic mean)
Definition: irrcache.h:93
Irradiance cache data structure based on &quot;A Ray Tracing Solution for Diffuse Interreflection&quot; by Greg...
Definition: irrcache.h:148
Float R0
Definition: irrcache.h:236