/// \file msh2geo.cpp
/// \brief Convert a msh file to a geo file.
/// \warning: For the moment only meshes wth tetrahedra are accepted.
///
/// Usage: ./msh2geo <mesh file name> <geo output file name>
///
/// Requires: libgmsh-dev ligcgal-dev (ver. >= 5.0)
/// \author Leblanc Christophe.
/// \date 18/11/2020
/// \mail cleblancad@gmail.com

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>

#include "gmsh.h"
#include "extract_surface_mesh_from_msh.hpp"

//
// Typedefs.
//
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef typename Kernel::Point_3  Point;
typedef CGAL::Surface_mesh<Point> Surface_mesh;
typedef ExtractSurfaceMeshFromMSH<Kernel, Surface_mesh> MSHExtractor;

int main(int argc, char** argv)
{
  gmsh::initialize(argc, argv);
  
  //
  // Extract surface mesh from MSH file (tetrahedron).
  //
  if(argc < 3)
  {
    std::cout << "Usage: \"./msh2geo <mesh file name> <output geo file name>\"\n";
    return EXIT_FAILURE;
  }
  
  const std::string mesh_filename = argv[1];
  const std::string geo_filename = argv[2];
  
  MSHExtractor extractor;
  extractor.set_mesh_file_name(mesh_filename);
  extractor.set_geo_file_name(geo_filename);
  extractor.set_mesh_size(1.0);
  extractor.extract();
  
  // Get the surface mesh.
  Surface_mesh mesh = extractor.get_surface_mesh();
  
  if(!mesh.is_valid())
  {
    std::cerr << "Invalid mesh found, please check your mesh..." << std::endl;
    return EXIT_FAILURE;
  }

  gmsh::finalize();
  return EXIT_SUCCESS;
}
