bezier_curve.h 5.07 KB
Newer Older
1
/**
2
* \file bezier_curve.h
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's avatar
stonneau committed
13
#include "curve_abc.h"
14
#include "bernstein.h"
15 16 17 18

#include "MathDefs.h"

#include <vector>
19
#include <stdexcept>
20 21 22

namespace spline
{
23
/// \class BezierCurve
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.
27
///
28
template<typename Time= double, typename Numeric=Time, std::size_t Dim=3, bool Safe=false
29
, typename Point= Eigen::Matrix<Numeric, Dim, 1> >
30
struct bezier_curve : public curve_abc<Time, Numeric, Dim, Safe, Point>
31 32 33
{
	typedef Point 	point_t;
	typedef Time 	time_t;
34 35
    typedef Numeric	num_t;
    typedef std::vector<point_t,Eigen::aligned_allocator<point_t> > t_point_t;
36
    typedef bezier_curve<Time, Numeric, Dim, Safe, Point > bezier_curve_t;
37

38 39
/* Constructors - destructors */
	public:
40 41
	///\brief Constructor
	///\param PointsBegin, PointsEnd : the points parametering the Bezier curve
42
    ///
43
	template<typename In>
stonneau's avatar
stonneau committed
44
	bezier_curve(In PointsBegin, In PointsEnd, const time_t minBound=0, const time_t maxBound=1)
45 46 47
	: minBound_(minBound)
	, maxBound_(maxBound)
	, size_(std::distance(PointsBegin, PointsEnd))
48
    , bernstein_(spline::makeBernstein<num_t>(size_-1))
49
	{
50
        assert(bernstein_.size() == size_);
51
		In it(PointsBegin);
52
        if(Safe && (size_<1 || minBound >= maxBound))
53
		{
54
            throw std::out_of_range("can't create bezier min bound is higher than max bound"); // TODO
55 56 57 58 59 60
		}
		for(; it != PointsEnd; ++it)
		{
			pts_.push_back(*it);
		}
	}
61

62
	///\brief Destructor
stonneau's avatar
stonneau committed
63
	~bezier_curve()
64 65 66
	{
		// NOTHING
	}
67 68

	private:
69 70
//	bezier_curve(const bezier_curve&);
//  bezier_curve& operator=(const bezier_curve&);
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
77
	///  \param return : the value x(t)
78
    virtual point_t operator()(const time_t t) const
79 80 81 82
	{
		num_t nT = (t - minBound_) / (maxBound_ - minBound_);
		if(Safe &! (0 <= nT && nT <= 1))
		{
83
            throw std::out_of_range("can't evaluate bezier curve, out of range"); // TODO
84
        }
85 86 87 88
		else
		{
			num_t dt = (1 - nT);
			switch(size_)
89 90 91
            {
                case 1 :
                    return pts_[0];
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;
100
                case 4 :
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;
105 106
                default :
                    return evalBernstein(dt);
107 108 109 110
				break;
			}
		}
	}
111

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);
    }

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<Bern<Numeric> >::const_iterator cit = bernstein_.begin();
            cit !=bernstein_.end(); ++cit, ++pts_it)
        {
            res += cit->operator()(u) * (*pts_it);
        }
        return res;
    }

154
/*Operations*/
155 156

/*Helpers*/
stonneau's avatar
stonneau committed
157 158
	virtual time_t min() const{return minBound_;}
	virtual time_t max() const{return maxBound_;}
159 160 161
/*Helpers*/

	public:
162 163 164
    const time_t minBound_, maxBound_;
    const std::size_t size_;
    const std::vector<Bern<Numeric> > bernstein_;
165
	
166 167 168 169
    private:
    t_point_t  pts_;

    //storing bernstein polynoms, even in low dimension
170
};
171 172 173
}
#endif //_CLASS_BEZIERCURVE