Commit df821dba authored by Laurent Belcour's avatar Laurent Belcour

Adding save header and load header for functions

parent 4923d85e
#pragma once
#include <string>
#include <sstream>
#include <map>
#include <cstdlib>
#include <iostream>
......@@ -223,6 +224,58 @@ class arguments
return res;
}
//! \brief get the reconstructed command line arguments (without
//! the executable name
std::string get_cmd() const
{
std::string cmd;
std::map<std::string, std::string>::const_iterator it = _map.begin();
for(;it != _map.end(); it++)
{
cmd.append(it->first);
cmd.append(" ");
cmd.append(it->second);
}
return cmd;
}
//! \brief Create an argument object from a string describing a command
//! line.
static arguments create_arguments(const std::string& n)
{
std::vector<std::string> cmd_vec;
std::stringstream stream(n);
#ifdef DEBUG_ARGS
std::cout << "<<DEBUG>> create argument vector: [";
#endif
while(stream.good())
{
std::string temp;
stream >> temp;
#ifdef DEBUG_ARGS
std::cout << temp << ", ";
#endif
cmd_vec.push_back(temp);
}
#ifdef DEBUG_ARGS
std::cout << "]" << std::endl;
#endif
int argc = cmd_vec.size();
char* argv[argc];
for(int i=0; i<argc; ++i)
{
argv[i] = &cmd_vec[i][0];
}
arguments current_args(argc, argv);
return current_args;
}
private: // data
std::map<std::string, std::string> _map ;
......
......@@ -69,7 +69,7 @@ class function : public parametrized
}
else
{
save(filename) ;
save_alta(filename, args) ;
}
}
......@@ -104,7 +104,7 @@ class function : public parametrized
protected: // function
//! \brief Standard saving function.
virtual void save(const std::string& filename) const
virtual void save_alta(const std::string& filename, const arguments& args) const
{
NOT_IMPLEMENTED();
}
......@@ -156,6 +156,55 @@ class function : public parametrized
NOT_IMPLEMENTED();
}
//! \brief parse the header of the file and return the corresponding
//! arguments and associate stream
void load_header(const std::string& filename, arguments& args, std::ifstream& file)
{
file.open(filename.c_str()) ;
if(!file.is_open())
{
std::cerr << "<<ERROR>> unable to open file \"" << filename << "\"" << std::endl ;
throw ;
}
while(file.peek() == '#')
{
std::string line ;
std::getline(file, line) ;
std::stringstream linestream(line) ;
linestream.ignore(1) ;
std::string comment ;
linestream >> comment ;
if(comment == std::string("DIM"))
{
linestream >> _nX >> _nY ;
}
else if(comment == std::string("CMD"))
{
args = arguments::create_arguments(line.substr(5, std::string::npos));
}
}
}
void save_header(const std::string& filename, arguments& args, std::ofstream& file)
{
file.open(filename.c_str()) ;
if(!file.is_open())
{
std::cerr << "<<ERROR>> unable to open file \"" << filename << "\"" << std::endl ;
throw ;
}
file << "#CMD " << args.get_cmd() << std::endl;
file << "#DIM " << _nX << " " << _nY << std::endl;
file << "#PARAM_IN " << params::get_name(input_parametrization()) << std::endl;
//file << "#PARAM_OUT " << params::get_name(output_parametrization()) << std::endl;
file << std::endl;
}
public: // methods
//! \brief L2 norm to data.
......
......@@ -194,38 +194,6 @@ fitter* plugins_manager::get_fitter()
#endif
}
arguments create_arguments(const std::string& n)
{
std::vector<std::string> cmd_vec;
std::stringstream stream(n);
#ifdef DEBUG_ARGS
std::cout << "<<DEBUG>> create argument vector: [";
#endif
while(stream.good())
{
std::string temp;
stream >> temp;
#ifdef DEBUG_ARGS
std::cout << temp << ", ";
#endif
cmd_vec.push_back(temp);
}
#ifdef DEBUG_ARGS
std::cout << "]" << std::endl;
#endif
int argc = cmd_vec.size();
char* argv[argc];
for(int i=0; i<argc; ++i)
{
argv[i] = &cmd_vec[i][0];
}
arguments current_args(argc, argv);
return current_args;
}
//! Get an instance of the function selected based on the name <em>n</em>.
//! Return NULL if no one exist.
function* plugins_manager::get_function(const arguments& args)
......@@ -259,7 +227,7 @@ function* plugins_manager::get_function(const arguments& args)
std::string n("--func ");
n.append(args_vec[0]);
func = get_function(create_arguments(n));
func = get_function(arguments::create_arguments(n));
//! return the compound class
......@@ -317,7 +285,7 @@ function* plugins_manager::get_function(const arguments& args)
}
}
fresnel* func_fres = dynamic_cast<fresnel*>(get_function(create_arguments(n)));
fresnel* func_fres = dynamic_cast<fresnel*>(get_function(arguments::create_arguments(n)));
func_fres->setBase(nl_func);
func = dynamic_cast<function*>(func_fres);
}
......
......@@ -14,12 +14,14 @@ ALTA_DLL_EXPORT function* provide_function()
return new schlick();
}
// Overload the function operator
vec schlick::operator()(const vec& x) const
//! Load function specific files
void schlick::load(const std::string& filename)
{
return value(x);
std::cerr << "Cannot load a Schlick file." << std::endl;
throw;
}
vec schlick::value(const vec& x) const
vec schlick::fresnelValue(const vec& x) const
{
vec res(_nY);
for(int i=0; i<_nY; ++i)
......@@ -30,37 +32,39 @@ vec schlick::value(const vec& x) const
return res;
}
//! Load function specific files
void schlick::load(const std::string& filename)
{
std::cerr << "Cannot load a Schlick file." << std::endl;
throw;
}
//! Number of parameters to this non-linear function
int schlick::nbParameters() const
//! \brief Number of parameters to this non-linear function
int schlick::nbFresnelParameters() const
{
return 0;
return dimY();
}
//! Get the vector of parameters for the function
vec schlick::parameters() const
//! \brief Get the vector of parameters for the function
vec schlick::getFresnelParameters() const
{
vec r(0);
return r;
vec p(1);
p[0] = R;
return p;
}
//! Update the vector of parameters for the function
void schlick::setParameters(const vec& p)
//! \brief Update the vector of parameters for the function
void schlick::setFresnelParameters(const vec& p)
{
R = p[0];
}
//! Obtain the derivatives of the function with respect to the
//! \brief Obtain the derivatives of the function with respect to the
//! parameters.
vec schlick::parametersJacobian(const vec& x) const
vec schlick::getFresnelParametersJacobian(const vec& x) const
{
vec r(0);
return r;
const int nY = dimY();
vec jac(nY);
for(int i=0; i<nY; ++i)
{
jac[i] = 1.0 - pow(1.0 - clamp(x[0], 0.0, 1.0), 5.0);
}
return jac;
}
......
......@@ -12,51 +12,35 @@
#include <core/common.h>
class schlick : public nonlinear_function
class schlick : public fresnel
{
public: // methods
// Overload the function operator
virtual vec operator()(const vec& x) const ;
virtual vec value(const vec& x) const ;
//! \brief Boostrap the function by defining the diffuse term
virtual void bootstrap(const data* d, const arguments& args);
//! \brief Load function specific files
virtual void load(const std::string& filename) ;
protected: // methods
virtual vec fresnelValue(const vec& x) const;
//! \brief Number of parameters to this non-linear function
virtual int nbParameters() const ;
virtual int nbFresnelParameters() const ;
//! \brief Get the vector of parameters for the function
virtual vec parameters() const ;
virtual vec getFresnelParameters() const ;
//! \brief Update the vector of parameters for the function
virtual void setParameters(const vec& p) ;
virtual void setFresnelParameters(const vec& p) ;
//! \brief Obtain the derivatives of the function with respect to the
//! parameters.
virtual vec parametersJacobian(const vec& x) const ;
//! \brief Provide the dimension of the input space of the function
inline virtual int dimX() const
{
return 1;
}
inline virtual int dimY() const
{
return 1;
}
//! \brief Provide the parametrization of the input space of the function.
//! For this one, we fix that the parametrization is in THETAD_PHID
virtual params::input parametrization() const;
virtual void setParametrization(params::input new_param);
protected: // methods
virtual vec getFresnelParametersJacobian(const vec& x) const ;
private: // data
......
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