//


import { vec3 } from "gl-matrix";
import { max2 } from "../../../math/max2.js";
import { Miniball } from "../../packing/miniball/Miniball.js";
import { PointSet } from "../../packing/miniball/PointSet.js";

/**
 *
 * @type {vec3}
 */
const delta_direction = [];


const outer_points = new Float32Array(12);

/**
 *
 * @param {Float32Array|vec4|number[]} result
 * @param {Float32Array|vec4|number[]} a
 * @param {Float32Array|vec4|number[]} b
 */
export function compute_bounding_sphere_of_2_spheres(result, a, b) {
    // compute distance between the spheres
    vec3.sub(delta_direction, a, b);

    if (delta_direction[0] === 0 && delta_direction[1] === 0 && delta_direction[2] === 0) {
        // sphere centers overlap
        result[0] = a[0];
        result[1] = a[1];
        result[2] = a[2];
        result[3] = max2(a[3], b[3]);
        return;
    }

    vec3.normalize(delta_direction, delta_direction);

    // compute outer points on the spheres along the line between their centers
    const a_radius = a[3];

    const delta_x = delta_direction[0];
    const delta_y = delta_direction[1];
    const delta_z = delta_direction[2]


    outer_points[0] = delta_x * a_radius + a[0];
    outer_points[1] = delta_y * a_radius + a[1];
    outer_points[2] = delta_z * a_radius + a[2];

    outer_points[3] = -delta_x * a_radius + a[0];
    outer_points[4] = -delta_y * a_radius + a[1];
    outer_points[5] = -delta_z * a_radius + a[2];

    const b_radius = b[3];

    outer_points[6] = delta_x * b_radius + b[0];
    outer_points[7] = delta_y * b_radius + b[1];
    outer_points[8] = delta_z * b_radius + b[2];

    outer_points[9] = -delta_x * b_radius + b[0];
    outer_points[10] = -delta_y * b_radius + b[1];
    outer_points[11] = -delta_z * b_radius + b[2];

    // run standard miniball  to get new bounding sphere
    const miniball = new Miniball(new PointSet(4, 3, outer_points));

    const center = miniball.center();

    result[0] = center[0];
    result[1] = center[1];
    result[2] = center[2];
    result[3] = miniball.radius();
}
