Blender V4.5
curve_intersect.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009-2020 Intel Corporation
2 *
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Adapted from Embree with modifications. */
6
7#pragma once
8
10#include "kernel/geom/object.h"
11
13
14/* Curve primitive intersection functions.
15 *
16 * The code here was adapted from curve_intersector_sweep.h in Embree, to get
17 * an exact match between Embree CPU ray-tracing and our GPU ray-tracing. */
18
19// NOLINTBEGIN
20#define CURVE_NUM_BEZIER_SUBDIVISIONS 3
21#define CURVE_NUM_BEZIER_SUBDIVISIONS_UNSTABLE (CURVE_NUM_BEZIER_SUBDIVISIONS + 1)
22#define CURVE_NUM_BEZIER_STEPS 2
23#define CURVE_NUM_JACOBIAN_ITERATIONS 5
24// NOLINTEND
25
26#ifdef __HAIR__
27
28/* Catmull-rom curve evaluation. */
29
30ccl_device_inline float4 catmull_rom_basis_eval(const float4 curve[4], float u)
31{
32 const float t = u;
33 const float s = 1.0f - u;
34 const float n0 = -t * s * s;
35 const float n1 = 2.0f + t * t * (3.0f * t - 5.0f);
36 const float n2 = 2.0f + s * s * (3.0f * s - 5.0f);
37 const float n3 = -s * t * t;
38 return 0.5f * (curve[0] * n0 + curve[1] * n1 + curve[2] * n2 + curve[3] * n3);
39}
40
41ccl_device_inline float4 catmull_rom_basis_derivative(const float4 curve[4], float u)
42{
43 const float t = u;
44 const float s = 1.0f - u;
45 const float n0 = -s * s + 2.0f * s * t;
46 const float n1 = 2.0f * t * (3.0f * t - 5.0f) + 3.0f * t * t;
47 const float n2 = 2.0f * s * (3.0f * t + 2.0f) - 3.0f * s * s;
48 const float n3 = -2.0f * s * t + t * t;
49 return 0.5f * (curve[0] * n0 + curve[1] * n1 + curve[2] * n2 + curve[3] * n3);
50}
51
52ccl_device_inline float4 catmull_rom_basis_derivative2(const float4 curve[4], float u)
53{
54
55 const float t = u;
56 const float n0 = -3.0f * t + 2.0f;
57 const float n1 = 9.0f * t - 5.0f;
58 const float n2 = -9.0f * t + 4.0f;
59 const float n3 = 3.0f * t - 1.0f;
60 return (curve[0] * n0 + curve[1] * n1 + curve[2] * n2 + curve[3] * n3);
61}
62
63/* Thick Curve */
64
65ccl_device_inline float3 dnormalize(const float3 p, const float3 dp)
66{
67 const float pp = dot(p, p);
68 const float pdp = dot(p, dp);
69 return (pp * dp - pdp * p) / (pp * sqrtf(pp));
70}
71
72ccl_device_inline float sqr_point_to_line_distance(const float3 PmQ0, const float3 Q1mQ0)
73{
74 const float3 N = cross(PmQ0, Q1mQ0);
75 const float3 D = Q1mQ0;
76 return dot(N, N) / dot(D, D);
77}
78
79ccl_device_inline bool cylinder_intersect(const float3 cylinder_start,
80 const float3 cylinder_end,
81 const float cylinder_radius,
82 const float3 ray_D,
84 ccl_private float *u0_o,
85 ccl_private float3 *Ng0_o,
86 ccl_private float *u1_o,
87 ccl_private float3 *Ng1_o)
88{
89 /* Calculate quadratic equation to solve. */
90 const float rl = 1.0f / len(cylinder_end - cylinder_start);
91 const float3 P0 = cylinder_start;
92 const float3 dP = (cylinder_end - cylinder_start) * rl;
93 const float3 O = -P0;
94 const float3 dO = ray_D;
95
96 const float dOdO = dot(dO, dO);
97 const float OdO = dot(dO, O);
98 const float OO = dot(O, O);
99 const float dOz = dot(dP, dO);
100 const float Oz = dot(dP, O);
101
102 const float A = dOdO - sqr(dOz);
103 const float B = 2.0f * (OdO - dOz * Oz);
104 const float C = OO - sqr(Oz) - sqr(cylinder_radius);
105
106 /* We miss the cylinder if determinant is smaller than zero. */
107 const float D = B * B - 4.0f * A * C;
108 if (!(D >= 0.0f)) {
109 *t_o = make_float2(FLT_MAX, -FLT_MAX);
110 return false;
111 }
112
113 /* Special case for rays that are parallel to the cylinder. */
114 const float eps = 16.0f * FLT_EPSILON * max(fabsf(dOdO), fabsf(sqr(dOz)));
115 if (fabsf(A) < eps) {
116 if (C <= 0.0f) {
117 *t_o = make_float2(-FLT_MAX, FLT_MAX);
118 return true;
119 }
120 *t_o = make_float2(-FLT_MAX, FLT_MAX);
121 return false;
122 }
123
124 /* Standard case for rays that are not parallel to the cylinder. */
125 const float Q = sqrtf(D);
126 const float rcp_2A = 1.0f / (2.0f * A);
127 const float t0 = (-B - Q) * rcp_2A;
128 const float t1 = (-B + Q) * rcp_2A;
129
130 /* Calculates u and Ng for near hit. */
131 {
132 *u0_o = (t0 * dOz + Oz) * rl;
133 const float3 Pr = t0 * ray_D;
134 const float3 Pl = (*u0_o) * (cylinder_end - cylinder_start) + cylinder_start;
135 *Ng0_o = Pr - Pl;
136 }
137
138 /* Calculates u and Ng for far hit. */
139 {
140 *u1_o = (t1 * dOz + Oz) * rl;
141 const float3 Pr = t1 * ray_D;
142 const float3 Pl = (*u1_o) * (cylinder_end - cylinder_start) + cylinder_start;
143 *Ng1_o = Pr - Pl;
144 }
145
146 *t_o = make_float2(t0, t1);
147
148 return true;
149}
150
151ccl_device_inline float2 half_plane_intersect(const float3 P, const float3 N, const float3 ray_D)
152{
153 const float3 O = -P;
154 const float3 D = ray_D;
155 const float ON = dot(O, N);
156 const float DN = dot(D, N);
157 const float min_rcp_input = 1e-18f;
158 const bool eps = fabsf(DN) < min_rcp_input;
159 const float t = -ON / DN;
160 const float lower = (eps || DN < 0.0f) ? -FLT_MAX : t;
161 const float upper = (eps || DN > 0.0f) ? FLT_MAX : t;
162 return make_float2(lower, upper);
163}
164
165ccl_device bool curve_intersect_iterative(const float3 ray_D,
166 const float ray_tmin,
167 ccl_private float *ray_tmax,
168 const float dt,
169 const float4 curve[4],
170 float u,
171 float t,
172 const bool use_backfacing,
174{
175 const float length_ray_D = len(ray_D);
176
177 /* Error of curve evaluations is proportional to largest coordinate. */
178 const float4 box_min = min(min(curve[0], curve[1]), min(curve[2], curve[3]));
179 const float4 box_max = max(min(curve[0], curve[1]), max(curve[2], curve[3]));
180 const float4 box_abs = max(fabs(box_min), fabs(box_max));
181 const float P_err = 16.0f * FLT_EPSILON *
182 max(box_abs.x, max(box_abs.y, max(box_abs.z, box_abs.w)));
183 const float radius_max = box_max.w;
184
185 for (int i = 0; i < CURVE_NUM_JACOBIAN_ITERATIONS; i++) {
186 const float3 Q = ray_D * t;
187 const float3 dQdt = ray_D;
188 const float Q_err = 16.0f * FLT_EPSILON * length_ray_D * t;
189
190 const float4 P4 = catmull_rom_basis_eval(curve, u);
191 const float4 dPdu4 = catmull_rom_basis_derivative(curve, u);
192
193 const float3 P = make_float3(P4);
194 const float3 dPdu = make_float3(dPdu4);
195 const float radius = P4.w;
196 const float dradiusdu = dPdu4.w;
197
198 const float3 ddPdu = make_float3(catmull_rom_basis_derivative2(curve, u));
199
200 const float3 R = Q - P;
201 const float len_R = len(R);
202 const float R_err = max(Q_err, P_err);
203 const float3 dRdu = -dPdu;
204 const float3 dRdt = dQdt;
205
206 const float3 T = normalize(dPdu);
207 const float3 dTdu = dnormalize(dPdu, ddPdu);
208 const float cos_err = P_err / len(dPdu);
209
210 const float f = dot(R, T);
211 const float f_err = len_R * P_err + R_err + cos_err * (1.0f + len_R);
212 const float dfdu = dot(dRdu, T) + dot(R, dTdu);
213 const float dfdt = dot(dRdt, T);
214
215 const float K = dot(R, R) - sqr(f);
216 const float dKdu = (dot(R, dRdu) - f * dfdu);
217 const float dKdt = (dot(R, dRdt) - f * dfdt);
218 const float rsqrt_K = inversesqrtf(K);
219
220 const float g = sqrtf(K) - radius;
221 const float g_err = R_err + f_err + 16.0f * FLT_EPSILON * radius_max;
222 const float dgdu = dKdu * rsqrt_K - dradiusdu;
223 const float dgdt = dKdt * rsqrt_K;
224
225 const float invdet = 1.0f / (dfdu * dgdt - dgdu * dfdt);
226 u -= (dgdt * f - dfdt * g) * invdet;
227 t -= (-dgdu * f + dfdu * g) * invdet;
228
229 if (fabsf(f) < f_err && fabsf(g) < g_err) {
230 t += dt;
231 if (!(t >= ray_tmin && t <= *ray_tmax)) {
232 return false; /* Rejects NaNs */
233 }
234 if (!(u >= 0.0f && u <= 1.0f)) {
235 return false; /* Rejects NaNs */
236 }
237
238 /* Back-face culling. */
239 const float3 R = normalize(Q - P);
240 const float3 U = dradiusdu * R + dPdu;
241 const float3 V = cross(dPdu, R);
242 const float3 Ng = cross(V, U);
243 if (!use_backfacing && dot(ray_D, Ng) > 0.0f) {
244 return false;
245 }
246
247 /* Record intersection. */
248 *ray_tmax = t;
249 isect->t = t;
250 isect->u = u;
251 isect->v = 0.0f;
252
253 return true;
254 }
255 }
256 return false;
257}
258
259ccl_device bool curve_intersect_recursive(const float3 ray_P,
260 const float3 ray_D,
261 const float ray_tmin,
262 float ray_tmax,
263 float4 curve[4],
265{
266 /* Move ray closer to make intersection stable. */
267 const float3 center = make_float3(0.25f * (curve[0] + curve[1] + curve[2] + curve[3]));
268 const float dt = dot(center - ray_P, ray_D) / dot(ray_D, ray_D);
269 const float3 ref = ray_P + ray_D * dt;
270 const float4 ref4 = make_float4(ref, 0.0f);
271 curve[0] -= ref4;
272 curve[1] -= ref4;
273 curve[2] -= ref4;
274 curve[3] -= ref4;
275
276 const bool use_backfacing = false;
277 const float step_size = 1.0f / (float)(CURVE_NUM_BEZIER_STEPS);
278
279 int depth = 0;
280
281 /* todo: optimize stack for GPU somehow? Possibly some bitflags are enough, and
282 * u0/u1 can be derived from the depth. */
283 struct {
284 float u0, u1;
285 int i;
287
288 bool found = false;
289
290 float u0 = 0.0f;
291 float u1 = 1.0f;
292 int i = 0;
293
294 while (true) {
295 for (; i < CURVE_NUM_BEZIER_STEPS; i++) {
296 const float step = i * step_size;
297
298 /* Subdivide curve. */
299 const float dscale = (u1 - u0) * (1.0f / 3.0f) * step_size;
300 const float vu0 = mix(u0, u1, step);
301 const float vu1 = mix(u0, u1, step + step_size);
302
303 const float4 P0 = catmull_rom_basis_eval(curve, vu0);
304 const float4 dP0du = dscale * catmull_rom_basis_derivative(curve, vu0);
305 const float4 P3 = catmull_rom_basis_eval(curve, vu1);
306 const float4 dP3du = dscale * catmull_rom_basis_derivative(curve, vu1);
307
308 const float4 P1 = P0 + dP0du;
309 const float4 P2 = P3 - dP3du;
310
311 /* Calculate bounding cylinders. */
312 const float rr1 = sqr_point_to_line_distance(make_float3(dP0du), make_float3(P3 - P0));
313 const float rr2 = sqr_point_to_line_distance(make_float3(dP3du), make_float3(P3 - P0));
314 const float maxr12 = sqrtf(max(rr1, rr2));
315 const float one_plus_ulp = 1.0f + 2.0f * FLT_EPSILON;
316 const float one_minus_ulp = 1.0f - 2.0f * FLT_EPSILON;
317 float r_outer = max(max(P0.w, P1.w), max(P2.w, P3.w)) + maxr12;
318 float r_inner = min(min(P0.w, P1.w), min(P2.w, P3.w)) - maxr12;
319 r_outer = one_plus_ulp * r_outer;
320 r_inner = max(0.0f, one_minus_ulp * r_inner);
321 bool valid = true;
322
323 /* Intersect with outer cylinder. */
324 float2 tc_outer;
325 float u_outer0;
326 float u_outer1;
327 float3 Ng_outer0;
328 float3 Ng_outer1;
329 valid = cylinder_intersect(make_float3(P0),
330 make_float3(P3),
331 r_outer,
332 ray_D,
333 &tc_outer,
334 &u_outer0,
335 &Ng_outer0,
336 &u_outer1,
337 &Ng_outer1);
338 if (!valid) {
339 continue;
340 }
341
342 /* Intersect with cap-planes. */
343 float2 tp = make_float2(ray_tmin - dt, ray_tmax - dt);
344 tp = make_float2(max(tp.x, tc_outer.x), min(tp.y, tc_outer.y));
345 const float2 h0 = half_plane_intersect(make_float3(P0), make_float3(dP0du), ray_D);
346 tp = make_float2(max(tp.x, h0.x), min(tp.y, h0.y));
347 const float2 h1 = half_plane_intersect(make_float3(P3), -make_float3(dP3du), ray_D);
348 tp = make_float2(max(tp.x, h1.x), min(tp.y, h1.y));
349 valid = tp.x <= tp.y;
350 if (!valid) {
351 continue;
352 }
353
354 /* Clamp and correct u parameter. */
355 u_outer0 = clamp(u_outer0, 0.0f, 1.0f);
356 u_outer1 = clamp(u_outer1, 0.0f, 1.0f);
357 u_outer0 = mix(u0, u1, (step + u_outer0) * (1.0f / (float)(CURVE_NUM_BEZIER_STEPS + 1)));
358 u_outer1 = mix(u0, u1, (step + u_outer1) * (1.0f / (float)(CURVE_NUM_BEZIER_STEPS + 1)));
359
360 /* Intersect with inner cylinder. */
361 float2 tc_inner;
362 float u_inner0;
363 float u_inner1;
364 float3 Ng_inner0;
365 float3 Ng_inner1;
366 const bool valid_inner = cylinder_intersect(make_float3(P0),
367 make_float3(P3),
368 r_inner,
369 ray_D,
370 &tc_inner,
371 &u_inner0,
372 &Ng_inner0,
373 &u_inner1,
374 &Ng_inner1);
375
376 /* At the unstable area we subdivide deeper. */
377# if 0
378 const bool unstable0 = (!valid_inner) |
379 (fabsf(dot(normalize(ray_D), normalize(Ng_inner0))) < 0.3f);
380 const bool unstable1 = (!valid_inner) |
381 (fabsf(dot(normalize(ray_D), normalize(Ng_inner1))) < 0.3f);
382# else
383 /* On the GPU appears to be a little faster if always enabled. */
384 (void)valid_inner;
385
386 const bool unstable0 = true;
387 const bool unstable1 = true;
388# endif
389
390 /* Subtract the inner interval from the current hit interval. */
391 const float eps = 0.001f;
392 const float2 tp0 = make_float2(tp.x, min(tp.y, tc_inner.x));
393 const float2 tp1 = make_float2(max(tp.x, tc_inner.y), tp.y);
394 /* The X component should be less than the Y component for a valid intersection,
395 * but due to precision issues, the X component can sometimes be greater than
396 * Y by a small amount, leading to missing intersections. */
397 const bool valid0 = valid && ((tp0.x - tp0.y) < eps);
398 const bool valid1 = valid && ((tp1.x - tp1.y) < eps);
399 if (!(valid0 || valid1)) {
400 continue;
401 }
402
403 /* Process one or two hits. */
404 bool recurse = false;
405 if (valid0) {
406 const int termDepth = unstable0 ? CURVE_NUM_BEZIER_SUBDIVISIONS_UNSTABLE :
408 if (depth >= termDepth) {
409 found |= curve_intersect_iterative(
410 ray_D, ray_tmin, &ray_tmax, dt, curve, u_outer0, tp0.x, use_backfacing, isect);
411 }
412 else {
413 recurse = true;
414 }
415 }
416
417 const float t1 = tp1.x + dt;
418 if (valid1 && (t1 >= ray_tmin && t1 <= ray_tmax)) {
419 const int termDepth = unstable1 ? CURVE_NUM_BEZIER_SUBDIVISIONS_UNSTABLE :
421 if (depth >= termDepth) {
422 found |= curve_intersect_iterative(
423 ray_D, ray_tmin, &ray_tmax, dt, curve, u_outer1, tp1.y, use_backfacing, isect);
424 }
425 else {
426 recurse = true;
427 }
428 }
429
430 if (recurse) {
431 stack[depth].u0 = u0;
432 stack[depth].u1 = u1;
433 stack[depth].i = i + 1;
434 depth++;
435
436 u0 = vu0;
437 u1 = vu1;
438 i = -1;
439 }
440 }
441
442 if (depth > 0) {
443 depth--;
444 u0 = stack[depth].u0;
445 u1 = stack[depth].u1;
446 i = stack[depth].i;
447 }
448 else {
449 break;
450 }
451 }
452
453 return found;
454}
455
456/* Ribbons */
457
458ccl_device_inline bool cylinder_culling_test(const float2 p1, const float2 p2, const float r)
459{
460 /* Performs culling against a cylinder. */
461 const float2 dp = p2 - p1;
462 const float num = dp.x * p1.y - dp.y * p1.x;
463 const float den2 = dot(dp, dp);
464 return num * num <= r * r * den2;
465}
466
473ccl_device_inline bool ribbon_intersect_quad(const float ray_tmin,
474 const float ray_tmax,
475 const float3 quad_v0,
476 const float3 quad_v1,
477 const float3 quad_v2,
478 const float3 quad_v3,
479 ccl_private float *u_o,
480 ccl_private float *v_o,
481 ccl_private float *t_o)
482{
483 /* Calculate vertices relative to ray origin? */
484 const float3 O = make_float3(0.0f, 0.0f, 0.0f);
485 const float3 D = make_float3(0.0f, 0.0f, 1.0f);
486 const float3 va = quad_v0 - O;
487 const float3 vb = quad_v1 - O;
488 const float3 vc = quad_v2 - O;
489 const float3 vd = quad_v3 - O;
490
491 const float3 edb = vb - vd;
492 const float WW = dot(cross(vd, edb), D);
493 const float3 v0 = (WW <= 0.0f) ? va : vc;
494 const float3 v1 = (WW <= 0.0f) ? vb : vd;
495 const float3 v2 = (WW <= 0.0f) ? vd : vb;
496
497 /* Calculate edges? */
498 const float3 e0 = v2 - v0;
499 const float3 e1 = v0 - v1;
500
501 /* perform edge tests */
502 const float U = dot(cross(v0, e0), D);
503 const float V = dot(cross(v1, e1), D);
504 if (!(max(U, V) <= 0.0f)) {
505 return false;
506 }
507
508 /* Calculate geometry normal and denominator? */
509 const float3 Ng = cross(e1, e0);
510 const float den = dot(Ng, D);
511 const float rcpDen = 1.0f / den;
512
513 /* Perform depth test? */
514 const float t = rcpDen * dot(v0, Ng);
515 if (!(t >= ray_tmin && t <= ray_tmax)) {
516 return false;
517 }
518
519 /* Avoid division by 0? */
520 if (!(den != 0.0f)) {
521 return false;
522 }
523
524 /* Update hit information? */
525 *t_o = t;
526 *u_o = U * rcpDen;
527 *v_o = V * rcpDen;
528 *u_o = (WW <= 0.0f) ? *u_o : 1.0f - *u_o;
529 *v_o = (WW <= 0.0f) ? *v_o : 1.0f - *v_o;
530 return true;
531}
532
533ccl_device_inline void ribbon_ray_space(const float3 ray_D,
534 const float ray_D_invlen,
535 float3 ray_space[3])
536{
537 const float3 D = ray_D * ray_D_invlen;
538 const float3 dx0 = make_float3(0, D.z, -D.y);
539 const float3 dx1 = make_float3(-D.z, 0, D.x);
540 ray_space[0] = normalize(dot(dx0, dx0) > dot(dx1, dx1) ? dx0 : dx1);
541 ray_space[1] = normalize(cross(D, ray_space[0]));
542 ray_space[2] = D * ray_D_invlen;
543}
544
545ccl_device_inline float4 ribbon_to_ray_space(const float3 ray_space[3],
546 const float3 ray_org,
547 const float4 P4)
548{
549 const float3 P = make_float3(P4) - ray_org;
550 return make_float4(dot(ray_space[0], P), dot(ray_space[1], P), dot(ray_space[2], P), P4.w);
551}
552
553ccl_device_inline bool ribbon_intersect(const float3 ray_org,
554 const float3 ray_D,
555 const float ray_tmin,
556 float ray_tmax,
557 const int N,
558 float4 curve[4],
560{
561 /* Transform control points into ray space. */
562 const float ray_D_invlen = 1.0f / len(ray_D);
563 float3 ray_space[3];
564 ribbon_ray_space(ray_D, ray_D_invlen, ray_space);
565
566 curve[0] = ribbon_to_ray_space(ray_space, ray_org, curve[0]);
567 curve[1] = ribbon_to_ray_space(ray_space, ray_org, curve[1]);
568 curve[2] = ribbon_to_ray_space(ray_space, ray_org, curve[2]);
569 curve[3] = ribbon_to_ray_space(ray_space, ray_org, curve[3]);
570
571 const float4 mx = max(max(fabs(curve[0]), fabs(curve[1])), max(fabs(curve[2]), fabs(curve[3])));
572 const float eps = 4.0f * FLT_EPSILON * max(max(mx.x, mx.y), max(mx.z, mx.w));
573 const float step_size = 1.0f / (float)N;
574
575 /* Evaluate first point and radius scaled normal direction. */
576 float4 p0 = catmull_rom_basis_eval(curve, 0.0f);
577 float3 dp0dt = make_float3(catmull_rom_basis_derivative(curve, 0.0f));
578 if (reduce_max(fabs(dp0dt)) < eps) {
579 const float4 p1 = catmull_rom_basis_eval(curve, step_size);
580 dp0dt = make_float3(p1 - p0);
581 }
582 float3 wn0 = normalize(make_float3(dp0dt.y, -dp0dt.x, 0.0f)) * p0.w;
583
584 /* Evaluate the bezier curve. */
585 for (int i = 0; i < N; i++) {
586 const float u = i * step_size;
587 const float4 p1 = catmull_rom_basis_eval(curve, u + step_size);
588 const bool valid = cylinder_culling_test(
589 make_float2(p0.x, p0.y), make_float2(p1.x, p1.y), max(p0.w, p1.w));
590
591 /* Evaluate next point. */
592 float3 dp1dt = make_float3(catmull_rom_basis_derivative(curve, u + step_size));
593 dp1dt = (reduce_max(fabs(dp1dt)) < eps) ? make_float3(p1 - p0) : dp1dt;
594 const float3 wn1 = normalize(make_float3(dp1dt.y, -dp1dt.x, 0.0f)) * p1.w;
595
596 if (valid) {
597 /* Construct quad coordinates. */
598 const float3 lp0 = make_float3(p0) + wn0;
599 const float3 lp1 = make_float3(p1) + wn1;
600 const float3 up0 = make_float3(p0) - wn0;
601 const float3 up1 = make_float3(p1) - wn1;
602
603 /* Intersect quad. */
604 float vu;
605 float vv;
606 float vt;
607 bool valid0 = ribbon_intersect_quad(ray_tmin, ray_tmax, lp0, lp1, up1, up0, &vu, &vv, &vt);
608
609 if (valid0) {
610 /* ignore self intersections */
611 const float avoidance_factor = 2.0f;
612 if (avoidance_factor != 0.0f) {
613 const float r = mix(p0.w, p1.w, vu);
614 valid0 = vt > avoidance_factor * r * ray_D_invlen;
615 }
616
617 if (valid0) {
618 vv = 2.0f * vv - 1.0f;
619
620 /* Record intersection. */
621 ray_tmax = vt;
622 isect->t = vt;
623 isect->u = u + vu * step_size;
624 isect->v = vv;
625 return true;
626 }
627 }
628 }
629
630 /* Store point for next step. */
631 p0 = p1;
632 wn0 = wn1;
633 }
634 return false;
635}
636
637ccl_device_forceinline bool curve_intersect(KernelGlobals kg,
639 const float3 ray_P,
640 const float3 ray_D,
641 const float tmin,
642 const float tmax,
643 const int object,
644 const int prim,
645 const float time,
646 const int type)
647{
648 const bool is_motion = (type & PRIMITIVE_MOTION);
649
650 const KernelCurve kcurve = kernel_data_fetch(curves, prim);
651
652 const int k0 = kcurve.first_key + PRIMITIVE_UNPACK_SEGMENT(type);
653 const int k1 = k0 + 1;
654 const int ka = max(k0 - 1, kcurve.first_key);
655 const int kb = min(k1 + 1, kcurve.first_key + kcurve.num_keys - 1);
656
657 float4 curve[4];
658 if (!is_motion) {
659 curve[0] = kernel_data_fetch(curve_keys, ka);
660 curve[1] = kernel_data_fetch(curve_keys, k0);
661 curve[2] = kernel_data_fetch(curve_keys, k1);
662 curve[3] = kernel_data_fetch(curve_keys, kb);
663 }
664 else {
665 motion_curve_keys(kg, object, time, ka, k0, k1, kb, curve);
666 }
667
668 if (type & PRIMITIVE_CURVE_RIBBON) {
669 /* todo: adaptive number of subdivisions could help performance here. */
670 const int subdivisions = kernel_data.bvh.curve_subdivisions;
671 if (ribbon_intersect(ray_P, ray_D, tmin, tmax, subdivisions, curve, isect)) {
672 isect->prim = prim;
673 isect->object = object;
674 isect->type = type;
675 return true;
676 }
677
678 return false;
679 }
680
681 if (curve_intersect_recursive(ray_P, ray_D, tmin, tmax, curve, isect)) {
682 isect->prim = prim;
683 isect->object = object;
684 isect->type = type;
685 return true;
686 }
687
688 return false;
689}
690
691ccl_device_inline void curve_shader_setup(KernelGlobals kg,
692 ccl_private ShaderData *sd,
693 float3 P,
694 float3 D,
695 float t,
696 const int isect_prim)
697{
698 if (!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
699 const Transform tfm = object_get_inverse_transform(kg, sd);
700
701 P = transform_point(&tfm, P);
702 D = transform_direction(&tfm, D * t);
703 D = safe_normalize_len(D, &t);
704 }
705
706 const KernelCurve kcurve = kernel_data_fetch(curves, isect_prim);
707
708 const int k0 = kcurve.first_key + PRIMITIVE_UNPACK_SEGMENT(sd->type);
709 const int k1 = k0 + 1;
710 const int ka = max(k0 - 1, kcurve.first_key);
711 const int kb = min(k1 + 1, kcurve.first_key + kcurve.num_keys - 1);
712
713 float4 P_curve[4];
714
715 if (!(sd->type & PRIMITIVE_MOTION)) {
716 P_curve[0] = kernel_data_fetch(curve_keys, ka);
717 P_curve[1] = kernel_data_fetch(curve_keys, k0);
718 P_curve[2] = kernel_data_fetch(curve_keys, k1);
719 P_curve[3] = kernel_data_fetch(curve_keys, kb);
720 }
721 else {
722 motion_curve_keys(kg, sd->object, sd->time, ka, k0, k1, kb, P_curve);
723 }
724
725 P = P + D * t;
726
727 const float4 dPdu4 = catmull_rom_basis_derivative(P_curve, sd->u);
728 const float3 dPdu = make_float3(dPdu4);
729
730 if (sd->type & PRIMITIVE_CURVE_RIBBON) {
731 /* Rounded smooth normals for ribbons, to approximate thick curve shape. */
732 const float3 tangent = normalize(dPdu);
733 const float3 bitangent = normalize(cross(tangent, -D));
734 const float sine = sd->v;
735 const float cosine = cos_from_sin(sine);
736
737 sd->N = normalize(sine * bitangent - cosine * normalize(cross(tangent, bitangent)));
738# if 0
739 /* This approximates the position and geometric normal of a thick curve too,
740 * but gives too many issues with wrong self intersections. */
741 const float dPdu_radius = dPdu4.w;
742 sd->Ng = sd->N;
743 P += sd->N * dPdu_radius;
744# endif
745 }
746 else {
747 /* Thick curves, compute normal using direction from inside the curve.
748 * This could be optimized by recording the normal in the intersection,
749 * however for Optix this would go beyond the size of the payload. */
750 /* NOTE: It is possible that P will be the same as P_inside (precision issues, or very small
751 * radius). In this case use the view direction to approximate the normal. */
752 const float3 P_inside = make_float3(catmull_rom_basis_eval(P_curve, sd->u));
753 const float3 N = (!isequal(P, P_inside)) ? normalize(P - P_inside) : -sd->wi;
754
755 sd->N = N;
756 sd->v = 0.0f;
757 }
758
759# ifdef __DPDU__
760 /* dPdu/dPdv */
761 sd->dPdu = dPdu;
762# endif
763
764 /* Convert to world space. */
765 if (!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
767 object_normal_transform_auto(kg, sd, &sd->N);
768 object_dir_transform_auto(kg, sd, &sd->dPdu);
769 }
770
771 sd->P = P;
772 sd->Ng = (sd->type & PRIMITIVE_CURVE_RIBBON) ? sd->wi : sd->N;
773 sd->dPdv = cross(sd->dPdu, sd->Ng);
774 sd->shader = kernel_data_fetch(curves, sd->prim).shader_id;
775}
776
777#endif
778
#define D
ATTR_WARN_UNUSED_RESULT const size_t num
#define K(key)
#define C
Definition RandGen.cpp:29
#define U
ATTR_WARN_UNUSED_RESULT const BMVert * v2
#define A
reduce_max(value.rgb)") DEFINE_VALUE("REDUCE(lhs
dot(value.rgb, luminance_coefficients)") DEFINE_VALUE("REDUCE(lhs
#define CURVE_NUM_BEZIER_SUBDIVISIONS_UNSTABLE
#define CURVE_NUM_BEZIER_STEPS
#define CURVE_NUM_JACOBIAN_ITERATIONS
#define CURVE_NUM_BEZIER_SUBDIVISIONS
#define kernel_data
#define ccl_device_forceinline
#define kernel_data_fetch(name, index)
#define PRIMITIVE_UNPACK_SEGMENT(type)
#define ccl_device
#define ccl_private
const ThreadKernelGlobalsCPU * KernelGlobals
#define ccl_device_inline
#define CCL_NAMESPACE_END
ccl_device_forceinline float4 make_float4(const float x, const float y, const float z, const float w)
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device_forceinline float2 make_float2(const float x, const float y)
#define fabsf(x)
#define sqrtf(x)
VecBase< float, 4 > float4
VecBase< float, D > normalize(VecOp< float, D >) RET
VecBase< float, D > step(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
#define mix(a, b, c)
Definition hash.h:35
ccl_device_inline Transform object_get_inverse_transform(KernelGlobals kg, const ccl_private ShaderData *sd)
#define object_normal_transform_auto
#define object_position_transform_auto
#define object_dir_transform_auto
@ PRIMITIVE_MOTION
@ PRIMITIVE_CURVE_RIBBON
@ SD_OBJECT_TRANSFORM_APPLIED
ccl_device_inline float sqr(const float a)
Definition math_base.h:600
ccl_device_inline float inversesqrtf(const float f)
Definition math_base.h:529
ccl_device_inline float cos_from_sin(const float s)
Definition math_base.h:610
ccl_device_inline float2 fabs(const float2 a)
ccl_device_inline bool isequal(const float2 a, const float2 b)
ccl_device_inline float3 safe_normalize_len(const float3 a, ccl_private float *t)
#define N
#define T
#define B
#define R
const btScalar eps
Definition poly34.cpp:11
#define min(a, b)
Definition sort.cc:36
#define FLT_MAX
Definition stdcycles.h:14
float x
float y
float y
Definition sky_float3.h:27
float x
Definition sky_float3.h:27
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251
ccl_device_inline float3 transform_direction(const ccl_private Transform *t, const float3 a)
Definition transform.h:87
ccl_device_inline float3 transform_point(const ccl_private Transform *t, const float3 a)
Definition transform.h:56
uint len
CCL_NAMESPACE_BEGIN struct Window V