humoto
cubic_polynomial.h
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4  @author Jan Michalczyk
5  @copyright 2014-2017 INRIA. Licensed under the Apache License, Version 2.0.
6  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
7 
8  @brief
9 */
10 
11 #pragma once
12 
13 
14 namespace humoto
15 {
16  namespace rigidbody
17  {
18  /**
19  * @brief Type of interpolated trajectory
20  */
22  {
23  public:
24  enum Type
25  {
26  UNDEFINED = 0,
27  POSITION = 1,
28  VELOCITY = 2,
30  };
31  };
32 
33 
34  /**
35  * @brief Class interpolating simple 1D cubic polynomial
36  * between two 1D points.
37  * Use this class to build more complex trajectories.
38  *
39  * f(t) = a0 + a1 t + a2 t^2 + a3 t^3
40  */
42  {
43  public:
44  /**
45  * @brief Constructor.
46  */
48  {
49  initialize(0, 0);
50  }
51 
52 
53  /**
54  * @brief Builds cubic polynomial in t = [0, 1]
55  * with first derivative equal to 0 at both ends
56  *
57  * @param[in] a initial position
58  * @param[in] b final position
59  */
60  void initialize(const double a, const double b)
61  {
62  a0_ = a;
63  a1_ = 0.0;
64  a2_ = 3.0*(b - a);
65  a3_ = -2.0*(b - a);
66  }
67 
68 
69  /**
70  * @brief Builds cubic polynomial in t = [0, 1]
71  * with first derivative not equal to 0 at ends
72  *
73  * @param[in] a initial 1D position
74  * @param[in] adot initial 1D velocity
75  * @param[in] b final 1D position
76  * @param[in] bdot final 1D velocity
77  */
78  void initialize(const double a,
79  const double adot,
80  const double b,
81  const double bdot)
82  {
83  a0_ = a;
84  a1_ = adot;
85  a2_ = 3.0*(b - a) - 2.0*(adot - bdot);
86  a3_ = -2.0*(b - a) + 1.0*(bdot - adot);
87  }
88 
89 
90  /**
91  * @brief Computes position at the given time point.
92  *
93  * @param[in] t time
94  *
95  * @return position at the given time point
96  */
97  double getPosition(const double t) const
98  {
99  HUMOTO_ASSERT((t >= 0.0) && (t <= 1.0), "time value not between 0 and 1." )
100  return (a0_ + a1_*t + a2_*t*t + a3_*t*t*t);
101  }
102 
103 
104  /**
105  * @brief Computes velocity at the given time point.
106  *
107  * @param[in] t time
108  *
109  * @return velocity at the given time point
110  */
111  double getVelocity(const double t) const
112  {
113  HUMOTO_ASSERT((t >= 0.0) && (t <= 1.0), "time value not between 0 and 1." )
114  return (a1_ + 2.0*a2_*t + 3.0*a3_*t*t);
115  }
116 
117 
118  /**
119  * @brief Computes acceleration at the given time point.
120  *
121  * @param[in] t time
122  *
123  * @return acceleration at the given time point
124  */
125  double getAcceleration(const double t) const
126  {
127  HUMOTO_ASSERT((t >= 0.0) && (t <= 1.0), "time value not between 0 and 1." )
128  return (2.0*a2_ + 6.0*a3_*t);
129  }
130 
131 
132  /**
133  * @brief Computes jerk.
134  *
135  * @return jerk.
136  */
137  double getJerk() const
138  {
139  return (6.0*a3_);
140  }
141 
142 
143  /**
144  * @brief Evaluate the spline at a point.
145  *
146  * @param[in] t time point
147  * @param[in] trajectory_type type of trajectory
148  *
149  * @return evaluated sample
150  */
151  double eval(const double t,
152  const TrajectoryEvaluationType::Type trajectory_type) const
153  {
154  switch(trajectory_type)
155  {
157  return getPosition(t);
159  return getVelocity(t);
161  return getAcceleration(t);
162  default:
163  HUMOTO_THROW_MSG("Unsupported trajectory type.");
164  }
165  }
166 
167 
168  /**
169  * @brief Evaluate the polynomial at time_interval points.
170  *
171  * @param[in] time_points eigen vector with time instants at which we evaluate
172  * @param[in] trajectory_type type of trajectory
173  *
174  * @return std vector with evaluated samples
175  */
176  std::vector<double> eval( const Eigen::VectorXd& time_points,
177  const TrajectoryEvaluationType::Type trajectory_type) const
178  {
179  std::vector<double> result(time_points.size());
180 
181  for(std::size_t i = 0; i < result.size(); ++i)
182  {
183  result[i] = eval(time_points(i), trajectory_type);
184  }
185 
186  return result;
187  }
188 
189 
190  /**
191  * @brief Scale vector.
192  *
193  * @param[in] time_points eigen vector
194  *
195  * @return eigen vector of scaled values
196  */
197  static Eigen::VectorXd scale(const Eigen::VectorXd& time_points)
198  {
199  Eigen::VectorXd result(time_points.size());
200 
201  for(EigenIndex i = 0; i < result.size(); ++i)
202  {
203  result[i] = (time_points(i) - time_points.minCoeff()) / (time_points.maxCoeff() - time_points.minCoeff());
204  }
205 
206  return result;
207  }
208 
209 
210  private:
211  double a0_;
212  double a1_;
213  double a2_;
214  double a3_;
215  };
216  }
217 }
void initialize(const double a, const double b)
Builds cubic polynomial in t = [0, 1] with first derivative equal to 0 at both ends.
double getVelocity(const double t) const
Computes velocity at the given time point.
#define HUMOTO_LOCAL
Definition: export_import.h:26
std::vector< double > eval(const Eigen::VectorXd &time_points, const TrajectoryEvaluationType::Type trajectory_type) const
Evaluate the polynomial at time_interval points.
static Eigen::VectorXd scale(const Eigen::VectorXd &time_points)
Scale vector.
#define HUMOTO_ASSERT(condition, message)
double getJerk() const
Computes jerk.
Class interpolating simple 1D cubic polynomial between two 1D points. Use this class to build more co...
#define HUMOTO_THROW_MSG(s)
HUMOTO_THROW_MSG throws an error message concatenated with the name of the function (if supported)...
EIGEN_DEFAULT_DENSE_INDEX_TYPE EigenIndex
Definition: utility.h:26
double getPosition(const double t) const
Computes position at the given time point.
void initialize(const double a, const double adot, const double b, const double bdot)
Builds cubic polynomial in t = [0, 1] with first derivative not equal to 0 at ends.
double eval(const double t, const TrajectoryEvaluationType::Type trajectory_type) const
Evaluate the spline at a point.
The root namespace of HuMoTo.
Definition: config.h:12
double getAcceleration(const double t) const
Computes acceleration at the given time point.
Type of interpolated trajectory.