Ray Cylinder Intersection in Optix?

I’ve been modifying the tutorial examples recently and I’ve had some trouble getting cylinder intersection to work. I rewrote the code provided for sphere intersection to make it more general and have succeeded in getting it to work for spheres. For some reason, my cylinders don’t seem to work. I have pasted my intersection code below - it is quite simple:

float3 O = ray.origin - center;
float3 D = ray.direction;

float a = D.x * D.x + D.z * D.z;
float b = 2*(O.x * D.x + O.z * D.z);
float c = (O.x * O.x + O.z * O.z) - radius*radius;

float disc = bb-4a*c;

float3 hit_p, offset;

if(disc > 0.0f) {
float root1, root2;

float sdisc = copysign(sqrtf(disc), b);
float q = (-b - sdisc) / 2.0;

root1 = q / a;

if (q != 0) {
  root2 = c / q;
}
else {
  root2 = root1;
}

if (root1 < 0) root1 = root2;
if (root2 < 0) root2 = root1;

float final_root = min(root1, root2);
float3 hit_p  = ray.origin + final_root*D;
float3 dummy_normal = hit_p; 

dummy_normal.y = 0;
dummy_normal = normalize(dummy_normal);


if( rtPotentialIntersection( final_root ) ) {
  shading_normal = geometric_normal = dummy_normal;
  rtReportIntersection(0);
}

}

I eventually fixed my issue, I wasn’t cutting off intersections based on height. I thought this would be covered by the bounding box but this wasn’t the case - this may have been due to some sort of rounding issue.

My original cylinder looked like a rectangular prism (the entire area of the bounding box was filled in), and by putting in height checks I was able to obtain a cylinder without caps.

I added the caps, and everything works fine now.