bernstein.h 1.53 KB
 Steve Tonneau committed Mar 13, 2017 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ``````/** * \file bezier_curve.h * \brief class allowing to create a Bezier curve of dimension 1 <= n <= 3. * \author Steve T. * \version 0.1 * \date 06/17/2013 */ #ifndef _CLASS_BERNSTEIN #define _CLASS_BERNSTEIN #include "curve_abc.h" #include "MathDefs.h" #include #include #include namespace spline { /// /// \brief Computes factorial of a number /// `````` t steve committed Nov 24, 2017 26 ``````inline unsigned int fact(const unsigned int n) `````` Steve Tonneau committed Mar 13, 2017 27 ``````{ `````` stevet committed Mar 19, 2018 28 29 `````` unsigned int res = 1; for (unsigned int i=2 ; i <= n ; ++i) `````` Steve Tonneau committed Mar 13, 2017 30 31 32 33 34 35 36 `````` res *= i; return res; } /// /// \brief Computes a binomal coefficient /// `````` t steve committed Nov 24, 2017 37 ``````inline unsigned int bin(const unsigned int n, const unsigned int k) `````` Steve Tonneau committed Mar 13, 2017 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 ``````{ return fact(n) / (fact(k) * fact(n - k)); } /// \class Bernstein /// \brief Computes a Bernstein polynome /// template struct Bern{ Bern(const unsigned int m, const unsigned int i) :m_minus_i(m - i) ,i_(i) ,bin_m_i_(bin(m,i)) {} ~Bern(){} Numeric operator()(const Numeric u) const { assert(u >= 0. && u <= 1.); return bin_m_i_*(pow(u, i_)) *pow((1-u),m_minus_i); } Numeric m_minus_i; Numeric i_; Numeric bin_m_i_; }; /// /// \brief Computes all Bernstein polynomes for a certain degree /// template std::vector > makeBernstein(const unsigned int n) { std::vector > res; for(unsigned int i = 0; i<= n; ++i) res.push_back(Bern(n, i)); return res; } } // namespace spline #endif //_CLASS_BERNSTEIN ``````