Libbarrett  1.2.4
include/barrett/math/spline.h
00001 /*
00002         Copyright 2009, 2010, 2011, 2012 Barrett Technology <support@barrett.com>
00003 
00004         This file is part of libbarrett.
00005 
00006         This version of libbarrett is free software: you can redistribute it
00007         and/or modify it under the terms of the GNU General Public License as
00008         published by the Free Software Foundation, either version 3 of the
00009         License, or (at your option) any later version.
00010 
00011         This version of libbarrett is distributed in the hope that it will be
00012         useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00013         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014         GNU General Public License for more details.
00015 
00016         You should have received a copy of the GNU General Public License along
00017         with this version of libbarrett.  If not, see
00018         <http://www.gnu.org/licenses/>.
00019 
00020         Further, non-binding information about licensing is available at:
00021         <http://wiki.barrett.com/libbarrett/wiki/LicenseNotes>
00022 */
00023 
00024 /*
00025  * spline.h
00026  *
00027  *  Created on: Dec 17, 2009
00028  *      Author: dc
00029  */
00030 
00031 
00032 #ifndef BARRETT_MATH_SPLINE_H_
00033 #define BARRETT_MATH_SPLINE_H_
00034 
00035 
00036 #include <vector>
00037 
00038 #include <boost/tuple/tuple.hpp>
00039 
00040 #define EIGEN_USE_NEW_STDVECTOR
00041 #include <Eigen/StdVector>
00042 #include <Eigen/Geometry>
00043 
00044 #include <barrett/detail/ca_macro.h>
00045 #include <barrett/math/detail/spline-helper.h>
00046 
00047 
00048 // forward declaration from <barrett/spline/spline.h>
00049 struct bt_spline;
00050 
00051 
00052 namespace barrett {
00053 namespace math {
00054 
00055 
00056 template<typename T>
00057 class Spline {
00058 public:
00059         typedef T data_type;
00060         typedef boost::tuple<double, T> tuple_type;
00061 
00062         template<template<typename, typename> class Container, typename Allocator>
00063         Spline(const Container<tuple_type, Allocator>& samples, bool saturateS = true);
00064 
00065         // initialDirection will be normalized internally
00066         template<template<typename, typename> class Container, typename Allocator>
00067         Spline(const Container<T, Allocator>& points, /*const typename T::unitless_type& initialDirection = typename T::unitless_type(0.0),*/ bool saturateS = true);
00068 
00069         ~Spline();
00070 
00071         double initialS() const { return s_0; }
00072         double finalS() const { return s_f; }
00073         double changeInS() const;
00074 
00075         T eval(double s) const;
00076         T evalDerivative(double s) const;
00077 
00078         typedef T result_type;  
00079         result_type operator() (double s) const {
00080                 return eval(s);
00081         }
00082 
00083         // Access the low-level spline implementation. Please avoid using this.
00084         struct bt_spline* getImplementation() { return impl; }
00085 
00086 protected:
00087         struct bt_spline* impl;
00088         bool sat;
00089         double s_0, s_f;
00090 
00091 private:
00092         // TODO(dc): write a real copy constructor and assignment operator?
00093         DISALLOW_COPY_AND_ASSIGN(Spline);
00094 };
00095 
00096 
00097 // Specialization for Eigen::Quaternion<> types
00098 template<typename Scalar>
00099 class Spline<Eigen::Quaternion<Scalar> > {
00100 public:
00101         typedef Eigen::Quaternion<Scalar> data_type;
00102         typedef boost::tuple<double, data_type> tuple_type;
00103 
00104         template<template<typename, typename> class Container, typename Allocator>
00105         Spline(const Container<tuple_type, Allocator>& samples, bool saturateS = true);
00106 
00107         // initialDirection is ignored for quaternion types
00108         template<template<typename, typename> class Container, typename Allocator>
00109         Spline(const Container<data_type, Allocator>& points, bool saturateS = true);
00110 
00111         double initialS() const { return boost::get<0>(data.front()); }
00112         double finalS() const { return boost::get<0>(data.back()); }
00113         double changeInS() const { return finalS() - initialS(); }
00114 
00115         data_type eval(double s) const;
00116 
00117         typedef data_type result_type;  
00118         result_type operator() (double s) const {
00119                 return eval(s);
00120         }
00121 
00122 protected:
00123         std::vector<tuple_type, Eigen::aligned_allocator<tuple_type> > data;
00124         bool sat;
00125 
00126         mutable size_t index;
00127         mutable double rate;
00128 
00129 private:
00130         // TODO(dc): write a real copy constructor and assignment operator?
00131         DISALLOW_COPY_AND_ASSIGN(Spline);
00132 
00133 public:
00134         EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
00135 };
00136 
00137 
00138 // Specialization for boost::tuple<> types
00139 template <
00140         typename T0, typename T1, typename T2, typename T3, typename T4,
00141         typename T5, typename T6, typename T7, typename T8, typename T9>
00142 class Spline<boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> > {
00143 public:
00144         typedef boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> data_type;
00145         typedef boost::tuple<double, data_type> tuple_type;
00146 
00147         static const size_t TUPLE_LEN = boost::tuples::length<data_type>::value;
00148         typedef detail::TupleSplineHolder<TUPLE_LEN, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> holder_type;
00149 
00150         template<template<typename, typename> class Container, typename Allocator>
00151         Spline(const Container<tuple_type, Allocator>& samples, bool saturateS = true) :
00152                 holder(samples, saturateS) {}
00153 
00154         // initialDirection is ignored for boost::tuple types
00155         template<template<typename, typename> class Container, typename Allocator>
00156         Spline(const Container<data_type, Allocator>& points, bool saturateS = true) :
00157                 holder(points, saturateS) {}
00158 
00159         double initialS() const { return holder.initialS; }
00160         double finalS() const { return initialS() + changeInS(); }
00161         double changeInS() const { return holder.maxChangeInS; }
00162 
00163         data_type eval(double s) const {
00164                 holder.collectValues(s);
00165                 return holder.data;
00166         }
00167 
00168         typedef data_type result_type;  
00169         result_type operator() (double s) const {
00170                 return eval(s);
00171         }
00172 
00173 protected:
00174         holder_type holder;
00175 
00176 private:
00177         // TODO(dc): write a real copy constructor and assignment operator?
00178         DISALLOW_COPY_AND_ASSIGN(Spline);
00179 
00180 public:
00181         EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
00182 };
00183 
00184 
00185 }
00186 }
00187 
00188 
00189 // include template definitions
00190 #include <barrett/math/detail/spline-inl.h>
00191 
00192 
00193 #endif /* BARRETT_MATH_SPLINE_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Defines