- 3.0.2 rigid body dynamics module.
kindrJITtest.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 #pragma GCC diagnostic push
9 #pragma GCC diagnostic ignored "-Wunused-parameter"
10 #pragma GCC diagnostic ignored "-Wunused-value"
11 #include <kindr/Core>
12 #pragma GCC diagnostic pop
13 
22 template <typename SCALAR>
23 Eigen::Matrix<SCALAR, 3, 1> testFunction(const Eigen::Matrix<SCALAR, 3, 1>& x)
24 {
25  kindr::EulerAnglesXyz<SCALAR> anglesIn(x);
26  kindr::EulerAnglesXyz<SCALAR> anglesTemp;
27 
28  anglesTemp.setX(3 * anglesIn.x() + 2 * anglesIn.x() * anglesIn.x() - anglesIn.y() * anglesIn.z());
29  anglesTemp.setY(anglesIn.z() + anglesIn.y() + 3);
30  anglesTemp.setZ(anglesIn.z());
31 
32  kindr::EulerAnglesXyz<SCALAR> anglesOut(anglesTemp);
33 
34  return anglesOut.toImplementation();
35 }
36 
43 template <typename SCALAR>
44 Eigen::Matrix<SCALAR, 3, 3> jacobianCheck(const Eigen::Matrix<SCALAR, 3, 1>& x)
45 {
46  Eigen::Matrix<SCALAR, 3, 3> jac;
47 
48  jac << 3 + 4 * x(0), -x(2), -x(1), 0, 1, 1, 0, 0, 1;
49 
50  return jac;
51 }
52 
53 
58 TEST(KindrJitTest, EulerAnglesTest)
59 {
60  using derivativesCppadJIT = ct::core::DerivativesCppadJIT<3, 3>;
61  using derivativesCppad = ct::core::DerivativesCppad<3, 3>;
62 
63  try
64  {
65  // create a function handle (also works for class methods, lambdas, function pointers, ...)
66  typename derivativesCppadJIT::FUN_TYPE_CG f = testFunction<derivativesCppadJIT::CG_SCALAR>;
67  typename derivativesCppad::FUN_TYPE_AD f_ad = testFunction<derivativesCppad::AD_SCALAR>;
68 
69  // initialize the Auto-Diff Codegen Jacobian
70  derivativesCppadJIT jacCG(f);
71  derivativesCppad jacAd(f_ad);
72 
73  DerivativesCppadSettings settings;
74  settings.createJacobian_ = true;
75 
76  // compile the Jacobian
77  jacCG.compileJIT(settings, "eulerAnglesTestLib");
78 
79  // create an input vector
80  Eigen::Matrix<double, 3, 1> angles;
81 
82  for (size_t i = 0; i < 10; i++)
83  {
84  // create a random input
85  angles.setRandom();
86 
87  // verify agains the analytical Jacobian
88  ASSERT_LT((jacCG.jacobian(angles) - jacobianCheck(angles)).array().abs().maxCoeff(), 1e-10);
89  ASSERT_LT((jacAd.jacobian(angles) - jacobianCheck(angles)).array().abs().maxCoeff(), 1e-10);
90  }
91  } catch (std::exception& e)
92  {
93  std::cout << "Kindr Euler angle test failed: " << e.what() << std::endl;
94  ASSERT_TRUE(false);
95  }
96 }
97 
98 
100 template <typename SCALAR>
101 Eigen::Matrix<SCALAR, 3, 1> rotateTestFunction(const Eigen::Matrix<SCALAR, 3, 1>& x)
102 {
103  kindr::EulerAnglesXyz<SCALAR> angles(x);
104 
105  // create a position and set to a trivial hard-coded value
106  kindr::Position<SCALAR, 3> pos;
107  pos.toImplementation()(0) = 1;
108  pos.toImplementation()(1) = 1;
109  pos.toImplementation()(2) = 1;
110 
111  return angles.rotate(pos).toImplementation();
112 }
113 TEST(KindrJitTest, EulerRotateTest)
114 {
115  using derivativesCppadJIT = ct::core::DerivativesCppadJIT<3, 3>;
116  try
117  {
118  // create a function handle (also works for class methods, lambdas, function pointers, ...)
119  typename derivativesCppadJIT::FUN_TYPE_CG f = rotateTestFunction<derivativesCppadJIT::CG_SCALAR>;
120 
121  // initialize the Auto-Diff Codegen Jacobian
122  derivativesCppadJIT jacCG(f);
123 
124  DerivativesCppadSettings settings;
125  settings.createJacobian_ = true;
126 
127  // compile the Jacobian
128  jacCG.compileJIT(settings, "EulerRotateTest");
129 
130  } catch (std::exception& e)
131  {
132  std::cout << "Kindr rotate test failed: " << e.what() << std::endl;
133  ASSERT_TRUE(false);
134  }
135 }
136 
137 
139 template <typename SCALAR>
140 Eigen::Matrix<SCALAR, 3, 1> kindrRotationMatrixTestFunction(const Eigen::Matrix<SCALAR, 3, 1>& x)
141 {
142  kindr::EulerAnglesXyz<SCALAR> angles(x);
143 
144  // create a position and set to a trivial hard-coded value
145  Eigen::Matrix<SCALAR, 3, 1> pos;
146  pos(0) = 1;
147  pos(1) = 1;
148  pos(2) = 1;
149 
150  Eigen::Matrix<SCALAR, 3, 3> someMatrix;
151  someMatrix.setIdentity();
152  someMatrix(0, 0) = ct::core::internal::CppADCodegenTrait::cos(x(0));
153  someMatrix(1, 1) = ct::core::internal::CppADCodegenTrait::cos(x(0));
154  someMatrix(0, 1) = -ct::core::internal::CppADCodegenTrait::sin(x(0));
155  someMatrix(1, 0) = ct::core::internal::CppADCodegenTrait::sin(x(0));
156 
157  kindr::RotationMatrix<SCALAR> rotMat(someMatrix);
158 
159  return rotMat.toImplementation() * pos;
160 }
161 TEST(KindrJitTest, RotationMatrixTest)
162 {
163  using derivativesCppadJIT = ct::core::DerivativesCppadJIT<3, 3>;
164 
165  try
166  {
167  // create a function handle (also works for class methods, lambdas, function pointers, ...)
168  typename derivativesCppadJIT::FUN_TYPE_CG f = kindrRotationMatrixTestFunction<derivativesCppadJIT::CG_SCALAR>;
169 
170  // initialize the Auto-Diff Codegen Jacobian
171  derivativesCppadJIT jacCG(f);
172 
173  DerivativesCppadSettings settings;
174  settings.createJacobian_ = true;
175  settings.createForwardZero_ = true;
176 
177  // compile the Jacobian
178  jacCG.compileJIT(settings, "rotationMatrixTestLib");
179 
180  } catch (std::exception& e)
181  {
182  std::cout << "Kindr rotation matrix test failed: " << e.what() << std::endl;
183  ASSERT_TRUE(false);
184  }
185 }
186 
187 
189 template <typename SCALAR>
190 Eigen::Matrix<SCALAR, 3, 1> convertRotationMatrixToKindrEulerTestFunction(const Eigen::Matrix<SCALAR, 3, 1>& x)
191 {
192  Eigen::Matrix<SCALAR, 3, 3> someMatrix;
193  someMatrix.setIdentity();
194  someMatrix(0, 0) = ct::core::internal::CppADCodegenTrait::cos(x(0));
195  someMatrix(1, 1) = ct::core::internal::CppADCodegenTrait::cos(x(0));
196  someMatrix(0, 1) = -ct::core::internal::CppADCodegenTrait::sin(x(0));
197  someMatrix(1, 0) = ct::core::internal::CppADCodegenTrait::sin(x(0));
198 
199  kindr::RotationMatrix<SCALAR> rotMat(someMatrix);
200 
201  kindr::EulerAnglesXyz<SCALAR> euler(rotMat); //<-- fails
202 
203  return euler.toImplementation();
204 }
205 TEST(KindrJitTest, DISABLED_KindrRotationMatrixToEulerAnglesXyzTest)
206 {
207  using derivativesCppadJIT = ct::core::DerivativesCppadJIT<3, 3>;
208 
209  try
210  {
211  // create a function handle (also works for class methods, lambdas, function pointers, ...)
212  typename derivativesCppadJIT::FUN_TYPE_CG f =
213  convertRotationMatrixToKindrEulerTestFunction<derivativesCppadJIT::CG_SCALAR>;
214 
215  // initialize the Auto-Diff Codegen Jacobian
216  derivativesCppadJIT jacCG(f);
217 
218  DerivativesCppadSettings settings;
219  settings.createJacobian_ = true;
220  settings.createForwardZero_ = true;
221 
222  // compile the Jacobian
223  jacCG.compileJIT(settings, "rotationMatrixToEulerTestLib");
224 
225  } catch (std::exception& e)
226  {
227  std::cout << "KindrRotationMatrixToEulerXyzTest test failed: " << e.what() << std::endl;
228  ASSERT_TRUE(false);
229  }
230 }
231 
232 
234 template <typename SCALAR>
235 Eigen::Matrix<SCALAR, 3, 1> convertRotationMatrixToEigenEulerTestFunction(const Eigen::Matrix<SCALAR, -1, 1>& x)
236 {
237  Eigen::Matrix<SCALAR, 3, 3> someMatrix(3, 3);
238  someMatrix.setIdentity();
239  someMatrix(0, 0) = ct::core::internal::CppADCodegenTrait::cos(x(0));
240  someMatrix(1, 1) = ct::core::internal::CppADCodegenTrait::cos(x(0));
241  someMatrix(0, 1) = -ct::core::internal::CppADCodegenTrait::sin(x(0));
242  someMatrix(1, 0) = ct::core::internal::CppADCodegenTrait::sin(x(0));
243 
244  Eigen::Matrix<SCALAR, 3, 1> eigenEulerAngles = someMatrix.eulerAngles(0, 1, 2); // <-- fails
245 
246  return eigenEulerAngles;
247 }
248 TEST(KindrJitTest, DISABLED_EigenRotationMatrixToEigenEulerAnglesTest)
249 {
250  using derivativesCppadJIT = ct::core::DerivativesCppadJIT<3, 3>;
251 
252  try
253  {
254  // create a function handle (also works for class methods, lambdas, function pointers, ...)
255  typename derivativesCppadJIT::FUN_TYPE_CG f =
256  convertRotationMatrixToEigenEulerTestFunction<derivativesCppadJIT::CG_SCALAR>;
257 
258  // initialize the Auto-Diff Codegen Jacobian
259  derivativesCppadJIT jacCG(f);
260 
261  DerivativesCppadSettings settings;
262  settings.createJacobian_ = true;
263  settings.createForwardZero_ = true;
264 
265  // compile the Jacobian
266  // Eigen::VectorXd test (3); test << 0.0, 0.0, 0.0;
267  // jacCG.forwardZero(test); // <--fails
268  jacCG.compileJIT(settings, "EigenRotationMatrixToEigenEulerAnglesTest");
269 
270  } catch (std::exception& e)
271  {
272  std::cout << "EigenRotationMatrixToEigenEulerAnglesTest test failed: " << e.what() << std::endl;
273  ASSERT_TRUE(false);
274  }
275 }
276 
277 /* //TODO: bring back this test
278 #if EIGEN_VERSION_AT_LEAST(3, 2, 7) // prior versions have a bug
280 template <typename SCALAR>
281 Eigen::Matrix<SCALAR, 3, 1> convertRotationMatrixToAngleAxisTestFunction(const Eigen::Matrix<SCALAR, -1, 1>& x)
282 {
283  Eigen::Matrix<SCALAR, 3, 3> someMatrix(3, 3);
284  someMatrix.setIdentity();
285  someMatrix(0, 0) = ct::core::internal::CppADCodegenTrait::cos(x(0));
286  someMatrix(1, 1) = ct::core::internal::CppADCodegenTrait::cos(x(0));
287  someMatrix(0, 1) = -ct::core::internal::CppADCodegenTrait::sin(x(0));
288  someMatrix(1, 0) = ct::core::internal::CppADCodegenTrait::sin(x(0));
289 
290  Eigen::AngleAxis<SCALAR> angleAxis(someMatrix);
291 
292  return angleAxis.axis();
293 }
294 
295 TEST(KindrJitTest, DISABLED_EigenRotationMatrixToAngleAxisTest)
296 {
297  using derivativesCppadJIT = ct::core::DerivativesCppadJIT<3, 3>;
298 
299  try
300  {
301  // create a function handle (also works for class methods, lambdas, function pointers, ...)
302  typename derivativesCppadJIT::FUN_TYPE_CG f =
303  convertRotationMatrixToAngleAxisTestFunction<derivativesCppadJIT::CG_SCALAR>;
304 
305  // initialize the Auto-Diff Codegen Jacobian
306  derivativesCppadJIT jacCG(f);
307 
308  DerivativesCppadSettings settings;
309  settings.createJacobian_ = true;
310 
311  // compile the Jacobian
312  jacCG.compileJIT(settings, "EigenRotationMatrixToAngleAxisTest");
313 
314  } catch (std::exception& e)
315  {
316  std::cout << "EigenRotationMatrixToAngleAxisTest test failed: " << e.what() << std::endl;
317  ASSERT_TRUE(false);
318  }
319 }
320 #endif
321 */
322 
324 template <typename SCALAR>
325 Eigen::Matrix<SCALAR, -1, 1> frobeniusNormTestFunction(const Eigen::Matrix<SCALAR, -1, 1>& x)
326 {
327  Eigen::Matrix<SCALAR, 3, 3> someMatrix(3, 3);
328  someMatrix.setIdentity();
329  someMatrix(0, 0) = ct::core::internal::CppADCodegenTrait::cos(x(0));
330  someMatrix(1, 1) = ct::core::internal::CppADCodegenTrait::cos(x(0));
331  someMatrix(0, 1) = -ct::core::internal::CppADCodegenTrait::sin(x(0));
332  someMatrix(1, 0) = ct::core::internal::CppADCodegenTrait::sin(x(0));
333 
334  Eigen::Matrix<SCALAR, 1, 1> someData;
335  someData(0, 0) = someMatrix.norm(); // per default Eigen returns the Frobenius when calling norm() on a matrix.
336  return someData;
337 }
338 TEST(KindrJitTest, FrobeniusNormTest)
339 {
340  using derivativesCppadJIT = ct::core::DerivativesCppadJIT<3, 1>;
341 
342  try
343  {
344  // create a function handle (also works for class methods, lambdas, function pointers, ...)
345  typename derivativesCppadJIT::FUN_TYPE_CG f = frobeniusNormTestFunction<derivativesCppadJIT::CG_SCALAR>;
346 
347  // initialize the Auto-Diff Codegen Jacobian
348  derivativesCppadJIT jacCG(f);
349 
350  DerivativesCppadSettings settings;
351  settings.createJacobian_ = true;
352 
353  // compile the Jacobian
354  jacCG.compileJIT(settings, "FrobeniusNormTest");
355 
356  } catch (std::exception& e)
357  {
358  std::cout << "FrobeniusNormTest test failed: " << e.what() << std::endl;
359  ASSERT_TRUE(false);
360  }
361 }
362 
363 
365 template <typename SCALAR>
366 Eigen::Matrix<SCALAR, 4, 1> convertRotationMatrixToKindrQuatTestFunction(const Eigen::Matrix<SCALAR, 3, 1>& x)
367 {
368  Eigen::Matrix<SCALAR, 3, 3> someMatrix;
369  someMatrix.setIdentity();
370  someMatrix(0, 0) = ct::core::internal::CppADCodegenTrait::cos(x(0));
371  someMatrix(1, 1) = ct::core::internal::CppADCodegenTrait::cos(x(0));
372  someMatrix(0, 1) = -ct::core::internal::CppADCodegenTrait::sin(x(0));
373  someMatrix(1, 0) = ct::core::internal::CppADCodegenTrait::sin(x(0));
374 
375  // this works
376  kindr::RotationMatrix<SCALAR> rotMat(someMatrix);
377 
378  // this does not, although it should.
379  kindr::RotationQuaternion<SCALAR> quat(rotMat); // <-- todo this fails
380 
381  Eigen::Matrix<SCALAR, 4, 1> someData;
382  someData << quat.w(), quat.x(), quat.y(), quat.z();
383 
384  return someData;
385 }
386 TEST(KindrJitTest, DISABLED_RotationMatrixToKindrRotationQuatTest)
387 {
388  using derivativesCppadJIT = ct::core::DerivativesCppadJIT<3, 4>;
389 
390  try
391  {
392  // create a function handle (also works for class methods, lambdas, function pointers, ...)
393  typename derivativesCppadJIT::FUN_TYPE_CG f =
394  convertRotationMatrixToKindrQuatTestFunction<derivativesCppadJIT::CG_SCALAR>;
395 
396  // initialize the Auto-Diff Codegen Jacobian
397  derivativesCppadJIT jacCG(f);
398 
399  DerivativesCppadSettings settings;
400  settings.createJacobian_ = true;
401  settings.createForwardZero_ = true;
402 
403  // compile the Jacobian
404  jacCG.compileJIT(settings, "convertRotationMatrixToQuatTestFunction");
405 
406  } catch (std::exception& e)
407  {
408  std::cout << "convertRotationMatrixToQuatTestFunction test failed: " << e.what() << std::endl;
409  ASSERT_TRUE(false);
410  }
411 }
412 
413 
415 template <typename SCALAR>
416 Eigen::Matrix<SCALAR, 4, 1> convertKindrRotationMatrixToEigenQuatTestFunction(const Eigen::Matrix<SCALAR, 3, 1>& x)
417 {
418  Eigen::Matrix<SCALAR, 3, 3> someMatrix;
419  someMatrix.setIdentity();
420  someMatrix(0, 0) = ct::core::internal::CppADCodegenTrait::cos(x(0));
421  someMatrix(1, 1) = ct::core::internal::CppADCodegenTrait::cos(x(0));
422  someMatrix(0, 1) = -ct::core::internal::CppADCodegenTrait::sin(x(0));
423  someMatrix(1, 0) = ct::core::internal::CppADCodegenTrait::sin(x(0));
424 
425  kindr::RotationMatrix<SCALAR> rotMat(someMatrix);
426 
427  // this does not, although it should.
428  Eigen::Quaternion<SCALAR> quat(someMatrix); // <-- todo this fails
429 
430  Eigen::Matrix<SCALAR, 4, 1> someData;
431  someData << quat.w(), quat.x(), quat.y(), quat.z();
432 
433  return someData;
434 }
435 TEST(KindrJitTest, DISABLED_KindrRotationMatrixToEigenRotationQuatTest)
436 {
437  using derivativesCppadJIT = ct::core::DerivativesCppadJIT<3, 4>;
438 
439  try
440  {
441  // create a function handle (also works for class methods, lambdas, function pointers, ...)
442  typename derivativesCppadJIT::FUN_TYPE_CG f =
443  convertKindrRotationMatrixToEigenQuatTestFunction<derivativesCppadJIT::CG_SCALAR>;
444 
445  // initialize the Auto-Diff Codegen Jacobian
446  derivativesCppadJIT jacCG(f);
447 
448  DerivativesCppadSettings settings;
449  settings.createJacobian_ = true;
450  settings.createForwardZero_ = true;
451 
452  // compile the Jacobian
453  jacCG.compileJIT(settings, "RotationMatrixToEigenRotationQuatTest");
454 
455  } catch (std::exception& e)
456  {
457  std::cout << "RotationMatrixToEigenQuatTest test failed: " << e.what() << std::endl;
458  ASSERT_TRUE(false);
459  }
460 }
461 
462 
464 template <typename SCALAR>
465 Eigen::Matrix<SCALAR, 3, 1> eigenTransformTestFunction(const Eigen::Matrix<SCALAR, 3, 1>& x)
466 {
467  Eigen::Transform<SCALAR, 3, Eigen::Affine> t;
468  t = Eigen::Translation<SCALAR, 3>(x);
469  t.rotate(Eigen::AngleAxis<SCALAR>(x(0), Eigen::Matrix<SCALAR, 3, 1>::UnitX()));
470  t.rotate(Eigen::AngleAxis<SCALAR>(x(1), Eigen::Matrix<SCALAR, 3, 1>::UnitY()));
471  t.rotate(Eigen::AngleAxis<SCALAR>(x(2), Eigen::Matrix<SCALAR, 3, 1>::UnitZ()));
472 
473  // return some random part of the eigen transform matrix
474  return (t.matrix()).template topLeftCorner<3, 1>();
475 }
476 TEST(KindrJitTest, EigenTransformTest)
477 {
478  using derivativesCppadJIT = ct::core::DerivativesCppadJIT<3, 3>;
479 
480  try
481  {
482  // create a function handle (also works for class methods, lambdas, function pointers, ...)
483  typename derivativesCppadJIT::FUN_TYPE_CG f = eigenTransformTestFunction<derivativesCppadJIT::CG_SCALAR>;
484 
485  // initialize the Auto-Diff Codegen Jacobian
486  derivativesCppadJIT jacCG(f);
487 
488  DerivativesCppadSettings settings;
489  settings.createJacobian_ = true;
490 
491  // compile the Jacobian
492  jacCG.compileJIT(settings, "eigenTransformTestLib");
493 
494  } catch (std::exception& e)
495  {
496  std::cout << "Eigen transform test failed: " << e.what() << std::endl;
497  ASSERT_TRUE(false);
498  }
499 }
500 
501 
503 template <typename SCALAR>
504 Eigen::Matrix<SCALAR, 3, 1> eigenQuaternionTestFunction(const Eigen::Matrix<SCALAR, 3, 1>& x)
505 {
506  Eigen::Quaternion<SCALAR> q;
507  q = Eigen::AngleAxis<SCALAR>(x(0), Eigen::Matrix<SCALAR, 3, 1>::UnitX()) *
508  Eigen::AngleAxis<SCALAR>(x(1), Eigen::Matrix<SCALAR, 3, 1>::UnitY()) *
509  Eigen::AngleAxis<SCALAR>(x(2), Eigen::Matrix<SCALAR, 3, 1>::UnitZ());
510 
511  // return some data
512  return q.toRotationMatrix().template topRightCorner<3, 1>();
513 }
514 TEST(KindrJitTest, EigenQuaternionTest)
515 {
516  using derivativesCppadJIT = ct::core::DerivativesCppadJIT<3, 3>;
517 
518  try
519  {
520  // create a function handle (also works for class methods, lambdas, function pointers, ...)
521  typename derivativesCppadJIT::FUN_TYPE_CG f = eigenQuaternionTestFunction<derivativesCppadJIT::CG_SCALAR>;
522 
523  // initialize the Auto-Diff Codegen Jacobian
524  derivativesCppadJIT jacCG(f);
525 
526  DerivativesCppadSettings settings;
527  settings.createJacobian_ = true;
528 
529  // compile the Jacobian
530  jacCG.compileJIT(settings, "EigenQuaternionTest");
531 
532  } catch (std::exception& e)
533  {
534  std::cout << "Eigen transform test failed: " << e.what() << std::endl;
535  ASSERT_TRUE(false);
536  }
537 }
538 
539 
541 template <typename SCALAR>
542 Eigen::Matrix<SCALAR, 4, 1> eigenQuatToKindrRotationQuatFunction(const Eigen::Matrix<SCALAR, 3, 1>& x)
543 {
544  Eigen::Quaternion<SCALAR> q;
545  q = Eigen::AngleAxis<SCALAR>(x(0), Eigen::Matrix<SCALAR, 3, 1>::UnitX()) *
546  Eigen::AngleAxis<SCALAR>(x(1), Eigen::Matrix<SCALAR, 3, 1>::UnitY()) *
547  Eigen::AngleAxis<SCALAR>(x(2), Eigen::Matrix<SCALAR, 3, 1>::UnitZ());
548 
549  kindr::RotationQuaternion<SCALAR> kindr_quat(q);
550 
551  Eigen::Matrix<SCALAR, 4, 1> someData;
552  someData << kindr_quat.w(), kindr_quat.x(), kindr_quat.y(), kindr_quat.z();
553 
554  return someData;
555 }
556 TEST(KindrJitTest, EigenQuatToKindrRotationQuatTest)
557 {
558  using derivativesCppadJIT = ct::core::DerivativesCppadJIT<3, 4>;
559 
560  try
561  {
562  // create a function handle (also works for class methods, lambdas, function pointers, ...)
563  typename derivativesCppadJIT::FUN_TYPE_CG f =
564  eigenQuatToKindrRotationQuatFunction<derivativesCppadJIT::CG_SCALAR>;
565 
566  // initialize the Auto-Diff Codegen Jacobian
567  derivativesCppadJIT jacCG(f);
568 
569  DerivativesCppadSettings settings;
570  settings.createJacobian_ = true;
571 
572  // compile the Jacobian
573  jacCG.compileJIT(settings, "EigenQuatToKindrRotationQuatTest");
574 
575  } catch (std::exception& e)
576  {
577  std::cout << "Eigen transform test failed: " << e.what() << std::endl;
578  ASSERT_TRUE(false);
579  }
580 }
581 
582 
584 template <typename SCALAR>
585 Eigen::Matrix<SCALAR, 4, 1> kindrQuaternionTestFunction(const Eigen::Matrix<SCALAR, 3, 1>& x)
586 {
587  kindr::Quaternion<SCALAR> q(x(0), x(1), x(2), x(2));
588 
589  Eigen::Matrix<SCALAR, 4, 1> res;
590  res << q.w(), q.x(), q.y(), q.z();
591 
592  return res;
593 }
594 TEST(KindrJitTest, kindrQuaternionTest)
595 {
596  using derivativesCppadJIT = ct::core::DerivativesCppadJIT<3, 4>;
597 
598  try
599  {
600  // create a function handle (also works for class methods, lambdas, function pointers, ...)
601  typename derivativesCppadJIT::FUN_TYPE_CG f = kindrQuaternionTestFunction<derivativesCppadJIT::CG_SCALAR>;
602 
603  // initialize the Auto-Diff Codegen Jacobian
604  derivativesCppadJIT jacCG(f);
605 
606  DerivativesCppadSettings settings;
607  settings.createJacobian_ = true;
608 
609  // compile the Jacobian
610  jacCG.compileJIT(settings, "kindrQuaternionTestTestLib");
611 
612  } catch (std::exception& e)
613  {
614  std::cout << "Eigen transform test failed: " << e.what() << std::endl;
615  ASSERT_TRUE(false);
616  }
617 }
618 
619 
621 template <typename SCALAR>
622 Eigen::Matrix<SCALAR, 4, 1> kindrUnitQuaternionTestFunction(const Eigen::Matrix<SCALAR, 3, 1>& x)
623 {
624  Eigen::Quaternion<SCALAR> q_init;
625  q_init = Eigen::AngleAxis<SCALAR>(x(0), Eigen::Matrix<SCALAR, 3, 1>::UnitX()) *
626  Eigen::AngleAxis<SCALAR>(x(1), Eigen::Matrix<SCALAR, 3, 1>::UnitY()) *
627  Eigen::AngleAxis<SCALAR>(x(2), Eigen::Matrix<SCALAR, 3, 1>::UnitZ());
628  q_init = Eigen::Quaternion<SCALAR>((SCALAR)1.0, (SCALAR)0, (SCALAR)0, (SCALAR)0);
629 
630  kindr::UnitQuaternion<SCALAR> q(q_init);
631 
632  Eigen::Matrix<SCALAR, 4, 1> res;
633  res << q.w(), q.x(), q.y(), q.z();
634 
635  return res;
636 }
637 TEST(KindrJitTest, kindrUnitQuaternionTest)
638 {
639  using derivativesCppadJIT = ct::core::DerivativesCppadJIT<3, 4>;
640 
641  try
642  {
643  // create a function handle (also works for class methods, lambdas, function pointers, ...)
644  typename derivativesCppadJIT::FUN_TYPE_CG f = kindrUnitQuaternionTestFunction<derivativesCppadJIT::CG_SCALAR>;
645 
646  // initialize the Auto-Diff Codegen Jacobian
647  derivativesCppadJIT jacCG(f);
648 
649  DerivativesCppadSettings settings;
650  settings.createJacobian_ = true;
651 
652  // compile the Jacobian
653  jacCG.compileJIT(settings, "kindrUnitQuaternionTestLib");
654 
655  } catch (std::exception& e)
656  {
657  std::cout << "Eigen transform test failed: " << e.what() << std::endl;
658  ASSERT_TRUE(false);
659  }
660 }
661 
662 
666 template <typename SCALAR>
667 Eigen::Matrix<SCALAR, 4, 1> kindrRotationQuaternionTestFunction(const Eigen::Matrix<SCALAR, 3, 1>& x)
668 {
669  kindr::RotationQuaternion<SCALAR> q(x(0), x(1), x(2), x(2));
670 
671  Eigen::Matrix<SCALAR, 4, 1> res;
672  res << q.w(), q.x(), q.y(), q.z();
673 
674  return res;
675 }
676 TEST(KindrJitTest, kindrRotationQuaternionTest)
677 {
678  using derivativesCppadJIT = ct::core::DerivativesCppadJIT<3, 4>;
679 
680  try
681  {
682  // create a function handle (also works for class methods, lambdas, function pointers, ...)
683  typename derivativesCppadJIT::FUN_TYPE_CG f =
684  kindrRotationQuaternionTestFunction<derivativesCppadJIT::CG_SCALAR>;
685 
686  // initialize the Auto-Diff Codegen Jacobian
687  derivativesCppadJIT jacCG(f);
688 
689  DerivativesCppadSettings settings;
690  settings.createJacobian_ = true;
691 
692  // compile the Jacobian
693  jacCG.compileJIT(settings, "kindrRotationQuaternionTest");
694 
695  } catch (std::exception& e)
696  {
697  std::cout << "Eigen transform test failed: " << e.what() << std::endl;
698  ASSERT_TRUE(false);
699  }
700 }
static Scalar sin(const Scalar &x)
Eigen::Matrix< SCALAR, 4, 1 > kindrQuaternionTestFunction(const Eigen::Matrix< SCALAR, 3, 1 > &x)
test the JIT compatibility of kindr Quaternions
Definition: kindrJITtest.h:585
Eigen::Matrix< SCALAR, 3, 1 > eigenQuaternionTestFunction(const Eigen::Matrix< SCALAR, 3, 1 > &x)
test the JIT compatibility of Eigen quaternions
Definition: kindrJITtest.h:504
Eigen::Matrix< SCALAR, 3, 1 > kindrRotationMatrixTestFunction(const Eigen::Matrix< SCALAR, 3, 1 > &x)
test the kindr RotationMatrix constructor
Definition: kindrJITtest.h:140
Eigen::Matrix< SCALAR, 3, 1 > convertRotationMatrixToKindrEulerTestFunction(const Eigen::Matrix< SCALAR, 3, 1 > &x)
test the behaviour of transcribing a kindr rotation matrix into Euler Angles
Definition: kindrJITtest.h:190
DerivativesCppad< inDim, outDim > derivativesCppad
TEST(KindrJitTest, EulerAnglesTest)
Definition: kindrJITtest.h:58
Eigen::Matrix< SCALAR, 4, 1 > kindrRotationQuaternionTestFunction(const Eigen::Matrix< SCALAR, 3, 1 > &x)
Definition: kindrJITtest.h:667
Eigen::Matrix< SCALAR, 4, 1 > convertKindrRotationMatrixToEigenQuatTestFunction(const Eigen::Matrix< SCALAR, 3, 1 > &x)
check conversion from kindr rotation matrix to an eigen quaternion
Definition: kindrJITtest.h:416
clear all close all load ct GNMSLog0 mat reformat t
Eigen::Matrix< SCALAR, -1, 1 > frobeniusNormTestFunction(const Eigen::Matrix< SCALAR, -1, 1 > &x)
test the Frobenius norm on an EigenMatrix
Definition: kindrJITtest.h:325
static Scalar cos(const Scalar &x)
DerivativesCppadJIT< inDim, outDim > derivativesCppadJIT
CppAD::AD< CppAD::cg::CG< double > > SCALAR
Eigen::Matrix< SCALAR, 3, 1 > rotateTestFunction(const Eigen::Matrix< SCALAR, 3, 1 > &x)
test the kindr rotate method on euler angles
Definition: kindrJITtest.h:101
for i
Eigen::Matrix< SCALAR, 3, 1 > testFunction(const Eigen::Matrix< SCALAR, 3, 1 > &x)
Definition: kindrJITtest.h:23
Eigen::Matrix< SCALAR, 4, 1 > convertRotationMatrixToKindrQuatTestFunction(const Eigen::Matrix< SCALAR, 3, 1 > &x)
test the behaviour of transcribing a rotation matrix into a kindr RotationQuaternion ...
Definition: kindrJITtest.h:366
Eigen::Matrix< SCALAR, 4, 1 > kindrUnitQuaternionTestFunction(const Eigen::Matrix< SCALAR, 3, 1 > &x)
test the JIT compatibility of kindr UnitQuaternions
Definition: kindrJITtest.h:622
Eigen::Matrix< SCALAR, 3, 1 > eigenTransformTestFunction(const Eigen::Matrix< SCALAR, 3, 1 > &x)
test the JIT compatibility of Eigen Transforms
Definition: kindrJITtest.h:465
Eigen::Matrix< SCALAR, 4, 1 > eigenQuatToKindrRotationQuatFunction(const Eigen::Matrix< SCALAR, 3, 1 > &x)
check the conversion between an Eigen Quaternion and an kindr RotationQuaternion. ...
Definition: kindrJITtest.h:542
Eigen::Matrix< SCALAR, 3, 3 > jacobianCheck(const Eigen::Matrix< SCALAR, 3, 1 > &x)
Definition: kindrJITtest.h:44
Eigen::Matrix< SCALAR, 3, 1 > convertRotationMatrixToEigenEulerTestFunction(const Eigen::Matrix< SCALAR, -1, 1 > &x)
test the behaviour of transcribing an Eigen rotation matrix into Eigen Euler Angles ...
Definition: kindrJITtest.h:235