Ejemplo n.º 1
0
bool KrigingTypeSelector_accessor::set_value( const std::string& str ) {
  QString qstr( str.c_str() );

  // str is just an element of an xml file, hence can not be parsed
  // by QDomDocument. We need to add a root element.
  qstr = "<root>" + qstr + "</root>";
  QDomDocument doc;
  bool parsed = doc.setContent( qstr );
  appli_assert( parsed );

  QDomElement root_element = doc.documentElement();
  QDomElement elem = root_element.firstChild().toElement();
  
  // Get the type of kriging
  QString val = elem.attribute( "type" );
  selector_->setKrigingType( val );

  QDomNodeList nodes = elem.elementsByTagName( "parameters" );


  if( selector_->krigingType().contains( "(SK)" ) ) {
    if( nodes.count() == 0 ) {
      appli_warning( "No element called \"parameters\"" << std::endl
		    << "Aborting" );
      return false;
    }
    QDomNode parameters_node = nodes.item(0);
    appli_assert( parameters_node.isElement() );
    QDomElement parameters_elem = parameters_node.toElement();
    val = parameters_elem.attribute( "mean" );
    selector_->setSkMean( val.toFloat() );
    return true;
  }

  
  if( selector_->krigingType().contains( "(KT)" ) ) {
    if( nodes.count() == 0 ) {
      appli_warning( "No element called \"parameters\"" << std::endl
		    << "Aborting" );
      return false;
    }
    QDomNode parameters_node = nodes.item(0);
    appli_assert( parameters_node.isElement() );
    QDomElement parameters_elem = parameters_node.toElement();
    val = parameters_elem.attribute( "trend" );
    
    // convert the string of 0 and 1 into a vector<bool>
    std::vector<bool> flags;
    std::istringstream input( val.latin1() );
    std::copy( std::istream_iterator<int>( input ), std::istream_iterator<int>(),
	       std::back_inserter( flags ) );
    selector_->setTrendComponents( flags );
    return true;
  }


  if( selector_->krigingType().contains( "(LVM)" ) ) {
    if( nodes.count() == 0 ) {
      appli_warning( "No element called \"parameters\"" << std::endl
		    << "Aborting" );
      return false;
    }
    QDomNode parameters_node = nodes.item(0);
    appli_assert( parameters_node.isElement() );
    QDomElement parameters_elem = parameters_node.toElement();
    QString grid = parameters_elem.attribute( "grid" );
    QString property = parameters_elem.attribute( "property" );
    selector_->setLocalMeanProperty( std::make_pair( grid, property ) );
    return true;
  }

  return true;
}
Ejemplo n.º 2
0
bool VariogramInput_accessor::set_value( const std::string& str ) {
  QString qstr( str.c_str() );
  
  // str is just an element of an xml file, hence can not be parsed
  // by QDomDocument. We need to add a root element.
  qstr = "<root>" + qstr + "</root>";
  QDomDocument doc;
  bool parsed = doc.setContent( qstr );
  appli_assert( parsed );

  QDomElement root_element = doc.documentElement();
  QDomElement elem = root_element.firstChild().toElement();
  
  // Get the nugget value and the number of structures
  QString val = elem.attribute( "nugget" );
  varg_input_->set_nugget( val.toFloat() );
  val = elem.attribute( "structures_count" );
  varg_input_->update_structures_count( val.toInt() );

  // work on each structure
  for( int i=0; i < varg_input_->structures_count() ; i++ ) {
    Variogram_structure_input* structure = varg_input_->structure( i );
    appli_assert( structure );
    
    // Get the node describing the structure and initialize the contribution
    // and variogram type values
    QString id;
    id.setNum( i+1 );
    QString structure_tagname = "structure_" + id;
    QDomNodeList nodes = elem.elementsByTagName( structure_tagname );
    if( nodes.count() == 0 ) {
      appli_warning( "No element called \"" <<  structure_tagname << "\"" << std::endl
		    << "Aborting" );
      return false;
    }
    QDomNode structure_node = nodes.item(0);
    appli_assert( structure_node.isElement() );
    QDomElement structure_elem = structure_node.toElement();

    val = structure_elem.attribute( "contribution" );
    structure->set_contribution( val.toFloat() );
    structure->set_variogram_type( structure_elem.attribute( "type" ) );


    // Get the ranges
    QDomNodeList ranges_node_list = structure_elem.elementsByTagName( "ranges" );
    if( ranges_node_list.count() == 0 ) {
      appli_warning( "No element called \"ranges\"" << std::endl
		    << "Aborting" );
      return false;
    }
    QDomNode ranges_node = ranges_node_list.item(0);
    appli_assert( ranges_node.isElement() );
    QDomElement ranges_elem = ranges_node.toElement();
    
    val = ranges_elem.attribute( "max" );
    structure->max_range( val.toFloat() );
    val = ranges_elem.attribute( "medium" );
    structure->medium_range( val.toFloat() );
    val = ranges_elem.attribute( "min" );
    structure->min_range( val.toFloat() );

    
    // Get the angles
    QDomNodeList angles_node_list = structure_elem.elementsByTagName( "angles" );
    if( angles_node_list.count() == 0 ) {
      appli_warning( "No element called \"angles\"" << std::endl
		    << "Aborting" );
      return false;
    }
    QDomNode angles_node = angles_node_list.item(0);
    appli_assert( angles_node.isElement() );
    QDomElement angles_elem = angles_node.toElement();
    
    val = angles_elem.attribute( "x" );
    structure->x_angle( val.toFloat() );
    val = angles_elem.attribute( "y" );
    structure->y_angle( val.toFloat() );
    val = angles_elem.attribute( "z" );
    structure->z_angle( val.toFloat() );

  }

  return true;
}
Ejemplo n.º 3
0
void Snapshot_dialog::take_snapshot( const QString& filename, const QString& f ) {
  if( filename.isEmpty() ) return;

  QApplication::setOverrideCursor( Qt::waitCursor );

  // Create an offscreen renderer which copies the settings from the Viewer
  SoOffscreenRenderer *snapshot = 
    new SoOffscreenRenderer( render_area_->getViewportRegion() );
  //snapshot->setTransparencyType(SoGLRenderAction::BLEND);
  snapshot->setBackgroundColor( render_area_->getBackgroundColor() );
  snapshot->setComponents( SoOffscreenRenderer::RGB );

  // Create the buffer in snapshot
  if ( !snapshot->render( render_area_->getSceneManager()->getSceneGraph()) ) {
    appli_warning("Snapshot: Failed to render the scene.");
    QApplication::restoreOverrideCursor();
    return;
  }
  
  QString format = f;

  // Write the buffer to file
    
  if( format.contains( "postscript", false ) || format.contains( "PS", false ) ) {
    FILE *fp = fopen( filename, "w");
    if( !fp ) {
      GsTLcerr << "Can't create file " << filename.ascii() << "\n" << gstlIO::end; 
      QApplication::restoreOverrideCursor();
      return;
    }
    snapshot->writeToPostScript(fp);
    fclose(fp);
  }


  if( format.contains( "PPM", false ) || format.contains( "PNG", false ) ||
      format.contains( "BMP", false ) ) {
//    fclose(fp);
//    std::ofstream ofile( filename.latin1() );

    format = format.section( " (", 0, 0 );
    SbVec2s size = 
      render_area_->getGLRenderAction()->getViewportRegion().getViewportSizePixels();
    int x = size[0];
    int y = size[1];
 
//    write_to_ppm_format( ofile, snapshot->getBuffer(), x, y );
 
    QImage qimage( x, y, 32 );
    uchar* image = snapshot->getBuffer();
    
    //for( int j=y-1 ; j>=0 ; j-- ) {
    for( int i = 0 ; i < x ; i++ ) {
      for( int j = 0 ; j < y ; j++ ) {
        int r,g,b;
        r= image[3*(j*x+i)];
        g= image[3*(j*x+i)+1];
        b= image[3*(j*x+i)+2];
        uint *p = (uint *)qimage.scanLine(y-(j+1)) + i;
        *p = qRgb(r,g,b);   
      }
    }
    qimage.save( filename, format );

  }

//  QSound::play( "shutter.wav" );

  delete snapshot;

  QApplication::restoreOverrideCursor();
}
void Lib_initializer::load_filters_plugins()
{
    SmartPtr<Named_interface> ni = Root::instance()->interface(topLevelInputFilters_manager);
    Manager* mng = dynamic_cast<Manager*> (ni.raw_ptr());
    appli_assert( mng );

    std::string filters_plugin_path(mng->plugin_path());
    QString path(filters_plugin_path.c_str());

    // Loop on all the library files (.so or .dll) in directory "path"
    QDir dir(path);
    if (!dir.exists())
        return;

    QStringList filters;
    filters << "*.so" << "*.dll";
    dir.setNameFilters(filters);
    dir.setFilter(QDir::Files);
    const QFileInfoList list = dir.entryInfoList();

    if (list.empty())
    {
        GsTLlog << "No filter plugins found.\n" << gstlIO::end;
        return;
    }

    QFileInfoList::const_iterator it = list.begin();
    const QFileInfo* f_info;

    for (; it != list.end(); ++it)
    {
        f_info = &(*it);
        // QLibrary wants the absolute path
        QString tmp = path + "/" + f_info->fileName();
        QByteArray tmps = tmp.toLatin1();
        appli_message( "loading file: " << tmps.constData());
        QLibrary lib(path + "/" + f_info->fileName());
        //lib.setAutoUnload( false );
        lib.load();
        if (!lib.isLoaded())
        {
            appli_warning( "library not loaded " << std::endl );
            continue;
        }

        typedef int (*Init_func_prototype)(void);

        // The function must be called [filename]_init()
        // QString init_func_name( f_info->baseName() + "_init" );

        // The function must be called plugin_init()
        QString init_func_name = "plugin_init";
        QByteArray tmp1 = init_func_name.toLatin1();
        Init_func_prototype init_func = (Init_func_prototype) lib.resolve(tmp1.constData());

        if (init_func)
            init_func();
        else
        {
            QByteArray s = init_func_name.toLatin1();
            appli_warning( "unable to resolve symbol " << s.constData() );
        }

    }

}
Ejemplo n.º 5
0
bool initialize( Kriging_type ktype,
                 KrigingCombiner*& Kcombiner,
                 KrigingConstraints*& Kconstraints,
                 KrigTagMap& tags_map,
                 const Parameters_handler* parameters,
                 Error_messages_handler* errors,
                 Geostat_grid* simulation_grid,
                 KrigDefaultsMap defaults ) {

  typedef Kriging_constraints_impl<Neighborhood, Location> KrigingConstraintsImpl;
  typedef Kriging_combiner_impl<WeightIterator, Neighborhood> KCombinerImpl;

  switch( ktype ) {
  case geostat_utils::SK :
    {
      appli_message( "doing SK" );

      double skmean;

      KrigDefaultsMap::const_iterator defaults_it = 
        defaults.find( geostat_utils::SK );
      if( defaults_it != defaults.end() ) {
        skmean = String_Op::to_number<double>( defaults_it->second );
      }
      else {
        // Retrieve the SK mean:
        std::string sk_tag = tags_map[SK];
        std::string skmean_str = parameters->value( sk_tag );
        if( skmean_str.empty() ) {
          errors->report( sk_tag, "No SK mean supplied" );
          return false;
        }

        skmean = String_Op::to_number<double>( skmean_str );
      }

      KrigingConstraintsImpl* constraints = 
        new SKConstraints_impl<Neighborhood, Location>;
      Kconstraints = new KrigingConstraints( constraints );

      KCombinerImpl* comb = 
        new SK_combiner<WeightIterator, Neighborhood>( skmean );
      Kcombiner = new KrigingCombiner( comb );

      delete comb;
      delete constraints;
      return true;
    }

  
  case geostat_utils::OK :
    {
      appli_message( "doing OK " );

      KrigingConstraintsImpl* constraints = 
        new OKConstraints_impl<Neighborhood, Location>;
      Kconstraints = new KrigingConstraints( constraints );

      KCombinerImpl* comb = new KCombinerImpl;
      Kcombiner = new KrigingCombiner( comb );

      delete comb;
      delete constraints;
      return true;
    }


  case geostat_utils::KT :
    {
      appli_message( "doing KT " );

      // Retrieve the trend components and store them into a vector<bool>
      std::vector<bool> flags;
      
      std::string kt_tag = tags_map[ geostat_utils::KT ];
      std::string trend = parameters->value( kt_tag );
      if( trend.empty() ) {
        errors->report( kt_tag, "No trend components supplied" );
        return false;
      }

      // convert the trend string into a vector of bool
      std::istringstream input( trend.c_str() );
      std::copy( std::istream_iterator<bool>( input ), std::istream_iterator<bool>(),
	               std::back_inserter( flags ) );

      std::vector<Trend_functor> trend_components;
      trend_components.push_back( Trend_functor( Trend_functions::id ) );
      if( flags[0] ) trend_components.push_back( Trend_functor( Trend_functions::x ) );
      if( flags[1] ) trend_components.push_back( Trend_functor( Trend_functions::y ) );
      if( flags[2] ) trend_components.push_back( Trend_functor( Trend_functions::z ) );
      if( flags[3] ) trend_components.push_back( Trend_functor( Trend_functions::x2 ) );
      if( flags[4] ) trend_components.push_back( Trend_functor( Trend_functions::y2 ) );
      if( flags[5] ) trend_components.push_back( Trend_functor( Trend_functions::z2 ) );
      if( flags[6] ) trend_components.push_back( Trend_functor( Trend_functions::xy ) );
      if( flags[7] ) trend_components.push_back( Trend_functor( Trend_functions::xz ) );
      if( flags[8] ) trend_components.push_back( Trend_functor( Trend_functions::yz ) );

      appli_warning( "number of components: " << trend_components.size() );
      KrigingConstraintsImpl* constraints = 
        new KTConstraints_impl<Trend_functor,Neighborhood, Location>( trend_components );
      Kconstraints = new KrigingConstraints( constraints );

      KCombinerImpl* comb = new KCombinerImpl;
      Kcombiner = new KrigingCombiner( comb );

      delete comb;
      delete constraints;
      return true;
    }


  case geostat_utils::LVM :
    {
      appli_message( "doing LVM" );
      appli_assert( simulation_grid );

      std::string lvm_tag = tags_map[ geostat_utils::LVM ];
      std::string mean_prop = parameters->value( lvm_tag );
    
      if( simulation_grid->property( mean_prop ) == 0 ) {
        errors->report( String_Op::split_string( lvm_tag, "/" ).first, 
                        "No valid property specified" );
        return false;
      }

      typedef SK_local_mean_combiner<WeightIterator, Neighborhood,
                                     Colocated_neighborhood> LVM_combiner;

      Colocated_neighborhood* coloc_neigh = 
        dynamic_cast<Colocated_neighborhood*>( 
	        simulation_grid->colocated_neighborhood( mean_prop )
	      );

      KCombinerImpl* comb = new LVM_combiner( *coloc_neigh );
      Kcombiner = new KrigingCombiner( comb );
    
      KrigingConstraintsImpl* constraints = 
        new SKConstraints_impl<Neighborhood, Location>;
      Kconstraints = new KrigingConstraints( constraints );
    
      delete comb;
      delete constraints;
      return true;
    }
  
  } //end of switch


  // Why did we get here ????
  appli_warning( "No valid kriging type provided" );
  errors->report( "Kriging_Type", "No valid kriging type provided" );
  return false;
}