| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /////////////////////////////////////////////////////////////////////////////// | ||
| 2 | // BSD 3-Clause License | ||
| 3 | // | ||
| 4 | // Copyright (C) 2021-2025, University of Edinburgh, Heriot-Watt University | ||
| 5 | // Copyright note valid unless otherwise stated in individual files. | ||
| 6 | // All rights reserved. | ||
| 7 | /////////////////////////////////////////////////////////////////////////////// | ||
| 8 | |||
| 9 | #ifndef BINDINGS_PYTHON_CROCODDYL_CORE_RESIDUAL_BASE_HPP_ | ||
| 10 | #define BINDINGS_PYTHON_CROCODDYL_CORE_RESIDUAL_BASE_HPP_ | ||
| 11 | |||
| 12 | #include "crocoddyl/core/residual-base.hpp" | ||
| 13 | #include "python/crocoddyl/core/core.hpp" | ||
| 14 | |||
| 15 | namespace crocoddyl { | ||
| 16 | namespace python { | ||
| 17 | |||
| 18 | template <typename _Scalar> | ||
| 19 | class ResidualModelAbstractTpl_wrap | ||
| 20 | : public ResidualModelAbstractTpl<_Scalar>, | ||
| 21 | public bp::wrapper<ResidualModelAbstractTpl<_Scalar>> { | ||
| 22 | public: | ||
| 23 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
| 24 | ✗ | CROCODDYL_DERIVED_CAST(ResidualModelBase, ResidualModelAbstractTpl_wrap) | |
| 25 | |||
| 26 | typedef _Scalar Scalar; | ||
| 27 | typedef typename ScalarSelector<Scalar>::type ScalarType; | ||
| 28 | typedef typename crocoddyl::ResidualModelAbstractTpl<Scalar> ResidualModel; | ||
| 29 | typedef typename crocoddyl::ResidualDataAbstractTpl<Scalar> ResidualData; | ||
| 30 | typedef typename crocoddyl::StateAbstractTpl<Scalar> State; | ||
| 31 | typedef typename ResidualModel::CostDataAbstract CostData; | ||
| 32 | typedef typename ResidualModel::ActivationDataAbstract ActivationData; | ||
| 33 | typedef typename ResidualModel::DataCollectorAbstract DataCollectorAbstract; | ||
| 34 | typedef typename ResidualModel::VectorXs VectorXs; | ||
| 35 | using ResidualModel::nr_; | ||
| 36 | using ResidualModel::nu_; | ||
| 37 | using ResidualModel::q_dependent_; | ||
| 38 | using ResidualModel::state_; | ||
| 39 | using ResidualModel::u_dependent_; | ||
| 40 | using ResidualModel::unone_; | ||
| 41 | using ResidualModel::v_dependent_; | ||
| 42 | |||
| 43 | ✗ | ResidualModelAbstractTpl_wrap(std::shared_ptr<State> state, | |
| 44 | const std::size_t nr, const std::size_t nu, | ||
| 45 | const bool q_dependent = true, | ||
| 46 | const bool v_dependent = true, | ||
| 47 | const bool u_dependent = true) | ||
| 48 | : ResidualModel(state, nr, nu, q_dependent, v_dependent, u_dependent), | ||
| 49 | ✗ | bp::wrapper<ResidualModel>() { | |
| 50 | ✗ | unone_ = VectorXs::Constant(nu_, Scalar(NAN)); | |
| 51 | ✗ | } | |
| 52 | |||
| 53 | ✗ | ResidualModelAbstractTpl_wrap(std::shared_ptr<State> state, | |
| 54 | const std::size_t nr, | ||
| 55 | const bool q_dependent = true, | ||
| 56 | const bool v_dependent = true, | ||
| 57 | const bool u_dependent = true) | ||
| 58 | : ResidualModel(state, nr, q_dependent, v_dependent, u_dependent), | ||
| 59 | ✗ | bp::wrapper<ResidualModel>() { | |
| 60 | ✗ | unone_ = VectorXs::Constant(nu_, Scalar(NAN)); | |
| 61 | ✗ | } | |
| 62 | |||
| 63 | ✗ | void calc(const std::shared_ptr<ResidualData>& data, | |
| 64 | const Eigen::Ref<const VectorXs>& x, | ||
| 65 | const Eigen::Ref<const VectorXs>& u) override { | ||
| 66 | ✗ | if (static_cast<std::size_t>(x.size()) != state_->get_nx()) { | |
| 67 | ✗ | throw_pretty( | |
| 68 | "Invalid argument: " << "x has wrong dimension (it should be " + | ||
| 69 | std::to_string(state_->get_nx()) + ")"); | ||
| 70 | } | ||
| 71 | ✗ | if (static_cast<std::size_t>(u.size()) != nu_) { | |
| 72 | ✗ | throw_pretty( | |
| 73 | "Invalid argument: " << "u has wrong dimension (it should be " + | ||
| 74 | std::to_string(nu_) + ")"); | ||
| 75 | } | ||
| 76 | ✗ | if (std::isnan( | |
| 77 | ✗ | scalar_cast<ScalarType>(u.template lpNorm<Eigen::Infinity>()))) { | |
| 78 | ✗ | return bp::call<void>(this->get_override("calc").ptr(), data, | |
| 79 | ✗ | (VectorXs)x); | |
| 80 | } else { | ||
| 81 | ✗ | return bp::call<void>(this->get_override("calc").ptr(), data, (VectorXs)x, | |
| 82 | ✗ | (VectorXs)u); | |
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | ✗ | void calcDiff(const std::shared_ptr<ResidualData>& data, | |
| 87 | const Eigen::Ref<const VectorXs>& x, | ||
| 88 | const Eigen::Ref<const VectorXs>& u) override { | ||
| 89 | ✗ | if (static_cast<std::size_t>(x.size()) != state_->get_nx()) { | |
| 90 | ✗ | throw_pretty( | |
| 91 | "Invalid argument: " << "x has wrong dimension (it should be " + | ||
| 92 | std::to_string(state_->get_nx()) + ")"); | ||
| 93 | } | ||
| 94 | ✗ | if (static_cast<std::size_t>(u.size()) != nu_) { | |
| 95 | ✗ | throw_pretty( | |
| 96 | "Invalid argument: " << "u has wrong dimension (it should be " + | ||
| 97 | std::to_string(nu_) + ")"); | ||
| 98 | } | ||
| 99 | ✗ | if (std::isnan( | |
| 100 | ✗ | scalar_cast<ScalarType>(u.template lpNorm<Eigen::Infinity>()))) { | |
| 101 | ✗ | return bp::call<void>(this->get_override("calcDiff").ptr(), data, | |
| 102 | ✗ | (VectorXs)x); | |
| 103 | } else { | ||
| 104 | ✗ | return bp::call<void>(this->get_override("calcDiff").ptr(), data, | |
| 105 | ✗ | (VectorXs)x, (VectorXs)u); | |
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | ✗ | std::shared_ptr<ResidualData> createData( | |
| 110 | DataCollectorAbstract* const data) override { | ||
| 111 | ✗ | enableMultithreading() = false; | |
| 112 | ✗ | if (boost::python::override createData = this->get_override("createData")) { | |
| 113 | return bp::call<std::shared_ptr<ResidualData>>(createData.ptr(), | ||
| 114 | ✗ | boost::ref(data)); | |
| 115 | } | ||
| 116 | ✗ | return ResidualModel::createData(data); | |
| 117 | } | ||
| 118 | |||
| 119 | ✗ | std::shared_ptr<ResidualData> default_createData( | |
| 120 | DataCollectorAbstract* const data) { | ||
| 121 | ✗ | return this->ResidualModel::createData(data); | |
| 122 | } | ||
| 123 | |||
| 124 | ✗ | void calcCostDiff(const std::shared_ptr<CostData>& cdata, | |
| 125 | const std::shared_ptr<ResidualData>& rdata, | ||
| 126 | const std::shared_ptr<ActivationData>& adata, | ||
| 127 | const bool update_u = true) override { | ||
| 128 | ✗ | if (boost::python::override calcCostDiff = | |
| 129 | this->get_override("calcCostDiff")) { | ||
| 130 | ✗ | return bp::call<void>(calcCostDiff.ptr(), boost::ref(cdata), | |
| 131 | ✗ | boost::ref(rdata), boost::ref(adata), update_u); | |
| 132 | } | ||
| 133 | ✗ | return ResidualModel::calcCostDiff(cdata, rdata, adata, update_u); | |
| 134 | } | ||
| 135 | |||
| 136 | ✗ | void default_calcCostDiff(const std::shared_ptr<CostData>& cdata, | |
| 137 | const std::shared_ptr<ResidualData>& rdata, | ||
| 138 | const std::shared_ptr<ActivationData>& adata, | ||
| 139 | const bool update_u) { | ||
| 140 | ✗ | return this->ResidualModel::calcCostDiff(cdata, rdata, adata, update_u); | |
| 141 | } | ||
| 142 | |||
| 143 | ✗ | void default_calcCostDiff_noupdate_u( | |
| 144 | const std::shared_ptr<CostData>& cdata, | ||
| 145 | const std::shared_ptr<ResidualData>& rdata, | ||
| 146 | const std::shared_ptr<ActivationData>& adata) { | ||
| 147 | ✗ | return this->ResidualModel::calcCostDiff(cdata, rdata, adata); | |
| 148 | } | ||
| 149 | |||
| 150 | template <typename NewScalar> | ||
| 151 | ✗ | ResidualModelAbstractTpl_wrap<NewScalar> cast() const { | |
| 152 | typedef ResidualModelAbstractTpl_wrap<NewScalar> ReturnType; | ||
| 153 | ✗ | ReturnType ret(state_->template cast<NewScalar>(), nr_, nu_, q_dependent_, | |
| 154 | ✗ | v_dependent_, u_dependent_); | |
| 155 | ✗ | return ret; | |
| 156 | } | ||
| 157 | }; | ||
| 158 | |||
| 159 | } // namespace python | ||
| 160 | } // namespace crocoddyl | ||
| 161 | |||
| 162 | #endif // BINDINGS_PYTHON_CROCODDYL_CORE_RESIDUAL_BASE_HPP_ | ||
| 163 |