Crocoddyl
squashing-base.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2020, University of Edinburgh, IRI: CSIC-UPC
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
8 
9 #ifndef CROCODDYL_CORE_SQUASHING_BASE_HPP_
10 #define CROCODDYL_CORE_SQUASHING_BASE_HPP_
11 
12 #include <boost/make_shared.hpp>
13 #include <boost/shared_ptr.hpp>
14 #include <stdexcept>
15 
16 #include "crocoddyl/core/fwd.hpp"
17 #include "crocoddyl/core/mathbase.hpp"
18 #include "crocoddyl/core/utils/exception.hpp"
19 
20 namespace crocoddyl {
21 
22 template <typename _Scalar>
24  public:
25  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
26 
27  typedef _Scalar Scalar;
30  typedef typename MathBase::VectorXs VectorXs;
31 
32  SquashingModelAbstractTpl(const std::size_t ns) : ns_(ns) {
33  if (ns_ == 0) {
34  throw_pretty("Invalid argument: "
35  << "ns cannot be zero");
36  }
37  };
38  virtual ~SquashingModelAbstractTpl(){};
39 
40  virtual void calc(const boost::shared_ptr<SquashingDataAbstract>& data,
41  const Eigen::Ref<const VectorXs>& s) = 0;
42  virtual void calcDiff(const boost::shared_ptr<SquashingDataAbstract>& data,
43  const Eigen::Ref<const VectorXs>& s) = 0;
44  virtual boost::shared_ptr<SquashingDataAbstract> createData() {
45  return boost::allocate_shared<SquashingDataAbstract>(
46  Eigen::aligned_allocator<SquashingDataAbstract>(), this);
47  }
48 
49  std::size_t get_ns() const { return ns_; };
50  const VectorXs& get_s_lb() const { return s_lb_; };
51  const VectorXs& get_s_ub() const { return s_ub_; };
52 
53  void set_s_lb(const VectorXs& s_lb) { s_lb_ = s_lb; };
54  void set_s_ub(const VectorXs& s_ub) { s_ub_ = s_ub; };
55 
56  protected:
57  std::size_t ns_;
58  VectorXs u_ub_; // Squashing function upper bound
59  VectorXs u_lb_; // Squashing function lower bound
60  VectorXs
61  s_ub_; // Bound for the s variable (to apply using the Quadratic barrier)
62  VectorXs
63  s_lb_; // Bound for the s variable (to apply using the Quadratic barrier)
64 };
65 
66 template <typename _Scalar>
68  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
69 
70  typedef _Scalar Scalar;
72  typedef typename MathBase::VectorXs VectorXs;
73  typedef typename MathBase::MatrixXs MatrixXs;
74 
75  template <template <typename Scalar> class Model>
76  explicit SquashingDataAbstractTpl(Model<Scalar>* const model)
77  : u(model->get_ns()), du_ds(model->get_ns(), model->get_ns()) {
78  u.setZero();
79  du_ds.setZero();
80  }
81  virtual ~SquashingDataAbstractTpl() {}
82 
83  VectorXs u;
84  MatrixXs du_ds;
85 };
86 
87 } // namespace crocoddyl
88 
89 #endif // CROCODDYL_CORE_SQUASHING_BASE_HPP_