import {
    line3_compute_segment_point_distance_eikonal
} from "../../../../line/line3_compute_segment_point_distance_eikonal.js";

const scratch_vertex = new Float32Array(3);

/**
 * Solves the quadratic Eikonal equation for a triangle.
 * Based on the Kimmel-Sethian update for Fast Marching Methods.
 * @param {BinaryTopology} mesh
 * @param {number} v0 Vertex ID of edge start
 * @param {number} v1 Vertex ID of edge end
 * @param {number} d0 Distance at v0
 * @param {number} d1 Distance at v1
 * @param {number} v_target The vertex we are solving for
 * @returns {number} The exact geodesic distance
 */
export function bt_mesh_compute_edge_distance_eikonal(
    mesh,
    v0, v1,
    d0, d1,
    v_target
) {
    // 1. Read Coordinates
    mesh.vertex_read_coordinate(scratch_vertex, 0, v0);
    const ax = scratch_vertex[0];
    const ay = scratch_vertex[1];
    const az = scratch_vertex[2];

    mesh.vertex_read_coordinate(scratch_vertex, 0, v1);
    const bx = scratch_vertex[0];
    const by = scratch_vertex[1];
    const bz = scratch_vertex[2];

    mesh.vertex_read_coordinate(scratch_vertex, 0, v_target);
    const cx = scratch_vertex[0];
    const cy = scratch_vertex[1];
    const cz = scratch_vertex[2];

    return line3_compute_segment_point_distance_eikonal(
        d0, d1,
        ax, ay, az,
        bx, by, bz,
        cx, cy, cz
    );
}