- 3.0.2 core module.
DiscreteTrajectoryBase.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 
8 #include "TrajectoryBase.h"
9 #include "../arrays/TimeArray.h"
10 #include "../arrays/DiscreteArray.h"
11 
13 
14 
15 namespace ct {
16 namespace core {
17 
19 
30 template <class T, class Alloc = Eigen::aligned_allocator<T>, typename SCALAR = double>
31 class DiscreteTrajectoryBase : public TrajectoryBase<T, SCALAR>
32 {
33 public:
34  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
35 
39 
48  const DiscreteArray<T, Alloc>& data,
49  const InterpolationType& type = ZOH)
50  : time_(time), data_(data), interp_(type)
51  {
52  }
53 
55 
63  const SCALAR& deltaT,
64  const SCALAR& t0,
65  const InterpolationType& type = ZOH)
66  : time_(), data_(data), interp_(type)
67  {
68  time_ = tpl::TimeArray<SCALAR>(deltaT, data.size(), t0);
69  }
70 
73  : time_(other.time_), data_(other.data_), interp_(other.interp_.getInterpolationType())
74  {
75  }
76 
78 
85  const size_t startIndex,
86  const size_t endIndex)
87  : time_(), data_(), interp_(other.interp_.getInterpolationType())
88  {
89  tpl::TimeArray<SCALAR> time_temp;
90  DiscreteArray<T, Alloc> data_temp;
91 
92  for (size_t i = startIndex; i <= endIndex; i++)
93  {
94  time_temp.push_back(other.time_[i]);
95  data_temp.push_back(other.data_[i]);
96  }
97 
98  time_ = time_temp;
99  data_ = data_temp;
100  }
101 
104 
105 
107 
110  void setData(const DiscreteArray<T, Alloc>& data) { data_ = data; }
112 
117 
120  void setTime(const tpl::TimeArray<SCALAR>& time) { time_ = time; }
122 
127  void shiftTime(const SCALAR& dt) { time_.addOffset(-dt); }
129 
135  virtual T eval(const SCALAR& evalTime) override
136  {
137  T result;
138  interp_.interpolate(time_, data_, evalTime, result);
139  return result;
140  }
141 
143 
146  size_t size() const
147  {
148  if (data_.size() != time_.size())
149  throw std::runtime_error("DiscreteTrajectoryBase: size inconsistent.");
150 
151  return this->data_.size();
152  }
153 
155  T& front() { return data_.front(); }
157  const T& front() const { return data_.front(); }
159  T& back() { return data_.back(); }
161  const T& back() const { return data_.back(); }
163  T& operator[](const size_t i) { return data_[i]; }
165  const T& operator[](const size_t i) const { return data_[i]; }
167  const SCALAR startTime() const { return time_.front(); }
169  const SCALAR finalTime() const { return time_.back(); }
171  const SCALAR duration() const { return time_.back() - time_.front(); }
173 
181  void push_back(const T& data, const SCALAR& time, const bool timeIsAbsolute)
182  {
183  if (timeIsAbsolute)
184  time_.push_back(time);
185  else
186  time_.push_back(time + time_.back());
187 
188  data_.push_back(data);
189  }
190 
192  void pop_back()
193  {
194  time_.pop_back();
195  data_.pop_back();
196  }
197 
199 
203  void eraseFront(const size_t& N, const SCALAR& dt = 0.0)
204  {
205  time_.eraseFront(N);
206  data_.eraseFront(N);
207  shiftTime(dt);
208  }
209 
211  void clear()
212  {
213  data_.clear();
214  time_.clear();
215  }
216 
218 
224  {
225  time_.swap(other.time_);
226  data_.swap(other.data_);
227  }
228 
231  {
232  if (this == &other)
233  return *this;
234 
235  time_ = other.time_;
236  data_ = other.data_;
238 
239  return *this;
240  }
241 
243  const SCALAR& getTimeFromIndex(const size_t& ind) const { return time_[ind]; }
245 
250  size_t getIndexFromTime(const SCALAR& t) { return interp_.findIndex(time_, t); }
254  const DiscreteArray<T, Alloc>& getDataArray() const { return data_; }
258  const tpl::TimeArray<SCALAR>& getTimeArray() const { return time_; }
260 
263  void print()
264  {
265  assert(time_.size() == data_.size());
266  for (size_t i = 0; i < time_.size(); i++)
267  {
268  std::cout << "time: \t " << time_[i] << std::endl << "data-point: \t" << data_[i] << std::endl;
269  }
270  }
271 
272 protected:
274 
276 
278 };
279 
280 } /* namespace core */
281 } /* namespace ct */
T & operator[](const size_t i)
access a certain index of the trajectory (does not interpolate)
Definition: DiscreteTrajectoryBase.h:163
void clear()
Clear the trajectory.
Definition: DiscreteTrajectoryBase.h:211
void pop_back()
Remove the last data and time pair.
Definition: DiscreteTrajectoryBase.h:192
An discrete array (vector) of a particular data type.
Definition: DiscreteArray.h:22
const T & operator[](const size_t i) const
access a certain index of the trajectory (does not interpolate)
Definition: DiscreteTrajectoryBase.h:165
T & back()
get the last element
Definition: DiscreteTrajectoryBase.h:159
int findIndex(const tpl::TimeArray< SCALAR > &timeArray, const SCALAR &enquiryTime)
find an index corresponding to a certain inquiry time
Definition: Interpolation.h:119
DiscreteTrajectoryBase(const DiscreteTrajectoryBase< T, Alloc > &other)
copy constructor
Definition: DiscreteTrajectoryBase.h:72
void shiftTime(const SCALAR &dt)
shift the trajectory forward in time
Definition: DiscreteTrajectoryBase.h:127
void setData(const DiscreteArray< T, Alloc > &data)
set the data array
Definition: DiscreteTrajectoryBase.h:110
DiscreteTrajectoryBase(DiscreteTrajectoryBase< T, Alloc, SCALAR > &other, const size_t startIndex, const size_t endIndex)
extraction constructor
Definition: DiscreteTrajectoryBase.h:84
const DiscreteArray< T, Alloc > & getDataArray() const
get the data array
Definition: DiscreteTrajectoryBase.h:254
tpl::TimeArray< SCALAR > time_
time array
Definition: DiscreteTrajectoryBase.h:273
size_t size() const
returns the size of the trajectory
Definition: DiscreteTrajectoryBase.h:146
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
const tpl::TimeArray< SCALAR > & getTimeArray() const
get the time array
Definition: DiscreteTrajectoryBase.h:258
const T & front() const
get the first element
Definition: DiscreteTrajectoryBase.h:157
const double dt
void setInterpolationType(const InterpolationType &type)
set the interpolation strategy
Definition: DiscreteTrajectoryBase.h:115
virtual ~DiscreteTrajectoryBase()
Destructor.
Definition: DiscreteTrajectoryBase.h:103
void swapData(DiscreteTrajectoryBase &other)
Swap two trajectories.
Definition: DiscreteTrajectoryBase.h:223
CppAD::AD< CppAD::cg::CG< double > > SCALAR
void print()
print out the trajectory
Definition: DiscreteTrajectoryBase.h:263
InterpolationType
Definition: Interpolation.h:14
A discrete, timed trajectory with interpolation.
Definition: DiscreteTrajectoryBase.h:31
DiscreteArray< T, Alloc > data_
data array
Definition: DiscreteTrajectoryBase.h:275
for i
const SCALAR duration() const
time duration of the trajectory
Definition: DiscreteTrajectoryBase.h:171
DiscreteTrajectoryBase(const DiscreteArray< T, Alloc > &data, const SCALAR &deltaT, const SCALAR &t0, const InterpolationType &type=ZOH)
constructor for uniformly spaced trajectories
Definition: DiscreteTrajectoryBase.h:62
const T & back() const
get the last element
Definition: DiscreteTrajectoryBase.h:161
InterpolationType getInterpolationType() const
get the employed interpolation type
Definition: Interpolation.h:115
DiscreteArray< T, Alloc > & getDataArray()
get the data array
Definition: DiscreteTrajectoryBase.h:252
const SCALAR & getTimeFromIndex(const size_t &ind) const
get the time stamp at a certain index
Definition: DiscreteTrajectoryBase.h:243
An array in time.
Definition: TimeArray.h:22
virtual T eval(const SCALAR &evalTime) override
evaluate the trajectory at a certain time
Definition: DiscreteTrajectoryBase.h:135
void eraseFront(const size_t &N, const SCALAR &dt=0.0)
Erase front elements and optionally shift the trajectory in time.
Definition: DiscreteTrajectoryBase.h:203
tpl::TimeArray< SCALAR > & getTimeArray()
get the time array
Definition: DiscreteTrajectoryBase.h:256
void push_back(const T &data, const SCALAR &time, const bool timeIsAbsolute)
Add a data and time point at the end.
Definition: DiscreteTrajectoryBase.h:181
T & front()
get the first element
Definition: DiscreteTrajectoryBase.h:155
Zero-Order hold.
Definition: Interpolation.h:16
EIGEN_MAKE_ALIGNED_OPERATOR_NEW DiscreteTrajectoryBase(const InterpolationType &type=ZOH)
default constructor
Definition: DiscreteTrajectoryBase.h:37
DiscreteTrajectoryBase & operator=(const DiscreteTrajectoryBase &other)
assignment operator
Definition: DiscreteTrajectoryBase.h:230
void setTime(const tpl::TimeArray< SCALAR > &time)
set timestamps
Definition: DiscreteTrajectoryBase.h:120
size_t getIndexFromTime(const SCALAR &t)
get the index associated with a certain time
Definition: DiscreteTrajectoryBase.h:250
void changeInterpolationType(const InterpolationType &type)
change the interpolation type
Definition: Interpolation.h:117
const SCALAR finalTime() const
get the time stamp of the last element
Definition: DiscreteTrajectoryBase.h:169
Basic interface class for a trajectory.
Definition: TrajectoryBase.h:22
Interpolation< T, Alloc, SCALAR > interp_
interpolation strategy
Definition: DiscreteTrajectoryBase.h:277
DiscreteTrajectoryBase(const tpl::TimeArray< SCALAR > &time, const DiscreteArray< T, Alloc > &data, const InterpolationType &type=ZOH)
constructor
Definition: DiscreteTrajectoryBase.h:47
const SCALAR startTime() const
get the time stamp of the first element
Definition: DiscreteTrajectoryBase.h:167