import { max3 } from "../../../math/max3.js";

/**
 * Compute squared distance to point, value is 0 if the point is inside the box
 * @param {number} x0
 * @param {number} y0
 * @param {number} z0
 * @param {number} x1
 * @param {number} y1
 * @param {number} z1
 * @param {number} point_x
 * @param {number} point_y
 * @param {number} point_z
 * @returns {number}
 */
export function aabb3_unsigned_distance_sqr_to_point(
    x0, y0, z0,
    x1, y1, z1,
    point_x, point_y, point_z
) {
    //do projection
    const xp0 = x0 - point_x;
    const xp1 = point_x - x1;
    const yp0 = y0 - point_y;
    const yp1 = point_y - y1;
    const zp0 = z0 - point_z;
    const zp1 = point_z - z1;

    //calculate separation in each axis
    const dx = max3(0, xp0, xp1);
    const dy = max3(0, yp0, yp1);
    const dz = max3(0, zp0, zp1);

    //straight-line distance
    return dx * dx + dy * dy + dz * dz;
}