bezier_curve.h 2.56 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 15 16 17 18 19 20

#include "MathDefs.h"

#include <vector>

namespace spline
{
21 22 23 24 25 26 27 28 29 30 31
/// \class BezierCurve
/// \brief Represents a curve
///
template<typename Time= double, typename Numeric=Time, int Dim=3, bool Safe=false
, 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;
	typedef Numeric	num_t;

32 33
/* Constructors - destructors */
	public:
34 35 36 37
	///\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
38
	bezier_curve(In PointsBegin, In PointsEnd, const time_t minBound=0, const time_t maxBound=1)
39 40 41 42 43 44 45 46 47 48 49 50 51 52
	: minBound_(minBound)
	, maxBound_(maxBound)
	, size_(std::distance(PointsBegin, PointsEnd))
	{
		In it(PointsBegin);
		if(Safe && (size_<=1 || minBound == maxBound))
		{
			throw; // TODO 
		}
		for(; it != PointsEnd; ++it)
		{
			pts_.push_back(*it);
		}
	}
53

54
	///\brief Destructor
stonneau's avatar
stonneau committed
55
	~bezier_curve()
56 57 58
	{
		// NOTHING
	}
59 60

	private:
61 62
	bezier_curve(const bezier_curve&);
	bezier_curve& operator=(const bezier_curve&);
63 64 65
/* Constructors - destructors */

/*Operations*/
66
	public:
67 68 69
	public:
	///  \brief Evaluation of the cubic spline at time t.
	///  \param t : the time when to evaluate the spine
70
	///  \param return : the value x(t)
stonneau's avatar
stonneau committed
71
	virtual point_t operator()(time_t t) const
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	{
		num_t nT = (t - minBound_) / (maxBound_ - minBound_);
		if(Safe &! (0 <= nT && nT <= 1))
		{
			throw; // TODO
		}
		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;
				default :
					return 	pts_[0] * dt * dt * dt
						+ 3 * pts_[1] * nT * dt * dt 
						+ 3 * pts_[2] * nT * nT * dt 
						+ pts_[3] * nT * nT *nT;
				break;
			}
		}
	}
100
/*Operations*/
101 102

/*Helpers*/
stonneau's avatar
stonneau committed
103 104
	virtual time_t MinBound() const{return minBound_;}
	virtual time_t MaxBound() const{return minBound_;}
105 106 107
/*Helpers*/

	public:
108 109
	const int size_;
	const time_t minBound_, maxBound_;
110 111
	
	private:
112 113 114
	typedef std::vector<Point,Eigen::aligned_allocator<Point> > T_Vector;
	T_Vector  pts_;
};
115 116 117
}
#endif //_CLASS_BEZIERCURVE