Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

sxy_series_coef.cpp

Go to the documentation of this file.
00001 
00002 // =======================================================================
00003 
00004 char* series_coef_cpp = "$Id: sxy_series_coef.cpp,v 1.3 2003/01/17 17:43:13 clinio Exp $";
00005 
00006 // =======================================================================
00007 
00008 #include <float.h>
00009 #include "sxy_series_coef.h"
00010 
00011 // =======================================================================
00012 
00013 #define MAX_PARAMETERS 10
00014 
00015 // =======================================================================
00016 
00017 // .......................................................................
00024 SXYCoeficientsAproximationSeries::SXYCoeficientsAproximationSeries(
00025 unsigned int num_variables, unsigned int buffer_size, 
00026 unsigned int resolution) { 
00027    // Inicialização do vetor de séries (parâmetros)
00028    parameters_vector = new SXYVector<SXYSeries*>(MAX_PARAMETERS, NULL);
00029 
00030    // Alocação do buffer interno
00031    max_index = buffer_size;
00032    num_points = resolution;
00033    num_coeficients = num_variables;
00034    buffer_x = new double[max_index];
00035    buffer_y = new double[max_index];
00036    coeficients = new double[num_coeficients];
00037    has_manual_limit = 0;
00038 
00039    assert(buffer_x && buffer_y && coeficients);
00040    resetCoeficients();
00041 }
00042 
00043 // .......................................................................
00046 SXYCoeficientsAproximationSeries::~SXYCoeficientsAproximationSeries(void) {
00047    if (parameters_vector != NULL) delete parameters_vector;
00048    parameters_vector = NULL;
00049 
00050    delete[] buffer_x;
00051    delete[] buffer_y;
00052    delete[] coeficients;
00053 
00054    buffer_x = NULL;
00055    buffer_y = NULL;
00056    coeficients = NULL;
00057 }
00058 
00059 // .......................................................................
00063 unsigned int SXYCoeficientsAproximationSeries::getNumPoints(void) {
00064    return num_points;
00065 }
00066 
00067 // .......................................................................
00068 
00069 unsigned int SXYCoeficientsAproximationSeries::fillBuffer(void) {
00070    // Valores extremos dos pontos das séries de entrada (parâmetros)
00071    // que serão aquisitados no loop de preenchimento abaixo.
00072    double minx = FLT_MAX;
00073    double maxx = -FLT_MAX;
00074 
00075    unsigned int is_log_x = isLogXCallback();
00076    unsigned int is_log_y = isLogYCallback();
00077    // Loop de preenchimento dos buffers de cálculo (pontos)
00078    unsigned int index = 0;
00079    for (unsigned int p = 0; p < getNumParameters(); p++) {
00080       SXYSeries* serie = getParameter(p);
00081       unsigned int np = serie->getNumPoints();
00082       for (unsigned int i = 0; i < np; i++) {
00083          assert( index < getBufferSize() );
00084          double vx, vy;
00085          unsigned int has_point = serie->getPoint(i, vx, vy);
00086          unsigned int is_correct = is_log_x ? vx > 1e-15 : 1; 
00087          is_correct &= is_log_y ? vy > 1e-15 : 1; 
00088          if (has_point && is_correct) {
00089             vx = is_log_x ? log10(vx) : vx;
00090             vy = is_log_y ? log10(vy) : vy;
00091 
00092             buffer_x[index] = vx;
00093             buffer_y[index] = vy;
00094             if (vx < minx) minx = vx;
00095             if (vx > maxx) maxx = vx;
00096             index++;
00097          }
00098       }
00099    }
00100    return index;
00101 }
00102 
00103 // .......................................................................
00111 unsigned int SXYCoeficientsAproximationSeries::getPoint(unsigned int n, 
00112 double& x, double& y) {
00113    // Testes para instrumentação 
00114    if (n < 0) return 0;
00115 
00116    // Cálculo dos coeficiente A e B da equação: y = A.x + B
00117    if (n == 0) {
00118       resetCoeficients();
00119       unsigned int index = fillBuffer();
00120       assert(index < getBufferSize() );
00121       if (!calculateCoeficients(index, buffer_x, buffer_y)) {
00122          resetCoeficients();
00123          return 0;
00124       }
00125    }
00126 
00127    unsigned int is_log_x = isLogXCallback();
00128    unsigned int is_log_y = isLogYCallback();
00129 
00130    // Utilizando os extremos valores calculados no loop para achar
00131    // um limite para a reta
00132    double _lxmin, _lxmax, _lymin, _lymax;
00133    getLimits(_lxmin, _lxmax, _lymin, _lymax);
00134    _lxmin = is_log_x ? log10(_lxmin) : _lxmin;
00135    _lxmax = is_log_x ? log10(_lxmax) : _lxmax;
00136    _lymin = is_log_y ? log10(_lymin) : _lymin;
00137    _lymax = is_log_y ? log10(_lymax) : _lymax;
00138 
00139    double dx = (_lxmax - _lxmin) / (getNumPoints() - 1); 
00140    x = _lxmin + dx*n; 
00141    if ( !aproxFunctionCallback(x, y) ) return 0;
00142    x = is_log_x ? pow(10,x) : x;
00143    y = is_log_y ? pow(10,y) : y;
00144    return 1;
00145 }
00146 
00147 // .......................................................................
00148 
00149 unsigned int SXYCoeficientsAproximationSeries::getNumCoeficients(void) const {
00150    return num_coeficients;
00151 }
00152 
00153 // .......................................................................
00154 
00155 void SXYCoeficientsAproximationSeries::resetCoeficients( void ) {
00156    for (unsigned int c = 0; c < getNumCoeficients(); c++) 
00157       coeficients[c] = SXYSeries::getAnInvalidNumber();
00158 }
00159 
00160 // .......................................................................
00164 void SXYCoeficientsAproximationSeries::insertParameter(SXYSeries* param) {
00165    assert(param != NULL);
00166    assert(parameters_vector != NULL);
00167    parameters_vector->insertElement(param);
00168 }
00169 
00170 // .......................................................................
00173 unsigned int SXYCoeficientsAproximationSeries::getNumParameters(void) const {
00174    if (parameters_vector == NULL) return 0;
00175    return parameters_vector->getLength();
00176 }
00177 
00178 // .......................................................................
00182 SXYSeries* SXYCoeficientsAproximationSeries::getParameter(int i) const {
00183    assert(parameters_vector != NULL);
00184    return parameters_vector->getElement(i);
00185 }
00186 
00187 
00188 // .......................................................................
00189 
00190 void SXYCoeficientsAproximationSeries::setCoeficient(unsigned int c, 
00191 double coef) {
00192    coeficients[c] = coef;
00193    assert(c < num_coeficients);
00194 }
00195 
00196 // .......................................................................
00199 unsigned int SXYCoeficientsAproximationSeries::getCoeficient(unsigned int c, 
00200 double& coef) const {
00201    coef = coeficients[c];
00202    assert(c < num_coeficients);
00203    return SXYSeries::isValidNumber(coef);
00204 }
00205 
00206 // .......................................................................
00210 void SXYCoeficientsAproximationSeries::getLog( unsigned int& logx, 
00211 unsigned int& logy ) { 
00212    logx = isLogXCallback();
00213    logy = isLogYCallback();
00214 }
00215 
00216 // .......................................................................
00218 unsigned int SXYCoeficientsAproximationSeries::getBufferSize(void) const {
00219    return max_index;
00220 }
00221 
00222 // .......................................................................
00224 void SXYCoeficientsAproximationSeries::resetLimits(void) {
00225    has_manual_limit = 0;
00226 }
00227 
00228 // .......................................................................
00234 void SXYCoeficientsAproximationSeries::setLimits( double xmin, double xmax, 
00235 double ymin, double ymax ) {
00236    lxmin = xmin; lxmax = xmax;
00237    lymin = ymin; lymax = ymax;
00238    has_manual_limit = 1;
00239 }
00240 
00241 // .......................................................................
00247 void SXYCoeficientsAproximationSeries::getLimits( double& xmin, double& xmax, 
00248 double& ymin, double& ymax ) {
00249    xmin = lxmin; xmax = lxmax;
00250    ymin = lymin; ymax = lymax;
00251    if (has_manual_limit) return;
00252    SXYSeries::getSeveralLimits(parameters_vector, xmin, xmax, ymin, ymax);
00253 }
00254 
00255 // .......................................................................
00256 
00257 

SXY
Tecgraf / PUC-Rio - Computer Graphics Technology Group