Xmipp  v3.23.11-Nereus
compare_views.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar coss@cnb.csic.es
4  * David Herreros Calero dherreros@cnb.csic.es
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19  * 02111-1307 USA
20  *
21  * All comments concerning this program package may be sent to the
22  * e-mail address 'xmipp@cnb.uam.es'
23  ***************************************************************************/
24 
25 #include <fstream>
26 #include <iterator>
27 #include <numeric>
28 #include <algorithm>
29 #include "data/cpu.h"
30 #include "compare_views.h"
31 #include "data/projection.h"
33 
34 // Params definition =======================================================
35 // -i ---> V2 (paper) / -r --> V1 (paper)
37  addUsageLine("Compute the deformation that properly fits two volumes using spherical harmonics");
38  addParamsLine(" -v1 <volume> : First volume to compare");
39  addParamsLine(" -v2 <volume> : Second volume to compare");
40  addParamsLine(" [-o <image=\"\">] : Output correlation image");
41  addParamsLine(" [--degstep <d=5.0>] : Degrees step size for rot and tilt angles");
42  addParamsLine(" [--thr <N=-1>] : Maximal number of the processing CPU threads");
43  addExampleLine("xmipp_compare_views -v1 vol1.vol -v2 vol2.vol -o corr_img.xmp");
44 }
45 
46 // Read arguments ==========================================================
48  std::string aux;
49  fnVol1 = getParam("-v1");
50  fnVol2 = getParam("-v2");
51  fnImgOut = getParam("-o");
52  degstep = getDoubleParam("--degstep");
53 
54  if (fnImgOut=="")
55  fnImgOut="Rot_tilt_corr_map.xmp";
56 
57  V1.read(fnVol1);
58  V1().setXmippOrigin();
59  V2.read(fnVol2);
60  V2().setXmippOrigin();
61 
62  // Update degstep to have evenly spaced points in the interval [0,180]
63  degstep = 360. / ROUND(360./degstep);
64 
65  int size_rot = 360./degstep;
66  int size_tlt = 180./degstep;
67  tilt_v.resize(size_tlt + 1);
68  rot_v.resize(size_rot + 1);
69  CorrImg().initZeros(size_rot + 1, size_tlt + 1);
70  CorrImg().setXmippOrigin();
71 
72  std::generate(tilt_v.begin(), tilt_v.end(), [&, n = -degstep] () mutable { return n+=degstep; });
73  std::generate(rot_v.begin(), rot_v.end(), [&, n = -degstep] () mutable { return n+=degstep; });
74 
75  int threads = getIntParam("--thr");
76  if (0 >= threads) {
77  threads = CPU::findCores();
78  }
79  m_threadPool.resize(threads);
80 }
81 
82 // Show ====================================================================
84  if (verbose==0)
85  return;
86  std::cout
87  << "First volume: " << fnVol1 << std::endl
88  << "Second volume: " << fnVol2 << std::endl
89  << "Output image: " << fnImgOut << std::endl
90  << "Degree step: " << degstep << std::endl
91  ;
92 
93 }
94 
96 {
97  Projection P1, P2;
98  auto &mCorrImg = CorrImg();
99  auto &mV1 = V1();
100  auto &mV2 = V2();
101  auto &mP1 = P1();
102  auto &mP2 = P2();
103  int size_x = XSIZE(mV1);
104  int size_y = YSIZE(mV1);
105  auto rot = rot_v[i];
106  for (int j = 0; j < tilt_v.size(); j++)
107  {
108  projectVolume(mV1, P1, size_y, size_x, rot, tilt_v[j], 0.);
109  projectVolume(mV2, P2, size_y, size_x, rot, tilt_v[j], 0.);
110  DIRECT_A2D_ELEM(mCorrImg, i, j) = correlationIndex(mP1, mP2);
111  }
112 }
113 
114 // Run =====================================================================
116  auto futures = std::vector<std::future<void>>();
117  futures.reserve(V1().zdim);
118 
119  auto routine = [this](int thrId, int i) {
121  };
122 
123  for (int i=0; i<rot_v.size(); i++)
124  {
125  futures.emplace_back(m_threadPool.push(routine, i));
126  }
127 
128  for (auto &f : futures) {
129  f.get();
130  }
131 
133 }
#define YSIZE(v)
double getDoubleParam(const char *param, int arg=0)
Image< double > V1
Images.
Definition: compare_views.h:53
auto push(F &&f, Rest &&... rest) -> std::future< decltype(f(0, rest...))>
Definition: ctpl.h:152
#define DIRECT_A2D_ELEM(v, i, j)
void write(const FileName &name="", size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE, CastWriteMode castMode=CW_CAST, int _swapWrite=0)
double correlationIndex(const MultidimArray< T > &x, const MultidimArray< T > &y, const MultidimArray< int > *mask=NULL, MultidimArray< double > *Contributions=NULL)
FileName fnImgOut
Output corelation image.
Definition: compare_views.h:46
static unsigned findCores()
Definition: cpu.h:41
void resize(int nThreads)
Definition: ctpl.h:70
void computeCorrImage(int i)
Compute corr image.
#define i
Image< double > CorrImg
Definition: compare_views.h:53
const char * getParam(const char *param, int arg=0)
std::vector< double > rot_v
Definition: compare_views.h:56
double * f
Image< double > V2
Definition: compare_views.h:53
#define XSIZE(v)
void defineParams()
Define params.
void addExampleLine(const char *example, bool verbatim=true)
#define ROUND(x)
Definition: xmipp_macros.h:210
void projectVolume(FourierProjector &projector, Projection &P, int Ydim, int Xdim, double rot, double tilt, double psi, const MultidimArray< double > *ctf)
FileName fnVol1
Volumes to compare.
Definition: compare_views.h:42
int verbose
Verbosity level.
void show()
Show.
std::vector< double > tilt_v
Rot and tilt vectors.
Definition: compare_views.h:56
#define j
double degstep
Degree step.
Definition: compare_views.h:49
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
void addUsageLine(const char *line, bool verbatim=false)
int getIntParam(const char *param, int arg=0)
int * n
void addParamsLine(const String &line)
void readParams()
Read arguments from command line.