pinocchio  3.6.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
cppadcg.hpp
1 //
2 // Copyright (c) 2018-2023 CNRS INRIA
3 //
4 
5 #ifndef __pinocchio_codegen_ccpadcg_hpp__
6 #define __pinocchio_codegen_ccpadcg_hpp__
7 
8 #define PINOCCHIO_WITH_CPPADCG_SUPPORT
9 
10 #include "pinocchio/math/fwd.hpp"
11 
12 #include <cmath>
13 #include <boost/mpl/int.hpp>
14 #include <cppad/cg/support/cppadcg_eigen.hpp>
15 
16 #include "pinocchio/autodiff/cppad.hpp"
17 
18 namespace boost
19 {
20  namespace math
21  {
22  namespace constants
23  {
24  namespace detail
25  {
26  template<typename Scalar>
27  struct constant_pi<CppAD::cg::CG<Scalar>> : constant_pi<Scalar>
28  {
29  typedef CppAD::cg::CG<Scalar> CGScalar;
30 
31  template<int N>
32  static inline CGScalar get(const mpl::int_<N> & n)
33  {
34  return CGScalar(constant_pi<Scalar>::get(n));
35  }
36 
37 #if BOOST_VERSION >= 107700
38  template<class T, T value>
39  static inline CGScalar get(const std::integral_constant<T, value> & n)
40  {
41  return CGScalar(constant_pi<Scalar>::get(n));
42  }
43 #else
44  template<class T, T value>
45  static inline CGScalar get(const boost::integral_constant<T, value> & n)
46  {
47  return CGScalar(constant_pi<Scalar>::get(n));
48  }
49 #endif
50  };
51  } // namespace detail
52  } // namespace constants
53  } // namespace math
54 } // namespace boost
55 
56 namespace Eigen
57 {
58  namespace internal
59  {
60  // Specialization of Eigen::internal::cast_impl for CppAD input types
61  template<typename Scalar>
62  struct cast_impl<CppAD::cg::CG<Scalar>, Scalar>
63  {
64 #if EIGEN_VERSION_AT_LEAST(3, 2, 90)
65  EIGEN_DEVICE_FUNC
66 #endif
67  static inline Scalar run(const CppAD::cg::CG<Scalar> & x)
68  {
69  return x.getValue();
70  }
71  };
72 
73  // Specialization of Eigen::internal::cast_impl for CppAD input types
74  template<typename Scalar>
75  struct cast_impl<CppAD::AD<CppAD::cg::CG<Scalar>>, Scalar>
76  {
77 #if EIGEN_VERSION_AT_LEAST(3, 2, 90)
78  EIGEN_DEVICE_FUNC
79 #endif
80  static inline Scalar run(const CppAD::AD<CppAD::cg::CG<Scalar>> & x)
81  {
82  return CppAD::Value(x).getValue();
83  }
84  };
85 
86  } // namespace internal
87 } // namespace Eigen
88 
89 namespace CppAD
90 {
91  namespace cg
92  {
93  template<class Scalar>
94  bool isfinite(const CG<Scalar> & x)
95  {
96  return std::isfinite(x.getValue());
97  }
98  } // namespace cg
99 } // namespace CppAD
100 
101 namespace pinocchio
102 {
103  template<typename Scalar>
104  struct TaylorSeriesExpansion<CppAD::cg::CG<Scalar>>
105  {
107  typedef CppAD::cg::CG<Scalar> CGScalar;
108 
109  template<int degree>
110  static CGScalar precision()
111  {
112  return CGScalar(Base::template precision<degree>());
113  }
114  };
115 
116  template<typename NewScalar, typename Scalar>
117  struct ScalarCast<NewScalar, CppAD::cg::CG<Scalar>>
118  {
119  static NewScalar cast(const CppAD::cg::CG<Scalar> & cg_value)
120  {
121  return static_cast<NewScalar>(cg_value.getValue());
122  }
123  };
124 
125 } // namespace pinocchio
126 
127 #endif // #ifndef __pinocchio_codegen_ccpadcg_hpp__
Main pinocchio namespace.
Definition: treeview.dox:11
Cast scalar type from type FROM to type TO.
Definition: fwd.hpp:106
static Scalar precision()
Computes the expected tolerance of the argument of a Taylor series expansion for a certain degree acc...