9#ifndef CROCODDYL_MACROS_HPP_ 10#define CROCODDYL_MACROS_HPP_ 16#ifdef CROCODDYL_WITH_CODEGEN 17#include <cppad/cg/support/cppadcg_eigen.hpp> 18#include <cppad/cppad.hpp> 21#define DEFAULT_SCALAR double 22typedef double Float64;
24#ifdef CROCODDYL_WITH_CODEGEN 25typedef CppAD::cg::CG<Float64> CGFloat64;
26typedef CppAD::AD<CGFloat64> ADFloat64;
29#if __cplusplus <= 201103L 31template <
typename T,
typename... Args>
32std::unique_ptr<T> make_unique(Args&&... args) {
33 return std::unique_ptr<T>(
new T(std::forward<Args>(args)...));
38#ifdef CROCODDYL_WITH_CODEGEN 39#define CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(class_name) \ 40 extern template class CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 42 extern template class CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 44 extern template class CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 45 class_name<ADFloat64>; 47#define CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT(class_name) \ 48 extern template struct CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 50 extern template struct CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 52 extern template struct CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 53 class_name<ADFloat64>; 55#define CROCODDYL_DECLARE_FLOATINGPOINT_EXTERN_TEMPLATE_CLASS(class_name) \ 56 extern template class CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 58 extern template class CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 61#define CROCODDYL_DECLARE_FLOATINGPOINT_EXTERN_TEMPLATE_STRUCT(class_name) \ 62 extern template struct CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 64 extern template struct CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 67#define CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(class_name) \ 68 extern template class CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 70 extern template class CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 73#define CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT(class_name) \ 74 extern template struct CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 76 extern template struct CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 79#define CROCODDYL_DECLARE_FLOATINGPOINT_EXTERN_TEMPLATE_CLASS(class_name) \ 80 extern template class CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 82 extern template class CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 85#define CROCODDYL_DECLARE_FLOATINGPOINT_EXTERN_TEMPLATE_STRUCT(class_name) \ 86 extern template struct CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 88 extern template struct CROCODDYL_EXPLICIT_INSTANTIATION_DECLARATION_DLLAPI \ 92#ifdef CROCODDYL_WITH_CODEGEN 93#define CROCODDYL_BASE_CAST(base_class, class) \ 94 template <typename Scalar> \ 95 std::shared_ptr<class<Scalar>> cast() const { \ 96 return std::static_pointer_cast<class<Scalar>>(cast_impl<Scalar>()); \ 97 } template <typename Scalar> \ 98 std::shared_ptr<base_class> cast_impl() const { \ 99 if (typeid(Scalar) == typeid(double)) { \ 100 return cloneAsDouble(); \ 101 } else if (typeid(Scalar) == typeid(float)) { \ 102 return cloneAsFloat(); \ 103 } else if (typeid(Scalar) == typeid(ADFloat64)) { \ 104 return cloneAsADDouble(); \ 106 std::cout << "Unsupported casting: casting to double as default" \ 108 return cloneAsDouble(); \ 112 virtual std::shared_ptr<base_class> cloneAsDouble() const = 0; \ 113 virtual std::shared_ptr<base_class> cloneAsFloat() const = 0; \ 114 virtual std::shared_ptr<base_class> cloneAsADDouble() const = 0; 127#define CROCODDYL_DERIVED_CAST(base_class, derived_class) \ 128 template <typename NewScalar> \ 129 explicit derived_class(const derived_class<NewScalar>& other) \ 130 : derived_class(std::move(other.template cast<Scalar>())) {} \ 132 std::shared_ptr<base_class> cloneAsDouble() const override { \ 133 return std::make_shared<derived_class<double>>(*this); \ 135 std::shared_ptr<base_class> cloneAsFloat() const override { \ 136 return std::make_shared<derived_class<float>>(*this); \ 138 std::shared_ptr<base_class> cloneAsADDouble() const override { \ 139 return std::make_shared<derived_class<ADFloat64>>(*this); \ 142#define CROCODDYL_DERIVED_FLOATINGPOINT_CAST(base_class, derived_class) \ 143 template <typename NewScalar> \ 144 explicit derived_class(const derived_class<NewScalar>& other) \ 145 : derived_class(std::move(other.template cast<Scalar>())) {} \ 147 std::shared_ptr<base_class> cloneAsDouble() const override { \ 148 return std::make_shared<derived_class<double>>(*this); \ 150 std::shared_ptr<base_class> cloneAsFloat() const override { \ 151 return std::make_shared<derived_class<float>>(*this); \ 153 std::shared_ptr<base_class> cloneAsADDouble() const override { \ 154 std::cout << "Unsupported casting: retuning to double as default" \ 156 return cloneAsDouble(); \ 159#define CROCODDYL_BASE_DERIVED_CAST(base_class, derived_class) \ 161 std::shared_ptr<base_class> cloneAsDouble() const override { \ 162 return std::shared_ptr<base_class>(nullptr); \ 164 std::shared_ptr<base_class> cloneAsFloat() const override { \ 165 return std::shared_ptr<base_class>(nullptr); \ 167 std::shared_ptr<base_class> cloneAsADDouble() const override { \ 168 return std::shared_ptr<base_class>(nullptr); \ 171#define CROCODDYL_BASE_DERIVED_FLOATINGPOINT_CAST(base_class, derived_class) \ 173 std::shared_ptr<base_class> cloneAsDouble() const override { \ 174 return std::shared_ptr<base_class>(nullptr); \ 176 std::shared_ptr<base_class> cloneAsFloat() const override { \ 177 return std::shared_ptr<base_class>(nullptr); \ 179 std::shared_ptr<base_class> cloneAsADDouble() const override { \ 180 return std::shared_ptr<base_class>(nullptr); \ 183#define CROCODDYL_INNER_DERIVED_CAST(base_class, inner_class, derived_class) \ 184 template <typename NewScalar> \ 185 explicit derived_class( \ 186 const typename inner_class<NewScalar>::derived_class& other) { \ 187 *this = other.template cast<Scalar>(); 191 std::shared_ptr<base_class> cloneAsDouble() const override { \ 192 return std::make_shared<typename inner_class<double>::derived_class>( \ 193 this->template cast<double>()); \ 195 std::shared_ptr<base_class> cloneAsFloat() const override { \ 196 return std::make_shared<typename inner_class<float>::derived_class>( \ 197 this->template cast<float>()); \ 199 std::shared_ptr<base_class> cloneAsADDouble() const override { \ 200 return std::make_shared<typename inner_class<ADFloat64>::derived_class>( \ 201 this->template cast<ADFloat64>()); \ 204#define CROCODDYL_BASE_CAST(base_class, class) \ 205 template <typename Scalar> \ 206 std::shared_ptr<class<Scalar>> cast() const { \ 207 return std::static_pointer_cast<class<Scalar>>(cast_impl<Scalar>()); \ 208 } template <typename Scalar> \ 209 std::shared_ptr<base_class> cast_impl() const { \ 210 if (typeid(Scalar) == typeid(double)) { \ 211 return cloneAsDouble(); \ 212 } else if (typeid(Scalar) == typeid(float)) { \ 213 return cloneAsFloat(); \ 215 std::cout << "Unsupported casting: casting to double as default" \ 217 return cloneAsDouble(); \ 221 virtual std::shared_ptr<base_class> cloneAsDouble() const = 0; \ 222 virtual std::shared_ptr<base_class> cloneAsFloat() const = 0; 235#define CROCODDYL_DERIVED_CAST(base_class, derived_class) \ 236 template <typename NewScalar> \ 237 explicit derived_class(const derived_class<NewScalar>& other) \ 238 : derived_class(std::move(other.template cast<Scalar>())) {} \ 240 std::shared_ptr<base_class> cloneAsDouble() const override { \ 241 return std::make_shared<derived_class<double>>(*this); \ 243 std::shared_ptr<base_class> cloneAsFloat() const override { \ 244 return std::make_shared<derived_class<float>>(*this); \ 247#define CROCODDYL_DERIVED_FLOATINGPOINT_CAST(base_class, derived_class) \ 248 template <typename NewScalar> \ 249 explicit derived_class(const derived_class<NewScalar>& other) \ 250 : derived_class(std::move(other.template cast<Scalar>())) {} \ 252 std::shared_ptr<base_class> cloneAsDouble() const override { \ 253 return std::make_shared<derived_class<double>>(*this); \ 255 std::shared_ptr<base_class> cloneAsFloat() const override { \ 256 return std::make_shared<derived_class<float>>(*this); \ 259#define CROCODDYL_BASE_DERIVED_CAST(base_class, derived_class) \ 261 std::shared_ptr<base_class> cloneAsDouble() const override { \ 262 return std::shared_ptr<base_class>(nullptr); \ 264 std::shared_ptr<base_class> cloneAsFloat() const override { \ 265 return std::shared_ptr<base_class>(nullptr); \ 268#define CROCODDYL_BASE_DERIVED_FLOATINGPOINT_CAST(base_class, derived_class) \ 270 std::shared_ptr<base_class> cloneAsDouble() const override { \ 271 return std::shared_ptr<base_class>(nullptr); \ 273 std::shared_ptr<base_class> cloneAsFloat() const override { \ 274 return std::shared_ptr<base_class>(nullptr); \ 277#define CROCODDYL_INNER_DERIVED_CAST(base_class, inner_class, derived_class) \ 278 template <typename NewScalar> \ 279 explicit derived_class( \ 280 const typename inner_class<NewScalar>::derived_class& other) { \ 281 *this = other.template cast<Scalar>(); 285 std::shared_ptr<base_class> cloneAsDouble() const override { \ 286 return std::make_shared<typename inner_class<double>::derived_class>( \ 287 this->template cast<double>()); \ 289 std::shared_ptr<base_class> cloneAsFloat() const override { \ 290 return std::make_shared<typename inner_class<float>::derived_class>( \ 291 this->template cast<float>()); \