import {BaseSopOperation} from "./_Base";
import {Vector3 as Vector32} from "three/src/math/Vector3";
import {ObjectType} from "../../geometry/Constant";
import {InputCloneMode as InputCloneMode2} from "../../../engine/poly/InputCloneMode";
import {BufferAttribute as BufferAttribute2} from "three/src/core/BufferAttribute";
import {BufferGeometry as BufferGeometry2} from "three/src/core/BufferGeometry";
export class CenterSopOperation extends BaseSopOperation {
  constructor() {
    super(...arguments);
    this._geo_center = new Vector32();
  }
  static type() {
    return "center";
  }
  cook(input_contents, params) {
    const core_group = input_contents[0];
    const src_objects = core_group.objectsWithGeo();
    const positions = new Array(src_objects.length * 3);
    positions.fill(0);
    for (let i = 0; i < src_objects.length; i++) {
      const src_object = src_objects[i];
      const src_geometry = src_object.geometry;
      src_geometry.computeBoundingBox();
      if (src_geometry.boundingBox) {
        src_geometry.boundingBox?.getCenter(this._geo_center);
        src_object.updateMatrixWorld();
        this._geo_center.applyMatrix4(src_object.matrixWorld);
        this._geo_center.toArray(positions, i * 3);
      }
    }
    const geometry = new BufferGeometry2();
    geometry.setAttribute("position", new BufferAttribute2(new Float32Array(positions), 3));
    const object = this.create_object(geometry, ObjectType.POINTS);
    return this.create_core_group_from_objects([object]);
  }
}
CenterSopOperation.DEFAULT_PARAMS = {};
CenterSopOperation.INPUT_CLONED_STATE = InputCloneMode2.FROM_NODE;
