bezier_curve.h 5.07 KB
 stonneau committed Jun 27, 2013 1 ``````/** `````` stonneau committed Jun 27, 2013 2 ``````* \file bezier_curve.h `````` stonneau committed Jun 27, 2013 3 4 5 6 7 8 9 10 11 12 ``````* \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_BEZIERCURVE #define _CLASS_BEZIERCURVE `````` stonneau committed Jun 27, 2013 13 ``````#include "curve_abc.h" `````` Steve Tonneau committed Mar 13, 2017 14 ``````#include "bernstein.h" `````` stonneau committed Jun 27, 2013 15 16 17 18 `````` #include "MathDefs.h" #include `````` Steve Tonneau committed Mar 13, 2017 19 ``````#include `````` stonneau committed Jun 27, 2013 20 21 22 `````` namespace spline { `````` stonneau committed Jun 27, 2013 23 ``````/// \class BezierCurve `````` Steve Tonneau committed Mar 13, 2017 24 25 26 ``````/// \brief Represents a Bezier curve of arbitrary dimension and order. /// For degree lesser than 4, the evaluation is analitycal.Otherwise /// the bernstein polynoms are used to evaluate the spline at a given location. `````` stonneau committed Jun 27, 2013 27 ``````/// `````` Steve Tonneau committed Nov 24, 2016 28 ``````template > `````` Steve Tonneau committed Mar 14, 2017 30 ``````struct bezier_curve : public curve_abc `````` stonneau committed Jun 27, 2013 31 32 33 ``````{ typedef Point point_t; typedef Time time_t; `````` Steve Tonneau committed Mar 13, 2017 34 35 `````` typedef Numeric num_t; typedef std::vector > t_point_t; `````` Steve Tonneau committed Mar 14, 2017 36 `````` typedef bezier_curve bezier_curve_t; `````` stonneau committed Jun 27, 2013 37 `````` `````` stonneau committed Jun 27, 2013 38 39 ``````/* Constructors - destructors */ public: `````` stonneau committed Jun 27, 2013 40 41 `````` ///\brief Constructor ///\param PointsBegin, PointsEnd : the points parametering the Bezier curve `````` Steve Tonneau committed Mar 13, 2017 42 `````` /// `````` stonneau committed Jun 27, 2013 43 `````` template `````` stonneau committed Jun 27, 2013 44 `````` bezier_curve(In PointsBegin, In PointsEnd, const time_t minBound=0, const time_t maxBound=1) `````` stonneau committed Jun 27, 2013 45 46 47 `````` : minBound_(minBound) , maxBound_(maxBound) , size_(std::distance(PointsBegin, PointsEnd)) `````` Steve Tonneau committed Mar 13, 2017 48 `````` , bernstein_(spline::makeBernstein(size_-1)) `````` stonneau committed Jun 27, 2013 49 `````` { `````` Steve Tonneau committed Mar 13, 2017 50 `````` assert(bernstein_.size() == size_); `````` stonneau committed Jun 27, 2013 51 `````` In it(PointsBegin); `````` Steve Tonneau committed Mar 14, 2017 52 `````` if(Safe && (size_<1 || minBound >= maxBound)) `````` stonneau committed Jun 27, 2013 53 `````` { `````` Steve Tonneau committed Mar 13, 2017 54 `````` throw std::out_of_range("can't create bezier min bound is higher than max bound"); // TODO `````` stonneau committed Jun 27, 2013 55 56 57 58 59 60 `````` } for(; it != PointsEnd; ++it) { pts_.push_back(*it); } } `````` stonneau committed Jun 27, 2013 61 `````` `````` stonneau committed Jun 27, 2013 62 `````` ///\brief Destructor `````` stonneau committed Jun 27, 2013 63 `````` ~bezier_curve() `````` stonneau committed Jun 27, 2013 64 65 66 `````` { // NOTHING } `````` stonneau committed Jun 27, 2013 67 68 `````` private: `````` Steve Tonneau committed Mar 13, 2017 69 70 ``````// bezier_curve(const bezier_curve&); // bezier_curve& operator=(const bezier_curve&); `````` stonneau committed Jun 27, 2013 71 72 73 74 75 76 ``````/* Constructors - destructors */ /*Operations*/ public: /// \brief Evaluation of the cubic spline at time t. /// \param t : the time when to evaluate the spine `````` stonneau committed Jun 27, 2013 77 `````` /// \param return : the value x(t) `````` Steve Tonneau committed Jan 26, 2017 78 `````` virtual point_t operator()(const time_t t) const `````` stonneau committed Jun 27, 2013 79 80 81 82 `````` { num_t nT = (t - minBound_) / (maxBound_ - minBound_); if(Safe &! (0 <= nT && nT <= 1)) { `````` Steve Tonneau committed Mar 13, 2017 83 `````` throw std::out_of_range("can't evaluate bezier curve, out of range"); // TODO `````` Steve Tonneau committed Jan 27, 2017 84 `````` } `````` stonneau committed Jun 27, 2013 85 86 87 88 `````` else { num_t dt = (1 - nT); switch(size_) `````` Steve Tonneau committed Mar 14, 2017 89 90 91 `````` { case 1 : return pts_[0]; `````` stonneau committed Jun 27, 2013 92 93 94 95 96 97 98 99 `````` case 2 : return pts_[0] * dt + nT * pts_[1]; break; case 3 : return pts_[0] * dt * dt + 2 * pts_[1] * nT * dt + pts_[2] * nT * nT; break; `````` Steve Tonneau committed Mar 13, 2017 100 `````` case 4 : `````` stonneau committed Jun 27, 2013 101 102 103 104 `````` return pts_[0] * dt * dt * dt + 3 * pts_[1] * nT * dt * dt + 3 * pts_[2] * nT * nT * dt + pts_[3] * nT * nT *nT; `````` Steve Tonneau committed Mar 13, 2017 105 106 `````` default : return evalBernstein(dt); `````` stonneau committed Jun 27, 2013 107 108 109 110 `````` break; } } } `````` Steve Tonneau committed Mar 13, 2017 111 `````` `````` Steve Tonneau committed Mar 14, 2017 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 `````` /// \brief Computes the derivative curve at order N. /// \param order : order of the derivative /// \param return : the value x(t) bezier_curve_t compute_derivate(const std::size_t order) const { if(order == 0) return *this; t_point_t derived_wp; for(typename t_point_t::const_iterator pit = pts_.begin(); pit != pts_.end()-1; ++pit) derived_wp.push_back(*(pit+1) - (*pit)); if(derived_wp.empty()) derived_wp.push_back(point_t::Zero()); bezier_curve_t deriv(derived_wp.begin(), derived_wp.end(),minBound_,maxBound_); return deriv.compute_derivate(order-1); } /// \brief Evaluates the derivative at order N of the curve. /// If the derivative is to be evaluated several times, it is /// rather recommended to compute the derivative curve using compute_derivate /// \param order : order of the derivative /// \param t : the time when to evaluate the spine /// \param return : the value x(t) virtual point_t derivate(const time_t t, const std::size_t order) const { bezier_curve_t deriv =compute_derivate(order); return deriv(t); } `````` Steve Tonneau committed Mar 13, 2017 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 `````` /// /// \brief Evaluates all Bernstein polynomes for a certain degree /// point_t evalBernstein(const Numeric u) const { point_t res = point_t::Zero(); typename t_point_t::const_iterator pts_it = pts_.begin(); for(typename std::vector >::const_iterator cit = bernstein_.begin(); cit !=bernstein_.end(); ++cit, ++pts_it) { res += cit->operator()(u) * (*pts_it); } return res; } `````` stonneau committed Jun 27, 2013 154 ``````/*Operations*/ `````` stonneau committed Jun 27, 2013 155 156 `````` /*Helpers*/ `````` stonneau committed Jun 27, 2013 157 158 `````` virtual time_t min() const{return minBound_;} virtual time_t max() const{return maxBound_;} `````` stonneau committed Jun 27, 2013 159 160 161 ``````/*Helpers*/ public: `````` Steve Tonneau committed Mar 13, 2017 162 163 164 `````` const time_t minBound_, maxBound_; const std::size_t size_; const std::vector > bernstein_; `````` stonneau committed Jun 27, 2013 165 `````` `````` Steve Tonneau committed Mar 13, 2017 166 167 168 169 `````` private: t_point_t pts_; //storing bernstein polynoms, even in low dimension `````` stonneau committed Jun 27, 2013 170 ``````}; `````` stonneau committed Jun 27, 2013 171 172 173 ``````} #endif //_CLASS_BEZIERCURVE ``````