00001
00002 #ifndef CCTBX_MAPTBX_COORDINATE_TRANSFORMERS_HPP
00003 #define CCTBX_MAPTBX_COORDINATE_TRANSFORMERS_HPP
00004
00005 #include<scitbx/mat3.h>
00006 #include<scitbx/math/utils.h>
00007 #include<cctbx/coordinates.h>
00008
00009 namespace cctbx {
00010
00011 namespace maptbx {
00012
00013 static const std::size_t dimension_3 = 3;
00014
00015
00016 template < typename FloatType, typename IntType >
00017 fractional<FloatType>
00018 grid_fractionalization ( grid_point<IntType> const& coordinate,
00019 af::tiny<IntType,dimension_3> const& extents ) {
00020 scitbx::vec3<FloatType> result;
00021 for ( std::size_t i=0; i<dimension_3; ++i )
00022 result[i] = FloatType(coordinate[i]) / FloatType(extents[i]);
00023 return result;
00024 }
00025
00026
00027 template < typename IntType, typename FloatType >
00028 scitbx::vec3<FloatType>
00029 strange_fractional_gridization ( fractional<FloatType> const& coordinate,
00030 af::tiny<IntType,dimension_3> const& extents ) {
00031 scitbx::vec3<FloatType> result;
00032 for ( std::size_t i=0; i<dimension_3; ++i )
00033 result[i] = coordinate[i] * extents[i];
00034 return result;
00035 }
00036
00037 template < typename IntType, typename FloatType >
00038 grid_point<IntType>
00039 floor_fractional_gridization ( fractional<FloatType> const& coordinate,
00040 af::tiny<IntType,dimension_3> const& extents ) {
00041 typedef scitbx::math::float_int_conversions<FloatType,IntType> fic;
00042 scitbx::vec3<FloatType> intermediate = strange_fractional_gridization(coordinate,extents);
00043 grid_point<IntType> result;
00044 for ( std::size_t i=0; i<dimension_3; ++i )
00045 result[i] = IntType( fic::ifloor( intermediate[i] ) );
00046 return result;
00047 }
00048
00049
00050 template < typename IntType, typename FloatType >
00051 grid_point<IntType>
00052 round_fractional_gridization ( fractional<FloatType> const& coordinate,
00053 af::tiny<IntType,dimension_3> const& extents ) {
00054 typedef scitbx::math::float_int_conversions<FloatType,IntType> fic;
00055 scitbx::vec3<FloatType> intermediate = strange_fractional_gridization(coordinate,extents);
00056 grid_point<IntType> result;
00057 for ( std::size_t i=0; i<dimension_3; ++i )
00058 result[i] = IntType( fic::iround( intermediate[i] ) );
00059 return result;
00060 }
00061
00062 template < typename FromType,
00063 typename ToType> struct transform;
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 template < typename T > struct transformer_types {
00074 typedef typename T::from_type from_type;
00075 typedef typename T::to_type to_type;
00076 };
00077
00078
00079 template < typename T >
00080 struct transform<T,T> {
00081 typedef T from_type;
00082 typedef T to_type;
00083 transform () {}
00084 T operator () ( T const& t ) const {
00085 return t;
00086 }
00087 maptbx::transform<T,T> inverse () const {
00088 return maptbx::transform<T,T>();
00089 }
00090 };
00091
00092
00093 template < typename FloatType, typename IntType >
00094 struct transform<fractional<FloatType>,grid_point<IntType> > {
00095 typedef fractional<FloatType> from_type;
00096 typedef grid_point<IntType> to_type;
00097 transform () {}
00098 transform ( af::tiny<IntType,dimension_3> const& to_grid ) {
00099 this->to_grid_ = to_grid;
00100 }
00101 grid_point<IntType> operator () ( fractional<FloatType> const& coord ) const {
00102 return round_fractional_gridization(coord,this->to_grid_);
00103 }
00104 maptbx::transform<grid_point<IntType>,fractional<FloatType> > inverse () const {
00105 return maptbx::transform<grid_point<IntType>,fractional<FloatType> >(this->to_grid_);
00106 }
00107 grid_point<IntType> floor_transform ( fractional<FloatType> const& coord ) const {
00108 return floor_fractional_gridization(coord,this->to_grid_);
00109 }
00110 scitbx::vec3<FloatType> strange_transform ( fractional<FloatType> const& coord ) const {
00111 return strange_fractional_gridization(coord,this->to_grid_);
00112 }
00113 af::tiny<IntType,dimension_3> to_grid_;
00114 };
00115
00116
00117 template < typename FloatType >
00118 struct transform<fractional<FloatType>,cartesian<FloatType> > {
00119 typedef fractional<FloatType> from_type;
00120 typedef cartesian<FloatType> to_type;
00121 transform () {}
00122 transform ( scitbx::mat3<FloatType> const& to_cart ) {
00123 this->to_cart_ = to_cart;
00124 }
00125 cartesian<FloatType> operator () ( fractional<FloatType> const& coord ) const {
00126 return this->to_cart_ * coord;
00127 }
00128 maptbx::transform<cartesian<FloatType>,fractional<FloatType> > inverse () const {
00129 return maptbx::transform<cartesian<FloatType>,fractional<FloatType> >(this->to_cart_.inverse());
00130 }
00131 scitbx::mat3<FloatType> to_cart_;
00132 };
00133
00134
00135 template < typename FloatType, typename IntType >
00136 struct transform<cartesian<FloatType>,grid_point<IntType> > {
00137 typedef cartesian<FloatType> from_type;
00138 typedef grid_point<IntType> to_type;
00139
00140 transform () {}
00141 transform (
00142 maptbx::transform<cartesian<FloatType>,fractional<FloatType> > const& c2f,
00143 maptbx::transform<fractional<FloatType>,grid_point<IntType> > const& f2g ) {
00144 this->to_frac_ = c2f.to_frac_;
00145 this->to_grid_ = f2g.to_grid_;
00146 }
00147 transform ( scitbx::mat3<FloatType> const& to_frac,
00148 af::tiny<IntType,dimension_3> const& to_grid ) {
00149 this->to_frac_ = to_frac;
00150 this->to_grid_ = to_grid;
00151 }
00152 grid_point<IntType> operator () ( cartesian<FloatType> const& coord ) const {
00153 scitbx::vec3<FloatType> F = this->to_frac_*coord;
00154 fractional<FloatType> frac(F[0],F[1],F[2]);
00155 return grid_point<IntType>( round_fractional_gridization( frac,
00156 this->to_grid_) );
00157 }
00158 maptbx::transform<grid_point<IntType>,cartesian<FloatType> > inverse () const {
00159 return maptbx::transform<grid_point<IntType>,cartesian<FloatType> >(
00160 this->to_grid_, this->to_frac_.inverse() );
00161 }
00162 scitbx::mat3<FloatType> to_frac_;
00163 af::tiny<IntType,dimension_3> to_grid_;
00164 };
00165
00166
00167 template < typename FloatType >
00168 struct transform<cartesian<FloatType>,fractional<FloatType> > {
00169 typedef cartesian<FloatType> from_type;
00170 typedef fractional<FloatType> to_type;
00171
00172 transform () {}
00173 transform ( scitbx::mat3<FloatType> const& to_frac ) {
00174 this->to_frac_ = to_frac;
00175 }
00176 fractional<FloatType> operator () ( cartesian<FloatType> const& coord ) const {
00177 return this->to_frac_ * coord;
00178 }
00179 maptbx::transform<fractional<FloatType>,cartesian<FloatType> > inverse () const {
00180 return maptbx::transform<fractional<FloatType>,cartesian<FloatType> >(this->to_frac_.inverse());
00181 }
00182 scitbx::mat3<FloatType> to_frac_;
00183 };
00184
00185
00186 template < typename FloatType, typename IntType >
00187 struct transform<grid_point<IntType>,cartesian<FloatType> > {
00188 typedef grid_point<IntType> from_type;
00189 typedef cartesian<FloatType> to_type;
00190
00191 transform () {}
00192 transform (
00193 maptbx::transform<grid_point<IntType>,fractional<FloatType> > const& g2f,
00194 maptbx::transform<fractional<FloatType>,cartesian<FloatType> > const& f2c ) {
00195 this->to_cart_ = f2c.to_cart_;
00196 this->to_frac_ = g2f.to_frac_;
00197 }
00198 transform ( af::tiny<IntType,dimension_3> const& to_frac,
00199 scitbx::mat3<FloatType> const& to_cart ) {
00200 this->to_frac_ = to_frac;
00201 this->to_cart_ = to_cart;
00202 }
00203 cartesian<FloatType> operator () ( grid_point<IntType> const& coord ) const {
00204 return cartesian<FloatType>( this->to_cart_ *
00205 grid_fractionalization<FloatType>(coord,this->to_frac_) );
00206 }
00207 maptbx::transform<cartesian<FloatType>,grid_point<IntType> > inverse () const {
00208 return maptbx::transform<cartesian<FloatType>,grid_point<IntType> >(
00209 this->to_cart_.inverse(), this->to_frac_ );
00210 }
00211 scitbx::mat3<FloatType> to_cart_;
00212 af::tiny<IntType,dimension_3> to_frac_;
00213 };
00214
00215
00216 template < typename FloatType, typename IntType >
00217 struct transform<grid_point<IntType>,fractional<FloatType> > {
00218 typedef grid_point<IntType> from_type;
00219 typedef fractional<FloatType> to_type;
00220
00221 transform () {}
00222 transform ( af::tiny<IntType,dimension_3> const& to_frac ) {
00223 this->to_frac_ = to_frac;
00224 }
00225 fractional<FloatType> operator () ( grid_point<IntType> const& coord ) const {
00226 return grid_fractionalization<FloatType>(coord,this->to_frac_);
00227 }
00228 maptbx::transform<fractional<FloatType>,grid_point<IntType> > inverse () const {
00229 return maptbx::transform<fractional<FloatType>,grid_point<IntType> >(this->to_frac_);
00230 }
00231 af::tiny<IntType,dimension_3> to_frac_;
00232 };
00233
00234 }
00235
00236 }
00237
00238 #endif//CCTBX_MAPTBX_COORDINATE_TRANSFORMERS_HPP