import { computeSkinnedMeshVertices } from "./computeSkinnedMeshVertices.js";
import { AABB3 } from "../../../../core/geom/3d/aabb/AABB3.js";
import Vector4 from "../../../../core/geom/Vector4.js";
import { computeBoundingSphereFromVertexData } from "../computeBoundingSphereFromVertexData.js";
import { Box3, Sphere as ThreeSphere, Vector3 as ThreeVector3 } from "three";
import { aabb3_from_v3_array } from "../../../../core/geom/3d/aabb/aabb3_from_v3_array.js";

/**
 *
 * @param {THREE.SkinnedMesh|SkinnedMesh} mesh
 */
export function computeSkinnedMeshBoundingVolumes(mesh) {
    const geometry = mesh.geometry;

    const aPosition = geometry.attributes.position;

    const vertices = new Float32Array(aPosition.count * aPosition.itemSize);

    computeSkinnedMeshVertices(vertices, mesh);

    const aabb3 = new AABB3(0, 0, 0, 0, 0, 0);
    const sphere = new Vector4();


    aabb3_from_v3_array(aabb3, vertices, vertices.length);
    computeBoundingSphereFromVertexData(vertices, sphere);

    const min = new ThreeVector3(aabb3.x0, aabb3.y0, aabb3.z0);
    const max = new ThreeVector3(aabb3.x1, aabb3.y1, aabb3.z1);

    geometry.boundingBox = new Box3(min, max);

    geometry.boundingSphere = new ThreeSphere(new ThreeVector3(sphere.x, sphere.y, sphere.z), sphere.w);
}
