Non-mathematician looking for a complete example of representing points in one 3D coordinate system as points in another

355 Views Asked by At

I am going crazy researching trigonometry, vectors, matrices, quaternions, Euler angles, etc. etc. in pursuit of an efficient, "best practice" solution to the problem of representing points in one 3D coordinate system as points in another 3D coordinate system. There are plenty of answers to this question already, but none that I can use because they are too high level, assuming the reader will know how to flesh out the described methods. So I'm looking for a specific, step by step, actual calculation that demonstrates how the job is done. I hope this approach will also help others who may be as confused by their research as I am.

Let's say we have object A in its own "right hand rule" 3D XYZ coordinate system, "floating in space" at no particular location or coordinates, with one point P defined in this object, at X=2, Y=4, Z=7. Let's say we have object B with its own 3D XYZ coordinate system, and this object initially was in the same position and the same orientation as A, their coordinate systems matched (and therefore P would have the same XYZ values in B as in A). But now B is separated from A, in A's coordinates, 10 units on X, 5 units on Y, and 4 on Z. B is additionally rotated, in order and in its own coordinates, 45 degrees on the Z axis, 225 degrees on the Y, and 3 on the X. What are the XYZ values of P in B's coordinates? Please provide the complete math that the reader may reproduce either by hand or programming.

I will be grateful for an answer based on any method, but it would be super cool if a number of answers are posted using different methods.

4

There are 4 best solutions below

1
On BEST ANSWER

Let $p_0$ be the original point in frame A, in this problem, $ p_0 = (2, 4, 7) $.

Now, you shift frame B by $(10, 5, 4)$, so the coordinates in frame B after this origin shift will be $(-8, -1, 3) $. Next you want to rotate frame B, three rotations relative to its own axes, first about the $z$ axis by $45^\circ$, then about the new $y$ axis by $225^\circ$, then about the new $x$ axis by $30^\circ$. The overall rotation matrix is given by

$ R = R_z(45^\circ) R_y(225^\circ) R_x(30^\circ) $

The right hand side of the above equation are straight forward to compute, as follows,

$ R_x(30^\circ) = \begin{bmatrix} 1 && 0 && 0 \\ 0 && \cos 30^\circ && -\sin 30^\circ \\ 0 && \sin 30^\circ && \cos 30^\circ \end{bmatrix}$

$ R_y(225^\circ) = \begin{bmatrix} \cos 225^\circ && 0 && \sin 225^\circ \\ 0 && 1 && 0 \\ -\sin 225^\circ && 0 && \cos 225^\circ \end{bmatrix}$

$ R_z(45^\circ) = \begin{bmatrix} \cos 45^\circ && - \sin 45^\circ && 0 \\ \sin 45^\circ && \cos 45^\circ && 0 \\ 0 && 0 && 1 \end{bmatrix} $

Multiplying the above three matrices in the given order gives the rotation matrix $R$.

You can verify that $R$ is given by

$ R = \begin{bmatrix} -0.5 && -0.862372436 && -0.079459311 \\ -0.5 && 0.362372436 && -0.786566092 \\ 0.707106781 && -0.353553391 && -0.612372436 \end{bmatrix}$

Now the old coordinates $P $ (before rotation) and the new coordinates $P'$ (after rotation) are related by

$ P = R P' $

Hence,

$ P' = R^{-1} P = R^T P = R^T [-8, -1, 3]^T $

From this you can compute the coordinates of the original point $[2, 4, 7]^T$, in the final shifted/rotated frame B.

You can verify that the final coordinates in frame $B$ as given by the above formula are

$ P' = \begin{bmatrix} 6.621320344 \\ 5.475946878 \\ -0.414876724 \end{bmatrix} $

6
On

So, I will not do explicit calculations, but I will tell you how to do that.

You said $B$ was initially overlapping with $A$, but then you translated and rotated it. In mathematical terms, this means that there is a function that maps any point of $A$ to a point of $B$ in a 1-to-1 correspondence. It is well known that a roto-translation is an affine map. This means that, in the coordinates of $A$, the map that we are looking for is of the form

$$ A\ni\left(\begin{gathered}{\alpha\\\beta\\\gamma}\end{gathered}\right)\mapsto M\cdot \left(\begin{gathered}{\alpha\\\beta\\\gamma}\end{gathered}\right)+ v, $$ where $M$ is a 3x3 matrix and $v$ is a vector (you can think of $M$ as representing the rotation and $v$ the translation). Let’s call this map $L$.

Now, given the map, we want to know how the coordinates of a given point change when passing from $A$’s to $B$’s coordinates. As a notation, I will write $Q=[a,b,c]_A$ to denote a point $Q$ that has coordinates $a,b,c$ in $A$’s coordinate system (analogously for $B$). First, as an example, let’s consider the point $O=[0,0,0]_A$ in $A$’s coordinates. Consider $O’=L(O)$: it is clear that $O’=[0,0,0]_B$, because the map $L$ is the one that “transports” $A$ over $B$ (think carefully about this). Moreover, looking at the map $L$, we also have $O’=[v_1,v_2,v_3]_A$, where $v_1,v_2,v_3$ are the components of $v$. This is a way to link the coordinates of $A$ and $B$ through the map $L$.

In the same way, I can take an arbitrary point $Q=[p,q,r]_A$, consider $Q’:=L(Q)$, and infere that $Q’=[p,q,r]_B=[p’,q’,r’]_A$, where

$$ \left(\begin{gathered}{p’\\q’\\r’}\end{gathered}\right)=M\cdot \left(\begin{gathered}{p\\q\\r}\end{gathered}\right)+ v. $$

So we can find the $A$-coordinates of $Q’$ given the $B$-coordinates, but we would like to do the opposite, so we simply need to invert the above relation:

$$ \left(\begin{gathered}{p\\q\\r}\end{gathered}\right)=M^{-1}\cdot \left(\left(\begin{gathered}{p’\\q’\\r’}\end{gathered}\right)- v\right). $$

The above is about the coordinates of $Q’$, but since the map $L$ is bijective, $Q’$ is essentially an arbitrary point in 3D space, so we can take $P=Q’$. We have thus found the general rule to change the coordinates. If you can write your roto-translation $L$ in the form above, you know how the coordinates change between the two coordinate systems.


Edit. I said nothing on how to write your map $L$ in the form above. In particular, I am assuming you know how to write down a rotation matrix in 3D that satisfies the properties that you want. If you have doubts about this, you should maybe try to read from a book on linear algebra or ask another question. Moreover, note that if you want to implement this in a program, you don’t necessarily need to brutally invert $M$, as inverting rotation matrices by hand is generally easier than inverting arbitrary matrices if you write them in a nice way.

2
On

First, think that when you have a transformation $T$ of a space into itself you can interpret it as a change in the point of view. If $T$ transform points into points, $T^{-1}$ transform coordinates of points with respect to the first point of view into the coordinates with respect to the second point of view. Maybe you should think a lot on this, or just believe it, is up to you.

Now, we are going to describe the transformation $T$ that sends your object $A$ to your object $B$ (assuming that is a translation followed by several rotations expressed in coordinates with respect to the first point of view). We will keep track of four points: $O=(0,0,0),e_1=(1,0,0),e_2=(0,1,0),e_3=(0,0,1)$. Observe that we can consider, in this case, that we perform firstly the rotations and finally the translation (again, this is not trivial, and it must be thought).

First rotation $R_1$
Sends $$ \begin{array}{l} e_1\mapsto e_1':=(\sqrt{2}/2,\sqrt{2}/2,0)\\ e_2\mapsto e_2':=(-\sqrt{2}/2,\sqrt{2}/2,0)\\ e_3 \mapsto e_3':=(0,0,1) \end{array} $$ so this transformation is given by the matrix $$ R_1:=\begin{pmatrix} \sqrt{2}/2 & -\sqrt{2}/2 &0\\ \sqrt{2}/2 & \sqrt{2}/2 &0\\ 0 & 0& 1\\ \end{pmatrix} $$

Second rotation $R_2$
Proceed in the same way, thinking where are you sending $e_1,e_2,e_3$ with the rotation of 225º around the new axis $Y$. This is more easily done by thinking where are you sending $e_1',e_2',e_3'$ to obtain the matrix $\tilde{R_2}$ with respect the new basis and then changing the basis. That is $$ \begin{array}{l} e_1'\mapsto e_1'':=(-\sqrt{2}/2,0,\sqrt{2}/2)\\ e_2'\mapsto e_2'':=(0,1,0)\\ e_3' \mapsto e_3'':=(-\sqrt{2}/2,0,-\sqrt{2}/2) \end{array} $$ and $$ \tilde{R}_2:=\begin{pmatrix} -\sqrt{2}/2 & 0 &-\sqrt{2}/2\\ 0 & 1 &0\\ \sqrt{2}/2 & 0& -\sqrt{2}/2\\ \end{pmatrix} $$

Now you compute the matrix with respect to $e_1,e_2,e_3$: $$ R_2=R_1 \cdot \tilde{R}_2 \cdot R_1^{-1} $$

Third rotation $R_3$
You proceed in the same way with $e_1'',e_2'',e_3''$: think of their images by the rotation to obtain $\tilde{R}_3$ (do it by yourself) and then change basis to express this last rotation with respect to $e_1,e_2,e_3$. It will be something like $$ R_3=C\cdot \tilde{R}_3\cdot C^{-1} $$ where $C$ is the matrix of change of basis, that in this case is $C=R_2 \cdot R_1=R_1\cdot \tilde{R}_2$, and so $$ R_3=R_1\tilde{R}_2 \tilde{R}_3 \tilde{R}_2^{-1} R_1^{-1} $$

The full rotation
It will be $R=R_3 R_2 R_1$ but this is $$ R=R_1 \tilde{R}_2 \tilde{R}_3 $$ so you can obtain it by matrix multiplication.

The transformation $T$
Given a point $(x,y,z)$ we have to apply the rotation and the translation, that is $$ T(x,y,z)= R\begin{pmatrix} x\\ y\\ z\\ \end{pmatrix} + \begin{pmatrix} 10\\ 5\\ 4\\ \end{pmatrix} $$

Conclusion
A point with coordinates $(\bar{x},\bar{y},\bar{z})$ in $A$ will have coordinates in $B$ given by $$ R^{-1}\cdot \begin{pmatrix} \bar{x}-10\\ \bar{y}-5\\ \bar{z}-4\\ \end{pmatrix} $$

For example, the arrival point of $B$, that is, the point with coordinates $(10,5,4)$ in $A$, has coordinates

$$ R^{-1}\cdot \begin{pmatrix} 10-10\\ 5-5\\ 4-4\\ \end{pmatrix}= R^{-1}\cdot \begin{pmatrix} 0\\ 0\\ 0\\ \end{pmatrix}= \begin{pmatrix} 0\\ 0\\ 0\\ \end{pmatrix} $$ with respect to $B$.

1
On

First, we need some tools from Linear Algebra.

In 3D, we can use 4×4 matrices to describe any rotation, translation, and scaling: $$\mathbf{T} = \left[ \begin{matrix} u_x & v_x & w_x & t_x \\ u_y & v_y & w_y & t_y \\ u_z & v_z & w_z & t_z \\ 0 & 0 & 0 & 1 \\ \end{matrix} \right] \tag{1}\label{1}$$ Such transformation matrices $\mathbf{T}$ consist of four vectors: three direction vectors $\vec{u}$, $\vec{v}$, $\vec{w}$, and one translation (position) vector $\vec{t}$, $$\vec{u} = \left[ \begin{matrix} u_x \\ u_y \\ u_z \\ 0 \end{matrix} \right], \quad \vec{v} = \left[ \begin{matrix} v_x \\ v_y \\ v_z \\ 0 \end{matrix} \right], \quad \vec{w} = \left[ \begin{matrix} w_x \\ w_y \\ w_z \\ 0 \end{matrix} \right], \quad \vec{t} = \left[ \begin{matrix} t_x \\ t_y \\ t_z \\ 1 \end{matrix} \right] \tag{2}\label{2}$$ When applied to a position vector $\vec{p}$, $$\vec{p} = \left[ \begin{matrix} x \\ y \\ z \\ 1 \end{matrix} \right ], \quad \vec{p}^\prime = \mathbf{T} \vec{p} = x \vec{u} + y \vec{v} + z \vec{w} + \vec{t} \tag{3a}\label{3a}$$ and when applied to a direction vector $\vec{d}$, $$\vec{d} = \left[ \begin{matrix} \chi \\ \mu \\ \zeta \\ 0 \end{matrix} \right ], \quad \vec{d}^\prime = \mathbf{T} \vec{d} = \chi \vec{u} + \mu \vec{v} + \zeta \vec{w} \tag{3b}\label{3b}$$

In simple terms, $\vec{u}$ represents unit $x$ axis vector, $\vec{v}$ unit $y$ axis vector, and $\vec{w}$ unit $z$ axis vector, in the new coordinate system. (By "unit", I mean a vector of length $1$ in the new coordinate system.) When applied, $\vec{t}$ is the location of the origin of the new coordinate system. All four vectors are expressed in old coordinate system coordinates.

(Direction and position vectors only differ by whether translation is applied or not. There is no need to consider them "different" in any fundamental way, it is just that in practice, this division is useful. For example, polyhedron vertices are position vectors and translation is applied to them, but their face normal vectors are direction vectors and translation is not applied.)

To construct a transformation $\mathbf{T}$, usually you treat the direction vector 3×3 sub-matrix (upper left corner) separately from the translation vector $\vec{t}$. For simplicity, let's call that upper left 3×3 sub-matrix the "rotation part" of the transformation.

You could use Euler or Tait-Bryan angles, but I wholeheartedly advise against it, because there are many incompatible conventions, and they all suffer from gimbal lock. Instead, orientations are best described using versors AKA unit quaternions or bivectors – the actual arithmetic operations end up being the same for the two. Closely related, you can also use an arbitrary unit axis vector (of length 1) and a rotation around that axis, i.e. axis-angle representation, to create the rotation part of the transformation matrix $\mathbf{T}$.

However, we also have additional tools on how we can manipulate the transformations directly.

  1. $\mathbf{T}^{-1}$, matrix inverse of $\mathbf{T}$, is the inverse transformation.

    If we have $\mathbf{T}$ that transforms the coordinate system from A to B, then $\mathbf{T}^{-1}$ transforms from B to A, as long as both A and B are fully 3D coordinate systems. (In other words, that the transformation does not project the 3D space onto a plane, line, or a point.)

    Using the notation in $\eqref{1}$ and $\eqref{2}$, $$\mathbf{T}^{-1} = \left[ \begin{matrix} \frac{v_y w_z - v_z w_y}{D} & \frac{v_z w_x - v_x w_z}{D} & \frac{v_x w_y - v_y w_x}{D} & \frac{t_x (v_z w_y - v_y w_z) + t_y (v_x w_z - v_z w_x) + t_z (v_y w_x - v_x w_y)}{D} \\ \frac{u_z w_y - u_y w_z}{D} & \frac{u_x w_z - u_z w_x}{D} & \frac{u_y w_x - u_x w_y}{D} & \frac{t_x (u_y w_z - u_z w_y) + t_y (u_z w_x - u_x w_z) + t_z (u_x w_y - u_y w_x)}{D} \\ \frac{u_y v_z - u_z v_y}{D} & \frac{u_z v_x - u_x v_z}{D} & \frac{u_x v_y - u_y v_x}{D} & \frac{t_x (u_z v_y - u_y v_z) + t_y (u_x v_z - u_z v_x) + t_z (u_y v_x - t_z u_x)}{D} \\ 0 & 0 & 0 & 1 \\ \end{matrix} \right] \tag{4a}\label{4a}$$ where $$D = \vec{u} \cdot \vec{v} \times \vec{w} = u_x (v_y w_z - v_z w_y) + u_y (v_z w_x - v_x w_z) + u_z (v_x w_y - v_y w_x) \tag{4b}\label{4b}$$ is the volume of the parallelepiped formed by the three vectors. When $D = 0$, the three vectors are in the same plane or in the same direction (i.e., $\mathbf{T}$ transforms the coordinate system into a plane, line, or a point), and the transformation cannot be inverted.

  2. Any number of transformations (including inverse transformations) can be chained by multiplying the transformation matrices. The transformations are listed rightmost first, i.e. first transformation rightmost, and last transformation leftmost.

    For example, if we have any two transformations, $\mathbf{T}_1$ and $\mathbf{T}_2$, then transforming from the first to the second is $$\mathbf{T}_{12} = \mathbf{T}_2 \mathbf{T}_1^{-1}$$ The inverse, transforming from the second to the first, is $$\mathbf{T}_{21} = \mathbf{T}_{12}^{-1} = \mathbf{T}_1 \mathbf{T}_2^{-1}$$

  3. Identity matrix $\mathbf{I}$ corresponds to "no transformation":

    $$\mathbf{T}_I = \mathbf{I} = \left[ \begin{matrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{matrix} \right]$$ It is its own inverse, $\mathbf{T}_I^{-1} = \mathbf{T}_I$.


With the above tools, we can trivially work out the stated problem. First, we would construct the transformation matrix corresponding to $\mathbf{T}_A$. Because we define $\mathbf{T}_B$ in terms of A, we'll set $\mathbf{T}_A = \mathbf{T}_I$, the identity matrix.

Next, we construct the transformation matrix corresponding to $\mathbf{T}_B$. Because OP defined it in terms of Euler/Tait-Bryan rotations, I won't do it numerically, as I always use versors or bivectors instead. If you want to do it numerically, do the rotation part first, then add the translation part, to get the full $\mathbf{T}_B$.

To get the transformation matrix from A to B, we calculate $\mathbf{T} = \mathbf{T}_B \mathbf{T}_A^{-1} = \mathbf{T}_B$ because $\mathbf{T}_A$ is the identity matrix. Given any direction or position vector $\vec{p}$ in A's coordinate system, $\mathbf{T}\vec{p}$ will yield the same in B's coordinate system. Similarly, given a direction or position vector $\vec{q}$ in B's coordinate system, $\mathbf{T}^{-1}\vec{q}$ will yield the same in A's coordinate system.