bindings/python/pinocchio/utils.py
Go to the documentation of this file.
1 #
2 # Copyright (c) 2015-2022 CNRS INRIA
3 #
4 
5 from __future__ import print_function, division
6 
7 import sys
8 
9 import numpy as np
10 import numpy.linalg as npl
11 
12 from . import pinocchio_pywrap as pin
13 from .pinocchio_pywrap.rpy import matrixToRpy, rpyToMatrix, rotate
14 
15 from .deprecation import deprecated
16 
17 def npToTTuple(M):
18  L = M.tolist()
19  for i in range(len(L)):
20  L[i] = tuple(L[i])
21  return tuple(L)
22 
23 def npToTuple(M):
24  if len(M.shape) == 1:
25  return tuple(M.tolist())
26  if M.shape[0] == 1:
27  return tuple(M.tolist()[0])
28  if M.shape[1] == 1:
29  return tuple(M.T.tolist()[0])
30  return npToTTuple(M)
31 
32 def eye(n):
33  res = np.eye(n)
34  return res
35 
36 def zero(n):
37  return np.zeros(n)
38 
39 def rand(n):
40  return np.random.rand(n) if isinstance(n, int) else np.random.rand(n[0], n[1])
41 
42 @deprecated("Please use numpy.cross(a, b) or numpy.cross(a, b, axis=0).")
43 def cross(a, b):
44  return np.cross(a, b, axis=0)
45 
46 @deprecated('Now useless. You can directly have access to this function from the main scope of Pinocchio')
47 def skew(p):
48  return pin.skew(p)
49 
50 @deprecated('Now useless. You can directly have access to this function from the main scope of Pinocchio')
51 def se3ToXYZQUAT(M):
52  return pin.SE3ToXYZQUATtuple(M)
53 
54 @deprecated('Now useless. You can directly have access to this function from the main scope of Pinocchio')
55 def XYZQUATToSe3(vec):
56  return pin.XYZQUATToSE3(vec)
57 
58 def isapprox(a, b, epsilon=1e-6):
59  if "np" in a.__class__.__dict__:
60  a = a.np
61  if "np" in b.__class__.__dict__:
62  b = b.np
63  if isinstance(a, (np.ndarray, list)) and isinstance(b, (np.ndarray, list)):
64  a = np.squeeze(np.array(a))
65  b = np.squeeze(np.array(b))
66  return np.allclose(a, b, epsilon)
67  return abs(a - b) < epsilon
68 
69 
70 def mprint(M, name="ans",eps=1e-15):
71  '''
72  Matlab-style pretty matrix print.
73  '''
74  if isinstance(M, pin.SE3):
75  M = M.homogeneous
76  if len(M.shape) == 1:
77  M = np.expand_dims(M, axis=0)
78  ncol = M.shape[1]
79  NC = 6
80  print(name, " = ")
81  print()
82 
83  Mmin = lambda M: M.min()
84  Mmax = lambda M: M.max()
85  Mm = Mmin(abs(M[np.nonzero(M)]))
86  MM = Mmax(abs(M[np.nonzero(M)]))
87 
88  fmt = "% 10.3e" if Mm < 1e-5 or MM > 1e6 or MM / Mm > 1e3 else "% 1.5f"
89 
90  for i in range((ncol - 1) // NC + 1):
91  cmin = i * 6
92  cmax = (i + 1) * 6
93  cmax = ncol if ncol < cmax else cmax
94  print("Columns %s through %s" % (cmin, cmax - 1))
95  print()
96  for r in range(M.shape[0]):
97  sys.stdout.write(" ")
98  for c in range(cmin, cmax):
99  if abs(M[r,c])>eps: sys.stdout.write(fmt % M[r,c] + " ")
100  else: sys.stdout.write(" 0"+" "*9)
101  print()
102  print()
103 
104 
106  vector = pin.StdVec_StdString()
107  vector.extend(item for item in items)
108  return vector
109 
110 
111 __all__ = ['np', 'npl', 'eye', 'zero', 'rand', 'isapprox', 'mprint',
112  'skew', 'cross',
113  'npToTTuple', 'npToTuple', 'rotate',
114  'rpyToMatrix', 'matrixToRpy',
115  'se3ToXYZQUAT', 'XYZQUATToSe3',
116  'fromListToVectorOfString']
def deprecated(instructions)
Definition: deprecation.py:7
def mprint(M, name="ans", eps=1e-15)
def isapprox(a, b, epsilon=1e-6)


pinocchio
Author(s):
autogenerated on Fri Jun 23 2023 02:38:33