import { Miniball } from "../../../packing/miniball/Miniball.js";
import { PointSet } from "../../../packing/miniball/PointSet.js";
import { assert } from "../../../../assert.js";

/**
 *
 * @param {number[]|vec4|Float32Array} result
 * @param {TopoMesh} mesh
 */
export function computeTopoMeshBoundingSphere(result, mesh) {
    assert.equal(mesh.isTopoMesh, true, 'mesh.isTopoMesh !== true');

    const vertices = mesh.vertices;
    const vertex_count = vertices.length;
    const vertex_data = new Float32Array(vertex_count * 3);

    for (let i = 0; i < vertex_count; i++) {
        const vertex = vertices[i];

        const i3 = i * 3;

        vertex_data[i3] = vertex.x;
        vertex_data[i3 + 1] = vertex.y;
        vertex_data[i3 + 2] = vertex.z;
    }

    const pointSet = new PointSet(vertex_count, 3, vertex_data);
    const miniball = new Miniball(pointSet);

    const center = miniball.center();
    const radius = miniball.radius();

    result[0] = center[0];
    result[1] = center[1];
    result[2] = center[2];
    result[3] = radius;
}
