示例#1
0
/**
 * \fn int main( int argc , char* argv[]  )
 *
 * \brief A simple program for testing the bc2d library.
 *
 * \param argc The number of command-line arguments.
 * \param argv An array with the command-line arguments.
 *
 * \return An integer number.
 */
int main( int argc , char* argv[] ) {
  //
  // Check command-line arguments.
  //
  
  if ( ( argc != 3 ) && ( argc != 4 ) ) {
    cerr << "Usage: "
         << endl
         << "\t\t channel2d arg1 arg2 [ arg3 ]"
         << endl
         << "\t\t arg1: name of the file describing the polygonal channel"
         << endl
         << "\t\t arg2: name of the output file describing the computed cubic b-spline curve"
         << endl
         << "\t\t arg3: name of an output file to store a CPLEX format definition of the LP solved by this program (OPTIONAL)"
         << endl
         << endl ;
    cerr.flush() ;
    
    return EXIT_FAILURE ;
  }
  
  //
  // Read in the input file.
  //
  
  clock_t start, end ;
  
  cerr << endl
       << "Reading file describing a polygonal channel..."
       << endl ;
  cerr.flush() ;
  
  string fn1( argv[ 1 ] ) ;

  size_t np ;
  size_t nc ;
  bool closed ;
  double* lx ;
  double* ly ;
  double* ux ;
  double* uy ;

  start = clock() ;
  try {
    read_input( fn1 , np , nc , closed , lx , ly , ux , uy ) ;
  }
  catch ( const ExceptionObject& xpt ) {
    treat_exception( xpt ) ;
    exit( EXIT_FAILURE ) ;
  }  
  end = clock() ;
  
  cerr << ( (double) ( end - start ) ) / CLOCKS_PER_SEC
       << " seconds."
       << endl
       << endl ;
  cerr.flush() ;
  
  //
  // Compute a cubic b-spline curve that passes through the channel.
  //
  
  cerr << "Computing a cubic b-spline curve that passes through the channel... "
       << endl ;
  cerr.flush() ;
  
  start = clock() ;
  CurveBuilder* builder = 0 ;
  try {
    builder = new CurveBuilder(
                               np ,
                               nc ,
                               closed ,
                               &lx[ 0 ] ,
                               &ly[ 0 ] ,
                               &ux[ 0 ] ,
                               &uy[ 0 ] 
                              ) ;
  }
  catch ( const ExceptionObject& xpt ) {
    treat_exception( xpt ) ;
    exit( EXIT_FAILURE ) ;
  }
  
  int error ;
  bool res = builder->build( error ) ;
  end = clock() ;
  
  cerr << ( (double) ( end - start ) ) / CLOCKS_PER_SEC
       << " seconds."
       << endl
       << endl ;
  cerr.flush() ;

  if ( res ) {
    //
    // Write the control points of the b-spline to a file.
    //
    cerr << "Writing out the control points of the b-spline to a file..."
	       << endl ;
    cerr.flush() ;

    start = clock() ;
    string fn2( argv[ 2 ] ) ;
    write_solution(
                   fn2 ,
                   *builder
                  ) ;
    end = clock() ;
  
    cerr << ( (double) ( end - start ) ) / CLOCKS_PER_SEC
         << " seconds."
         << endl
         << endl ;
    cerr.flush() ;
  }
  else {
    //
    // Print the error message returned by the LP solver.
    //
    cerr << endl
         << "ATTENTION: "
         << endl
         << builder->get_solver_error_message( error )
         << endl
         << endl ;
  }

  //
  // Generate a description of the linear program in CPLEX format.
  //
  if ( argc == 4 ) {
    cerr << "Writing out a description of the linear program in CPLEX format..."
	       << endl ;
    cerr.flush() ;
  
    start = clock() ;  
    string fn3( argv[ 3 ] ) ;
    write_lp(
             fn3 ,
             *builder
            ) ;
    end = clock() ;
  
    cerr << ( (double) ( end - start ) ) / CLOCKS_PER_SEC
         << " seconds."
         << endl
         << endl ;
    cerr.flush() ;
  }
  
  //
  // Release memory
  //
  
  cerr << "Releasing memory..."
       << endl ;
  cerr.flush() ;
  
  start = clock() ;
  if ( lx != 0 ) delete[ ] lx ;
  if ( ly != 0 ) delete[ ] ly ;
  if ( ux != 0 ) delete[ ] ux ;
  if ( uy != 0 ) delete[ ] uy ;
  if ( builder != 0 ) delete builder ;
  end = clock() ;
  
  cerr << ( (double) ( end - start ) ) / CLOCKS_PER_SEC
  << " seconds."
  << endl
  << endl ;
  cerr.flush() ;
  
  //
  // Done.
  //
  
  cerr << "Finished."
       << endl
       << endl
       << endl ;
  cerr.flush() ;
  
  return EXIT_SUCCESS ;
}