Lodestar
An integrated real-time control package in C++
ReImToComplexBlock.hpp
1 //
2 // Created by Hamza El-Kebir on 12/23/21.
3 //
4 
5 #ifndef LODESTAR_REIMTOCOMPLEXBLOCK_HPP
6 #define LODESTAR_REIMTOCOMPLEXBLOCK_HPP
7 
8 #include "Lodestar/blocks/Block.hpp"
9 #include <iomanip>
10 #include <complex>
11 #include <cmath>
12 #include "Eigen/Dense"
13 
14 namespace ls {
15  namespace blocks {
16  namespace std {
17  template<typename TType, typename SFINAE = void>
19  public Block<
20  ::std::tuple<TType>,
21  ::std::tuple<TType>,
22  BlockProto::empty
23  > {
24  static_assert(!::std::is_same<TType, TType>::value,
25  "ReImToComplexBlock not defined for this type.");
26  };
27 
28  template<typename TType>
29  class ReImToComplexBlock<TType, typename ::std::enable_if<(
30  ::std::is_same<TType, float>::value ||
31  ::std::is_same<TType, double>::value ||
32  ::std::is_same<TType, long double>::value)>::type>
33  :
34  public Block<
35  ::std::tuple<TType, TType>,
36  ::std::tuple<::std::complex<TType>>,
37  BlockProto::empty
38  > {
39  public:
40  using type = ReImToComplexBlock<
41  TType,
42  typename ::std::enable_if<(
43  ::std::is_same<TType, float>::value ||
44  ::std::is_same<TType, double>::value ||
45  ::std::is_same<TType, long double>::value)>::type
46  >;
47 
48  using Base =
49  Block<
50  ::std::tuple<TType, TType>,
51  ::std::tuple<::std::complex<TType>>,
52  BlockProto::empty
53  >;
54 
55  static const ::std::complex<TType> j;
56 
58  {
59  bindEquation();
60  }
61 
62  Signal<TType> &real()
63  {
64  return this->template i<0>();
65  }
66 
67  Signal<TType> &real(TType re)
68  {
69  this->template i<0>() = re;
70  return this->template i<0>();
71  }
72 
73  const Signal<TType> &real() const
74  {
75  return this->template i<0>();
76  }
77 
78  Signal<TType> &imag()
79  {
80  return this->template i<1>();
81  }
82 
83  Signal<TType> &imag(TType im)
84  {
85  this->template i<1>() = im;
86  return this->template i<1>();
87  }
88 
89  const Signal<TType> &imag() const
90  {
91  return this->template i<1>();
92  }
93 
94  protected:
95  void bindEquation()
96  {
97  this->equation = ::std::bind(
98  &type::triggerFunction,
99  this,
100  ::std::placeholders::_1
101  );
102  }
103 
104  void triggerFunction(Base &b)
105  {
106  b.template o<0>().object.real(b.template i<0>());
107  b.template o<0>().object.imag(b.template i<1>());
108  }
109  };
110 
111  template<typename TType>
112  const ::std::complex<TType> ReImToComplexBlock<TType, typename ::std::enable_if<(
113  ::std::is_same<TType, float>::value ||
114  ::std::is_same<TType, double>::value ||
115  ::std::is_same<TType, long double>::value)>::type>::j =
116  ::std::complex<TType>{0, 1};
117 
118  template<typename TScalar, int TRows, int TCols>
120  ::std::is_same<TScalar, float>::value ||
121  ::std::is_same<TScalar, double>::value ||
122  ::std::is_same<TScalar, long double>::value)>::type> :
123  public Block<
124  ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>, Eigen::Matrix<TScalar, TRows, TCols>>,
125  ::std::tuple<Eigen::Matrix<::std::complex<TScalar>, TRows, TCols>>,
126  BlockProto::empty
127  > {
128  public:
129  using type = ReImToComplexBlock<
130  Eigen::Matrix<TScalar, TRows, TCols>,
131  typename ::std::enable_if<(
132  ::std::is_same<TScalar, float>::value ||
133  ::std::is_same<TScalar, double>::value ||
134  ::std::is_same<TScalar, long double>::value)
135  >::type>;
136 
137  using Base =
138  Block<
139  ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>, Eigen::Matrix<TScalar, TRows, TCols>>,
140  ::std::tuple<Eigen::Matrix<::std::complex<TScalar>, TRows, TCols>>,
141  BlockProto::empty
142  >;
143 
144  static const ::std::complex<TScalar> j;
145 
146  using RealMatrix = Eigen::Matrix<TScalar, TRows, TCols>;
147 
149  {
150  bindEquation();
151  }
152 
153  Signal<RealMatrix> &real()
154  {
155  return this->template i<0>();
156  }
157 
158  Signal<RealMatrix> &real(const RealMatrix &re)
159  {
160  this->template i<0>() = re;
161  return this->template i<0>();
162  }
163 
164  const Signal<RealMatrix> &real() const
165  {
166  return this->template i<0>();
167  }
168 
169  Signal<RealMatrix> &imag()
170  {
171  return this->template i<1>();
172  }
173 
174  Signal<RealMatrix> &imag(const RealMatrix &im)
175  {
176  this->template i<1>() = im;
177  return this->template i<1>();
178  }
179 
180  const Signal<RealMatrix> &imag() const
181  {
182  return this->template i<1>();
183  }
184 
185  protected:
186  void bindEquation()
187  {
188  this->equation = ::std::bind(
189  &type::triggerFunction,
190  this,
191  ::std::placeholders::_1
192  );
193  }
194 
195  void triggerFunction(Base &b)
196  {
197  b.template o<0>().object = real().object +
198  ::std::complex<TScalar>(0, 1) *
199  imag().object;
200  }
201  };
202 
203  template<typename TScalar, int TRows, int TCols>
204  const ::std::complex<TScalar> ReImToComplexBlock<Eigen::Matrix<TScalar, TRows, TCols>, typename ::std::enable_if<(
205  ::std::is_same<TScalar, float>::value ||
206  ::std::is_same<TScalar, double>::value ||
207  ::std::is_same<TScalar, long double>::value)>::type>::j =
208  ::std::complex<TScalar>{0, 1};
209  }
210 
211  template<typename TType, typename SFINAE>
213  public:
214  static constexpr const BlockType blockType = BlockType::ReImToComplexBlock;
215  static constexpr const bool directFeedthrough = true;
216 
218  using Base = typename type::Base;
219 
220  static const constexpr int kIns = type::Base::kIns;
221  static const constexpr int kOuts = type::Base::kOuts;
222  static const constexpr int kPars = type::Base::kPars;
223  };
224  }
225 }
226 
227 
228 #endif //LODESTAR_REIMTOCOMPLEXBLOCK_HPP
ls::blocks::BlockType::ReImToComplexBlock
@ ReImToComplexBlock
Real and imaginary to complex block.
ls::blocks::BlockTraits
A traits object that exposes information about TBlock.
Definition: BlockTraits.hpp:37
ls::blocks::BlockType
BlockType
Block type information.
Definition: BlockType.hpp:25
ls::blocks::std::ReImToComplexBlock< TType, typename ::std::enable_if<(::std::is_same< TType, float >::value||::std::is_same< TType, double >::value||::std::is_same< TType, long double >::value)>::type >
Definition: ReImToComplexBlock.hpp:29
ls
Main Lodestar code.
Definition: BilinearTransformation.hpp:12
ls::blocks::std::ReImToComplexBlock
Definition: ReImToComplexBlock.hpp:18
ls::blocks::Signal
Definition: Signal.hpp:22
ls::blocks::Block
Generic base template class for all tuple-based Block instances.
Definition: Block.hpp:45