#include <Core/Grids_Uniform_Computations/MARCHING_CUBES.h>
#include <Core/Implicit_Objects/LEVELSET_IMPLICIT_OBJECT.h>
#include <Core/Log/LOG.h>
#include <Core/Meshes/TRIANGULATED_SURFACE.h>
#include <Core/Read_Write/FILE_UTILITIES.h>
#include <cstring>
#include <fstream>
using namespace PhysBAM;
using namespace std;
//#################################################################
// Function main
//#################################################################
int main(int argc,char *argv[])
{
    typedef float T;
    typedef VECTOR<T,3> TV;

    if(argc<3){
        LOG::cout<<"Usage ./phi2tri <phi input> <tri output>"<<std::endl;
        return 1;}
    
    GRID<VECTOR<T,3>> grid;
    ARRAY<T,VECTOR<int,3> > phi;
    LEVELSET_IMPLICIT_OBJECT<TV> implicit_surface(grid,at_cells,phi);
    Read_From_File(argv[1],implicit_surface);
    std::cout<<"Grid : "<<grid<<std::endl;

    TRIANGULATED_SURFACE<T> triangulated_surface;
    MARCHING_CUBES<TV>::Create_Surface(triangulated_surface,grid,at_cells,phi);
    
    std::cout<<"Surface triangles : "<<triangulated_surface.mesh.elements.m<<std::endl;
    std::cout<<"Surface particles : "<<triangulated_surface.particles.Size()<<std::endl;

    Write_To_File(STREAM_TYPE::type_float,argv[2],triangulated_surface);

    return 0;
}
//#################################################################
