data.h 4.91 KB
Newer Older
1 2 3
#pragma once

#include <string>
4
#include <utility>
5 6 7
#include <iostream>
#include <limits>
#include <fstream>
8

9 10
#include <QtPlugin>

11
#include "common.h"
Laurent Belcour's avatar
Laurent Belcour committed
12
#include "args.h"
13
#include "params.h"
14
#include "clustering.h"
15 16

/*! \brief A data object. Allows to load data from files.
17 18
 *  \ingroup core
 */
19
class data
20
{
21
	public: // methods
22 23 24

		// Load data from a file
		virtual void load(const std::string& filename) = 0 ;
Laurent Belcour's avatar
Laurent Belcour committed
25
		virtual void load(const std::string& filename, const arguments& args) = 0 ;
26

27
        // Save the data to a file
28
        virtual void save(const std::string& filename) const
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
        {
            std::ofstream file(filename.c_str(), std::ios_base::trunc);
            file << "#DIM " << _nX << " " << _nY << std::endl;
            for(int i=0; i<size(); ++i)
            {
                vec x = this->get(i);
                for(int j=0; j<_nX+_nY; ++j)
                {
                    file << x[j] << "\t";
                }
                file << std::endl;
            }
            file.close();
        }

44
		// Acces to data
45 46
		virtual vec get(int i) const = 0 ;
		virtual vec operator[](int i) const = 0 ;
47 48 49 50 51 52 53 54 55

		//! \brief Provide an evaluation in a BRDF way of the data. 
		//!
		//! \details
		//! The input vector in (can have texture coordinate) and the output
		//! vector out are taken to grad a value and return it. The two vectors 
		//! should be compliant with the size and parametrization of the data.
		virtual vec value(vec in, vec out) const = 0;

56 57 58
		//! \brief Put the sample inside the data
		virtual void set(vec x) = 0;

59

60
		// Get data size, e.g. the number of samples to fit
61 62
		virtual int size() const = 0 ;

63

64
		// Get min and max input space values
65 66 67
		virtual vec min() const = 0 ;
		virtual vec max() const = 0 ;

68 69

		// Get the size of the input X domain and output Y domain
70 71
		virtual int dimX() const { return _nX ; }
		virtual int dimY() const { return _nY ; }
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
		//! \brief provide the parametrization of the function.
		//! \note some function type can modify the parametrization to adapt
		//! to the data.
		virtual params::input parametrization() const
		{
			return _in_param;
		}

		//! \brief can set the input parametrization of a non-parametrized
		//! function. Throw an exception if it tries to erase a previously
		//! defined one.
		virtual void setParametrization(params::input new_param)
		{
			if(_in_param != params::UNKNOWN_INPUT)
				throw("A parametrization is already defined");

			_in_param = new_param;
		}

	protected: // data
93

94
		// Dimensions of the data
95
		int _nX, _nY ;
96 97 98 99

		// Input and output parametrization
		params::input  _in_param ;
		params::output _out_param ;
100
} ;
101
	 
102
Q_DECLARE_INTERFACE(data, "Fitter.Data")
103 104 105 106 107 108

/*! \brief Change the parametrization of data to fit the parametrization of the
 *  function to be fitted.
 *
 *  \ingroup core
 *  \internal
109
 *  \todo Finish this class
110 111 112
 */
class data_params : public data
{
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	public: // structures

		//! \brief when changing from a parametrization to another, you might
		//! lose some dimensions. This list enumerate the different operators
		//! that can be applied on the raw data to be clusterized.
		//! \note by default we use <em>none</em>, but if the input space
		//! dimension is reduced, the program will halt.
		enum clustrering
		{
			MEAN,
			MEDIAN,
			NONE
		};

	public: // methods

		//! \brief contructor requires the definition of a base class that
		//! has a parametrization, and a new parametrization.
Laurent Belcour's avatar
Laurent Belcour committed
131
		data_params(const data* d, params::input new_param,
132
				data_params::clustrering method = data_params::NONE) :
Laurent Belcour's avatar
Laurent Belcour committed
133
			_clustering_method(method)
134
		{
Laurent Belcour's avatar
Laurent Belcour committed
135
			_nX = params::dimension(new_param);
136 137
			_nY = d->dimY();

Laurent Belcour's avatar
Laurent Belcour committed
138 139
			std::cout << "<<INFO>> Reparametrization of the data" << std::endl;
			clustering<data>(d, _nY, d->parametrization(), new_param, _data);
140

Laurent Belcour's avatar
Laurent Belcour committed
141 142
			std::cout << "<<INFO>> clustering left " << _data.size() << "/" << d->size() << " elements" << std::endl;
			save(std::string("cluster.gnuplot"));
143 144
		}

145 146
		virtual vec value(vec in, vec out) const 
		{
147
			NOT_IMPLEMENTED();
148 149
		}

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
		// Load data from a file
		virtual void load(const std::string& filename)
		{
			std::cerr << "<<ERROR>> this data type cannot load data" << std::endl;
			throw;
		}

		virtual void load(const std::string& filename, const arguments& args)
		{
			std::cerr << "<<ERROR>> this data type cannot load data" << std::endl;
			throw;
		}

		// Acces to data
		virtual vec get(int i) const
		{
166
            return _data[i];
167 168 169 170 171
		}
		virtual vec operator[](int i) const
		{
			return this->get(i);
		}
172 173 174 175 176 177
		
		//! \todo This should crash at execution.
		virtual void set(vec x)
		{
			this->set(x);
		}
178 179 180 181

		// Get data size, e.g. the number of samples to fit
		virtual int size() const
		{
182
            return _data.size();
183 184 185 186 187
		}

		// Get min and max input space values
		virtual vec min() const
		{
188
            return _min;
189 190 191
		}
		virtual vec max() const
		{
192
            return _max;
193 194 195 196 197 198
		}

	protected: // data

		data_params::clustrering _clustering_method;

199 200 201
        std::vector<vec> _data;

        vec _min, _max;
202
};