spline.cc
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright (c) 2018, Alexander W. Winkler. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6 
7 * Redistributions of source code must retain the above copyright notice, this
8  list of conditions and the following disclaimer.
9 
10 * Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 * Neither the name of the copyright holder nor the names of its
15  contributors may be used to endorse or promote products derived from
16  this software without specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ******************************************************************************/
29 
30 #include <towr/variables/spline.h>
31 
32 #include <numeric> // std::accumulate
33 
34 namespace towr {
35 
36 Spline::Spline(const VecTimes& poly_durations, int n_dim)
37 {
38  uint n_polys = poly_durations.size();
39 
40  cubic_polys_.assign(n_polys, CubicHermitePolynomial(n_dim));
41  for (int i=0; i<cubic_polys_.size(); ++i) {
42  cubic_polys_.at(i).SetDuration(poly_durations.at(i));
43  }
44 
46 }
47 
48 int
49 Spline::GetSegmentID(double t_global, const VecTimes& durations)
50 {
51  double eps = 1e-10; // double precision
52  assert(t_global >= 0.0);
53 
54  double t = 0;
55  int i=0;
56  for (double d: durations) {
57  t += d;
58 
59  if (t >= t_global-eps) // at junctions, returns previous spline (=)
60  return i;
61 
62  i++;
63  }
64 
65  assert(false); // this should never be reached
66 }
67 
68 std::pair<int,double>
69 Spline::GetLocalTime (double t_global, const VecTimes& durations) const
70 {
71  int id = GetSegmentID(t_global, durations);
72 
73  double t_local = t_global;
74  for (int i=0; i<id; i++)
75  t_local -= durations.at(i);
76 
77  return std::make_pair(id, t_local);
78 }
79 
80 const State
81 Spline::GetPoint(double t_global) const
82 {
83  int id; double t_local;
84  std::tie(id, t_local) = GetLocalTime(t_global, GetPolyDurations());
85 
86  return GetPoint(id, t_local);
87 }
88 
89 const State
90 Spline::GetPoint(int poly_id, double t_local) const
91 {
92  return cubic_polys_.at(poly_id).GetPoint(t_local);
93 }
94 
95 void
97 {
98  for (auto& p : cubic_polys_)
99  p.UpdateCoeff();
100 }
101 
102 int
104 {
105  return cubic_polys_.size();
106 }
107 
110 {
111  VecTimes poly_durations;
112  for (const auto& p : cubic_polys_)
113  poly_durations.push_back(p.GetDuration());
114 
115  return poly_durations;
116 }
117 
118 double
120 {
121  auto v = GetPolyDurations();
122  return std::accumulate(v.begin(), v.end(), 0.0);
123 }
124 
125 } /* namespace towr */
126 
VecTimes GetPolyDurations() const
Definition: spline.cc:109
Stores at state comprised of values and higher-order derivatives.
Definition: state.h:49
std::vector< double > VecTimes
Definition: spline.h:47
std::pair< int, double > GetLocalTime(double t_global, const VecTimes &d) const
How much time of the current segment has passed at t_global.
Definition: spline.cc:69
int GetPolynomialCount() const
Definition: spline.cc:103
VecPoly cubic_polys_
the sequence of polynomials making up the spline.
Definition: spline.h:89
void UpdatePolynomialCoeff()
Updates the cubic-Hermite polynomial coefficients using the currently set nodes values and durations...
Definition: spline.cc:96
static int GetSegmentID(double t_global, const VecTimes &durations)
Definition: spline.cc:49
double GetTotalTime() const
Definition: spline.cc:119
const State GetPoint(double t) const
Definition: spline.cc:81
Represents a Cubic-Hermite-Polynomial.
Definition: polynomial.h:109
Spline(const VecTimes &poly_durations, int n_dim)
Definition: spline.cc:36


towr
Author(s): Alexander W. Winkler
autogenerated on Mon Feb 28 2022 23:54:22