import { ELEMENT_WORD_COUNT } from "../../../../../core/bvh2/bvh3/BVH.js";
import {
    aabb3_unsigned_distance_sqr_to_point
} from "../../../../../core/geom/3d/aabb/aabb3_unsigned_distance_sqr_to_point.js";

/**
 *
 * @param {number[]} result
 * @param {number} result_offset
 * @param {BVH} bvh
 * @param {number[]} node_list
 * @param {number} node_list_offset
 * @param {number} node_list_size
 * @param {number} x
 * @param {number} y
 * @param {number} z
 */
export function build_bvh_nodes_distances_sqr_to_point(
    result, result_offset,
    bvh,
    node_list, node_list_offset, node_list_size,
    x, y, z
) {
    /*
     For performance, we bind data directly to avoid extra copies required to read out AABB
     */
    const float32 = bvh.__data_float32;

    for (let i = 0; i < node_list_size; i++) {

        const node = node_list[i + node_list_offset];

        const address = node * ELEMENT_WORD_COUNT;

        const distance = aabb3_unsigned_distance_sqr_to_point(
            float32[address], float32[address + 1], float32[address + 2],
            float32[address + 3], float32[address + 4], float32[address + 5],
            x, y, z
        );

        result[result_offset + i] = distance;
    }
}