QXRD  0.11.16
qxrddistortioncorrection.cpp
Go to the documentation of this file.
2 #include "qcepmutexlocker.h"
3 #include "qcepsettingssaver.h"
4 #include "levmar.h"
5 #include <qmath.h>
6 #include <stdio.h>
7 #include "qxrdexperiment.h"
8 
10  : QcepObject("distortion", NULL),
11  m_DistortionImagePath(saver, this, "distortionImagePath", "", "File path for distortion calibration image"),
12  m_P0(saver, this, "p0", QPointF(100,100), "Origin of distortion image grid"),
13  m_P1(saver, this, "p1", QPointF(200,100), "1st X Position on distortion grid"),
14  m_P2(saver, this, "p2", QPointF(100,200), "1st Y Position on distortion grid"),
15  m_N1(saver, this, "n1", 40, "Number of grid points along x"),
16  m_N2(saver, this, "n2", 40, "Number of grid points along y"),
17  m_F0(saver, this, "f0", QPointF(100,100), "Fitted Origin of distortion image grid"),
18  m_F1(saver, this, "f1", QPointF(200,100), "Fitted 1st X Position on distortion grid"),
19  m_F2(saver, this, "f2", QPointF(100,200), "Fitted 1st Y Position on distortion grid"),
20  m_IVals(saver, this, "iVals", QcepIntVector(), "Grid point i indices"),
21  m_JVals(saver, this, "jVals", QcepIntVector(), "Grid point j indices"),
22  m_XVals(saver, this, "xVals", QcepDoubleVector(), "Grid point x coords"),
23  m_YVals(saver, this, "yVals", QcepDoubleVector(), "Grid point y coords"),
24  m_FXVals(saver, this, "fxVals", QcepDoubleVector(), "Fitted x coords"),
25  m_FYVals(saver, this, "fyVals", QcepDoubleVector(), "Fitted y coords"),
26  m_DXVals(saver, this, "dxVals", QcepDoubleVector(), "Delta x coords"),
27  m_DYVals(saver, this, "dyVals", QcepDoubleVector(), "Delta y coords"),
28  m_WMin(saver, this, "wMin", 1, "Minimum acceptable grid peak width (pixels)"),
29  m_WMax(saver, this, "wMax", 4, "Maximum acceptable grid peak width (pixels)"),
30  m_WNom(saver, this, "wNom", 2, "Nominal initial grid peak width (pixels)"),
31  m_RatMin(saver, this, "ratMin", 3, "Minimum acceptable peak/background ratio for grid peaks"),
32  m_HgtMin(saver, this, "hgtMin", 1000, "Minimum acceptable peak height for grid peaks"),
33  m_DistMax(saver, this, "distMax", QPointF(5,5), "Maximum acceptable grid peak position distance from nominal"),
34  m_Experiment(expt)
35 {
36 }
37 
39 {
40  return pt;
41 }
42 
44 {
45  return pt;
46 }
47 
49 {
50  prop_IVals()->clear();
51  prop_JVals()->clear();
52  prop_XVals()->clear();
53  prop_YVals()->clear();
54  prop_FXVals()->clear();
55  prop_FYVals()->clear();
56  prop_DXVals()->clear();
57  prop_DYVals()->clear();
58 }
59 
60 void QxrdDistortionCorrection::appendGridPoint(int i, int j, double x, double y)
61 {
62  prop_IVals()->appendValue(i);
63  prop_JVals()->appendValue(j);
64  prop_XVals()->appendValue(x);
65  prop_YVals()->appendValue(y);
66 // prop_FXVals()->appendValue(0);
67 // prop_FYVals()->appendValue(0);
68 // prop_DXVals()->appendValue(0);
69 // prop_DYVals()->appendValue(0);
70 }
71 
72 void QxrdDistortionCorrection::evaluateFitGrid(double parms[], double hx[], int m, int n)
73 {
74  double p0x = parms[0];
75  double p0y = parms[1];
76  double dxx = parms[2]-p0x;
77  double dxy = parms[3]-p0y;
78  double dyx = parms[4]-p0x;
79  double dyy = parms[5]-p0y;
80 
81  for (int i=0; i<n; i++) {
82  int ii = get_IVals()[i];
83  int jj = get_JVals()[i];
84  double fx = p0x + ii*dxx + jj*dyx;
85  double fy = p0y + ii*dxy + jj*dyy;
86 
87  double dx = fx - get_XVals()[i];
88  double dy = fy - get_YVals()[i];
89 
90  hx[i] = sqrt(dx*dx + dy*dy);
91  }
92 }
93 
94 static void fitGrid(double parms[], double hx[], int m, int n, void *adata)
95 {
97 
98  if (dc) {
99  dc->evaluateFitGrid(parms, hx, m, n);
100  }
101 }
102 
104 {
105  double parms[6];
106 
107  parms[0] = get_P0().x();
108  parms[1] = get_P0().y();
109  parms[2] = get_P1().x();
110  parms[3] = get_P1().y();
111  parms[4] = get_P2().x();
112  parms[5] = get_P2().y();
113 
114  double info[LM_INFO_SZ];
115 
116  int ndata = get_IVals().count();
117 
118  int niter = dlevmar_dif(fitGrid, parms, NULL, 6, ndata, 300, NULL, info, NULL, NULL, this);
119 
120  if (niter >= 0) {
121  set_F0(QPointF(parms[0], parms[1]));
122  set_F1(QPointF(parms[2], parms[3]));
123  set_F2(QPointF(parms[4], parms[5]));
124  }
125 }
126 
128 {
129  double p0x = get_F0().x();
130  double p0y = get_F0().y();
131  double dxx = get_F1().x() - p0x;
132  double dxy = get_F1().y() - p0y;
133  double dyx = get_F2().x() - p0x;
134  double dyy = get_F2().y() - p0y;
135 
136  int n = get_IVals().count();
137 
138  for (int i=0; i<n; i++) {
139  int ii = get_IVals()[i];
140  int jj = get_JVals()[i];
141  double fx = p0x + ii*dxx + jj*dyx;
142  double fy = p0y + ii*dxy + jj*dyy;
143 
144  double dx = fx - get_XVals()[i];
145  double dy = fy - get_YVals()[i];
146 
147  prop_FXVals()->appendValue(fx);
148  prop_FYVals()->appendValue(fy);
149  prop_DXVals()->appendValue(dx);
150  prop_DYVals()->appendValue(dy);
151  }
152 }
153 
155 {
156  int n = get_IVals().count();
157 
159 
160  FILE *f = fopen(qPrintable(path), "a+");
161 
162  if (f) {
163  fprintf(f, "i\tj\tx\ty\tfx\tfy\tdx\tdy\n");
164 
165  for (int i=0; i<n; i++) {
166  fprintf(f, "%d\t%d\t%g\t%g\t%g\t%g\t%g\t%g\n",
167  get_IVals()[i], get_JVals()[i],
168  get_XVals()[i], get_YVals()[i],
169  get_FXVals()[i], get_FYVals()[i],
170  get_DXVals()[i], get_DYVals()[i]);
171  }
172 
173  fclose(f);
174  }
175 }
QSharedPointer< QxrdExperiment > QxrdExperimentPtr
QVector< int > QcepIntVector
Definition: qcepmacros.h:23
static void fitGrid(double parms[], double hx[], int m, int n, void *adata)
QWeakPointer< QxrdExperiment > QxrdExperimentWPtr
void evaluateFitGrid(double parms[], double hx[], int m, int n)
QVector< double > QcepDoubleVector
Definition: qcepmacros.h:19
void appendGridPoint(int i, int j, double x, double y)
QxrdDistortionCorrection(QcepSettingsSaverWPtr saver, QxrdExperimentWPtr expt)
QWeakPointer< QcepSettingsSaver > QcepSettingsSaverWPtr