MC_grid_index_spatial_hashing.cpp

Go to the documentation of this file.
00001 
00002 #include <MC_grid_index_spatial_hashing.hpp>
00003 
00004 namespace mesh_conv
00005 {
00006     MC_grid_index_spatial_hashing::MC_grid_index_spatial_hashing(){}
00007     MC_grid_index_spatial_hashing::MC_grid_index_spatial_hashing(const MC_int_vector& grid_size)
00008     {
00009         index_grid=MC_grid_3d<std::set<int> >(grid_size);
00010     }
00011     MC_int_vector MC_grid_index_spatial_hashing::size() const
00012     {
00013         return index_grid.size();
00014     }
00015 
00016     void MC_grid_index_spatial_hashing::add_index(const MC_int_vector& X,const int index)
00017     {
00018         index_grid.get(X).insert(index);
00019     }
00020     void MC_grid_index_spatial_hashing::delete_index(const MC_int_vector& X,const int index)
00021     {
00022         index_grid.get(X).erase(index);
00023     }
00024     std::set<int>& MC_grid_index_spatial_hashing::get(const MC_int_vector& X)
00025     {
00026         return index_grid.get(X);
00027     }
00028     const std::set<int>& MC_grid_index_spatial_hashing::get(const MC_int_vector& X) const
00029     {
00030         return index_grid.get(X);
00031     }
00032 
00033     void MC_grid_index_spatial_hashing::add_sphere(const MC_v3d& center,const double& radius,const int& id)
00034     {
00035         MC_int_vector_vector voxel=get_voxel_within_sphere(center,radius);
00036 
00037         for(unsigned int k=0,N=voxel.size();k<N;++k)
00038             add_index(voxel[k],id);
00039     }
00040     std::set<int> MC_grid_index_spatial_hashing::neighboors_index_in_radius(const MC_v3d& center,const double& radius) const
00041     {
00042         MC_int_vector_vector voxel=get_voxel_within_sphere(center,radius);
00043 
00044         std::set<int> index_neighbors;
00045         for(unsigned int k=0,N=voxel.size();k<N;++k)
00046         {
00047             std::set<int> current_index=get(MC_int_vector(voxel[k]));
00048             for(std::set<int>::const_iterator it=current_index.begin(),it_end=current_index.end();it!=it_end;++it)
00049                 index_neighbors.insert(*it);
00050         }
00051         return index_neighbors;
00052     }
00053 
00054     MC_int_vector_vector MC_grid_index_spatial_hashing::get_voxel_within_sphere(const MC_v3d& center,const double& radius) const
00055     {
00056         MC_int_vector_vector v_voxel;
00057 
00058         MC_int_vector X0=MC_int_vector(
00059                 static_cast<int>(center[0]),
00060                 static_cast<int>(center[1]),
00061                 static_cast<int>(center[2])
00062                 );
00063 
00064 
00065         int radius_int=static_cast<int>(radius+1.0);
00066 
00067         for(int k_x=-radius_int;k_x<=radius_int;++k_x)
00068         {
00069             for(int k_y=-radius_int;k_y<=radius_int;++k_y)
00070             {
00071                 for(int k_z=-radius_int;k_z<=radius_int;++k_z)
00072                 {
00073                     if( (k_x*k_x+k_y*k_y+k_z*k_z)<=radius_int*radius_int )
00074                     {
00075                         MC_int_vector y=X0+MC_int_vector(k_x,k_y,k_z);
00076                         if(is_in_bound(y))
00077                             v_voxel.add(y);
00078                     }
00079                 }
00080             }
00081         }
00082 
00083         return v_voxel;
00084 
00085     }
00086 
00087     void MC_grid_index_spatial_hashing::clear()
00088     {
00089         index_grid.fill(MC_int_vector().to_set());
00090     }
00091 
00092     bool MC_grid_index_spatial_hashing::is_in_bound(const MC_int_vector& position) const
00093     {
00094         const MC_int_vector& dim=size();
00095         if(position[0]>=0 && position[0]<dim[0])
00096             if(position[1]>=0 && position[1]<dim[1])
00097                 if(position[2]>=0 && position[2]<dim[2])
00098                     return true;
00099         return false;
00100     }
00101 
00102 }

Generated on Sun Apr 18 20:24:47 2010 by  doxygen 1.6.1