bernstein.h 2.3 KB
 Steve Tonneau committed Mar 13, 2017 1 ``````/** `````` Guilhem Saurel committed Sep 24, 2019 2 3 4 5 6 7 `````` * \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 */ `````` Steve Tonneau committed Mar 13, 2017 8 9 10 11 12 13 14 15 16 17 18 19 `````` #ifndef _CLASS_BERNSTEIN #define _CLASS_BERNSTEIN #include "curve_abc.h" #include "MathDefs.h" #include #include #include `````` Guilhem Saurel committed Sep 24, 2019 20 21 22 23 24 25 26 27 28 29 30 31 ``````namespace curves { /// \brief Computes a binomial coefficient . /// \param n : an unsigned integer. /// \param k : an unsigned integer. /// \return \f\$\binom{n}{k}f\$ /// inline unsigned int bin(const unsigned int n, const unsigned int k) { if (k > n) throw std::runtime_error("binomial coefficient higher than degree"); if (k == 0) return 1; if (k > n / 2) return bin(n, n - k); return n * bin(n - 1, k - 1) / k; } `````` JasonChmn committed Sep 03, 2019 32 `````` `````` Guilhem Saurel committed Sep 24, 2019 33 34 35 36 37 38 39 ``````/// \class Bernstein. /// \brief Computes a Bernstein polynome. /// template struct Bern { Bern() {} Bern(const unsigned int m, const unsigned int i) : m_minus_i(m - i), i_(i), bin_m_i_(bin(m, i)) {} `````` JasonChmn committed Sep 03, 2019 40 `````` `````` Guilhem Saurel committed Sep 24, 2019 41 `````` ~Bern() {} `````` JasonChmn committed Sep 03, 2019 42 `````` `````` Guilhem Saurel committed Sep 24, 2019 43 44 45 46 `````` Numeric operator()(const Numeric u) const { assert(u >= 0. && u <= 1.); return bin_m_i_ * (pow(u, i_)) * pow((1 - u), m_minus_i); } `````` JasonChmn committed Sep 03, 2019 47 `````` `````` Pierre Fernbach committed Dec 17, 2019 48 49 50 51 52 53 54 55 56 57 58 `````` virtual bool operator== (const Bern& other) const{ return m_minus_i == other.m_minus_i && i_ == other.i_ && bin_m_i_ == other.bin_m_i_; } virtual bool operator!=(const Bern& other) const { return !(*this == other); } `````` Guilhem Saurel committed Sep 24, 2019 59 60 61 62 63 `````` /* Attributes */ Numeric m_minus_i; Numeric i_; Numeric bin_m_i_; /* Attributes */ `````` JasonChmn committed Sep 03, 2019 64 `````` `````` Guilhem Saurel committed Sep 24, 2019 65 66 67 68 69 `````` // Serialization of the class friend class boost::serialization::access; template void serialize(Archive& ar, const unsigned int version) { if (version) { `````` JasonChmn committed Sep 03, 2019 70 `````` // Do something depending on version ? `````` JasonChmn committed Sep 03, 2019 71 `````` } `````` Guilhem Saurel committed Sep 24, 2019 72 73 74 `````` ar& boost::serialization::make_nvp("m_minus_i", m_minus_i); ar& boost::serialization::make_nvp("i", i_); ar& boost::serialization::make_nvp("bin_m_i", bin_m_i_); `````` JasonChmn committed Sep 03, 2019 75 `````` } `````` Guilhem Saurel committed Sep 24, 2019 76 ``````}; // End struct Bern `````` Steve Tonneau committed Mar 13, 2017 77 `````` `````` Guilhem Saurel committed Sep 24, 2019 78 79 80 81 82 83 84 85 86 87 88 89 ``````/// \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 curves #endif //_CLASS_BERNSTEIN``````