Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:54

0001 #pragma once
0002 /**
0003 intersect_leaf_infcylinder
0004 ----------------------------------
0005 
0006 Use standard Z-axial cylinder orientation to see how much it simplifies::
0007 
0008     x^2 + y^2 = r^2
0009 
0010     (Ox + t Dx)^2 + (Oy + t Dy)^2 = r^2 
0011 
0012     Ox ^2 + t^2 Dx^2 + 2 t Ox Dx   
0013     Oy ^2 + t^2 Dy^2 + 2 t Oy Dy
0014 
0015 
0016     t^2 (Dx^2 + Dy^2) + 2 t ( OxDx + Oy Dy ) + Ox^2 + Oy^2 - r^2  = 0     
0017 
0018 Contrast this eqn with that on RTCD p195 "bk-;bk-rtcd 195"  : its a natural simplification.
0019 Instead of dotting all components and subtracting the axial part can just directly 
0020 dot the non-axial x and y thanks to the fixed orientation.
0021 
0022 **/
0023 
0024 LEAF_FUNC
0025 void intersect_leaf_infcylinder( bool& valid_isect, float4& isect, const quad& q0, const quad& q1, const float t_min, const float3& ray_origin, const float3& ray_direction )
0026 {
0027     const float r = q0.f.w ; 
0028 
0029     const float3& O = ray_origin ;    
0030     const float3& D = ray_direction ;    
0031 
0032     float a = D.x*D.x + D.y*D.y ; 
0033     float b = O.x*D.x + O.y*D.y ; 
0034     float c = O.x*O.x + O.y*O.y - r*r ;  
0035 
0036     float disc = b*b-a*c;
0037 
0038     if(disc > 0.0f)  // has intersections with the infinite cylinder
0039     {
0040         float t_NEAR, t_FAR, sdisc ;   
0041 
0042         robust_quadratic_roots(t_NEAR, t_FAR, disc, sdisc, a, b, c); //  Solving:  a t^2 + 2 b t +  c = 0 
0043 
0044         float t_cand = sdisc > 0.f ? ( t_NEAR > t_min ? t_NEAR : t_FAR ) : t_min ;
0045 
0046         valid_isect = t_cand > t_min ; 
0047 
0048         if( valid_isect  )
0049         {
0050             isect.x = (O.x + t_cand*D.x)/r ;   // normalized by construction
0051             isect.y = (O.y + t_cand*D.y)/r ;
0052             isect.z = 0.f ;
0053             isect.w = t_cand ; 
0054         }
0055     }
0056 }
0057