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

/**
 * Tests whether AABB intersects a clipping volume defined by a set of planes
 * Mainly useful for frustum checks
 * @param {number} x0
 * @param {number} y0
 * @param {number} z0
 * @param {number} x1
 * @param {number} y1
 * @param {number} z1
 * @param {ArrayLike<number>|number[]|Float32Array|Float64Array} planes
 * @param {number} planes_offset
 * @param {number} plane_count
 * @return {boolean}
 */
export function aabb3_intersects_clipping_volume_array(
    x0, y0, z0,
    x1, y1, z1,
    planes, planes_offset, plane_count
) {

    assert.isNonNegativeInteger(planes_offset, 'frustum_offset');

    const limit = plane_count * 4;

    for (let i = 0; i < limit; i += 4) {

        const offset = planes_offset + i;

        const plane_x = planes[offset];
        const plane_y = planes[offset + 1];
        const plane_z = planes[offset + 2];
        const plane_w = planes[offset + 3];

        const plane_distance = aabb3_compute_distance_above_plane_max(
            plane_x, plane_y, plane_z, plane_w,
            x0, y0, z0,
            x1, y1, z1
        );

        if (plane_distance < 0) {
            return false;
        }

    }

    return true;

}
