pub fn hit<'scene>(
        &self,
        objects: &[&'scene (dyn Hittable + Sync)],
        r: &Ray,
        t_min: f32,
        t_max: f32,
    ) -> Option<Hit<'scene>> {
        let inv_dir = r.inv_direction();
        let inv_dir_f32x4 = f32x4::from(*inv_dir.as_array());
        let inv_dir_neg_mask = inv_dir_f32x4.cmp_lt(wide::f32x4::ZERO);
        let inv_dir_neg_bitmask = inv_dir_neg_mask.move_mask();
        let mut index_stack = ArrayVec::<_, 64>::new();
        index_stack.push(0);
        let mut hit: Option<Hit<'scene>> = None;
        let mut t_max = t_max;
        while let Some(i) = index_stack.pop() {
            match &self.nodes[i] {
                BvhNode::Leaf { obj_i } => {
                    if let Some(h) = objects[*obj_i].hit(r, t_min, t_max) {
                        t_max = h.t;
                        hit = Some(h);
                    }
                }
                BvhNode::Inner {
                    aabb,
                    axis,
                    left_i,
                    right_i,
                } => {
                    if !aabb.intersect(r, inv_dir_f32x4, inv_dir_neg_mask, t_min, t_max) {
                        continue;
                    }
                    if inv_dir_neg_bitmask & 1 << (*axis as usize) != 0 {
                        if let Some(i) = *left_i {
                            index_stack.push(i);
                        }
                        if let Some(i) = *right_i {
                            index_stack.push(i);
                        }
                    } else {
                        if let Some(i) = *right_i {
                            index_stack.push(i);
                        }
                        if let Some(i) = *left_i {
                            index_stack.push(i);
                        }
                    }
                }
            }
        }
        hit
    }
/// Hit traverses the BVH and returns the closest hit, if any.
/// See http://www.pbr-book.org/3ed-2018/Primitives_and_Intersection_Acceleration/Bounding_Volume_Hierarchies.html#Traversal

Hit traverses the BVH and returns the closest hit, if any.
Hit traverses the BVH and returns the closest hit if any .
pub fn hit<'scene>(
        &self,
        objects: &[&'scene (dyn Hittable + Sync)],
        r: &Ray,
        t_min: f32,
        t_max: f32,
    ) -> Option<Hit<'scene>> {
        let inv_dir = r.inv_direction();
        let inv_dir_f32x4 = f32x4::from(*inv_dir.as_array());
        let inv_dir_neg_mask = inv_dir_f32x4.cmp_lt(wide::f32x4::ZERO);
        let inv_dir_neg_bitmask = inv_dir_neg_mask.move_mask();
        let mut index_stack = ArrayVec::<_, 64>::new();
        index_stack.push(0);
        let mut hit: Option<Hit<'scene>> = None;
        let mut t_max = t_max;
        while let Some(i) = index_stack.pop() {
            match &self.nodes[i] {
                BvhNode::Leaf { obj_i } => {
                    if let Some(h) = objects[*obj_i].hit(r, t_min, t_max) {
                        t_max = h.t;
                        hit = Some(h);
                    }
                }
                BvhNode::Inner {
                    aabb,
                    axis,
                    left_i,
                    right_i,
                } => {
                    if !aabb.intersect(r, inv_dir_f32x4, inv_dir_neg_mask, t_min, t_max) {
                        continue;
                    }
                    if inv_dir_neg_bitmask & 1 << (*axis as usize) != 0 {
                        if let Some(i) = *left_i {
                            index_stack.push(i);
                        }
                        if let Some(i) = *right_i {
                            index_stack.push(i);
                        }
                    } else {
                        if let Some(i) = *right_i {
                            index_stack.push(i);
                        }
                        if let Some(i) = *left_i {
                            index_stack.push(i);
                        }
                    }
                }
            }
        }
        hit
    }
pub fn hit < ' scene > ( & self , objects : & [ & ' scene ( dyn Hittable + Sync ) ] , r : & Ray , t_min : f32 , t_max : f32 , ) -> Option < Hit < ' scene > > { let inv_dir = r . inv_direction ( ) ; let inv_dir_f32x4 = f32x4 :: from ( * inv_dir . as_array ( ) ) ; let inv_dir_neg_mask = inv_dir_f32x4 . cmp_lt ( wide :: f32x4 :: ZERO ) ; let inv_dir_neg_bitmask = inv_dir_neg_mask . move_mask ( ) ; let mut index_stack = ArrayVec :: < _ , 64 > :: new ( ) ; index_stack . push ( 0 ) ; let mut hit : Option < Hit < ' scene > > = None ; let mut t_max = t_max ; while let Some ( i ) = index_stack . pop ( ) { match & self . nodes [ i ] { BvhNode :: Leaf { obj_i } => { if let Some ( h ) = objects [ * obj_i ] . hit ( r , t_min , t_max ) { t_max = h . t ; hit = Some ( h ) ; } } BvhNode :: Inner { aabb , axis , left_i , right_i , } => { if ! aabb . intersect ( r , inv_dir_f32x4 , inv_dir_neg_mask , t_min , t_max ) { continue ; } if inv_dir_neg_bitmask & 1 << ( * axis as usize ) != 0 { if let Some ( i ) = * left_i { index_stack . push ( i ) ; } if let Some ( i ) = * right_i { index_stack . push ( i ) ; } } else { if let Some ( i ) = * right_i { index_stack . push ( i ) ; } if let Some ( i ) = * left_i { index_stack . push ( i ) ; } } } } } hit }
Hit traverses the BVH and returns the closest hit, if any.
Hit traverses the BVH and returns the closest hit if any .
&self
objects
&[&'scene (dyn Hittable + Sync)]
r
&Ray
t_min
f32
t_max
f32
&self
objects
&[&'scene (dyn Hittable + Sync)]
r
&Ray
t_min
f32
t_max
f32