How to apply affine transformation (4x4 matrix) to ndgrid / meshgrid results?

M - an affine transformation matrix, which can transform coordinates from one coordinate system to another as follows:

v_old = [ x_old y_old z_old 1];
v_new = M * v_old;
% v_new contains [ x_new y_new z_new 1 ]

Now I have the coordinates on the ndgrid / meshgrid form:

[ X_old Y_old Z_old ] = ndgrid( 0:15 ); % for instance

How can I convert them to X_newetc.

I could do this with three for-loops ( X_old(i,j,k)matches x_oldabove), but there should be a better solution.

+3
source share
1 answer

You just need to change the values ​​so that each point has four coordinates on the same line:

result = [X_old(:) Y_old(:) Z_old(:) ones(numel(X_old),1)] * M;

Each line resultgives new coordinates for each point.

, , A times matrix B A B.

, (- ),

result = X_old(:)*M(1,:) + Y_old(:)*M(2,:) + Z_old(:)*M(3,:) + ones(numel(X_old),1)*M(4,:);

, , , X_old, Y_old, Z_old, :

s = size(X_old);
X_new = reshape(result(:,1), s);
Y_new = reshape(result(:,2), s);
Z_new = reshape(result(:,3), s);
+2

All Articles