00001 00002 00003 #ifndef _MC_GRID_3D_HPP_ 00004 #define _MC_GRID_3D_HPP_ 00005 00006 #include <MC_int_vector.hpp> 00007 #include <MC_int_vector_vector.hpp> 00008 #include <MC_exception.hpp> 00009 #include <MC_string_helper.hpp> 00010 00011 #include <vector> 00012 #include <algorithm> 00013 00014 namespace mesh_conv 00015 { 00016 00017 namespace exception 00018 { 00019 class MC_exception_grid_3d : public MC_exception 00020 { 00021 public: 00022 MC_exception_grid_3d(const std::string& message):MC_exception(message){} 00023 private: 00024 }; 00025 00026 class MC_exception_grid_3d_out_of_bounds : public MC_exception_grid_3d 00027 { 00028 public: 00029 MC_exception_grid_3d_out_of_bounds(const std::string& message):MC_exception_grid_3d(message){} 00030 private: 00031 }; 00032 } 00033 00034 00035 00037 template <typename VALUE> 00038 class MC_grid_3d 00039 { 00040 public: 00041 00042 // ************************************************ // 00043 // ************************************************ // 00044 // Constructor 00045 // ************************************************ // 00046 // ************************************************ // 00047 00049 MC_grid_3d(); 00051 MC_grid_3d(const MC_int_vector& given_size); 00052 00053 00054 // ************************************************ // 00055 // ************************************************ // 00056 // Size 00057 // ************************************************ // 00058 // ************************************************ // 00059 00061 const MC_int_vector& size() const; 00062 00063 00064 // ************************************************ // 00065 // ************************************************ // 00066 // Get/Set value 00067 // ************************************************ // 00068 // ************************************************ // 00069 00071 VALUE& get(const MC_int_vector& X); 00073 const VALUE& get(const MC_int_vector& X) const; 00075 std::vector<VALUE> get(const MC_int_vector_vector& X) const; 00076 00078 void set(const MC_int_vector& X,const VALUE& y); 00079 00080 00081 00083 void fill(const VALUE& y); 00084 00085 00086 00087 00088 private: 00089 00091 std::vector<VALUE> grid; 00093 MC_int_vector dimension; 00094 00095 00097 void assert_is_in_range(const MC_int_vector& X) const throw(exception::MC_exception_grid_3d_out_of_bounds); 00098 00099 00100 }; 00101 } 00102 00103 00104 00105 00106 namespace mesh_conv 00107 { 00108 template <typename VALUE> 00109 MC_grid_3d<VALUE>::MC_grid_3d(const MC_int_vector& given_size) 00110 { 00111 if(given_size.size()!=3) 00112 { 00113 std::cout<<"Error in MC_grid_3d() size dim must be 3 and not "<<given_size.size()<<std::endl; 00114 throw exception::MC_exception_grid_3d("Error in MC_grid_3d size dim must be 3"); 00115 } 00116 00117 grid.resize(given_size[0]*given_size[1]*given_size[2]); 00118 dimension=given_size; 00119 } 00120 00121 template <typename VALUE> 00122 const MC_int_vector& MC_grid_3d<VALUE>::size() const 00123 {return dimension;} 00124 00125 template <typename VALUE> 00126 VALUE& MC_grid_3d<VALUE>::get(const MC_int_vector& X) 00127 { 00128 if(X.size()!=3) 00129 { 00130 std::cout<<"Error in MC_grid_3d::get size position must be 3 and not "<<X.size()<<std::endl; 00131 throw exception::MC_exception_grid_3d("Error in MC_grid_3d::get size position must be 3"); 00132 } 00133 00134 assert_is_in_range(X); 00135 return grid[X[0]+dimension[0]*(X[1]+dimension[1]*X[2])]; 00136 } 00137 00138 template <typename VALUE> 00139 const VALUE& MC_grid_3d<VALUE>::get(const MC_int_vector& X) const 00140 { 00141 if(X.size()!=3) 00142 { 00143 std::cout<<"Error in MC_grid_3d::get size position must be 3 and not "<<X.size()<<std::endl; 00144 throw exception::MC_exception_grid_3d("Error in MC_grid_3d::get size position must be 3"); 00145 } 00146 assert_is_in_range(X); 00147 00148 return grid[X[0]+dimension[0]*(X[1]+dimension[1]*X[2])]; 00149 } 00150 00151 template <typename VALUE> 00152 std::vector<VALUE> MC_grid_3d<VALUE>::get(const MC_int_vector_vector& X) const 00153 { 00154 std::vector<VALUE> vec; 00155 for(unsigned int k=0,N=X.size();k<N;++k) 00156 vec.push_back(get(X[k])); 00157 return vec; 00158 } 00159 00160 template <typename VALUE> 00161 void MC_grid_3d<VALUE>::set(const MC_int_vector& X,const VALUE& y) 00162 { 00163 if(X.size()!=3) 00164 { 00165 std::cout<<"Error in MC_grid_3d::get size position must be 3 and not "<<X.size()<<std::endl; 00166 throw exception::MC_exception_grid_3d("Error in MC_grid_3d::get size position must be 3"); 00167 } 00168 assert_is_in_range(X); 00169 grid[X[0]+dimension[0]*(X[1]+dimension[1]*X[2])]=y; 00170 } 00171 00172 template <typename VALUE> 00173 void MC_grid_3d<VALUE>::assert_is_in_range(const MC_int_vector& X) const throw(exception::MC_exception_grid_3d_out_of_bounds) 00174 { 00175 if(X[0]<0 || X[0]>=dimension[0]) 00176 throw exception::MC_exception_grid_3d_out_of_bounds("Error in MC_grid_3d::assert_is_in_range, try to access x="+MC_string_converter::to_string(X[0])+"/"+MC_string_converter::to_string(dimension[0])); 00177 if(X[1]<0 || X[1]>=dimension[1]) 00178 throw exception::MC_exception_grid_3d_out_of_bounds("Error in MC_grid_3d::assert_is_in_range, try to access y="+MC_string_converter::to_string(X[1])+"/"+MC_string_converter::to_string(dimension[1])); 00179 if(X[2]<0 || X[2]>=dimension[2]) 00180 throw exception::MC_exception_grid_3d_out_of_bounds("Error in MC_grid_3d::assert_is_in_range, try to access z="+MC_string_converter::to_string(X[2])+"/"+MC_string_converter::to_string(dimension[2])); 00181 00182 } 00183 00184 template <typename VALUE> 00185 MC_grid_3d<VALUE>::MC_grid_3d() 00186 :dimension(MC_int_vector(0,0,0)) 00187 {} 00188 00189 template <typename VALUE> 00190 void MC_grid_3d<VALUE>::fill(const VALUE& y) 00191 { 00192 unsigned int N=grid.size(); 00193 if(N>0) 00194 { 00195 VALUE* p_grid=&grid[0]; 00196 for(unsigned int k=0;k<N;++k,++p_grid) 00197 *p_grid=y; 00198 } 00199 } 00200 00201 00202 00203 00204 } 00205 00206 #endif
1.6.1