Commit 2a1ac82b authored by Laurent Belcour's avatar Laurent Belcour

Changing the way the functions are loaded

parent bccc682f
......@@ -29,7 +29,7 @@ class function : public parametrized
virtual vec value(const vec& x) const = 0 ;
//! Load function specific files
virtual void load(const std::string& filename) = 0 ;
virtual void load(std::istream& in) = 0 ;
//! \brief Provide a first rough fit of the function.
//!
......@@ -278,11 +278,11 @@ class compound_function: public nonlinear_function, public std::vector<nonlinear
}
//! Load function specific files
virtual void load(const std::string& filename)
virtual void load(std::istream& in)
{
for(int i=0; i<this->size(); ++i)
{
this->at(i)->load(filename);
this->at(i)->load(in);
}
}
......@@ -466,11 +466,11 @@ class fresnel : public nonlinear_function
}
//! Load function specific files
virtual void load(const std::string& filename)
virtual void load(std::istream& in)
{
if(f != NULL)
{
f->load(filename);
f->load(in);
}
else
{
......
......@@ -247,7 +247,7 @@ function* plugins_manager::get_function(const std::string& filename)
f->setDimY(nY);
// Load the function part from the file object
//f->load(file);
f->load(file);
return f;
}
......
......@@ -24,7 +24,7 @@ rational_function_1d::rational_function_1d(const std::vector<double>& a,
{
}
void rational_function_1d::load(const std::string& filename)
void rational_function_1d::load(std::istream& in)
{
}
......@@ -366,16 +366,8 @@ vec rational_function::value(const vec& x) const
}
// IO function to text files
void rational_function::load(const std::string& filename)
void rational_function::load(std::istream& file)
{
std::ifstream file(filename.c_str()) ;
if(!file.is_open())
{
std::cerr << "<<ERROR>> unable to open file \"" << filename << "\"" << std::endl ;
throw ;
}
int nX, nY ;
vec xmin, xmax ;
vec a, b;
......
......@@ -36,7 +36,7 @@ class rational_function_1d : public function
virtual double q(const vec& x, int j) const ;
// IO function to text files
virtual void load(const std::string& filename) ;
virtual void load(std::istream& in);
// Update the function
virtual void update(const std::vector<double>& in_a,
......@@ -89,7 +89,7 @@ class rational_function : public function
virtual vec operator()(const vec& x) const { return value(x) ; }
// IO function to text files
virtual void load(const std::string& filename) ;
virtual void load(std::istream& in) ;
// Update the function
virtual void update(int i, rational_function_1d* r) ;
......
......@@ -31,9 +31,29 @@ vec diffuse_function::value(const vec& x) const
}
//! Load function specific files
void diffuse_function::load(const std::string& filename)
void diffuse_function::load(std::istream &in)
{
NOT_IMPLEMENTED();
// Parse line until the next comment
while(in.peek() != '#')
{
char line[256];
in.getline(line, 256);
}
// Checking for the comment line #FUNC nonlinear_diffuse
std::string token;
in >> token;
if(token != "FUNC") { std::cerr << "<<ERROR>> parsing the stream. The #FUNC is not the next line defined." << std::endl; }
in >> token;
if(token != "nonlinear_diffuse") { std::cerr << "<<ERROR>> parsing the stream. function name is not the next token." << std::endl; }
// kd [double]
for(int i=0; i<dimY(); ++i)
{
in >> token >> _kd[i];
}
}
void diffuse_function::save_alta(const std::string& filename, const arguments& args) const
......
......@@ -27,7 +27,7 @@ class diffuse_function : public nonlinear_function
virtual void bootstrap(const data* d, const arguments& args);
//! \brief Load function specific files
virtual void load(const std::string& filename) ;
virtual void load(std::istream& in) ;
//! \brief Number of parameters to this non-linear function
virtual int nbParameters() const ;
......
......@@ -306,82 +306,44 @@ std::ofstream& type_affectation(std::ofstream& out, const std::string& name, con
//! Load function specific files
void isotropic_lafortune_function::load(const std::string& filename)
void isotropic_lafortune_function::load(std::istream& in)
{
std::ifstream file(filename.c_str()) ;
if(!file.is_open())
// Parse line until the next comment
while(in.peek() != '#')
{
std::cerr << "<<ERROR>> unable to open file \"" << filename << "\"" << std::endl ;
throw ;
char line[256];
in.getline(line, 256);
}
_nX = 0 ; _nY = 0 ;
_n = 0;
// Checking for the comment line #FUNC nonlinear_diffuse
std::string token;
in >> token;
if(token != "FUNC") { std::cerr << "<<ERROR>> parsing the stream. The #FUNC is not the next line defined." << std::endl; }
double x, y, dy ;
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("NB_LOBES"))
{
linestream >> _n ;
}
}
in >> token;
if(token != "nonlinear_lafortune") { std::cerr << "<<ERROR>> parsing the stream. function name is not the next token." << std::endl; }
_kd = vec(_nY);
setNbLobes(_n);
// Parse the diffuse
for(int i=0; i<_nY; ++i)
{
file >> _kd[i];
}
// Shoudl have the #NB_LOBES [int]
int nb_lobes;
in >> token >> nb_lobes;
setNbLobes(nb_lobes);
// Parse the lobe
int n=0;
std::string line ;
while(n < _n)
for(int n=0; n<_n; ++n)
{
std::getline(file, line) ;
if(line.size() > 1)
for(int i=0; i<_nY; ++i)
{
std::string sub = line.substr(0,2);
if(sub == "#C")
{
for(int i=0; i<_nY; ++i)
{
file >> _C[(n*_nY + i)*2 + 0] >> _C[(n*_nY + i)*2 + 1];
}
}
else if(sub == "#N")
{
for(int i=0; i<_nY; ++i)
{
file >> _N[n*_nY+i];
}
++n;
}
in >> token >> _C[(n*_nY + i)*2 + 0];
in >> token >> _C[(n*_nY + i)*2 + 1];
in >> token >> _N[i];
}
}
std::cout << "<<INFO>> Kd = " << _kd << std::endl;
std::cout << "<<INFO>> Cd = " << _C << std::endl;
std::cout << "<<INFO>> N = " << _N << std::endl;
}
void isotropic_lafortune_function::save(const std::string& filename) const
......
......@@ -44,7 +44,7 @@ class isotropic_lafortune_function : public nonlinear_function
virtual vec value(const vec& x, const vec& p) const;
//! \brief Load function specific files
virtual void load(const std::string& filename) ;
virtual void load(std::istream& in) ;
//! \brief Boostrap the function by defining the diffuse term
//!
......
......@@ -349,80 +349,42 @@ std::ofstream& type_affectation(std::ofstream& out, const std::string& name, con
//! Load function specific files
void lafortune_function::load(const std::string& filename)
void lafortune_function::load(std::istream& in)
{
std::ifstream file(filename.c_str()) ;
if(!file.is_open())
{
std::cerr << "<<ERROR>> unable to open file \"" << filename << "\"" << std::endl ;
throw ;
}
_nX = 0 ; _nY = 0 ;
_n = 0;
double x, y, dy ;
while(file.peek() == '#')
{
std::string line ;
std::getline(file, line) ;
std::stringstream linestream(line) ;
linestream.ignore(1) ;
// Parse line until the next comment
while(in.peek() != '#')
{
char line[256];
in.getline(line, 256);
}
std::string comment ;
linestream >> comment ;
// Checking for the comment line #FUNC nonlinear_diffuse
std::string token;
in >> token;
if(token != "FUNC") { std::cerr << "<<ERROR>> parsing the stream. The #FUNC is not the next line defined." << std::endl; }
if(comment == std::string("DIM"))
{
linestream >> _nX >> _nY ;
}
else if(comment == std::string("NB_LOBES"))
{
linestream >> _n ;
}
}
in >> token;
if(token != "nonlinear_lafortune") { std::cerr << "<<ERROR>> parsing the stream. function name is not the next token." << std::endl; }
_kd = vec(_nY);
setNbLobes(_n);
// Parse the diffuse
for(int i=0; i<_nY; ++i)
{
file >> _kd[i];
}
// Shoudl have the #NB_LOBES [int]
int nb_lobes;
in >> token >> nb_lobes;
setNbLobes(nb_lobes);
// Parse the lobe
for(int n=0; n<_n; ++n)
{
// TODO find a way to discard those lines
while(file.peek() == '#')
{
std::string line ;
std::getline(file, line) ;
}
// std::cout << (char)file.peek() << std::endl;
for(int i=0; i<_nY; ++i)
{
file >> _C[(n*_nY + i)*3 + 0] >> _C[(n*_nY + i)*3 + 1] >> _C[(n*_nY + i)*3 + 2];
}
// TODO find a way to discard those lines
while(file.peek() == '#')
{
std::string line ;
std::getline(file, line) ;
}
for(int i=0; i<_nY; ++i)
{
file >> _N[i];
in >> token >> _C[(n*_nY + i)*3 + 0];
in >> token >> _C[(n*_nY + i)*3 + 1];
in >> token >> _C[(n*_nY + i)*3 + 2];
in >> token >> _N[i];
}
}
std::cout << "<<INFO>> Kd = " << _kd << std::endl;
std::cout << "<<INFO>> Cd = " << _C << std::endl;
std::cout << "<<INFO>> N = " << _N << std::endl;
}
......
......@@ -36,7 +36,7 @@ class lafortune_function : public nonlinear_function
virtual vec value(const vec& x, const vec& p);
//! \brief Load function specific files
virtual void load(const std::string& filename) ;
virtual void load(std::istream& in) ;
//! \brief Boostrap the function by defining the diffuse term
virtual void bootstrap(const data* d, const arguments& args);
......
......@@ -31,10 +31,30 @@ vec phong_function::value(const vec& x) const
}
//! Load function specific files
void phong_function::load(const std::string& filename)
void phong_function::load(std::istream& in)
{
std::cerr << "Not implemented " << __FILE__ << ":" << __LINE__ << std::endl;
throw;
// Parse line until the next comment
while(in.peek() != '#')
{
char line[256];
in.getline(line, 256);
}
// Checking for the comment line #FUNC nonlinear_diffuse
std::string token;
in >> token;
if(token != "FUNC") { std::cerr << "<<ERROR>> parsing the stream. The #FUNC is not the next line defined." << std::endl; }
in >> token;
if(token != "nonlinear_phong") { std::cerr << "<<ERROR>> parsing the stream. function name is not the next token." << std::endl; }
// ks [double]
// N [double]
for(int i=0; i<dimY(); ++i)
{
in >> token >> _ks[i];
in >> token >> _N[i];
}
}
//! Number of parameters to this non-linear function
......
......@@ -50,7 +50,7 @@ class phong_function : public nonlinear_function
virtual void bootstrap(const data* d, const arguments& args);
//! \brief Load function specific files
virtual void load(const std::string& filename) ;
virtual void load(std::istream& filename) ;
//! \brief Number of parameters to this non-linear function
virtual int nbParameters() const ;
......
......@@ -51,8 +51,7 @@ int main(int argc, char** argv)
d = manager.get_data(args["data"]) ;
function* f = NULL;
f = manager.get_function(args);
f->load(args["input"]);
f = manager.get_function(args["input"]);
// Modify function or data to provide coherent
// interfaces
......
......@@ -34,7 +34,7 @@ int main(int argc, char** argv)
return 1 ;
}
function* f = manager.get_function(args) ;
function* f = manager.get_function(args["input"]) ;
data* d = NULL ;
if(args.is_defined("data"))
......@@ -57,9 +57,6 @@ int main(int argc, char** argv)
linear_plot = true;
}
// Load the BRDF
f->load(args["input"]);
// Create output file
std::ofstream file(args["output"].c_str(), std::ios_base::trunc);
......
......@@ -43,8 +43,7 @@ int main(int argc, char** argv)
// Import data
function* f = NULL ;
f = manager.get_function(args) ;
f->load(args["input"]);
f = manager.get_function(args["input"]) ;
// Create output file
std::ofstream file(args["output"].c_str(), std::ios_base::trunc);
......
......@@ -49,15 +49,15 @@ int main(int argc, char** argv)
{
fit->set_parameters(args) ;
function* f = plugins_manager::get_function(args);
function* f = plugins_manager::get_function(args);
data* d = plugins_manager::get_data(args["data"]);
d->load(args["input"], args);
if(f == NULL || d == NULL)
{
std::cerr << "<<ERROR>> no function or data object correctly defined" << std::endl;
return 1;
}
if(f == NULL || d == NULL)
{
std::cerr << "<<ERROR>> no function or data object correctly defined" << std::endl;
return 1;
}
// Check the compatibility between the data and the function
plugins_manager::check_compatibility(d, f, args);
......
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