52 "For every pair of images in the metadata, find the common line");
54 addParamsLine(
" [-o <file_out=\"commonlines.txt\">] : Output filename");
57 addParamsLine(
" matrix: Text file with the indexes of the corresponding common lines");
58 addParamsLine(
" line_entries: Text file with a line for each matrix entry");
59 addParamsLine(
" [--lpf <f=0.01>] : low pass frequency (0<lpf<0.5)");
60 addParamsLine(
" [--hpf <f=0.35>] : high pass frequency (lpf<hpf<0.5)");
63 addParamsLine(
" [--mem <m=1>] : float number with the memory available in Gb");
64 addParamsLine(
" [--thr <thr=1>] : number of threads for each process");
65 addParamsLine(
" [--scaleDistance] : scale the output distance to 16-bit integers");
66 addParamsLine(
" [--outlierFraction <f=0.25>] : Fraction of expected outliers in the detection of common lines");
76 size_t Ydim, Zdim, Ndim;
83 for (
int i = 0;
i < Nimg *
Nimg;
i++)
89 std::cout <<
"File in: " <<
fn_sel << std::endl <<
"File out: " 90 <<
fn_out << std::endl <<
"Lowpass: " <<
lpf << std::endl
91 <<
"Highpass: " <<
hpf << std::endl <<
"StepAng: " 92 <<
stepAng << std::endl <<
"Memory(Gb): " <<
mem << std::endl
93 <<
"Block size: " <<
Nblock << std::endl <<
"N.Threads: " 102 std::vector<MultidimArray<std::complex<double> > > *
blockRTFs;
111 size_t Ydim,
Xdim, Zdim, Ndim;
118 auto NInsideMask = (int)(
XSIZE(mask) *
YSIZE(mask) - mask.
sum());
122 if (parent->
lpf > 0 && parent->
hpf > 0)
125 Filter.
w1 = parent->
lpf;
126 Filter.
w2 = parent->
hpf;
128 else if (parent->
lpf > 0)
131 Filter.
w1 = parent->
lpf;
133 else if (parent->
hpf > 0)
136 Filter.
w1 = parent->
hpf;
149 for (
size_t objId : SFi.
ids())
151 if ((ii + 1) % parent->
Nthr == master->myThreadID) {
153 I().setXmippOrigin();
169 double meanInside = 0;
174 meanInside /= NInsideMask;
187 for (
size_t i = 0;
i <
YSIZE(RT);
i++) {
198 (*(master->blockRTFs))[ii] = RTFourier;
199 (*(master->blockRTs))[ii] = RT;
207 std::vector<
MultidimArray<std::complex<double> > > &blockRTFs,
214 int jmax = SFi.
size();
217 for (
int j = 0;
j < jmax;
j++)
219 blockRTFs.push_back(dummyF);
220 blockRTs.push_back(dummy);
224 auto * th_ids =
new pthread_t[
Nthr];
226 for (
int nt = 0; nt <
Nthr; nt++) {
228 th_args[nt].
parent =
this;
229 th_args[nt].myThreadID = nt;
230 th_args[nt].SFi = &SFi;
231 th_args[nt].blockRTFs = &blockRTFs;
232 th_args[nt].blockRTs = &blockRTs;
234 (
void *) (th_args + nt));
238 for (
int nt = 0; nt <
Nthr; nt++)
239 pthread_join(*(th_ids + nt),
nullptr);
266 for (
size_t ii = 0; ii <
YSIZE(RTFi) / 2 + 1; ii++) {
270 for (
size_t jj=0; jj<
YSIZE(RTFj); jj++)
299 std::vector<MultidimArray<std::complex<double> > > *
RTFsi;
300 std::vector<MultidimArray<std::complex<double> > > *
RTFsj;
301 std::vector<MultidimArray<double> > *
RTsi;
302 std::vector<MultidimArray<double> > *
RTsj;
311 int blockIsize = master->RTFsi->size();
312 int blockJsize = master->RTFsj->size();
317 for (
int i = 0;
i < blockIsize;
i++) {
318 long int ii = parent->
Nblock * master->i +
i;
319 for (
int j = 0;
j < blockJsize;
j++) {
321 long int jj = parent->
Nblock * master->j +
j;
324 if ((ii * blockJsize + jj + 1) % parent->
Nthr != master->myThreadID)
328 long int idx_ij = ii * parent->
Nimg + jj;
330 *(master->RTFsi), *(master->RTsi),
i,
331 *(master->RTFsj), *(master->RTsj),
j,
332 parent, parent->
CLmatrix[idx_ij], transformer);
335 long int idx_ji = jj * parent->
Nimg + ii;
351 std::vector<MultidimArray<std::complex<double> > > RTFsi, RTFsj;
352 std::vector<MultidimArray<double> > RTsi, RTsj;
359 auto * th_ids =
new pthread_t[
Nthr];
361 for (
int nt = 0; nt <
Nthr; nt++) {
363 th_args[nt].
parent =
this;
364 th_args[nt].myThreadID = nt;
367 th_args[nt].RTFsi = &RTFsi;
368 th_args[nt].RTsi = &RTsi;
371 th_args[nt].RTFsj = &RTFsj;
372 th_args[nt].RTsj = &RTsj;
376 th_args[nt].RTFsj = &RTFsi;
377 th_args[nt].RTsj = &RTsi;
380 (
void *) (th_args + nt));
384 for (
int nt = 0; nt <
Nthr; nt++)
385 pthread_join(*(th_ids + nt),
nullptr);
397 distance.reserve(imax);
399 for (
size_t i=0;
i<imax; ++
i)
400 distance.push_back(
CLmatrix[
i].distanceij);
402 std::sort(distance.begin(),distance.end());
404 std::vector<double>::iterator dbegin=distance.begin();
405 std::vector<double>::iterator low;
406 for (
size_t i=0; i<imax; ++
i)
408 low=std::lower_bound(dbegin, distance.end(),
CLmatrix[
i].distanceij);
417 double minVal = 2, maxVal = -2;
423 double val =
CLmatrix[
i * Nimg +
j].distanceij;
437 for (
int i = 0;
i <
j;
i++) {
438 int ii =
i * Nimg +
j;
444 std::ofstream fh_out;
445 fh_out.open(
fn_out.c_str());
449 for (
int i = 0;
i <
j;
i++) {
450 int ii =
i * Nimg +
j;
452 fh_out << j <<
" " <<
i <<
" ";
455 (maxVal-minVal)) <<
" ";
457 fh_out <<
CLmatrix[ii].distanceij <<
" ";
461 <<
CLmatrix[ii].percentile << std::endl;
469 for (
size_t objId :
SF.
ids())
487 for (
int i = 0;
i <
j;
i++, n++) {
488 int ii =
i * Nimg +
j;
491 double sini, cosi, sinj, cosj;
509 const double maxShiftError=1;
522 for (
int i = 0;
i < maxBlock;
i++)
523 for (
int j = 0;
j < maxBlock;
j++)
525 int numBlock =
i * maxBlock +
j;
565 saveMatrix(
"random_numbers.txt", quaternions);
569 for (
int j = 0;
j <
k; ++
j)
578 saveMatrix(
"random_quaternions.txt", quaternions);
591 fs << std::scientific <<
dMij(matrix,
j,
i) <<
"\t";
615 rotMatrix(0, 1) = 2 *
q1 *
q2 - 2 *
q0 *
q3;
616 rotMatrix(0, 2) = 2 *
q0 *
q2 + 2 *
q1 *
q3;
618 rotMatrix(1, 0) = 2 *
q1 *
q2 + 2 *
q0 *
q3;
620 rotMatrix(1, 2) = -2 *
q0 *
q1 + 2 *
q2 *
q3;
622 rotMatrix(2, 0) = -2 *
q0 *
q2 + 2 *
q1 *
q3;
623 rotMatrix(2, 1) = 2 *
q0 *
q1 + 2 *
q2 *
q3;
631 #define AXIS(var) DVector var(3); var.initZeros() 653 for (
int i = 0;
i < 4; ++
i)
680 DVector X1, Y1, Z1, X2, Y2, Z2;
694 double normZ3 = Z3.
module();
725 double normEv1 = ev1.
module() / normZ3;
726 double normEv2 = ev2.
module() / normZ3;
728 if (normEv1 > 1.0e-12 || normEv2 > 1.0e-12)
730 formatString(
"GCAR:largeErrors: Common line is not common. Error1 = %f, Error2 = %f", normEv1, normEv2));
740 double theta1 = atan2(
YY(c1),
XX(c1)) +
PI;
741 double theta2 = atan2(
YY(c2),
XX(c2)) +
PI;
772 int n = quaternions.
Xdim();
779 for (
int i = 0;
i < n - 1; ++
i)
780 for (
int j =
i + 1;
j <
n; ++
j)
786 dMij(clCorr,
i,
j) = 1.0e-8;
798 double alpha1 =
TWOPI * clI / nRays;
799 double alpha2 =
TWOPI * clJ / nRays;
807 dMij(U, 1, 0) = sin(alpha1);
808 dMij(U, 0, 0) = cos(alpha1);
810 dMij(U, 1, 1) = sin(alpha2);
811 dMij(U, 0, 1) = cos(alpha2);
828 constexpr
long double EPS = 1.0e-13;
830 #define cl(i, j) dMij(clMatrix, k##i, k##j) 836 int k1,
int k2,
int k3,
DMatrix &R)
840 double factor =
TWOPI / nRays;
841 double a = cos(factor * (
cl(3,2)-
cl(3,1)));
842 double b = cos(factor * (
cl(2,3)-
cl(2,1)));
843 double c = cos(factor * (
cl(1,3)-
cl(1,2)));
845 if (1 + 2 * a * b * c - (
POW2(a) +
POW2(b) +
POW2(c))<1.0e-5)
884 #define SET_POINT(x, y) dMij(syncMatrix, x+2*k1, y+2*k2) = dMij(R, x, y) 902 int K = clMatrix.
Xdim();
911 for (
int k1 = 0; k1 < K - 1; ++k1)
913 for (
int k2 = k1 + 1; k2 <
K; ++k2)
927 for (
int k3 = 0; k3 <
K; ++k3)
928 if (k3 != k1 && k3 != k2)
936 std::cerr <<
"DEBUG_JM: triplet: " <<
formatString(
"%d %d, %d", k1, k2, k3) << std::endl;
937 std::cerr <<
"DEBUG_JM: R: " << R << std::endl;
949 int K = sMatrix.
Xdim() / 2;
952 DMatrix U, V, V1(3, K), V2(3, K);
955 sMatrix.
svd(U, W, V);
961 #define SORTED_INDEX(i) dMi(indexes, i) 962 #define SORTED_ELEM(M, i) dMi(M, SORTED_INDEX(i)) 976 for (
int i = 0;
i <
K; ++
i)
979 for (
int j = 0;
j < 3; ++
j)
986 std::cerr <<
"DEBUG_JM: V1: " << V1 << std::endl;
987 std::cerr <<
"DEBUG_JM: V2: " << V2 << std::endl;
993 for (
int k = 0;
k <
K; ++
k)
994 for (
int i = 0;
i < 3; ++
i)
995 for (
int j = 0;
j < 3; ++
j)
1009 int cols[6] = {0, 1, 2, 4, 5, 8};
1010 for (
int i = 0;
i < 6; ++
i)
1013 trunc_equations.
setCol(i, W);
1018 for (
int i = 2;
i < Kx3;
i+=3)
1033 dMij(ATA, 0, 0) =
dMi(ATA_vec, 0);
1034 dMij(ATA, 0, 1) =
dMi(ATA_vec, 1);
1035 dMij(ATA, 0, 2) =
dMi(ATA_vec, 2);
1036 dMij(ATA, 1, 0) =
dMi(ATA_vec, 1);
1037 dMij(ATA, 1, 1) =
dMi(ATA_vec, 3);
1038 dMij(ATA, 1, 2) =
dMi(ATA_vec, 4);
1039 dMij(ATA, 2, 0) =
dMi(ATA_vec, 2);
1040 dMij(ATA, 2, 1) =
dMi(ATA_vec, 4);
1041 dMij(ATA, 2, 2) =
dMi(ATA_vec, 5);
1043 std::cerr <<
"DEBUG_JM: ATA: " << ATA << std::endl;
1049 std::cerr <<
"DEBUG_JM: A: " << A << std::endl;
1055 std::vector<DMatrix> rotations(K);
1058 auto idIt(MD.
ids().begin());
1059 for (
int i = 0;
i <
K; ++
i)
1074 double rot, tilt,
psi;
1082 std::cerr <<
"DEBUG_JM: R" <<
i <<
" : " << R << std::endl;
1084 MD.
write(
"imagesReconstruction.xmd");
std::vector< MultidimArray< double > > * blockRTs
void init_progress_bar(long total)
double lpf
Low pass filter.
#define A2D_ELEM(v, i, j)
void resize(size_t Ndim, size_t Zdim, size_t Ydim, size_t Xdim, bool copy=true)
double getDoubleParam(const char *param, int arg=0)
String outputStyle
Output style.
#define VEC_SWAP(v, i, j, aux)
#define REPORT_ERROR(nerr, ErrormMsg)
void setIndex(int i, double theta)
void quaternionCommonLines(const DMatrix &quaternions, CommonLineInfo &clInfo)
void processBlock(int i, int j)
std::vector< MultidimArray< double > > * RTsi
void setImages(int k1, int k2)
constexpr signed int SMALL_TRIANGLE
void sqrt(Image< double > &op)
void BinaryCircularMask(MultidimArray< int > &mask, double radius, int mode, double x0, double y0, double z0)
Couldn't write to file.
FileName insertBeforeExtension(const String &str) const
#define DIRECT_A2D_ELEM(v, i, j)
void getAndPrepareBlock(int i, std::vector< MultidimArray< std::complex< double > > > &blockRTFs, std::vector< MultidimArray< double > > &blockRTs)
void commonlineMatrixCheat(const DMatrix &quaternions, size_t nRays, DMatrix &clMatrix, DMatrix &clCorr)
void maxIndex(int &jmax) const
void anglesRotationMatrix(size_t nRays, int clI, int clJ, const DVector &Q1, const DVector &Q2, DMatrix &R)
Matrix2D< T > transpose() const
Matrix1D< T > vectorProduct(const Matrix1D< T > &v1, const Matrix1D< T > &v2)
double hpf
High pass filter.
double angj
Angle of the best common line in image j.
std::vector< MultidimArray< std::complex< double > > > * RTFsj
void randomQuaternions(int k, DMatrix &quaternions)
int readApplyGeo(const FileName &name, const MDRow &row, const ApplyGeoParams ¶ms=DefaultApplyGeoParams)
void * threadCompareImages(void *args)
double angi
Angle of the best common line in image 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
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
void resizeNoCopy(int Ydim, int Xdim)
void putRotationMatrix(const DMatrix &R, int k1, int k2, DMatrix &syncMatrix)
#define MAT_ELEM(m, i, j)
void Radon_Transform(const MultidimArray< double > &vol, double rot, double tilt, MultidimArray< double > &RT)
#define FOR_ALL_ELEMENTS_IN_MATRIX1D(v)
#define DIRECT_A1D_ELEM(v, i)
std::vector< CommonLine > CLmatrix
int Nthr
Number of threads.
const char * getParam(const char *param, int arg=0)
void cholesky(const Matrix2D< double > &M, Matrix2D< double > &L)
int tripletRotationMatrix(const DMatrix &clMatrix, size_t nRays, int k1, int k2, int k3, DMatrix &R)
void computeSyncMatrix(const DMatrix &clMatrix, size_t nRays, DMatrix &sMatrix)
int Nmpi
Number of processors.
void quaternionToMatrix(const DVector &q, DMatrix &rotMatrix)
void commonLineTwoImages(std::vector< MultidimArray< std::complex< double > > > &RTFsi, std::vector< MultidimArray< double > > &RTsi, int idxi, std::vector< MultidimArray< std::complex< double > > > &RTFsj, std::vector< MultidimArray< double > > &RTsj, int idxj, ProgCommonLine *parent, CommonLine &result, FourierTransformer &transformer)
void * threadPrepareImages(void *args)
void setCol(size_t j, const Matrix1D< T > &v)
void resize(size_t Xdim, bool copy=true)
void progress_bar(long rlen)
double stepAng
Angular sampling.
Error related to numerical calculation.
void fast_correlation_vector(const MultidimArray< std::complex< double > > &FFT1, const MultidimArray< std::complex< double > > &FFT2, MultidimArray< double > &R, FourierTransformer &transformer)
#define SORTED_ELEM(M, i)
void solveLinearSystem(PseudoInverseHelper &h, Matrix1D< double > &result)
void sort(struct DCEL_T *dcel)
double outlierFraction
Outlier fraction.
T det3x3() const
determinat of 3x3 matrix
double distanceij
Distance between both common lines.
std::vector< MultidimArray< double > > * RTsj
void getCol(size_t j, Matrix1D< T > &v) const
void svd(Matrix2D< double > &U, Matrix1D< double > &W, Matrix2D< double > &V) const
TYPE distance(struct Point_T *p, struct Point_T *q)
void ransacWeightedLeastSquares(WeightedLeastSquaresHelper &h, Matrix1D< double > &result, double tol, int Niter, double outlierFraction, int Nthreads)
FileName fn_sel
input file
constexpr long double EPS
std::vector< MultidimArray< std::complex< double > > > * blockRTFs
void saveMatrix(const char *fn, DMatrix &matrix)
double psi(const double x)
FileName fn_out
output file
void write(const FileName &fn) const
void Euler_matrix2angles(const Matrix2D< double > &A, double &alpha, double &beta, double &gamma, bool homogeneous)
void qualifyCommonLines()
String formatString(const char *format,...)
void resizeNoCopy(int Xdim)
bool checkParam(const char *param)
void aliasRow(const MultidimArray< T > &m, size_t select_row)
constexpr int OUTSIDE_MASK
std::vector< MultidimArray< std::complex< double > > > * RTFsi
void addUsageLine(const char *line, bool verbatim=false)
void initZeros(const MultidimArray< T1 > &op)
void initGaussian(int Ydim, int Xdim, double op1=0., double op2=1.)
void rotationsFromSyncMatrix(const DMatrix &sMatrix)
double percentile
Percentile (good common lines have very high percentiles)
int getIntParam(const char *param, int arg=0)
void generateMask(MultidimArray< double > &v)
void getRow(size_t i, Matrix1D< T > &v) const
void resize(size_t Ydim, size_t Xdim, bool noCopy=false)
void addParamsLine(const String &line)
void statisticsAdjust(U avgF, U stddevF)
void applyMaskSpace(MultidimArray< double > &v)
bool scaleDistance
Scale output measure.