601 long indx, indy, l, l1, l2,
m, m1, m2,
n, n1, n2;
603 double phi,
theta,
psi,
x0,
y0, Sinphi, Cosphi, Sinpsi, Cospsi, Sintheta, Costheta;
604 double *hlp, *Rz1, *Ry, *Rz2, *DRz1, *DRy, *DRz2;
605 double *DR0, *DR1, *DR2, *mn, *Arg;
606 double *Grad_re, *Grad_im, *Hessian_re, *Hessian_im, *Gradient_re, *Gradient_im;
607 double scx, scy,
x,
y,
z;
608 double xminusl, yminusm, zminusn, re_Coeff, im_Coeff;
609 double re_columns, re_rows, im_columns, im_rows;
610 double re_slices, re_slices_d1, re_slices_d2, re_slices_d3, re_columns_d1, re_columns_d2, re_rows_d1;
611 double im_slices, im_slices_d1, im_slices_d2, im_slices_d3, im_columns_d1, im_columns_d2, im_rows_d1;
612 double a,
b,
c, SinC, CosC, redftproj, imdftproj;
613 double da, db, dc, da0[1], da1[1], da2[1], da3[1], da4[1];
614 double db0[1], db1[1], db2[1], db3[1], db4[1], dc0[1], dc1[1], dc2[1], dc3[1], dc4[1];
615 double prv_re, prv_im, Difference_re, Difference_im, Weight;
616 double dp0_re, dp0_im, dp1_re, dp1_im, dp2_re, dp2_im, dp3_re, dp3_im, dp4_re, dp4_im;
617 double cost_re, cost_im, *R, *Q, *Q2, *dQ0, *dQ1, *dQ2, *dQ2_0, *dQ2_1, *dQ2_2;
618 double sum_xx_re, sum_xx_im, sc_re, sc_im;
619 double *DP_0, *DP_1, *DP_2, *DP_3, *DP_4, *pntr_ReOut, *pntr_ImOut;
620 double *pntr_DP_re, *pntr_DP_im, *pntr_DP_0_re, *pntr_DP_0_im, *pntr_DP_1_re, *pntr_DP_1_im;
621 double *pntr_DP_2_re, *pntr_DP_2_im, *pntr_DP_3_re, *pntr_DP_3_im;
622 double *pntr_DP_4_re, *pntr_DP_4_im;
624 size_t poolSize = (18 * 16) + (4 * 4) + (2 * 5) + (2 * 25);
625 auto pool = std::unique_ptr<double[]>(
new double[poolSize]);
626 double *poolNext = pool.get();
627 auto getNextFromPool = [&](
size_t count) {
633 if (( ! pool) || (
nullptr == poolNext)) {
639 theta = Parameters[1];
648 Sintheta = sin(theta);
649 Costheta = cos(theta);
651 Rz1 = getNextFromPool(16);
652 Ry = getNextFromPool(16);
653 Rz2 = getNextFromPool(16);
663 hlp += (std::ptrdiff_t)3L;
676 hlp += (std::ptrdiff_t)3L;
688 hlp += (std::ptrdiff_t)2L;
690 hlp += (std::ptrdiff_t)6L;
692 hlp += (std::ptrdiff_t)2L;
695 R = getNextFromPool(16);
702 DRz1 = getNextFromPool(16);
703 DRy = getNextFromPool(16);
704 DRz2 = getNextFromPool(16);
705 for (i = 0L; i < 16L; i++)
715 hlp += (std::ptrdiff_t)3L;
722 hlp += (std::ptrdiff_t)3L;
728 hlp += (std::ptrdiff_t)2L;
730 hlp += (std::ptrdiff_t)6L;
732 hlp += (std::ptrdiff_t)2L;
735 DR0 = getNextFromPool(16);
736 DR1 = getNextFromPool(16);
737 DR2 = getNextFromPool(16);
754 mn = getNextFromPool(4);
755 Arg = getNextFromPool(4);
756 Grad_re = getNextFromPool(4);
757 Grad_im = getNextFromPool(4);
758 Gradient_re = getNextFromPool(5);
759 Gradient_im = getNextFromPool(5);
760 Hessian_re = getNextFromPool(25);
761 Hessian_im = getNextFromPool(25);
803 pntr_ReOut = dftCompProj;
804 pntr_ImOut = pntr_ReOut + (std::ptrdiff_t) SizeIm;
807 pntr_DP_0_im = pntr_DP_0_re + (std::ptrdiff_t) SizeIm;
810 pntr_DP_1_im = pntr_DP_1_re + (std::ptrdiff_t) SizeIm;
813 pntr_DP_2_im = pntr_DP_2_re + (std::ptrdiff_t) SizeIm;
816 pntr_DP_3_im = pntr_DP_3_re + (std::ptrdiff_t) SizeIm;
819 pntr_DP_4_im = pntr_DP_4_re + (std::ptrdiff_t) SizeIm;
821 Q = getNextFromPool(16);
822 Q2 = getNextFromPool(16);
823 auto freeAllVolumes = [&] {
852 dQ0 = getNextFromPool(16);
853 dQ1 = getNextFromPool(16);
854 dQ2 = getNextFromPool(16);
855 dQ2_0 = getNextFromPool(16);
856 dQ2_1 = getNextFromPool(16);
857 dQ2_2 = getNextFromPool(16);
925 for (indy = 0L; indy < My; indy++)
927 for (indx = 0L; indx < Mx; indx++)
930 mn[0] = (double)(indx - Mx / 2L);
931 mn[1] = (double)(indy - My / 2L);
946 l1 = (long) ceil(x - 2.0);
948 m1 = (long) ceil(y - 2.0);
950 n1 = (long) ceil(z - 2.0);
961 for (n = n1; n <= n2; n++)
965 CC1 = Nx * Ny * Laux;
972 for (m = m1; m <= m2; m++)
975 CC = CC1 + Nx * Laux;
980 for (l = l1; l <= l2; l++)
982 xminusl = x - (double) l;
984 long Laux2=CC + Laux;
985 re_Coeff = CoefRe[Laux2];
986 im_Coeff = CoefIm[Laux2];
989 re_rows += re_Coeff * aux;
990 re_rows_d1 += re_Coeff * aux2;
991 im_rows += im_Coeff * aux;
992 im_rows_d1 += im_Coeff * aux2;
994 yminusm = y - (double) m;
997 re_columns += re_rows * aux;
998 re_columns_d1 += re_rows_d1 * aux;
999 re_columns_d2 += re_rows * aux2;
1000 im_columns += im_rows * aux;
1001 im_columns_d1 += im_rows_d1 * aux;
1002 im_columns_d2 += im_rows * aux2;
1004 zminusn = z - (double) n;
1007 re_slices += re_columns * aux;
1008 re_slices_d1 += re_columns_d1 * aux;
1009 re_slices_d2 += re_columns_d2 * aux;
1010 re_slices_d3 += re_columns * aux2;
1011 im_slices += im_columns * aux;
1012 im_slices_d1 += im_columns_d1 * aux;
1013 im_slices_d2 += im_columns_d2 * aux;
1014 im_slices_d3 += im_columns * aux2;
1018 Grad_re[0] = re_slices_d1;
1019 Grad_re[1] = re_slices_d2;
1020 Grad_re[2] = re_slices_d3;
1024 Grad_im[0] = im_slices_d1;
1025 Grad_im[1] = im_slices_d2;
1026 Grad_im[2] = im_slices_d3;
1029 c = scx * mn[0] + scy * mn[1];
1034 redftproj = (a * CosC + b * SinC) * S;
1035 imdftproj = (b * CosC - a * SinC) * S;
1037 index = indy * Mx + indx;
1039 pntr_ReOut[
index] = redftproj;
1040 pntr_ImOut[
index] = imdftproj;
1043 if (!((indx == (Mx / 2L)) && (indy == (My / 2L))))
1045 sum_xx_re += redftproj * redftproj;
1046 sum_xx_im += imdftproj * imdftproj;
1115 dc3[0] = scx1 * mn[0];
1116 dc4[0] = scy1 * mn[1];
1121 pntr_DP_re = pntr_DP_0_re;
1122 pntr_DP_im = pntr_DP_0_im;
1124 (da * CosC - a * SinC * dc + db * SinC + b * CosC * dc) * S;
1126 (db * CosC - b * SinC * dc - da * SinC - a * CosC * dc) * S;
1132 pntr_DP_re = pntr_DP_1_re;
1133 pntr_DP_im = pntr_DP_1_im;
1135 (da * CosC - a * SinC * dc + db * SinC + b * CosC * dc) * S;
1137 ((db * CosC - b * SinC * dc - da * SinC - a * CosC * dc) * S);
1142 pntr_DP_re = pntr_DP_2_re;
1143 pntr_DP_im = pntr_DP_2_im;
1145 ((da * CosC - a * SinC * dc + db * SinC + b * CosC * dc) * S);
1147 ((db * CosC - b * SinC * dc - da * SinC - a * CosC * dc) * S);
1153 pntr_DP_re = pntr_DP_3_re;
1154 pntr_DP_im = pntr_DP_3_im;
1156 ((da * CosC - a * SinC * dc + db * SinC + b * CosC * dc) * S);
1158 ((db * CosC - b * SinC * dc - da * SinC - a * CosC * dc) * S);
1163 pntr_DP_re = pntr_DP_4_re;
1164 pntr_DP_im = pntr_DP_4_im;
1166 ((da * CosC - a * SinC * dc + db * SinC + b * CosC * dc) * S);
1168 ((db * CosC - b * SinC * dc - da * SinC - a * CosC * dc) * S);
1178 for (i = 0L; i < 5L; i++)
1180 Gradient_re[
i] = 0.0;
1181 Gradient_im[
i] = 0.0;
1182 for (j = 0L; j < 5L; j++)
1184 Hessian_re[i * 5L +
j] = 0.0;
1185 Hessian_im[i * 5L +
j] = 0.0;
1189 sc_re = 1.0 /
sqrt(sum_xx_re + sum_xx_im);
1193 for (indy = 0L; indy < My; indy++)
1195 for (indx = 0L; indx < Mx; indx++)
1198 index = indy * Mx + indx;
1200 Weight = (double) pntr_Weight[index];
1202 prv_re = (sc_re * (double) pntr_ReOut[index]);
1203 Difference_re = (double)(prv_re - pntr_ReInp[index]);
1205 prv_im = (sc_im * (double) pntr_ImOut[index]);
1206 Difference_im = (double)(prv_im - pntr_ImInp[index]);
1208 dp0_re = sc_re * (double) pntr_DP_0_re[index];
1209 dp1_re = sc_re * (double) pntr_DP_1_re[index];
1210 dp2_re = sc_re * (double) pntr_DP_2_re[index];
1211 dp3_re = sc_re * (double) pntr_DP_3_re[index];
1212 dp4_re = sc_re * (double) pntr_DP_4_re[index];
1214 dp0_im = sc_im * (double) pntr_DP_0_im[index];
1215 dp1_im = sc_im * (double) pntr_DP_1_im[index];
1216 dp2_im = sc_im * (double) pntr_DP_2_im[index];
1217 dp3_im = sc_im * (double) pntr_DP_3_im[index];
1218 dp4_im = sc_im * (double) pntr_DP_4_im[index];
1220 if (
gradhesscost_atpixel(Gradient_re, Hessian_re, &cost_re, Difference_re, dp0_re, dp1_re, dp2_re, dp3_re, dp4_re, Weight) ==
ERROR)
1226 if (
gradhesscost_atpixel(Gradient_im, Hessian_im, &cost_im, Difference_im, dp0_im, dp1_im, dp2_im, dp3_im, dp4_im, Weight))
1237 *cost = (cost_re + cost_im) / 2.0;
1239 for (i = 0L; i < 5L; i++)
1241 Gradient[
i] = Gradient_re[
i] + Gradient_im[
i];
1242 for (j = 0L; j < 5L; j++)
1243 Hessian[i * 5L + j] = Hessian_re[i * 5L + j] + Hessian_im[i * 5L + j];
1247 for (i = 0L; i < 4L; i++)
1248 for (j = i + 1L; j < 5L; j++)
1249 Hessian[j * 5L + i] = Hessian[i * 5L + j];
int return_gradhesscost(double *Gradient, double *Hessian, double *cost, double *Parameters, double *CoefRe, double *CoefIm, double *pntr_ReInp, double *pntr_ImInp, double *pntr_Weight, long Nx, long Ny, long Nz, long indx0, long indy0, long indz0, long Mx, long My, double *Left, double *Right, double *Q1, double *Q3, double scx1, double scy1, double S, long SizeIm, int DoDesProj, double *dftCompProj)
void sqrt(Image< double > &op)
int VectorScalarProduct(double A[], double B[], double *X, long Lines)
int MatrixTimesVector(double *A, double *B, double *X, long Lines, long Columns)
int gradhesscost_atpixel(double *Gradient, double *Hessian, double *cost, double Difference, double dp0, double dp1, double dp2, double dp3, double dp4, double Weight)
#define WRITE_ERROR(FunctionName, String)
#define BSPLINE03DIFF1(y, x)
#define PERIODIZATION(y, period, k)
int AllocateVolumeDouble(double **Volume, long Nx, long Ny, long Nz, int *Status)
int FreeVolumeDouble(double **Volume)
int MatrixTranspose(double *A, double *At, long Lines, long Columns)
double psi(const double x)
int GetIdentitySquareMatrix(double *A, long Size)
int multiply_3Matrices(double *A, double *B, double *C, double *X, long Lines, long CommonSizeH, long CommonSizeW, long Columns)