Blender V5.3
cycles/kernel/camera/projection.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009-2010 Sony Pictures Imageworks Inc., et al.
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 "kernel/types.h"
11
12#include "util/math.h"
13#include "util/types.h"
14
16
17/* Equirectangular coordinates <-> Cartesian direction */
18
20{
21 if (is_zero(dir)) {
22 return zero_float2();
23 }
24
25 const float u = (atan2f(dir.y, dir.x) - range.y) / range.x;
26 const float v = (acosf(dir.z / len(dir)) - range.w) / range.z;
27
28 return make_float2(u, v);
29}
30
32{
33 if (is_zero(dir)) {
34 return make_zero<dual2>();
35 }
36
37 const dual1 u = (atan2(dir.y(), dir.x()) - range.y) / range.x;
38 const dual1 v = (acos(dir.z() / len(dir)) - range.w) / range.z;
39
40 return make_float2(u, v);
41}
42
44 const float v,
45 const float4 range)
46{
47 const float phi = range.x * u + range.y;
48 const float theta = range.z * v + range.w;
49 return spherical_to_direction(theta, phi);
50}
51
56
61
66
68{
69 const float z = dir.z / len(make_float2(dir.x, dir.y));
70 const float theta = atan2f(dir.y, dir.x);
71 const float u = inverse_lerp(range.x, range.y, theta);
72 const float v = inverse_lerp(range.z, range.w, z);
73 return make_float2(u, v);
74}
75
77 const float v,
78 const float4 range)
79{
80 const float theta = mix(range.x, range.y, u);
81 const float z = mix(range.z, range.w, v);
82 return make_float3(cosf(theta), sinf(theta), z);
83}
84
85/* Fisheye <-> Cartesian direction */
86
88 const float u,
89 float v,
90 const float r)
91{
92 float phi = safe_acosf(safe_divide(u, r));
93 if (v < 0.0f) {
94 phi = -phi;
95 }
96
97 return make_float3(cosf(theta), -cosf(phi) * sinf(theta), sinf(phi) * sinf(theta));
98}
99
101{
102 const float r = atan2f(len(make_float2(dir.y, dir.z)), dir.x) / fov;
103 const float2 uv = r * safe_normalize(make_float2(dir.y, dir.z));
104 return make_float2(0.5f - uv.x, uv.y + 0.5f);
105}
106
108{
109 u = (u - 0.5f) * 2.0f;
110 v = (v - 0.5f) * 2.0f;
111
112 const float r = sqrtf(u * u + v * v);
113
114 if (r > 1.0f) {
115 return zero_float3();
116 }
117
118 const float theta = r * fov * 0.5f;
119
120 return fisheye_to_direction(theta, u, v, r);
121}
122
124 const float lens,
125 const float width,
126 const float height)
127{
128 const float theta = safe_acosf(dir.x);
129 const float r = 2.0f * lens * sinf(theta * 0.5f);
130
131 const float2 uv = r * safe_normalize(make_float2(dir.y, dir.z));
132 return make_float2(0.5f - uv.x / width, uv.y / height + 0.5f);
133}
134
136 float u, float v, float lens, const float fov, const float width, const float height)
137{
138 u = (u - 0.5f) * width;
139 v = (v - 0.5f) * height;
140
141 const float rmax = 2.0f * lens * sinf(fov * 0.25f);
142 const float r = sqrtf(u * u + v * v);
143
144 if (r > rmax) {
145 return zero_float3();
146 }
147
148 const float theta = 2.0f * asinf(r / (2.0f * lens));
149
150 return fisheye_to_direction(theta, u, v, r);
151}
152
154 float v,
155 float coeff0,
156 const float4 coeffs,
157 const float fov,
158 const float width,
159 const float height)
160{
161 u = (u - 0.5f) * width;
162 v = (v - 0.5f) * height;
163
164 const float r = sqrtf(u * u + v * v);
165 const float r2 = r * r;
166 const float4 rr = make_float4(r, r2, r2 * r, r2 * r2);
167 const float theta = -(coeff0 + dot(coeffs, rr));
168
169 if (fabsf(theta) > 0.5f * fov) {
170 return zero_float3();
171 }
172
173 return fisheye_to_direction(theta, u, v, r);
174}
175
176ccl_device float2 direction_to_fisheye_lens_polynomial(
177 float3 dir, const float coeff0, const float4 coeffs, const float width, const float height)
178{
179 const float theta = -safe_acosf(dir.x);
180
181 /* Initialize r with the closed-form solution for the special case
182 * coeffs.y = coeffs.z = coeffs.w = 0 */
183 float r = (theta - coeff0) / coeffs.x;
184
185 const float4 diff_coeffs = make_float4(1.0f, 2.0f, 3.0f, 4.0f) * coeffs;
186
187 for (int i = 0; i < 20; i++) {
199 const float old_r = r;
200 const float r2 = r * r;
201 const float F_r = theta - (coeff0 + dot(coeffs, make_float4(r, r2, r2 * r, r2 * r2)));
202 const float dF_r = dot(diff_coeffs, make_float4(1.0f, r, r2, r2 * r));
203 r += F_r / dF_r;
204
205 /* Early termination if the change is below the threshold */
206 if (fabsf(r - old_r) < 1e-6f) {
207 break;
208 }
210 }
211
212 const float2 uv = r * safe_normalize(make_float2(dir.y, dir.z));
213 return make_float2(0.5f - uv.x / width, uv.y / height + 0.5f);
214}
215
216/* Mirror Ball <-> Cartesian direction. */
217
218ccl_device float3 mirrorball_to_direction(const float u, const float v)
219{
220 /* point on sphere */
221 float3 dir;
222
223 dir.x = 2.0f * u - 1.0f;
224 dir.z = 2.0f * v - 1.0f;
225
226 if (dir.x * dir.x + dir.z * dir.z > 1.0f) {
227 return zero_float3();
228 }
229
230 dir.y = -sqrtf(max(1.0f - dir.x * dir.x - dir.z * dir.z, 0.0f));
231
232 /* reflection */
233 const float3 I = make_float3(0.0f, -1.0f, 0.0f);
234
235 return 2.0f * dot(dir, I) * dir - I;
236}
237
239{
240 /* inverse of mirrorball_to_direction */
241 dir.y -= 1.0f;
242
243 const float div = 2.0f * sqrtf(max(-0.5f * dir.y, 0.0f));
244 if (div > 0.0f) {
245 dir /= div;
246 }
247
248 const float u = 0.5f * (dir.x + 1.0f);
249 const float v = 0.5f * (dir.z + 1.0f);
250
251 return make_float2(u, v);
252}
253
255{
256 /* inverse of mirrorball_to_direction */
257 dir.val.y -= 1.0f;
258
259 dir = dir * 0.5f * inversesqrt(-0.5f * dir.y());
260
261 const dual1 u = 0.5f * (dir.x() + 1.0f);
262 const dual1 v = 0.5f * (dir.z() + 1.0f);
263
264 return make_float2(u, v);
265}
266
267/* Single face of a equiangular cube map projection as described in
268 * https://blog.google/products/google-ar-vr/bringing-pixels-front-and-center-vr-video/ */
270{
271 u = tanf((0.5f - u) * M_PI_2_F);
272 v = tanf((v - 0.5f) * M_PI_2_F);
273
274 return normalize(make_float3(1.0f, u, v));
275}
276
278{
279 const float u = 0.5f - atan2f(dir.y, dir.x) * 2.0f / M_PI_F;
280 const float v = atan2f(dir.z, dir.x) * 2.0f / M_PI_F + 0.5f;
281
282 return make_float2(u, v);
283}
284
286 const float u,
287 float v)
288{
289 switch (cam->panorama_type) {
291 return equirectangular_range_to_direction(u, v, cam->equirectangular_range);
295 return mirrorball_to_direction(u, v);
297 return fisheye_equidistant_to_direction(u, v, cam->fisheye_fov);
300 v,
301 cam->fisheye_lens_polynomial_bias,
302 cam->fisheye_lens_polynomial_coefficients,
303 cam->fisheye_fov,
304 cam->sensorwidth,
305 cam->sensorheight);
307 return central_cylindrical_to_direction(u, v, cam->central_cylindrical_range);
309 default:
311 u, v, cam->fisheye_lens, cam->fisheye_fov, cam->sensorwidth, cam->sensorheight);
312 }
313}
314
316{
317 switch (cam->panorama_type) {
319 return direction_to_equirectangular_range(dir, cam->equirectangular_range);
323 return direction_to_mirrorball(dir);
325 return direction_to_fisheye_equidistant(dir, cam->fisheye_fov);
327 return direction_to_fisheye_lens_polynomial(dir,
328 cam->fisheye_lens_polynomial_bias,
329 cam->fisheye_lens_polynomial_coefficients,
330 cam->sensorwidth,
331 cam->sensorheight);
333 return direction_to_central_cylindrical(dir, cam->central_cylindrical_range);
335 default:
337 dir, cam->fisheye_lens, cam->sensorwidth, cam->sensorheight);
338 }
339}
340
344{
345 float interocular_offset = cam->interocular_offset;
346
347 /* Interocular offset of zero means either non stereo, or stereo without
348 * spherical stereo. */
349 kernel_assert(interocular_offset != 0.0f);
350
351 if (cam->pole_merge_angle_to > 0.0f) {
352 const float pole_merge_angle_from = cam->pole_merge_angle_from;
353 const float pole_merge_angle_to = cam->pole_merge_angle_to;
354 const float altitude = fabsf(safe_asinf((*D).z));
355 if (altitude > pole_merge_angle_to) {
356 interocular_offset = 0.0f;
357 }
358 else if (altitude > pole_merge_angle_from) {
359 const float fac = (altitude - pole_merge_angle_from) /
360 (pole_merge_angle_to - pole_merge_angle_from);
361 const float fade = cosf(fac * M_PI_2_F);
362 interocular_offset *= fade;
363 }
364 }
365
366 const float3 up = make_float3(0.0f, 0.0f, 1.0f);
367 const float3 side = normalize(cross(*D, up));
368 const float3 stereo_offset = side * interocular_offset;
369
370 *P += stereo_offset;
371
372 /* Convergence distance is FLT_MAX in the case of parallel convergence mode,
373 * no need to modify direction in this case either. */
374 const float convergence_distance = cam->convergence_distance;
375
376 if (convergence_distance != FLT_MAX) {
377 const float3 screen_offset = convergence_distance * (*D);
378 *D = normalize(screen_offset - stereo_offset);
379 }
380}
381
#define D
btScalar max
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
ccl_device float3 equiangular_cubemap_face_to_direction(float u, float v)
CCL_NAMESPACE_BEGIN ccl_device float2 direction_to_equirectangular_range(const float3 dir, const float4 range)
ccl_device_inline float2 direction_to_panorama(ccl_constant KernelCamera *cam, const float3 dir)
ccl_device float2 direction_to_mirrorball(float3 dir)
ccl_device float2 direction_to_fisheye_equidistant(const float3 dir, const float fov)
ccl_device_inline float3 fisheye_to_direction(const float theta, const float u, float v, const float r)
ccl_device_inline float3 fisheye_lens_polynomial_to_direction(float u, float v, float coeff0, const float4 coeffs, const float fov, const float width, const float height)
ccl_device float3 fisheye_equidistant_to_direction(float u, float v, float fov)
ccl_device float2 direction_to_fisheye_equisolid(const float3 dir, const float lens, const float width, const float height)
ccl_device_inline void spherical_stereo_transform(ccl_constant KernelCamera *cam, ccl_private float3 *P, ccl_private float3 *D)
ccl_device_inline float3 fisheye_equisolid_to_direction(float u, float v, float lens, const float fov, const float width, const float height)
ccl_device float2 direction_to_central_cylindrical(const float3 dir, const float4 range)
ccl_device_inline float3 panorama_to_direction(ccl_constant KernelCamera *cam, const float u, float v)
ccl_device float3 equirectangular_range_to_direction(const float u, const float v, const float4 range)
ccl_device float3 mirrorball_to_direction(const float u, const float v)
ccl_device float2 direction_to_equiangular_cubemap_face(const float3 dir)
ccl_device float3 central_cylindrical_to_direction(const float u, const float v, const float4 range)
ccl_device float2 direction_to_equirectangular(const float3 dir)
ccl_device float3 equirectangular_to_direction(const float u, const float v)
ccl_device float3 spherical_to_direction(const float theta, const float phi)
#define kernel_assert(cond)
#define M_PI_2_F
#define ccl_constant
#define ccl_private
#define ccl_device_inline
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define asinf(x)
#define acosf(x)
#define FLT_MAX
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
constexpr T inversesqrt(T) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
#define acos
float fade(float t)
#define atan2
bool is_zero(float3x3 a)
float safe_divide(float a, float b)
VecT safe_normalize(VecT vector)
@ PANORAMA_MIRRORBALL
@ PANORAMA_FISHEYE_EQUISOLID
@ PANORAMA_CENTRAL_CYLINDRICAL
@ PANORAMA_EQUIANGULAR_CUBEMAP_FACE
@ PANORAMA_FISHEYE_EQUIDISTANT
@ PANORAMA_FISHEYE_LENS_POLYNOMIAL
@ PANORAMA_EQUIRECTANGULAR
ccl_device float safe_asinf(const float a)
Definition math_base.h:585
ccl_device float safe_acosf(const float a)
Definition math_base.h:590
ccl_device_inline float inverse_lerp(const float a, const float b, const float x)
Definition math_base.h:554
ccl_device_inline T make_zero()
Definition math_dual.h:20
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
CCL_NAMESPACE_BEGIN ccl_device_inline float2 zero_float2()
Definition math_float2.h:13
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
#define I
#define mix
#define fabsf
#define sqrtf
#define ccl_device
#define M_2PI_F
#define sinf
#define make_float2
#define tanf
#define make_float4
#define M_PI_F
#define cosf
#define atan2f
ccl_device_inline_method dual< float > x() const
Definition types_dual.h:62
ccl_device_inline_method dual< float > y() const
Definition types_dual.h:66
ccl_device_inline_method dual< float > z() const
Definition types_dual.h:70
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
dual< float3 > dual3
Definition types_dual.h:106
dual< float2 > dual2
Definition types_dual.h:105
dual< float > dual1
Definition types_dual.h:104