import { max2 } from "../../../../../core/math/max2.js";
import {
    sphere_radius_sqr_from_v3_array_transformed
} from "../../../../../core/geom/3d/sphere/sphere_radius_sqr_from_v3_array_transformed.js";

/**
 * Computes radius of bounding sphere around origin for given group of objects
 *
 * NOTE: This may seem pointless, but by doing this computation on per-vertex basis we get the tightest possible bounds,
 * allowing us to get the best possible texture space utilization.
 * @param {{mesh:ShadedGeometry, transform:mat4}[]} objects
 * @returns {number}
 */
export function compute_bounding_sphere_radius_only(objects) {
    let distance_sqr = 0;

    const n = objects.length;
    for (let i = 0; i < n; i++) {
        const object = objects[i];

        const attribute = object.mesh.geometry.getAttribute('position');

        const array = attribute.array;

        const transform = object.transform;

        const n = array.length;

        const radius2 = sphere_radius_sqr_from_v3_array_transformed(array, n, transform);

        distance_sqr = max2(distance_sqr, radius2);

    }


    return Math.sqrt(distance_sqr);
}
