import { PLYLoader } from "three/examples/jsm/loaders/PLYLoader.js";
import {
    compute_buffer_geometry_byte_size
} from "../../../../../engine/graphics/geometry/buffered/compute_buffer_geometry_byte_size.js";

import { noop } from "../../../../function/noop.js";
import { number_pretty_print } from "../../../../primitives/numbers/number_pretty_print.js";
import { BinaryTopology } from "./binary/BinaryTopology.js";
import { bt_mesh_from_indexed_geometry } from "./binary/io/bt_mesh_from_indexed_geometry.js";
import { TopoMesh } from "./TopoMesh.js";

function promise_ply(url) {

    return new Promise((resolve, reject) => {
        new PLYLoader().load(url, resolve, noop, reject);
    });
}


async function main() {

    const url = "data/models/stanford/Lucy100k.ply";
    // const url = "data/models/stanford/dragon_recon/dragon_vrip.ply";
    const lucy_geom = await promise_ply(url);

    const mesh = new BinaryTopology();
    const topoMesh = new TopoMesh();

    const positions_array = lucy_geom.getAttribute('position').array;
    const index_array = lucy_geom.getIndex().array;



    console.time('topo build - bin');
    // console.profile('topo build - bin');
    bt_mesh_from_indexed_geometry(mesh, index_array, positions_array);
    // console.profileEnd('topo build - bin');
    console.timeEnd('topo build - bin');
    //
    // console.time('topo build - obj');
    // topoMesh.build(positions_array, index_array);
    // console.timeEnd('topo build - obj');

    mesh.trim();

    console.log(mesh, lucy_geom);

    console.log(`source mesh size: ${number_pretty_print(compute_buffer_geometry_byte_size(lucy_geom))} bytes`);
    console.log(`binary topology size: ${number_pretty_print(mesh.byteSize)} bytes`);
    console.log(`object topology size: ${number_pretty_print(topoMesh.byteSize)} bytes`);

    window.topo_obj = topoMesh;
}

main();

