/// Calculate closest point on triangle to an input point
Calculate closest point on triangle to an input point
Calculate closest point on triangle to an input point
fn triangle_closest_point_to_point(triangle: [Vec3f; 3], p: Vec3f) -> Vec3f {
    let a = triangle[0];
    let b = triangle[1];
    let c = triangle[2];
    // Check if P in vertex region outside A
    let ab = b - a;
    let ac = c - a;
    let ap = p - a;
    let d1 = ab.dot(ap);
    let d2 = ac.dot(ap);
    if d1 <= 0.0f32 && d2 <= 0.0f32 {
        return a; // barycentric coordinates (1,0,0)
    }
    // Check if P in vertex region outside B
    let bp = p - b;
    let d3 = ab.dot(bp);
    let d4 = ac.dot(bp);
    if d3 >= 0.0f32 && d4 <= d3 {
        return b; // barycentric coordinates (0,1,0)
    }
    // Check if P in edge region of AB, if so return projection of P onto AB
    let vc = d1*d4 - d3*d2;
    if vc <= 0.0f32 && d1 >= 0.0f32 && d3 <= 0.0f32 {
        let v = d1 / (d1 - d3);
        return a + ab * v; // barycentric coordinates (1-v,v,0)
    }
    // Check if P in vertex region outside C
    let cp = p - c;
    let d5 = ab.dot(cp);
    let d6 = ac.dot(cp);
    if d6 >= 0.0f32 && d5 <= d6 {
        return c; // barycentric coordinates (0,0,1)
    }
    // Check if P in edge region of AC, if so return projection of P onto AC
    let vb = d5*d2 - d1*d6;
    if vb <= 0.0f32 && d2 >= 0.0f32 && d6 <= 0.0f32 {
        let w = d2 / (d2 - d6);
        return a + ac * w; // barycentric coordinates (1-w,0,w)
    }
    // Check if P in edge region of BC, if so return projection of P onto BC
    let va = d3*d6 - d5*d4;
    if va <= 0.0f32 && (d4 - d3) >= 0.0f32 && (d5 - d6) >= 0.0f32 {
        let w = (d4 - d3) / ((d4 - d3) + (d5 - d6));
        return b + (c - b) * w; // barycentric coordinates (0,1-w,w)
    }
    // P inside face region. Compute Q through its barycentric coordinates (u,v,w)
    let denom = 1.0f32 / (va + vb + vc);
    let v = vb * denom;
    let w = vc * denom;
    return a + ab * v + ac * w; // = u*a + v*b + w*c, u = va * denom = 1.0f - v - w
}
fn triangle_closest_point_to_point ( triangle : [ Vec3f ; 3 ] , p : Vec3f ) -> Vec3f { let a = triangle [ 0 ] ; let b = triangle [ 1 ] ; let c = triangle [ 2 ] ; // Check if P in vertex region outside A let ab = b - a ; let ac = c - a ; let ap = p - a ; let d1 = ab . dot ( ap ) ; let d2 = ac . dot ( ap ) ; if d1 <= 0.0f32 && d2 <= 0.0f32 { return a ; // barycentric coordinates (1,0,0) } // Check if P in vertex region outside B let bp = p - b ; let d3 = ab . dot ( bp ) ; let d4 = ac . dot ( bp ) ; if d3 >= 0.0f32 && d4 <= d3 { return b ; // barycentric coordinates (0,1,0) } // Check if P in edge region of AB, if so return projection of P onto AB let vc = d1 * d4 - d3 * d2 ; if vc <= 0.0f32 && d1 >= 0.0f32 && d3 <= 0.0f32 { let v = d1 / ( d1 - d3 ) ; return a + ab * v ; // barycentric coordinates (1-v,v,0) } // Check if P in vertex region outside C let cp = p - c ; let d5 = ab . dot ( cp ) ; let d6 = ac . dot ( cp ) ; if d6 >= 0.0f32 && d5 <= d6 { return c ; // barycentric coordinates (0,0,1) } // Check if P in edge region of AC, if so return projection of P onto AC let vb = d5 * d2 - d1 * d6 ; if vb <= 0.0f32 && d2 >= 0.0f32 && d6 <= 0.0f32 { let w = d2 / ( d2 - d6 ) ; return a + ac * w ; // barycentric coordinates (1-w,0,w) } // Check if P in edge region of BC, if so return projection of P onto BC let va = d3 * d6 - d5 * d4 ; if va <= 0.0f32 && ( d4 - d3 ) >= 0.0f32 && ( d5 - d6 ) >= 0.0f32 { let w = ( d4 - d3 ) / ( ( d4 - d3 ) + ( d5 - d6 ) ) ; return b + ( c - b ) * w ; // barycentric coordinates (0,1-w,w) } // P inside face region. Compute Q through its barycentric coordinates (u,v,w) let denom = 1.0f32 / ( va + vb + vc ) ; let v = vb * denom ; let w = vc * denom ; return a + ab * v + ac * w ; // = u*a + v*b + w*c, u = va * denom = 1.0f - v - w }
Calculate closest point on triangle to an input point
Calculate closest point on triangle to an input point
triangle
[Vec3f; 3]
p
Vec3f