QXRD  0.11.16
qxrdfitterringpoint.cpp
Go to the documentation of this file.
1 #include "qxrdfitterringpoint.h"
2 #include "qxrdcenterfinder.h"
3 
4 #include "levmar.h"
5 
6 # ifdef LINSOLVERS_RETAIN_MEMORY
7 # ifdef _MSC_VER
8 # pragma message("LINSOLVERS_RETAIN_MEMORY is not safe in a multithreaded environment and should be turned off!")
9 # else
10 # warning LINSOLVERS_RETAIN_MEMORY is not safe in a multithreaded environment and should be turned off!
11 # endif /* _MSC_VER */
12 # endif /* LINSOLVERS_RETAIN_MEMORY */
13 
14 QxrdFitterRingPoint::QxrdFitterRingPoint(QxrdCenterFinder *cf, int index, double x0, double y0, double pkht, double bkgd) :
15  QxrdFitterPeakOrRing(cf, index, x0, y0, pkht, bkgd)
16 {
17 }
18 
21 {
22 }
23 
24 void QxrdFitterRingPoint::staticEvaluate(double *p, double *hx, int m, int n, void *adata)
25 {
27 
28  if (rf) {
29  rf->evaluate(p,hx,m,n);
30  }
31 }
32 
33 void QxrdFitterRingPoint::evaluate(double *parm, double *xv, int np, int /*nx*/)
34 {
35  if (m_CenterFinder) {
36 // m_CenterFinder->printMessage("Fitting");
37 
38  if (np!=6) {
39  m_CenterFinder->printMessage("Wrong number of parameters");
40  } else {
41  double pr = parm[0];
42  double wd = parm[1];
43  double ht = parm[2];
44  double bg = parm[3];
45  double bx = parm[4];
46  double by = parm[5];
47 
48  double dr = m_CenterFinder->get_PeakFitRadius();
49  double xc = m_CenterFinder->get_CenterX();
50  double yc = m_CenterFinder->get_CenterY();
51  double cx0 = m_X0;
52  double cy0 = m_Y0;
53  double az = atan2(cy0-yc, cx0-xc);
54 
55  int n = dr*2+1;
56  int x0 = cx0 - dr;
57  int y0 = cy0 - dr;
58 // int nn = n*n;
59  int i=0;
60 
61  double cx = xc + pr*cos(az);
62  double cy = yc + pr*sin(az);
63 
64  double dx0 = cx - xc;
65  double dy0 = cy - yc;
66  double r0 = sqrt(dx0*dx0+dy0*dy0);
67 
68  for (int y=y0; y<y0+n; y++) {
69  for (int x=x0; x<x0+n; x++) {
70  double d = m_CenterFinder->imageValue(x,y);
71 
72  double dx = x-cx;
73  double dy = y-cy;
74  double dx1 = x - xc;
75  double dy1 = y - yc;
76  double r1 = sqrt(dx1*dx1+dy1*dy1);
77  double dr = r1-r0;
78  double pk = bg + dx*bx + dy*by + ht*exp(-((dr*dr)/(2.0*wd*wd)));
79 
80  xv[i++] = pk - d;
81  }
82  }
83  }
84  }
85 }
86 
88 {
89  int niter = -1;
90 
91  if (m_CenterFinder) {
92 
93  double x = m_X0, y = m_Y0;
94 
95 // if (qcepDebug(DEBUG_FITTING) || m_CenterFinder->get_PeakFitDebug()) {
96 // m_CenterFinder -> printMessage(QObject::tr("Fitting i: %1, x0: %2, y0: %3")
97 // .arg(index()).arg(m_X0).arg(m_Y0));
98 // }
99 
100  int width = 0, height = 0;
101 
103 
104  if (data) {
105  width = data -> get_Width()+1;
106  height = data -> get_Height()+1;
107 
108  if (x<0 || x>width || y<0 || y>height) {
110  } else {
111  double xc = m_CenterFinder->get_CenterX();
112  double yc = m_CenterFinder->get_CenterY();
113  double dx = x - xc;
114  double dy = y - yc;
115  double r = sqrt(dx*dx + dy*dy);
116  double chi = atan2(dy, dx);
117  double dr = m_CenterFinder->get_PeakFitRadius();
118  double bkgd = ( m_CenterFinder->imageValue(xc+(r+dr)*cos(chi), yc+(r+dr)*sin(chi))
119  +m_CenterFinder->imageValue(xc+(r-dr)*cos(chi), yc+(r-dr)*sin(chi)))/2.0;
120  double pkht = m_CenterFinder->imageValue(x,y) - bkgd;
121 
122  double parms[6];
123 
124  parms[0] = r;
125  parms[1] = 2.0;
126  parms[2] = pkht;
127  parms[3] = bkgd;
128  parms[4] = 0;
129  parms[5] = 0;
130 
131  double info[LM_INFO_SZ];
132 
133  int n = m_CenterFinder->get_PeakFitRadius()*2+1;
134 
135  niter = dlevmar_dif(QxrdFitterRingPoint::staticEvaluate,
136  parms, NULL, 6, n*n,
137  m_CenterFinder->get_PeakFitIterations(),
138  NULL, info, NULL, NULL, this);
139 
140  if (niter > 0) {
142  m_FittedX = xc + parms[0]*cos(chi);
143  m_FittedY = yc + parms[0]*sin(chi);
144  m_FittedWidth = parms[1];
145  m_FittedHeight = parms[2];
146  m_FittedBkgd = parms[3];
147  m_FittedBkgdX = parms[4];
148  m_FittedBkgdY = parms[5];
149 
150  double dx = m_FittedX - m_X0;
151  double dy = m_FittedY - m_Y0;
152  double dr = sqrt(dx*dx + dy*dy);
153 
154  if ((fabs(m_FittedWidth)<m_CenterFinder->get_FittedWidthMin()) ||
155  (fabs(m_FittedWidth)>m_CenterFinder->get_FittedWidthMax())) {
156  m_Reason = BadWidth;
157  } else if ((m_FittedHeight/m_Pkht)<m_CenterFinder->get_FittedHeightMinRatio()) {
159  } else if (dr>m_CenterFinder->get_FittedPositionMaxDistance()) {
161  }
162  }
163  }
164  }
165  }
166 
167  return niter;
168 }
169 
FitResult m_Reason
Definition: qxrdfitter.h:32
QcepDoubleImageDataPtr data() const
void evaluate(double *parm, double *xv, int np, int nx)
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime()) const
QxrdCenterFinder * m_CenterFinder
Definition: qxrdfitter.h:31
static void staticEvaluate(double *parm, double *xv, int np, int nx, void *adata)
double imageValue(double x, double y)
QSharedPointer< QcepDoubleImageData > QcepDoubleImageDataPtr