Commit 2781a6e5 authored by Laurent Belcour's avatar Laurent Belcour

Adding an interpolant data plugin to fill gaps.

parent 8fdf12dc
......@@ -64,20 +64,7 @@ class data : public parametrized
// Get data size, e.g. the number of samples to fit
virtual int size() const = 0 ;
// Get min and max input space values
virtual vec min() const = 0 ;
virtual vec max() const = 0 ;
// Get the size of the input X domain and output Y domain
virtual int dimX() const { return _nX ; }
virtual int dimY() const { return _nY ; }
protected: // data
// Dimensions of the data
int _nX, _nY ;
} ;
/*! \brief Change the parametrization of data to fit the parametrization of the
......
......@@ -37,32 +37,6 @@ class function : public parametrized
//! example. The default behaviour is to load a function file.
virtual void bootstrap(const data*, const arguments& args);
//! Provide the dimension of the input space of the function
virtual int dimX() const { return _nX ; }
//! Provide the dimension of the output space of the function
virtual int dimY() const { return _nY ; }
//! Set the dimension of the input space of the function
virtual void setDimX(int nX) { _nX = nX ; }
//! Set the dimension of the output space of the function
virtual void setDimY(int nY) { _nY = nY ; }
/* DEFINITION DOMAIN OF THE FUNCTION */
//! \brief Set the minimum value the input can take
virtual void setMin(const vec& min) ;
//! \brief Set the maximum value the input can take
virtual void setMax(const vec& max) ;
//! \brief Get the minimum value the input can take
virtual vec getMin() const ;
//! \brief Get the maximum value the input can take
virtual vec getMax() const ;
/* IMPORT/EXPORT FUNCTIONS */
//! Load function specific files
......@@ -109,10 +83,6 @@ class function : public parametrized
protected: // data
// Dimension of the function & domain of definition.
int _nX, _nY ;
vec _min, _max ;
};
/*! \brief Non-linear function interface
......
......@@ -335,8 +335,41 @@ class parametrized
}
}
/* DIMENSION OF THE INPUT AND OUTPUT DOMAIN */
//! Provide the dimension of the input space of the function
virtual int dimX() const { return _nX ; }
//! Provide the dimension of the output space of the function
virtual int dimY() const { return _nY ; }
//! Set the dimension of the input space of the function
virtual void setDimX(int nX) { _nX = nX ; }
//! Set the dimension of the output space of the function
virtual void setDimY(int nY) { _nY = nY ; }
/* DEFINITION DOMAIN OF THE FUNCTION */
//! \brief Set the minimum value the input can take
virtual void setMin(const vec& min) ;
//! \brief Set the maximum value the input can take
virtual void setMax(const vec& max) ;
//! \brief Get the minimum value the input can take
virtual vec getMin() const ;
//! \brief Get the maximum value the input can take
virtual vec getMax() const ;
protected:
// Input and output parametrization
params::input _in_param ;
params::output _out_param ;
// Dimension of the function & domain of definition.
int _nX, _nY ;
vec _min, _max ;
};
#include "data.h"
#include <cstdio>
#include <cstdlib>
#include <cmath>
data_interpolant::data_interpolant()
{
_kdtree = new flann::Index< flann::L2<double> >(flann::KDTreeIndexParams(4));
_knn = 10;
}
data_interpolant::~data_interpolant()
{
delete _data;
delete _kdtree;
}
// Load data from a file
void data_interpolant::load(const std::string& filename)
{
// Load the data
_data->load(filename);
// Copy the informations
setDimX(_data->dimX());
setDimY(_data->dimY());
setMin(_data->getMin());
setMax(_data->getMax());
// Update the KDtreee by inserting all points
for(int i=0; i<_data->size(); ++i)
{
vec x = _data->get(i);
flann::Matrix<double> pts(&x[0], dimX(), 1);
_kdtree->addPoints(pts);
}
_kdtree->buildIndex();
}
void data_interpolant::load(const std::string& filename, const arguments&)
{
load(filename);
}
void data_interpolant::save(const std::string& filename) const
{
}
// Acces to data
vec data_interpolant::get(int id) const
{
vec res(dimX() + dimY()) ;
return res ;
}
vec data_interpolant::operator[](int i) const
{
return get(i) ;
}
//! \todo Test this function
void data_interpolant::set(vec x)
{
assert(x.size() == dimX());
}
vec data_interpolant::value(vec, vec) const
{
vec res(dimY());
std::cerr << "<<ERROR>> Deprecated function: " << __func__ << std::endl;
return res;
}
vec data_interpolant::value(vec x) const
{
vec res = vec::Zero(dimY());
// Query point
flann::Matrix<double> pts(&x[0], dimX(), 1);
std::vector< std::vector<int> > indices;
std::vector< std::vector<double> > dists;
_kdtree->knnSearch(pts, indices, dists, _knn, flann::SearchParams());
// Interpolate the value using the indices
double cum_dist = 0.0;
for(int i=0; i<indices[0].size(); ++i)
{
int indice = indices[0][i];
vec y = _data->get(indice);
for(int j=0; j<dimY(); ++j)
{
res[j] += dists[0][i] * y[dimX() + j];
}
cum_dist += dists[0][i];
}
for(int j=0; j<dimY(); ++j)
{
res[j] /= cum_dist;
}
return res;
}
// Get data size, e.g. the number of samples to fit
int data_interpolant::size() const
{
return _data->size();
}
ALTA_DLL_EXPORT data* provide_data()
{
return new data_interpolant();
}
#pragma once
#include <core/data.h>
#include <core/common.h>
#include <core/args.h>
#include <flann/flann.hpp>
/*! \brief Load a data file, but provide access to an interpolated version of
* the data points.
*
* <h2>Requirements:</h2>
* The FLANN library.
* On linux plateforms it can be obtained using the package manager:
* \verbatim
* sudo apt-get install libflann-dev
* \endverbatim
*/
class data_interpolant : public data
{
public: // methods
data_interpolant();
~data_interpolant();
// Load data from a file
virtual void load(const std::string& filename) ;
virtual void load(const std::string& filename, const arguments& args) ;
virtual void save(const std::string& filename) const ;
// Acces to data
virtual vec get(int i) const ;
virtual vec operator[](int i) const ;
virtual vec value(vec in, vec out) const;
virtual vec value(vec x) const;
// Set data
virtual void set(vec x);
// Get data size, e.g. the number of samples to fit
virtual int size() const ;
private: // data
// The data object used to load sparse points sets
data* _data;
// Interpolation
flann::Index< flann::L2<double> >* _kdtree;
flann::Matrix<double>* _points;
int _knn;
} ;
TEMPLATE = lib
CONFIG *= plugin \
eigen
DESTDIR = ../../build
INCLUDEPATH += ../..
HEADERS = data.h
SOURCES = data.cpp
LIBS += -L../../build \
-lcore
......@@ -176,8 +176,8 @@ bool nonlinear_fitter_ceres::fit_data(const data* d, function* fit, const argume
// to the dimension of my fitting problem
fit->setDimX(d->dimX()) ;
fit->setDimY(d->dimY()) ;
fit->setMin(d->min()) ;
fit->setMax(d->max()) ;
fit->setMin(d->getMin()) ;
fit->setMax(d->getMax()) ;
// Convert the function and bootstrap it with the data
if(dynamic_cast<nonlinear_function*>(fit) == NULL)
......
......@@ -273,8 +273,8 @@ bool nonlinear_fitter_eigen::fit_data(const data* d, function* fit, const argume
// to the dimension of my fitting problem
fit->setDimX(d->dimX()) ;
fit->setDimY(d->dimY()) ;
fit->setMin(d->min()) ;
fit->setMax(d->max()) ;
fit->setMin(d->getMin()) ;
fit->setMax(d->getMax()) ;
// Convert the function and bootstrap it with the data
if(dynamic_cast<nonlinear_function*>(fit) == NULL)
......
......@@ -218,8 +218,8 @@ bool nonlinear_fitter_ipopt::fit_data(const data* d, function* fit, const argume
// to the dimension of my fitting problem
fit->setDimX(d->dimX()) ;
fit->setDimY(d->dimY()) ;
fit->setMin(d->min()) ;
fit->setMax(d->max()) ;
fit->setMin(d->getMin()) ;
fit->setMax(d->getMax()) ;
// Convert the function and bootstrap it with the data
if(dynamic_cast<nonlinear_function*>(fit) == NULL)
......
......@@ -122,8 +122,8 @@ bool nonlinear_fitter_nlopt::fit_data(const data* d, function* fit, const argume
// to the dimension of my fitting problem
fit->setDimX(d->dimX()) ;
fit->setDimY(d->dimY()) ;
fit->setMin(d->min()) ;
fit->setMax(d->max()) ;
fit->setMin(d->getMin()) ;
fit->setMax(d->getMax()) ;
// Convert the function and bootstrap it with the data
if(dynamic_cast<nonlinear_function*>(fit) == NULL)
......
......@@ -28,5 +28,6 @@ SUBDIRS = \
nonlinear_function_isotropic_lafortune \
data_merl \
data_brdf_slice \
data_interpolant \
# data_astm
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