bindings_SE3.py
Go to the documentation of this file.
1 import unittest
2 from copy import copy, deepcopy
3 
4 import numpy as np
5 import pinocchio as pin
6 from pinocchio.utils import eye, rand, zero
7 
8 
9 def ones(n):
10  return np.ones([n, 1] if isinstance(n, int) else n)
11 
12 
13 class TestSE3Bindings(unittest.TestCase):
14  def test_identity(self):
15  transform = pin.SE3.Identity()
16  self.assertTrue(np.allclose(zero(3), transform.translation))
17  self.assertTrue(np.allclose(eye(3), transform.rotation))
18  self.assertTrue(np.allclose(eye(4), transform.homogeneous))
19  self.assertTrue(np.allclose(eye(6), transform.action))
20  transform.setRandom()
21  transform.setIdentity()
22  self.assertTrue(np.allclose(eye(4), transform.homogeneous))
23 
24  def test_constructor(self):
25  M = pin.SE3.Random()
26  quat = pin.Quaternion(M.rotation)
27  M_from_quat = pin.SE3(quat, M.translation)
28  self.assertTrue(M_from_quat.isApprox(M))
29 
31  transform = pin.SE3.Identity()
32  self.assertTrue(np.allclose(transform.translation, zero(3)))
33 
34  def test_get_rotation(self):
35  transform = pin.SE3.Identity()
36  self.assertTrue(np.allclose(transform.rotation, eye(3)))
37 
39  transform = pin.SE3.Identity()
40  transform.translation = ones(3)
41  self.assertFalse(np.allclose(transform.translation, zero(3)))
42  self.assertTrue(np.allclose(transform.translation, ones(3)))
43 
44  def test_set_rotation(self):
45  transform = pin.SE3.Identity()
46  transform.rotation = zero([3, 3])
47  self.assertFalse(np.allclose(transform.rotation, eye(3)))
48  self.assertTrue(np.allclose(transform.rotation, zero([3, 3])))
49 
50  def test_homogeneous(self):
51  amb = pin.SE3.Random()
52  H = amb.homogeneous
53  self.assertTrue(
54  np.allclose(H[0:3, 0:3], amb.rotation)
55  ) # top left 33 corner = rotation of amb
56  self.assertTrue(
57  np.allclose(H[0:3, 3], amb.translation)
58  ) # top 3 of last column = translation of amb
59  self.assertTrue(
60  np.allclose(H[3, :], [0.0, 0.0, 0.0, 1.0])
61  ) # last row = 0 0 0 1
62 
63  amb_from_H = pin.SE3(H)
64  self.assertTrue(amb_from_H.isApprox(amb))
65 
66  def test_action_matrix(self):
67  amb = pin.SE3.Random()
68  aXb = amb.action
69  self.assertTrue(
70  np.allclose(aXb[:3, :3], amb.rotation)
71  ) # top left 33 corner = rotation of amb
72  self.assertTrue(
73  np.allclose(aXb[3:, 3:], amb.rotation)
74  ) # bottom right 33 corner = rotation of amb
75  tblock = pin.skew(amb.translation).dot(amb.rotation)
76  self.assertTrue(
77  np.allclose(aXb[:3, 3:], tblock)
78  ) # top right 33 corner = translation + rotation
79  self.assertTrue(
80  np.allclose(aXb[3:, :3], zero([3, 3]))
81  ) # bottom left 33 corner = 0
82 
83  def test_inverse(self):
84  amb = pin.SE3.Random()
85  aMb = amb.homogeneous
86  bma = amb.inverse()
87  self.assertTrue(np.allclose(np.linalg.inv(aMb), bma.homogeneous))
88 
90  amb = pin.SE3.Random()
91  bmc = pin.SE3.Random()
92  amc = amb * bmc
93  cma = amc.inverse()
94 
95  aMb = amb.homogeneous
96  bMc = bmc.homogeneous
97  aMc = amc.homogeneous
98  cMa = np.linalg.inv(aMc)
99 
100  self.assertTrue(np.allclose(aMb.dot(bMc), aMc))
101  self.assertTrue(np.allclose(cma.homogeneous, cMa))
102 
104  amb = pin.SE3.Random()
105  bmc = pin.SE3.Random()
106  amc = amb * bmc
107  cma = amc.inverse()
108 
109  aXb = amb.action
110  bXc = bmc.action
111  aXc = amc.action
112  cXa = np.linalg.inv(aXc)
113 
114  self.assertTrue(np.allclose(aXb.dot(bXc), aXc))
115  self.assertTrue(np.allclose(cma.action, cXa))
116 
117  def test_point_action(self):
118  amb = pin.SE3.Random()
119  aMb = amb.homogeneous
120  p_homogeneous = rand(4)
121  p_homogeneous[3] = 1
122  p = p_homogeneous[0:3].copy()
123 
124  # act
125  self.assertTrue(np.allclose(amb.act(p), (aMb.dot(p_homogeneous))[0:3]))
126 
127  # actinv
128  bMa = np.linalg.inv(aMb)
129  bma = amb.inverse()
130  self.assertTrue(np.allclose(bma.act(p), (bMa.dot(p_homogeneous))[0:3]))
131 
132  def test_member(self):
133  M = pin.SE3.Random()
134  trans = M.translation
135  M.translation[2] = 1.0
136 
137  self.assertTrue(trans[2] == M.translation[2])
138 
139  def test_conversions(self):
140  def compute(m):
141  tq_vec = pin.SE3ToXYZQUAT(m)
142  tq_tup = pin.SE3ToXYZQUATtuple(m)
143  mm_vec = pin.XYZQUATToSE3(tq_vec)
144  mm_tup = pin.XYZQUATToSE3(tq_tup)
145  mm_lis = pin.XYZQUATToSE3(list(tq_tup))
146  return tq_vec, tq_tup, mm_vec, mm_tup, mm_lis
147 
148  m = pin.SE3.Identity()
149  tq_vec, tq_tup, mm_vec, mm_tup, mm_lis = compute(m)
150  self.assertTrue(np.allclose(m.homogeneous, mm_tup.homogeneous))
151  self.assertTrue(np.allclose(m.homogeneous, mm_vec.homogeneous))
152  self.assertTrue(np.allclose(m.homogeneous, mm_lis.homogeneous))
153  for i in range(6):
154  self.assertTrue(tq_vec[i] == 0 and tq_tup[i] == 0)
155  self.assertTrue(tq_vec[6] == 1 and tq_tup[6] == 1)
156 
157  m = pin.SE3.Random()
158  tq_vec, tq_tup, mm_vec, mm_tup, mm_lis = compute(m)
159  self.assertTrue(len(tq_vec) == 7)
160  self.assertTrue(len(tq_tup) == 7)
161  for a, b in zip(tq_vec, tq_tup):
162  self.assertTrue(a == b)
163  self.assertTrue(np.allclose(m.homogeneous, mm_tup.homogeneous))
164  self.assertTrue(mm_vec == mm_tup)
165  self.assertTrue(mm_vec == mm_lis)
166 
167  M = pin.SE3.Random()
168  h = np.array(M)
169 
170  M_from_h = pin.SE3(h)
171  self.assertTrue(M == M_from_h)
172 
173  def test_several_init(self):
174  for _ in range(100000):
175  r = pin.SE3.Random() * pin.SE3.Random()
176  s = r.__str__()
177  self.assertTrue(s != "")
178 
179  def test_copy(self):
180  M = pin.SE3.Random()
181  Mc = copy(M)
182  Mdc = deepcopy(M)
183  self.assertTrue(M == Mc)
184  self.assertTrue(M == Mdc)
185 
186  Mc.setRandom()
187  self.assertFalse(M == Mc)
188  Mdc.setRandom()
189  self.assertFalse(M == Mc)
190 
191 
192 if __name__ == "__main__":
193  unittest.main()
bindings_SE3.TestSE3Bindings
Definition: bindings_SE3.py:13
bindings_SE3.TestSE3Bindings.test_homogeneous
def test_homogeneous(self)
Definition: bindings_SE3.py:50
bindings_SE3.TestSE3Bindings.test_identity
def test_identity(self)
Definition: bindings_SE3.py:14
bindings_SE3.TestSE3Bindings.test_several_init
def test_several_init(self)
Definition: bindings_SE3.py:173
bindings_SE3.ones
def ones(n)
Definition: bindings_SE3.py:9
bindings_SE3.TestSE3Bindings.test_action_matrix
def test_action_matrix(self)
Definition: bindings_SE3.py:66
bindings_SE3.TestSE3Bindings.test_get_rotation
def test_get_rotation(self)
Definition: bindings_SE3.py:34
bindings_SE3.TestSE3Bindings.test_conversions
def test_conversions(self)
Definition: bindings_SE3.py:139
bindings_SE3.TestSE3Bindings.test_copy
def test_copy(self)
Definition: bindings_SE3.py:179
bindings_SE3.TestSE3Bindings.test_point_action
def test_point_action(self)
Definition: bindings_SE3.py:117
bindings_SE3.TestSE3Bindings.test_constructor
def test_constructor(self)
Definition: bindings_SE3.py:24
pinocchio.utils
Definition: bindings/python/pinocchio/utils.py:1
bindings_SE3.TestSE3Bindings.test_internal_product_vs_homogeneous
def test_internal_product_vs_homogeneous(self)
Definition: bindings_SE3.py:89
bindings_SE3.TestSE3Bindings.test_member
def test_member(self)
Definition: bindings_SE3.py:132
bindings_SE3.TestSE3Bindings.test_inverse
def test_inverse(self)
Definition: bindings_SE3.py:83
pinocchio.utils.eye
def eye(n)
Definition: bindings/python/pinocchio/utils.py:32
bindings_SE3.TestSE3Bindings.test_get_translation
def test_get_translation(self)
Definition: bindings_SE3.py:30
bindings_SE3.TestSE3Bindings.test_internal_product_vs_action
def test_internal_product_vs_action(self)
Definition: bindings_SE3.py:103
bindings_SE3.TestSE3Bindings.test_set_translation
def test_set_translation(self)
Definition: bindings_SE3.py:38
bindings_SE3.TestSE3Bindings.test_set_rotation
def test_set_rotation(self)
Definition: bindings_SE3.py:44
pinocchio::copy
void copy(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const DataTpl< Scalar, Options, JointCollectionTpl > &origin, DataTpl< Scalar, Options, JointCollectionTpl > &dest, KinematicLevel kinematic_level)
Copy part of the data from origin to dest. Template parameter can be used to select at which differen...
Definition: copy.hpp:42
pinocchio.utils.rand
def rand(n)
Definition: bindings/python/pinocchio/utils.py:41
pinocchio.utils.zero
def zero(n)
Definition: bindings/python/pinocchio/utils.py:37


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