bindings_spatial.py
Go to the documentation of this file.
1 import unittest
2 
3 import numpy as np
4 from numpy.random import rand
5 
6 import pinocchio as pin
7 from pinocchio import skew, unSkew, skewSquare
8 
9 try:
10  import casadi
11  from pinocchio import casadi as cpin
12 
13  WITH_CASADI = True
14 except:
15  WITH_CASADI = False
16 
17 from test_case import PinocchioTestCase
18 
19 
21  def test_skew(self):
22  v3 = rand(3)
23  self.assertApprox(v3, unSkew(skew(v3)))
24  self.assertLess(np.linalg.norm(skew(v3).dot(v3)), 1e-10)
25 
26  x, y, z = tuple(rand(3).tolist())
27  M = np.array([[0.0, x, y], [-x, 0.0, z], [-y, -z, 0.0]])
28  self.assertApprox(M, skew(unSkew(M)))
29 
30  rhs = rand(3)
31  self.assertApprox(np.cross(v3, rhs, axis=0), skew(v3).dot(rhs))
32  self.assertApprox(M.dot(rhs), np.cross(unSkew(M), rhs, axis=0))
33 
34  x, y, z = tuple(v3.tolist())
35  self.assertApprox(skew(v3), np.array([[0, -z, y], [z, 0, -x], [-y, x, 0]]))
36 
37  def skewSquare(self):
38  v3 = rand(3)
39 
40  Mss = skewSquare(v3)
41 
42  Ms = skew(v3)
43  Mss_ref = Ms.dot(Ms)
44 
45  self.assertApprox(Mss, Mss_ref)
46 
48  if WITH_CASADI:
49  Rtarget = pin.utils.rotate("x", 3.14 / 4) # Target
50  R0 = pin.Quaternion(0.707107, 0.707107, 0, 0).matrix()
51  nu0 = pin.log3(R0)
52 
53  # Casadi symbolic variables and functions functions
54  nu = casadi.SX.sym("v", 3, 1)
55  D = cpin.log3(
56  cpin.exp3(nu).T @ Rtarget
57  ) # This seems to be the probelematic function
58  dDi_dnu = [
59  casadi.Function("gradient" + str(i), [nu], [casadi.gradient(D[i], nu)])
60  for i in range(3)
61  ] # Compute the gradient of function D wrt nu
62 
63  d0 = dDi_dnu[0](nu0) # Evaluate the gradient at a problematic point nu0
64  self.assertFalse(
65  np.any(np.isnan(d0)), "NaN detected in the log3 function derivative"
66  )
67 
68  def test_Jlog6(self):
69  for _ in range(10):
70  M0: pin.SE3 = pin.SE3.Random()
71  M1 = M0.copy()
72  dM = M0.actInv(M1)
73  J = pin.Jlog6(dM)
74  R = dM.rotation
75  logR = pin.log3(R)
76  Jrot = pin.Jlog3(R)
77  print(R, ":\nlog: {}".format(logR))
78  print(Jrot)
79 
80  self.assertApprox(dM, pin.SE3.Identity())
81  self.assertApprox(Jrot, np.eye(3))
82  self.assertApprox(J, np.eye(6))
83 
84 
85 if __name__ == "__main__":
86  unittest.main()
bindings_spatial.TestSpatial.test_Jlog6
def test_Jlog6(self)
Definition: bindings_spatial.py:68
bindings_spatial.TestSpatial
Definition: bindings_spatial.py:20
bindings_spatial.TestSpatial.test_skew
def test_skew(self)
Definition: bindings_spatial.py:21
bindings_spatial.TestSpatial.skewSquare
def skewSquare(self)
Definition: bindings_spatial.py:37
test_case.PinocchioTestCase.assertApprox
def assertApprox(self, a, b, eps=1e-6)
Definition: test_case.py:12
bindings_spatial.TestSpatial.test_NaN_log3_casadi
def test_NaN_log3_casadi(self)
Definition: bindings_spatial.py:47
pinocchio::python::skew
Eigen::Matrix< typename Vector3::Scalar, 3, 3, Vector3::Options > skew(const Vector3 &v)
Definition: expose-skew.cpp:19
pinocchio::python::unSkew
Eigen::Matrix< typename Matrix3::Scalar, 3, 1, Matrix3::Options > unSkew(const Matrix3 &mat)
Definition: expose-skew.cpp:36
test_case.PinocchioTestCase
Definition: test_case.py:11
pinocchio.utils.rand
def rand(n)
Definition: bindings/python/pinocchio/utils.py:42


pinocchio
Author(s):
autogenerated on Sun Jun 16 2024 02:43:06