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

Code before:
    #[inline]
    pub fn intersects_obb(
        &self,
        aabb: &Aabb,
        world_from_local: &Affine3A,
        intersect_near: bool,
        intersect_far: bool,
    ) -> bool {
        let aabb_center_world = world_from_local.transform_point3a(aabb.center).extend(1.0);
        for (idx, half_space) in self.half_spaces.into_iter().enumerate() {
            if idx == 4 && !intersect_near {
                continue;
            }
            if idx == 5 && !intersect_far {
                continue;
            }
            let p_normal = half_space.normal();
            let relative_radius = aabb.relative_radius(&p_normal, &world_from_local.matrix3);
            if half_space.normal_d().dot(aabb_center_world) + relative_radius <= 0.0 {
                return false;
            }
        }
        true
    }

    /// optimized version of [`Frustum::intersects_obb`]
    /// where the transform is [`Affine3A::IDENTITY`] and both `intersect_near` and `intersect_far` are `true`.
    #[inline]
    pub fn intersects_obb_identity(&self, aabb: &Aabb) -> bool {
        let aabb_center_world = aabb.center.extend(1.0);
        for half_space in self.half_spaces.iter() {
            let p_normal = half_space.normal();
            let relative_radius = aabb.half_extents.abs().dot(p_normal.abs());
            if half_space.normal_d().dot(aabb_center_world) + relative_radius <= 0.0 {
                return false;
            }
        }
        true
    }

    /// Check if the frustum contains the entire Axis-Aligned Bounding Box (AABB).
    /// Referenced from: [Frustum Culling](https://learnopengl.com/Guest-Articles/2021/Scene/Frustum-Culling)
    #[inline]
    pub fn contains_aabb(&self, aabb: &Aabb, world_from_local: &Affine3A) -> bool {
        for half_space in &self.half_spaces {
            if !aabb.is_in_half_space(half_space, world_from_local) {
                return false;
            }
        }
        true
    }

Review comment (performance):
```suggestion
    /// Optimized version of [`Frustum::intersects_obb`]
```

Code after:
        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,
        aabb: &Aabb,
        world_from_local: &Affine3A,
        intersect_near: bool,
        intersect_far: bool,
    ) -> bool {
        let aabb_center_world = world_from_local.transform_point3a(aabb.center).extend(1.0);

        for (idx, half_space) in self.half_spaces.into_iter().enumerate() {
            if (idx == Self::NEAR_PLANE_IDX && !intersect_near)
                || (idx == Self::FAR_PLANE_IDX && !intersect_far)
            {
                continue;
            }
            let p_normal = half_space.normal();
            let relative_radius = aabb.relative_radius(&p_normal, &world_from_local.matrix3);
            if half_space.normal_d().dot(aabb_center_world) + relative_radius <= 0.0 {