bindings_inertia.py
Go to the documentation of this file.
1 import unittest
2 import pinocchio as pin
3 import numpy as np
4 from pinocchio.utils import eye, zero, rand
5 
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  I = pin.Inertia.Random()
94 
95  v = I.toDynamicParameters()
96 
97  self.assertApprox(v[0], I.mass)
98  self.assertApprox(v[1:4], I.mass * I.lever)
99 
100  I_o = I.inertia + I.mass * pin.skew(I.lever).transpose().dot(pin.skew(I.lever))
101  I_ov = np.array(
102  [
103  [float(v[4]), float(v[5]), float(v[7])],
104  [float(v[5]), float(v[6]), float(v[8])],
105  [float(v[7]), float(v[8]), float(v[9])],
106  ]
107  )
108 
109  self.assertApprox(I_o, I_ov)
110 
111  I2 = pin.Inertia.FromDynamicParameters(v)
112  self.assertApprox(I2, I)
113 
114  def test_array(self):
115  I = pin.Inertia.Random()
116  I_array = np.array(I)
117 
118  self.assertApprox(I_array, I.matrix())
119 
120  def test_several_init(self):
121  for _ in range(100000):
122  i = pin.Inertia.Random() + pin.Inertia.Random()
123  s = i.__str__()
124  self.assertTrue(s != "")
125 
126 
127 if __name__ == "__main__":
128  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
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:33
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:42
pinocchio.utils.zero
def zero(n)
Definition: bindings/python/pinocchio/utils.py:38
bindings_inertia.TestInertiaBindings.test_array
def test_array(self)
Definition: bindings_inertia.py:114
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:120


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