BernsteinPoly.h

00001 //===========================================================================
00002 // GoTools - SINTEF Geometry Tools
00003 //
00004 // GoTools module: Implicitization, version 1.0
00005 //
00006 // Copyright (C) 2000-2007 SINTEF ICT, Applied Mathematics, Norway.
00007 //
00008 // This program is free software; you can redistribute it and/or          
00009 // modify it under the terms of the GNU General Public License            
00010 // as published by the Free Software Foundation version 2 of the License. 
00011 //
00012 // This program is distributed in the hope that it will be useful,        
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of         
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
00015 // GNU General Public License for more details.                           
00016 //
00017 // You should have received a copy of the GNU General Public License      
00018 // along with this program; if not, write to the Free Software            
00019 // Foundation, Inc.,                                                      
00020 // 59 Temple Place - Suite 330,                                           
00021 // Boston, MA  02111-1307, USA.                                           
00022 //
00023 // Contact information: E-mail: tor.dokken@sintef.no                      
00024 // SINTEF ICT, Department of Applied Mathematics,                         
00025 // P.O. Box 124 Blindern,                                                 
00026 // 0314 Oslo, Norway.                                                     
00027 //
00028 // Other licenses are also available for this software, notably licenses
00029 // for:
00030 // - Building commercial software.                                        
00031 // - Building software whose source code you wish to keep private.        
00032 //===========================================================================
00033 #ifndef _BERNSTEINPOLY_H
00034 #define _BERNSTEINPOLY_H
00035 
00036 #include <vector>
00037 #include <iostream>
00038 
00039 namespace Go {
00042 
00043 
00044 
00054 class BernsteinPoly {
00055 public:
00057     BernsteinPoly() { }
00061     explicit BernsteinPoly(const std::vector<double>& coefs)
00062         : coefs_(coefs) { }
00067     template <typename ForwardIterator>
00068     BernsteinPoly(ForwardIterator begin, ForwardIterator end)
00069         : coefs_(begin, end) { }
00072     explicit BernsteinPoly(double coef)
00073         : coefs_(std::vector<double>(1, coef)) { }
00074 
00077     int degree() const
00078     { return coefs_.size() - 1; }
00079 
00081     const double& operator[] (int num) const
00082     { return coefs_[num]; }
00084     double& operator[] (int num)
00085     { return coefs_[num]; }
00087     std::vector<double>::iterator coefsBegin()
00088     { return coefs_.begin(); }
00090     std::vector<double>::const_iterator coefsBegin() const
00091     { return coefs_.begin(); }
00093     std::vector<double>::iterator coefsEnd()
00094     { return coefs_.end(); }
00096     std::vector<double>::const_iterator coefsEnd() const
00097     { return coefs_.end(); }
00098 
00103     double operator() (double t) const;
00104 
00110     bool isZero(const double eps = 0.0) const;
00120     bool isStrictlyPositive(const double eps = 0.0) const;
00121 
00128     double norm() const;
00132     void normalize();
00133 
00138     BernsteinPoly deriv(int der) const;
00139 
00142     double integral(double a = 0.0, double b = 1.0) const;
00143 
00152     double blossom(const std::vector<double>& tvec) const;
00160     BernsteinPoly pickInterval(double a, double b) const;
00161 
00165     void degreeElevate(int d);
00166 
00168     BernsteinPoly& operator*= (const BernsteinPoly& poly);
00170     BernsteinPoly& operator*= (double c);
00171 
00173     BernsteinPoly& operator+= (const BernsteinPoly& poly);
00175     BernsteinPoly& operator+= (double c);
00176 
00178     BernsteinPoly& operator-= (const BernsteinPoly& poly);
00180     BernsteinPoly& operator-= (double c);
00181 
00183     BernsteinPoly& operator/= (double c);
00184 
00186     void read(std::istream& is);
00188     void write(std::ostream& os) const;
00189 
00190 private:
00191     std::vector<double> coefs_;
00192 };
00193 
00194 
00196 inline BernsteinPoly operator* (const BernsteinPoly& p1,
00197                                 const BernsteinPoly& p2)
00198 {
00199     BernsteinPoly tmp = p1;
00200     tmp *= p2;
00201     return tmp;
00202 }
00203 
00204 
00206 inline BernsteinPoly operator* (const BernsteinPoly& p,
00207                                 double c)
00208 {
00209     BernsteinPoly tmp = p;
00210     tmp *= c;
00211     return tmp;
00212 }
00213 
00214 
00216 inline BernsteinPoly operator* (double c,
00217                                 const BernsteinPoly& p)
00218 {
00219     BernsteinPoly tmp = p;
00220     tmp *= c;
00221     return tmp;
00222 }
00223 
00224 
00226 inline BernsteinPoly operator+ (const BernsteinPoly& p1,
00227                                 const BernsteinPoly& p2)
00228 {
00229     BernsteinPoly tmp = p1;
00230     tmp += p2;
00231     return tmp;
00232 }
00233 
00234 
00236 inline BernsteinPoly operator+ (double c,
00237                                 const BernsteinPoly& p)
00238 {
00239     BernsteinPoly tmp = p;
00240     tmp += c;
00241     return tmp;
00242 }
00243 
00244 
00246 inline BernsteinPoly operator+ (const BernsteinPoly& p,
00247                                 double c)
00248 {
00249     BernsteinPoly tmp = p;
00250     tmp += c;
00251     return tmp;
00252 }
00253 
00254 
00256 inline BernsteinPoly operator- (const BernsteinPoly& p1,
00257                                 const BernsteinPoly& p2)
00258 {
00259     BernsteinPoly tmp = p1;
00260     tmp -= p2;
00261     return tmp;
00262 }
00263 
00264 
00266 inline BernsteinPoly operator- (double c,
00267                                 const BernsteinPoly& p)
00268 {
00269     BernsteinPoly tmp(c);
00270     tmp -= p;
00271     return tmp;
00272 }
00273 
00274 
00276 inline BernsteinPoly operator- (const BernsteinPoly& p,
00277                                 double c)
00278 {
00279     BernsteinPoly tmp = p;
00280     tmp -= c;
00281     return tmp;
00282 }
00283 
00284 
00286 inline BernsteinPoly operator/ (const BernsteinPoly& p,
00287                                 double c)
00288 {
00289     BernsteinPoly tmp = p;
00290     tmp /= c;
00291     return tmp;
00292 }
00293 
00294 
00296 } // namespace Go
00297 
00298 
00300 namespace std {
00303 
00304 
00305 
00307 inline std::istream& operator >> (std::istream& is,
00308                                   Go::BernsteinPoly& p)
00309 {
00310     p.read(is);
00311     return is;
00312 }
00313 
00314 
00316 inline std::ostream& operator << (std::ostream& os,
00317                                   const Go::BernsteinPoly& p)
00318 {
00319     p.write(os);
00320     return os;
00321 }
00322 
00323 
00325 } // namespace std
00326 
00327 
00328 #endif // _BERNSTEINPOLY_H
00329 
00330 

Generated on Mon Jun 11 15:13:16 2007 for GoTools Implicitization Library by  doxygen 1.5.1