21 class SampledTraj :
public TrajBase<T>,
public std::map<double,T>
25 using Type =
typename ExprType<T>::Type;
28 : TrajBase<T>(), std::map<double,T>()
31 SampledTraj(
const std::list<double>& l_t,
const std::list<T>& l_x)
34 assert_release(l_t.size() == l_x.size());
35 auto it_t = l_t.begin();
auto it_x = l_x.begin();
36 while(it_t != l_t.end())
38 this->set(*it_x, *it_t);
43 SampledTraj(
const std::map<double,T>& m)
44 : TrajBase<T>(), std::map<double,T>(m)
48 virtual Index size()
const
50 if constexpr(std::is_same_v<typename ExprType<T>::Type,ScalarType>)
58 return this->begin()->second.size();
62 virtual std::pair<Index,Index> shape()
const
64 if constexpr(std::is_same_v<typename ExprType<T>::Type,ScalarType>)
71 return {this->begin()->second.rows(),this->begin()->second.cols()};
75 size_t nb_samples()
const
77 return std::map<double,T>::size();
80 virtual bool is_empty()
const
82 return std::map<double,T>::empty();
85 virtual Interval tdomain()
const
90 return { this->begin()->first, this->rbegin()->first };
93 virtual void truncate_tdomain(
const Interval& new_tdomain)
95 assert_release(this->tdomain().is_superset(new_tdomain));
98 T y_lb = (*this)(new_tdomain.lb());
99 T y_ub = (*this)(new_tdomain.ub());
101 auto it = this->begin();
102 while(it != this->end())
104 if(!new_tdomain.contains(it->first))
105 it = this->erase(it);
110 this->set(y_lb, new_tdomain.lb());
111 this->set(y_ub, new_tdomain.ub());
114 virtual typename Wrapper<T>::Domain codomain()
const
116 typename Wrapper<T>::Domain hull(this->begin()->second);
117 for(
const auto& [t,v] : *
this)
122 virtual T operator()(
double t)
const
124 if(!this->tdomain().contains(t))
125 return this->nan_value();
127 auto it_lower = this->lower_bound(t);
128 if(it_lower->first == t)
129 return it_lower->second;
131 auto it_upper = it_lower;
135 return it_lower->second +
136 (t - it_lower->first) * (it_upper->second - it_lower->second) /
137 (it_upper->first - it_lower->first);
140 virtual typename Wrapper<T>::Domain operator()(
const Interval& t)
const
143 typename Wrapper<T>::Domain hull(this->begin()->second);
145 if(!this->tdomain().is_superset(t))
146 return hull.init(Interval(-oo,oo));
150 hull = (*this)(t.lb());
151 for(
auto it = this->lower_bound(t.lb()) ; it != this->upper_bound(t.ub()) ; it++)
153 hull |= (*this)(t.ub());
158 void set(
const T& x,
double t)
160 assert(this->empty() || size_of(x) == this->size());
161 std::map<double,T>::operator[](t) = x;
164 virtual SampledTraj<T> sampled(
double dt)
const
166 return sampled(dt,
true);
169 SampledTraj<T> sampled(
double dt,
bool keep_original_values)
const
174 auto straj = TrajBase<T>::sampled(dt);
176 if(keep_original_values)
179 for(
const auto& [ti,xi] : *
this)
187 SampledTraj<T> sampled_as(
const SampledTraj<Q>& x)
const
189 return TrajBase<T>::sampled_as(x);
193 SampledTraj<T> sampled_as(
const SampledTraj<Q>& x,
bool keep_original_values)
const
195 SampledTraj<T> straj = TrajBase<T>::sampled_as(x);
196 if(keep_original_values)
197 for(
const auto& [ti,xi] : *
this)
202 SampledTraj<T>& shift_tdomain(
double shift)
204 std::map<double,T> save = *
this;
206 for(
const auto& [ti,xi] : save)
207 this->std::map<double,T>::operator[](ti+shift) = xi;
211 SampledTraj<T>& stretch_tdomain(
const Interval& tdomain)
213 Interval a = this->tdomain(), b = tdomain;
214 std::map<double,T> save = *
this;
216 for(
const auto& [ti,xi] : save)
217 this->std::map<double,T>::operator[]([&]() {
221 return ((ti-a.lb())*b.diam()/a.diam())+b.lb();
224 assert(this->tdomain() == tdomain);
228 template<
typename T_=T>
229 requires std::is_same_v<T_,Vector>
230 SampledTraj<double> operator[](Index i)
const
232 assert_release(i >= 0 && i < size());
233 std::map<double,double> m;
234 for(
const auto& [t,y] : *
this)
236 assert(i < y.size());
243 template<
typename T_=T>
244 requires std::is_same_v<T_,Vector>
245 SampledTraj<Vector> subvector(Index i, Index j)
const
247 assert_release(i >= 0 && i <= j && j < size());
248 std::map<double,Vector> m;
249 for(
const auto& [t,y] : *
this)
251 assert(j < y.size());
252 m[t] = y.subvector(i,j);
258 AnalyticFunction<typename ExprType<T>::Type> as_function()
const
262 AnalyticExprWrapper<typename ExprType<T>::Type>(
263 std::make_shared<AnalyticOperationExpr<
264 TrajectoryOp<SampledTraj<T>>,
typename ExprType<T>::Type,ScalarType>>(*
this,t))
270 inline std::ostream&
operator<<(std::ostream& os,
const SampledTraj<T>& x)
272 os <<
"SampledTraj. " << x.tdomain() <<
"↦" << x.codomain() <<
", " << x.nb_samples() <<
" pts";
276 inline SampledTraj<double> continuous_traj(
const SampledTraj<double>& x)
278 SampledTraj<double> x_continuous;
279 const Interval periodicity = x.codomain();
281 double prev_xi = 0., value_mod = 0.;
283 for(
const auto& [ti,xi] : x)
285 if(!x_continuous.empty())
287 if(prev_xi - xi > periodicity.diam()*0.9)
288 value_mod += periodicity.diam();
289 else if(prev_xi - xi < -periodicity.diam()*0.9)
290 value_mod -= periodicity.diam();
294 x_continuous.set(xi+value_mod, ti);
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:551
std::ostream & operator<<(std::ostream &os, const BoolInterval &x)
Streams out a BoolInterval.
Definition codac2_BoolInterval.h:45