Change of Basis

INTRODUCTION

If you need to convert a vector/matrix from a coordinate system to another one, a change of basis matrix is what you need. The easiest example is a conversion between right-handend and left-handed coordinate systems.

It has been a while since I wrote something formally sound from the math point of view, so I hope this makes sense. It does makes sense to me at least, so its job as a personal reminder is done!

GENERAL CHANGE OF BASIS FORMULA

M_B = CM_AC^{-1}

Where:

  • M_A is a matrix written based on your Source coordinate system, A
  • M_B is a matrix which will be valid in your Destination coordinate system, B
  • C is the change of basis transformation matrix from A to B

EXAMPLE:

This example describes a change of basis we had to perform to convert 3D rotations from an OpenCV Augmented Reality tracking frame (right-handed, Z forward) to Unreal Engine (left-handed, X forward).

Let’s consider a right-handed coordinate system A with:

  • +Z_A in the forward direction
  • +X_A in the right direction
  • +Y_A in the down direction

And a left-handed coordinate system B with:

  • +X_B in the forward direction
  • +Y_B in the right direction
  • +Z_B in the up direction

In our case, A : OpenCV, B: Unreal.

Using different coordinate systems is somewhat similar to speaking different languages: people speaking B and people speaking A need some arrangement in order to communicate with each other. That arrangement is the C change of basis matrix.

How do we create C? Coordinates system can be thought as consisting of forward, right/left up/down directions.

What we need to do is understand the different ways of A and B to describe those directions.

Going from A to B?

Looking at A from B‘s point of view we could say that:

  • +X_A corresponds to +Y_B
  • +Y_A corresponds to -Z_B
  • +Z_A corresponds to +X_B

This translates in the following matrix (columns “represent” X, Y, Z):


C_{AB} = \begin{bmatrix} 0 & 0 & 1\\ 1 & 0 & 0\\ 0 & -1 & 0 \end{bmatrix}

Now that we know this, we could for example go from a rotation matrix R_A to R_B by doing the following:



R_B = C_{AB}R_AC_{AB}^{-1}

Going from B to A?

Looking at B from A ‘s point of view we could say that:

  • +X_B corresponds to +Z_A
  • +Y_B corresponds to +X_A
  • +Z_B corresponds to -Y_A

C_{BA} = \begin{bmatrix} 0 & 1 & 0\\ 0 & 0 & -1\\ 1 & 0 & 0 \end{bmatrix}

We should obtain the same result by inverting C_{AB}

Side note: transposing and inverting these matrices is the same, since they are orthogonal.

Credits to

dario mazzanti, 2021

Up ↑