簡體   English   中英

旋轉和處理MATLAB 3D對象

[英]Rotating and working on MATLAB 3D objects

當坐標為MATLAB樣式(X,Y和Z保留在不同的數組中)時,如何圍繞三個軸旋轉3D對象。

此代碼是一個開始。 我想我已經找到了旋轉矩陣 (對於pi / 2繞x旋轉),在這里它稱為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);這樣的圖形例程h = surf(X, Y, Z); 然后使用f.ex對圖形對象進行操作。 hgtransform。

使用X,Y,Z進行翻譯和縮放很方便。-您只需按標量進行加和乘。 但是我問這個問題以了解如何旋轉。

另一方面,如果對圖形對象進行操作,則可以使用函數hgtransform。 但是您必須先創建其他對象,因為據我所知,hgtransform不能直接在圖形對象上運行。 (除了rotatex(h, angle)類的函數。F.ex,我還沒有找到對應的“ translatex(h,distance)”。這讓我感到驚訝。也許我看起來還不夠好。)

好吧,我是新來的。 任何簡單,實用的指針,如何輕松地旋轉,縮放和平移MATLAB 3D坐標/對象 (圍繞坐標系軸),都將受到贊賞。

編輯

根據下面有效的Prakhar的答案,以下是填補上述空白所需的代碼。 謝謝你,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是相同大小的列向量)

現在,您可以從rotatedCoordinates中分別獲取新的X,Y和Z坐標為rotatedCoordinates(:, 1),rotatedCoordinates(:, 2)和rotatedCoordinates(:, 3)。

編輯:當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]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM