旋转和处理 MATLAB 3D 对象



当坐标为 MATLAB 样式(X、Y 和 Z 保存在不同的数组中)时,如何围绕三个轴中的每一个旋转 3D 对象。

此代码是一个开始。我想我已经找到了旋转矩阵(对于围绕 x 的 pi/2 旋转),这里称为 rotX90_2。但是rotX90_2应该如何在X,Y,Z上操作呢?

[X,Y,Z] = cylinder;
% rotX90_1 = makehgtform('xrotate',pi/2) gives
rotX90_1 = ...
     [1     0     0     0;
      0     0    -1     0;
      0     1     0     0;
      0     0     0     1];
rotX90_2 = rotX90_1(1:3, 1:3);
% Here rotX90_2 should operate on [X,Y,Z] in order to ...
% rotate it 90 degrees around x, but how is this done?
% == What code should be put here to rotate the cylinder? ==
surf(X,Y,Z);

我刚刚开始使用 MATLAB。据我了解,操作 3D 图形的基本方法是像这里一样在 X、Y、Z 上进行操作,或者您可以先运行像 h = surf(X, Y, Z); 这样的图形例程,然后使用 f.ex 对图形对象进行操作。HGTRANSFORM。

使用 X、Y、Z 进行平移和缩放很方便。 - 您只需加乘标量即可。但我问这个问题是为了了解如何旋转。

另一方面,如果对图形对象进行操作,则可以使用函数 hgtransform。但是你必须首先创建其他对象,因为据我所知,hgtransform并不直接对图形对象进行操作。(rotatex(h, angle) 等函数除外。F.ex,我没有找到相应的"翻译(h,距离)"。这让我很惊讶。也许我看起来不够好。

好的,我是新手。任何简单实用的指针,如何轻松旋转、缩放和平移 MATLAB 3D 坐标/对象(围绕坐标系轴)将不胜感激。

编辑

根据Prakhar下面的答案,填补上述空白所需的代码如下。谢谢你,普拉哈尔。

[row, col] = size(X);
coordinates = [reshape(X, [row*col, 1]), reshape(Y, [row*col, 1]), reshape(Z, [row*col, 1])];
rC = coordinates * rotX90_2;
X = reshape(rC(:, 1), [row, col]);
Y = reshape(rC(:, 2), [row, col]);
Z = reshape(rC(:, 3), [row, col]);

假设 R 是合适的 3x3 旋转矩阵。

coordinates = [X Y Z];
rotatedCoordinates = coordinates * R;

(假设 X、Y 和 Z 是相同大小的列向量)

现在,您可以从旋转坐标分别作为旋转坐标(:,1),旋转坐标(:,2)和

旋转坐标(:,3)获取新的X,Y和Z坐标。

编辑:当X,Y,Z是2D矩阵时,另一种选择:

[X, Y, Z] = cylinder;
[row, col] = size(X);
coordinates = [reshape(X, [row*col, 1]), reshape(Y, [row*col, 1]), reshape(Z, [row*col, 1])];
rC = coordinates*R;
Xn = reshape(rC(:, 1), [row, col]);
Yn = reshape(rC(:, 2), [row, col]);
Zn = reshape(rC(:, 3), [row, col]);

最新更新