Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

robomath Mat Matrix does not properly cast to numpy array

#1
`numpy.asarray(rm.Mat)` returns the original matrix, transposed. This property is not reversed when casting back using `rm.Mat(numpy.ndarray)`.

To reproduce:

Code:
import numpy as np
from robodk import robomath
A = robomath.eye()
A[0,3] = 1
A
Quote:Pose(1.000, 0.000, 0.000,  0.000, 0.000, 0.000):
[[ 1, 0, 0, 1 ],
[ 0, 1, 0, 0 ],
[ 0, 0, 1, 0 ],
[ 0, 0, 0, 1 ]]

Code:
A = np.asarray(A)
A
Quote:array([[1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, 0],
      [1, 0, 0, 1]])

Code:
A = robomath.Mat(A)
A
Quote:Matrix: (4, 1)
[[ [1 0 0 0] ],
[ [0 1 0 0] ],
[ [0 0 1 0] ],
[ [1 0 0 1] ]]
#2
Bumping this thread. Should I open an issue on GitHub?
#3
I would not say this is a bug but maybe something we could have done better by design. Changing this behavior could brake many other things and break backwards compatibility.

Instead, we could add this function to the Mat class. Example:
Code:
def toNumpy(self):
    return np.asarray(self.tr())
So so that we can do:
Code:
pose_mat = transl(10,20,30)
pose_np = pose_mat.toNumpy()
pose_mat2 = Mat(pose_np)
# the 3 poses should be the same
Woudl this work?
#4
Yep, that would work.
#5
Great then, we just added fromNumpy and toNumpy functions to convert to a numpy array and from a numpy array.
Code:
    def fromNumpy(ndarray):
        """Convert a numpy array to a Mat matrix"""
        return Mat(ndarray.tolist())

    def toNumpy(self):
        """Return a copy of the Mat matrix as a numpy array"""
        import numpy
        return numpy.asarray(self.rows, float)
You can find the latest version of the source code on our GitHub repository:
https://github.com/RoboDK/RoboDK-API/tre...hon/robodk
#6
Thanks, Albert!
  




Users browsing this thread:
1 Guest(s)