00001
00002
00003
00004 #ifndef CCTBX_MAPTBX_ACCESSORS_C_GRID_P1_H
00005 #define CCTBX_MAPTBX_ACCESSORS_C_GRID_P1_H
00006
00007 #include <scitbx/array_family/accessors/flex_grid.h>
00008 #include <cctbx/import_scitbx_af.h>
00009 #include <scitbx/math/modulo.h>
00010
00011 namespace cctbx { namespace maptbx {
00012
00013 template <std::size_t Nd>
00014 class c_grid_p1;
00015
00016 template <>
00017 class c_grid_p1<3> : public af::tiny<int, 3>
00018 {
00019 public:
00020 typedef af::tiny<int, 3> index_type;
00021 typedef index_type::value_type index_value_type;
00022 typedef index_value_type value_type;
00023
00024 c_grid_p1() : index_type(0,0,0) {}
00025
00026 c_grid_p1(index_type const& n) : index_type(n) {}
00027
00028 c_grid_p1(index_value_type const& n0,
00029 index_value_type const& n1,
00030 index_value_type const& n2)
00031 : index_type(n0, n1, n2)
00032 {}
00033
00034 template <typename FlexIndexType>
00035 c_grid_p1(af::flex_grid<FlexIndexType> const& flex_g)
00036 :
00037 index_type(af::adapt(flex_g.all()))
00038 {
00039 SCITBX_ASSERT(flex_g.is_0_based());
00040 }
00041
00042 af::flex_grid<>
00043 as_flex_grid() const
00044 {
00045 return af::flex_grid<>(af::adapt(*this));
00046 }
00047
00048 std::size_t
00049 size_1d() const
00050 {
00051 return this->elems[0] * this->elems[1] * this->elems[2];
00052 }
00053
00054 index_type
00055 index_nd(index_value_type const& i_1d) const
00056 {
00057 index_type i_nd;
00058 i_nd[2] = i_1d % this->elems[2];
00059 i_nd[0] = i_1d / this->elems[2];
00060 i_nd[1] = i_nd[0] % this->elems[1];
00061 i_nd[0] /= this->elems[1];
00062 return i_nd;
00063 }
00064
00065 std::size_t
00066 operator()(index_type const& i) const
00067 {
00068 const index_value_type* n = this->elems;
00069 return (scitbx::math::mod_positive(i[0], n[0]) * n[1]
00070 + scitbx::math::mod_positive(i[1], n[1])) * n[2]
00071 + scitbx::math::mod_positive(i[2], n[2]);
00072 }
00073
00074 std::size_t
00075 operator()(index_value_type const& i0,
00076 index_value_type const& i1,
00077 index_value_type const& i2) const
00078 {
00079 const index_value_type* n = this->elems;
00080 return (scitbx::math::mod_positive(i0, n[0]) * n[1]
00081 + scitbx::math::mod_positive(i1, n[1])) * n[2]
00082 + scitbx::math::mod_positive(i2, n[2]);
00083 }
00084 };
00085
00086 }}
00087
00088 #endif // CCTBX_MAPTBX_ACCESSORS_C_GRID_P1_H