.. _homogeneous: Homogeneous Matrix ===================== .. include:: ../../replace.txt Geometric translation is often added to the rotation matrix to make a matrix that is called the *homogeneous transformation matrix*. The translation coordinates (:math:`x_t` and :math:`y_t`) are added in a third column. A third row is also added so that the resulting matrix is square, . .. math:: \mathbf{T}(\theta, x_t, y_t) = \spalignmat[r]{\cos\theta, -\sin\theta, x_t; \sin\theta, \cos\theta, y_t; 0, 0, 1} Homogeneous rotation alone is given by the matrix. .. math:: \mathbf{R}(\theta) = \spalignmat[r]{\cos\theta, -\sin\theta, 0; \sin\theta, \cos\theta, 0; 0, 0, 1} Homogeneous translation alone is given by the matrix. .. math:: \mathbf{T}_t(x_t, y_t) = \spalignmat[r]{1, 0, x_t; 0, 1, y_t; 0, 0, 1} The combined rotation and translation homogeneous transformation matrix can be found by matrix multiplication. .. math:: \mathbf{T}(\theta, x_t, y_t) = \mathbf{T}_t(x_t, y_t) \, \mathbf{R}(\theta) Applying Transformations to a Point ======================================= When applied to a point, the homogeneous transformation matrix uses rotation followed by translation in the original coordinate frame. It is not translation followed by rotation. .. topic:: Why do we say that rotation comes before translation? The rotated and translated point is given by the product of two matrices and the original point location. .. math:: \mathbf{p'} = \mathbf{T}_t(x_t, y_t) \, \mathbf{R}(\theta) \, \mathbf{p} :label: eq-pointTransform The left to right reading of :eq:`eq-pointTransform` might lead one to say that the translation occurs before the rotation. We say that rotation occurs first because the rotation matrix is immediately left of the point location. So it is the multiplication of the point with the rotation matrix that moves the point first. It is the complete product that gives us the desired result and it does not matter which multiplication is computed first. Since multiplication of matrices is not commutative, the rotation matrix must be immediately left of the point location to get the desired result. .. math:: \mathbf{p'} = \underbrace{\mathbf{T}_t(x_t, y_t) \, \overbrace{\mathbf{R}(\theta)\, \mathbf{p}}^{\text{first product}}}_{\text{second product}} .. _fig-pointTransform: .. figure:: pointTransform.png :align: center :width: 40% A transformation applied to a point, :math:`\mathbf{p'} = \mathbf{T_t\,R_{\theta}\,p}`, rotates and translates the point relative to the coordinate frame. In the following |M| session, we define matrix and translation matrices using homogeneous coordinates. We compound the two with matrix multiplication and apply the compound transformation matrix to a point. The rotation and translation matrices are constructed with a sequence of commands. `Peter Corke's Spatial Math Toolbox `_ provides functions that expedite the task. Depending on your interests, you might also be interested in his toolboxes for robotics and machine vision. :: >> % Rotation matrix >> theta = pi/3; >> R2 = [cos(theta) -sin(theta); sin(theta) cos(theta)] R2 = 0.5000 -0.8660 0.8660 0.5000 >> R = [R2 [0; 0]; [0 0 1]] % homogeneous coordinates R = 0.5000 -0.8660 0 0.8660 0.5000 0 0 0 1.0000 >> % translation matrix >> xt = 2; yt = 1; >> tr = eye(3); tr(1:2,3) = [xt; yt] tr = 1 0 2 0 1 1 0 0 1 >> % transformation matrix, rotation (pi/3) and translation (2, 1) >> T = tr*R T = 0.5000 -0.8660 2.0000 0.8660 0.5000 1.0000 0 0 1.0000 >> % Define a point at (1, 1) >> p = [1;1]; % Cartesian coordinates >> p1 = [p;1] % Homogeneous coordinates p1 = 1 1 1 >> % transformation of p >> p2 = T*p1 p2 = 1.6340 2.3660 1.0000 >> p_new = p2(1:2) % Cartesian coordinates p_new = 1.6340 2.3660 We can also apply the transformation in Cartesian coordinates, but it is not a simple matrix multiplication as it is in homogeneous coordinates. We rotate the point and then add the translation to the coordinates. :: >> R2*p + [xt; yt] ans = 1.6340 2.3660