20 class SampledTraj :
public TrajBase<T>,
public std::map<double,T>
25 : TrajBase<T>(), std::map<double,T>()
28 SampledTraj(
const std::list<double>& l_t,
const std::list<T>& l_x)
31 assert_release(l_t.size() == l_x.size());
32 auto it_t = l_t.begin();
auto it_x = l_x.begin();
33 while(it_t != l_t.end())
35 this->set(*it_t,*it_x);
40 SampledTraj(
const std::map<double,T>& m)
41 : TrajBase<T>(), std::map<double,T>(m)
45 virtual Index size()
const
47 if constexpr(std::is_same_v<typename ValueType<T>::Type,ScalarType>)
55 return this->begin()->second.size();
59 virtual std::pair<Index,Index> shape()
const
61 if constexpr(std::is_same_v<typename ValueType<T>::Type,ScalarType>)
68 return {this->begin()->second.rows(),this->begin()->second.cols()};
72 size_t nb_samples()
const
74 return std::map<double,T>::size();
77 virtual bool is_empty()
const
79 return std::map<double,T>::empty();
82 virtual Interval tdomain()
const
87 return { this->begin()->first, this->rbegin()->first };
90 virtual void truncate_tdomain(
const Interval& new_tdomain)
92 assert_release(this->tdomain().is_superset(new_tdomain));
95 T y_lb = (*this)(new_tdomain.lb());
96 T y_ub = (*this)(new_tdomain.ub());
98 auto it = this->begin();
99 while(it != this->end())
101 if(!new_tdomain.contains(it->first))
102 it = this->erase(it);
107 this->set(new_tdomain.lb(), y_lb);
108 this->set(new_tdomain.ub(), y_ub);
111 virtual typename Wrapper<T>::Domain codomain()
const
113 typename Wrapper<T>::Domain hull(this->begin()->second);
114 for(
const auto& [t,v] : *
this)
119 virtual T operator()(
double t)
const
121 if(!this->tdomain().contains(t))
122 return this->nan_value();
124 auto it_lower = this->lower_bound(t);
125 if(it_lower->first == t)
126 return it_lower->second;
128 auto it_upper = it_lower;
132 return it_lower->second +
133 (t - it_lower->first) * (it_upper->second - it_lower->second) /
134 (it_upper->first - it_lower->first);
137 virtual typename Wrapper<T>::Domain operator()(
const Interval& t)
const
140 typename Wrapper<T>::Domain hull(this->begin()->second);
142 if(!this->tdomain().is_superset(t))
143 return hull.init(Interval(-oo,oo));
147 hull = (*this)(t.lb());
148 for(
auto it = this->lower_bound(t.lb()) ; it != this->upper_bound(t.ub()) ; it++)
150 hull |= (*this)(t.ub());
155 void set(
double t,
const T& x)
157 assert(this->empty() || size_of(x) == this->size());
158 std::map<double,T>::operator[](t) = x;
161 virtual SampledTraj<T> sampled(
double dt)
const
163 return sampled(dt,
true);
166 SampledTraj<T> sampled(
double dt,
bool keep_original_values)
const
171 auto straj = TrajBase<T>::sampled(dt);
173 if(keep_original_values)
176 for(
const auto& [ti,xi] : *
this)
184 SampledTraj<T> sampled_as(
const SampledTraj<Q>& x)
const
186 return TrajBase<T>::sampled_as(x);
190 SampledTraj<T> sampled_as(
const SampledTraj<Q>& x,
bool keep_original_values)
const
192 SampledTraj<T> straj = TrajBase<T>::sampled_as(x);
193 if(keep_original_values)
194 for(
const auto& [ti,xi] : *
this)
199 SampledTraj<T>& shift_tdomain(
double shift)
201 std::map<double,T> save = *
this;
203 for(
const auto& [ti,xi] : save)
204 this->std::map<double,T>::operator[](ti+shift) = xi;
208 SampledTraj<T>& stretch_tdomain(
const Interval& tdomain)
210 Interval a = this->tdomain(), b = tdomain;
211 std::map<double,T> save = *
this;
213 for(
const auto& [ti,xi] : save)
214 this->std::map<double,T>::operator[]([&]() {
218 return ((ti-a.lb())*b.diam()/a.diam())+b.lb();
221 assert(this->tdomain() == tdomain);
225 template<
typename T_=T>
226 requires std::is_same_v<T_,Vector>
227 SampledTraj<double> operator[](Index i)
const
229 assert_release(i >= 0 && i < size());
230 std::map<double,double> m;
231 for(
const auto& [t,y] : *
this)
233 assert(i < y.size());
240 template<
typename T_=T>
241 requires std::is_same_v<T_,Vector>
242 SampledTraj<Vector> subvector(Index i, Index j)
const
244 assert_release(i >= 0 && i <= j && j < size());
245 std::map<double,Vector> m;
246 for(
const auto& [t,y] : *
this)
248 assert(j < y.size());
249 m[t] = y.subvector(i,j);
257 inline std::ostream&
operator<<(std::ostream& os,
const SampledTraj<T>& x)
259 os <<
"SampledTraj. " << x.tdomain() <<
"↦" << x.codomain() <<
", " << x.nb_samples() <<
" pts";
263 inline SampledTraj<double> continuous_traj(
const SampledTraj<double>& x)
265 SampledTraj<double> x_continuous;
266 const Interval periodicity = x.codomain();
268 double prev_xi = 0., value_mod = 0.;
270 for(
const auto& [ti,xi] : x)
272 if(!x_continuous.empty())
274 if(prev_xi - xi > periodicity.diam()*0.9)
275 value_mod += periodicity.diam();
276 else if(prev_xi - xi < -periodicity.diam()*0.9)
277 value_mod -= periodicity.diam();
281 x_continuous.set(ti, xi+value_mod);
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:62
static Interval empty()
Provides an empty interval.
Definition codac2_Interval_impl.h:535
std::ostream & operator<<(std::ostream &os, const BoolInterval &x)
Streams out a BoolInterval.
Definition codac2_BoolInterval.h:45