bdaiinstitute / spatialmath-python

Create, manipulate and convert representations of position and orientation in 2D or 3D using Python
MIT License
517 stars 85 forks source link

Initialize a SE3/SO3 instance using the numpy array #112

Closed ningmimg closed 6 months ago

ningmimg commented 10 months ago

When I try to initialize a SE3 instance with a 4X4 numpy array, it doesn't seem to work,(maybe I don't have enough skills ?)

instead I tried to create an instance of SO3 using a 3X3 rotation matrix, and I was pretty sure the numpy array A I gave was a rotation matrix (normalized). and
A @ A.T = array([[ 1.00000000e+00, -2.77555756e-17, 0.00000000e+00], [-2.77555756e-17, 1.00000000e+00, 0.00000000e+00], [ 0.00000000e+00, 0.00000000e+00, 1.00000005e+00]]) but the code always report error to me that `ValueError Traceback (most recent call last) Cell In[119], line 7 4 # # u,_,v = np.linalg.svd(a) 5 # a1 = np.dot(u,v) 6 from spatialmath import SO3 ----> 7 SO3(a) 8 # # a.dtype 9 # # a1.T@a1 10 # a.T@a

File c:\pythonproject2\venv1\lib\site-packages\spatialmath\pose3d.py:104, in SO3.init(self, arg, check) 101 self.data = [smb.t2r(x) for x in arg.data] 103 elif not super().arghandler(arg, check=check): --> 104 raise ValueError("bad argument to constructor") when I change the value of A to A1 = np.asarray([[ 0.00000000000001, -1., 0.], [ 1., 0., 0.], [ 0., 0., 1.]])`
the SO3 instance is created successfully. hence, I suspect the root of the problem may be a matter of precision , But there is no proper way to solve it

jcao-bdai commented 10 months ago

Hi @ningmimg , thank you for bringing up this issue.

Firstly, creating SE3 from numpy array should work (but see below):

>>> from spatialmath import SE3
>>> import numpy as np
>>> x = np.eye(4, 4)
>>> x
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])
>>> y = SE3(x)
>>> y
SE3(array([[1., 0., 0., 0.],
           [0., 1., 0., 0.],
           [0., 0., 1., 0.],
           [0., 0., 0., 1.]]))
>>> y.t
array([0., 0., 0.])
>>> y.R
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

However, given the second part of your description, I suspect you found it not working for the same reason as how it did not work in the SO3 example.

I see you've provided "A @ A.T" above, could you please also give me the value of A itself (the one that you used to produce that error for SO3)? Or, if you are willing to share, a few lines of actual code that you were having problem with?

jcao-bdai commented 6 months ago

Closing the issue as we haven't got any response from the reporter.