QXRD  0.11.16
qxrdfitterringcircle.cpp
Go to the documentation of this file.
1 #include "qxrdfitterringcircle.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 QxrdFitterRingCircle::QxrdFitterRingCircle(QxrdCenterFinder *cf, int ringIndex, double x0, double y0) :
15  QxrdFitter(cf),
16  m_RingIndex(ringIndex),
17  m_X0(x0),
18  m_Y0(y0),
19  m_FittedX(0),
20  m_FittedY(0),
21  m_FittedR(0)
22 {
23 }
24 
26  QxrdFitter(),
27  m_RingIndex(-1),
28  m_X0(0),
29  m_Y0(0),
30  m_FittedX(0),
31  m_FittedY(0),
32  m_FittedR(0)
33 {
34 }
35 
36 void QxrdFitterRingCircle::staticEvaluate(double *p, double *hx, int m, int n, void *adata)
37 {
39 
40  if (rf) {
41  rf->evaluate(p,hx,m,n);
42  }
43 }
44 
45 void QxrdFitterRingCircle::evaluate(double *parm, double *xv, int /*np*/, int nx)
46 {
47  if (m_CenterFinder) {
48  double cx = parm[0];
49  double cy = parm[1];
50  double r = parm[2];
51 
52  for (int i=0; i<nx; i++) {
54 
55  if (pt.isValid()) {
56  double dx = pt.x() - cx;
57  double dy = pt.y() - cy;
58  double rcalc = sqrt(dx*dx + dy*dy);
59 
60  xv[i] = rcalc - r;
61  }
62  }
63  }
64 }
65 
67 {
68  int niter = -1;
69 
70  if (m_CenterFinder) {
71 
73  double rsum = 0;
74 
75  for (int i=0; i<npts; i++) {
77 
78  double dx = pt.x() - m_X0;
79  double dy = pt.y() - m_Y0;
80 
81  rsum += sqrt(dx*dx + dy*dy);
82  }
83 
84  double parms[3];
85  double info[LM_INFO_SZ];
86 
87  parms[0] = m_X0;
88  parms[1] = m_Y0;
89  parms[2] = rsum/npts;
90 
91  niter = dlevmar_dif(QxrdFitterRingCircle::staticEvaluate,
92  parms, NULL, 3, npts,
93  m_CenterFinder->get_PeakFitIterations(),
94  NULL, info, NULL, NULL, this);
95 
96  if (niter > 0) {
98 
99  m_FittedX = parms[0];
100  m_FittedY = parms[1];
101  m_FittedR = parms[2];
102  } else {
103  m_Reason = NoResult;
104  }
105  }
106 
107  return niter;
108 }
double y() const
FitResult m_Reason
Definition: qxrdfitter.h:32
int countPowderRingPoints() const
void evaluate(double *parm, double *xv, int np, int nx)
QxrdCenterFinder * m_CenterFinder
Definition: qxrdfitter.h:31
QxrdPowderPoint powderRingPoint(int i) const
bool isValid() const
double x() const
static void staticEvaluate(double *parm, double *xv, int np, int nx, void *adata)