5 #include "pinocchio/codegen/cppadcg.hpp" 7 #include "pinocchio/multibody/model.hpp" 8 #include "pinocchio/parsers/sample-models.hpp" 12 #include <boost/test/unit_test.hpp> 13 #include <boost/utility/binary.hpp> 15 BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
19 using namespace CppAD;
22 typedef CG<double> CGD;
30 CppAD::vector<ADCG>
x(2);
36 CppAD::vector<ADCG>
y(1);
39 ADCG
a = x[0] / 1. + x[1] * x[1];
51 CodeHandler<double> handler;
53 CppAD::vector<CGD> indVars(2);
54 handler.makeVariables(indVars);
56 CppAD::vector<CGD> jac = fun.SparseJacobian(indVars);
58 LanguageC<double> langC(
"double");
59 LangCDefaultVariableNameGenerator<double> nameGen;
61 std::ostringstream code;
62 handler.generateCode(code, langC, jac, nameGen);
63 std::cout << code.str();
68 typedef CppAD::cg::CG<double> CGD;
69 typedef CppAD::AD<CGD> ADCG;
71 typedef Eigen::Matrix<ADCG,Eigen::Dynamic,1> ADCGVector;
73 ADCGVector vec_zero(ADCGVector::Zero(100));
74 BOOST_CHECK(vec_zero.isZero());
76 ADCGVector vec_ones(10);
77 vec_ones.fill((ADCG)1);
78 BOOST_CHECK(vec_ones.isOnes());
87 ADCGVector vec_nan(10);
88 vec_nan.fill((ADCG)NAN);
93 ADCG val_minus_one(-1.);
94 ADCG val_abs(abs(val_minus_one));
95 ADCGVector vec_minus_one(10); vec_minus_one.fill(val_minus_one);
96 vec_minus_one.array().abs();
98 BOOST_CHECK(!vec_ones.isUnitary());
105 typedef CppAD::cg::CG<double> CGScalar;
106 typedef CppAD::AD<double> ADScalar;
107 typedef CppAD::AD<float> ADFloat;
113 CGSE3 cg_M = M.
cast<CGScalar>();
114 BOOST_CHECK(cg_M.cast<
double>().isApprox(M));
118 BOOST_CHECK(axis.isUnitary());
123 CGScalar cg_value; cg_value = -1.;
124 ADScalar ad_value; ad_value = -1.;
125 ADFloat ad_float; ad_float = -1.;
130 CPPAD_TESTVECTOR(ADScalar) ad_x(3);
131 CGJointModelPrismaticUnaligned cg_jmodel_prismatic(axis.cast<CGScalar>());
136 CGModel cg_model = model.
cast<CGScalar>();
139 CppAD::AD<double> ad_value(-1.);
142 CppAD::cg::CG<double> cg_value(-1.);
150 using namespace CppAD;
154 typedef CG<double> CGD;
155 typedef AD<CGD> ADCG;
157 typedef AD<double> ADScalar;
164 std::vector<ADCG>
x(2);
168 std::vector<ADCG>
y(1);
171 ADCG
a = x[0] / 1. + x[1] * x[1];
174 ADFun<CGD> fun(x, y);
182 ModelCSourceGen<double> cgen(fun,
"model");
183 cgen.setCreateJacobian(
true);
184 cgen.setCreateForwardOne(
true);
185 cgen.setCreateReverseOne(
true);
186 cgen.setCreateReverseTwo(
true);
187 ModelLibraryCSourceGen<double> libcgen(cgen);
190 DynamicModelLibraryProcessor<double>
p(libcgen);
192 GccCompiler<double> compiler;
193 std::unique_ptr<DynamicLib<double>> dynamicLib = p.createDynamicLibrary(compiler);
196 SaveFilesModelLibraryProcessor<double> p2(libcgen);
203 std::unique_ptr<GenericModel<double>>
model = dynamicLib->model(
"model");
204 CPPAD_TESTVECTOR(
double) xv(x.size()); xv[0] = 2.5; xv[1] = 3.5;
205 CPPAD_TESTVECTOR(
double) jac = model->Jacobian(xv);
207 std::vector<ADScalar> x_ad(2);
211 std::vector<ADScalar> y_ad(1);
214 ADScalar a_ad = x_ad[0] / 1. + x_ad[1] * x_ad[1];
217 ADFun<double> ad_fun(x_ad, y_ad);
219 CPPAD_TESTVECTOR(
double) jac_ref = ad_fun.Jacobian(xv);
222 std::cout << jac[0] <<
" " << jac[1] << std::endl;
224 BOOST_CHECK(Eigen::Map<Eigen::Vector2d>(jac.data()).
isApprox(Eigen::Map<Eigen::Vector2d>(jac_ref.data())));
227 BOOST_AUTO_TEST_SUITE_END()
ModelTpl< NewScalar, Options, JointCollectionTpl > cast() const
BOOST_AUTO_TEST_CASE(test_simple_cppadcg)
bool isApprox(const Shape &s1, const Shape &s2, const FCL_REAL tol)
SE3Tpl< NewScalar, Options > cast() const
traits< SE3Tpl >::Vector3 Vector3
void humanoidRandom(ModelTpl< Scalar, Options, JointCollectionTpl > &model, bool usingFF=true)
Create a humanoid kinematic tree with 6-DOF limbs and random joint placements.