Azzera filtri
Azzera filtri

Tilted 3D model orientation

11 visualizzazioni (ultimi 30 giorni)
Erin
Erin il 30 Lug 2023
Modificato: Matt J il 1 Ago 2023
The code I am writing opens an interface into which I can upload 3D models. The easiest way to describe the models is as stick-shaped, so much longer on one plane that the others. I am trying to write a script that will orientate each 3D model however, I am struggling a bit because the models are uploaded in different original orientations. So I need to write a script that will put all the models into the same general position on x, y, and z axes before the rest of the orientation program can run. Ideally being parallel to the x-axis. I've put the script that I was trying to use below but I'm not sure if its the best solution.
Any help would be greatly appreciated!
function [x, y, z] = correctZTilt(oc)
% oc - original coordinates
%Find centroid of the model
centroid = mean(oc, 1);
%Step 1: Align the centroid with the x-z plane
angle_x_tilt = atan2(centroid(2), centroid(3)); % Angle to align the centroid with the x-z plane
R_align_xz = [1, 0, 0;
0, cos(angle_x_tilt), -sin(angle_x_tilt);
0, sin(angle_x_tilt), cos(angle_x_tilt)];
aligned_oc = (R_align_xz * oc')';
%Step 2: Align the primary axis of the model with the x-axis
% Assuming that the primary axis of the model is the z-axis
angle_y_tilt = -atan2(centroid(1), centroid(3)); % Angle to align the centroid with the x-axis
R_align_x = [cos(angle_y_tilt), 0, sin(angle_y_tilt);
0, 1, 0;
-sin(angle_y_tilt), 0, cos(angle_y_tilt)];
rotated_oc = (R_align_x * aligned_oc')';
%Extract the coordinates after correction
x = rotated_oc(:, 1);
y = rotated_oc(:, 2);
z = rotated_oc(:, 3);
end
% Sample 3D model represented by points
oc = [1, 2, 3;
4, 5, 6;
7, 8, 9;
% Add more points here...
];
% Correct the tilt of the model
[x, y, z] = correctZTilt(oc);
  2 Commenti
Bruno Luong
Bruno Luong il 30 Lug 2023
Modificato: Bruno Luong il 30 Lug 2023
" but I'm not sure if its the best solution."
First question: Does your function aligns correctly the centroid with x-axis? Meaning does it perform as your expectation?
Beside that I see clearly a problem.
An object orientation has 3 DOFs. Your code estime 2DOFs to align the centroid with x-axis. So there is 1 DOF that is not estimated just by align the centroid with x-axis.
Also rotation about the origin seems kind of arbitrary to me.
Bruno Luong
Bruno Luong il 31 Lug 2023
Modificato: Bruno Luong il 31 Lug 2023
I answer my own question "First question: Does your function aligns correctly the centroid with x-axis? "
The answer is No; I fix the original code to achieve that goal as see below.
My other comments on DOFs still valid. Align centroid with x-axis is not enough to resolve and restore orientation. The (new) points can still freely rotate about the x-axis, and the centroid remains on this axis. This is the missing DOF.
% Sample 3D model represented by points
oc = [1, 2, 3;
4, 5, 6;
7, 8, 9;
% Add more points here...
];
% Correct the tilt of the model
[x, y, z] = correctZTilt(oc);
% Check if the new coordinates has centroid aligne with x-axis
mean(x)
ans = 8.7750
mean(y)
ans = -3.7007e-16
mean(z)
ans = 5.5511e-16
function [x, y, z] = correctZTilt(oc)
% oc - original coordinates
%Find centroid of the model
centroid = mean(oc, 1);
%Step 1: Align the centroid with the x-z plane
angle_x_tilt = atan2(centroid(2), centroid(3)); % Angle to align the centroid with the x-z plane
R_align_xz = [1, 0, 0;
0, cos(angle_x_tilt), -sin(angle_x_tilt);
0, sin(angle_x_tilt), cos(angle_x_tilt)];
aligned_oc = (R_align_xz * oc')';
%Step 2: Align the primary axis of the model with the x-axis
% Assuming that the primary axis of the model is the z-axis
% These two lines are modified by Bruno Luong
aligned_centroid = (R_align_xz * centroid')';
angle_y_tilt = atan2(aligned_centroid(3), aligned_centroid(1)); % Angle to align the NE centroid with the x-axis
R_align_x = [cos(angle_y_tilt), 0, sin(angle_y_tilt);
0, 1, 0;
-sin(angle_y_tilt), 0, cos(angle_y_tilt)];
rotated_oc = (R_align_x * aligned_oc')';
%Extract the coordinates after correction
x = rotated_oc(:, 1);
y = rotated_oc(:, 2);
z = rotated_oc(:, 3);
end

Accedi per commentare.

Risposte (2)

Star Strider
Star Strider il 30 Lug 2023
The rotate function could be worth exploring.
Example —
[X,Y,Z] = peaks(50);
figure
hs1 = surf(X,Y,Z+10);
hold on
hs2 = surf(X,Y,Z);
hs3 = surf(X,Y,Z-15);
hold off
grid on
rotate(hs1, [1 1 0], 30)
rotate(hs2, [0 1 0.5], 30)
rotate(hs3, [0.5 0 1], 30)
Once the surf plots are formed, they can be shifted or rotated however you want. (It will likely take a bit of experimentation to get them the way you want them.) The (1x3) vectors also do not need to be limited to values of 0 or 1, since they can also be decimal fractions (as illustrated).
.

Chhayank Srivastava
Chhayank Srivastava il 31 Lug 2023
Modificato: Chhayank Srivastava il 31 Lug 2023
Hi, you can look into MATLAB view command
https://www.mathworks.com/help/pde/ug/pde.discretegeometry.rotate.html
https://www.mathworks.com/help/robotics/ref/eul2rotm.html
You can use a rotate and eul2rotm command, followed by view command.
function [x, y, z] = correctZTilt(oc)
% oc - original coordinates
%Find centroid of the model
centroid = mean(oc, 1);
%Step 1: Align the centroid with the x-z plane
angle_x_tilt = atan2(centroid(2), centroid(3));
rotation_matrix = eul2rotm([angle_x_tilt * 180 / pi,0,0]) %rotation matrix
point_new = oc + [oc-centroid]* rotation_matrix
%Extract the coordinates after correction
x = point_new(:, 1);
y = point_new(:, 2);
z = point_new(:, 3);
end
% Sample 3D model represented by points
oc = [1, 2, 3;
4, 5, 6;
7, 8, 9;
% Add more points here...
];
% Correct the tilt of the model
[x, y, z] = correctZTilt(oc);
% View
view([1,1,1])
Let me know if this works out for you.

Categorie

Scopri di più su Graphics Performance in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by