/**
 *
 * @param {BVH} bvh
 * @param {number[]|Float32Array} triangles
 * @param {number} triangle_count
 */
export function bvh_build_from_unindexed_triangles(
    bvh,
    triangles,
    triangle_count,
) {

    for (let i = 0; i < triangle_count; i++) {
        const triangle_address = i * 9;

        const ax = triangles[triangle_address];
        const ay = triangles[triangle_address + 1];
        const az = triangles[triangle_address + 2];

        const bx = triangles[triangle_address + 3];
        const by = triangles[triangle_address + 4];
        const bz = triangles[triangle_address + 5];

        const cx = triangles[triangle_address + 6];
        const cy = triangles[triangle_address + 7];
        const cz = triangles[triangle_address + 8];

        const bounds_x0 = Math.min(ax, bx, cx);
        const bounds_x1 = Math.max(ax, bx, cx);
        const bounds_z0 = Math.min(az, bz, cz);

        const bounds_y0 = Math.min(ay, by, cy);
        const bounds_y1 = Math.max(ay, by, cy);
        const bounds_z1 = Math.max(az, bz, cz);

        const node = bvh.allocate_node();

        bvh.node_set_user_data(node, i);
        bvh.node_set_aabb_primitive(node,
            bounds_x0, bounds_y0, bounds_z0,
            bounds_x1, bounds_y1, bounds_z1
        );

        bvh.insert_leaf(node);
    }
}