import { assert } from "../../../assert.js";
import { aabb3_array_intersects_sphere_array } from "../../../geom/3d/aabb/aabb3_array_intersects_sphere_array.js";
import { BVHQuery } from "./BVHQuery.js";

/**
 * We read node bounds here
 * @type {Float32Array}
 */
const scratch_aabb = new Float32Array(6);

export class BVHQueryIntersectsSphere extends BVHQuery {

    /**
     * [x,y,z,radius]
     * @type {number[]}
     */
    sphere = [0,0,0,0];

    /**
     *
     * @param {number[]|ArrayLike<number>} sphere (x,y,z,radius)
     * @returns {BVHQueryIntersectsRay}
     */
    static from(sphere) {
        assert.isArrayLike(sphere, 'sphere');

        const r = new BVHQueryIntersectsSphere();

        r.sphere = sphere;

        return r;
    }

    evaluate(node, tree) {

        tree.node_get_aabb(node, scratch_aabb);

        // test node against the ray
        return aabb3_array_intersects_sphere_array(
            scratch_aabb,
            this.sphere
        );
    }
}
