bezier_curve.h 3.73 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 > struct bezier_curve : public curve_abc { 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; `````` stonneau committed Jun 27, 2013 36 `````` `````` stonneau committed Jun 27, 2013 37 38 ``````/* Constructors - destructors */ public: `````` stonneau committed Jun 27, 2013 39 40 `````` ///\brief Constructor ///\param PointsBegin, PointsEnd : the points parametering the Bezier curve `````` Steve Tonneau committed Mar 13, 2017 41 `````` /// `````` stonneau committed Jun 27, 2013 42 `````` template `````` stonneau committed Jun 27, 2013 43 `````` bezier_curve(In PointsBegin, In PointsEnd, const time_t minBound=0, const time_t maxBound=1) `````` stonneau committed Jun 27, 2013 44 45 46 `````` : minBound_(minBound) , maxBound_(maxBound) , size_(std::distance(PointsBegin, PointsEnd)) `````` Steve Tonneau committed Mar 13, 2017 47 `````` , bernstein_(spline::makeBernstein(size_-1)) `````` stonneau committed Jun 27, 2013 48 `````` { `````` Steve Tonneau committed Mar 13, 2017 49 `````` assert(bernstein_.size() == size_); `````` stonneau committed Jun 27, 2013 50 51 52 `````` In it(PointsBegin); if(Safe && (size_<=1 || minBound == maxBound)) { `````` Steve Tonneau committed Mar 13, 2017 53 `````` throw std::out_of_range("can't create bezier min bound is higher than max bound"); // TODO `````` stonneau committed Jun 27, 2013 54 55 56 57 58 59 `````` } for(; it != PointsEnd; ++it) { pts_.push_back(*it); } } `````` stonneau committed Jun 27, 2013 60 `````` `````` stonneau committed Jun 27, 2013 61 `````` ///\brief Destructor `````` stonneau committed Jun 27, 2013 62 `````` ~bezier_curve() `````` stonneau committed Jun 27, 2013 63 64 65 `````` { // NOTHING } `````` stonneau committed Jun 27, 2013 66 67 `````` private: `````` Steve Tonneau committed Mar 13, 2017 68 69 ``````// bezier_curve(const bezier_curve&); // bezier_curve& operator=(const bezier_curve&); `````` stonneau committed Jun 27, 2013 70 71 72 73 74 75 ``````/* 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 76 `````` /// \param return : the value x(t) `````` Steve Tonneau committed Jan 26, 2017 77 `````` virtual point_t operator()(const time_t t) const `````` stonneau committed Jun 27, 2013 78 79 80 81 `````` { num_t nT = (t - minBound_) / (maxBound_ - minBound_); if(Safe &! (0 <= nT && nT <= 1)) { `````` Steve Tonneau committed Mar 13, 2017 82 `````` throw std::out_of_range("can't evaluate bezier curve, out of range"); // TODO `````` Steve Tonneau committed Jan 27, 2017 83 `````` } `````` stonneau committed Jun 27, 2013 84 85 86 87 88 89 90 91 92 93 94 95 96 `````` else { num_t dt = (1 - nT); switch(size_) { 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 97 `````` case 4 : `````` stonneau committed Jun 27, 2013 98 99 100 101 `````` 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 102 103 `````` default : return evalBernstein(dt); `````` stonneau committed Jun 27, 2013 104 105 106 107 `````` break; } } } `````` Steve Tonneau committed Mar 13, 2017 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 `````` /// /// \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 124 ``````/*Operations*/ `````` stonneau committed Jun 27, 2013 125 126 `````` /*Helpers*/ `````` stonneau committed Jun 27, 2013 127 128 `````` virtual time_t min() const{return minBound_;} virtual time_t max() const{return maxBound_;} `````` stonneau committed Jun 27, 2013 129 130 131 ``````/*Helpers*/ public: `````` Steve Tonneau committed Mar 13, 2017 132 133 134 `````` const time_t minBound_, maxBound_; const std::size_t size_; const std::vector > bernstein_; `````` stonneau committed Jun 27, 2013 135 `````` `````` Steve Tonneau committed Mar 13, 2017 136 137 138 139 `````` private: t_point_t pts_; //storing bernstein polynoms, even in low dimension `````` stonneau committed Jun 27, 2013 140 ``````}; `````` stonneau committed Jun 27, 2013 141 142 143 ``````} #endif //_CLASS_BEZIERCURVE ``````