Une MAJ de sécurité est nécessaire sur notre version actuelle. Elle sera effectuée lundi 02/08 entre 12h30 et 13h. L'interruption de service devrait durer quelques minutes (probablement moins de 5 minutes).

Commit 132bb66e authored by Laurent Belcour's avatar Laurent Belcour
Browse files

Changing the name of the Blinn repository to fit the correct value

Also refactored the code to fit better the current API
parent 63e37eb1
......@@ -11,27 +11,27 @@
ALTA_DLL_EXPORT function* provide_function()
{
return new phong_function();
return new blinn_function();
}
// Overload the function operator
vec phong_function::operator()(const vec& x) const
vec blinn_function::operator()(const vec& x) const
{
return value(x);
}
vec phong_function::value(const vec& x) const
vec blinn_function::value(const vec& x) const
{
vec res(dimY());
for(int i=0; i<dimY(); ++i)
{
res[i] = _kd[i] + _ks[i] * std::pow(x[0], _N[i]);
res[i] = _ks[i] * std::pow(x[0], _N[i]);
}
return res;
}
//! Load function specific files
void phong_function::load(std::istream& in)
void blinn_function::load(std::istream& in)
{
// Parse line until the next comment
while(in.peek() != '#')
......@@ -40,13 +40,13 @@ void phong_function::load(std::istream& in)
in.getline(line, 256);
}
// Checking for the comment line #FUNC nonlinear_function_phong
// Checking for the comment line #FUNC nonlinear_function_blinn
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_function_phong") { std::cerr << "<<ERROR>> parsing the stream. function name is not the next token." << std::endl; }
if(token != "nonlinear_function_blinn") { std::cerr << "<<ERROR>> parsing the stream. function name is not the next token." << std::endl; }
// ks [double]
// N [double]
......@@ -58,57 +58,37 @@ void phong_function::load(std::istream& in)
}
//! Number of parameters to this non-linear function
int phong_function::nbParameters() const
int blinn_function::nbParameters() const
{
#ifdef FIT_DIFFUSE
return 3*dimY();
#else
return 2*dimY();
#endif
}
//! Get the vector of parameters for the function
vec phong_function::parameters() const
vec blinn_function::parameters() const
{
#ifdef FIT_DIFFUSE
vec res(3*dimY());
for(int i=0; i<dimY(); ++i)
{
res[i*3 + 0] = _kd[i];
res[i*3 + 1] = _ks[i];
res[i*3 + 2] = _N[i];
}
#else
vec res(2*dimY());
for(int i=0; i<dimY(); ++i)
{
res[i*2 + 0] = _ks[i];
res[i*2 + 1] = _N[i];
}
#endif
return res;
}
//! Update the vector of parameters for the function
void phong_function::setParameters(const vec& p)
void blinn_function::setParameters(const vec& p)
{
for(int i=0; i<dimY(); ++i)
{
#ifdef FIT_DIFFUSE
_kd[i] = p[i*3 + 0];
_ks[i] = p[i*3 + 1];
_N[i] = p[i*3 + 2];
#else
_ks[i] = p[i*2 + 0];
_N[i] = p[i*2 + 1];
#endif
}
}
//! Obtain the derivatives of the function with respect to the
//! parameters.
vec phong_function::parametersJacobian(const vec& x) const
vec blinn_function::parametersJacobian(const vec& x) const
{
vec jac(dimY()*nbParameters());
for(int i=0; i<dimY(); ++i)
......@@ -116,40 +96,20 @@ vec phong_function::parametersJacobian(const vec& x) const
{
if(i == j)
{
#ifdef FIT_DIFFUSE
// df / dk_d
jac[i*nbParameters() + j*3+0] = 1.0;
// df / dk_s
jac[i*nbParameters() + j*3+1] = std::pow(x[0], _N[j]);
jac[i*nbParameters() + j*2+0] = std::pow(x[0], _N[j]);
// df / dN
if(x[0] == 0.0)
jac[i*nbParameters() + j*3+2] = 0.0;
jac[i*nbParameters() + j*2+1] = 0.0;
else
jac[i*nbParameters() + j*3+2] = _ks[j] * std::log(x[0]) * std::pow(x[0], _N[j]);
#else
// df / dk_s
jac[i*nbParameters() + j*2+0] = std::pow(x[0], _N[j]);
// df / dN
if(x[0] == 0.0)
jac[i*nbParameters() + j*2+1] = 0.0;
else
jac[i*nbParameters() + j*2+1] = _ks[j] * std::log(x[0]) * std::pow(x[0], _N[j]);
#endif
}
jac[i*nbParameters() + j*2+1] = _ks[j] * std::log(x[0]) * std::pow(x[0], _N[j]);
}
else
{
#ifdef FIT_DIFFUSE
jac[i*nbParameters() + j*3+0] = 0.0;
jac[i*nbParameters() + j*3+1] = 0.0;
jac[i*nbParameters() + j*3+2] = 0.0;
#else
jac[i*nbParameters() + j*2+0] = 0.0;
jac[i*nbParameters() + j*2+1] = 0.0;
#endif
jac[i*nbParameters() + j*2+0] = 0.0;
jac[i*nbParameters() + j*2+1] = 0.0;
}
}
......@@ -157,87 +117,68 @@ vec phong_function::parametersJacobian(const vec& x) const
}
void phong_function::bootstrap(const data* d, const arguments& args)
void blinn_function::bootstrap(const data* d, const arguments& args)
{
vec x0 = d->get(0);
for(int i=0; i<d->dimY(); ++i)
_kd[i] = x0[d->dimX() + i];
for(int i=1; i<d->size(); ++i)
for(int i=0; i<dimY(); ++i)
{
vec xi = d->get(i);
for(int j=0; j<d->dimY(); ++j)
_kd[j] = std::min(xi[d->dimX() + j], _kd[j]);
_ks[i] = 1.0;
_N[i] = 1.0;
}
}
std::ofstream& type_affectation(std::ofstream& out,
const std::string& name,
const vec& x, int nY)
void blinn_function::save_call(std::ostream& out,
const arguments& args) const
{
if(nY == 1)
out << "float " ;
else
out << "vec" << nY << " ";
out << name << " = ";
bool is_alta = !args.is_defined("export") || args["export"] == "alta";
if(nY != 1)
out << "vec" << nY << "(";
if(is_alta)
{
out << "#FUNC nonlinear_function_blinn" << std::endl ;
for(int i=0; i<_nY; ++i)
{
out << "Ks " << _ks[i] << std::endl;
out << "N " << _N[i] << std::endl;
}
for(int i=0; i<nY; ++i)
{
if(i != 0) out << ", ";
out << x[i];
}
out << std::endl;
}
else
{
out << "blinn(L, V, N, X, Y, vec3(";
for(int i=0; i<_nY; ++i)
{
out << _ks[i];
if(i < _nY-1) { out << ", "; }
}
if(nY != 1)
out << ")";
out << "), vec3(";
for(int i=0; i<_nY; ++i)
{
out << _N[i];
if(i < _nY-1) { out << ", "; }
}
out << ";" << std::endl;
out << "))";
}
return out;
}
//! \brief Output the function using a BRDF Explorer formating.
void phong_function::save_brdfexplorer(const std::string& filename,
const arguments& args) const
void blinn_function::save_body(std::ostream& out,
const arguments& args) const
{
std::ofstream file(filename.c_str(), std::ios_base::trunc);
file << "analytic" << std::endl;
file << std::endl;
// file << "::begin parameters" << std::endl;
// file << "::end parameters" << std::endl;
// file << std::endl;
file << std::endl;
file << "::begin shader" << std::endl;
type_affectation(file, std::string("n"), _N, _nY);
type_affectation(file, std::string("kd"), _kd, _nY);
type_affectation(file, std::string("ks"), _ks, _nY);
file << std::endl;
file << "const float PI = 3.14159265358979323846;" << std::endl;
file << std::endl;
file << "vec3 BRDF( vec3 L, vec3 V, vec3 N, vec3 X, vec3 Y )" << std::endl;
file << "{" << std::endl;
file << " vec3 H = normalize(L+V);" << std::endl;
if(_nY == 1)
{
file << " float D = kd + ks * pow(max(0, dot(N,H)),n);" << std::endl;
}
else
bool is_shader = args["export"] == "shader" || args["export"] == "explorer";
if(is_shader)
{
file << " vec" << _nY << " D = kd + ks * pow(vec" << _nY << "(max(0.0, dot(N,H))),n);" << std::endl;
out << "vec3 blinn(vec3 L, vec3 V, vec3 N, vec3 X, vec3 Y, vec3 ks, vec3 Nl)" << std::endl;
out << "{" << std::endl;
out << "\tvec3 H = normalize(L + V);" << std::endl;
out << "\tvec3 ext_dot = vec3(dot(H,N));" << std::endl;
out << "\treturn pow(max(ext_dot, vec3(0,0,0)), Nl);" << std::endl;
out << "}" << std::endl;
}
file << " return vec3(D);" << std::endl;
// file << " if (normalized)" << std::endl;
// file << " D *= (2+n) / (2*PI);" << std::endl;
file << "}" << std::endl;
file << std::endl;
file << "::end shader" << std::endl;
file.close();
}
#pragma once
// Include STL
#include <vector>
#include <string>
// Interface
#include <core/function.h>
#include <core/data.h>
#include <core/args.h>
#include <core/common.h>
/*! \brief A blinn lobe class. It is provided for testing with the nonlinear
* fitting algorithms.
*
* \details
* A blinn lobe is defined as \f$k_s |N.H|^a\f$
* \todo Finish implementation
*/
class blinn_function : public nonlinear_function
{
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(std::istream& filename) ;
//! \brief Number of parameters to this non-linear function
virtual int nbParameters() const ;
//! \brief Get the vector of parameters for the function
virtual vec parameters() const ;
//! \brief Update the vector of parameters for the function
virtual void setParameters(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
virtual int dimX() const
{
return 1 ;
}
//! \brief Provide the parametrization of the input space of the
//! function.
virtual params::input parametrization() const
{
return params::COS_TH ;
}
virtual void setParametrization(params::input new_param)
{
std::cerr << "Cannot change the ouput parametrization "
<< __FILE__ << ":" << __LINE__ << std::endl;
throw;
}
void setDimY(int nY)
{
_nY = nY ;
// Update the length of the vectors
_ks.resize(_nY) ;
_N.resize(_nY) ;
}
void save_call(std::ostream& out, const arguments& args) const;
void save_body(std::ostream& out, const arguments& args) const;
private: // data
//! \brief The blinn lobe data
vec _ks, _N;
} ;
#pragma once
// Include STL
#include <vector>
#include <string>
// Interface
#include <core/function.h>
#include <core/rational_function.h>
#include <core/data.h>
#include <core/fitter.h>
#include <core/args.h>
#include <core/common.h>
/*! \brief A phong lobe class. It is provided for testing with the nonlinear
* fitting algorithms.
*
* \details
* A phong lobe is defined as \f$k_d + k_s |N.H|^a\f$
* \todo Finish implementation
*/
class phong_function : public nonlinear_function
{
public: // methods
// Overload the function operator
virtual vec operator()(const vec& x) const ;
virtual vec value(const vec& x) const ;
virtual vec value(const vec& x, const vec& p) const
{
// Test input parameters for correct size
assert(p.size() == nbParameters());
vec res(dimY());
for(int i=0; i<dimY(); ++i)
{
const double kd = p[i*3 + 0];
const double ks = p[i*3 + 1];
const double N = p[i*3 + 2];
res[i] = kd + ks * std::pow(x[0], N);
}
return res;
}
//! \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(std::istream& filename) ;
//! \brief Number of parameters to this non-linear function
virtual int nbParameters() const ;
//! \brief Get the vector of parameters for the function
virtual vec parameters() const ;
//! \brief Update the vector of parameters for the function
virtual void setParameters(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
virtual int dimX() 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
{
return params::COS_TH ;
}
virtual void setParametrization(params::input new_param)
{
std::cerr << "Cannot change the ouput parametrization " << __FILE__ << ":" << __LINE__ << std::endl;
throw;
}
void setDimY(int nY)
{
_nY = nY ;
// Update the length of the vectors
_kd.resize(_nY) ;
_ks.resize(_nY) ;
_N.resize(_nY) ;
}
protected: // methods
virtual void save(const std::string& filename) const
{
std::ofstream file(filename.c_str(), std::ios_base::trunc);
file << "#DIM " << _nX << " " << _nY << std::endl ;
file << "#kd" << std::endl;
for(int i=0; i<_nY; ++i)
{
file << _kd[i] << std::endl;
}
file << std::endl;
file << "#ks" << std::endl;
for(int i=0; i<_nY; ++i)
{
file << _ks[i] << std::endl;
}
file << std::endl;
file << "#N" << std::endl;
for(int i=0; i<_nY; ++i)
{
file << _N[i] << std::endl;
}
file << std::endl;
}
//! \brief Output the function using a BRDF Explorer formating.
virtual void save_brdfexplorer(const std::string& filename,
const arguments& args) const;
private: // data
//! \brief The phong lobe data
vec _kd, _ks, _N;
} ;
......@@ -14,7 +14,7 @@ SUBDIRS = \
nonlinear_fitter_nlopt \
nonlinear_fresnel_schlick \
nonlinear_function_diffuse \
nonlinear_function_phong \
nonlinear_function_blinn \
nonlinear_function_retroblinn \
nonlinear_function_lafortune \
nonlinear_function_isotropic_lafortune \
......
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