Blender V5.3
sample/mapping.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009-2010 Sony Pictures Imageworks Inc., et al. All Rights Reserved.
2 * SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * Adapted code from Open Shading Language. */
7
8#pragma once
9
10#include "util/math.h"
11#include "util/projection.h"
12
13#ifndef __KERNEL_GPU__
14# include <climits>
15#endif
16
18
19/* Distribute 2D uniform random samples on [0, 1] over unit disk [-1, 1], with concentric mapping
20 * to better preserve stratification for some RNG sequences. */
22{
23 float phi;
24 float r;
25 const float a = 2.0f * rand.x - 1.0f;
26 const float b = 2.0f * rand.y - 1.0f;
27
28 if (a == 0.0f && b == 0.0f) {
29 return zero_float2();
30 }
31
32 if (a * a > b * b) {
33 r = a;
34 phi = M_PI_4_F * (b / a);
35 }
36 else {
37 r = b;
38 phi = M_PI_2_F - M_PI_4_F * (a / b);
39 }
40
41 return polar_to_cartesian(r, phi);
42}
43
44/* return an orthogonal tangent and bitangent given a normal and tangent that
45 * may not be exactly orthogonal */
47 const float3 T,
50{
51 *b = normalize(cross(N, T));
52 *a = cross(*b, N);
53}
54
56 const float3 T,
59{
60 *b = safe_normalize(cross(N, T));
61 if (len_squared(*b) < 0.99f) {
62 /* Normalization failed, so fall back to basic orthonormals. */
64 }
65 else {
66 *a = cross(*b, N);
67 }
68}
69
70/* sample direction with cosine weighted distributed in hemisphere */
72 const float2 rand_in,
74 ccl_private float *pdf)
75{
76 const float2 rand = sample_uniform_disk(rand_in);
77 const float costheta = safe_sqrtf(1.0f - len_squared(rand));
78
79 float3 T;
80 float3 B;
82 *wo = rand.x * T + rand.y * B + costheta * N;
83 *pdf = costheta * M_1_PI_F;
84}
85
87{
88 const float cos_theta = dot(N, D);
89 return cos_theta > 0 ? cos_theta * M_1_PI_F : 0.0f;
90}
91
92/* sample direction uniformly distributed in hemisphere */
94 const float2 rand,
96 ccl_private float *pdf)
97{
98 float2 xy = sample_uniform_disk(rand);
99 const float z = 1.0f - len_squared(xy);
100
101 xy *= safe_sqrtf(z + 1.0f);
102
103 float3 T;
104 float3 B;
105 make_orthonormals(N, &T, &B);
106
107 *wo = xy.x * T + xy.y * B + z * N;
108 *pdf = M_1_2PI_F;
109}
110
111ccl_device_inline float pdf_uniform_cone(const float3 N, const float3 D, const float angle)
112{
113 const float z = precise_angle(N, D);
114 if (z < angle) {
115 return M_1_2PI_F / one_minus_cos(angle);
116 }
117 return 0.0f;
118}
119
120/* Uniformly sample a direction in a cone of given angle around `N`. Use concentric mapping to
121 * better preserve stratification. Return the angle between `N` and the sampled direction as
122 * `cos_theta`.
123 * Pass `1 - cos(angle)` as argument instead of `angle` to alleviate precision issues at small
124 * angles (see sphere light for reference). */
126 const float one_minus_cos_angle,
127 const float2 rand,
128 ccl_private float *cos_theta,
129 ccl_private float *pdf)
130{
131 if (one_minus_cos_angle > 0) {
132 /* Remap radius to get a uniform distribution w.r.t. solid angle on the cone.
133 * The logic to derive this mapping is as follows:
134 *
135 * Sampling a cone is comparable to sampling the hemisphere, we just restrict theta. Therefore,
136 * the same trick of first sampling the unit disk and the projecting the result up towards the
137 * hemisphere by calculating the appropriate z coordinate still works.
138 *
139 * However, by itself this results in cosine-weighted hemisphere sampling, so we need some kind
140 * of remapping. Cosine-weighted hemisphere and uniform cone sampling have the same conditional
141 * PDF for phi (both are constant), so we only need to think about theta, which corresponds
142 * directly to the radius.
143 *
144 * To find this mapping, we consider the simplest sampling strategies for cosine-weighted
145 * hemispheres and uniform cones. In both, phi is chosen as `2pi * random()`. For the former,
146 * `r_disk(rand) = sqrt(rand)`. This is just naive disk sampling, since the projection to the
147 * hemisphere doesn't change the radius.
148 * For the latter, `r_cone(rand) = sin_from_cos(mix(cos_angle, 1, rand))`.
149 *
150 * So, to remap, we just invert r_disk `(-> rand(r_disk) = r_disk^2)` and insert it into
151 * r_cone: `r_cone(r_disk) = r_cone(rand(r_disk)) = sin_from_cos(mix(cos_angle, 1, r_disk^2))`.
152 * In practice, we need to replace `rand` with `1 - rand` to preserve the stratification,
153 * but since it's uniform, that's fine. */
154 float2 xy = sample_uniform_disk(rand);
155 const float r2 = len_squared(xy);
156
157 /* Equivalent to `mix(cos_angle, 1.0f, 1.0f - r2)`. */
158 *cos_theta = 1.0f - r2 * one_minus_cos_angle;
159
160 /* Remap disk radius to cone radius, equivalent to `xy *= sin_theta / sqrt(r2)`. */
161 xy *= safe_sqrtf(one_minus_cos_angle * (2.0f - one_minus_cos_angle * r2));
162
163 *pdf = M_1_2PI_F / one_minus_cos_angle;
164
165 float3 T;
166 float3 B;
167 make_orthonormals(N, &T, &B);
168 return xy.x * T + xy.y * B + *cos_theta * N;
169 }
170
171 *cos_theta = 1.0f;
172 *pdf = 1.0f;
173
174 return N;
175}
176
177/* sample uniform point on the surface of a sphere */
179{
180 const float z = 1.0f - 2.0f * rand.x;
181 const float r = sin_from_cos(z);
182 const float phi = M_2PI_F * rand.y;
183
184 return make_float3(polar_to_cartesian(r, phi), z);
185}
186
187/* sample point in unit polygon with given number of corners and rotation */
188ccl_device float2 regular_polygon_sample(const float corners, float rotation, const float2 rand)
189{
190 float u = rand.x;
191 float v = rand.y;
192
193 /* sample corner number and reuse u */
194 const float corner = floorf(u * corners);
195 u = u * corners - corner;
196
197 /* uniform sampled triangle weights */
198 u = sqrtf(u);
199 v = v * u;
200 u = 1.0f - u;
201
202 /* point in triangle */
203 const float angle = M_PI_F / corners;
204 const float2 p = make_float2((u + v) * cosf(angle), (u - v) * sinf(angle));
205
206 /* rotate */
207 rotation += corner * 2.0f * angle;
208
209 const float cr = cosf(rotation);
210 const float sr = sinf(rotation);
211
212 return make_float2(cr * p.x - sr * p.y, sr * p.x + cr * p.y);
213}
214
215/* Generate random variable x following geometric distribution p(x) = r * (1 - r)^x, 0 <= p <= 1.
216 * Also compute the probability mass function pmf.
217 * The sampled order is truncated at `cut_off`. */
219 const float r,
220 ccl_private float &pmf,
221 const int cut_off = INT_MAX)
222{
223 const int n = min(int(floorf(logf(rand) / logf(1.0f - r))), cut_off);
224 pmf = (n == cut_off) ? powf(1.0f - r, n) : r * powf(1.0f - r, n);
225 return n;
226}
227
228/* Generate random variable x following exponential distribution p(x) = lambda * exp(-lambda * x),
229 * where lambda > 0 is the rate parameter. */
230ccl_device_inline float sample_exponential_distribution(const float rand, const float lambda)
231{
232 return -logf(1.0f - rand) / lambda;
233}
234
235/* Generate random variable x following bounded exponential distribution
236 * p(x) = lambda * exp(-lambda * x) / (exp(-lambda * t.min) - exp(-lambda * t.max)),
237 * where lambda > 0 is the rate parameter.
238 * The generated sample lies in (t.min, t.max). */
240 const float lambda,
241 const Interval<float> t)
242{
243 const float attenuation = 1.0f - expf(lambda * (t.min - t.max));
244 return clamp(t.min - logf(1.0f - rand * attenuation) / lambda, t.min, t.max);
245}
246
248 const Spectrum lambda,
249 const Interval<float> t)
250{
251 const Spectrum attenuation = exp(-lambda * t.min) - exp(-lambda * t.max);
252 return safe_divide(lambda * exp(-lambda * clamp(x, t.min, t.max)), attenuation);
253}
254
#define D
ccl_device_inline float cos_theta(const float3 w)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
ccl_device_inline float2 polar_to_cartesian(const float r, const float phi)
#define M_PI_2_F
#define M_PI_4_F
#define ccl_private
#define ccl_device_inline
#define M_1_PI_F
#define M_1_2PI_F
#define logf(x)
#define expf(x)
#define powf(x, y)
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
float3 sample_cos_hemisphere(const float2 rand)
float3 sample_uniform_cone(float2 rand, float cos_angle)
#define INT_MAX
#define exp
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
constexpr T clamp(T, U, U) RET
float sin_from_cos(float c)
float safe_divide(float a, float b)
VecT safe_normalize(VecT vector)
ccl_device_inline float safe_sqrtf(const float f)
Definition math_base.h:571
ccl_device_inline float one_minus_cos(const float angle)
Definition math_base.h:672
ccl_device_inline dual1 len_squared(const ccl_private dual< T > &a)
Definition math_dual.h:273
CCL_NAMESPACE_BEGIN ccl_device_inline float2 zero_float2()
Definition math_float2.h:13
ccl_device_inline float precise_angle(const float3 a, const float3 b)
ccl_device_inline void make_orthonormals(const float3 N, ccl_private float3 *a, ccl_private float3 *b)
#define N
#define T
#define B
#define floorf
#define sqrtf
#define ccl_device
#define M_2PI_F
#define sinf
#define make_float2
#define M_PI_F
#define cosf
ccl_device_inline float pdf_uniform_cone(const float3 N, const float3 D, const float angle)
ccl_device_inline float sample_exponential_distribution(const float rand, const float lambda)
ccl_device_inline void sample_uniform_hemisphere(const float3 N, const float2 rand, ccl_private float3 *wo, ccl_private float *pdf)
CCL_NAMESPACE_BEGIN ccl_device float2 sample_uniform_disk(const float2 rand)
ccl_device float3 sample_uniform_sphere(const float2 rand)
ccl_device void make_orthonormals_tangent(const float3 N, const float3 T, ccl_private float3 *a, ccl_private float3 *b)
ccl_device_inline int sample_geometric_distribution(const float rand, const float r, ccl_private float &pmf, const int cut_off=INT_MAX)
ccl_device_inline float pdf_cos_hemisphere(const float3 N, const float3 D)
ccl_device_inline Spectrum pdf_exponential_distribution(const float x, const Spectrum lambda, const Interval< float > t)
ccl_device void make_orthonormals_safe_tangent(const float3 N, const float3 T, ccl_private float3 *a, ccl_private float3 *b)
ccl_device float2 regular_polygon_sample(const float corners, float rotation, const float2 rand)
#define min(a, b)
Definition sort.cc:38
float x
float y
float3 Spectrum