import { v3_distance_sqr } from "../../../vec3/v3_distance_sqr.js";
import { v3_dot } from "../../../vec3/v3_dot.js";

/**
 *
 * @param {TopoTriangle} f0
 * @param {TopoTriangle} f1
 * @returns {number}
 */
export function compute_face_connection_weight(f0, f1) {
    const vertices_0 = f0.vertices;
    const vertices_1 = f1.vertices;

    // compute center
    const f0_center_x = (vertices_0[0].x + vertices_0[1].x + vertices_0[2].x) * 0.333;
    const f0_center_y = (vertices_0[0].y + vertices_0[1].y + vertices_0[2].y) * 0.333;
    const f0_center_z = (vertices_0[0].z + vertices_0[1].z + vertices_0[2].z) * 0.333;

    const f1_center_x = (vertices_1[0].x + vertices_1[1].x + vertices_1[2].x) * 0.333;
    const f1_center_y = (vertices_1[0].y + vertices_1[1].y + vertices_1[2].y) * 0.333;
    const f1_center_z = (vertices_1[0].z + vertices_1[1].z + vertices_1[2].z) * 0.333;

    const distance_sqr = v3_distance_sqr(
        f0_center_x, f0_center_y, f0_center_z,
        f1_center_x, f1_center_y, f1_center_z
    );

    // compute normal dot
    const n0 = f0.normal;
    const n1 = f1.normal;

    const dot = v3_dot(
        n0[0], n0[1], n0[2],
        n1[0], n1[1], n1[2],
    );

    const distance_weight = distance_sqr !== 0 ? 0.01 / distance_sqr : 10000;

    return 2 + distance_weight + dot * 0.1;
}
