import pytest
import trimesh
import numpy as np

from signDNE.utils import compute_face2vertex

from signDNE.signDNE import prep
from signDNE.signDNE import signDNE
from signDNE.signDNE import make_watertight


def test_prep():
    mesh = trimesh.load("signDNE/data/normal.ply")
    prep(mesh)
    
    assert not mesh.is_empty
    assert np.all(np.isfinite(mesh.vertices))
    assert np.all(np.isfinite(mesh.faces))
    assert len(mesh.faces) > 0


def test_signDNE():
    """
    Tests against DNE values computed with the original MATLAB implementation 
    """
    mesh_names = ["normal", "low", "high", "noise1", "noise2", "smooth"]
    expected_dnes = np.loadtxt("signDNE/tests/MATLAB_dnes.txt", delimiter = ",")
    dnes = np.zeros(len(mesh_names))
    for i in range(len(mesh_names)):
        mesh = trimesh.load(f"signDNE/data/{mesh_names[i]}.ply")
        dne = signDNE(mesh)[2]
        dnes[i] = dne
    
    assert np.allclose(expected_dnes, dnes)


def test_make_watertight():
    mesh = trimesh.load("signDNE/data/normal.ply")
    watertight_mesh = make_watertight(mesh)
    
    assert watertight_mesh.is_watertight == True
