Repository: bevyengine/bevy
File: crates/bevy_camera/src/primitives.rs (Rust)

Code before:
        clip_from_world: &Mat4,
        view_translation: &Vec3,
        view_backward: &Vec3,
        far: f32,
    ) -> Self {
        let mut frustum = Frustum::from_clip_from_world_no_far(clip_from_world);
        let far_center = *view_translation - far * *view_backward;
        frustum.half_spaces[Self::FAR_PLANE_IDX] =
            HalfSpace::new(view_backward.extend(-view_backward.dot(far_center)));
        frustum
    }

    // NOTE: This approach of extracting the frustum half-space from the view
    // projection matrix is from Foundations of Game Engine Development 2
    // Rendering by Lengyel.
    /// Returns a frustum derived from `view_projection`,
    /// without a far plane.
    fn from_clip_from_world_no_far(clip_from_world: &Mat4) -> Self {
        let mut half_spaces = [HalfSpace::default(); 6];

        let row0 = clip_from_world.row(0);
        let row1 = clip_from_world.row(1);
        let row2 = clip_from_world.row(2);
        let row3 = clip_from_world.row(3);

        half_spaces[Self::LEFT_PLANE_IDX] = HalfSpace::new(row3 + row0);
        half_spaces[Self::RIGHT_PLANE_IDX] = HalfSpace::new(row3 - row0);
        half_spaces[Self::BOTTOM_PLANE_IDX] = HalfSpace::new(row3 + row1);
        half_spaces[Self::TOP_PLANE_IDX] = HalfSpace::new(row3 - row1);
        half_spaces[Self::NEAR_PLANE_IDX] = HalfSpace::new(row3 + row2);
        half_spaces[Self::FAR_PLANE_IDX] = HalfSpace::new(Self::INACTIVE_HALF_SPACE);
        Self { half_spaces }
    }

    /// Checks if a sphere intersects the frustum.
    #[inline]
    pub fn intersects_sphere(&self, sphere: &Sphere, intersect_far: bool) -> bool {
        let sphere_center = sphere.center.extend(1.0);
        let max = if intersect_far {
            Self::FAR_PLANE_IDX
        } else {
            Self::NEAR_PLANE_IDX
        };
        for half_space in &self.half_spaces[..=max] {
            if half_space.normal_d().dot(sphere_center) + sphere.radius <= 0.0 {
                return false;
            }
        }
        true
    }


Review comment (suggestion):
I think this section could also be written as a simple array instead of assigning each element individually.
i.e. `Self { half_spaces: [HalfSpace::new(row3 + row0), ..] }`

Code after:
        let mut frustum = Frustum::from_clip_from_world_no_far(clip_from_world);
        let far_center = *view_translation - far * *view_backward;
        frustum.half_spaces[Self::FAR_PLANE_IDX] =
            HalfSpace::new(view_backward.extend(-view_backward.dot(far_center)));
        frustum
    }

    // NOTE: This approach of extracting the frustum half-space from the view
    // projection matrix is from Foundations of Game Engine Development 2
    // Rendering by Lengyel.
    /// Returns a frustum derived from `view_projection`,
    /// without a far plane.
    fn from_clip_from_world_no_far(clip_from_world: &Mat4) -> Self {
        let row0 = clip_from_world.row(0);
        let row1 = clip_from_world.row(1);
        let row2 = clip_from_world.row(2);
        let row3 = clip_from_world.row(3);

        Self {
            half_spaces: [
                HalfSpace::new(row3 + row0),
                HalfSpace::new(row3 - row0),
                HalfSpace::new(row3 + row1),
                HalfSpace::new(row3 - row1),
                HalfSpace::new(row3 + row2),
                HalfSpace::new(Self::INACTIVE_HALF_SPACE),
            ],
        }
    }

    /// Checks if a sphere intersects the frustum.
    #[inline]
    pub fn intersects_sphere(&self, sphere: &Sphere, intersect_far: bool) -> bool {
        let sphere_center = sphere.center.extend(1.0);
        let max = if intersect_far {
            Self::FAR_PLANE_IDX
        } else {
            Self::NEAR_PLANE_IDX
        };
        for half_space in &self.half_spaces[..=max] {
            if half_space.normal_d().dot(sphere_center) + sphere.radius <= 0.0 {
                return false;
            }
        }
        true
    }

    /// Checks if an Oriented Bounding Box (obb) intersects the frustum.
    #[inline]
    pub fn intersects_obb(
        &self,