9#ifndef CROCODDYL_UTILS_CONVERSIONS_HPP_ 10#define CROCODDYL_UTILS_CONVERSIONS_HPP_ 15#ifdef CROCODDYL_WITH_CODEGEN 16#include <cppad/cg/support/cppadcg_eigen.hpp> 17#include <cppad/cppad.hpp> 20#include "crocoddyl/core/mathbase.hpp" 24template <
typename Scalar>
26 typedef typename std::conditional<std::is_floating_point<Scalar>::value,
27 Scalar,
double>::type type;
31template <
typename NewScalar,
typename Scalar>
32static typename std::enable_if<std::is_floating_point<NewScalar>::value &&
33 std::is_floating_point<Scalar>::value,
35scalar_cast(
const Scalar& x) {
36 return static_cast<NewScalar
>(x);
39template <
typename NewScalar,
typename Scalar,
40 template <
typename>
class ItemTpl>
41std::vector<ItemTpl<NewScalar>> vector_cast(
42 const std::vector<ItemTpl<Scalar>>& in) {
43 std::vector<ItemTpl<NewScalar>> out;
44 out.reserve(in.size());
45 for (
const auto& obj : in) {
46 out.push_back(obj.template cast<NewScalar>());
51template <
typename NewScalar,
typename Scalar,
52 template <
typename>
class ItemTpl>
53std::vector<std::shared_ptr<ItemTpl<NewScalar>>> vector_cast(
54 const std::vector<std::shared_ptr<ItemTpl<Scalar>>>& in) {
55 std::vector<std::shared_ptr<ItemTpl<NewScalar>>> out;
56 out.reserve(in.size());
57 for (
const auto& obj : in) {
58 out.push_back(std::static_pointer_cast<ItemTpl<NewScalar>>(
59 obj->template cast<NewScalar>()));
66#ifdef CROCODDYL_WITH_CODEGEN 73struct cast_impl<CppAD::AD<CppAD::cg::CG<double>>, float> {
74 EIGEN_DEVICE_FUNC
static inline float run(
75 const CppAD::AD<CppAD::cg::CG<double>>& x) {
79 return static_cast<float>(CppAD::Value(x).getValue());
84struct cast_impl<CppAD::AD<CppAD::cg::CG<double>>, double> {
85 EIGEN_DEVICE_FUNC
static inline double run(
86 const CppAD::AD<CppAD::cg::CG<double>>& x) {
87 return CppAD::Value(x).getValue();
92struct cast_impl<CppAD::AD<CppAD::cg::CG<float>>, float> {
93 EIGEN_DEVICE_FUNC
static inline float run(
94 const CppAD::AD<CppAD::cg::CG<float>>& x) {
95 return CppAD::Value(x).getValue();
100struct cast_impl<CppAD::AD<CppAD::cg::CG<float>>, double> {
101 EIGEN_DEVICE_FUNC
static inline double run(
102 const CppAD::AD<CppAD::cg::CG<float>>& x) {
106 return static_cast<float>(CppAD::Value(x).getValue());
113struct cast_impl<CppAD::AD<CppAD::cg::CG<float>>,
114 CppAD::AD<CppAD::cg::CG<double>>> {
115 EIGEN_DEVICE_FUNC
static inline CppAD::AD<CppAD::cg::CG<double>> run(
116 const CppAD::AD<CppAD::cg::CG<float>>& x) {
117 return CppAD::AD<CppAD::cg::CG<double>>(
118 CppAD::cg::CG<double>(CppAD::Value(x).getValue()));
125struct cast_impl<CppAD::AD<CppAD::cg::CG<double>>,
126 CppAD::AD<CppAD::cg::CG<float>>> {
127 EIGEN_DEVICE_FUNC
static inline CppAD::AD<CppAD::cg::CG<float>> run(
128 const CppAD::AD<CppAD::cg::CG<double>>& x) {
129 return CppAD::AD<CppAD::cg::CG<float>>(
130 CppAD::cg::CG<float>(
static_cast<float>(CppAD::Value(x).getValue())));
140template <
typename NewScalar,
typename Scalar>
141static typename std::enable_if<
142 std::is_floating_point<Scalar>::value &&
143 (std::is_same<NewScalar, CppAD::AD<CppAD::cg::CG<double>>>::value ||
144 std::is_same<NewScalar, CppAD::AD<CppAD::cg::CG<float>>>::value),
146scalar_cast(
const Scalar& x) {
147 return static_cast<NewScalar
>(x);
151template <
typename NewScalar,
typename Scalar>
152static inline typename std::enable_if<std::is_floating_point<Scalar>::value,
154scalar_cast(
const CppAD::AD<CppAD::cg::CG<Scalar>>& x) {
155 return static_cast<NewScalar
>(CppAD::Value(x).getValue());