Commit e5a96d3a authored by Laurent Belcour's avatar Laurent Belcour
Browse files

Fixing the command line argument class to handle vector of strings

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