bezier_curve.h 3.58 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 24 25
/// \class BezierCurve
/// \brief Represents a curve
///
26
template<typename Time= double, typename Numeric=Time, std::size_t Dim=3, bool Safe=false
27 28 29 30 31
, typename Point= Eigen::Matrix<Numeric, Dim, 1> >
struct bezier_curve : public  curve_abc<Time, Numeric, Dim, Safe, Point>
{
	typedef Point 	point_t;
	typedef Time 	time_t;
32 33
    typedef Numeric	num_t;
    typedef std::vector<point_t,Eigen::aligned_allocator<point_t> > t_point_t;
34

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

59
	///\brief Destructor
stonneau's avatar
stonneau committed
60
	~bezier_curve()
61 62 63
	{
		// NOTHING
	}
64 65

	private:
66 67
//	bezier_curve(const bezier_curve&);
//  bezier_curve& operator=(const bezier_curve&);
68 69 70 71 72 73
/* Constructors - destructors */

/*Operations*/
	public:
	///  \brief Evaluation of the cubic spline at time t.
	///  \param t : the time when to evaluate the spine
74
	///  \param return : the value x(t)
75
    virtual point_t operator()(const time_t t) const
76 77 78 79
	{
		num_t nT = (t - minBound_) / (maxBound_ - minBound_);
		if(Safe &! (0 <= nT && nT <= 1))
		{
80
            throw std::out_of_range("can't evaluate bezier curve, out of range"); // TODO
81
        }
82 83 84 85 86 87 88 89 90 91 92 93 94
		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;
95
                case 4 :
96 97 98 99
					return 	pts_[0] * dt * dt * dt
						+ 3 * pts_[1] * nT * dt * dt 
						+ 3 * pts_[2] * nT * nT * dt 
						+ pts_[3] * nT * nT *nT;
100 101
                default :
                    return evalBernstein(dt);
102 103 104 105
				break;
			}
		}
	}
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

    ///
    /// \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;
    }

122
/*Operations*/
123 124

/*Helpers*/
stonneau's avatar
stonneau committed
125 126
	virtual time_t min() const{return minBound_;}
	virtual time_t max() const{return maxBound_;}
127 128 129
/*Helpers*/

	public:
130 131 132
    const time_t minBound_, maxBound_;
    const std::size_t size_;
    const std::vector<Bern<Numeric> > bernstein_;
133
	
134 135 136 137
    private:
    t_point_t  pts_;

    //storing bernstein polynoms, even in low dimension
138
};
139 140 141
}
#endif //_CLASS_BEZIERCURVE