Geostat_grid* Cartesian_grid_geometry_xml_io:: read_grid_geometry(QDir dir,const QDomElement& elem, std::string* errors) const { QString grid_name = elem.attribute("name"); QDomElement elemGeom = elem.firstChildElement("Geometry"); std::string final_grid_name; SmartPtr<Named_interface> ni = Root::instance()->new_interface( "cgrid://"+grid_name.toStdString(), gridModels_manager + "/" + grid_name.toStdString()); Cartesian_grid* grid = dynamic_cast<Cartesian_grid*>( ni.raw_ptr() ); if(grid == 0) return 0; int nx = elemGeom.attribute("nx").toInt(); int ny = elemGeom.attribute("ny").toInt(); int nz = elemGeom.attribute("nz").toInt(); double xsize = elemGeom.attribute("dx").toDouble(); double ysize = elemGeom.attribute("dy").toDouble(); double zsize = elemGeom.attribute("dz").toDouble(); double ox = elemGeom.attribute("ox").toDouble(); double oy = elemGeom.attribute("oy").toDouble(); double oz = elemGeom.attribute("oz").toDouble(); float rotation_z_angle = elemGeom.attribute("rotation_z_angle").toFloat(); grid->set_dimensions( nx, ny, nz, xsize, ysize, zsize ); grid->origin( Cartesian_grid::location_type( ox,oy,oz ) ); grid->set_rotation_z(rotation_z_angle); return grid; }
Geostat_grid* Simulacre_input_filter::read_cartesian_grid( QDataStream& stream, std::string* errors ) { char* name; stream >> name; std::string grid_name( name ); Q_INT32 version; stream >> version; if( version < 100 ) { errors->append( "file too old" ); return 0; } Q_UINT32 nx, ny, nz; stream >> nx >> ny >> nz; float xsize, ysize, zsize; float ox,oy,oz; stream >> xsize >> ysize >> zsize >> ox >> oy >> oz; std::string final_grid_name; SmartPtr<Named_interface> ni = Root::instance()->new_interface( "cgrid", gridModels_manager + "/" + grid_name, &final_grid_name ); Cartesian_grid* grid = dynamic_cast<Cartesian_grid*>( ni.raw_ptr() ); grid->set_dimensions( nx, ny, nz, xsize, ysize, zsize ); grid->origin( Cartesian_grid::location_type( ox,oy,oz ) ); Q_UINT32 properties_count; stream >> properties_count; std::vector< char* > prop_names( properties_count ); for( unsigned int i = 0; i < properties_count; i++ ) stream >> prop_names[i]; for( unsigned int j = 0; j < properties_count; j++ ) { std::string prop_name( prop_names[j] ); GsTLGridProperty* prop = grid->add_property( prop_name ); for( GsTLInt k = 0; k < nx*ny*nz ; k++ ) { float val; stream >> val; prop->set_value( val, k ); } } // clean up for( unsigned int k = 0; k < properties_count; k++ ) { delete [] prop_names[k]; } delete [] name; return grid; }
bool Create_mgrid_from_cgrid::exec(){ // Get the grid from the manager SmartPtr<Named_interface> ni = Root::instance()->interface( gridModels_manager + "/" + cgrid_name_ ); if( ni.raw_ptr() == 0 ) { errors_->report( "Object " + cgrid_name_ + " does not exist." ); return false; } Cartesian_grid* cgrid = dynamic_cast<Cartesian_grid*>( ni.raw_ptr() ); if( cgrid == 0) { errors_->report( "Object " + cgrid_name_ + " is not a cartesian grid." ); return false; } // Get the region std::vector<Grid_region*> regions; std::vector<std::string>::iterator it = region_names_.begin(); for( ; it!= region_names_.end(); ++it) { Grid_region* region = cgrid->region( *it ); if(!region) { errors_->report( "Region " + (*it) + " does not exist." ); return false; } regions.push_back(region); } // Create the new masked_grid ni = Root::instance()->new_interface("reduced_grid://"+mgrid_name_, gridModels_manager + "/" + mgrid_name_); if( ni.raw_ptr() == 0 ) { errors_->report( "Object " + mgrid_name_ + " already exists. Use a different name." ); return false; } Reduced_grid* grid = dynamic_cast<Reduced_grid*>( ni.raw_ptr() ); //Create the grid if( regions.size() == 1 ) { //avoid a copy of the region array grid->set_dimensions( cgrid->geometry()->dim(0), cgrid->geometry()->dim(1), cgrid->geometry()->dim(2), cgrid->cell_dimensions()[0], cgrid->cell_dimensions()[1], cgrid->cell_dimensions()[2], regions[0]->data(), cgrid->rotation_z()); } else { int mask_size = regions[0]->size(); std::vector<bool> mask( mask_size ); for( int i=0; i< mask_size; ++i ) { bool ok = false; for( int j = 0; j<regions.size(); j++ ) { ok == ok || regions[j]->is_inside_region(i); } mask[i] = ok; } grid->set_dimensions( cgrid->geometry()->dim(0), cgrid->geometry()->dim(1), cgrid->geometry()->dim(2), cgrid->cell_dimensions()[0], cgrid->cell_dimensions()[1], cgrid->cell_dimensions()[2], mask, cgrid->rotation_z()); } grid->origin( cgrid->origin() ); proj_->new_object( mgrid_name_ ); return true; }
int main(int argc, char **argv) { // Initialize Inventor and Qt QWidget *myWindow = SoQt::init(argv[0]); GsTL_SoNode::initClass(); if (myWindow == NULL) exit(1); SmartPtr<Named_interface> ni_cmaps = Root::instance()->new_interface("directory://colormaps", colormap_manager ); Manager* dir = dynamic_cast<Manager*>( ni_cmaps.raw_ptr() ); if( !dir ) { GsTLlog << "could not create directory " << colormap_manager << "\n"; return 1; } dir->factory( "colormap", Color_scale::create_new_interface ); Root::instance()->new_interface( "colormap://rainbow.map", colormap_manager + "/rainbow" ); Root::instance()->new_interface( "colormap://cyan_red.map", colormap_manager + "/cyan_red" ); Root::instance()->list_all( cout ); int nx=5; int ny=3; int nz=6; Cartesian_grid grid; grid.set_dimensions( nx, ny, nz, 1, 1, 1 ); grid.origin( GsTLPoint( -0.5, -0.5, -0.5 ) ); GsTLGridProperty* prop = grid.add_property( "toto", typeid( float ) ); for( int i=0; i < nx*ny*nz; i ++ ) prop->set_value( i, i ); Oinv_cgrid oinv_grid; oinv_grid.init( &grid ); oinv_grid.set_property( "toto" ); oinv_grid.oinv_node()->visible = true; cout << "Setting up oinv scene" << endl; SoSelection* root = new SoSelection; SoSeparator* line_sep = new SoSeparator; SoLightModel* light_model = new SoLightModel; light_model->model = SoLightModel::BASE_COLOR; line_sep->addChild( light_model ); SoMaterial* material = new SoMaterial; material->diffuseColor.setValue( 1.0, 0.0, 0.0 ); line_sep->addChild( material ); SoDrawStyle* draw_style = new SoDrawStyle; draw_style->style = SoDrawStyle::LINES; draw_style->lineWidth = 2; line_sep->addChild( draw_style ); oinv_grid.property_display_mode( Oinv::NOT_PAINTED ); line_sep->addChild( oinv_grid.oinv_node() ); root->addChild( line_sep ); SoSeparator* filled_sep = new SoSeparator; SoDrawStyle* draw_style_filled = new SoDrawStyle; draw_style_filled->style = SoDrawStyle::FILLED; filled_sep->addChild( draw_style_filled ); oinv_grid.property_display_mode( Oinv::PAINTED ); filled_sep->addChild( oinv_grid.oinv_node() ); root->addChild( filled_sep ); SoQtGsTLViewer* myViewer = new SoQtGsTLViewer( (QWidget*) myWindow, "camera" ); //SoQtExaminerViewer *myViewer = new SoQtExaminerViewer(myWindow); myViewer->setSceneGraph(root); myViewer->show(); SoQt::show(myWindow); SoQt::mainLoop(); }
Geostat_grid* Csv_grid_infilter::read( std::ifstream& infile ) { int nx = dialog_->nx(); int ny = dialog_->ny(); int nz = dialog_->nz(); float x_size = dialog_->x_size(); float y_size = dialog_->y_size(); float z_size = dialog_->z_size(); float Ox = dialog_->Ox(); float Oy = dialog_->Oy(); float Oz = dialog_->Oz(); bool use_no_data_value = dialog_->use_no_data_value(); float no_data_value = GsTLGridProperty::no_data_value; QString no_data_value_str = QString().arg(no_data_value); if( dialog_->use_no_data_value() ) { no_data_value = dialog_->no_data_value(); no_data_value_str = QString::number(no_data_value); } QByteArray tmp = dialog_->name().simplified().toLatin1(); std::string name( tmp.constData() ); // ask manager to get a new grid and initialize it SmartPtr<Named_interface> ni = Root::instance()->interface( gridModels_manager + "/" + name ); if( ni.raw_ptr() != 0 ) { GsTLcerr << "object " << name << " already exists\n" << gstlIO::end; return 0; } appli_message( "creating new grid '" << name << "'" << " of dimensions: " << nx << "x" << ny << "x" << nz); ni = Root::instance()->new_interface( "cgrid", gridModels_manager + "/" + name ); Cartesian_grid* grid = dynamic_cast<Cartesian_grid*>( ni.raw_ptr() ); appli_assert( grid != 0 ); grid->set_dimensions( nx, ny, nz, x_size, y_size, z_size); grid->origin( GsTLPoint( Ox,Oy,Oz) ); appli_message( "grid resized to " << nx << "x" << ny << "x" << nz << " total=: " << grid->size() ); std::string buffer; //------------------------- // now, read the file std::getline( infile, buffer, '\n'); QStringList property_names = QString(buffer.c_str()).split(","); //Read one column at a time std::streampos start_data = infile.tellg(); for(unsigned int j = 0; j< property_names.size(); j++) { infile.clear(); infile.seekg( start_data ); // Check if property j is categorical bool is_categ = false; for(unsigned int i=0; i<30 ; i++ ) { bool ok; if( std::getline(infile, buffer) ) break; QString qstr(buffer.c_str()); QStringList values_str = qstr.split(","); values_str[j].toFloat(&ok); if(!ok) { is_categ = true; break; } } infile.clear(); infile.seekg( start_data ); if(is_categ) { GsTLGridCategoricalProperty* prop = grid->add_categorical_property(property_names[j].toStdString()); ni = Root::instance()->new_interface( categoricalDefinition_manager, name+"-"+property_names[j].toStdString()); CategoricalPropertyDefinitionName* cat_def = dynamic_cast<CategoricalPropertyDefinitionName*>(ni.raw_ptr()); while( std::getline(infile, buffer) ) { QString qstr(buffer.c_str()); QStringList values_qstr = qstr.split(","); cat_def->add_category(values_qstr[j].toStdString()); } prop->set_category_definition(cat_def->name()); infile.clear(); infile.seekg( start_data ); int node_id=0; while( std::getline(infile, buffer) ) { QString qstr(buffer.c_str()); QStringList values_qstr = qstr.split(","); QString val = values_qstr[j]; if(use_no_data_value && val == no_data_value_str) { prop->set_value( GsTLGridProperty::no_data_value, node_id ); } else { prop->set_value( val.toStdString(), node_id ); } node_id++; } } else { GsTLGridProperty* prop = grid->add_property(property_names[j].toStdString()); int node_id=0; while( std::getline(infile, buffer) ) { QString qstr(buffer.c_str()); QStringList values_qstr = qstr.split(","); if(!values_qstr[j].isEmpty()) { bool ok; float val = values_qstr[j].toFloat(&ok); if(ok && val != no_data_value) prop->set_value(val,node_id); } node_id++; } } } return grid; }
Geostat_grid* Gslib_grid_infilter::read( std::ifstream& infile ) { int nx = dialog_->nx(); int ny = dialog_->ny(); int nz = dialog_->nz(); float x_size = dialog_->x_size(); float y_size = dialog_->y_size(); float z_size = dialog_->z_size(); float Ox = dialog_->Ox(); float Oy = dialog_->Oy(); float Oz = dialog_->Oz(); QByteArray tmp = dialog_->name().simplified().toLatin1(); std::string name( tmp.constData() ); // ask manager to get a new grid and initialize it SmartPtr<Named_interface> ni = Root::instance()->interface( gridModels_manager + "/" + name ); if( ni.raw_ptr() != 0 ) { GsTLcerr << "object " << name << " already exists\n" << gstlIO::end; return 0; } appli_message( "creating new grid '" << name << "'" << " of dimensions: " << nx << "x" << ny << "x" << nz); ni = Root::instance()->new_interface( "cgrid", gridModels_manager + "/" + name ); Cartesian_grid* grid = dynamic_cast<Cartesian_grid*>( ni.raw_ptr() ); appli_assert( grid != 0 ); grid->set_dimensions( nx, ny, nz, x_size, y_size, z_size); grid->origin( GsTLPoint( Ox,Oy,Oz) ); appli_message( "grid resized to " << nx << "x" << ny << "x" << nz << " total=: " << grid->size() ); std::string buffer; //------------------------- // now, read the file // read title std::getline( infile, buffer, '\n'); // read nb of properties int property_count; infile >> property_count; std::getline( infile, buffer, '\n'); // check whether the file contains multiple realizations bool has_multi_real = false; int lines_to_skip = grid->size() + property_count; for( int pos=0; pos < lines_to_skip ; pos++ ) std::getline( infile, buffer, '\n'); float val; if( infile >> val ) has_multi_real = true; // reposition the stream infile.clear(); infile.seekg( 0 ); std::getline( infile, buffer, '\n'); std::getline( infile, buffer, '\n'); if( has_multi_real ) { std::vector<MultiRealization_property*> properties; for( int i=0; i<property_count; i++ ) { std::getline( infile, buffer, '\n'); QString prop_name( buffer.c_str() ); MultiRealization_property* prop; QByteArray tmp = prop_name.simplified().toAscii(); prop = grid->add_multi_realization_property( tmp.constData() ); if( !prop ) { GsTLcerr << "Several properties share the same name " << gstlIO::end; return 0; } properties.push_back( prop ); } while( infile ) { if( !infile ) break; char c = infile.peek(); if( !std::isdigit(c) ) break; std::vector<GsTLGridProperty*> props; int index = 0; for( unsigned int ii=0; ii < property_count; ii++) { GsTLGridProperty* prop = properties[index]->new_realization(); props.push_back( prop ); ++index; } read_one_realization( infile, props, grid->size() ); } } else { std::vector<GsTLGridProperty*> properties; for( int i=0; i<property_count; i++ ) { std::getline( infile, buffer, '\n'); QString prop_name( buffer.c_str() ); QByteArray tmp =prop_name.simplified().toAscii(); GsTLGridProperty* prop = grid->add_property( tmp.constData() ); properties.push_back( prop ); } read_one_realization( infile, properties, grid->size() ); } return grid; }