9 #ifndef CROCODDYL_UTILS_CONVERSIONS_HPP_
10 #define CROCODDYL_UTILS_CONVERSIONS_HPP_
14 #ifdef CROCODDYL_WITH_CODEGEN
15 #include <cppad/cg/support/cppadcg_eigen.hpp>
16 #include <cppad/cppad.hpp>
19 #include "crocoddyl/core/mathbase.hpp"
23 template <
typename Scalar>
25 typedef typename std::conditional<std::is_floating_point<Scalar>::value,
26 Scalar,
double>::type type;
30 template <
typename NewScalar,
typename Scalar>
31 static typename std::enable_if<std::is_floating_point<NewScalar>::value &&
32 std::is_floating_point<Scalar>::value,
34 scalar_cast(
const Scalar& x) {
35 return static_cast<NewScalar
>(x);
38 template <
typename NewScalar,
typename Scalar,
39 template <
typename>
class ItemTpl>
40 std::vector<ItemTpl<NewScalar>> vector_cast(
41 const std::vector<ItemTpl<Scalar>>& in) {
42 std::vector<ItemTpl<NewScalar>> out;
43 out.reserve(in.size());
44 for (
const auto& obj : in) {
45 out.push_back(obj.template cast<NewScalar>());
50 template <
typename NewScalar,
typename Scalar,
51 template <
typename>
class ItemTpl>
52 std::vector<std::shared_ptr<ItemTpl<NewScalar>>> vector_cast(
53 const std::vector<std::shared_ptr<ItemTpl<Scalar>>>& in) {
54 std::vector<std::shared_ptr<ItemTpl<NewScalar>>> out;
55 out.reserve(in.size());
56 for (
const auto& obj : in) {
57 out.push_back(std::static_pointer_cast<ItemTpl<NewScalar>>(
58 obj->template cast<NewScalar>()));
65 #ifdef CROCODDYL_WITH_CODEGEN
72 struct cast_impl<CppAD::AD<CppAD::cg::CG<double>>, float> {
73 EIGEN_DEVICE_FUNC
static inline float run(
74 const CppAD::AD<CppAD::cg::CG<double>>& x) {
78 return static_cast<float>(CppAD::Value(x).getValue());
83 struct cast_impl<CppAD::AD<CppAD::cg::CG<double>>, double> {
84 EIGEN_DEVICE_FUNC
static inline double run(
85 const CppAD::AD<CppAD::cg::CG<double>>& x) {
86 return CppAD::Value(x).getValue();
91 struct cast_impl<CppAD::AD<CppAD::cg::CG<float>>, float> {
92 EIGEN_DEVICE_FUNC
static inline float run(
93 const CppAD::AD<CppAD::cg::CG<float>>& x) {
94 return CppAD::Value(x).getValue();
99 struct cast_impl<CppAD::AD<CppAD::cg::CG<float>>, double> {
100 EIGEN_DEVICE_FUNC
static inline double run(
101 const CppAD::AD<CppAD::cg::CG<float>>& x) {
105 return static_cast<float>(CppAD::Value(x).getValue());
112 struct cast_impl<CppAD::AD<CppAD::cg::CG<float>>,
113 CppAD::AD<CppAD::cg::CG<double>>> {
114 EIGEN_DEVICE_FUNC
static inline CppAD::AD<CppAD::cg::CG<double>> run(
115 const CppAD::AD<CppAD::cg::CG<float>>& x) {
116 return CppAD::AD<CppAD::cg::CG<double>>(
117 CppAD::cg::CG<double>(CppAD::Value(x).getValue()));
124 struct cast_impl<CppAD::AD<CppAD::cg::CG<double>>,
125 CppAD::AD<CppAD::cg::CG<float>>> {
126 EIGEN_DEVICE_FUNC
static inline CppAD::AD<CppAD::cg::CG<float>> run(
127 const CppAD::AD<CppAD::cg::CG<double>>& x) {
128 return CppAD::AD<CppAD::cg::CG<float>>(
129 CppAD::cg::CG<float>(
static_cast<float>(CppAD::Value(x).getValue())));
136 namespace crocoddyl {
139 template <
typename NewScalar,
typename Scalar>
140 static typename std::enable_if<
141 std::is_floating_point<Scalar>::value &&
142 (std::is_same<NewScalar, CppAD::AD<CppAD::cg::CG<double>>>::value ||
143 std::is_same<NewScalar, CppAD::AD<CppAD::cg::CG<float>>>::value),
145 scalar_cast(
const Scalar& x) {
146 return static_cast<NewScalar
>(x);
150 template <
typename NewScalar,
typename Scalar>
151 static inline typename std::enable_if<std::is_floating_point<Scalar>::value,
153 scalar_cast(
const CppAD::AD<CppAD::cg::CG<Scalar>>& x) {
154 return static_cast<NewScalar
>(CppAD::Value(x).getValue());