// Computes sum of the values of a unitary blob on grid points. The blob is // supposed to be at the origin of the absolute coordinate system double sum_blob_SimpleGrid(const struct blobtype &blob, const SimpleGrid &grid, const Matrix2D<double> *D) { SPEED_UP_temps012; Matrix1D<double> gr(3), ur(3), corner1(3), corner2(3); double actual_radius; int i, j, k; double sum = 0.0; // Compute the limits of the blob in the grid coordinate system grid.universe2grid(vectorR3(-blob.radius, -blob.radius, -blob.radius), corner1); grid.universe2grid(vectorR3(blob.radius, blob.radius, blob.radius), corner2); if (D != NULL) box_enclosing(corner1, corner2, *D, corner1, corner2); // Compute the sum in the points inside the grid // The integer part of the vectors is taken for not picking points // just in the border of the blob, which we know they are 0. for (i = (int)XX(corner1); i <= (int)XX(corner2); i++) for (j = (int)YY(corner1); j <= (int)YY(corner2); j++) for (k = (int)ZZ(corner1); k <= (int)ZZ(corner2); k++) { VECTOR_R3(gr, i, j, k); grid.grid2universe(gr, ur); if (D != NULL) M3x3_BY_V3x1(ur, *D, ur); actual_radius = ur.module(); if (actual_radius < blob.radius) sum += kaiser_value(actual_radius, blob.radius, blob.alpha, blob.order); } return sum; }
// Apply transformation --------------------------------------------------- void applyTransformation(const MultidimArray<double> &V2, MultidimArray<double> &Vaux, double *p) { Matrix1D<double> r(3); Matrix2D<double> A, Aaux; double greyScale = p[0]; double greyShift = p[1]; double rot = p[2]; double tilt = p[3]; double psi = p[4]; double scale = p[5]; ZZ(r) = p[6]; YY(r) = p[7]; XX(r) = p[8]; Euler_angles2matrix(rot, tilt, psi, A, true); translation3DMatrix(r,Aaux); A = A * Aaux; scale3DMatrix(vectorR3(scale, scale, scale),Aaux); A = A * Aaux; applyGeometry(LINEAR, Vaux, V2, A, IS_NOT_INV, WRAP); if (greyScale!=1 || greyShift!=0) FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(Vaux) DIRECT_MULTIDIM_ELEM(Vaux,n)=DIRECT_MULTIDIM_ELEM(Vaux,n)*greyScale+greyShift; }
long int Residue::addAtom(std::string atomname, double x, double y, double z, double occ, double bfac) { Atom atom(atomname); atom.coords = vectorR3(x,y,z); atom.occupancy = occ; atom.bfactor = bfac; long int result = atoms.size(); atoms.push_back(atom); return result; }
/* Sum spline on a grid ---------------------------------------------------- */ double sum_spatial_Bspline03_SimpleGrid(const SimpleGrid &grid) { Matrix1D<double> gr(3), ur(3), corner1(3), corner2(3); int i, j, k; double sum = 0.0; // Compute the limits of the spline in the grid coordinate system grid.universe2grid(vectorR3(-2.0, -2.0, -2.0), corner1); grid.universe2grid(vectorR3(2.0, 2.0, 2.0), corner2); // Compute the sum in the points inside the grid // The integer part of the vectors is taken for not picking points // just in the border of the spline, which we know they are 0. for (i = (int)XX(corner1); i <= (int)XX(corner2); i++) for (j = (int)YY(corner1); j <= (int)YY(corner2); j++) for (k = (int)ZZ(corner1); k <= (int)ZZ(corner2); k++) { VECTOR_R3(gr, i, j, k); grid.grid2universe(gr, ur); sum += spatial_Bspline03(ur); } return sum; }
void run () { mask.allowed_data_types = INT_MASK; // Main program ========================================================= params.V1.read(fn1); params.V1().setXmippOrigin(); params.V2.read(fn2); params.V2().setXmippOrigin(); // Initialize best_fit double best_fit = 1e38; Matrix1D<double> best_align(8); bool first = true; // Generate mask if (mask_enabled) { mask.generate_mask(params.V1()); params.mask_ptr = &(mask.get_binary_mask()); } else params.mask_ptr = NULL; // Exhaustive search if (!usePowell && !useFRM) { // Count number of iterations int times = 1; if (!tell) { if (grey_scale0 != grey_scaleF) times *= FLOOR(1 + (grey_scaleF - grey_scale0) / step_grey); if (grey_shift0 != grey_shiftF) times *= FLOOR(1 + (grey_shiftF - grey_shift0) / step_grey_shift); if (rot0 != rotF) times *= FLOOR(1 + (rotF - rot0) / step_rot); if (tilt0 != tiltF) times *= FLOOR(1 + (tiltF - tilt0) / step_tilt); if (psi0 != psiF) times *= FLOOR(1 + (psiF - psi0) / step_psi); if (scale0 != scaleF) times *= FLOOR(1 + (scaleF - scale0) / step_scale); if (z0 != zF) times *= FLOOR(1 + (zF - z0) / step_z); if (y0 != yF) times *= FLOOR(1 + (yF - y0) / step_y); if (x0 != xF) times *= FLOOR(1 + (xF - x0) / step_x); init_progress_bar(times); } else std::cout << "#grey_factor rot tilt psi scale z y x fitness\n"; // Iterate int itime = 0; int step_time = CEIL((double)times / 60.0); Matrix1D<double> r(3); Matrix1D<double> trial(9); for (double grey_scale = grey_scale0; grey_scale <= grey_scaleF ; grey_scale += step_grey) for (double grey_shift = grey_shift0; grey_shift <= grey_shiftF ; grey_shift += step_grey_shift) for (double rot = rot0; rot <= rotF ; rot += step_rot) for (double tilt = tilt0; tilt <= tiltF ; tilt += step_tilt) for (double psi = psi0; psi <= psiF ; psi += step_psi) for (double scale = scale0; scale <= scaleF ; scale += step_scale) for (ZZ(r) = z0; ZZ(r) <= zF ; ZZ(r) += step_z) for (YY(r) = y0; YY(r) <= yF ; YY(r) += step_y) for (XX(r) = x0; XX(r) <= xF ; XX(r) += step_x) { // Form trial vector trial(0) = grey_scale; trial(1) = grey_shift; trial(2) = rot; trial(3) = tilt; trial(4) = psi; trial(5) = scale; trial(6) = ZZ(r); trial(7) = YY(r); trial(8) = XX(r); // Evaluate double fit = fitness(MATRIX1D_ARRAY(trial)); // The best? if (fit < best_fit || first) { best_fit = fit; best_align = trial; first = false; if (tell) std::cout << "Best so far\n"; } // Show fit if (tell) std::cout << trial << " " << fit << std::endl; else if (++itime % step_time == 0) progress_bar(itime); } if (!tell) progress_bar(times); } else if (usePowell) { // Use Powell optimization Matrix1D<double> x(9), steps(9); double fitness; int iter; steps.initConstant(1); if (onlyShift) steps(0)=steps(1)=steps(2)=steps(3)=steps(4)=steps(5)=0; if (params.alignment_method == COVARIANCE) steps(0)=steps(1)=0; x(0)=grey_scale0; x(1)=grey_shift0; x(2)=rot0; x(3)=tilt0; x(4)=psi0; x(5)=scale0; x(6)=z0; x(7)=y0; x(8)=x0; powellOptimizer(x,1,9,&wrapperFitness,NULL,0.01,fitness,iter,steps,true); best_align=x; best_fit=fitness; first=false; } else if (useFRM) { String scipionPython; initializeScipionPython(scipionPython); PyObject * pFunc = getPointerToPythonFRMFunction(); double rot,tilt,psi,x,y,z,score; Matrix2D<double> A; alignVolumesFRM(pFunc, params.V1(), params.V2(), Py_None, rot,tilt,psi,x,y,z,score,A,maxShift,maxFreq,params.mask_ptr); best_align.initZeros(9); best_align(0)=1; // Gray scale best_align(1)=0; // Gray shift best_align(2)=rot; best_align(3)=tilt; best_align(4)=psi; best_align(5)=1; // Scale best_align(6)=z; best_align(7)=y; best_align(8)=x; best_fit=-score; } if (!first) std::cout << "The best correlation is for\n" << "Scale : " << best_align(5) << std::endl << "Translation (X,Y,Z) : " << best_align(8) << " " << best_align(7) << " " << best_align(6) << std::endl << "Rotation (rot,tilt,psi): " << best_align(2) << " " << best_align(3) << " " << best_align(4) << std::endl << "Best grey scale : " << best_align(0) << std::endl << "Best grey shift : " << best_align(1) << std::endl << "Fitness value : " << best_fit << std::endl; Matrix1D<double> r(3); XX(r) = best_align(8); YY(r) = best_align(7); ZZ(r) = best_align(6); Matrix2D<double> A,Aaux; Euler_angles2matrix(best_align(2), best_align(3), best_align(4), A, true); translation3DMatrix(r,Aaux); A = A * Aaux; scale3DMatrix(vectorR3(best_align(5), best_align(5), best_align(5)),Aaux); A = A * Aaux; if (verbose!=0) std::cout << "xmipp_transform_geometry will require the following values" << "\n Angles: " << best_align(2) << " " << best_align(3) << " " << best_align(4) << "\n Shifts: " << A(0,3) << " " << A(1,3) << " " << A(2,3) << std::endl; if (apply) { applyTransformation(params.V2(),params.Vaux(),MATRIX1D_ARRAY(best_align)); params.V2()=params.Vaux(); params.V2.write(fnOut); } }
Matrix1D<float> vectorR3(double xx, double yy, double zz) { return vectorR3((float)xx, (float)yy, (float)zz); }