Skip to content

Commit

Permalink
Merge pull request #12 from bchretien/polynomial-3
Browse files Browse the repository at this point in the history
polynomial-3: use a typedef of Polynomial<3>.
  • Loading branch information
bchretien committed Apr 11, 2014
2 parents 9df41ed + 1953b7f commit 7351003
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 273 deletions.
61 changes: 5 additions & 56 deletions include/roboptim/trajectory/polynomial-3.hh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (C) 2012 by Florent Lamiraux, CNRS.
// Copyright (C) 2014 by Benjamin Chrétien, CNRS-LIRMM.
//
// This file is part of the roboptim.
//
Expand All @@ -18,73 +19,21 @@
#ifndef ROBOPTIM_TRAJECTORY_POLYNOMIAL_3_HH
# define ROBOPTIM_TRAJECTORY_POLYNOMIAL_3_HH

# include <roboptim/core/function.hh>

# include <ostream>
# include <roboptim/trajectory/polynomial.hh>

namespace roboptim {

/// \brief Polynomial of degree at most 3.
/// \f[
/// P (t) = \sum_{i=0}{3} a_i (t-t_0)^i
/// P (t) = \sum_{i=0}^{3} a_i (t-t_0)^i
/// \f]
class Polynomial3
{
public:
typedef Function::size_type size_type;

/// \brief Default constructor: create a null polynomial.
Polynomial3 ();
Polynomial3 (double t0, double a0, double a1, double a2, double a3);
Polynomial3 operator* (const Polynomial3& poly) const;
Polynomial3 operator+ (const Polynomial3& poly) const;
Polynomial3 operator- (const Polynomial3& poly) const;

/// \brief Return a new polynomial translated from (t-t0) to (t-t1).
Polynomial3 translate (const double &t1) const;

/// \brief Translate the polynomial (in place) from (t-t0) to (t-t1).
void translateInPlace (const double &t1);

/// \brief Evaluate the polynomial.
double operator () (const double& t) const;

/// \brief Evaluate the derivative of a given order.
double derivative (const double& t, size_type order = 1) const;

/// \brief Print Polynomial3.
std::ostream& print (std::ostream& o) const throw ();

/// \brief Get the i-th polynomial coefficient.
/// \param i number of the coefficient to get.
/// \return i-th polynomial coefficient αi, with: α_i (t-t₀)^i
double operator [] (int i) const;

/// \brief Return the real roots of the translated polynomial.
/// \warning This function relies on Eigen's experimental polynomial solver.
/// \return vector of the real roots of the translated polynomial.
std::vector<double> realRoots () const;

/// \brief Polynomial coefficients (ordered from lowest to highest degree).
double coefs_ [4];

/// \brief Point on which the polynomial is centered.
double t0_;
}; // class Polynomial3

/// \brief Scalar multiplication of a 3rd degree polynomial.
Polynomial3 operator* (const double& lambda, const Polynomial3& poly);
typedef Polynomial<3> Polynomial3;

/// \brief Monomial3
/// \f[
/// M (t) = t-t_0
/// \f]
struct Monomial3 : public Polynomial3
{
Monomial3 (double t0) : Polynomial3 (t0, 0., 1., 0., 0.) {}
}; // class Monomial

std::ostream& operator << (std::ostream& o, const Polynomial3& p);
typedef Monomial<3> Monomial3;

} // namespace roboptim
#endif // ROBOPTIM_TRAJECTORY_POLYNOMIAL_3_HH
6 changes: 6 additions & 0 deletions include/roboptim/trajectory/polynomial.hh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ namespace roboptim
/// \param coefs polynomial coefficients.
Polynomial (value_type t0, const vector_t& coefs);

/// \brief Variadic constructor.
/// Note: this is a legacy constructor used to keep Polynomial3's API.
/// \param t0 polynomial of (t-t₀).
/// \param ... variadic arguments containing [a₀,a₁,...,a_N]
Polynomial (value_type t0, ...);

/// \brief Copy constructor of polynomials of different orders.
///
/// \warning If N < M, coefficients of higher degrees will be discarded.
Expand Down
19 changes: 19 additions & 0 deletions include/roboptim/trajectory/polynomial.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

# include <algorithm> // transform
# include <limits> // numeric_limits
# include <cstdarg> // va_list, va_start, va_arg, va_end

// Polynomial solver
# include <unsupported/Eigen/Polynomials>
Expand All @@ -43,6 +44,24 @@ namespace roboptim
coefs_ = coefs;
}

template <int N>
Polynomial<N>::Polynomial (value_type t0, ...)
: t0_ (t0)
{
va_list vl;
// Note: void va_start (va_list ap, paramN);
// ---> "paramN: Name of the last named parameter in the function
// definition. The arguments extracted by subsequent calls to
// va_arg are those after paramN."
// See: http://www.cplusplus.com/reference/cstdarg/va_start/
va_start (vl, t0);

for (size_type i = 0; i < N + 1; ++i)
// We expect coefs to be of size N+1.
coefs_[i] = va_arg (vl, value_type);
va_end (vl);
}

template <int N>
template <int M>
Polynomial<N>::Polynomial (const Polynomial<M>& p)
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ ADD_LIBRARY(roboptim-trajectory SHARED
spline-length.cc
debug.hh
cubic-b-spline.cc
polynomial-3.cc
)
PKG_CONFIG_USE_DEPENDENCY(roboptim-trajectory roboptim-core)
SET_TARGET_PROPERTIES(roboptim-trajectory PROPERTIES SOVERSION 1.1.0)
Expand Down
205 changes: 0 additions & 205 deletions src/polynomial-3.cc

This file was deleted.

11 changes: 0 additions & 11 deletions tests/polynomial.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,6 @@ void poly_props<3>::check_translate (const Polynomial<3>& poly,
}
}

//FIXME: import code from Polynomial3 once it is replaced by Polynomial
template <>
void poly_props<3>::check_evaluate (double t0,
Polynomial<3>::coefs_t& params,
double t)
{
Polynomial<3> new_poly (t0, params);
Polynomial3 old_poly (t0, params (0), params (1), params (2), params (3));
BOOST_CHECK_CLOSE (new_poly (t), old_poly (t), tol);
}


template <>
void poly_props<5>::check_translate (const Polynomial<5>& poly,
Expand Down

0 comments on commit 7351003

Please sign in to comment.