How can I convert a 4x4 transformation matrix from one coordinate system to another using Python?

205 Views Asked by At

I am trying to convert a transformation matrix from one coordinate system to another. The first coordinate system looks like this in a pybullet simulation:

pybullet simulation

I'm assuming this coordinate system would be:

  • X = Forward

  • Y = Away From Camera

  • Z = Up

Though I'm not sure about the orientation of the XY plane.

The second coordinate system looks like this:

enter image description here

I'm assuming this coordinate system would be:

  • X = Forward

  • Y= Up

  • Z = Toward The Camera

Though I'm not sure about the orientation of the XZ plane.

The coordinate conversion should then be something like this:

(X,Y,Z)->(X,-Z,Y)

This is the following code I wrote to achieve the transformation:

def transform_matrix(self,transformation_matrix):
    #X maps to X (1,0,0)
    #Y maps to -Z (0,0,-1)
    #Z maps to -Y (0,-1,0)
        

    C = np.matrix([
        [1, 0, 0, 0],
        [0, 0, -1,0],
        [0, -1, 0, 0],
        [0, 0, 0, 1]])

    C_prime = np.transpose(C)

    return C @ transformation_matrix @ C_prime 

Which I derived from here

This code, however, isn't working. I'm not sure if it's because the code itself is incorrect, or if my mapping is incorrect. Any help would be appreciated!

2

There are 2 best solutions below

4
JamesParrott On

Is there a simple sign error? In the link, the middle two non-zero entries are +1 and -1. Your matrix has both equal to -1.

4x4 matrix from the link

0
John Aven On

So @JamesParrott was correct in that you do have a mathematical error. The concept that you are missing is not that you are mapping coordinates simply applying a sign change or such. I think we need more context here. Are you applying a rotation matrix (as link indicated)

Also the link you provided is a discussion around a 4D rotation matrix (x,y,z,w) - I am guessing your application would be a 3x3 Matrix.

Furthermore, I am gonna link here - don't kill this post, haha. https://en.wikipedia.org/wiki/Rotation_matrix

That link is on Rotation Matrices - a standard mathematical concept you can find any number of links on.

If you are simply wanting to map the space (X,Y,Z) to (X, -Z, -Y), then your matrix should be [[1 0 0], [0, 0, -1], [0, -1, 0]]

To understand why this works, do the following matrix multiplication (I recommend using Wolfram Alpha for simple understandings like this)

Transformation