import { assert } from "../../../../core/assert.js";

/**
 * Assumes that the mesh is compacted
 * @param {AttributeDataTexture} vertices
 * @param {AttributeDataTexture} neighbours
 * @param {TetrahedralMesh} mesh
 */
export function tetrahedral_mesh_to_texture(
    vertices,
    neighbours,
    mesh
) {

    assert.equal(mesh.isCompacted, true, 'mesh.isCompacted');

    if (mesh.isCompacted !== true) {
        throw new Error(`mesh is not compacted`);
    }

    if (vertices.spec.itemSize !== 4) {
        throw new Error(`vertices texture spec must have itemSize of 4, instead was ${vertices.spec.itemSize}`);
    }

    if (neighbours.spec.itemSize !== 4) {
        throw new Error(`neighbours texture spec must have itemSize of 4, instead was ${neighbours.spec.itemSize}`);
    }

    const tetrahedron_count = mesh.count;

    vertices.resize(tetrahedron_count);
    neighbours.resize(tetrahedron_count);

    const vertices_data = vertices.data;
    const neighbours_data = neighbours.data;

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

        const i4 = i * 4;

        for (let j = 0; j < 4; j++) {
            vertices_data[i4 + j] = mesh.getVertexIndex(i, j);

            neighbours_data[i4 + j] = mesh.getNeighbour(i, j) >>> 2;
        }

    }

    vertices.texture.needsUpdate = true;
    neighbours.texture.needsUpdate = true;
}