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 size_t nb_samples()
const
61 return std::map<double,T>::size();
64 virtual bool is_empty()
const
66 return std::map<double,T>::empty();
69 virtual Interval tdomain()
const
74 return { this->begin()->first, this->rbegin()->first };
77 virtual void truncate_tdomain(
const Interval& new_tdomain)
79 assert_release(this->tdomain().is_superset(new_tdomain));
82 T y_lb = (*this)(new_tdomain.lb());
83 T y_ub = (*this)(new_tdomain.ub());
85 auto it = this->begin();
86 while(it != this->end())
88 if(!new_tdomain.contains(it->first))
94 this->set(new_tdomain.lb(), y_lb);
95 this->set(new_tdomain.ub(), y_ub);
98 virtual typename Wrapper<T>::Domain codomain()
const
100 typename Wrapper<T>::Domain hull(this->begin()->second);
101 for(
const auto& [t,v] : *
this)
106 virtual T operator()(
double t)
const
108 if(!this->tdomain().contains(t))
109 return this->nan_value();
111 auto it_lower = this->lower_bound(t);
112 if(it_lower->first == t)
113 return it_lower->second;
115 auto it_upper = it_lower;
119 return it_lower->second +
120 (t - it_lower->first) * (it_upper->second - it_lower->second) /
121 (it_upper->first - it_lower->first);
124 virtual typename Wrapper<T>::Domain operator()(
const Interval& t)
const
127 typename Wrapper<T>::Domain hull(this->begin()->second);
129 if(!this->tdomain().is_superset(t))
130 return hull.init(Interval(-oo,oo));
134 hull = (*this)(t.lb());
135 for(
auto it = this->lower_bound(t.lb()) ; it != this->upper_bound(t.ub()) ; it++)
137 hull |= (*this)(t.ub());
142 void set(
double t,
const T& x)
144 assert(this->empty() || size_of(x) == this->size());
145 std::map<double,T>::operator[](t) = x;
148 virtual SampledTraj<T> sampled(
double dt)
const
150 return sampled(dt,
true);
153 SampledTraj<T> sampled(
double dt,
bool keep_original_values)
const
158 auto straj = TrajBase<T>::sampled(dt);
160 if(keep_original_values)
163 for(
const auto& [ti,xi] : *
this)
171 SampledTraj<T> sampled_as(
const SampledTraj<Q>& x)
const
173 return TrajBase<T>::sampled_as(x);
177 SampledTraj<T> sampled_as(
const SampledTraj<Q>& x,
bool keep_original_values)
const
179 SampledTraj<T> straj = TrajBase<T>::sampled_as(x);
180 if(keep_original_values)
181 for(
const auto& [ti,xi] : *
this)
186 SampledTraj<T>& shift_tdomain(
double shift)
188 std::map<double,T> save = *
this;
190 for(
const auto& [ti,xi] : save)
191 this->std::map<double,T>::operator[](ti+shift) = xi;
195 SampledTraj<T>& stretch_tdomain(
const Interval& tdomain)
197 Interval a = this->tdomain(), b = tdomain;
198 std::map<double,T> save = *
this;
200 for(
const auto& [ti,xi] : save)
201 this->std::map<double,T>::operator[]([&]() {
205 return ((ti-a.lb())*b.diam()/a.diam())+b.lb();
208 assert(this->tdomain() == tdomain);
212 template<
typename T_=T>
213 requires std::is_same_v<T_,Vector>
214 SampledTraj<double> operator[](Index i)
const
216 assert_release(i >= 0 && i < size());
217 std::map<double,double> m;
218 for(
const auto& [t,y] : *
this)
220 assert(i < y.size());
227 template<
typename T_=T>
228 requires std::is_same_v<T_,Vector>
229 SampledTraj<Vector> subvector(Index i, Index j)
const
231 assert_release(i >= 0 && i <= j && j < size());
232 std::map<double,Vector> m;
233 for(
const auto& [t,y] : *
this)
235 assert(j < y.size());
236 m[t] = y.subvector(i,j);
244 inline std::ostream&
operator<<(std::ostream& os,
const SampledTraj<T>& x)
246 os <<
"SampledTraj. " << x.tdomain() <<
"↦" << x.codomain() <<
", " << x.nb_samples() <<
" pts";
250 inline SampledTraj<double> continuous_traj(
const SampledTraj<double>& x)
252 SampledTraj<double> x_continuous;
253 const Interval periodicity = x.codomain();
255 double prev_xi = 0., value_mod = 0.;
257 for(
const auto& [ti,xi] : x)
259 if(!x_continuous.empty())
261 if(prev_xi - xi > periodicity.diam()*0.9)
262 value_mod += periodicity.diam();
263 else if(prev_xi - xi < -periodicity.diam()*0.9)
264 value_mod -= periodicity.diam();
268 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