55 return (x == 1 ? x : x *
factorial(x - 1));
65 cout <<
"ophWaveAberration Constructor" << endl;
66 uint wavelength_num = 1;
74 cout <<
"ophWaveAberration Destructor" << endl;
80 vector<double>::size_type x_max = x.size();
81 vector<double>::size_type y_max = y.size();
82 double radius = d / 2;
89 double **Z =
new double*[x_max];
90 double **A =
new double*[x_max];
91 for(
int i = 0; i < (int)x_max; i++)
93 A[i] =
new double[y_max];
94 Z[i] =
new double[y_max];
99 for(
int ix = 0; ix < (int)x_max; ix++)
101 for(
int iy = 0; iy < (int)y_max; iy++)
103 A[ix][iy] = (sqrt(pow(x[ix],2) + pow(y[iy],2)) <= radius);
108 N = sqrt(2 * (n + 1) / (1 + (
m == 0)));
112 for(
int i=0; i<(int)x_max; i++)
113 memcpy(Z[i], A[i],y_max*
sizeof(
double));
117 for(
int i = 0; i<(int)x_max; i++)
118 memset(Z[i],0, y_max*
sizeof(
double));
120 for(
int ix = 0; ix < (int)x_max; ix++)
122 for(
int iy = 0; iy < (int)y_max; iy++)
124 r = sqrt(pow(x[ix], 2) + pow(y[iy],2));
126 if (((x[ix] >= 0) && (y[iy] >= 0)) || ((x[ix] >= 0) & (y[iy] < 0)))
127 theta = atan(y[iy] / (x[ix] + 1e-30));
129 theta =
M_PI + atan(y[iy] / (x[ix] + 1e-30));
131 for(
int s = 0; s <= (n -
abs(
m)) / 2; s++)
133 Z[ix][iy] = Z[ix][iy] + pow((-1),s)*
factorial(n - s)*pow((r/radius),(n - 2 * s)) /
138 Z[ix][iy] = A[ix][iy]*N*Z[ix][iy]*((
m >= 0)*co - (
m < 0)*si);
143 for (
size_t i=0; i < x_max; i++)
156 double x, y, tx, tx1, ty, ty1, scale_x, scale_y;
158 scale_x = (double)Nx / (
double)nx;
159 scale_y = (double)Ny / (
double)ny;
161 for (
int i = 0; i < nx; i++)
163 x = (double)i * scale_x;
167 tx1 = double(1.0) - tx;
168 for (
int j = 0; j < ny; j++)
170 y = (double)j * scale_y;
173 ty1 = double(1.0) - ty;
175 Y[i][j] = X[fx][fy] * (tx1*ty1) + X[fx][fy + 1] * (tx1*ty) + X[fx + 1][fy] * (tx*ty1) + X[fx + 1][fy + 1] * (tx*ty);
188 int z_max =
sizeof(zernikeCoefficent)/
sizeof(zernikeCoefficent[0]);
190 ZC = zernikeCoefficent;
199 double DE = max(dxa*xr, dya*yr);
207 double max_xn = floor(DE/dxa+1);
208 double max_yn = floor(DE/dya+1);
210 xn.reserve((
int)max_xn);
211 for (
int i = 0; i < (int)max_xn; i++)
213 xn.push_back(-DE / 2 + dxa*i);
216 yn.reserve((
int)max_yn);
217 for (
int i = 0; i < max_yn; i++)
219 yn.push_back(-DE / 2 + dya*i);
224 vector<double>::size_type length_xn = xn.size();
225 vector<double>::size_type length_yn = yn.size();
227 double **W =
new double*[(int)length_xn];
228 double **Temp_W =
new double*[(int)length_xn];
230 for (
int i = 0; i < (int)length_xn; i++)
232 W[i] =
new double[length_yn];
233 Temp_W[i] =
new double[length_yn];
236 for (
int i = 0; i < (int)length_xn; i++)
238 memset(W[i], 0, length_yn*
sizeof(
double));
239 memset(Temp_W[i], 0, length_yn *
sizeof(
double));
244 for (
int i = 0; i < z_max; i++)
248 n = ceil((-3 + sqrt(9 + 8 * i)) / 2);
249 m = 2 * i - n * (n + 2);
253 for(
size_t ii = 0; ii < length_xn; ii++)
255 for (
size_t jj = 0; jj < length_yn; jj++)
257 W[ii][jj] = W[ii][jj] + ZC[i] * Temp_W[ii][jj];
265 for (
int i = 0; i < (int)length_xn; i++)
267 memset(Temp_W[i], 0, length_yn *
sizeof(
double));
270 int min_xnn, max_xnn;
271 int min_ynn, max_ynn;
273 min_xnn = (int)round(length_xn / 2 - xr / 2);
274 max_xnn = (int)round(length_xn / 2 + xr / 2 + 1);
275 min_ynn = (int)round(length_yn / 2 - yr / 2);
276 max_ynn = (int)round(length_yn / 2 + yr / 2 + 1);
278 int length_xnn, length_ynn;
279 length_xnn = max_xnn - min_xnn;
280 length_ynn = max_ynn - min_ynn;
282 double **WT =
new double*[length_xnn];
283 for (
int i = 0; i < length_xnn; i++)
285 WT[i] =
new double[length_ynn];
286 memset(WT[i], 0, length_ynn *
sizeof(
double));
289 for (
int i = 0; i < length_xnn; i++)
291 for (
int j = 0; j < length_ynn; j++)
293 WT[i][j] = W[min_xnn+i][min_ynn+j];
297 double **WS =
new double*[(int)xr];
298 for (
int i = 0; i < (int)xr; i++)
300 WS[i] =
new double[yr];
301 memset(WS[i], 0, yr *
sizeof(
double));
304 imresize(WT, length_xnn, length_ynn, xr, yr, WS);
309 for(
int i = 0; i < (int)xr; i++)
312 for(
int ii = 0; ii < (int)xr; ii ++ )
314 for (
int jj = 0; jj < (int)yr; jj++)
322 for (
int i = 0; i < (int)length_xn; i++)
330 for (
int i = 0; i < (int)xr; i++)
336 for (
int i = 0; i < (int)length_xnn; i++)
344 for (
unsigned int x = 0; x < xr; x++) {
345 for (
unsigned int y = 0; y < yr; y++) {
354 auto during_time = ((std::chrono::duration<Real>)(end_time - start_time)).count();
356 LOG(
"Implement time : %.5lf sec\n", during_time);
364 delete[] doublePtr[i];
371 std::cout <<
" ophFree" << std::endl;
377 LOG(
"Reading....%s...\n", fname);
389 LOG(
"file's extension is not 'xml'\n");
395 LOG(
"Failed to load file \"%s\"\n", fname);
424 for(
int i=0; i< 45; i++)
428 xml_attribute=xml_attribute->
Next();
451 cout <<
"Zernike Coefficient: " << endl;
452 for(
int i=0; i<45; i++)
454 if (i!=0 && (i+1)%5 == 0)
455 cout <<
"z["<<i<<
"]="<< zernikeCoefficent[i]<<endl;
457 cout <<
"z[" << i <<
"]=" << zernikeCoefficent[i] <<
" ";
471 ofstream fout(fname, ios_base::out | ios_base::binary);
483 ifstream fin(fname, ios_base::in | ios_base::binary);
491 LOG(
"Failed load file");
502 for (
int x = 0; x < xr; x++) {
503 for (
int y = 0; y < yr; y++) {
void abs(const oph::Complex< T > &src, oph::Complex< T > &dst)
Real factorial(double x)
Factorial using recursion.
ophWaveAberration()
Constructor.
oph::Complex< Real > ** complex_W
double pointer of the 2D data array of a wave aberration
~ophWaveAberration()
Destructor.
void setPixelNumberOHC(const ivec2 pixel_number)
getter/setter for OHC file read and write
virtual bool loadAsOhc(const char *fname)
Function to read OHC file.
double ** calculateZernikePolynomial(double n, double m, vector< double > x, vector< double > y, double d)
Calculates Zernike polynomial.
void accumulateZernikePolynomial()
Sums up the calculated Zernike polynomials.
XMLError QueryIntText(int *ival) const
void setPixelPitchOHC(const vec2 pixel_pitch)
bool readConfig(const char *fname)
read configuration from a configration file
bool checkExtension(const char *fname, const char *ext)
Functions for extension checking.
void ophFree(void)
Pure virtual function for override in child classes.
structure for 2-dimensional integer vector and its arithmetic.
const XMLNode * FirstChild() const
Get the first child node, or null if none exists.
void setWavelengthOHC(const Real wavelength, const LenUnit wavelength_unit)
void readAberration(const char *fname)
reads the 2D data array of a wave aberration from a file
void Free2D(oph::Complex< Real > **doublePtr)
deletes 2D memory array using double pointer
structure for 2-dimensional Real type vector and its arithmetic.
void saveAberration(const char *fname)
saves the 2D data array of a wave aberration into a file
XMLError QueryDoubleValue(double *value) const
See QueryIntValue.
virtual bool loadAsOhc(const char *fname)
Function to read OHC file.
XMLError LoadFile(const char *filename)
XMLError QueryDoubleText(double *dval) const
See QueryIntText()
Complex< Real > ** complex_H
const XMLAttribute * Next() const
The next attribute in the list.
const XMLElement * FirstChildElement(const char *name=0) const
void imresize(double **X, int Nx, int Ny, int nx, int ny, double **Y)
Resizes 2D data using bilinear interpolation.
const XMLAttribute * FirstAttribute() const
Return the first attribute in the list.