00001 #ifndef CCTBX_MAPTBX_GENERIC_GRID_H
00002 #define CCTBX_MAPTBX_GENERIC_GRID_H
00003
00004 #include <cctbx/import_scitbx_af.h>
00005 #include <scitbx/array_family/accessors/c_grid_padded.h>
00006
00007 namespace cctbx {
00008
00009 namespace maptbx {
00010
00011 template < typename MapType,
00012 typename FloatType,
00013 typename IntType > class generic_grid;
00014
00015
00016 template < typename FloatType, typename IntType >
00017 class generic_grid<void,FloatType,IntType> {
00018 public:
00019 typedef basic_mapper<void,FloatType,IntType> mapper_type;
00020 typedef generic_grid<void,FloatType,IntType> grid_intf_type;
00021 typedef chiltbx::handle::handle<grid_intf_type> handle_type;
00022 typedef af::ref<FloatType, af::flex_grid<> > flex_grid_ref;
00023 typedef af::const_ref<FloatType, af::flex_grid<> > const_flex_grid_ref;
00024 typedef af::c_grid_padded<dimension_3> c_grid_padded_3;
00025 typedef af::ref<FloatType, c_grid_padded_3> c_grid_ref;
00026 typedef af::const_ref<FloatType, c_grid_padded_3> const_c_grid_ref;
00027 virtual handle_type as_handle () const = 0;
00028 virtual mapper_type remap ( fractional<FloatType> const& ) const = 0;
00029 virtual FloatType get_value ( grid_point<IntType> const& ) const = 0;
00030 virtual void set_value ( grid_point<IntType> const&, FloatType const& ) = 0;
00031 virtual bool is_inside ( grid_point<IntType> const& ) const = 0;
00032 virtual bool is_inside ( fractional<FloatType> const& ) const = 0;
00033 virtual af::tiny<IntType,dimension_3> origin () const = 0;
00034 virtual af::tiny<IntType,dimension_3> last () const = 0;
00035 virtual af::tiny<IntType,dimension_3> focus () const = 0;
00036 virtual af::tiny<IntType,dimension_3> all () const = 0;
00037 };
00038
00039 namespace cdsa = crystal::direct_space_asu;
00040 typedef sgtbx::space_group tbx_space_group;
00041
00042 template < typename FloatType, typename IntType >
00043 class generic_grid<asu,FloatType,IntType>
00044 : public generic_grid<void,FloatType,IntType> {
00045 public:
00046 typedef generic_grid<void,FloatType,IntType> grid_intf_type;
00047 typedef chiltbx::handle::handle<grid_intf_type> handle_type;
00048 typedef basic_mapper<void,FloatType,IntType> mapper_type;
00049 typedef af::ref<FloatType, af::flex_grid<> > flex_grid_ref;
00050 typedef af::const_ref<FloatType, af::flex_grid<> > const_flex_grid_ref;
00051 typedef af::c_grid_padded<dimension_3> c_grid_3;
00052 typedef af::ref<FloatType, c_grid_3> c_grid_ref;
00053 typedef af::const_ref<FloatType, c_grid_3 > const_c_grid_ref;
00054 typedef typename af::flex_grid<>::index_type map_index;
00055 generic_grid ( af::versa<FloatType, af::flex_grid<> > const& data,
00056 tbx_space_group const& space_group,
00057 cdsa::float_asu<FloatType> const& fasu,
00058 af::tiny<IntType,dimension_3> const& grid_len,
00059 FloatType const& min_distance_sym_equiv=0.5,
00060 bool assert_min_distance_sym_equiv=true )
00061 : space_group_(space_group)
00062 , float_asu_(fasu)
00063 , grid_length_(grid_len)
00064 , min_distance_sym_equiv_(min_distance_sym_equiv)
00065 , assert_min_distance_sym_equiv_(assert_min_distance_sym_equiv) {
00066 this->versa_data_ = data;
00067 this->versa_ref_ = this->versa_data_.ref();
00068 CCTBX_ASSERT(this->versa_ref_.accessor().nd() == 3);
00069 }
00070 generic_grid ( generic_grid const& gintf )
00071 : space_group_(gintf.space_group_)
00072 , float_asu_(gintf.float_asu_)
00073 , grid_length_(gintf.grid_length_) {
00074 this->versa_data_ = gintf.versa_data_;
00075 this->versa_ref_ = this->versa_data_.ref();
00076 }
00077 generic_grid& operator = ( generic_grid const& gintf ) {
00078 this->versa_data_ = gintf.versa_data_;
00079 this->versa_ref_ = this->versa_data_.ref();
00080 this->space_group_ = gintf.space_group_;
00081 this->float_asu_ = gintf.float_asu_;
00082 this->grid_length_ = gintf.grid_length_;
00083 return *this;
00084 }
00085 virtual handle_type as_handle () const {
00086 return handle_type(*this);
00087 }
00088 virtual mapper_type remap ( fractional<FloatType> const& F ) const {
00089 return basic_mapper<asu,FloatType,IntType>(F,this->space_group_,this->float_asu_);
00090 }
00091 map_index grid_point_to_map_index ( grid_point<IntType> const& gpt ) const {
00092 map_index midx(3,0);
00093 midx[0] = gpt[0];
00094 midx[1] = gpt[1];
00095 midx[2] = gpt[2];
00096 return midx;
00097 }
00098 virtual FloatType get_value ( grid_point<IntType> const& coord ) const {
00099 return this->versa_ref_(this->grid_point_to_map_index(coord));
00100 }
00101 void basic_set_value ( grid_point<IntType> const& coord, FloatType const& F ) {
00102 this->versa_ref_.begin()[
00103 this->versa_ref_.accessor()(
00104 this->grid_point_to_map_index(coord))] = F;
00105 }
00106 virtual void set_value ( grid_point<IntType> const& coord, FloatType const& F ) {
00107 this->basic_set_value(coord,F);
00108 }
00109 virtual bool is_inside ( grid_point<IntType> const& coord ) const {
00110 return this->is_inside( grid_fractionalization<FloatType>(coord,this->grid_length_) );
00111 }
00112 virtual bool is_inside ( fractional<FloatType> const& coord ) const {
00113 return this->float_asu_.is_inside(coord);
00114 }
00115 virtual af::tiny<IntType,dimension_3> origin () const {
00116 af::tiny<IntType,dimension_3> result;
00117 typename af::flex_grid<>::index_type idx = this->versa_ref_.accessor().origin();
00118 for ( std::size_t i=0; i<dimension_3; ++i )
00119 result[i] = idx[i];
00120 return result;
00121 }
00122 virtual af::tiny<IntType,dimension_3> last () const {
00123 af::tiny<IntType,dimension_3> result;
00124 typename af::flex_grid<>::index_type idx = this->versa_ref_.accessor().last();
00125 for ( std::size_t i=0; i<dimension_3; ++i )
00126 result[i] = idx[i];
00127 return result;
00128 }
00129 virtual af::tiny<IntType,dimension_3> focus () const {
00130 af::tiny<IntType,dimension_3> result;
00131 typename af::flex_grid<>::index_type idx = this->versa_ref_.accessor().focus();
00132 for ( std::size_t i=0; i<dimension_3; ++i )
00133 result[i] = idx[i];
00134 return result;
00135 }
00136 virtual af::tiny<IntType,dimension_3> all () const {
00137 af::tiny<IntType,dimension_3> result;
00138 typename af::flex_grid<>::index_type idx = this->versa_ref_.accessor().all();
00139 for ( std::size_t i=0; i<dimension_3; ++i )
00140 result[i] = idx[i];
00141 return result;
00142 }
00143 private:
00144 tbx_space_group space_group_;
00145 cdsa::float_asu<FloatType> float_asu_;
00146 af::tiny<IntType,dimension_3> grid_length_;
00147 af::versa<FloatType, af::flex_grid<> > versa_data_;
00148 af::ref<FloatType, af::flex_grid<> > versa_ref_;
00149 FloatType min_distance_sym_equiv_;
00150 bool assert_min_distance_sym_equiv_;
00151 };
00152
00153 template < typename FloatType, typename IntType >
00154 class generic_grid<unit_cell,FloatType,IntType>
00155 : public generic_grid<void,FloatType,IntType> {
00156 public:
00157 typedef generic_grid<void,FloatType,IntType> grid_intf_type;
00158 typedef chiltbx::handle::handle<grid_intf_type> handle_type;
00159 typedef basic_mapper<void,FloatType,IntType> mapper_type;
00160 typedef af::ref<FloatType, af::flex_grid<> > flex_grid_ref;
00161 typedef af::const_ref<FloatType, af::flex_grid<> > const_flex_grid_ref;
00162 typedef af::c_grid_padded<dimension_3> c_grid_3;
00163 typedef af::ref<FloatType, c_grid_3> c_grid_ref;
00164 typedef af::const_ref<FloatType, c_grid_3 > const_c_grid_ref;
00165 generic_grid ( af::versa<FloatType,af::flex_grid<> > const& data ) {
00166 this->c_grid_data_ = af::versa<FloatType, af::c_grid_padded<dimension_3> >(
00167 data,
00168 af::c_grid_padded<dimension_3>(
00169 data.accessor()));
00170 this->c_grid_ref_ = this->c_grid_data_.ref();
00171 }
00172 generic_grid ( af::versa<FloatType, af::c_grid_padded<dimension_3> > const& data ) {
00173 this->c_grid_data_ = data;
00174 this->c_grid_ref_ = this->c_grid_data_.ref();
00175 }
00176 generic_grid ( generic_grid const& gintf ){
00177 this->versa_data_ = gintf.versa_data_;
00178 this->versa_ref_ = this->versa_data_.ref();
00179 this->c_grid_data_ = gintf.c_grid_data_;
00180 this->c_grid_ref_ = this->c_grid_data_.ref();
00181 }
00182 generic_grid& operator = ( generic_grid const& gintf ) {
00183 this->versa_data_ = gintf.versa_data_;
00184 this->versa_ref_ = this->versa_data_.ref();
00185 this->c_grid_data_ = gintf.c_grid_data_;
00186 this->c_grid_ref_ = this->c_grid_data_.ref();
00187 return *this;
00188 }
00189 virtual mapper_type remap ( fractional<FloatType> const& F ) const {
00190 return basic_mapper<unit_cell,FloatType,IntType>(F);
00191 }
00192 typedef typename af::c_grid_padded<dimension_3>::index_type map_index;
00193 map_index grid_point_to_map_index ( grid_point<IntType> const& gpt ) const {
00194 map_index midx(3,0);
00195 midx[0] = gpt[0];
00196 midx[1] = gpt[1];
00197 midx[2] = gpt[2];
00198 return midx;
00199 }
00200 virtual handle_type as_handle () const {
00201 return handle_type(*this);
00202 }
00203 virtual FloatType get_value ( grid_point<IntType> const& coord ) const {
00204 return this->c_grid_ref_(this->grid_point_to_map_index(coord));
00205 }
00206 void basic_set_value ( grid_point<IntType> const& coord, FloatType const& F ) {
00207 this->c_grid_ref_.begin()[
00208 this->c_grid_ref_.accessor()(
00209 this->grid_point_to_map_index(coord))] = F;
00210 }
00211 virtual void set_value ( grid_point<IntType> const& coord, FloatType const& F ) {
00212 this->basic_set_value(coord,F);
00213 }
00214 virtual bool is_inside ( grid_point<IntType> const& coord ) const {
00215 for ( std::size_t i=0; i<dimension_3; ++i )
00216 if ( coord[i] < 0 || coord[i] >= this->c_grid_ref_.accessor().focus()[i] )
00217 return false;
00218 return true;
00219 }
00220 virtual bool is_inside ( fractional<FloatType> const& coord ) const {
00221
00222 for ( std::size_t i=0; i<dimension_3; ++i )
00223 if ( coord[i] < 0 || coord[i] >= 1 )
00224 return false;
00225 return true;
00226 }
00227 virtual af::tiny<IntType,dimension_3> origin () const {
00228 return af::tiny<IntType,dimension_3>(0,0,0);
00229 }
00230 virtual af::tiny<IntType,dimension_3> last () const {
00231 af::tiny<IntType,dimension_3> result;
00232 typename af::c_grid_padded<dimension_3>::index_type idx
00233 = this->c_grid_ref_.accessor().focus();
00234 for ( std::size_t i=0; i<dimension_3; ++i )
00235 result[i] = idx[i];
00236 return result;
00237 }
00238 virtual af::tiny<IntType,dimension_3> focus () const {
00239 af::tiny<IntType,dimension_3> result;
00240 typename af::c_grid_padded<dimension_3>::index_type idx
00241 = this->c_grid_ref_.accessor().focus();
00242 for ( std::size_t i=0; i<dimension_3; ++i )
00243 result[i] = idx[i];
00244 return result;
00245 }
00246 virtual af::tiny<IntType,dimension_3> all () const {
00247 af::tiny<IntType,dimension_3> result;
00248 typename af::c_grid_padded<dimension_3>::index_type idx
00249 = this->c_grid_ref_.accessor().all();
00250 for ( std::size_t i=0; i<dimension_3; ++i )
00251 result[i] = idx[i];
00252 return result;
00253 }
00254 private:
00255 af::versa<FloatType, af::c_grid_padded<dimension_3> > c_grid_data_;
00256 af::ref<FloatType, af::c_grid_padded<dimension_3> > c_grid_ref_;
00257 af::versa<FloatType, af::flex_grid<> > versa_data_;
00258 af::ref<FloatType, af::flex_grid<> > versa_ref_;
00259 };
00260
00261 template < typename FloatType, typename IntType >
00262 class generic_grid<non_symmetric,FloatType,IntType>
00263 : public generic_grid<void,FloatType,IntType> {
00264 public:
00265 typedef generic_grid<void,FloatType,IntType> grid_intf_type;
00266 typedef chiltbx::handle::handle<grid_intf_type> handle_type;
00267 typedef basic_mapper<void,FloatType,IntType> mapper_type;
00268 typedef af::ref<FloatType, af::flex_grid<> > flex_grid_ref;
00269 typedef af::const_ref<FloatType, af::flex_grid<> > const_flex_grid_ref;
00270 typedef af::c_grid_padded<dimension_3> c_grid_3;
00271 typedef af::ref<FloatType, c_grid_3> c_grid_ref;
00272 typedef af::const_ref<FloatType, c_grid_3 > const_c_grid_ref;
00273 generic_grid ( af::versa<FloatType,af::flex_grid<> > const& data,
00274 af::tiny<IntType,dimension_3> const& grid_len )
00275 : grid_length_(grid_len) {
00276 this->versa_data_ = data;
00277 this->versa_ref_ = this->versa_data_.ref();
00278 CCTBX_ASSERT(this->versa_ref_.accessor().nd() == 3);
00279 }
00280 generic_grid ( generic_grid const& gintf )
00281 : grid_length_(gintf.grid_length_) {
00282 this->versa_data_ = gintf.versa_data_;
00283 this->versa_ref_ = this->versa_data_.ref();
00284 }
00285 generic_grid& operator = ( generic_grid const& gintf ) {
00286 this->grid_length_ = gintf.grid_length_;
00287 this->versa_data_ = gintf.versa_data_;
00288 this->versa_ref_ = this->versa_data_.ref();
00289 return *this;
00290 }
00291 virtual mapper_type remap ( fractional<FloatType> const& F ) const {
00292 return basic_mapper<non_symmetric,FloatType,IntType>(F);
00293 }
00294 typedef typename af::flex_grid<>::index_type map_index;
00295 map_index grid_point_to_map_index ( grid_point<IntType> const& gpt ) const {
00296 map_index midx(3,0);
00297 midx[0] = gpt[0];
00298 midx[1] = gpt[1];
00299 midx[2] = gpt[2];
00300 return midx;
00301 }
00302 virtual handle_type as_handle () const {
00303 return handle_type(*this);
00304 }
00305 virtual FloatType get_value ( grid_point<IntType> const& coord ) const {
00306 return this->versa_ref_(this->grid_point_to_map_index(coord));
00307 }
00308 void basic_set_value ( grid_point<IntType> const& coord, FloatType const& F ) {
00309 this->versa_ref_.begin()[
00310 this->versa_ref_.accessor()(
00311 this->grid_point_to_map_index(coord))] = F;
00312 }
00313 virtual void set_value ( grid_point<IntType> const& coord, FloatType const& F ) {
00314 this->basic_set_value(coord,F);
00315 }
00316 virtual bool is_inside ( grid_point<IntType> const& gpt ) const {
00317 for ( std::size_t i=0; i<dimension_3; ++i )
00318 if ( gpt[i] < this->versa_ref_.accessor().origin()[i]
00319 || gpt[i] >= this->versa_ref_.accessor().last()[i] )
00320 return false;
00321 return true;
00322 }
00323 virtual bool is_inside ( fractional<FloatType> const& coord ) const {
00324 grid_point<IntType> g_origin(this->versa_ref_.accessor().origin().begin());
00325 grid_point<IntType> g_last(this->versa_ref_.accessor().last().begin());
00326 fractional<FloatType> origin = grid_fractionalization<FloatType>(g_origin,this->grid_length_);
00327 fractional<FloatType> last = grid_fractionalization<FloatType>(g_last,this->grid_length_);
00328 for ( std::size_t i=0; i<dimension_3; ++i )
00329 if ( coord[i] < origin[i] || coord[i] >= last[i] )
00330 return false;
00331 return true;
00332 }
00333 virtual af::tiny<IntType,dimension_3> origin () const {
00334 af::tiny<IntType,dimension_3> result;
00335 typename af::flex_grid<>::index_type idx = this->versa_ref_.accessor().origin();
00336 for ( std::size_t i=0; i<dimension_3; ++i )
00337 result[i] = idx[i];
00338 return result;
00339 }
00340 virtual af::tiny<IntType,dimension_3> last () const {
00341 af::tiny<IntType,dimension_3> result;
00342 typename af::flex_grid<>::index_type idx = this->versa_ref_.accessor().last();
00343 for ( std::size_t i=0; i<dimension_3; ++i )
00344 result[i] = idx[i];
00345 return result;
00346 }
00347 virtual af::tiny<IntType,dimension_3> focus () const {
00348 af::tiny<IntType,dimension_3> result;
00349 typename af::flex_grid<>::index_type idx = this->versa_ref_.accessor().focus();
00350 for ( std::size_t i=0; i<dimension_3; ++i )
00351 result[i] = idx[i];
00352 return result;
00353 }
00354 virtual af::tiny<IntType,dimension_3> all () const {
00355 af::tiny<IntType,dimension_3> result;
00356 typename af::flex_grid<>::index_type idx = this->versa_ref_.accessor().all();
00357 for ( std::size_t i=0; i<dimension_3; ++i )
00358 result[i] = idx[i];
00359 return result;
00360 }
00361 private:
00362 af::tiny<IntType,dimension_3> grid_length_;
00363 af::versa<FloatType, af::flex_grid<> > versa_data_;
00364 af::ref<FloatType, af::flex_grid<> > versa_ref_;
00365 };
00366
00367 }
00368
00369 }
00370
00371 #endif//CCTBX_MAPTBX_GENERIC_GRID_H