Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #pragma once
0002 
0003 LEAF_FUNC
0004 float distance_leaf_plane( const float3& pos, const quad& q0 )
0005 {
0006     const float3 n = make_float3(q0.f.x, q0.f.y, q0.f.z) ;   // plane normal direction  
0007     const float d = q0.f.w ;                                 // distance to origin 
0008     float pn = dot(pos, n ); 
0009     float sd = pn - d ;  
0010     return sd ; 
0011 }
0012 
0013 
0014 /**
0015 intersect_leaf_plane
0016 -----------------------
0017 
0018 * https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-plane-and-ray-disk-intersection
0019 
0020 Equation for points p that are in the plane::
0021 
0022    (p - p0).n = 0      
0023 
0024    p0   : point in plane which is pointed to by normal n vector from origin,  
0025    p-p0 : vector that lies within the plane, and hence is perpendicular to the normal direction 
0026    p0.n : d, distance from plane to origin 
0027 
0028 
0029    p = o + t v   : parametric ray equation  
0030 
0031    (o + t v - p0).n = 0 
0032 
0033    (p0-o).n  = t v.n
0034 
0035             (p0 - o).n        d - o.n
0036        t  = -----------  =   -----------
0037                v.n              v.n  
0038 
0039 
0040 Special case example : 
0041 
0042 * for rays within XZ plane what is the z-coordinate at which rays cross the x=0 "line" ?
0043 
0044 
0045                 : 
0046                 :    Z
0047                 :    |
0048                 p0   +--X
0049                /:
0050               / :
0051              /  :
0052             /   :      
0053            /    :
0054           +     :
0055          o     x=0
0056     
0057 
0058          plane normal  [-1, 0, 0]
0059 
0060     t0 = -o.x/v.x
0061     z0 =  o.z + t0*v.z 
0062 
0063 **/
0064 
0065 LEAF_FUNC
0066 void intersect_leaf_plane( bool& valid_isect, float4& isect, const quad& q0, const float t_min, const float3& ray_origin, const float3& ray_direction )
0067 {
0068    const float3 n = make_float3(q0.f.x, q0.f.y, q0.f.z) ;   // plane normal direction  
0069    const float d = q0.f.w ;                                 // distance to origin 
0070 
0071    float idn = 1.f/dot(ray_direction, n );
0072    float on = dot(ray_origin, n ); 
0073 
0074    float t_cand = (d - on)*idn ;
0075 
0076    bool valid_isect = t_cand > t_min ;
0077    if( valid_isect ) 
0078    {
0079        isect.x = n.x ;
0080        isect.y = n.y ;
0081        isect.z = n.z ;
0082        isect.w = t_cand ; 
0083    }
0084 }
0085 
0086 
0087