- 3.0.2 core module.
Interpolation.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
10 
11 namespace ct {
12 namespace core {
13 
15 {
16  ZOH = 0,
17  LIN
18 };
19 
21 
28 template <typename Data_T, class Alloc_Data = Eigen::aligned_allocator<Data_T>, typename SCALAR = double>
30 {
31 public:
32  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
33 
35 
37 
43  Interpolation(const InterpolationType& type = LIN) : index_(0), type_(type) {}
45  Interpolation(const Interpolation& arg) : index_(arg.index_), type_(arg.type_) {}
47 
54  void interpolate(const tpl::TimeArray<SCALAR>& timeArray,
55  const DiscreteArray_t& dataArray,
56  const SCALAR& enquiryTime,
57  Data_T& enquiryData,
58  int greatestLessTimeStampIndex = -1)
59  {
60  if (timeArray.size() == 0)
61  throw std::runtime_error("Interpolation.h : TimeArray is size 0.");
62 
63  if (dataArray.size() == 0)
64  throw std::runtime_error("Interpolation.h : DataArray is size 0.");
65 
66  if (timeArray.size() != dataArray.size())
67  throw std::runtime_error("Interpolation.h : The size of timeStamp vector (=" +
68  std::to_string(timeArray.size()) + ") is not equal to the size of data vector (=" +
69  std::to_string(dataArray.size()) + ").");
70 
71 
72  // treat special case of trajectory length equal 1
73  if (dataArray.size() == 1)
74  {
75  enquiryData = dataArray.front();
76  return;
77  }
78 
79  int ind;
80  if (greatestLessTimeStampIndex == -1)
81  ind = findIndex(timeArray, enquiryTime);
82  else
83  {
84  ind = greatestLessTimeStampIndex;
85  index_ = greatestLessTimeStampIndex;
86  }
87 
88  if (enquiryTime < timeArray.front())
89  {
90  enquiryData = dataArray.front();
91  return;
92  }
93 
94  if (ind == (int)timeArray.size() - 1)
95  {
96  enquiryData = dataArray.back();
97  return;
98  }
99 
100  SCALAR alpha = (enquiryTime - timeArray.at(ind + 1)) / (timeArray.at(ind) - timeArray.at(ind + 1));
101 
102 
104  enquiryData = alpha * dataArray.at(ind) + (1 - alpha) * dataArray.at(ind + 1);
105  else if (type_ == InterpolationType::ZOH)
106  enquiryData = dataArray.at(ind);
107  else
108  throw std::runtime_error("Unknown Interpolation type!");
109  }
110 
111 
117  void changeInterpolationType(const InterpolationType& type) { type_ = type; }
119  int findIndex(const tpl::TimeArray<SCALAR>& timeArray, const SCALAR& enquiryTime)
120  {
121  int index = -1;
122 
123  index_ = std::min(index_, (int)timeArray.size() - 1);
124 
125  if (timeArray.at(index_) > enquiryTime)
126  {
127  for (int i = index_; i >= 0; i--)
128  {
129  index = i;
130  if (timeArray.at(i) <= enquiryTime)
131  break;
132  }
133  }
134  else
135  {
136  for (int i = index_; i < (int)timeArray.size(); i++)
137  {
138  index = i;
139  if (timeArray.at(i) > enquiryTime)
140  {
141  index = i - 1;
142  break;
143  }
144  }
145  }
146 
147  // throw error if index is wrong
148  if (index < 0)
149  throw std::runtime_error(
150  "Interpolation.h : index in protected member findIndex((const SCALAR& enquiryTime) not computed "
151  "properly");
152 
153  index_ = index;
154 
155  return index;
156  }
157 
158 
159 protected:
160  int index_;
161 
163 };
164 
165 
166 } // namespace ct
167 } // namespace core
Interpolation(const InterpolationType &type=LIN)
Default constructor.
Definition: Interpolation.h:43
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef DiscreteArray< Data_T, Alloc_Data > DiscreteArray_t
Definition: Interpolation.h:34
int findIndex(const tpl::TimeArray< SCALAR > &timeArray, const SCALAR &enquiryTime)
find an index corresponding to a certain inquiry time
Definition: Interpolation.h:119
void interpolate(const tpl::TimeArray< SCALAR > &timeArray, const DiscreteArray_t &dataArray, const SCALAR &enquiryTime, Data_T &enquiryData, int greatestLessTimeStampIndex=-1)
This method performs the interpolation.
Definition: Interpolation.h:54
Interpolation(const Interpolation &arg)
Copy Constructor.
Definition: Interpolation.h:45
int index_
Definition: Interpolation.h:160
CppAD::AD< CppAD::cg::CG< double > > SCALAR
InterpolationType
Definition: Interpolation.h:14
for i
InterpolationType getInterpolationType() const
get the employed interpolation type
Definition: Interpolation.h:115
Class that performs interpolation of data in time.
Definition: Interpolation.h:29
An array in time.
Definition: TimeArray.h:22
LIN.
Definition: Interpolation.h:17
InterpolationType type_
Definition: Interpolation.h:162
int getGreatestLessTimeStampIndex()
access the greatest index which is smaller than the inquired interpolation time
Definition: Interpolation.h:113
Zero-Order hold.
Definition: Interpolation.h:16
void changeInterpolationType(const InterpolationType &type)
change the interpolation type
Definition: Interpolation.h:117