bool Cgrid_to_cgrid_copier::copy( const Geostat_grid* server, 
                                  const Grid_continuous_property* server_prop,
                                  Geostat_grid* client, 
                                  Grid_continuous_property* client_prop ) {
  Cartesian_grid* to_grid = dynamic_cast< Cartesian_grid* >( client );
  const Cartesian_grid* from_grid = dynamic_cast< const Cartesian_grid* >( server );

  if( !from_grid || !to_grid ) return false;

  copy_categorical_definition(server_prop,client_prop);

  // if the 2 grids are identical, just copy the property
  if( are_identical_grids( from_grid, to_grid ) ) {
    
    appli_assert( server_prop->size() == client_prop->size() );
    for(int nodeid=0; nodeid<from_grid->size();++nodeid) {
      if( server_prop->is_informed(nodeid) ) {
      
		    //client_prop->set_value( server_prop->get_value( i ), i );
        client_prop->set_value( server_prop->get_value(nodeid), nodeid );
		    if( mark_as_hard_ )   //added by Yongshe
                client_prop->set_harddata( true, nodeid); //added by Yongshe
		  }
	    else if(overwrite_)
		    client_prop->set_not_informed( nodeid );
    }
    return true;
  }
  

  typedef Grid_continuous_property::property_type Property_type;

  // 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;

 
  // 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 = *( to_grid->cursor() );
  cursor.set_multigrid_level( 1 );

  GsTL_cube bbox = to_grid->bounding_box();

  // 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 Cartesian_grid::location_type location_type;
  typedef std::map<GsTLInt,location_type>::iterator map_iterator;
  std::map<GsTLInt,location_type> already_assigned;

  for(int server_id = 0 ; server_id < from_grid->size(); ++server_id ) {

    if( !server_prop->is_informed(server_id) ) continue;

    // only consider the points inside the target's bounding box
    location_type current_loc = from_grid->location(server_id);
    if( !bbox.contains( current_loc) ) continue;


    GsTLInt node_id = to_grid->closest_node( current_loc );
    appli_assert( node_id >=0 && node_id < client_prop->size() );
    appli_assert( server_id < server_prop->size() );
    if(  !to_grid->is_inside_selected_region(node_id) ) continue;
            
    // 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 = to_grid->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, current_loc ) > 
          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( server_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(server_id);
    client_prop->set_value( val, node_id );
    already_assigned[node_id] = current_loc;
    if( mark_as_hard_ )
      client_prop->set_harddata( true, node_id );
    
  }
  
  unset_harddata_flag_ = mark_as_hard_;
  return true;
}
bool Pset_to_structured_grid_copier::copy( const Geostat_grid* server, 
                     const Grid_continuous_property* server_prop,
                     Geostat_grid* client, 
                     Grid_continuous_property* client_prop ) {

  Structured_grid* to_grid = dynamic_cast< Structured_grid* >( client );
  const Point_set* from_grid = dynamic_cast< const Point_set* >( server );
  client_property_ = client_prop;
  index_client_to_source_.clear();
  original_client_value_.clear();

  int nx = to_grid->nx();
  int ny = to_grid->ny();
  int nz = to_grid->nz();
  GsTLPoint o = to_grid->origin();
  int xy = nx*ny;

  if( !from_grid || !to_grid ) return false;

  copy_categorical_definition(server_prop,client_prop);

  // 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 Geostat_grid::location_type location_type;
  typedef std::map<GsTLInt,location_type>::iterator map_iterator;
  std::map<GsTLInt,location_type> already_assigned;

  // Copy the property
   for( int i=0; i < server_prop->size() ; i++ ) {
     location_type pset_loc = to_grid->coordinate_mapper()->uvw_coords(from_grid->location(i));
	   //pset_loc = from_grid->location(i);
     int id = to_grid->closest_node(pset_loc);
     if(id < 0 ) continue;

     std::map<GsTLInt,location_type>::iterator it = already_assigned.find(id);
     if( it != already_assigned.end() ) {
       
       location_type to_grid_loc = to_grid->location(id);

     	  // 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( pset_loc, to_grid_loc) > 
          square_euclidean_distance( it->second, to_grid_loc ) ) 
          continue; 
     }

     if( server_prop->is_informed(i ) ) {

      // Check if it the first time this client node is considered
      if(client_prop->is_informed(id) && it == already_assigned.end()) {
        original_client_value_[id] = client_prop->get_value(id);
      }


      client_prop->set_value( server_prop->get_value( i ), id );
      already_assigned[id] = pset_loc;

      if(mark_as_hard_) client_prop->set_harddata(true,id);

      index_client_to_source_[id] = i;
       

     }
    else if(overwrite_) {
		client_prop->set_not_informed( id );
    index_client_to_source_[id] = i;
    already_assigned[id] = pset_loc;
    }

   }

   return true;
}
Пример #3
0
 // g1 < g2 if the covariance between g1 and the center is higher
 bool operator() ( const Geovalue& g1, const Geovalue& g2 ) const  {
   return square_euclidean_distance( g1.location(), center_ ) < 
          square_euclidean_distance(g2.location(), center_ );
 }