Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
geodist2.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_GEODIST2_H_)
21 #define __MITSUBA_BIDIR_GEODIST2_H_
22 
23 #include <mitsuba/bidir/path.h>
24 
26 
27 /**
28  * \brief Clamped two-tailed geometric distribution
29  *
30  * This class implements a specialized clamped two-tailed geometric
31  * distribution with support for sample generation and evaluation
32  * of the probability mass and cumulative distribution functions.
33  *
34  * The precise form of the distribution is
35  *
36  * \f[
37  * P(i) = \begin{cases}
38  * c b^{-|i-o|},&\text{if } i\ge l\text{ and }i\le r\\
39  * 0,&\mathrm{otherwise}
40  * \end{cases}
41  * \f]
42  *
43  * where \f$b\in\mathbb{R}\f$ is the base parameter of the distribution,
44  * \f$[l,r]\subseteq\mathbb{Z}\f$ denotes the domain of the probability
45  * mass function, \f$o\in\mathbb{Z}\f$ is offset, and \f$c\f$ is a suitably
46  * chosen normalization constant.
47  *
48  * This function is used to propose bidirectional mutations; see the
49  * MLT writeup for details.
50  *
51  * \author Wenzel Jakob
52  * \ingroup libbidir
53  */
55 public:
56  /// Create a new two-tailed distribution for the given base constant
57  TwoTailedGeoDistr(Float base) : m_base(base) {
58  m_baseNormalization = 1.0f / (Float) (base+1);
59  m_invLogBase = 1.0f / std::log(base);
60  }
61 
62  /// Configure the domain and center of the distribution
63  void configure(int center, int start, int end) {
64  m_center = center;
65  m_start = start - center;
66  m_end = end - center;
67  m_offset = R(m_start - 1);
68  m_normalization = R(m_end) - m_offset;
69  }
70 
71  /// Evaluate the probability mass function at position \c i
72  inline Float pmf(int i) const {
73  i -= m_center;
74 
75  if (i < m_start || i > m_end)
76  return 0.0f;
77 
78  return r(i) / m_normalization;
79  }
80 
81  /// Evaluate the cumulative distribution function at position \c i
82  inline Float cdf(int i) const {
83  i -= m_center;
84 
85  if (i < m_start)
86  return 0.0f;
87  else if (i > m_end)
88  i = m_end;
89 
90  return (R(i) - m_offset) / m_normalization;
91  }
92 
93  /// Draw a position according to the probability mass function
94  inline int sample(Float xi) const {
95  return std::max(m_start,
96  Rinv(xi * m_normalization + m_offset)) + m_center;
97  }
98 
99 protected:
100  inline Float r(int i) const {
101  return (m_base-1) * m_baseNormalization
102  * std::pow(m_base, - (Float) std::abs(i));
103  }
104 
105  inline Float R(int i) const {
106  if (i <= 0)
107  return std::pow(m_base, (Float) (i+1)) * m_baseNormalization;
108  else
109  return 1-std::pow(m_base, - (Float) i) * m_baseNormalization;
110  }
111 
112  inline int Rinv(Float x) const {
113  Float result;
114  if (x < m_base * m_baseNormalization)
115  result = std::log((1+m_base) * x) * m_invLogBase - 1;
116  else
117  result = -std::log((1+m_base) * (1-x)) * m_invLogBase;
118  return (int) std::ceil(result);
119  }
120 private:
121  Float m_base, m_invLogBase, m_baseNormalization;
122  Float m_normalization, m_offset;
123  int m_center, m_start, m_end;
124 };
125 
127 
128 #endif /* __MITSUBA_BIDIR_GEODIST2_H_ */
Float r(int i) const
Definition: geodist2.h:100
int Rinv(Float x) const
Definition: geodist2.h:112
Float pmf(int i) const
Evaluate the probability mass function at position i.
Definition: geodist2.h:72
int sample(Float xi) const
Draw a position according to the probability mass function.
Definition: geodist2.h:94
Clamped two-tailed geometric distribution.
Definition: geodist2.h:54
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
void configure(int center, int start, int end)
Configure the domain and center of the distribution.
Definition: geodist2.h:63
Float R(int i) const
Definition: geodist2.h:105
TwoTailedGeoDistr(Float base)
Create a new two-tailed distribution for the given base constant.
Definition: geodist2.h:57
#define MTS_NAMESPACE_END
Definition: platform.h:138
Float cdf(int i) const
Evaluate the cumulative distribution function at position i.
Definition: geodist2.h:82