00001
00002
00003
00004
00005
00006
00007
00008 #ifndef IMPEM_PROJECT_H
00009 #define IMPEM_PROJECT_H
00010
00011 #include "em_config.h"
00012 #include "DensityMap.h"
00013 #include <IMP/algebra/utility.h>
00014 #include <IMP/algebra/Matrix3D.h>
00015 #include <IMP/algebra/Matrix2D.h>
00016 #include <IMP/algebra/Vector3D.h>
00017 #include <IMP/algebra/SphericalVector3D.h>
00018 #include <IMP/algebra/Rotation3D.h>
00019 #include <IMP/constants.h>
00020 #include <algorithm>
00021 #include <fstream>
00022
00023 IMPEM_BEGIN_NAMESPACE
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 template<typename T>
00062 void project_given_rotation1(IMP::algebra::Matrix3D<T>& m3,
00063 IMP::algebra::Matrix2D<double>& m2,
00064 const int Ydim,const int Xdim,
00065 const IMP::algebra::Rotation3D& Rot,
00066 const IMP::algebra::VectorD<3>& shift,
00067 const double equality_tolerance) {
00068
00069
00070 m2.resize(Ydim, Xdim);
00071
00072 std::vector<int> orig2D(2), orig3D(3);
00073 orig2D[0]=m2.get_start(0);orig2D[1]=m2.get_start(1);
00074 orig3D[0]=m3.get_start(0);orig3D[1]=m3.get_start(1);orig3D[2]=m3.get_start(2);
00075
00076 m2.centered_start();
00077 m3.centered_start();
00078
00079
00080
00081 IMP::algebra::Rotation3D InvRot = Rot.get_inverse();
00082 IMP::algebra::VectorD<3> direction;
00083 for (unsigned int i=0; i< 3; ++i) {
00084 IMP::algebra::VectorD<3> v(0,0,0);
00085 v[i]=1;
00086 algebra::VectorD<3> r= Rot.get_rotated(v);
00087 direction[i]=r[2];
00088 }
00089
00090 #ifdef IMP_DEBUG_PROJECT
00091 std::cout << " direction " << direction << std::endl;
00092 std::cout << "Rotation: " << Rot << std::endl;
00093 std::cout << "Inverse rotation: " << InvRot << std::endl;
00094 #endif
00095
00096
00097
00098 for (int ii = 0;ii < 3;ii++) {
00099 if (std::abs(direction[ii]) < equality_tolerance) {
00100 direction[ii] = equality_tolerance;
00101 }
00102 }
00103
00104
00105 IMP::algebra::VectorD<3> init0, end0, signs, half_signs;
00106 for (int i = 0;i < 3;i++) {
00107 init0[i] = m3.get_start(2-i);
00108
00109
00110 end0[i] = m3.get_finish(2-i);
00111 signs[i] = IMP::algebra::get_sign(direction[i]);
00112 half_signs[i] = 0.5 * signs[i];
00113 }
00114
00115 IMP::algebra::VectorD<3> r;
00116
00117 IMP::algebra::VectorD<3> p;
00118
00119
00120
00121 double step = 1.0 / 3.0;
00122
00123 for (int j = m2.get_start(1);j <= m2.get_finish(1);j++) {
00124 for (int i = m2.get_start(0);i <= m2.get_finish(0);i++) {
00125
00126 double ray_sum = 0.0;
00127 for (int rays_per_pixel = 0; rays_per_pixel < 4; rays_per_pixel++) {
00128 #ifdef IMP_DEBUG_PROJECT
00129 std::cout << "(" << j << "," << i << ") init ray " <<
00130 rays_per_pixel << std::endl;
00131 #endif
00132
00133 switch (rays_per_pixel) {
00134 case 0:
00135 p = IMP::algebra::VectorD<3>(j - step, i - step, 0);
00136 break;
00137 case 1:
00138 p = IMP::algebra::VectorD<3>(j - step, i + step, 0);
00139 break;
00140 case 2:
00141 p = IMP::algebra::VectorD<3>(j + step, i - step, 0);
00142 break;
00143 case 3:
00144 p = IMP::algebra::VectorD<3>(j + step, i + step, 0);
00145 break;
00146 }
00147
00148
00149 if (get_linf_norm(shift) > equality_tolerance) {
00150 p -= shift;
00151 }
00152
00153 r = InvRot.get_rotated(p);
00154 #ifdef IMP_DEBUG_PROJECT
00155 std::cout << "p: " << p << std::endl;
00156 std::cout << "r: " << r << std::endl;
00157 #endif
00158
00159
00160 IMP::algebra::VectorD<3> v_alpha_min, v_alpha_max, v_alpha, v_diff;
00161 double alpha_min=-1/equality_tolerance;
00162 double alpha_max=1/equality_tolerance;
00163 for (int ii = 0;ii < 3;ii++) {
00164 v_alpha_min[ii] = (init0[ii] - 0.5 - r[ii]) / direction[ii];
00165 v_alpha_max[ii] = ( end0[ii] + 0.5 - r[ii]) / direction[ii];
00166
00167
00168 if(std::abs(direction[ii]) > equality_tolerance ) {
00169 alpha_min=std::max(alpha_min,
00170 std::min(v_alpha_min[ii],v_alpha_max[ii]));
00171 alpha_max=std::min(alpha_max,
00172 std::max(v_alpha_min[ii],v_alpha_max[ii]));
00173 }
00174 }
00175
00176 #ifdef IMP_DEBUG_PROJECT
00177 std::cout << "v_alpha_min " << v_alpha_min;
00178 std::cout << " v_alpha_max " << v_alpha_max << std::endl;
00179 #endif
00180 if (std::abs(alpha_max- alpha_min) < equality_tolerance) {
00181 continue;
00182 }
00183
00184 IMP::algebra::VectorD<3> v,idx;
00185
00186 v = r + alpha_min * direction;
00187 for (int ii=0;ii < 3;ii++) {
00188 if (v[ii] >= 0.) {
00189 idx[ii]
00190 = IMP::algebra::get_constrained((double)((int)(v[ii] + 0.5)),
00191 init0[ii], end0[ii]);
00192 } else {
00193 idx[ii]
00194 = IMP::algebra::get_constrained((double)((int)(v[ii] - 0.5)),
00195 init0[ii], end0[ii]);
00196 }
00197 }
00198 #ifdef IMP_DEBUG_PROJECT
00199 std::cout << " v " << v << std::endl;
00200 std::cout << std::endl;
00201 #endif
00202
00203 double alpha = alpha_min;
00204 #ifdef IMP_DEBUG_PROJECT
00205 std::cout << " alpha_min " << alpha_min;
00206 std::cout << " alpha_max " << alpha_max;
00207 std::cout << " initial alpha " << alpha << std::endl;
00208 #endif
00209 do {
00210 for (int ii=0;ii < 3;ii++) {
00211 v_alpha[ii] = (idx[ii] + half_signs[ii] - r[ii]) / direction[ii];
00212 v_diff[ii] = std::abs(alpha - v_alpha[ii]);
00213 }
00214
00215
00216 #ifdef IMP_DEBUG_PROJECT
00217 std::cout << " v_alpha " << v_alpha << std::endl;
00218 std::cout << " v_diff " << v_diff << std::endl;
00219 #endif
00220 double diff_alpha=std::min(std::min(v_diff[0],v_diff[1]),v_diff[2]);
00221
00222
00223
00224 #ifdef IMP_DEBUG_PROJECT
00225 std::cout << "inverted idx " <<
00226 idx[2] << " " << idx[1] << " " << idx[0] << " | ";
00227 std::cout << "m3(inverted idx) = " <<
00228 m3((int)idx[2],(int)idx[1],(int)idx[0]) << std::endl;
00229 #endif
00230 ray_sum += diff_alpha * m3((int)idx[2],(int)idx[1],(int)idx[0]);
00231
00232 for (int ii=0;ii < 3;ii++) {
00233 if (std::abs(diff_alpha-v_diff[ii]) <
00234 equality_tolerance) {
00235 alpha = v_alpha[ii];
00236 idx[ii] += signs[ii];
00237 }
00238 }
00239 #ifdef IMP_DEBUG_PROJECT
00240 std::cout << " alpha =" << alpha << std::endl;
00241 #endif
00242 } while ((alpha_max - alpha) > equality_tolerance);
00243 #ifdef IMP_DEBUG_PROJECT
00244 std::cout << " final alpha =" << alpha << std::endl;
00245 std::cout << " ray_sum =" << ray_sum << std::endl;
00246 #endif
00247 }
00248
00249 m2(i,j) = ray_sum * 0.25;
00250 #ifdef IMP_DEBUG_PROJECT
00251 std::cout << "m2(" << i << "," << j << ")= " << m2(i,j) << std::endl;
00252 #endif
00253 }
00254 }
00255
00256
00257 m2.reindex(orig2D);
00258 m3.reindex(orig3D);
00259 }
00260
00261
00262 template<typename T>
00263 void project_given_direction1(IMP::algebra::Matrix3D<T>& m3,
00264 IMP::algebra::Matrix2D<T>& m2,
00265 const int Ydim,const int Xdim,
00266 IMP::algebra::VectorD<3>& direction,
00267 const IMP::algebra::VectorD<3>& shift,
00268 const double equality_tolerance) {
00269 IMP::algebra::SphericalVector3D sph(direction);
00270 algebra::Rotation3D angles
00271 = algebra::get_rotation_from_fixed_zyz(sph[2],sph[1],0.0);
00272 project_given_rotation1(m3,m2,Ydim,Xdim,angles,shift,equality_tolerance);
00273 };
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 void IMPEMEXPORT project_given_direction(DensityMap& map,
00315 IMP::algebra::Matrix2D<double>& m2,
00316 const int Ydim,const int Xdim,
00317 IMP::algebra::VectorD<3>& direction,
00318 const IMP::algebra::VectorD<3>& shift,
00319 const double equality_tolerance);
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 void IMPEMEXPORT project_given_rotation(DensityMap& map,
00360 IMP::algebra::Matrix2D<double>& m2,
00361 const int Ydim,const int Xdim,
00362 const IMP::algebra::Rotation3D& Rot,
00363 const IMP::algebra::VectorD<3>& shift,
00364 const double equality_tolerance);
00365
00366
00367 IMPEM_END_NAMESPACE
00368
00369 #endif