state.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Software License Agreement (BSD License) *
3  * Copyright (C) 2016 by Horatiu George Todoran <todorangrg@gmail.com> *
4  * *
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above copyright *
10  * notice, this list of conditions and the following disclaimer. *
11  * 2. Redistributions in binary form must reproduce the above copyright *
12  * notice, this list of conditions and the following disclaimer in *
13  * the documentation and/or other materials provided with the *
14  * distribution. *
15  * 3. Neither the name of the copyright holder nor the names of its *
16  * contributors may be used to endorse or promote products derived *
17  * from this software without specific prior written permission. *
18  * *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY *
29  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
30  * POSSIBILITY OF SUCH DAMAGE. *
31  ***************************************************************************/
32 
33 #ifndef STATE_H
34 #define STATE_H
35 
36 #include <float.h>
37 #include <memory>
38 
39 #include <eigen3/Eigen/Eigen>
40 
41 namespace tuw
42 {
90 
92 
105 
110 
114 
118 
120 
124 
128 class State;
129 using StateSPtr = std::shared_ptr<State>;
130 using StateConstSPtr = std::shared_ptr<State const>;
131 using StateUPtr = std::unique_ptr<State>;
132 using StateConstUPtr = std::unique_ptr<State const>; /*
133  using StateVectorSPtr = std::shared_ptr<std::vector<State > >;
134  using StateSPtrVectorSPtr = std::shared_ptr<std::vector<StateSPtr> >;*/
135 class State
136 {
137  // special class member functions
138 public:
139  State(State* _parent) : parent_(_parent)
140  {
142  }
143 
144 public:
145  State() : parent_(nullptr)
146  {
148  }
149 
150 public:
151  virtual ~State() = default;
152 
153 public:
154  State(const State&) = default;
155 
156 public:
157  State& operator=(const State&) = default;
158 
159 public:
160  State(State&&) = default;
161 
162 public:
163  State& operator=(State&&) = default;
164 
165  // pure virtual functions
167 public:
168  virtual StateSPtr cloneState() const = 0;
170 public:
171  virtual double& value(const std::size_t& _i) = 0;
173 public:
174  virtual const double& value(const std::size_t& _i) const = 0;
176 public:
177  virtual size_t valueSize() const = 0;
178 
180 public:
181  virtual void updateSize()
182  {
183  }
185 public:
186  virtual void resize(const size_t& _i)
187  {
188  throw "function not supported for the instantiated class";
189  }
191 public:
192  virtual StateSPtr& state(const std::size_t& _i)
193  {
194  static StateSPtr stateNullPtr_ = nullptr;
195  return stateNullPtr_;
196  }
198 public:
199  virtual size_t stateSize() const
200  {
201  return 0;
202  }
204 public:
205  void toSTLVec(std::vector<double>& _vec)
206  {
207  _vec.resize(valueSize());
208  for (size_t i = 0; i < _vec.size(); ++i)
209  {
210  _vec[i] = value(i);
211  }
212  }
214 public:
215  void toEIGENVec(Eigen::VectorXd& _vec)
216  {
217  _vec.resize(valueSize());
218  for (size_t i = 0; i < (size_t)_vec.rows(); ++i)
219  {
220  _vec(i) = value(i);
221  }
222  }
225 public:
226  void fromSTLVec(const std::vector<double>& _vec)
227  {
228  if (_vec.size() != valueSize())
229  {
230  throw "cannot copy from container of different size";
231  };
232  for (size_t i = 0; i < _vec.size(); ++i)
233  {
234  value(i) = _vec[i];
235  }
236  }
239 public:
240  void fromEIGENVec(const Eigen::VectorXd& _vec)
241  {
242  if ((size_t)_vec.rows() != valueSize())
243  {
244  throw "cannot copy from container of different size";
245  };
246  for (size_t i = 0; i < (size_t)_vec.rows(); ++i)
247  {
248  value(i) = _vec(i);
249  }
250  }
257  static std::vector<double>& plus(State& _lhs, State& _rhs, std::vector<double>& _ans)
258  {
259  if (_lhs.valueSize() != _rhs.valueSize())
260  {
261  throw "cannot add two containers of different sizes";
262  }
263  _ans.resize(_lhs.valueSize());
264  for (size_t i = 0; i < _lhs.valueSize(); ++i)
265  {
266  _ans[i] = _lhs.value(i) + _rhs.value(i);
267  }
268  return _ans;
269  }
276  static Eigen::VectorXd& plus(State& _lhs, State& _rhs, Eigen::VectorXd& _ans)
277  {
278  if (_lhs.valueSize() != _rhs.valueSize())
279  {
280  throw "cannot add two containers of different sizes";
281  }
282  _ans.resize(_lhs.valueSize());
283  for (size_t i = 0; i < _lhs.valueSize(); ++i)
284  {
285  _ans[i] = _lhs.value(i) + _rhs.value(i);
286  }
287  return _ans;
288  }
296  static State& plus(State& _lhs, State& _rhs, State& _ans)
297  {
298  if (!((_ans.valueSize() == _rhs.valueSize()) && (_lhs.valueSize() == _rhs.valueSize())))
299  {
300  throw "cannot add two containers of different sizes";
301  }
302  for (size_t i = 0; i < _ans.valueSize(); ++i)
303  {
304  _ans.value(i) = _lhs.value(i) + _rhs.value(i);
305  }
306  return _ans;
307  }
314  static std::vector<double>& minus(State& _lhs, State& _rhs, std::vector<double>& _ans)
315  {
316  if (_lhs.valueSize() != _rhs.valueSize())
317  {
318  throw "cannot add two containers of different sizes";
319  }
320  _ans.resize(_lhs.valueSize());
321  for (size_t i = 0; i < _lhs.valueSize(); ++i)
322  {
323  _ans[i] = _lhs.value(i) - _rhs.value(i);
324  }
325  return _ans;
326  }
333  static Eigen::VectorXd& minus(State& _lhs, State& _rhs, Eigen::VectorXd& _ans)
334  {
335  if (_lhs.valueSize() != _rhs.valueSize())
336  {
337  throw "cannot add two containers of different sizes";
338  }
339  _ans.resize(_lhs.valueSize());
340  for (size_t i = 0; i < _lhs.valueSize(); ++i)
341  {
342  _ans[i] = _lhs.value(i) - _rhs.value(i);
343  }
344  return _ans;
345  }
353  static State& minus(State& _lhs, State& _rhs, State& _ans)
354  {
355  if (!((_ans.valueSize() == _rhs.valueSize()) && (_lhs.valueSize() == _rhs.valueSize())))
356  {
357  throw "cannot add two containers of different sizes";
358  }
359  for (size_t i = 0; i < _ans.valueSize(); ++i)
360  {
361  _ans.value(i) = _lhs.value(i) - _rhs.value(i);
362  }
363  return _ans;
364  }
370  friend std::ostream& operator<<(std::ostream& os, const State& o)
371  {
372  for (size_t i = 0; i < o.valueSize(); i++)
373  {
374  os << (i == 0 ? "[ " : ", ") << o.value(i);
375  }
376  return os << "]";
377  };
378 
379 protected:
381  {
382  if (parent_)
383  {
385  }
386  else
387  {
388  updateSize();
389  }
390  }
391 protected:
393 };
394 }
395 
396 #endif // STATE_H
virtual void resize(const size_t &_i)
Resizes the array.
Definition: state.h:186
void toEIGENVec(Eigen::VectorXd &_vec)
Converts all the array values to an Eigen vector.
Definition: state.h:215
static State & plus(State &_lhs, State &_rhs, State &_ans)
Performs addition. Answer variable, left operand and right operand are required to have the same valu...
Definition: state.h:296
std::shared_ptr< State > StateSPtr
Definition: state.h:129
State()
Definition: state.h:145
static Eigen::VectorXd & plus(State &_lhs, State &_rhs, Eigen::VectorXd &_ans)
Performs addition. Left and right operand are required to have the same valueSize.
Definition: state.h:276
virtual double & value(const std::size_t &_i)=0
Access state variable based on index _i.
static std::vector< double > & plus(State &_lhs, State &_rhs, std::vector< double > &_ans)
Performs addition. Left and right operand are required to have the same valueSize.
Definition: state.h:257
virtual void updateSize()
Performs internal manipulation when any of the underlying arrays are being resized.
Definition: state.h:181
std::unique_ptr< State const > StateConstUPtr
Definition: state.h:132
virtual StateSPtr & state(const std::size_t &_i)
Access sub-state based on index _i.
Definition: state.h:192
void fromEIGENVec(const Eigen::VectorXd &_vec)
Copies all values from an Eigen vector. The valueSize of the State object has to be equal with the Ei...
Definition: state.h:240
State(State *_parent)
Definition: state.h:139
std::shared_ptr< State const > StateConstSPtr
Definition: state.h:130
virtual size_t stateSize() const
Size of the sub-states.
Definition: state.h:199
State & operator=(const State &)=default
virtual ~State()=default
virtual size_t valueSize() const =0
Size of the state variables.
static std::vector< double > & minus(State &_lhs, State &_rhs, std::vector< double > &_ans)
Performs substraction. Left and right operand are required to have the same valueSize.
Definition: state.h:314
Generic tree-like recursive structure that allows sub-structure access as well as ordered (array-like...
Definition: state.h:135
friend std::ostream & operator<<(std::ostream &os, const State &o)
prints the state value to an output stream.
Definition: state.h:370
std::unique_ptr< State > StateUPtr
Definition: state.h:131
void fromSTLVec(const std::vector< double > &_vec)
Copies all values from an STL vector. The valueSize of the State object has to be equal with the STL ...
Definition: state.h:226
static State & minus(State &_lhs, State &_rhs, State &_ans)
Performs substraction. Answer variable, left operand and right operand are required to have the same ...
Definition: state.h:353
void toSTLVec(std::vector< double > &_vec)
Converts all the array values to an STL vector.
Definition: state.h:205
void callRootUpdateSize()
Calls (if present) the parent updateSize procedure. Otherwise performs root updateSize.
Definition: state.h:380
State * parent_
Pointer to the parent State structure.
Definition: state.h:392
virtual StateSPtr cloneState() const =0
Clone-to-base-class-ptr function.
static Eigen::VectorXd & minus(State &_lhs, State &_rhs, Eigen::VectorXd &_ans)
Performs substraction. Left and right operand are required to have the same valueSize.
Definition: state.h:333


tuw_control
Author(s): George Todoran
autogenerated on Mon Jun 10 2019 15:27:21