fn barycentric_coordinates(s1: Vec3, s2: Vec3, s3: Vec3, pt: Vec3) -> Vec3 {
    let s1 = s1 - pt;
    let s2 = s2 - pt;
    let s3 = s3 - pt;

    let normal = (s2 - s1).cross(s3 - s1);
    let p0 = normal * s1.dot(normal) / normal.length_squared();

    // Find the axis with the greatest projected area
    let mut idx = 0;
    let mut area_max = 0.0;
    for i in 0..3 {
        let j = (i + 1) % 3;
        let k = (i + 2) % 3;

        let a = Vec2::new(s1[j], s1[k]);
        let b = Vec2::new(s2[j], s2[k]);
        let c = Vec2::new(s3[j], s3[k]);
        let ab = b - a;
        let ac = c - a;

        let area = ab.x * ac.y - ab.y * ac.x;
        if area * area > area_max * area_max {
            idx = i;
            area_max = area;
        }
    }

    // Project onto the appropriate axis
    let x = (idx + 1) % 3;
    let y = (idx + 2) % 3;
    let s = [
        Vec2::new(s1[x], s1[y]),
        Vec2::new(s2[x], s2[y]),
        Vec2::new(s3[x], s3[y]),
    ];
    let p = Vec2::new(p0[x], p0[y]);

    // Get the sub-areas of the triangles formed from the projected origin and edges
    let mut areas = Vec3::ZERO;
    for i in 0..3 {
        let j = (i + 1) % 3;
        let k = (i + 2) % 3;

        let a = p;
        let b = s[j];
        let c = s[k];
        let ab = b - a;
        let ac = c - a;

        areas[i] = ab.x * ac.y - ab.y * ac.x;
    }

    let mut lambdas = areas / area_max;
    if !lambdas.is_finite() {
        lambdas = Vec3::X;
    }

    lambdas
}
// This borrows our signed volum code to perform the barycentric coordinates.
This borrows our signed volum code to perform the barycentric coordinates.
This borrows our signed volum code to perform the barycentric coordinates .
fn barycentric_coordinates(s1: Vec3, s2: Vec3, s3: Vec3, pt: Vec3) -> Vec3 {
    let s1 = s1 - pt;
    let s2 = s2 - pt;
    let s3 = s3 - pt;
    let normal = (s2 - s1).cross(s3 - s1);
    let p0 = normal * s1.dot(normal) / normal.length_squared();
    // Find the axis with the greatest projected area
    let mut idx = 0;
    let mut area_max = 0.0;
    for i in 0..3 {
        let j = (i + 1) % 3;
        let k = (i + 2) % 3;
        let a = Vec2::new(s1[j], s1[k]);
        let b = Vec2::new(s2[j], s2[k]);
        let c = Vec2::new(s3[j], s3[k]);
        let ab = b - a;
        let ac = c - a;
        let area = ab.x * ac.y - ab.y * ac.x;
        if area * area > area_max * area_max {
            idx = i;
            area_max = area;
        }
    }
    // Project onto the appropriate axis
    let x = (idx + 1) % 3;
    let y = (idx + 2) % 3;
    let s = [
        Vec2::new(s1[x], s1[y]),
        Vec2::new(s2[x], s2[y]),
        Vec2::new(s3[x], s3[y]),
    ];
    let p = Vec2::new(p0[x], p0[y]);
    // Get the sub-areas of the triangles formed from the projected origin and edges
    let mut areas = Vec3::ZERO;
    for i in 0..3 {
        let j = (i + 1) % 3;
        let k = (i + 2) % 3;
        let a = p;
        let b = s[j];
        let c = s[k];
        let ab = b - a;
        let ac = c - a;
        areas[i] = ab.x * ac.y - ab.y * ac.x;
    }
    let mut lambdas = areas / area_max;
    if !lambdas.is_finite() {
        lambdas = Vec3::X;
    }
    lambdas
}
fn barycentric_coordinates ( s1 : Vec3 , s2 : Vec3 , s3 : Vec3 , pt : Vec3 ) -> Vec3 { let s1 = s1 - pt ; let s2 = s2 - pt ; let s3 = s3 - pt ; let normal = ( s2 - s1 ) . cross ( s3 - s1 ) ; let p0 = normal * s1 . dot ( normal ) / normal . length_squared ( ) ; // Find the axis with the greatest projected area let mut idx = 0 ; let mut area_max = 0.0 ; for i in 0 .. 3 { let j = ( i + 1 ) % 3 ; let k = ( i + 2 ) % 3 ; let a = Vec2 :: new ( s1 [ j ] , s1 [ k ] ) ; let b = Vec2 :: new ( s2 [ j ] , s2 [ k ] ) ; let c = Vec2 :: new ( s3 [ j ] , s3 [ k ] ) ; let ab = b - a ; let ac = c - a ; let area = ab . x * ac . y - ab . y * ac . x ; if area * area > area_max * area_max { idx = i ; area_max = area ; } } // Project onto the appropriate axis let x = ( idx + 1 ) % 3 ; let y = ( idx + 2 ) % 3 ; let s = [ Vec2 :: new ( s1 [ x ] , s1 [ y ] ) , Vec2 :: new ( s2 [ x ] , s2 [ y ] ) , Vec2 :: new ( s3 [ x ] , s3 [ y ] ) , ] ; let p = Vec2 :: new ( p0 [ x ] , p0 [ y ] ) ; // Get the sub-areas of the triangles formed from the projected origin and edges let mut areas = Vec3 :: ZERO ; for i in 0 .. 3 { let j = ( i + 1 ) % 3 ; let k = ( i + 2 ) % 3 ; let a = p ; let b = s [ j ] ; let c = s [ k ] ; let ab = b - a ; let ac = c - a ; areas [ i ] = ab . x * ac . y - ab . y * ac . x ; } let mut lambdas = areas / area_max ; if ! lambdas . is_finite ( ) { lambdas = Vec3 :: X ; } lambdas }
This borrows our signed volum code to perform the barycentric coordinates.
This borrows our signed volum code to perform the barycentric coordinates .
s1
Vec3
s2
Vec3
s3
Vec3
pt
Vec3
s1
Vec3
s2
Vec3
s3
Vec3
pt
Vec3