00001 #ifndef CCTBX_MAPTBX_UTILS_H
00002 #define CCTBX_MAPTBX_UTILS_H
00003
00004 #include <cstddef>
00005
00006 namespace cctbx { namespace maptbx {
00007
00009 template <typename IntegerType>
00010 inline
00011 IntegerType
00012 ih_as_h(IntegerType ih, std::size_t n_real)
00013 {
00014 if (ih <= n_real/2) return ih;
00015 return ih - n_real;
00016 }
00017
00019
00021 template <typename IntegerType>
00022 inline
00023 IntegerType
00024 h_as_ih_exact(IntegerType h, IntegerType n_complex, bool positive_only)
00025 {
00026 if (positive_only) {
00027 if (0 > h || h >= n_complex) return -1;
00028 }
00029 else {
00030 IntegerType m = (n_complex - 1) / 2;
00031 if (-m > h || h > m) return -1;
00032 else if (h < 0) return h + n_complex;
00033 }
00034 return h;
00035 }
00036
00038
00040 template <typename IndexTypeN>
00041 af::int3
00042 h_as_ih_exact_array(bool anomalous_flag,
00043 miller::index<> const& h,
00044 IndexTypeN const& n_complex)
00045 {
00046 af::int3 ih;
00047 bool positive_only[] = {false, false, !anomalous_flag};
00048 for(std::size_t i=0;i<3;i++) {
00049 ih[i] = h_as_ih_exact(h[i], n_complex[i], positive_only[i]);
00050 }
00051 return ih;
00052 }
00053
00055
00057 template <typename IntegerType>
00058 inline
00059 IntegerType
00060 h_as_ih_mod(IntegerType h, IntegerType const& n_real)
00061 {
00062 h %= n_real;
00063 if (h < 0) h += n_real;
00064 return h;
00065 }
00066
00068
00072 template <typename IndexTypeN>
00073 inline
00074 IndexTypeN
00075 h_as_ih_mod_array(miller::index<> const& h, IndexTypeN const& n_real)
00076 {
00077 IndexTypeN ih;
00078 for(std::size_t i=0;i<3;i++) {
00079 ih[i] = h_as_ih_mod(h[i], n_real[i]);
00080 }
00081 return ih;
00082 }
00083
00084 }}
00085
00086 #endif // CCTBX_MAPTBX_UTILS_H