intersection_sphere in sphere.cu

Hello,

I’m trying to figure out what the intersection test in sphere.cu does in every step.
I found a different algorithm which also computes the intersection points of a line with a sphere, but works a bit different.

These first steps of the algorithm in sphere.cu are clear to me.

float3 L = ray.origin - center;
float3 D = ray.direction;
float b = dot(L, D);

But I’m not sure what the next steps do:

float c = dot(L, L)-radius*radius;
float disc = b*b-c;
float sdisc = sqrtf(disc);
float root1 = (-b - sdisc);
float root2 = (-b + sdisc);

After that, the intersection points are computed like that:

(O + (root1 + root11)*D)
(O + root2*D)

Can someone try to explain the algorithm with the help of the following image?

Thank you.

It’s doing the analytic solution explained on the same site you found that image on:
https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection

Ok, thank you. Seems like I read over this.

Ok, the part I still don’t get is why OptiX is neglecting some factors.

float b = dot(O, D);// instead of 2*dot(O,D)
float disc = b*b-c;// instead of b * b - 4 * a * c with a=dot(D, D)
float root1 = (-b - sdisc);// instead of (-b - sdisc)/2a

It’s not neglecting them, it optimized them away because they are either multiplications with one or cancel out when filling them into the formula to solve a quadratic equation.

Read that site again, especially the big grey box which explains why a == 1.

Simply put a == 1 and as b in this original formula use your 2 * b. (Read “±” as “+” or “-” for the two solutions.)
x(1,2) = (-b ± sqrt(b * b - 4 * a * c)) / (2 * a)
=>
x(1,2) = (- 2 * b ± sqrt(2 * b * 2 * b - 4 * 1 * c)) / (2 * 1)
and optimize, which will give you this
x(1,2) = -b ± sqrt(b * b - c)
which is exactly what’s done in the code.

Ok thanks a lot, Detlef.