Library of Assembled Shared Sources
distribution_transformations.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45/** @defgroup DistributionTransformations
46 *
47 * A set of functions to transform samples between distribution
48 */
49
50#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_DISTRIBUTION_TRANSFORMATIONS_H
51#define LASS_GUARDIAN_OF_INCLUSION_NUM_DISTRIBUTION_TRANSFORMATIONS_H
52
53#include "num_common.h"
54#include "../prim/point_2d.h"
55#include "../prim/point_3d.h"
56
57namespace lass
58{
59namespace num
60{
61
62/** @ingroup DistributionTransformations
63 */
64template <typename T>
65const prim::Point3D<T> uniformSphere(const prim::Point2D<T>& sample, T& pdf)
66{
67 const T z = 1 - 2 * sample.x;
68 const T rho = sqrt(std::max<T>(0, 1 - z * z));
69 const T theta = 2 * num::NumTraits<T>::pi * sample.y;
70 pdf = inv(4 * num::NumTraits<T>::pi);
71 return prim::Point3D<T>(rho * cos(theta), rho * sin(theta), z);
72}
73
74/** @ingroup DistributionTransformations
75 */
76template <typename T>
77const prim::Point3D<T> uniformHemisphere(const prim::Point2D<T>& sample, T& pdf)
78{
79 const T z = sample.x;
80 const T rho = sqrt(std::max<T>(0, 1 - z * z));
81 const T theta = 2 * num::NumTraits<T>::pi * sample.y;
82 pdf = inv(2 * num::NumTraits<T>::pi);
83 return prim::Point3D<T>(rho * cos(theta), rho * sin(theta), z);
84}
85
86/** @ingroup DistributionTransformations
87 */
88template <typename T>
89const prim::Point3D<T> uniformCone(const prim::Point2D<T>& sample, T minCosTheta, T& pdf)
90{
91 const T z = minCosTheta + sample.x * (1 - minCosTheta);
92 const T rho = sqrt(std::max<T>(0, 1 - z * z));
93 const T theta = 2 * NumTraits<T>::pi * sample.y;
94 pdf = inv(2 * NumTraits<T>::pi * (1 - minCosTheta));
95 return prim::Point3D<T>(rho * cos(theta), rho * sin(theta), z);
96}
97
98/** @ingroup DistributionTransformations
99 */
100template <typename T>
101const prim::Point2D<T> uniformDisk(const prim::Point2D<T>& sample, T& pdf)
102{
103 T rho, theta;
104 const T x = 2 * sample.x - 1;
105 const T y = 2 * sample.y - 1;
106 const T pi_4 = num::NumTraits<T>::pi / 4;
107
108 if (x > -y)
109 {
110 if (x > y)
111 {
112 rho = x;
113 theta = pi_4 * (y / x);
114 }
115 else
116 {
117 rho = y;
118 theta = pi_4 * (2 - (x / y));
119 }
120 }
121 else
122 {
123 if (x < y)
124 {
125 rho = -x;
126 theta = pi_4 * (4 + (y / x));
127 }
128 else
129 {
130 rho = -y;
131 if (y != 0)
132 {
133 theta = pi_4 * (6 - (x / y));
134 }
135 else
136 {
137 theta = 0;
138 }
139 }
140 }
141
142 pdf = inv(NumTraits<T>::pi);
143 return prim::Point2D<T>(rho * cos(theta), rho * sin(theta));
144}
145
146/** @ingroup DistributionTransformations
147 */
148template <typename T>
149const prim::Point3D<T> cosineHemisphere(const prim::Point2D<T>& sample, T& pdf)
150{
151 const prim::Point2D<T> xy = uniformDisk(sample, pdf);
152 const T z = num::sqrt(std::max(T(), 1 - xy.position().squaredNorm()));
153 pdf *= z;
154 return prim::Point3D<T>(xy.x, xy.y, z);
155}
156
157
158/** @ingroup DistributionTransformations
159 */
160template <typename T>
161T uniformToExponential(T uniform, T sigma, T& pdf)
162{
163 pdf = (1 - uniform) * sigma;
164 return sigma > 0 ? (-num::log1p(std::max<T>(-1, -uniform)) / sigma) : NumTraits<T>::infinity;
165}
166
167
168}
169
170}
171
172#endif
173
174// EOF
T inv(const T &x)
return x ^ -1
Definition basic_ops.h:178
T uniform(RG &generator)
numeric types and traits.
Definition basic_ops.h:70
Library for Assembled Shared Sources.
Definition config.h:53