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 Pset_to_cgrid_copier::copy( const Geostat_grid* server, const Grid_continuous_property* server_prop, Geostat_grid* client, Grid_continuous_property* client_prop ) { Cartesian_grid* cgrid = dynamic_cast< Cartesian_grid* >( client ); const Point_set* pset = dynamic_cast< const Point_set* >( server ); if( !cgrid || !pset ) return false; typedef Grid_continuous_property::property_type Property_type; copy_categorical_definition(server_prop,client_prop); // check if we already worked with "source" and "property_name" // If that's the case and we're not required to do the assignement // from scratch, use what we did last time. if( !from_scratch_ && server == server_ && server_prop == server_prop_ && client == client_ ) { for( unsigned int i = 0 ; i < last_assignement_.size() ; i++ ) { Property_type val = server_prop->get_value( last_assignement_[i].first ); client_prop->set_value( val, last_assignement_[i].second ); if( mark_as_hard_ ) client_prop->set_harddata( true, last_assignement_[i].second ); } return true; } last_assignement_.clear(); backup_.clear(); server_ = server; server_prop_ = server_prop; client_ = client; client_property_ = client_prop; typedef Point_set::location_type location_type; typedef std::vector<location_type> Location_vector; typedef std::vector<location_type>::const_iterator const_iterator; // We will need to obtain the coordinates of a grid node from its // node id. Hence we need a grid-cursor, set to multigrid level 1. SGrid_cursor cursor = *( cgrid->cursor() ); cursor.set_multigrid_level( 1 ); const Location_vector& locations = pset->point_locations(); GsTL_cube bbox = cgrid->bounding_box(); GsTLInt current_id = 0; // Use a map to record what point was assigned to which grid node // This map is used in case multiple points could be assigned to the // same grid node: in that case the new point is assigned if it is closer // to the grid node than the previously assigned node was. typedef std::map<GsTLInt,location_type>::iterator map_iterator; std::map<GsTLInt,location_type> already_assigned; overwrite_ = true; // added by Jianbing Wu, 03/20/2006 for( const_iterator loc_ptr = locations.begin(); loc_ptr != locations.end(); ++loc_ptr, current_id++ ) { if( !server_prop->is_informed( current_id ) ) continue; // only consider the points inside the target's bounding box if( !bbox.contains( *loc_ptr ) ) continue; GsTLInt node_id = cgrid->closest_node( *loc_ptr ); appli_assert( node_id >=0 && node_id < client_prop->size() ); appli_assert( current_id < server_prop->size() ); // If there is already a property value (not assigned by the // grid initializer), and we don't want to overwrite, leave it alone if( !overwrite_ && client_prop->is_informed( node_id ) ) continue; // bool perform_assignment = true; // check if a point was already assigned to that node map_iterator it = already_assigned.find( node_id ); if( it != already_assigned.end() ) { int i,j,k; GsTLCoordVector sizes = cgrid->cell_dimensions(); cursor.coords( node_id, i,j,k ); location_type node_loc( float(i)*sizes.x(), float(j)*sizes.y(), float(k)*sizes.z() ); // if the new point is further away to the grid node than // the already assigned node, don't assign the new point if( square_euclidean_distance( node_loc, *loc_ptr ) > square_euclidean_distance( node_loc, it->second ) ) continue; //perform_assignment = false; } // if( perform_assignment ) { // store the node id of the source and of the target and make a backup of the // property value of the client property last_assignement_.push_back( std::make_pair( current_id, node_id ) ); if( !undo_enabled_ ) { Property_type backup_val = Grid_continuous_property::no_data_value; if( client_prop->is_informed( node_id ) ) backup_val = client_prop->get_value( node_id ); backup_.push_back( std::make_pair( node_id, backup_val ) ); } Property_type val = server_prop->get_value( current_id ); client_prop->set_value( val, node_id ); already_assigned[node_id] = *loc_ptr; if( mark_as_hard_ ) client_prop->set_harddata( true, node_id ); } overwrite_ = false; // added by Jianbing Wu, 03/20/2006 unset_harddata_flag_ = mark_as_hard_; return true; }
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; }