import {
    tetrahedral_mesh_find_tets_attached_to_vertex
} from "../../../../core/geom/3d/tetrahedra/tetrahedral_mesh_find_tets_attached_to_vertex.js";
import { v3_distance_sqr } from "../../../../core/geom/vec3/v3_distance_sqr.js";
import { max2 } from "../../../../core/math/max2.js";

/**
 *
 * @type {number[]}
 */
const tets = [];

/**
 *
 * @param {number} vertex_index
 * @param {TetrahedralMesh} mesh
 * @param {number[]} points
 */
export function find_max_depth_radius_for_point(vertex_index, mesh, points) {

    const count = tetrahedral_mesh_find_tets_attached_to_vertex(
        tets, 0,
        mesh,
        vertex_index
    );

    const ref_x = points[vertex_index * 3];
    const ref_y = points[vertex_index * 3 + 1];
    const ref_z = points[vertex_index * 3 + 2];

    let radius_sqr = 0;

    for (let i = 0; i < count; i++) {
        const tet = tets[i];

        for (let j = 0; j < 4; j++) {
            const v = mesh.getVertexIndex(tet, j);

            const x = points[v * 3];
            const y = points[v * 3 + 1];
            const z = points[v * 3 + 2];

            const r2 = v3_distance_sqr(ref_x, ref_y, ref_z, x, y, z);

            radius_sqr = max2(r2, radius_sqr);
        }
    }

    return Math.sqrt(radius_sqr);
}