13 static const double GOLDEN_RATIO = 1.618033988749894848;
14 static const double EPS = 1e-7;
15 static const double EPSqr = 1e-14;
16 inline void swap(
double& a,
double& b)
35 double& a,
double& b,
double& c,
36 double& fa,
double& fb,
double& fc)
53 c = b+ GOLDEN_RATIO*(b-a);
63 if (q>=0 && q<EPSqr) q=EPSqr;
64 else if (q<0 && q+EPSqr>0) q=-1.0*EPSqr;
70 double tol = EPS*(1.0+
std::max(std::fabs(b),std::fabs(c)));
73 if (du>=0 && du<tol) du=tol;
74 else if (du<0 && du+tol>0) du=-1.0*tol;
79 if ((u-c)<tol && (u-c)>=0) u+=tol;
80 else if ((c-u)<tol && (c-u)>=0) u-=tol;
82 double u_limit = b + 100*(c-b);
91 a=b; fa=fb; b=u; fb=fu;
101 if (a>c) {
swap(a,c);
swap(fa,fc); }
105 u = c+GOLDEN_RATIO*(c-b);
108 else if ((u-c)*(u_limit-u)>0.0)
114 a=b; fa=fb; b=c; fb=fc; c=u; fc=fu;
116 if (a>c) {
swap(a,c);
swap(fa,fc); }
120 else if ((u_limit-c)*(u-u_limit)>=0)
128 u = c+GOLDEN_RATIO*(c-b);
An object that represents a function from R^n -> R.
bool set_size(size_t n)
Resize to n elements.
double f(vnl_vector< double > const &x) override
The main function. Given the parameter vector x, compute the value of f(x).
void vnl_bracket_minimum(vnl_cost_function &fn, double &a, double &b, double &c, double &fa, double &fb, double &fc)
Given initial values a and b, find bracket a<b<c s.t. f(a)>f(b)<f(c).
Function to bracket a minimum.
vnl_decnum max(vnl_decnum const &x, vnl_decnum const &y)
void swap(vnl_matrix< T > &A, vnl_matrix< T > &B)
Swap two matrices.
vnl_bm_func(vnl_cost_function &fn)
double operator()(double x)
Function to fit a parabola to three point to predict the centre line.
void vnl_fit_parabola(double xa, double xb, double xc, double fa, double fb, double fc, double &p, double &q)
Fit a parabola so as to estimate the position of the centre line.