0

I'm currently trying to figure out a rendering approach for implicit geometry which involves a ray intersection algorithm for oriented rounded box primitives. It's just a small toy project and I'm not the best at deriving the below formulas myself mathematically, which is why I'm asking this question.

The intersection of a ray originating from ro in the direction rd with a box of half-dimensions bd is easy to compute:

void box_intersection (vec3 ro, vec3 rd, vec3 bd) 
{
    vec3 m = 1.0 / rd;

    vec3 plane_hit = -1.0 * m * ro;
    vec3 plane_offset = abs(m) * bd;

    vec3 planes_near = plane_hit - plane_offset;
    vec3 planes_far = plane_hit + plane_offset;

    float near_distance = max(max( planes_near.x, planes_near.y ), planes_near.z );
    float far_distance = min(min( planes_far.x, planes_far.y ), planes_far.z );

    if (near_distance <= far_distance)
    {
        // handle intersection
    }
}

sources:

But once you want the intersection with a uniformly rounded box, the necessary algorithm explodes in complexity. I don't know how to hide snippets of code behind a collapsible section or something like that, but the code for intersecting a rounded box can also be found on iq's site (iquilezles.org) linked above, right below the snippet for standard box intersections.

As far as I know, this intersection test considers the radius to be interior i.e. contained within the bounding box defined by the box dimensions. This is unlike another approach to rendering a rounded box using signed distance fields (SDFs), where the radius is "added" to the box, visually expanding it by that radius in all directions. However, I'm not using a ray-marching approach which is needed to find the intersection to such an SDF along a ray.

I understand why the algorithm for gaining the closest distance to a rounded box is so much simpler compared to gaining accurate intersections without ray marching. The former "only" needs to consider a point as input while the latter considers an entire line. However I did not expect the difference in computational effort to be this drastic.

Given that the article is fairly old in computer graphics terms (the shadertoy examples are from 2019) and I don't know if Inigo Quilez updates that article anymore - I was wondering if there are any more efficient approaches out there by now? I'm not having any luck using search engines, as it usually refers me to bounding box intersection approaches...

I "only" need the closest point along a ray and I'm fine with the radius being "added outside" of the box dimensions as seen in the SDF version - if that makes any calculations simpler.

Does anyone know of a more efficient approach to getting the closest intersection with a rounded box? Thanks in advance!

3
  • Is it that complex? If the ray-BB intersection is more than radius from the edge, you're done. If not, test against radius sized cubes at the relevant corners, depending on those intersection points, you should do either ray-sphere, ray-cylinder tests or maybe both. Commented Oct 10, 2024 at 14:36
  • @beyond I don‘t understand where the sudden jump in complexity is coming from either, given that box and sphere intersections are so trivial. But yes, it is that computationally complex. The algorithms for both sphere and box intersections have more or less 10 operations in them, maybe 6 lines of code (which is obviously no robust metric in any way, but a good hint at computational effort) - the rounded box intersection algorithm linked in the post has 50 lines with sometimes up to 5 or 6 arithemtic operations per line… there has to be a simpler approach to this… Commented Oct 10, 2024 at 18:08
  • I would expect all non-trivial implicit objects to have rather complex intersection tests :) Commented Oct 11, 2024 at 6:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.