// Motorgraph - Copyright (C) 2010-2013 T. Mouton
//
// See the LICENSE.txt file for license information. Please report all
// bugs and problems to <thibaud.mouton@gmail.com>.

#include "motorgraph.h"
#include <cstdlib>
#include <time.h>

int main ( int argc, char** argv )
{
  char filename[100];
  int error = 0;

  sprintf ( filename, "%s.dat", argv[1] );
  printf ( "Reading %s ....\n", filename );
  FILE *fp = fopen ( filename, "r" );
  int nb_vx;
  if ( fscanf ( fp, "%d", &nb_vx ) != 1 )
    error++;
  printf ( "--> %d vertices\n", nb_vx );

  using namespace motorgraph;

  Graph *mg = new Graph;
  int count = 0;

  for ( int i = 0; i < nb_vx; i++ )
  {
    double p[3];
    int index;
    if ( fscanf ( fp, "%d %lf %lf %lf", &index, &p[0], &p[1], &p[2] ) != 4 )
      error++;

    double angle = drand48()*2.0*M_PI;    
    
    for ( int j = 0; j < 4; j++, angle += ( M_PI/2.0 ) )
    {
      double dir[2];
      dir[0] = cos ( angle );
      dir[1] = sin ( angle );

      Motorcycle *mc = new Motorcycle ( p, dir );
      mc->set_index ( count++ );
      mc->set_team ( i );
      mg->add_motorcycle ( mc );
    }
  }
  fclose ( fp );

  mg->init();
  mg->run();
  mg->export_vtk ( "motorgraph" );

  srand ( time ( NULL ) );

//     feenableexcept(FE_UNDERFLOW | FE_DIVBYZERO);

}
