codac 1.5.6
Loading...
Searching...
No Matches
codac2_TrajBase.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include "codac2_Interval.h"
13#include "codac2_Wrapper.h"
15#include "codac2_ValueType.h"
16
17namespace codac2
18{
19 template<typename T>
20 class SampledTraj;
21
22 template<typename T>
23 class TrajBase
24 {
25 public:
26
27 TrajBase()
28 { }
29
30 virtual Index size() const = 0;
31 virtual bool is_empty() const = 0;
32 virtual Interval tdomain() const = 0;
33 virtual void truncate_tdomain(const Interval& new_tdomain) = 0;
34 virtual typename Wrapper<T>::Domain codomain() const = 0;
35 virtual T operator()(double t) const = 0;
36 virtual typename Wrapper<T>::Domain operator()(const Interval& t) const = 0;
37
38 auto nan_value() const
39 {
40 if constexpr(std::is_same_v<T,double> || std::is_same_v<typename ValueType<T>::Type,ScalarType>)
41 return std::numeric_limits<double>::quiet_NaN();
42
43 else
44 return T((*this)(tdomain().lb())) // we obtain the output dimension by an evalution...
45 .init(std::numeric_limits<double>::quiet_NaN());
46 }
47
48 virtual SampledTraj<T> sampled(double dt) const
49 {
50 assert_release(dt > 0.);
51 assert_release(!is_empty());
52
53 auto tdom = tdomain();
54 SampledTraj<T> straj;
55 for(double t = tdom.lb() ; t < tdom.ub() ; t+=dt)
56 straj.set(t, (*this)(t));
57 straj.set(tdom.ub(), (*this)(tdom.ub()));
58 return straj;
59 }
60
61 template<typename Q>
62 SampledTraj<T> sampled_as(const SampledTraj<Q>& x) const
63 {
64 assert_release(x.tdomain().is_subset(this->tdomain()));
65
66 SampledTraj<T> straj;
67 for(const auto& [ti,dump] : x)
68 straj.set(ti, (*this)(ti));
69 return straj;
70 }
71
72 SampledTraj<T> primitive(const T& y0, double dt) const
73 {
74 assert_release(dt > 0.);
75 assert_release(!is_empty());
76
77 SampledTraj<T> p;
78 double t = tdomain().lb(), last_t = t;
79 p.set(t, y0); t += dt;
80 T y = y0;
81
82 while(t < tdomain().ub())
83 {
84 y += ((*this)(last_t)+(*this)(t))*dt/2.;
85 p.set(t, y);
86 last_t = t;
87 t += dt;
88 }
89
90 t = tdomain().ub();
91 y += ((*this)(last_t)+(*this)(t))*(t-last_t)/2.;
92 p.set(t, y);
93
94 return p;
95 }
96
97 // Implementation in codac2_Trajectory_operator.h
98 AnalyticFunction<typename ValueType<T>::Type> as_function() const;
99 };
100}