Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
photonmap.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_PHOTONMAP_H_)
21 #define __MITSUBA_RENDER_PHOTONMAP_H_
22 
23 #include <mitsuba/render/photon.h>
24 
26 
27 /** \brief Implementation of the photon map data structure
28  *
29  * Based on Henrik Wann Jensen's book "Realistic Image Synthesis
30  * Using Photon Mapping".
31  *
32  * \ingroup librender
33  */
35 public:
38  typedef PhotonTree::SearchResult SearchResult;
39 
40  /* ===================================================================== */
41  /* Public access methods */
42  /* ===================================================================== */
43 
44  /**
45  * \brief Create an empty photon map and reserve memory
46  * for a specified number of photons.
47  */
48  PhotonMap(size_t photonCount = 0);
49 
50  /**
51  * \brief Unserialize a photon map from a binary data stream
52  */
53  PhotonMap(Stream *stream, InstanceManager *manager);
54 
55  // =============================================================
56  //! @{ \name \c stl::vector-like interface
57  // =============================================================
58  /// Clear the kd-tree array
59  inline void clear() { m_kdtree.clear(); }
60  /// Resize the kd-tree array
61  inline void resize(size_t size) { m_kdtree.resize(size); }
62  /// Reserve a certain amount of memory for the kd-tree array
63  inline void reserve(size_t size) { m_kdtree.reserve(size); }
64  /// Return the size of the kd-tree
65  inline size_t size() const { return m_kdtree.size(); }
66  /// Return the capacity of the kd-tree
67  inline size_t capacity() const { return m_kdtree.capacity(); }
68  /// Append a kd-tree photon to the photon array
69  inline void push_back(const Photon &photon) { m_kdtree.push_back(photon); }
70  /// Return one of the photons by index
71  inline Photon &operator[](size_t idx) { return m_kdtree[idx]; }
72  /// Return one of the photons by index (const version)
73  inline const Photon &operator[](size_t idx) const { return m_kdtree[idx]; }
74  //! @}
75  // =============================================================
76 
77  // =============================================================
78  //! @{ \name \c Photon map query functions
79  // =============================================================
80 
81  /**
82  * \brief Estimate the irradiance at a given surface position
83  *
84  * Uses a Simpson filter to smooth the data.
85  *
86  * \param p
87  * The surface position for the estimate
88  * \param n
89  * Normal vector of the surface in question
90  * \param searchRadius
91  * Size of the spherical photon search region
92  * \param maxDepth
93  * Ignore photons that have undergone more than
94  * maxDepth interactions
95  * \param maxPhotons
96  * How many photon should (at most) be used in the estimate?
97  */
98  Spectrum estimateIrradiance(
99  const Point &p, const Normal &n,
100  Float searchRadius, int maxDepth,
101  size_t maxPhotons) const;
102 
103  /**
104  * \brief Estimate the radiance received from an intersected surface
105  *
106  * Uses a Simpson filter to smooth the data.
107  *
108  * \param p
109  * The surface position for the estimate
110  * \param n
111  * Normal vector of the surface in question
112  * \param searchRadius
113  * Size of the spherical photon search region
114  * \param maxPhotons
115  * How many photon should (at most) be used in the estimate?
116  */
117  Spectrum estimateRadiance(const Intersection &its,
118  Float searchRadius, size_t maxPhotons) const;
119 
120  /**
121  * \brief Compute scattered contributions from all photons within
122  * the specified radius.
123  *
124  * Does no weighting/filtering/dynamic search radius reduction
125  * and simply sums over all photons. Only considers photons with
126  * a depth value less than or equal to the \c maxDepth parameter.
127  * This function is meant to be used with progressive photon mapping.
128  */
129  size_t estimateRadianceRaw(const Intersection &its,
130  Float searchRadius, Spectrum &result, int maxDepth) const;
131 
132  /// Perform a nearest-neighbor query, see \ref PointKDTree for details
133  inline size_t nnSearch(const Point &p, Float &sqrSearchRadius,
134  size_t k, SearchResult *results) const {
135  return m_kdtree.nnSearch(p, sqrSearchRadius, k, results);
136  }
137 
138  /// Perform a nearest-neighbor query, see \ref PointKDTree for details
139  inline size_t nnSearch(const Point &p,
140  size_t k, SearchResult *results) const {
141  return m_kdtree.nnSearch(p, k, results);
142  }
143  //! @}
144  // =============================================================
145 
146 
147  /**
148  * \brief Try to append a photon to the photon map
149  *
150  * \return \c false If the photon map is full
151  */
152  inline bool tryAppend(const Photon &photon) {
153  if (size() < capacity()) {
154  push_back(photon);
155  return true;
156  } else {
157  return false;
158  }
159  }
160 
161  /// Scale all photon power values contained in this photon map
162  inline void setScaleFactor(Float value) { m_scale = value; }
163 
164  /// Return the power scale factor of this photon map
165  inline Float getScaleFactor() const { return m_scale; }
166 
167  /**
168  * \brief Build a photon map over the supplied photons.
169  *
170  * This has to be done once after all photons have been stored,
171  * but prior to executing any queries.
172  */
173  inline void build(bool recomputeAABB = false) { m_kdtree.build(recomputeAABB); }
174 
175  /// Return the depth of the constructed KD-tree
176  inline size_t getDepth() const { return m_kdtree.getDepth(); }
177 
178  /// Determine if the photon map is completely filled
179  inline bool isFull() const {
180  return capacity() == size();
181  }
182 
183  /// Serialize a photon map to a binary data stream
184  void serialize(Stream *stream, InstanceManager *manager) const;
185 
186  /// Dump the photons to an OBJ file to analyze their spatial distribution
187  void dumpOBJ(const std::string &filename);
188 
189  /// Return a string representation
190  std::string toString() const;
191 
193 protected:
194  /// Virtual destructor
195  virtual ~PhotonMap();
196 protected:
197  PhotonTree m_kdtree;
198  Float m_scale;
199 };
200 
202 
203 #endif /* __MITSUBA_RENDER_PHOTONMAP_H_ */
void build(bool recomputeAABB=false)
Build a photon map over the supplied photons.
Definition: photonmap.h:173
PhotonTree::SearchResult SearchResult
Definition: photonmap.h:38
Three-dimensional normal data structure.
Definition: normal.h:39
size_t getDepth() const
Return the depth of the constructed KD-tree.
Definition: photonmap.h:176
PointKDTree< Photon > PhotonTree
Definition: photonmap.h:36
Base class of all reference-counted objects with serialization support.
Definition: serialization.h:35
virtual void serialize(Stream *stream, InstanceManager *manager) const =0
Serialize this object to a stream.
Float getScaleFactor() const
Return the power scale factor of this photon map.
Definition: photonmap.h:165
bool isFull() const
Determine if the photon map is completely filled.
Definition: photonmap.h:179
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
size_t capacity() const
Return the capacity of the kd-tree.
Definition: photonmap.h:67
void clear()
Clear the kd-tree array.
Definition: photonmap.h:59
size_t nnSearch(const Point &p, Float &sqrSearchRadius, size_t k, SearchResult *results) const
Perform a nearest-neighbor query, see PointKDTree for details.
Definition: photonmap.h:133
void push_back(const Photon &photon)
Append a kd-tree photon to the photon array.
Definition: photonmap.h:69
size_t nnSearch(const Point &p, size_t k, SearchResult *results) const
Perform a nearest-neighbor query, see PointKDTree for details.
Definition: photonmap.h:139
size_t size() const
Return the size of the kd-tree.
Definition: photonmap.h:65
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
Implementation of the photon map data structure.
Definition: photonmap.h:34
Memory-efficient photon representation for use with PointKDTree.
Definition: photon.h:57
void setScaleFactor(Float value)
Scale all photon power values contained in this photon map.
Definition: photonmap.h:162
bool tryAppend(const Photon &photon)
Try to append a photon to the photon map.
Definition: photonmap.h:152
Container for all information related to a surface intersection.
Definition: shape.h:36
Definition: fwd.h:100
Coordinates the serialization and unserialization of object graphs.
Definition: serialization.h:65
#define MTS_EXPORT_RENDER
Definition: platform.h:109
void reserve(size_t size)
Reserve a certain amount of memory for the kd-tree array.
Definition: photonmap.h:63
Discrete spectral power distribution based on a number of wavelength bins over the 360-830 nm range...
Definition: spectrum.h:663
Photon & operator[](size_t idx)
Return one of the photons by index.
Definition: photonmap.h:71
virtual std::string toString() const
Return a human-readable string representation of the object&#39;s contents.
void resize(size_t size)
Resize the kd-tree array.
Definition: photonmap.h:61
#define MTS_NAMESPACE_END
Definition: platform.h:138
const Photon & operator[](size_t idx) const
Return one of the photons by index (const version)
Definition: photonmap.h:73
PhotonTree::IndexType IndexType
Definition: photonmap.h:37