import { assert } from "../../../assert.js";
import { frustum_compute_corners } from "./frustum_compute_corners.js";

const scratch_corners = new Float32Array(24);

/**
 *
 * @param {number[]} output
 * @param {number} output_offset
 * @param {number[]} frustum
 * @param {number} frustum_offset
 */
export function frustum_compute_center(
    output,
    output_offset,
    frustum,
    frustum_offset
) {
    assert.defined(output,'output');
    assert.isNonNegativeInteger(output_offset,'output_offset');
    assert.defined(frustum,'frustum');
    assert.isNonNegativeInteger(frustum_offset,'frustum_offset');

    frustum_compute_corners(scratch_corners, 0, frustum, frustum_offset);

    let x = 0;
    let y = 0;
    let z = 0;

    for (let i = 0; i < 8; i++) {
        const i3 = i * 3;

        x += scratch_corners[i3]
        y += scratch_corners[i3 + 1]
        z += scratch_corners[i3 + 2]
    }

    output[output_offset] = x / 8;
    output[output_offset + 1] = y / 8;
    output[output_offset + 2] = z / 8;
}