from __future__ import absolute_import
import mcubes


def export_to_dae(filename, data, level=0):
    """
    Converts a dense annotation to a DAE, using Marching Cubes (PyMCubes).

    Arguments:
        filename (str): The filename to write out to
        data (numpy.ndarray): The dense annotation
        level (int): The level at which to run mcubes

    Returns:
        boolean success
    """

    if ".dae" not in filename:
        filename = filename + ".dae"

    vs, fs = mcubes.marching_cubes(cutout, level)
    mcubes.export_mesh(vs, fs, filename, "ndioexport")


def export_to_obj(filename, data, level=0):
    """
    Converts a dense annotation to a obj, using Marching Cubes (PyMCubes).

    Arguments:
        filename (str): The filename to write out to
        data (numpy.ndarray): The dense annotation
        level (int): The level at which to run mcubes

    Returns:
        boolean success
    """

    if ".obj" not in filename:
        filename = filename + ".obj"

    vs, fs = mcubes.marching_cubes(cutout, level)
    mcubes.export_obj(vs, fs, filename)


def export_to_ply(filename, data, level=0):
    """
    Converts a dense annotation to a .PLY, using Marching Cubes (PyMCubes).

    Arguments:
        filename (str): The filename to write out to
        data (numpy.ndarray): The dense annotation
        level (int): The level at which to run mcubes

    Returns:
        boolean success
    """

    if ".ply" not in filename:
        filename = filename + ".ply"

    vs, fs = mcubes.marching_cubes(cutout, level)

    with open(filename, 'w') as fh:
        lines = [
            "ply"
            "format ascii 1.0",
            "comment generated by ndio",
            "element vertex " + str(len(vs)),
            "property float32 x",
            "property float32 y",
            "property float32 z",
            "element face " + str(len(fs)),
            "property list uint8 int32 vertex_index",
            "end_header"
        ]
        fh.writelines(lines)
        for v in vs:
            fh.write("{} {} {}".format(v[0], v[1], v[2]))
        for f in fs:
            fh.write("3 {} {} {}".format(f[0], f[1], f[2]))
