bindings_inertia.py
Go to the documentation of this file.
1 import unittest
2 
3 import numpy as np
4 import pinocchio as pin
5 from pinocchio.utils import eye, rand, zero
6 from test_case import PinocchioTestCase as TestCase
7 
8 
9 class TestInertiaBindings(TestCase):
10  def test_zero_getters(self):
11  Y = pin.Inertia.Zero()
12  self.assertTrue(Y.mass == 0)
13  self.assertTrue(np.allclose(zero(3), Y.lever))
14  self.assertTrue(np.allclose(zero([3, 3]), Y.inertia))
15 
17  Y = pin.Inertia.Identity()
18  self.assertTrue(Y.mass == 1)
19  self.assertTrue(np.allclose(zero(3), Y.lever))
20  self.assertTrue(np.allclose(eye(3), Y.inertia))
21 
22  # TODO: this is not nice, since a random matrix can *in theory* be unity
23  def test_setRandom(self):
24  Y = pin.Inertia.Identity()
25  Y.setRandom()
26  self.assertFalse(Y.mass == 1)
27  self.assertFalse(np.allclose(zero(3), Y.lever))
28  self.assertFalse(np.allclose(eye(3), Y.inertia))
29 
30  def test_setZero(self):
31  Y = pin.Inertia.Zero()
32  Y.setRandom()
33  Y.setZero()
34  self.assertTrue(Y.mass == 0)
35  self.assertTrue(np.allclose(zero(3), Y.lever))
36  self.assertTrue(np.allclose(zero([3, 3]), Y.inertia))
37 
38  def test_setIdentity(self):
39  Y = pin.Inertia.Zero()
40  Y.setIdentity()
41  self.assertTrue(Y.mass == 1)
42  self.assertTrue(np.allclose(zero(3), Y.lever))
43  self.assertTrue(np.allclose(eye(3), Y.inertia))
44 
45  def test_set_mass(self):
46  Y = pin.Inertia.Zero()
47  Y.mass = 10
48  self.assertTrue(np.allclose(Y.mass, 10))
49 
50  def test_set_lever(self):
51  Y = pin.Inertia.Zero()
52  lev = rand(3)
53  Y.lever = lev
54  self.assertTrue(np.allclose(Y.lever, lev))
55 
56  def test_set_inertia(self):
57  Y = pin.Inertia.Zero()
58  iner = rand([3, 3])
59  iner = (iner + iner.T) / 2.0 # Symmetrize the matrix
60  Y.inertia = iner
61  self.assertTrue(np.allclose(Y.inertia, iner))
62 
63  def test_internal_sums(self):
64  Y1 = pin.Inertia.Random()
65  Y2 = pin.Inertia.Random()
66  Y = Y1 + Y2
67  self.assertTrue(np.allclose(Y1.matrix() + Y2.matrix(), Y.matrix()))
68 
69  def test_se3_action(self):
70  m = pin.SE3.Random()
71  Y = pin.Inertia.Random()
72  v = pin.Motion.Random()
73  self.assertTrue(np.allclose((Y * v).vector, Y.matrix().dot(v.vector)))
74  self.assertTrue(
75  np.allclose(
76  (m * Y).matrix(),
77  m.inverse().action.T.dot(Y.matrix()).dot(m.inverse().action),
78  )
79  )
80  self.assertTrue(
81  np.allclose(
82  m.act(Y).matrix(),
83  m.inverse().action.T.dot(Y.matrix()).dot(m.inverse().action),
84  )
85  )
86  self.assertTrue(
87  np.allclose(
88  (m.actInv(Y)).matrix(), m.action.T.dot(Y.matrix()).dot(m.action)
89  )
90  )
91 
93  In = pin.Inertia.Random()
94 
95  v = In.toDynamicParameters()
96 
97  self.assertApprox(v[0], In.mass)
98  self.assertApprox(v[1:4], In.mass * In.lever)
99 
100  I_o = In.inertia + In.mass * pin.skew(In.lever).transpose().dot(
101  pin.skew(In.lever)
102  )
103  I_ov = np.array(
104  [
105  [float(v[4]), float(v[5]), float(v[7])],
106  [float(v[5]), float(v[6]), float(v[8])],
107  [float(v[7]), float(v[8]), float(v[9])],
108  ]
109  )
110 
111  self.assertApprox(I_o, I_ov)
112 
113  I2 = pin.Inertia.FromDynamicParameters(v)
114  self.assertApprox(I2, In)
115 
116  # Test FromDynamicParameters raise an exception if the wrong
117  # vector size is provided
118  with self.assertRaises(ValueError):
119  pin.Inertia.FromDynamicParameters(np.array([]))
120 
122  In = pin.Inertia.Random()
123 
124  pseudo = In.toPseudoInertia()
125 
126  # test accessing mass, h, sigma
127  self.assertApprox(pseudo.mass, In.mass)
128  self.assertApprox(pseudo.h, In.mass * In.lever)
129  self.assertEqual(pseudo.sigma.shape, (3, 3))
130 
131  # test toMatrix
132  _ = pseudo.toMatrix()
133 
134  # test toDynamicParameters
135  params = pseudo.toDynamicParameters()
136 
137  # test fromDynamicParameters
138  pseudo2 = pin.PseudoInertia.FromDynamicParameters(params)
139  self.assertApprox(pseudo.mass, pseudo2.mass)
140  self.assertApprox(pseudo.h, pseudo2.h)
141  self.assertApprox(pseudo.sigma, pseudo2.sigma)
142 
143  # Test FromDynamicParameters raise an exception if the wrong
144  # vector size is provided
145  with self.assertRaises(ValueError):
146  pin.PseudoInertia.FromDynamicParameters(np.array([]))
147 
148  # test fromMatrix
149  pseudo3 = pin.PseudoInertia.FromMatrix(pseudo.toMatrix())
150  self.assertApprox(pseudo.mass, pseudo3.mass)
151  self.assertApprox(pseudo.h, pseudo3.h)
152  self.assertApprox(pseudo.sigma, pseudo3.sigma)
153 
154  # test fromInertia
155  pseudo4 = pin.PseudoInertia.FromInertia(In)
156  self.assertApprox(pseudo.mass, pseudo4.mass)
157  self.assertApprox(pseudo.h, pseudo4.h)
158  self.assertApprox(pseudo.sigma, pseudo4.sigma)
159 
160  # test toInertia
161  self.assertApprox(pseudo4.toInertia(), In)
162 
163  # test from PseudoInertia
164  pin.PseudoInertia(pseudo)
165 
166  # test array
167  pseudo_array = np.array(pseudo)
168  self.assertApprox(pseudo_array, pseudo.toMatrix())
169 
170  def test_log_cholesky(self):
171  log_cholesky = pin.LogCholeskyParameters(np.random.randn(10))
172  In = pin.Inertia.FromLogCholeskyParameters(log_cholesky)
173 
174  # Test constructor with wrong vector size
175  with self.assertRaises(ValueError):
176  log_cholesky = pin.LogCholeskyParameters(np.array([]))
177 
178  # test accessing parameters
179  self.assertEqual(log_cholesky.parameters.shape, (10,))
180 
181  # test toDynamicParameters
182  params = log_cholesky.toDynamicParameters()
183  params2 = In.toDynamicParameters()
184  self.assertApprox(params, params2)
185 
186  # test toPseudoInertia
187  pseudo = log_cholesky.toPseudoInertia()
188  pseudo2 = In.toPseudoInertia()
189  self.assertApprox(pseudo.mass, pseudo2.mass)
190  self.assertApprox(pseudo.h, pseudo2.h)
191  self.assertApprox(pseudo.sigma, pseudo2.sigma)
192 
193  # test toInertia
194  In2 = log_cholesky.toInertia()
195  self.assertApprox(In, In2)
196 
197  # test calculateJacobian
198  log_cholesky.calculateJacobian()
199 
200  # test array
201  log_cholesky_array = np.array(log_cholesky)
202  self.assertApprox(log_cholesky_array, log_cholesky.parameters)
203 
204  def test_array(self):
205  In = pin.Inertia.Random()
206  I_array = np.array(In)
207 
208  self.assertApprox(I_array, In.matrix())
209 
210  def test_several_init(self):
211  for _ in range(100000):
212  In = pin.Inertia.Random() + pin.Inertia.Random()
213  s = In.__str__()
214  self.assertTrue(s != "")
215 
216 
217 if __name__ == "__main__":
218  unittest.main()
bindings_inertia.TestInertiaBindings.test_set_mass
def test_set_mass(self)
Definition: bindings_inertia.py:45
bindings_inertia.TestInertiaBindings.test_se3_action
def test_se3_action(self)
Definition: bindings_inertia.py:69
bindings_inertia.TestInertiaBindings.test_internal_sums
def test_internal_sums(self)
Definition: bindings_inertia.py:63
bindings_inertia.TestInertiaBindings.test_dynamic_parameters
def test_dynamic_parameters(self)
Definition: bindings_inertia.py:92
bindings_inertia.TestInertiaBindings.test_pseudo_inertia
def test_pseudo_inertia(self)
Definition: bindings_inertia.py:121
bindings_inertia.TestInertiaBindings
Definition: bindings_inertia.py:9
pinocchio.utils
Definition: bindings/python/pinocchio/utils.py:1
bindings_inertia.TestInertiaBindings.test_zero_getters
def test_zero_getters(self)
Definition: bindings_inertia.py:10
bindings_inertia.TestInertiaBindings.test_set_lever
def test_set_lever(self)
Definition: bindings_inertia.py:50
pinocchio.utils.eye
def eye(n)
Definition: bindings/python/pinocchio/utils.py:32
bindings_inertia.TestInertiaBindings.test_setZero
def test_setZero(self)
Definition: bindings_inertia.py:30
bindings_inertia.TestInertiaBindings.test_identity_getters
def test_identity_getters(self)
Definition: bindings_inertia.py:16
bindings_inertia.TestInertiaBindings.test_setRandom
def test_setRandom(self)
Definition: bindings_inertia.py:23
bindings_inertia.TestInertiaBindings.test_setIdentity
def test_setIdentity(self)
Definition: bindings_inertia.py:38
pinocchio.utils.rand
def rand(n)
Definition: bindings/python/pinocchio/utils.py:41
bindings_inertia.TestInertiaBindings.test_log_cholesky
def test_log_cholesky(self)
Definition: bindings_inertia.py:170
pinocchio.utils.zero
def zero(n)
Definition: bindings/python/pinocchio/utils.py:37
bindings_inertia.TestInertiaBindings.test_array
def test_array(self)
Definition: bindings_inertia.py:204
bindings_inertia.TestInertiaBindings.test_set_inertia
def test_set_inertia(self)
Definition: bindings_inertia.py:56
bindings_inertia.TestInertiaBindings.test_several_init
def test_several_init(self)
Definition: bindings_inertia.py:210


pinocchio
Author(s):
autogenerated on Sun Dec 22 2024 03:41:06