#ifndef __LSSYSTEM_H__ #define __LSSYSTEM_H__ #include #include /*This library provides a LS fitting function including support to weights. It rellies in callback functions defined bellow, it is up to the library which defines the desired model to initialize ALL the attributes of the ls_model_t structure. Please reffer to lighting_models.h and lighting_models.c for a example */ /*Types of each function*/ typedef double phi_function(int i, double* x,void* l_data); /*Given the coeficient number{i}, the argument vector {x} and given lighting model {l_data} computes the value of Phi{i}*/ typedef int number_components_function(void* l_data); /*Give the number of the coeficients for the LS system*/ typedef void retrieve_function(double* C, int n,void* l_data); /*Given the {n} computed LS coeficients {C}, returns the values of the lighting model ${l_data}*/ typedef void update_weight_function(void* l_data,double** X, double* F,double* weights, int n,double* sigma); /*updates the weights {weights} and value of ${sigma} given the lighting model {l_data} values and {X} and F(X)*/ typedef double evaluate_function(double* x,void* l_data); /*Given a normal and the lighting model, returns its light intensity*/ typedef void write_function(FILE* arq,void* l_data); /*writes {l_data} parameters into {arq} the parameters in humam-readable fashion*/ typedef void* read_function(FILE* arq); /*reads {l_data} parameters from {arq} the parameters in humam-readable fashion written by write_function*/ typedef int validate_function(double* c, int basis_size, bool_t* validity_array) ; /*Given a coeficient array and its size, fills in a validity array wich tells wich coeficients are valid*/ typedef void* copy_data_function(void* l_data); /*Given a lighting_data structure creates a copy of it with same parameters*/ typedef double compare_function(void* l_data1,void* l_data2); /*Given two lighting data structures , compute its difference*/ typedef void release_data_function(void* l_data); /*Releases a given lighting data structure*/ typedef double compute_pos_weights_function(double*x, void* l_data); /*Compute the weight for the LS system given a determined x value of the domain of F*/ typedef double retrieve_alpha_value_function(int r, void* l_data); /*Retrieve the coeficient of basis function r of the lighting model if applicable, returns 1 otherwise*/ typedef double* pack_parameter_function(void* l_data); /*This function type converts from a LS set of parameters to an array of parameters for Non Linear solution*/ typedef void* unpack_parameter_function(double* parameters, void* l_data); /*This function type converts from set parameters of Non Linear solution to an array of parameters for LS. Notice that alpha coeficients from LS are not granted to be the best solution, they are copied from the reference ${l_data} as well any data not packed on it*/ typedef int num_packed_params_function(void* l_data); /*This function returns the number of Non linear params. It has to be the size of the array returned by {unpack_parameter_function} */ /*LS generic model fitting data structure, contain all needed callback functions*/ struct Approx_Model_t{ /*Mandatory members - without then the fitting procedures will crash ! */ int type; /*This is meant to be used for debugging and enum types*/ // void* ls_data; /*this contain the data structure which defines the modeled function F, stores initially the initial guess and in the end the fitted function*/ phi_function* phi; /*Linear basis function*/ number_components_function* get_num_components; /*retrieve the number of linear basis functions*/ set_alphas_function* set_alphas; /*Sets the linear coeficientes from the model from a vector*/ get_alphas_function* get_alphas; /*Gets the linear coeficients from the model and stores into a vector*/ validate_function* validate_results; /*Discards the undesirable members of A*/ copy_data_function* copy_data; /*Make a copy of ls_data, it is needed to "save" the results of an previous iteration*/ release_data_function* release_data; /*Release a ls_data pointer from memory, mandatory if you dont wish to have your memory taken by multiple iterations !*/ /*Optional members - they wont be called by the fitting functions and can be NULL*/ evaluate_function* evaluate; /* compute the value of the fitting function, it is optional*/ write_function* write_params; /*write the params in human readable manner into a file*/ read_function* read_params; /*read the params from a file in human readable manner*/ /*for non linear models*/ update_weight_function* update_weights; /*recompute the temporary weights based on the current data record the A matrix*/ compare_function* compare; /*compare the non linear parameters of two data records*/ num_packed_params_function* get_num_nl_params; /*Number of non-linear parameters*/ pack_parameter_function* pack_nl_params; /*Stores non-linear parameters from the data structure into a vector */ unpack_parameter_function* unpack_nl_params; /* Sets non linear parameters from a vector into a data structure */ pack_parameter_function* pack_nl_param_errors; /*Stores the uncertainties of non linear parameters from the data structure into a vector*/ unpack_parameter_function* unpack_nl_param_errors; /*Stores the uncertainties of non linear parameters from the vector into the data structure*/ }; typedef struct Approx_Model_t approx_model_t; void computeLeastSquaresMatrix(double* A,phi_function* phi, int basis_size, double** X,int n, double w[], double wpos[],void* l_data); /*Computes the Least Square matrix A using the weights,n-dimensional X values and current values of the function.*/ void computeLeastSquaresRHSVector(double* b,phi_function* phi, int basis_size,double** X,double* F,int n , double w[], double wpos[],void* l_data); /*Same above, but for RHS vector*/ void computeLSTerms(double* A, double* b , double* c, phi_function* phi, int basis_size, validate_function* validateResults, double** X, double* F, int n, double* w, double* wpos, void* l_data ); /*Compute the LS terms for the given matrix {A}, RHS vector {b} and stores it in {c}, weights and current l*/ void fit_linear_model(double** X, double* F,double* W, int n, ls_model_t* lm, void* l_data,int update_steps, double outliers_fraction,double* Wo); /* Adjusts the linear coeficients of {lm} by Least Squares method, using the current values of the non linear parameters. If {update_steps} and {outliers_fraction} are positive, iterates the adjustment at most {update_steps}, recomputing the a posteriori weights {Wo} according to the outlier model given fraction {outliers_fraction} of outliers. */ void fit_non_linear_model_simplex(double** X, double* F,double* W, int n,ls_model_t* lm,void* l_data, int update_steps,double outliers_fraction, int num_iterations,double* Wo); /* Adjusts the linear and non-linear parameters of {lm} by iterated quadratic minimization. The non linear parameters are adjusted by fitting a quadratic function to the mean quadratic error and obtaining its minimum, repeated at maximum {num_iterations} iterations. At each iteration adjusts the linear coeficients of {lm} by Least Squares method. If {update_steps} and {outliers_fraction} are positive, iterates the adjustment at most {update_steps}, recomputing the a posteriori weights {Wo} according to the outlier model given fraction {outliers_fraction} of outliers */ #endif