Exemplo n.º 1
0
/**
 * \fn void read_input( const string& fn , size_t& np , size_t& nc , bool& closed , double*& lx , double*& ly , double*& ux , double*& uy ) throw( ExceptionObject ) 
 *
 * \brief Read in a file describing a polygonal channel.
 *
 * \param fn The name of a file describing a polygonal channel.
 * \param  np A  reference  to  the number  of  b-spline segments.
 * \param nc A reference to the number of c-segments of the channel.
 * \param closed A reference to a flag to indicate whether the channel
 * is closed.
 * \param  lx  A  reference  to  a   pointer  to  an  array  with  the
 * x-coordinates of the lower polygonal chain of the channel.
 * \param  ly  A  reference  to  a   pointer  to  an  array  with  the
 * y-coordinates of the lower polygonal chain of the channel.
 * \param  ux  A  reference  to  a   pointer  to  an  array  with  the
 * x-coordinates of the upper polygonal chain of the channel.
 * \param  uy  A  reference  to  a   pointer  to  an  array  with  the
 * y-coordinates of the upper polygonal chain of the channel.
 *
 */
void read_input(
                const string& fn ,
                size_t& np ,
                size_t& nc ,
                bool& closed ,
                double*& lx ,
                double*& ly ,
                double*& ux ,
                double*& uy
               )
  throw( ExceptionObject ) 
{
  //
  // Open the input file
  //
  std::ifstream in( fn.c_str() ) ;
  
  if ( in.is_open() ) {
    //
    // Read in the number of segments of the b-spline.
    //
    in >> np ;
    
    //
    // Read in the number of c-segments of the channel.
    //
    in >> nc ;
    
    //
    // Read in the flag indicating whether the channel is closed.
    //
    unsigned flag ;
    in >> flag ;
    
    if ( ( flag != 0 ) && ( flag != 1 ) ) {
      std::stringstream ss( std::stringstream::in | std::stringstream::out ) ;
      ss << "Flag value indicating whether the channel is closed or open is invalid" ;
      in.close() ;
      throw ExceptionObject( __FILE__ , __LINE__ , ss.str().c_str() ) ;
    }
      
    closed = ( flag == 1 ) ;
      
    if ( closed ) {
      if ( np < 4 ) {
        std::stringstream ss( std::stringstream::in | std::stringstream::out ) ;
        ss << "The number of curve segments must be at least 4 for a closed curve" ;
        in.close() ;
        throw ExceptionObject( __FILE__ , __LINE__ , ss.str().c_str() ) ;
      }
      if ( nc < 3 ) {
        std::stringstream ss( std::stringstream::in | std::stringstream::out ) ;
        ss << "The number of segments of a closed channel must be at least 3" ;
        in.close() ;
        throw ExceptionObject( __FILE__ , __LINE__ , ss.str().c_str() ) ;
      }
    }
    else {
      if ( np < 1 ) {
        std::stringstream ss( std::stringstream::in | std::stringstream::out ) ;
        ss << "The number of curve segments must be at least 1" ;
        in.close() ;
        throw ExceptionObject( __FILE__ , __LINE__ , ss.str().c_str() ) ;
      }
      if ( nc < 1 ) {
        std::stringstream ss( std::stringstream::in | std::stringstream::out ) ;
        ss << "The number of segments of an open channel must be at least 1" ;
        in.close() ;
        throw ExceptionObject( __FILE__ , __LINE__ , ss.str().c_str() ) ;
      }
    }
      
    //
    // Read in the channel vertex coordinates.
    //
    const size_t nn = ( closed ) ? nc : ( nc + 1 ) ;
      
    lx = new double[ nn ] ;
    ly = new double[ nn ] ;
      
    for ( size_t i = 0 ; i < nn ; i++ ) {
      //
      // Read in the X and Y coordinates of the i-th vertex.
      //
      in >> lx[ i ] ;
      in >> ly[ i ] ;
    }
      
    ux = new double[ nn ] ;
    uy = new double[ nn ] ;
      
    for ( size_t i = 0 ; i < nn ; i++ ) {
      //
      // Read in the X and Y coordinates of the i-th vertex.
      //
      in >> ux[ i ] ;
      in >> uy[ i ] ;
    }
      
    //
    // Close file
    //
      
    in.close() ;
  }