bindings/python/pinocchio/utils.py
Go to the documentation of this file.
1 #
2 # Copyright (c) 2015-2020 CNRS INRIA
3 #
4 
5 from __future__ import print_function
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  if pin.getNumpyType()==np.matrix:
35  return np.matrix(res)
36  else:
37  return res
38 
39 def zero(n):
40  if pin.getNumpyType()==np.matrix:
41  return np.matrix(np.zeros([n, 1] if isinstance(n, int) else n))
42  else:
43  return np.zeros(n)
44 
45 def rand(n):
46  if pin.getNumpyType()==np.matrix:
47  return np.matrix(np.random.rand(n, 1) if isinstance(n, int) else np.random.rand(n[0], n[1]))
48  else:
49  return np.random.rand(n) if isinstance(n, int) else np.random.rand(n[0], n[1])
50 
51 @deprecated("Please use numpy.cross(a, b) or numpy.cross(a, b, axis=0).")
52 def cross(a, b):
53  return np.matrix(np.cross(a, b, axis=0))
54 
55 @deprecated('Now useless. You can directly have access to this function from the main scope of Pinocchio')
56 def skew(p):
57  return pin.skew(p)
58 
59 @deprecated('Now useless. You can directly have access to this function from the main scope of Pinocchio')
60 def se3ToXYZQUAT(M):
61  return pin.SE3ToXYZQUATtuple(M)
62 
63 @deprecated('Now useless. You can directly have access to this function from the main scope of Pinocchio')
64 def XYZQUATToSe3(vec):
65  return pin.XYZQUATToSE3(vec)
66 
67 def isapprox(a, b, epsilon=1e-6):
68  if "np" in a.__class__.__dict__:
69  a = a.np
70  if "np" in b.__class__.__dict__:
71  b = b.np
72  if isinstance(a, (np.ndarray, list)) and isinstance(b, (np.ndarray, list)):
73  a = np.squeeze(np.array(a))
74  b = np.squeeze(np.array(b))
75  return np.allclose(a, b, epsilon)
76  return abs(a - b) < epsilon
77 
78 
79 def mprint(M, name="ans",eps=1e-15):
80  '''
81  Matlab-style pretty matrix print.
82  '''
83  if isinstance(M, pin.SE3):
84  M = M.homogeneous
85  if len(M.shape==1):
86  M = np.expand_dims(M, axis=0)
87  ncol = M.shape[1]
88  NC = 6
89  print(name, " = ")
90  print()
91 
92  Mmin = lambda M: M.min()
93  Mmax = lambda M: M.max()
94  Mm = Mmin(abs(M[np.nonzero(M)]))
95  MM = Mmax(abs(M[np.nonzero(M)]))
96 
97  fmt = "% 10.3e" if Mm < 1e-5 or MM > 1e6 or MM / Mm > 1e3 else "% 1.5f"
98 
99  for i in range((ncol - 1) / NC + 1):
100  cmin = i * 6
101  cmax = (i + 1) * 6
102  cmax = ncol if ncol < cmax else cmax
103  print("Columns %s through %s" % (cmin, cmax - 1))
104  print()
105  for r in range(M.shape[0]):
106  sys.stdout.write(" ")
107  for c in range(cmin, cmax):
108  if abs(M[r,c])>eps: sys.stdout.write(fmt % M[r,c] + " ")
109  else: sys.stdout.write(" 0"+" "*9)
110  print()
111  print()
112 
113 
115  vector = pin.StdVec_StdString()
116  vector.extend(item for item in items)
117  return vector
118 
119 
120 __all__ = ['np', 'npl', 'eye', 'zero', 'rand', 'isapprox', 'mprint',
121  'skew', 'cross',
122  'npToTTuple', 'npToTuple', 'rotate',
123  'rpyToMatrix', 'matrixToRpy',
124  'se3ToXYZQUAT', 'XYZQUATToSe3',
125  '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 Tue Jun 1 2021 02:45:05