Commit e5a96d3a authored by Laurent Belcour's avatar Laurent Belcour

Fixing the command line argument class to handle vector of strings

parent fb08e7eb
......@@ -26,52 +26,59 @@ class arguments
{
}
arguments(int argc, char** const argv)
{
for(int i=0; i<argc; ++i)
{
std::string temp(argv[i]) ;
std::string key, data ;
{
for(int i=0; i<argc; ++i)
{
std::string temp(argv[i]) ;
std::string key, data ;
if(temp.compare(0, 2, "--") == 0)
{
key = temp.substr(2, temp.size()-2) ;
if(temp.compare(0, 2, "--") == 0)
{
//#define DEBUG_ARGS
key = temp.substr(2, temp.size()-2) ;
#ifdef DEBUG_ARGS
std::cout << "<<DEBUG>> (" << i << ")" << key << " -> [ ";
std::cout << "<<DEBUG>> (" << i << ")" << key << " -> [ ";
#endif
int k = i+1;
int j = k;
while(j < argc)
{
if(++i < argc)
{
std::string next(argv[j]) ;
if(next[0] == '[' || next[next.size()-1] == ']' || next.compare(0, 2, "--") != 0)
{
if(j != k)
{
data.append(" ");
std::string next(argv[i]) ;
if(next[0] == '[' && next[next.size()-1] != ']')
{
data.append(next);
++i;
while(i < argc && next[next.size()-1] != ']')
{
next = argv[i] ;
data.append(" ");
#ifdef DEBUG_ARGS
std::cout << " ";
std::cout << " ";
#endif
}
data.append(next);
data.append(next);
#ifdef DEBUG_ARGS
std::cout << "(" << j << ")" << next;
std::cout << "(" << i << ")" << next;
#endif
}
else
{
break ;
}
++j;
++i;
}
++i;
}
--i;
}
else
{
#ifdef DEBUG_ARGS
std::cout << "]" << std::endl;
std::cout << next;
#endif
}
_map.insert(std::pair<std::string, std::string>(key, data)) ;
}
}
data.append(next);
}
}
#ifdef DEBUG_ARGS
std::cout << "]" << std::endl;
#endif
}
_map.insert(std::pair<std::string, std::string>(key, data)) ;
}
}
~arguments()
{
}
......
......@@ -235,6 +235,8 @@ double dot(const vec& a, const vec& b);
vec product(const vec& a, const vec& b);
std::ostream& operator<<(std::ostream& out, const vec& v);
//! \brief locate the first index of value v in vector vec. Complexity in
//! O(n) is the worst case.
template<typename T> int is_in(std::vector<T> ve, T v)
......
......@@ -282,23 +282,23 @@ function* plugins_manager::get_function(const arguments& args)
//! functions in it.
compound_function* compound = new compound_function();
//! For each args_vec element, create a function object and add
//! it to the compound one.
for(unsigned int i=0; i<args_vec.size(); ++i)
{
std::string n("--func ");
n.append(args_vec[i]);
function* f = get_function(arguments::create_arguments(n));
if(dynamic_cast<nonlinear_function*>(f) == NULL)
{
std::cerr << "<<ERROR>> only non-linear function care compatible with a compound" << std::endl;
}
else
{
compound->push_back(dynamic_cast<nonlinear_function*>(f));
}
}
//! For each args_vec element, create a function object and add
//! it to the compound one.
for(unsigned int i=0; i<args_vec.size(); ++i)
{
std::string n("--func ");
n.append(args_vec[i]);
arguments temp_args = arguments::create_arguments(n);
function* f = get_function(temp_args);
if(dynamic_cast<nonlinear_function*>(f) == NULL)
{
std::cerr << "<<ERROR>> only non-linear function care compatible with a compound" << std::endl;
}
else
{
compound->push_back(dynamic_cast<nonlinear_function*>(f));
}
}
//! return the compound class
func = compound;
......
......@@ -148,14 +148,18 @@ bool nonlinear_fitter_ceres::fit_data(const data* d, function* fit, const argume
}
// Solves the NL problem
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);
std::cout << summary.BriefReport() << std::endl;
std::cout << "<<INFO>> found parameters: " << p << std::endl;
nf->setParameters(p);
return true;
// Solves the NL problem
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);
#ifdef DEBUG
std::cout << summary.BriefReport() << std::endl;
#endif
std::cout << "<<INFO>> found parameters: " << p << std::endl;
nf->setParameters(p);
return true;
}
void nonlinear_fitter_ceres::set_parameters(const arguments& args)
......
......@@ -127,5 +127,5 @@ vec schlick::getFresnelParametersJacobian(const vec& x) const
void schlick::fresnelBootstrap(const data* d, const arguments& args)
{
R = 0.1;
R = 0.5;
}
......@@ -22,8 +22,8 @@ ALTA_DLL_EXPORT fitter* provide_fitter()
struct EigenFunctor: Eigen::DenseFunctor<double>
{
EigenFunctor(nonlinear_function* f, const data* d) :
DenseFunctor<double>(f->nbParameters(), d->dimY()*d->size()), _f(f), _d(d)
EigenFunctor(nonlinear_function* f, const data* d, bool use_cosine) :
DenseFunctor<double>(f->nbParameters(), d->dimY()*d->size()), _f(f), _d(d), _cosine(use_cosine)
{
#ifndef DEBUG
std::cout << "<<DEBUG>> constructing an EigenFunctor for n=" << inputs() << " parameters and m=" << values() << " points" << std::endl ;
......@@ -45,12 +45,21 @@ struct EigenFunctor: Eigen::DenseFunctor<double>
{
vec _x = _d->get(s);
// Compute the cosine factor. Only update the constant if the flag
// is set in the object.
double cos = 1.0;
if(_cosine)
{
double cart[6]; params::convert(&_x[0], _d->input_parametrization(), params::CARTESIAN, cart);
cos = cart[5];
}
vec _di = vec(_f->dimY());
for(int i=0; i<_f->dimY(); ++i)
_di[i] = _x[_f->dimX() + i];
// Should add the resulting vector completely
vec _y = _di - (*_f)(_x);
vec _y = _di - cos*(*_f)(_x);
for(int i=0; i<_f->dimY(); ++i)
y(i*_d->size() + s) = _y[i];
......@@ -75,6 +84,15 @@ struct EigenFunctor: Eigen::DenseFunctor<double>
// Get the position
vec xi = _d->get(s);
// Compute the cosine factor. Only update the constant if the flag
// is set in the object.
double cos = 1.0;
if(_cosine)
{
double cart[6]; params::convert(&xi[0], _d->input_parametrization(), params::CARTESIAN, cart);
cos = cart[5];
}
// Get the associated jacobian
vec _jac = _f->parametersJacobian(xi);
......@@ -88,7 +106,7 @@ struct EigenFunctor: Eigen::DenseFunctor<double>
// vector row
for(int i=0; i<_f->dimY(); ++i)
{
fjac(i*_d->size() + s, j) = -_jac[i*_f->nbParameters() + j];
fjac(i*_d->size() + s, j) = - cos * _jac[i*_f->nbParameters() + j];
#ifdef DEBUG
temp(i, j) = _jac[i*_f->nbParameters() + j];
#endif
......@@ -108,6 +126,9 @@ struct EigenFunctor: Eigen::DenseFunctor<double>
nonlinear_function* _f;
const data* _d;
// Flags
bool _cosine;
};
nonlinear_fitter_eigen::nonlinear_fitter_eigen()
......@@ -153,7 +174,7 @@ bool nonlinear_fitter_eigen::fit_data(const data* d, function* fit, const argume
x[i] = nf_x[i];
}
EigenFunctor functor(nf, d);
EigenFunctor functor(nf, d, args.is_defined("fit-with-cosine"));
Eigen::LevenbergMarquardt<EigenFunctor> lm(functor);
info = lm.minimize(x);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment