craycastingraytracing

Torus intersection formula for raycasting?


I'm currently learning raycasting and I was able to render several objects in terminal using functions like this (vec3 is just a struct with xyz variables) :

float hit_sphere(vec3 center, float radius, vec3 ray) {
    float a = pow(magnitude(ray), 2.0);
    float h = dot(ray, center);
    float c = pow(magnitude(center), 2.0) - radius*radius;

    float discriminant = h*h - a*c;
    if (discriminant < 0.0f) return -1.0f;
    else {
        return (h - sqrt(discriminant)) / a;
    }
}

I tried to find such a function for a torus, but all I found were some books with fancy equations that I don't really understand.

So it would be great if you could explain to me how to make such a function for a torus.

As far as I know, raycasting is just raytracing without reflections and stuff, so I think simplified raytracing formula would work here too.


Solution

  • Finally I found one, and it works for me:

    // The MIT License
    // Copyright © 2014 Inigo Quilez
    // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    double iTorus( vec3_t ro, vec3_t rd, torus_t tor )
    {
        double po = 1.0;
        
        double Ra2 = tor.R*tor.R;
        double ra2 = tor.r*tor.r;
        
        double m = dot(ro,ro);
        double n = dot(ro,rd);
    
        // bounding sphere
        {
        double h = n*n - m + (tor.R+tor.r)*(tor.R+tor.r);
        if( h<0.0 ) return -1.0;
        //double t = -n-sqrt(h); // could use this to compute intersections from ro+t*rd
        }
        
        // find quartic equation
        double k = (m - ra2 - Ra2)/2.0;
        double k3 = n;
        double k2 = n*n + Ra2*rd.z*rd.z + k;
        double k1 = k*n + Ra2*ro.z*rd.z;
        double k0 = k*k + Ra2*ro.z*ro.z - Ra2*ra2;
        
        #if 1
        // prevent |c1| from being too close to zero
        if( fabs(k3*(k3*k3 - k2) + k1) < 0.01 )
        {
            po = -1.0;
            double tmp=k1; k1=k3; k3=tmp;
            k0 = 1.0/k0;
            k1 = k1*k0;
            k2 = k2*k0;
            k3 = k3*k0;
        }
        #endif
    
        double c2 = 2.0*k2 - 3.0*k3*k3;
        double c1 = k3*(k3*k3 - k2) + k1;
        double c0 = k3*(k3*(-3.0*k3*k3 + 4.0*k2) - 8.0*k1) + 4.0*k0;
    
        
        c2 /= 3.0;
        c1 *= 2.0;
        c0 /= 3.0;
        
        double Q = c2*c2 + c0;
        double R = 3.0*c0*c2 - c2*c2*c2 - c1*c1;
        
        
        double h = R*R - Q*Q*Q;
        double z = 0.0;
        if( h < 0.0 )
        {
            // 4 intersections
            double sQ = sqrt(Q);
            z = 2.0*sQ*cos( acos(R/(sQ*Q)) / 3.0 );
        }
        else
        {
            // 2 intersections
            double sQ = pow( sqrt(h) + fabs(R), 1.0/3.0 );
            z = sign(R)*fabs( sQ + Q/sQ );
        }       
        z = c2 - z;
        
        double d1 = z   - 3.0*c2;
        double d2 = z*z - 3.0*c0;
        if( fabs(d1) < 1.0e-4 )
        {
            if( d2 < 0.0 ) return -1.0;
            d2 = sqrt(d2);
        }
        else
        {
            if( d1 < 0.0 ) return -1.0;
            d1 = sqrt( d1/2.0 );
            d2 = c1/d1;
        }
    
        //----------------------------------
        
        double result = 1e20;
    
        h = d1*d1 - z + d2;
        if( h > 0.0 )
        {
            h = sqrt(h);
            double t1 = -d1 - h - k3; t1 = (po<0.0)?2.0/t1:t1;
            double t2 = -d1 + h - k3; t2 = (po<0.0)?2.0/t2:t2;
            if( t1 > 0.0 ) result=t1; 
            if( t2 > 0.0 ) result=fmin(result,t2);
        }
    
        h = d1*d1 - z - d2;
        if( h > 0.0 )
        {
            h = sqrt(h);
            double t1 = d1 - h - k3;  t1 = (po<0.0)?2.0/t1:t1;
            double t2 = d1 + h - k3;  t2 = (po<0.0)?2.0/t2:t2;
            if( t1 > 0.0 ) result=fmin(result,t1);
            if( t2 > 0.0 ) result=fmin(result,t2);
        }
    
        return result;
    }
    

    I found it at https://www.shadertoy.com/view/4sBGDy