Corentin Chauvin-Hameau – 2019-2020
Coverage Path Planning for an underwater robot surveying a marine farm
spline.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  *
4  * \brief Declaration of a class for 3D spline interpolation
5  * \author Corentin Chauvin-Hameau
6  * \date 2020
7  */
8 
9 #ifndef SPLINE_HPP
10 #define SPLINE_HPP
11 
12 #include <eigen3/Eigen/Dense>
13 #include <vector>
14 
15 namespace mfcpp {
16 
17 /**
18  * \brief Class for spline interpolation
19  *
20  * Interpolates a list of given positions and orientations by cubic splines at
21  * constant speed. The evaluation function is optimised for successive calls
22  * with increasing time.
23  */
24 class Spline {
25  public:
26  /// \brief Default constructor
27  Spline();
28 
29  /**
30  * \brief Constructor for a spline given by a list of poses
31  *
32  * Will call `Spline::prepare()`.
33  *
34  * \param positions Positions to interpolate
35  * \param orientations Corresponding orientations
36  * \param speed Desired constant speed
37  */
38  Spline(
39  const std::vector<Eigen::Vector3f> &positions,
40  const std::vector<Eigen::Vector3f> &orientations,
41  float speed
42  );
43 
44  /**
45  * \brief Sets the poses to interpolate
46  *
47  * \param positions Positions to interpolate
48  * \param orientations Corresponding orientations
49  */
50  void set_poses(
51  const std::vector<Eigen::Vector3f> &positions,
52  const std::vector<Eigen::Vector3f> &orientations
53  );
54 
55  /**
56  * \brief Set the desired speed on the path
57  *
58  * \param speed Desired speed
59  */
60  void set_speed(float speed);
61 
62  /**
63  * \brief Evaluates the spline at a specific time instant (assuming constant speed)
64  *
65  * If t is larger than the time t_max when the last specified pose is reached,
66  * the output pose will be this last pose.
67  *
68  * \param[in] t Time instant (should be positive)
69  * \param[out] position Interpolated position
70  * \param[out] orientation Interpolated orientation
71  * \param[out] last_reached Whether the last pose has been reached
72  */
73  void evaluate(
74  float t,
75  Eigen::Vector3f &position,
76  Eigen::Vector3f &orientation,
77  bool &last_reached
78  );
79 
80 
81  private:
82  int n_; ///< Number of poses to interpolate
83  float speed_; ///< Constant speed to adopt on the path
84  std::vector<Eigen::Vector3f> p_; ///< Positions to interpolate
85  std::vector<Eigen::Vector3f> o_; ///< Corresponding orientations
86  std::vector< std::vector<Eigen::Vector3f> > a_; ///< Spline parameters for each segment (ie between each pose)
87  bool prepared_; ///< Whether the class is ready for interpolation
88  float last_s_; ///< Curvilinear abscissa of the last evaluated point
89  float last_t_; ///< Time instant of the last evaluated point
90 
91  /**
92  * \brief Computes the spline parameters
93  */
94  void compute_parameters();
95 
96  /**
97  * \brief Prepares the class for interpolation
98  *
99  * Computes the spline parameters and the time instants at the given points.
100  */
101  void prepare();
102 
103  /**
104  * \brief Evaluates the spline at a specific curvilinear abscissa
105  *
106  * \param s Curvilinear abscissa (in [0, n])
107  * \return Interpolated position
108  */
109  Eigen::Vector3f evaluate_position(float s);
110 
111  /**
112  * \brief Computes the orientation at a specific curvilinear abscissa
113  *
114  * \param s Curvilinear abscissa (in [0, n])
115  * \return Interpolated orientation
116  */
117  Eigen::Vector3f compute_orientation(float s);
118 
119  /**
120  * \brief Computes the derivative of the curvilinear abscissa wrt time
121  *
122  * This is the ODE used to compute the abscissa corresponding to at time.
123  *
124  * \param[in] Abscissa at which to compute the derivative (vector of size 1)
125  * \param[out] Derivative of the abscissa (vector of size 1)
126  * \param[in] Time parameter (not used here since no time dependency)
127  */
128  void deriv_abscissa(
129  const std::vector<double> &s,
130  std::vector<double> &dsdt,
131  const double t
132  );
133 
134  /**
135  * \brief Computes the curvilinear abscissa corresponding to a time instant
136  *
137  * The function is optimised for successive calls with an increasing s.
138  *
139  * \param t Time at which to interpolate (should be positive)
140  * \return Corresponding curvilinear abscissa
141  */
142  float compute_abscissa(float t);
143 };
144 
145 
146 } // namespace mfcpp
147 
148 #endif
Definition: common.hpp:23
float last_t_
Time instant of the last evaluated point.
Definition: spline.hpp:89
float compute_abscissa(float t)
Computes the curvilinear abscissa corresponding to a time instant.
Definition: spline.cpp:150
void compute_parameters()
Computes the spline parameters.
Definition: spline.cpp:101
float speed_
Constant speed to adopt on the path.
Definition: spline.hpp:83
std::vector< Eigen::Vector3f > o_
Corresponding orientations.
Definition: spline.hpp:85
void evaluate(float t, Eigen::Vector3f &position, Eigen::Vector3f &orientation, bool &last_reached)
Evaluates the spline at a specific time instant (assuming constant speed)
Definition: spline.cpp:63
std::vector< Eigen::Vector3f > p_
Positions to interpolate.
Definition: spline.hpp:84
Eigen::Vector3f compute_orientation(float s)
Computes the orientation at a specific curvilinear abscissa.
Definition: spline.cpp:127
void set_poses(const std::vector< Eigen::Vector3f > &positions, const std::vector< Eigen::Vector3f > &orientations)
Sets the poses to interpolate.
Definition: spline.cpp:40
int n_
Number of poses to interpolate.
Definition: spline.hpp:82
std::vector< std::vector< Eigen::Vector3f > > a_
Spline parameters for each segment (ie between each pose)
Definition: spline.hpp:86
float last_s_
Curvilinear abscissa of the last evaluated point.
Definition: spline.hpp:88
void deriv_abscissa(const std::vector< double > &s, std::vector< double > &dsdt, const double t)
Computes the derivative of the curvilinear abscissa wrt time.
Definition: spline.cpp:141
void set_speed(float speed)
Set the desired speed on the path.
Definition: spline.cpp:56
bool prepared_
Whether the class is ready for interpolation.
Definition: spline.hpp:87
Eigen::Vector3f evaluate_position(float s)
Evaluates the spline at a specific curvilinear abscissa.
Definition: spline.cpp:114
Spline()
Default constructor.
Definition: spline.cpp:22
void prepare()
Prepares the class for interpolation.
Definition: spline.cpp:90
Class for spline interpolation.
Definition: spline.hpp:24