Skip to content

Commit

Permalink
Merge pull request #14 from bchretien/constrained-b-spline
Browse files Browse the repository at this point in the history
Constrained B-spline
  • Loading branch information
thomas-moulard committed Apr 14, 2014
2 parents 2178602 + c593b84 commit c219722
Show file tree
Hide file tree
Showing 8 changed files with 560 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ SET(HEADERS
${CSD}/include/roboptim/trajectory/anthropomorphic-cost-function.hxx
${CSD}/include/roboptim/trajectory/b-spline.hh
${CSD}/include/roboptim/trajectory/b-spline.hxx
${CSD}/include/roboptim/trajectory/constrained-b-spline.hh
${CSD}/include/roboptim/trajectory/constrained-b-spline.hxx
${CSD}/include/roboptim/trajectory/cubic-b-spline.hh
${CSD}/include/roboptim/trajectory/cubic-b-spline.hxx
${CSD}/include/roboptim/trajectory/free-time-trajectory.hh
Expand Down
34 changes: 30 additions & 4 deletions include/roboptim/trajectory/b-spline.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace roboptim
{
public:
typedef Trajectory<N> parent_t;

typedef typename parent_t::interval_t interval_t;
typedef typename parent_t::size_type size_type;
typedef typename parent_t::value_type value_type;
Expand All @@ -49,6 +50,12 @@ namespace roboptim
typedef typename parent_t::result_t result_t;
typedef typename parent_t::gradient_t gradient_t;
typedef typename parent_t::jacobian_t jacobian_t;

typedef Polynomial<N> polynomial_t;
typedef Monomial<N> monomial_t;
typedef std::map<int, polynomial_t> cox_map;
typedef typename cox_map::iterator cox_map_itr_t;

/// \brief Instantiate a B-Spline from its definition.
///
/// \param timeRange spline time range: $\f$[t_3,t_n]\f$
Expand Down Expand Up @@ -118,8 +125,29 @@ namespace roboptim

size_type interval (value_type t) const;

/// \brief Get the number of control points of the spline.
/// \return Number of control points of the spline.
size_type getNumberControlPoints() const
{
return nbp_;
}

/// \brief Constant getter for the basis polynomials of the B-spline.
///
/// Note: computeBasisPolynomials() needs to be called beforehand (which is
/// done in the BSpline constructor).
///
/// \return constant reference to the basis polynomials.
const std::vector <std::vector <polynomial_t> >&
basisPolynomials () const
{
return basisPolynomials_;
}

protected:

using Trajectory<N>::impl_compute;

void impl_compute (result_t&, double) const throw ();
void impl_derivative (gradient_t& g, double x, size_type order)
const throw ();
Expand All @@ -128,14 +156,12 @@ namespace roboptim

vector_t basisFunctions (value_type t, size_type order) const
ROBOPTIM_TRAJECTORY_DEPRECATED;

void computeBasisPolynomials ();

/// order of the B-Spline
static const size_type order_ = N;

typedef Polynomial<N> polynomial_t;
typedef Monomial<N> monomial_t;
typedef std::map<int, polynomial_t> cox_map;
typedef typename cox_map::iterator cox_map_itr_t;
cox_map cox_de_boor (size_type j, size_type n) const;

private:
Expand Down
3 changes: 2 additions & 1 deletion include/roboptim/trajectory/b-spline.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ namespace roboptim
: Trajectory<N> (spline.timeRange (), spline.outputSize (),
spline.parameters_),
nbp_ (spline.parameters_.size () / spline.outputSize ()),
knots_ (spline.knots_), basisPolynomials_ (spline.basisPolynomials_),
knots_ (spline.knots_),
basisPolynomials_ (spline.basisPolynomials_),
uniform_ (spline.uniform_)
{
// Parameter size should be a multiple of spline dimension
Expand Down
133 changes: 133 additions & 0 deletions include/roboptim/trajectory/constrained-b-spline.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Copyright (C) 2013 by Alexander Werner, DLR.
// Copyright (C) 2014 by Benjamin Chrétien, CNRS-LIRMM.
//
// This file is part of the roboptim.
//
// roboptim is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// roboptim is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with roboptim. If not, see <http://www.gnu.org/licenses/>.

#ifndef ROBOPTIM_TRAJECTORY_CONSTRAINED_B_SPLINE_HH
# define ROBOPTIM_TRAJECTORY_CONSTRAINED_B_SPLINE_HH

# include <roboptim/trajectory/sys.hh>
# include <roboptim/trajectory/deprecated.hh>
# include <roboptim/trajectory/b-spline.hh>

namespace roboptim
{
/// \addtogroup roboptim_function
/// @{

/// \brief Constrained B-spline.
///
/// \tparam N B-spline order.
template <int N>
class ConstrainedBSpline : public BSpline<N>
{
public:
typedef BSpline<N> parent_t;
typedef typename parent_t::interval_t interval_t;
typedef typename parent_t::size_type size_type;
typedef typename parent_t::value_type value_type;
typedef typename parent_t::vector_t vector_t;
typedef typename parent_t::matrix_t matrix_t;
typedef typename parent_t::jacobian_t jacobian_t;

/// \brief see B-Spline constructors for documentation
ConstrainedBSpline (interval_t timeRange, size_type dimension,
const vector_t& parameters,
const std::string name = "Constrained B-Spline")
throw ();

/// \brief see B-Spline constructors for documentation
ConstrainedBSpline (interval_t tr, size_type dimension,
const vector_t& parameters,
const vector_t& knots,
std::string name = "Constrained B-Spline") throw ();

/// \brief Destructor of constrained B-spline.
virtual ~ConstrainedBSpline () throw ();

/// Creates a constraint on the basic spline.
/// This reduces the number of parameter by one.
/// The constraint equation is:
/// \f$value = \frac{\partial^derivative}{\partial t^derivative}
/// f_{dimension}(t) \f$
/// \param t time in the spline to constrain.
/// \param dimension Which dimension of the spline to constrain.
/// \param value Desired spline value at t.
/// \param derivative Which derivative of the spline to constrain.
void addFixedConstraint (double t,
size_type dimension,
value_type value,
size_type derivative = 0) throw ();

/// \brief Create a constraint against another part of the spline.
/// \f$\frac{\partial^d}{\partial t^d} f_{d_1}(t_1)
/// = factor * \frac{\partial^d}{\partial t^d} f_{d_2}(t_2) \f$
/// \param t_1 first time.
/// \param dimension_1 first dimension.
/// \param t_2 second time.
/// \param dimension_2 second dimension.
/// \param derivative derivative order (\f$d\f$).
/// \param factor scalar factor.
void addCoupledConstraint
(value_type t_1, size_type dimension_1,
value_type t_2, size_type dimension_2,
size_type derivative = 0, value_type factor = 1.) throw ();

/// Overloaded parameters method from Trajectory<N>.
/// Returns only the tunable parameters.
const vector_t& parameters () const throw ();

/// Overloaded setParameters method from Trajectory<N>.
/// Given the free parameters, it calculates the spline parameters.
void setParameters (const vector_t&) throw ();

/// \brief Clone and resize a trajectory.
/// \param timeRange new time range.
/// \return cloned trajectory with new time range.
virtual Trajectory<N>* resize (interval_t timeRange) const throw ();

jacobian_t variationDerivWrtParam (double t, size_type order)
const throw ();

protected:

/// \brief Update the projector matrix. Called after adding a constraint.
void updateProjector();

/// \brief Matrix storing constraints.
/// One row per constraint, one column per parameter.
matrix_t constraints_;

/// \brief Constraint values.
vector_t constraint_values_;

/// \brief Tunable parameters, i.e. the free parameters.
vector_t tunables_;

/// \brief Projector matrix.
matrix_t projector_;

/// \brief Result of: Constraints * X = Constraint values
vector_t projector_offset_;
};

/// @}

} // end of namespace roboptim.

# include <roboptim/trajectory/constrained-b-spline.hxx>

#endif //! ROBOPTIM_TRAJECTORY_CONSTRAINED_B_SPLINE_HH

0 comments on commit c219722

Please sign in to comment.