/*
    C++ Mesh Generation Library
    Copyright (c) 2000echet <eric at bechet dot ca>

    This file is part of the C++ Mesh Generation Library.

    See the NOTICE & LICENSE files for conditions.
*/
#include <cmath>
#include <iostream>


#include "gmshNode.h"
#include "gmshElement.h"
#include "FastMarching.h"
#include "SpeedFunctor.h"
#include "ExportFunctor.h"
#include "SPoint3.h"
#include <cstring>
#include "GModel.h"
#include "GmshGlobal.h"
#include "SPointToDouble.h"
#include "FMField.h"

using namespace std;


bool Get_Options ( int narg, char  * arg[], char  * NameProblem=0, double *lc=0 )
{
  if (NameProblem)
  {
    strcpy ( NameProblem, "" ) ;

    if ( narg>1 )
    {
      strcpy ( NameProblem, arg[1] ) ;
    }
    else 
    {
      return false;
    }
  }
  if (lc)
  {
    if ( narg>2 )
      *lc=atof(arg[2]);
    else
      *lc=1.0;
  }
  return true ;
}


int main ( int argc, char *argv[] )
{
  char pname[256];
  Get_Options ( argc,argv,pname );
  GmshInitialize(argc, argv);
  FM::FMModel *Model=new FM::FMModel();
 
  Model->readMSH(pname);
  Model->buildAdj(1<<(Model->getDim()-1));// highest dimension only
 
  FMSpeedConstant sp;
  FastMarch_c fastM ( sp );

  map<FM::gmshNode,FastMarch_c::FieldPair> initvals;
  set<FM::gmshElement> bndinit;

  Init ( Model, SPlane (SPoint3( -0.05, 0.00, 0. ), SVector3 ( -1.,0.,0. ) ), SPlane ( SPoint3 ( -0.05, 0.00, 0 ), SVector3( 0.,1.,0. ) ),initvals,bndinit );

  fastM.Init ( initvals,bndinit );
  ExportMSH ef ( Model );
  fastM.SetExportFunctor ( ef );
  fastM.Export ( "ls" );

  fastM.Propagate ( 1.0 );
  SPoint3 pt(-0.20,0,0);
  FMField F(fastM);
  FastMarch_c::FieldPair f=F(pt);
  fastM.Export ( "ls2" );
  std::cout << "dist at pt" << "("<< pt.x() << "," << pt.y() << "," << pt.z() << ") = " << f.first << std::endl;
    if (fabs(f.first + pt.x()+0.05)<1e-6) return 0; else return 1;
}

 
