32 double hires,
double lores,
double shiftlimit,
double mindist,
33 double threshold,
int maxhits,
int refinepeaks,
50 mindist, threshold, maxhits, refinepeaks);
63 View view,
double hires,
double lores,
double shiftlimit,
64 double shiftlimitz,
double shiftlimitxy,
double mindist,
int refinepeaks,
96 if ( shiftlimit == 0 && shiftlimitz == 0 && shiftlimitxy == 0) {
99 peak->
fom[0] = (*pcc)[i];
104 shiftlimit, shiftlimitz, shiftlimitxy,
105 mindist, -1, -1, refinepeaks);
143 double alpha_step,
double theta_step,
double phi_step,
144 double alpha_limit,
double thetaphi_limit,
145 double hires,
double lores,
double shiftlimit,
double mindist,
147 int refinepeaks,
Bstring ccmax_file)
149 if ( !pmask ) { cerr <<
"Warning: No reciprocal space mask given." << endl; }
150 if ( !pmask2 ) { cerr <<
"Warning: No real space mask given." << endl; }
153 if ( shiftlimit < 0 ) {
155 cout << endl <<
"Shift limit not specified, searching the whole reconstruction." << endl;
189 View* allviews = NULL;
190 if ( sym->
point < 102 ) {
191 allviews =
views_within_limits(refview, theta_step, phi_step, alpha_step, thetaphi_limit, alpha_limit);
200 cout <<
"Searching for the template in the map:" << endl;
201 cout <<
"Map: " << p->
file_name() << endl;
202 cout <<
"Template: " << ptemp->
file_name() << endl;
203 cout <<
"Map size: " << p->
size() << endl;
204 cout <<
"Template size: " << ptemp->
size() << endl;
205 cout <<
"Template origin: " << setprecision(4) << origin << endl;
206 cout <<
"Binning: " << bin << endl;
207 cout <<
"Search radius: " << shiftlimit << endl;
208 cout <<
"Minimum separation distance: " << mindist << endl;
210 cout <<
"Real space mask: " << pmask2->
file_name() << endl;
212 cout <<
"Reciprocal space mask: " << pmask->
file_name() << endl;
213 cout <<
"Alpha step size: " << alpha_step*180.0/
M_PI <<
" degrees" << endl;
214 cout <<
"Theta step size: " << theta_step*180.0/
M_PI <<
" degrees" << endl;
215 cout <<
"Phi step size: " << phi_step*180.0/
M_PI <<
" degrees" << endl;
216 cout <<
"Limit in alpha: " << alpha_limit*180.0/
M_PI <<
" degrees" << endl;
218 cout <<
"Symmetry: " << sym->
label << endl;
220 cout <<
"Limit in theta & phi: " << thetaphi_limit*180.0/
M_PI <<
" degrees" << endl;
221 cout <<
"Number of views: " << nviews << endl;
222 if ( ccmax_file.
length() )
223 cout <<
"Cross correlation map: " << ccmax_file << endl;
233 for ( i=0, v=allviews; v; v=v->
next, i++ ) view_arr[i] = *v;
242 cout <<
"#\tViewX\tViewY\tViewZ\tViewA\tNpart" << endl;
246 dispatch_queue_t myq = dispatch_queue_create(NULL, NULL);
247 dispatch_apply(nviews, dispatch_get_global_queue(0, 0), ^(
size_t i){
248 part_arr[i] =
img_search_view(p, ptemp, pmask, view_arr[i], hires, lores,
249 shiftlimit, mindist, threshold, maxhits, refinepeaks,
planf,
planb);
251 dispatch_sync(myq, ^{
253 cout << i+1 <<
tab << view_arr[i] <<
tab << np << endl << flush;
257 #pragma omp parallel for
258 for (
long i=0; i < nviews; i++ ) {
259 part_arr[i] =
img_search_view(p, ptemp, pmask, view_arr[i], hires, lores,
260 shiftlimit, mindist, threshold, maxhits, refinepeaks,
planf,
planb);
265 cout << i+1 <<
tab << view_arr[i] <<
tab << np << endl << flush;
274 cout <<
"Transferring the particles found to the reconstruction" << endl;
275 for ( i=0; i < nviews; i++ )
if ( part_arr[i] ) {
276 if ( !rec->
part ) rec->
part = part = part_arr[i];
277 else part->
next = part_arr[i];
278 for ( ; part->
next; part = part->
next ) ;
283 for ( ipart=0, part = rec->
part; part; part = part->
next ) {
285 part->
ori = origin * bin;
286 translate = origin - part->
loc;
288 part->
loc = translate * bin;
290 cout << ipart <<
tab << setprecision(2)
291 << part->
loc <<
tab << setprecision(4) << part->
view <<
tab << part->
fom[0] << endl;
336 double alpha_step_orig,
double theta_step_orig,
double phi_step_orig,
337 double alpha_limit_orig,
double thetaphi_limit_orig,
338 double hires,
double lores,
double shiftlimit_orig,
339 double shiftlimitz_orig,
double shiftlimitxy_orig,
double mindist,
342 if ( !pmask ) { cerr <<
"Warning: No reciprocal space mask given." << endl; }
343 if ( !pmask2 ) { cerr <<
"Warning: No real space mask given." << endl; }
346 if ( shiftlimit_orig < 0 ) {
348 cout << endl <<
"Shift limit not specified, using half the size of the template." << endl;
370 if ( sym->
point > 101 && iters > 1) {
372 cerr <<
"Warning: Symmetry selected, running only one iteration." << endl;
375 if (
verbose &
VERB_RESULT ){ cout << endl <<
"Padding the template to a larger box." << endl;}
377 double maxshift = shiftlimit_orig + shiftlimitz_orig + shiftlimitxy_orig;
388 ptemp->
origin(origin+translate);
395 if (
verbose &
VERB_RESULT ){ cout << endl <<
"Padding the real space mask to a larger box." << endl;}
400 cout << endl <<
"Refining the template in the map around the starting view:" << endl;
401 cout <<
"Map: " << p->
file_name() << endl;
402 cout <<
"Template: " << ptemp->
file_name() << endl;
403 cout <<
"Map size: " << p->
size() << endl;
404 cout <<
"Template size: " << ptemp->
size() << endl;
405 cout <<
"Template origin: " << setprecision(4) << ptemp->
image->
origin() << endl;
406 cout <<
"Binning: " << bin << endl;
407 cout <<
"Maximum translation: " << shiftlimit_orig <<
" pixels" << endl;
408 if ( shiftlimitz_orig > 0 )
409 cout <<
"Additional shift along view: " << shiftlimitz_orig << endl;
410 if ( shiftlimitxy_orig > 0 )
411 cout <<
"Additional shift orthogonal view: " << shiftlimitxy_orig << endl;
413 cout <<
"Real space mask: " << pmask2->
file_name() << endl;
415 cout <<
"Reciprocal space mask: " << pmask->
file_name() << endl;
416 cout <<
"Alpha step size: " << alpha_step_orig*180.0/
M_PI <<
" degrees" << endl;
417 cout <<
"Theta step size: " << theta_step_orig*180.0/
M_PI <<
" degrees" << endl;
418 cout <<
"Phi step size: " << phi_step_orig*180.0/
M_PI <<
" degrees" << endl;
419 cout <<
"Limit in alpha: " << alpha_limit_orig*180.0/
M_PI <<
" degrees" << endl;
420 if ( sym->
point < 102 )
421 cout <<
"Limit in theta & phi: " << thetaphi_limit_orig*180.0/
M_PI <<
" degrees" << endl;;
425 long iter, nviews, i, ibest(0);
427 double shiftlimit, shiftlimitz, shiftlimitxy;
428 double alpha_step,theta_step,phi_step;
429 double alpha_limit, thetaphi_limit;
434 View* allviews = NULL;
441 for ( part = rec->
part; part; part = part->
next )
if ( part->
sel > 0 ) {
444 cout <<
"Particle: " << part->
id <<
" of " << npart << endl;
447 cout <<
"Starting location: " << part->
loc << endl;
448 cout <<
"Starting view: " << part->
view << endl;
453 alpha_step = alpha_step_orig;
454 theta_step = theta_step_orig;
455 phi_step = phi_step_orig;
456 thetaphi_limit = thetaphi_limit_orig;
457 alpha_limit = alpha_limit_orig;
458 shiftlimit = shiftlimit_orig;
459 shiftlimitxy = shiftlimitxy_orig;
460 shiftlimitz = shiftlimitz_orig;
463 bestview = part->
view;
465 for ( iter=1; iter <= iters; iter++) {
468 if ( sym->
point < 102 ) {
469 allviews =
views_within_limits(bestview, theta_step, phi_step, alpha_step, thetaphi_limit, alpha_limit);
477 cout <<
"Iteration: " << iter <<
" of " << iters << endl;
478 cout <<
"Alpha step size: " << alpha_step*180.0/
M_PI <<
" degrees" << endl;
479 cout <<
"Theta step size: " << theta_step*180.0/
M_PI <<
" degrees" << endl;
480 cout <<
"Phi step size: " << phi_step*180.0/
M_PI <<
" degrees" << endl;
481 cout <<
"Limit in alpha: " << alpha_limit*180.0/
M_PI <<
" degrees" << endl;
482 if ( sym->
point < 102 )
483 cout <<
"Limit in theta & phi: " << thetaphi_limit*180.0/
M_PI <<
" degrees" << endl;;
484 cout <<
"Number of views: " << nviews << endl;
489 if (
verbose &
VERB_FULL ){ cout << endl <<
"Cropping the map." << endl << endl;}
490 translate = -part->
loc;
491 if ( bin[0] ) { translate /= bin; }
492 translate = translate + neworigin;
504 for ( i=0, v=allviews; v; v=v->
next, i++ ) view_arr[i] = *v;
509 dispatch_apply(nviews, dispatch_get_global_queue(0, 0), ^(
size_t i){
510 part_arr[i] =
img_refine_view(pcrop, ptemp, pmask, pmask2, view_arr[i],
511 hires, lores, shiftlimit, shiftlimitz, shiftlimitxy,
515 #pragma omp parallel for
516 for ( i=0; i < nviews; i++ ) {
517 part_arr[i] =
img_refine_view(pcrop, ptemp, pmask, pmask2, view_arr[i],
518 hires, lores, shiftlimit, shiftlimitz, shiftlimitxy,
523 for ( i=0; i < nviews; i++ ) {
524 if ( part_arr[i]->fom[0] > best_cc ) {
525 best_cc = part_arr[i]->
fom[0];
527 bestview = part_arr[i]->
view;
528 bestshift = part_arr[i]->
loc;
536 translate = -bestshift;
554 if ( bin[0] ) { translate *= bin; }
557 part->
view = bestview;
558 part->
loc = part->
loc + translate;
559 part->
fom[0] = best_cc;
564 cout <<
" \r" << flush << iter <<
tab << ibest+1 <<
tab << part->
loc <<
tab << bestview <<
tab << best_cc << endl;
602 double shift_along,
double shift_orthogonal,
double mindist,
603 double threshold,
int maxhits,
int refinepeaks)
616 if ( (peakcc >= threshold) || (threshold < 0) ) {
623 peak->
fom[0] = peakcc;
626 cout << hits <<
tab << peak->
loc <<
tab << view <<
tab << peakcc << endl;
629 cout <<
"Found a cross-correlation peak:\t" << peak->
loc <<
tab << peakcc << endl;
632 if (threshold < 0) {
break; }
635 if (maxhits > 0 && hits >= maxhits) {
break; }
641 }
while ( peakcc >= threshold );
647 cout <<
"No cross-correlation peaks found above the threshold (the maximum was " << peakcc <<
")" << endl;
669 double shift_along,
double shift_orthogonal)
673 if ( shift < 0 ) shift = 1e30;
677 double x2, y2, z2, r2;
678 double value(0), max;
679 double shift2 = shift*shift;
689 if ( shift_along > 0 ) {
690 endpoint1 =
Vector3<double>(view[0] * shift_along, view[1] * shift_along, view[2] * shift_along);
691 endpoint2 =
Vector3<double>(-view[0] * shift_along, -view[1] * shift_along, -view[2] * shift_along);
695 for ( z=0; z<p->
sizeZ(); z++ ) {
697 if ( z2 < -zh ) z2 += p->
sizeZ();
698 if ( z2 > zh ) z2 -= p->
sizeZ();
700 for ( y=0; y<p->
sizeY(); y++ ) {
702 if ( y2 < -yh ) y2 += p->
sizeY();
703 if ( y2 > yh ) y2 -= p->
sizeY();
705 for ( x=0; x<p->
sizeX(); x++, i++ ) {
707 if ( x2 < -xh ) x2 += p->
sizeX();
708 if ( x2 > xh ) x2 -= p->
sizeX();
712 if ( shift_along > 0 ) {
717 else if ( shift_orthogonal > 0 ) {
724 r2 = x2*x2 + y2*y2 + z2*z2;
726 if ( r2 > shift2 ) value = 0;
728 if ( r2 > shift2 ) value = 0;
738 cout <<
"Peak: " << peak <<
tab << max << endl;
765 t = (p-v).
scalar(line) / l2;
768 if ( t < 0 ) { closest_point = v; }
771 else if ( t > 1 ) { closest_point = w; }
778 return closest_point;
804 t = (p-v).
scalar(line) / l2;
807 if ( t < 0 ) { closest_point = v; }
810 else if ( t > 1 ) { closest_point = w; }
817 return (closest_point-p).length2();
847 double radius2 = radius * radius;
850 c = q[0]*view[0] + q[1]*view[1] + q[2]*view[2];
852 a = (view[0] * p[0] + view[1] * p[1] + view[2] * p[2]) - c;
853 pprime =
Vector3<double>(p[0] - a * view[0], p[1] - a * view[1], p[2] - a * view[2]);
855 pprime_to_q2 = (pprime-q).
length2();
856 p_to_pprime2 = (p-pprime).
length2();
858 if (pprime_to_q2 > radius2) {
860 pprime_to_q = (pprime-q).
length();
861 pprime_to_c2 = (pprime_to_q - radius) * (pprime_to_q - radius);
863 p_to_c2 = pprime_to_c2 + p_to_pprime2;
913 double PXsum=0, PXsumsq=0, PXsumcube=0;
914 double PYsum=0, PYsumsq=0, PYsumcube=0;
915 double PZsum=0, PZsumsq=0, PZsumcube=0;
917 double PXYsum=0, PXZsum=0, PYZsum=0;
918 double PX2Ysum=0, PX2Zsum=0, PY2Xsum=0, PY2Zsum=0, PZ2Xsum=0, PZ2Ysum=0;
923 for ( p = part; p; p = p-> next ) {
931 PXsumcube += x * x * x;
935 PYsumcube += y * y * y;
939 PZsumcube += z * z * z;
945 PX2Ysum += x * x * y;
946 PX2Zsum += x * x * z;
947 PY2Xsum += y * y * x;
948 PY2Zsum += y * y * z;
949 PZ2Xsum += z * z * x;
950 PZ2Ysum += z * z * y;
953 double Xn = PXsum/Pnpoints;
954 double Xn2 = PXsumsq/Pnpoints;
955 double Xn3 = PXsumcube/Pnpoints;
956 double Yn = PYsum/Pnpoints;
957 double Yn2 = PYsumsq/Pnpoints;
958 double Yn3 = PYsumcube/Pnpoints;
959 double Zn = PZsum/Pnpoints;
960 double Zn2 = PZsumsq/Pnpoints;
961 double Zn3 = PZsumcube/Pnpoints;
963 double XY = PXYsum/Pnpoints;
964 double XZ = PXZsum/Pnpoints;
965 double YZ = PYZsum/Pnpoints;
966 double X2Y = PX2Ysum/Pnpoints;
967 double X2Z = PX2Zsum/Pnpoints;
968 double Y2X = PY2Xsum/Pnpoints;
969 double Y2Z = PY2Zsum/Pnpoints;
970 double Z2X = PZ2Xsum/Pnpoints;
971 double Z2Y = PZ2Ysum/Pnpoints;
974 double F0 = Xn2 + Yn2 + Zn2;
976 double F2 = -8.0*(Xn3 + Y2X + Z2X);
977 double F3 = -8.0*(X2Y + Yn3 + Z2Y);
978 double F4 = -8.0*(X2Z + Y2Z + Zn3);
989 double QS = A2 + B2 + C2;
990 double QB = - 2*(A*Xn + B*Yn + C*Zn);
993 double Rsq = F0 + QB + QS;
996 double Q0 = 0.5*(QS - Rsq);
998 double Q2 = 8*( QS - Rsq + QB + F0 );
999 double aA,aB,aC,nA,nB,nC,dA,dB,dC;
1007 aA = Q2 + 16*(A2 - 2*A*Xn + Xn2);
1008 aB = Q2 + 16*(B2 - 2*B*Yn + Yn2);
1009 aC = Q2 + 16*(C2 - 2*C*Zn + Zn2);
1010 aA = (aA == 0) ? 1.0 : aA;
1011 aB = (aB == 0) ? 1.0 : aB;
1012 aC = (aC == 0) ? 1.0 : aC;
1016 nA = A - ((F2 + 16*( B*XY + C*XZ + Xn*(-A2 - Q0) + A*(Xn2 + Q1 - C*Zn - B*Yn) ) )/aA);
1017 nB = B - ((F3 + 16*( A*XY + C*YZ + Yn*(-B2 - Q0) + B*(Yn2 + Q1 - A*Xn - C*Zn) ) )/aB);
1018 nC = C - ((F4 + 16*( A*XZ + B*YZ + Zn*(-C2 - Q0) + C*(Zn2 + Q1 - A*Xn - B*Yn) ) )/aC);
1024 if( (dA*dA + dB*dB + dC*dC) <= Nstop ){
break; }
1034 QB = - 2*(A*Xn + B*Yn + C*Zn);
1036 Q0 = 0.5*(QS - Rsq);
1038 Q2 = 8*( QS - Rsq + QB + F0 );
1041 double R = sqrt(Rsq);