22 class SampledTraj :
public TrajBase<T>,
public std::map<double,T>
26 using Type =
typename ExprType<T>::Type;
29 : TrajBase<T>(), std::map<double,T>()
32 SampledTraj(
const std::list<double>& l_t,
const std::list<T>& l_x)
35 assert_release(l_t.size() == l_x.size());
36 auto it_t = l_t.begin();
auto it_x = l_x.begin();
37 while(it_t != l_t.end())
39 this->set(*it_x, *it_t);
44 SampledTraj(
const std::map<double,T>& m)
45 : TrajBase<T>(), std::map<double,T>(m)
49 virtual Index size()
const
51 if constexpr(std::is_same_v<typename ExprType<T>::Type,ScalarType>)
59 return this->begin()->second.size();
63 virtual std::pair<Index,Index> shape()
const
65 if constexpr(std::is_same_v<typename ExprType<T>::Type,ScalarType>)
72 return {this->begin()->second.rows(),this->begin()->second.cols()};
76 size_t nb_samples()
const
78 return std::map<double,T>::size();
81 virtual bool is_empty()
const
83 return std::map<double,T>::empty();
86 virtual Interval tdomain()
const
91 return { this->begin()->first, this->rbegin()->first };
94 virtual void truncate_tdomain(
const Interval& new_tdomain)
96 assert_release(this->tdomain().
is_superset(new_tdomain));
99 T y_lb = (*this)(new_tdomain.lb());
100 T y_ub = (*this)(new_tdomain.ub());
102 auto it = this->begin();
103 while(it != this->end())
105 if(!new_tdomain.contains(it->first))
106 it = this->erase(it);
111 this->set(y_lb, new_tdomain.lb());
112 this->set(y_ub, new_tdomain.ub());
115 virtual typename Wrapper<T>::Domain codomain()
const
117 typename Wrapper<T>::Domain hull(this->begin()->second);
118 for(
const auto& [t,v] : *
this)
123 virtual T operator()(
double t)
const
126 return this->nan_value();
128 auto it_lower = this->lower_bound(t);
129 if(it_lower->first == t)
130 return it_lower->second;
132 auto it_upper = it_lower;
136 return it_lower->second +
137 (t - it_lower->first) * (it_upper->second - it_lower->second) /
138 (it_upper->first - it_lower->first);
141 virtual typename Wrapper<T>::Domain operator()(
const Interval& t)
const
144 typename Wrapper<T>::Domain hull(this->begin()->second);
147 return hull.init(Interval(-oo,oo));
151 hull = (*this)(t.lb());
152 for(
auto it = this->lower_bound(t.lb()) ; it != this->upper_bound(t.ub()) ; it++)
154 hull |= (*this)(t.ub());
159 void set(
const T& x,
double t)
161 assert(this->empty() || size_of(x) == this->size());
162 std::map<double,T>::operator[](t) = x;
165 virtual SampledTraj<T> sampled(
double dt)
const
167 return sampled(dt,
true);
170 SampledTraj<T> sampled(
double dt,
bool keep_original_values)
const
175 auto straj = TrajBase<T>::sampled(dt);
177 if(keep_original_values)
180 for(
const auto& [ti,xi] : *
this)
188 SampledTraj<T> sampled_as(
const SampledTraj<Q>& x)
const
190 return TrajBase<T>::sampled_as(x);
194 SampledTraj<T> sampled_as(
const SampledTraj<Q>& x,
bool keep_original_values)
const
196 SampledTraj<T> straj = TrajBase<T>::sampled_as(x);
197 if(keep_original_values)
198 for(
const auto& [ti,xi] : *
this)
203 SampledTraj<T>& shift_tdomain(
double shift)
205 std::map<double,T> save = *
this;
207 for(
const auto& [ti,xi] : save)
208 this->std::map<double,T>::operator[](ti+shift) = xi;
212 SampledTraj<T>& stretch_tdomain(
const Interval& tdomain)
214 Interval a = this->tdomain(), b = tdomain;
215 std::map<double,T> save = *
this;
217 for(
const auto& [ti,xi] : save)
218 this->std::map<double,T>::operator[]([&]() {
222 return ((ti-a.lb())*b.diam()/a.diam())+b.lb();
225 assert(this->tdomain() == tdomain);
229 template<
typename T_=T>
230 requires std::is_same_v<T_,Vector>
231 SampledTraj<double> operator[](Index i)
const
233 assert_release(i >= 0 && i < size());
234 std::map<double,double> m;
235 for(
const auto& [t,y] : *
this)
237 assert(i < y.size());
244 template<
typename T_=T>
245 requires std::is_same_v<T_,Vector>
246 SampledTraj<Vector> subvector(Index i, Index j)
const
248 assert_release(i >= 0 && i <= j && j < size());
249 std::map<double,Vector> m;
250 for(
const auto& [t,y] : *
this)
252 assert(j < y.size());
253 m[t] = y.subvector(i,j);
259 AnalyticFunction<typename ExprType<T>::Type> as_function()
const
263 AnalyticExprWrapper<typename ExprType<T>::Type>(
264 std::make_shared<AnalyticOperationExpr<
265 TrajectoryOp<SampledTraj<T>>,
typename ExprType<T>::Type,ScalarType>>(*
this,t))
271 inline std::ostream&
operator<<(std::ostream& os,
const SampledTraj<T>& x)
273 os <<
"SampledTraj. " << x.tdomain() <<
"↦" << x.codomain() <<
", " << x.nb_samples() <<
" pts";
277 inline SampledTraj<double> continuous_traj(
const SampledTraj<double>& x)
279 SampledTraj<double> x_continuous;
280 const Interval periodicity = x.codomain();
282 double prev_xi = 0., value_mod = 0.;
284 for(
const auto& [ti,xi] : x)
286 if(!x_continuous.empty())
288 if(prev_xi - xi > periodicity.diam()*0.9)
289 value_mod += periodicity.diam();
290 else if(prev_xi - xi < -periodicity.diam()*0.9)
291 value_mod -= periodicity.diam();
295 x_continuous.set(xi+value_mod, ti);
301 inline std::vector<SampledTraj<double>> as_scalar_trajs(
const SampledTraj<Vector>& x)
303 std::vector<SampledTraj<double>> v(x.size());
304 for(
const auto& [ti,xi] : x)
305 for(Index i = 0 ; i < x.size() ; i++)
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:49
static Interval empty()
Provides an empty interval.
Definition codac2_Interval_impl.h:558
bool is_superset(const Matrix< codac2::Interval, RowsAtCompileTime, ColsAtCompileTime > &x) const
Checks whether this matrix is a superset of another interval matrix.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:648
bool contains(const Matrix< double, RowsAtCompileTime, ColsAtCompileTime > &x) const
Checks if this interval matrix contains the specified matrix x.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:198
Definition codac2_OctaSym.h:21
std::ostream & operator<<(std::ostream &os, const BoolInterval &x)
Streams out a BoolInterval.
Definition codac2_BoolInterval.h:64