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

Code before:
        }
    }

    /// Returns the unit normal vector of the bisecting plane that characterizes the `HalfSpace`.
    #[inline]
    pub fn normal(&self) -> Vec3A {
        Vec3A::from_vec4(self.normal_d)
    }

    /// Returns the signed distance from the bisecting plane to the origin along
    /// the plane's unit normal vector.
    #[inline]
    pub fn d(&self) -> f32 {
        self.normal_d.w
    }

    /// Returns the bisecting plane's unit normal vector and the signed distance
    /// from the plane to the origin.
    #[inline]
    pub fn normal_d(&self) -> Vec4 {
        self.normal_d
    }

    /// Returns the intersection position if the three halfspaces all intersect at a single point.
    #[inline]
    pub fn intersect(a: HalfSpace, b: HalfSpace, c: HalfSpace) -> Option<Vec3> {
        let an = a.normal();
        let bn = b.normal();
        let cn = c.normal();

        let x = Vec3A::new(an.x, bn.x, cn.x);
        let y = Vec3A::new(an.y, bn.y, cn.y);
        let z = Vec3A::new(an.z, bn.z, cn.z);

        let d = -Vec3A::new(a.d(), b.d(), c.d());

        let u = y.cross(z);
        let v = x.cross(d);

        let denom = x.dot(u);

        if ops::abs(denom) < f32::EPSILON {
            return None;
        }

        Some(Vec3::new(d.dot(u), z.dot(v), -y.dot(v)) / denom)
    }
}


Review comment (bug):
Do we want to consider renaming this function something like `intersection_point` or `planar_intersection`? I think it may help clarify:

1. With the current name, my first assumption would be that it returns the convex set that is the intersection of the points in each half space, and not the planes defining them. 
2. In the edge case that the planes intersect at a line or plane, this function returns None (the best I can tell from some simple testing).

Code after:
        }
    }

    /// Returns the unit normal vector of the bisecting plane that characterizes the `HalfSpace`.
    #[inline]
    pub fn normal(&self) -> Vec3A {
        Vec3A::from_vec4(self.normal_d)
    }

    /// Returns the signed distance from the bisecting plane to the origin along
    /// the plane's unit normal vector.
    #[inline]
    pub fn d(&self) -> f32 {
        self.normal_d.w
    }

    /// Returns the bisecting plane's unit normal vector and the signed distance
    /// from the plane to the origin.
    #[inline]
    pub fn normal_d(&self) -> Vec4 {
        self.normal_d
    }

    /// Returns the intersection point if the three halfspaces all intersect at a single point.
    #[inline]
    pub fn intersection_point(a: HalfSpace, b: HalfSpace, c: HalfSpace) -> Option<Vec3> {
        let an = a.normal();
        let bn = b.normal();
        let cn = c.normal();

        let x = Vec3A::new(an.x, bn.x, cn.x);
        let y = Vec3A::new(an.y, bn.y, cn.y);
        let z = Vec3A::new(an.z, bn.z, cn.z);

        let d = -Vec3A::new(a.d(), b.d(), c.d());

        let u = y.cross(z);
        let v = x.cross(d);

        let denom = x.dot(u);

        if ops::abs(denom) < f32::EPSILON {
            return None;
        }

        Some(Vec3::new(d.dot(u), z.dot(v), -y.dot(v)) / denom)
    }
}

#[cfg(test)]
mod half_space_tests {