QXRD  0.11.16
qxrdfitterpeakpoint.cpp
Go to the documentation of this file.
1 #include "qxrdfitterpeakpoint.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 QxrdFitterPeakPoint::QxrdFitterPeakPoint(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 QxrdFitterPeakPoint::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 QxrdFitterPeakPoint::evaluate(double *parm, double *xv, int np, int /*nx*/)
34 {
35  if (m_CenterFinder) {
36 // m_CenterFinder->printMessage("Fitting");
37 
38  if (np!=7) {
39  m_CenterFinder->printMessage("Wrong number of parameters");
40  } else {
41  double cx = parm[0];
42  double cy = parm[1];
43  double r = parm[2];
44  double ht = parm[3];
45  double bg = parm[4];
46  double bx = parm[5];
47  double by = parm[6];
48 
49  double cx0 = m_X0;
50  double cy0 = m_Y0;
51  double rr0 = m_CenterFinder->get_PeakFitRadius();
52 
53  int n = rr0*2+1;
54  int x0 = cx0 - rr0;
55  int y0 = cy0 - rr0;
56 // int nn = n*n;
57  int i=0;
58 
59  for (int y=y0; y<y0+n; y++) {
60  for (int x=x0; x<x0+n; x++) {
61  double d = m_CenterFinder->imageValue(x,y);
62 
63  double dx = x-cx;
64  double dy = y-cy;
65  double pk = bg + dx*bx + dy*by + ht*exp(-((dx*dx+dy*dy)/(2.0*r*r)));
66 
67  xv[i++] = pk - d;
68  }
69  }
70  }
71  }
72 }
73 
75 {
76  int niter = -1;
77 
78  if (m_CenterFinder) {
79 
80  double x = m_X0, y = m_Y0;
81 
82 // if (qcepDebug(DEBUG_FITTING) || m_CenterFinder->get_PeakFitDebug()) {
83 // m_CenterFinder -> printMessage(QObject::tr("Fitting i: %1, x0: %2, y0: %3")
84 // .arg(index()).arg(m_X0).arg(m_Y0));
85 // }
86 
87  int width = 0, height = 0;
88 
90 
91  if (data) {
92  width = data -> get_Width()+1;
93  height = data -> get_Height()+1;
94 
95  if (x<0 || x>width || y<0 || y>height) {
97  } else {
98  double dr = m_CenterFinder->get_PeakFitRadius();
99 
100  double bkgd = ( m_CenterFinder->imageValue(x+dr, y) +
101  m_CenterFinder->imageValue(x, y+dr) +
102  m_CenterFinder->imageValue(x-dr, y) +
103  m_CenterFinder->imageValue(x, y-dr) )/4.0;
104  double pkht = m_CenterFinder->imageValue(x,y) - bkgd;
105 
106  double parms[7];
107 
108  parms[0] = x;
109  parms[1] = y;
110  parms[2] = 2.0;
111  parms[3] = pkht;
112  parms[4] = bkgd;
113  parms[5] = 0;
114  parms[6] = 0;
115 
116  double info[LM_INFO_SZ];
117 
118  int n = m_CenterFinder->get_PeakFitRadius()*2+1;
119 
120  niter = dlevmar_dif(QxrdFitterPeakPoint::staticEvaluate,
121  parms, NULL, 7, n*n,
122  m_CenterFinder->get_PeakFitIterations(),
123  NULL, info, NULL, NULL, this);
124 
125  if (niter > 0) {
127  m_FittedX = parms[0];
128  m_FittedY = parms[1];
129  m_FittedWidth = parms[2];
130  m_FittedHeight = parms[3];
131  m_FittedBkgd = parms[4];
132  m_FittedBkgdX = parms[5];
133  m_FittedBkgdY = parms[6];
134 
135  double dx = m_FittedX - m_X0;
136  double dy = m_FittedY - m_Y0;
137  double dr = sqrt(dx*dx + dy*dy);
138 
139  if ((fabs(m_FittedWidth)<m_CenterFinder->get_FittedWidthMin()) ||
140  (fabs(m_FittedWidth)>m_CenterFinder->get_FittedWidthMax())) {
141  m_Reason = BadWidth;
142  } else if ((m_FittedHeight/m_Pkht)<m_CenterFinder->get_FittedHeightMinRatio()) {
144  } else if (dr>m_CenterFinder->get_FittedPositionMaxDistance()) {
146  }
147  }
148  }
149  }
150  }
151 
152  return niter;
153 }
154 
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