GLBernsteinPolynomial.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Jacques Gasselin                                *
00003  *   jacquesgasselin@hotmail.com                                           *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Library General Public License as       *
00007  *   published by the Free Software Foundation; either version 2 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU Library General Public     *
00016  *   License along with this program; if not, write to the                 *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "GLBernsteinPolynomial.h"
00021 
00022 namespace mathglpp
00023 {
00024 
00025 std::vector<std::vector<GLuint> > GLBernsteinPolynomial::pascalsTriangle;
00026 
00027 GLBernsteinPolynomial::GLBernsteinPolynomial(GLuint ord)
00028 {
00029    if(ord >= pascalsTriangle.size() )
00030       extendPascal(ord);
00031 }
00032 
00033 GLBernsteinPolynomial::~GLBernsteinPolynomial()
00034 {
00035 }
00036 
00037 void GLBernsteinPolynomial::setOrder(GLuint ord)
00038 {
00039    if(ord >= pascalsTriangle.size())
00040        extendPascal(ord);
00041 }
00042 
00043 void GLBernsteinPolynomial::extendPascal(GLuint level)
00044 {
00045    if(pascalsTriangle.size() < 2)
00046    {
00047       std::vector<GLuint> row;
00048       row.push_back(1);
00049 
00050       pascalsTriangle.push_back(row);
00051       row.push_back(1);
00052       pascalsTriangle.push_back(row);
00053    }
00054 
00055    if(level >= pascalsTriangle.size() )
00056    {
00057       std::vector<GLuint> row;
00058       row.push_back(1);
00059 
00060       for(register GLuint i = 1; i < pascalsTriangle.size(); ++i)
00061          row.push_back( pascalsTriangle[pascalsTriangle.size()-1][i-1] +
00062                         pascalsTriangle[pascalsTriangle.size()-1][i] );
00063 
00064       row.push_back(1);
00065       pascalsTriangle.push_back(row);
00066 
00067       extendPascal(level);
00068    }
00069 }
00070 
00071 const GLfloat GLBernsteinPolynomial::get(const GLuint order, const GLuint choice, const GLfloat u)
00072 {
00073    assert(choice <= order);
00074 
00075    if(choice == 0)
00076    {
00077       if( u <= 0.0000001 )
00078          return 1;
00079       else
00080       if( u >= 0.9999999 )
00081          return 0;
00082       else
00083                   return pow(GLfloat(1-u), GLint(order));
00084    }
00085    else
00086    if(choice == order)
00087    {
00088       if( u <= 0.0000001 )
00089          return 0;
00090       else
00091       if( u >= 0.9999999 )
00092          return 1;
00093       else
00094          return pow((GLfloat)u, (GLint)choice);
00095    }
00096    else
00097    if( u <= 0.0000001 )
00098       return 0;
00099    else
00100    if( u >= 0.9999999 )
00101       return 0;
00102    else
00103       return pascalsTriangle[order][choice]
00104            * pow((GLfloat)u, (GLint)choice)
00105        * pow((GLfloat)(1-u), (GLint)(order - choice));
00106 }
00107 
00108 const void GLBernsteinPolynomial::getVector(const GLuint order, const GLfloat u, GLfloat* vec)
00109 {
00110    assert(vec);
00111    for(register GLuint i = 0; i < order; ++i)
00112        vec[i] = get(order,i,u);
00113 }
00114 
00115 };

Generated on Wed Oct 3 12:50:48 2007 for MathGL++ by  doxygen 1.5.2