fitting-model.h revision d37634d73670180f99a3e0ea583621373d90ec4f
/*
* Fitting Models for Geom Types
*
* Authors:
* Marco Cecchetti <mrcekets at gmail.com>
*
* Copyright 2008 authors
*
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*/
#ifndef _NL_FITTING_MODEL_H_
#define _NL_FITTING_MODEL_H_
/*
* A model is an abstraction for an expression dependent from a parameter where
* the coefficients of this expression are the unknowns of the fitting problem.
* For a ceratain number of parameter values we know the related values
* the expression evaluates to: from each parameter value we get a row of
* the matrix of the fitting problem, from each expression value we get
* the related constant term.
* Example: given the model a*x^2 + b*x + c = 0; from x = 1 we get
* the equation a + b + c = 0, in this example the constant term is always
* the same for each parameter value.
*
* A model is required to implement 3 methods:
*
* - size : returns the number of unknown coefficients that appear in
* the expression of the fitting problem;
* - feed : its input is a parameter value and the related expression value,
* it generates a matrix row and a new entry of the constant vector
* of the fitting problem;
* - instance : it has an input parameter represented by the raw vector
* solution of the fitting problem and an output parameter
* of type InstanceType that return a specific object that is
* generated using the fitting problem solution, in the example
* above the object could be a Poly type.
*/
/*
* completely unknown models must inherit from this template class;
* example: the model a*x^2 + b*x + c = 0 to be solved wrt a, b, c;
* example: the model A(t) = known_sample_value_at(t) to be solved wrt
* the coefficients of the curve A(t) expressed in S-Basis form;
* parameter type: the type of x and t variable in the examples above;
* value type: the type of the known sample values (in the first example
* is constant )
* instance type: the type of the objects produced by using
* the fitting raw data solution
*/
{
typedef ParameterType parameter_type;
typedef ValueType value_type;
typedef InstanceType instance_type;
static const bool WITH_FIXED_TERMS = false;
/*
* a LinearFittingModel must implement the following methods:
*
* void feed( VectorView & vector,
* parameter_type const& sample_parameter ) const;
*
* size_t size() const;
*
* void instance(instance_type &, raw_type const& raw_data) const;
*
*/
};
/*
* partially known models must inherit from this template class
* example: the model a*x^2 + 2*x + c = 0 to be solved wrt a and c
*/
{
typedef ParameterType parameter_type;
typedef ValueType value_type;
typedef InstanceType instance_type;
static const bool WITH_FIXED_TERMS = true;
/*
* a LinearFittingModelWithFixedTerms must implement the following methods:
*
* void feed( VectorView & vector,
* value_type & fixed_term,
* parameter_type const& sample_parameter ) const;
*
* size_t size() const;
*
* void instance(instance_type &, raw_type const& raw_data) const;
*
*/
};
// incomplete model, it can be inherited to make up different kinds of
// instance type; the raw data is a vector of coefficients of a polynomial
// rapresented in standard power basis
{
{
}
{
coeff[0] = 1;
double x_i = 1;
{
x_i *= sample_parameter;
}
}
{
return m_size;
}
};
// this model generates Geom::Poly objects
{
{
}
{
{
}
}
};
// incomplete model, it can be inherited to make up different kinds of
// instance type; the raw data is a vector of coefficients of a polynomial
// rapresented in standard power basis with leading term coefficient equal to 1
{
{
}
double & known_term,
double sample_parameter ) const
{
}
{
}
};
// incomplete model, it can be inherited to make up different kinds of
// instance type; the raw data is a vector of coefficients of the equation
// of an ellipse curve
//template< typename InstanceType >
//class LFMEllipseEquation
// : public LinearFittingModelWithFixedTerms<Point, double, InstanceType>
//{
// public:
// void feed( VectorView & coeff, double & fixed_term, Point const& p ) const
// {
// coeff[0] = p[X] * p[Y];
// coeff[1] = p[Y] * p[Y];
// coeff[2] = p[X];
// coeff[3] = p[Y];
// coeff[4] = 1;
// fixed_term = p[X] * p[X];
// }
//
// size_t size() const
// {
// return 5;
// }
//};
// incomplete model, it can be inherited to make up different kinds of
// instance type; the raw data is a vector of coefficients of the equation
// of a conic section
{
{
coeff[0] = p[X] * p[Y];
coeff[1] = p[Y] * p[Y];
coeff[2] = p[X];
coeff[3] = p[Y];
fixed_term = p[X] * p[X];
}
{
return 5;
}
};
// this model generates Ellipse curves
{
{
}
};
// this model generates Ellipse curves
{
{
}
};
// incomplete model, it can be inherited to make up different kinds of
// instance type; the raw data is a vector of coefficients of the equation
// of a circle curve
{
{
coeff[0] = p[X];
coeff[1] = p[Y];
fixed_term = p[X] * p[X] + p[Y] * p[Y];
}
{
return 3;
}
};
// this model generates Ellipse curves
{
{
}
};
// this model generates SBasis objects
{
{
}
{
double u0 = 1-t;
double u1 = t;
{
u0 *= s;
u1 *= s;
}
}
{
return m_size;
}
{
{
}
}
};
// this model generates D2<SBasis> objects
{
{
}
{
}
{
}
{
}
};
// this model generates Bezier objects
{
{
}
{
double s = 1;
{
s *= t;
}
double u = 1-t;
s = 1;
{
coeff[i] *= s;
s *= u;
}
coeff[0] *= s;
}
{
return m_size;
}
{
{
b[i] = raw_data[i];
}
}
};
// this model generates Bezier curves
{
{
}
{
}
{
}
{
}
};
} // end namespace NL
} // end namespace Geom
#endif // _NL_FITTING_MODEL_H_
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :