Commit 0550f489 authored by JasonChmn's avatar JasonChmn
Browse files

Fix cubic hermite spline problem with duration not equal to 1 / delete nfs...

Fix cubic hermite spline problem with duration not equal to 1 / delete nfs file / Edit name setTimeSplines and getTimeSplines / Delete setTimeSplinesDefault
parent 3bb91b85
/**
* \file bezier_curve.h
* \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_BERNSTEIN
#define _CLASS_BERNSTEIN
#include "curve_abc.h"
#include "MathDefs.h"
#include <math.h>
#include <vector>
#include <stdexcept>
namespace curves
{
/// \brief Computes factorial of a number.
/// \param n : an unsigned integer.
/// \return \f$n!\f$
///
inline unsigned int fact(const unsigned int n)
{
unsigned int res = 1;
for (unsigned int i=2 ; i <= n ; ++i)
res *= i;
return res;
}
/// \brief Computes a binomial coefficient.
/// \param n : an unsigned integer.
/// \param k : an unsigned integer.
/// \return \f$\binom{n}{k}f$
///
inline unsigned int bin(const unsigned int n, const unsigned int k)
{
return fact(n) / (fact(k) * fact(n - k));
}
/// \class Bernstein.
/// \brief Computes a Bernstein polynome.
///
template <typename Numeric = double>
struct Bern{
Bern(const unsigned int m, const unsigned int i)
:m_minus_i(m - i)
,i_(i)
,bin_m_i_(bin(m,i)) {}
~Bern(){}
Numeric operator()(const Numeric u) const
{
assert(u >= 0. && u <= 1.);
return bin_m_i_*(pow(u, i_)) *pow((1-u),m_minus_i);
}
Numeric m_minus_i;
Numeric i_;
Numeric bin_m_i_;
};
/// \brief Computes all Bernstein polynomes for a certain degree.
///
template <typename Numeric>
std::vector<Bern<Numeric> > makeBernstein(const unsigned int n)
{
std::vector<Bern<Numeric> > res;
for(unsigned int i = 0; i<= n; ++i)
res.push_back(Bern<Numeric>(n, i));
return res;
}
} // namespace curves
#endif //_CLASS_BERNSTEIN
......@@ -2,7 +2,6 @@
#define _CLASS_CUBICHERMITESPLINE
#include "curve_abc.h"
#include "bernstein.h"
#include "curve_constraint.h"
#include "MathDefs.h"
......@@ -77,7 +76,7 @@ struct cubic_hermite_spline : public curve_abc<Time, Numeric, Dim, Safe, Point>
{
control_points_.push_back(*it);
}
setTimeSplines(time_control_points);
setTime(time_control_points);
}
/// \brief Destructor.
......@@ -120,7 +119,7 @@ struct cubic_hermite_spline : public curve_abc<Time, Numeric, Dim, Safe, Point>
/// values corresponding to times for \f$P_0, P_1, P_2, ..., P_N\f$ respectively.<br>
/// \param time_control_points : Vector containing time for each control point.
///
void setTimeSplines(const Vector_time & time_control_points)
void setTime(const Vector_time & time_control_points)
{
time_control_points_ = time_control_points;
T_min_ = time_control_points_.front();
......@@ -136,29 +135,6 @@ struct cubic_hermite_spline : public curve_abc<Time, Numeric, Dim, Safe, Point>
}
}
/// \brief Set duration by default of each spline.
/// Set a linear time from 0 to 1 for each control point with a \f$step=1.0/N\f$
/// where \f$N\f$ is the number of control points.<br>
/// Exemple for 5 control points : vector time_control_points_ will contain \f$(0., 0.25, 0.5, 0.75, 1.0)\f$
/// corresponding to time for \f$P_0\f$, \f$P_1\f$, \f$P_2\f$, \f$P_3\f$ and \f$P_4\f$ respectively.
///
void setTimeSplinesDefault()
{
time_control_points_.clear();
T_min_ = 0.;
T_max_ = 1.;
Time timestep = (T_max_- T_min_) / (control_points_.size()-1);
Time time = 0.;
Index i = 0;
for (i=0; i<size(); i++)
{
//time_control_points_.push_back(time);
time_control_points_.push_back(time);
time += timestep;
}
computeDurationSplines();
}
/// \brief Get vector of pair (positition, derivative) corresponding to control points.
/// \return vector containing control points.
///
......@@ -170,7 +146,7 @@ struct cubic_hermite_spline : public curve_abc<Time, Numeric, Dim, Safe, Point>
/// \brief Get vector of Time corresponding to Time for each control point.
/// \return vector containing time of each control point.
///
Vector_time getTimeSplines()
Vector_time getTime()
{
return time_control_points_;
}
......@@ -234,7 +210,11 @@ struct cubic_hermite_spline : public curve_abc<Time, Numeric, Dim, Safe, Point>
Numeric h00, h10, h01, h11;
evalCoeffs(alpha,h00,h10,h01,h11,order_derivative);
//std::cout << "for val t="<<t<<" coef : h00="<<h00<<" h10="<<h10<<" h01="<<h01<<" h11="<<h11<<std::endl;
Point p_ = (h00 * Pair0.first + h10 * Pair0.second + h01 * Pair1.first + h11 * Pair1.second);
Point p_ = (h00 * Pair0.first + h10 * Pair0.second * dt + h01 * Pair1.first + h11 * Pair1.second * dt);
for (std::size_t i=0; i<order_derivative; i++)
{
p_ /= dt;
}
return p_;
}
......
......@@ -968,23 +968,19 @@ void CubicHermitePairsPositionDerivativeTest(bool& error)
control_points.push_back(Pair_point_tangent(P0,T0));
time_control_points.push_back(0.); // Time at P0
control_points.push_back(Pair_point_tangent(P1,T1));
time_control_points.push_back(1.); // Time at P1
time_control_points.push_back(2.); // Time at P1
// Create cubic hermite spline
cubic_hermite_spline_t cubic_hermite_spline_1Pair(control_points.begin(), control_points.end(), time_control_points);
cubic_hermite_spline_1Pair.setTimeSplinesDefault();
cubic_hermite_spline_1Pair.setTime(time_control_points);
//Check
res1 = cubic_hermite_spline_1Pair(0.); // t=0
ComparePoints(P0, res1, errmsg1, error);
res1 = cubic_hermite_spline_1Pair(1.); // t=1
res1 = cubic_hermite_spline_1Pair(2.); // t=1
ComparePoints(P1, res1, errmsg1, error);
res1 = cubic_hermite_spline_1Pair(0.5); // t=0.5
ComparePoints(point_t(0.55,1.0375,1.625), res1, errmsg2, error);
// Test derivative : two pairs
res1 = cubic_hermite_spline_1Pair.derivate(0.,1);
ComparePoints(T0, res1, errmsg3, error);
res1 = cubic_hermite_spline_1Pair.derivate(0.5,1);
ComparePoints(point_t(1.35,2.825,4.5), res1, errmsg3, error);
res1 = cubic_hermite_spline_1Pair.derivate(1.,1);
res1 = cubic_hermite_spline_1Pair.derivate(2.,1);
ComparePoints(T1, res1, errmsg3, error);
// Three pairs
......@@ -1001,8 +997,6 @@ void CubicHermitePairsPositionDerivativeTest(bool& error)
ComparePoints(P1, res1, errmsg2, error);
res1 = cubic_hermite_spline_2Pairs(5.); // t=5
ComparePoints(P2, res1, errmsg1, error);
res1 = cubic_hermite_spline_2Pairs(1.); // t=1.0 , same than in two pairs at t=0.5
ComparePoints(point_t(0.55,1.0375,1.625), res1, errmsg2, error);
// Test derivative : three pairs
res1 = cubic_hermite_spline_2Pairs.derivate(0.,1);
ComparePoints(T0, res1, errmsg3, error);
......@@ -1012,7 +1006,11 @@ void CubicHermitePairsPositionDerivativeTest(bool& error)
ComparePoints(T2, res1, errmsg3, error);
// Test time control points by default => with N control points :
// Time at P0= 0. | Time at P1= 1.0/(N-1) | Time at P2= 2.0/(N-1) | ... | Time at P_(N-1)= (N-1)/(N-1)= 1.0
cubic_hermite_spline_2Pairs.setTimeSplinesDefault();
time_control_points.clear();
time_control_points.push_back(0.); // Time at P0
time_control_points.push_back(0.5); // Time at P1
time_control_points.push_back(1.); // Time at P2
cubic_hermite_spline_2Pairs.setTime(time_control_points);
res1 = cubic_hermite_spline_2Pairs(0.); // t=0
ComparePoints(P0, res1, errmsg1, error);
res1 = cubic_hermite_spline_2Pairs(0.5); // t=0.5
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment