Xmipp  v3.23.11-Nereus
Classes | Functions | Variables
angular_projection_matching.h File Reference
#include "core/metadata_db.h"
#include "core/multidim_array.h"
#include "core/xmipp_program.h"
#include "core/xmipp_threads.h"
#include "data/polar.h"
#include "data/sampling.h"
Include dependency graph for angular_projection_matching.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Image< T >
 
struct  structThreadRotationallyAlignOneImage
 
class  ProgAngularProjectionMatching
 

Functions

void * threadRotationallyAlignOneImage (void *data)
 

Variables

constexpr int MY_OUPUT_SIZE = 10
 

Function Documentation

◆ threadRotationallyAlignOneImage()

void* threadRotationallyAlignOneImage ( void *  data)

Definition at line 530 of file angular_projection_matching.cpp.

531 {
532  auto * thread_data = (structThreadRotationallyAlignOneImage *) data;
533 
534  // Variables from above
535  size_t thread_id = thread_data->thread_id;
536  ProgAngularProjectionMatching *prm = thread_data->prm;
537  size_t thread_num = prm->threads;
538  MultidimArray<double> *img = thread_data->img;
539  size_t &this_image = thread_data->this_image;
540 
541 
542  int *opt_refno = thread_data->opt_refno;
543  double *opt_psi = thread_data->opt_psi;
544  bool *opt_flip = thread_data->opt_flip;
545  double *maxcorr = thread_data->maxcorr;
546 
547  // Local variables
549  MultidimArray<double> ang, corr;
550  MultidimArray<int> indxCorr;
551  size_t myinit, myfinal, myincr;
552  int refno;
553  bool done_once=false;
554  double mean, stddev;
555  Polar<double> P;
558  Polar_fftw_plans local_plans;
559  size_t imgno = this_image - FIRST_IMAGE;
560 
561 #ifdef TIMING
562 
563  TimeStamp t0,t1,t2;
564  time_config();
565  annotate_time(&t0);
566  annotate_time(&t2);
567 #endif
568 
570  // Precalculate polar transform of each translation
571  // This loop is also threaded
572  myinit = thread_id;
573  myincr = thread_num;
574  for (size_t itrans = myinit; itrans < prm->nr_trans; itrans+=myincr)
575  {
576  P.getPolarFromCartesianBSpline(Maux,prm->Ri,prm->Ro,3,
577  (double)prm->search5d_xoff[itrans],
578  (double)prm->search5d_yoff[itrans]);
579  P.computeAverageAndStddev(mean,stddev);
580  P -= mean; // for normalized cross-correlation coefficient
581  if (itrans == myinit)
582  P.calculateFftwPlans(local_plans);
583  fourierTransformRings(P,prm->fP_img[itrans],local_plans,false);
584  fourierTransformRings(P,prm->fPm_img[itrans],local_plans,true);
585  prm->stddev_img[itrans] = stddev;
586  done_once=true;
587  }
588  // If thread did not have to do any itrans, initialize fftw plans
589  if (!done_once)
590  {
591  P.getPolarFromCartesianBSpline(Maux,prm->Ri,prm->Ro);
592  P.calculateFftwPlans(local_plans);
593  }
594  // Prepare FFTW plan for rotational correlation
595  corr.resize(P.getSampleNoOuterRing());
596  rotAux.local_transformer.setReal(corr);
598 
599  // All threads have to wait until the itrans loop is done
600  barrier_wait(&(prm->thread_barrier));
601 
602 #ifdef TIMING
603 
604  float prepare_img = elapsed_time(t0);
605  float get_refs = 0.;
606  annotate_time(&t0);
607 #endif
608 
609  //pthread_mutex_lock( &debug_mutex );
610  // Switch the order of looping through the references every time.
611  // That way, in case max_nr_refs_in_memory<total_nr_refs
612  // the references read in memory for the previous image
613  // will still be there when processing the next image
614  // Every thread processes a part of the references
615  if (prm->loop_forward_refs)
616  {
617  myinit = 0;
618  myfinal = prm->mysampling.my_neighbors[imgno].size();
619  myincr = +1;
620  }
621  else
622  {
623  myinit = prm->mysampling.my_neighbors[imgno].size() - 1;
624  myfinal = -1;
625  myincr = -1;
626  }
627  // Loop over all relevant "neighbours" (i.e. directions within the search range)
628  //for (int i = myinit; i != myfinal; i+=myincr)
629  for (size_t i = myinit; i != myfinal; i += myincr)
630  {
631  if (i%thread_num == thread_id)
632  {
633 
634 #ifdef DEBUG_THREADS
635  pthread_mutex_lock( &debug_mutex );
636  std::cerr<<" thread_id= "<<thread_id<<" i= "<<i<<" "<<myinit<<" "<<myfinal<<" "<<myincr<<std::endl;
637  pthread_mutex_unlock( &debug_mutex );
638 #endif
639 
640 #ifdef TIMING
641 
642  annotate_time(&t1);
643 #endif
644  // Get pointer to the current reference image
645 #ifdef DEBUG
646 
647  if(prm->mysampling.my_neighbors[imgno][i]==58)
648  {
649  std::cerr << "XXXXpointer_allrefs2refsinmemXXXXXX" <<std::endl;
650  for (std::vector<int>::iterator i = prm->
651  pointer_allrefs2refsinmem.begin();
652  i != prm->pointer_allrefs2refsinmem.end();
653  ++i)
654  std::cerr << *i << std::endl;
655  std::cerr << "XXXXpointer_refsinmem2allrefsXXXXXX" <<std::endl;
656  for (std::vector<int>
657  ::iterator i = prm->pointer_refsinmem2allrefs.begin();
658  i != prm->pointer_refsinmem2allrefs.end();
659  ++i)
660  std::cerr << *i << std::endl;
661  std::cerr <<std::endl;
662 
663  }
664 #endif
665 
666  refno = prm->pointer_allrefs2refsinmem[prm->mysampling.my_neighbors[imgno][i]];
667  if (refno == -1)
668  {
669  // Reference is not stored in memory (anymore): (re-)read from disc
670  prm->getCurrentReference(prm->mysampling.my_neighbors[imgno][i],local_plans);
671  refno = prm->pointer_allrefs2refsinmem[prm->mysampling.my_neighbors[imgno][i]];
672  }
673 
674 
675 #ifdef TIMING
676  get_refs += elapsed_time(t1);
677 #endif
678  //#define DEBUG
679 #ifdef DEBUG
680 
681  std::cerr << "imgno " << imgno <<std::endl;
682  std::cerr<<"Got refno= "<<refno
683  <<" pointer= "<<prm->mysampling.my_neighbors[imgno][i]<<std::endl;
684 #endif
685 
686  // Loop over all 5D-search translations
687  for (size_t itrans = 0; itrans < prm->nr_trans; itrans++)
688  {
689 #ifdef DEBUG
690 
691  std::cerr<< "prm->stddev_ref[refno], prm->stddev_img[itrans]: " <<
692  prm->stddev_ref[refno] << " " <<
693  prm->stddev_img[itrans];
694 #endif
695 
696  MultidimArray<double> allCorr, allAng;
697  allCorr.resizeNoCopy(XSIZE(corr)*2);
698  allAng.resizeNoCopy(XSIZE(corr)*2);
699 
700  // A. Check straight image
701  rotationalCorrelation(prm->fP_img[itrans],
702  prm->fP_ref[refno],
703  ang,rotAux);
704  corr /= prm->stddev_ref[refno] * prm->stddev_img[itrans]; // for normalized ccf
705 
706 
707  memcpy(&dAi(allCorr,0),&dAi(corr,0),XSIZE(corr)*sizeof(double));
708  memcpy(&dAi(allAng,0),&dAi(ang,0),XSIZE(ang)*sizeof(double));
709 
710  rotationalCorrelation(prm->fPm_img[itrans],prm->fP_ref[refno],ang,rotAux);
711  corr /= prm->stddev_ref[refno] * prm->stddev_img[itrans]; // for normalized ccf
712  memcpy(&dAi(allCorr,XSIZE(corr)),&dAi(corr,0),XSIZE(corr)*sizeof(double));
713  memcpy(&dAi(allAng,XSIZE(corr)),&dAi(ang,0),XSIZE(corr)*sizeof(double));
714 
715  size_t nIter = XMIPP_MIN(thread_data->numOrientations,corr.getSize());
716  double bestLastCorr = 99e99;
717  for (size_t n = 0; n < nIter; n++)
718  {
719  for (size_t k = 0; k < XSIZE(allCorr); k++)
720  {
721  if ( (DIRECT_A1D_ELEM(allCorr,k)> maxcorr[n]) && (DIRECT_A1D_ELEM(allCorr,k)<bestLastCorr))
722  {
723  maxcorr[n] = DIRECT_A1D_ELEM(allCorr,k);
724  opt_psi[n] = DIRECT_A1D_ELEM(allAng,k);
725  //FIXME not sure about FIRST_IMAGE
726  opt_refno[n] = prm->mysampling.my_neighbors[imgno][i];/*+FIRST_IMAGE;*/
727  if ( k >= XSIZE(corr))
728  opt_flip[n] = true;
729  else
730  opt_flip[n] = false;
731  }
732  }
733 
734  bestLastCorr = maxcorr[n];
735  }
736 
737 
738 #ifdef DEBUG
739  std::cerr<<"mirror: corr "<<maxcorr;
740  if (opt_flip)
741  std::cerr<<"**";
742  std::cerr<<std::endl;
743 #endif
744 #undef DEBUG
745 
746  }
747  //#define DEBUG
748 #ifdef DEBUG
749  std::cerr << "DEBUG_ROB, imgno:" << imgno << std::endl;
750  std::cerr << "DEBUG_ROB, i:" << i << std::endl;
751  std::cerr << "DEBUG_ROB, prm->mysampling.my_neighbors[imgno][i]:" << prm->mysampling.my_neighbors[imgno][i] << std::endl;
752  std::cerr<<"straight: corr "<<maxcorr<<std::endl;
753 #endif
754 #undef DEBUG
755 
756  }
757  }
758 
759 
760 #ifdef TIMING
761  float all_rot_align = elapsed_time(t0);
762  float total_rot = elapsed_time(t2);
763  std::cerr<<" rotal%% "<<total_rot
764  <<" => prep: "<<prepare_img
765  <<" all_refs: "<<all_rot_align
766  <<" (of which "<<get_refs
767  <<" to get "<< prm->mysampling.my_neighbors[imgno].size()
768  <<" refs for imgno "<<imgno<<" )"
769  <<std::endl;
770 #endif
771  //pthread_mutex_unlock( &debug_mutex );
772  //std::cerr << "DEBUG_JM: threadRotationallyAlignOneImage END" <<std::endl;
773  return nullptr;
774 }
#define dAi(v, i)
void resize(size_t Ndim, size_t Zdim, size_t Ydim, size_t Xdim, bool copy=true)
void getCurrentReference(int refno, Polar_fftw_plans &local_plans)
void resizeNoCopy(const MultidimArray< T1 > &v)
int getSampleNoOuterRing() const
Definition: polar.h:386
void rotationalCorrelation(const Polar< std::complex< double > > &M1, const Polar< std::complex< double > > &M2, MultidimArray< double > &angles, RotationalCorrelationAux &aux)
Definition: polar.cpp:99
void computeAverageAndStddev(double &avg, double &stddev, int mode=FULL_CIRCLES) const
Definition: polar.h:488
double elapsed_time(ProcessorTimeStamp &time, bool _IN_SECS)
Polar< std::complex< double > > * fP_ref
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
void time_config()
#define DIRECT_A1D_ELEM(v, i)
pthread_mutex_t debug_mutex
void setReal(MultidimArray< double > &img)
Definition: xmipp_fftw.cpp:129
void getPolarFromCartesianBSpline(const MultidimArray< T > &M1, int first_ring, int last_ring, int BsplineOrder=3, double xoff=0., double yoff=0., double oversample1=1., int mode1=FULL_CIRCLES)
Definition: polar.h:625
void produceSplineCoefficients(int SplineDegree, MultidimArray< double > &coeffs, const MultidimArray< T > &V1)
#define XSIZE(v)
void FourierTransform(T &v, T1 &V, bool getCopy=true)
Definition: xmipp_fftw.h:166
Polar< std::complex< double > > * fPm_img
#define XMIPP_MIN(x, y)
Definition: xmipp_macros.h:181
std::vector< std::vector< size_t > > my_neighbors
Definition: sampling.h:87
void fourierTransformRings(Polar< double > &in, Polar< std::complex< double > > &out, Polar_fftw_plans &plans, bool conjugated)
Definition: polar.cpp:34
Polar< std::complex< double > > * fP_img
FourierTransformer local_transformer
Definition: polar.h:794
#define FIRST_IMAGE
ProgClassifyCL2D * prm
void annotate_time(TimeStamp *time)
int barrier_wait(barrier_t *barrier)
void calculateFftwPlans(Polar_fftw_plans &out)
Definition: polar.h:708
int * n
size_t TimeStamp
Definition: xmipp_funcs.h:823

Variable Documentation

◆ MY_OUPUT_SIZE

constexpr int MY_OUPUT_SIZE = 10

Definition at line 40 of file angular_projection_matching.h.